github.com/fxsjy/go-ethereum@v1.8.4-0.20180410143526-2e247705cd27/les/handler.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package les implements the Light Ethereum Subprotocol.
    18  package les
    19  
    20  import (
    21  	"encoding/binary"
    22  	"errors"
    23  	"fmt"
    24  	"math/big"
    25  	"net"
    26  	"sync"
    27  	"time"
    28  
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/consensus"
    31  	"github.com/ethereum/go-ethereum/core"
    32  	"github.com/ethereum/go-ethereum/core/state"
    33  	"github.com/ethereum/go-ethereum/core/types"
    34  	"github.com/ethereum/go-ethereum/eth/downloader"
    35  	"github.com/ethereum/go-ethereum/ethdb"
    36  	"github.com/ethereum/go-ethereum/event"
    37  	"github.com/ethereum/go-ethereum/light"
    38  	"github.com/ethereum/go-ethereum/log"
    39  	"github.com/ethereum/go-ethereum/p2p"
    40  	"github.com/ethereum/go-ethereum/p2p/discover"
    41  	"github.com/ethereum/go-ethereum/p2p/discv5"
    42  	"github.com/ethereum/go-ethereum/params"
    43  	"github.com/ethereum/go-ethereum/rlp"
    44  	"github.com/ethereum/go-ethereum/trie"
    45  )
    46  
    47  const (
    48  	softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data.
    49  	estHeaderRlpSize  = 500             // Approximate size of an RLP encoded block header
    50  
    51  	ethVersion = 63 // equivalent eth version for the downloader
    52  
    53  	MaxHeaderFetch           = 192 // Amount of block headers to be fetched per retrieval request
    54  	MaxBodyFetch             = 32  // Amount of block bodies to be fetched per retrieval request
    55  	MaxReceiptFetch          = 128 // Amount of transaction receipts to allow fetching per request
    56  	MaxCodeFetch             = 64  // Amount of contract codes to allow fetching per request
    57  	MaxProofsFetch           = 64  // Amount of merkle proofs to be fetched per retrieval request
    58  	MaxHelperTrieProofsFetch = 64  // Amount of merkle proofs to be fetched per retrieval request
    59  	MaxTxSend                = 64  // Amount of transactions to be send per request
    60  	MaxTxStatus              = 256 // Amount of transactions to queried per request
    61  
    62  	disableClientRemovePeer = false
    63  )
    64  
    65  // errIncompatibleConfig is returned if the requested protocols and configs are
    66  // not compatible (low protocol version restrictions and high requirements).
    67  var errIncompatibleConfig = errors.New("incompatible configuration")
    68  
    69  func errResp(code errCode, format string, v ...interface{}) error {
    70  	return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
    71  }
    72  
    73  type BlockChain interface {
    74  	Config() *params.ChainConfig
    75  	HasHeader(hash common.Hash, number uint64) bool
    76  	GetHeader(hash common.Hash, number uint64) *types.Header
    77  	GetHeaderByHash(hash common.Hash) *types.Header
    78  	CurrentHeader() *types.Header
    79  	GetTd(hash common.Hash, number uint64) *big.Int
    80  	State() (*state.StateDB, error)
    81  	InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error)
    82  	Rollback(chain []common.Hash)
    83  	GetHeaderByNumber(number uint64) *types.Header
    84  	GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash
    85  	Genesis() *types.Block
    86  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    87  }
    88  
    89  type txPool interface {
    90  	AddRemotes(txs []*types.Transaction) []error
    91  	Status(hashes []common.Hash) []core.TxStatus
    92  }
    93  
    94  type ProtocolManager struct {
    95  	lightSync   bool
    96  	txpool      txPool
    97  	txrelay     *LesTxRelay
    98  	networkId   uint64
    99  	chainConfig *params.ChainConfig
   100  	blockchain  BlockChain
   101  	chainDb     ethdb.Database
   102  	odr         *LesOdr
   103  	server      *LesServer
   104  	serverPool  *serverPool
   105  	lesTopic    discv5.Topic
   106  	reqDist     *requestDistributor
   107  	retriever   *retrieveManager
   108  
   109  	downloader *downloader.Downloader
   110  	fetcher    *lightFetcher
   111  	peers      *peerSet
   112  	maxPeers   int
   113  
   114  	SubProtocols []p2p.Protocol
   115  
   116  	eventMux *event.TypeMux
   117  
   118  	// channels for fetcher, syncer, txsyncLoop
   119  	newPeerCh   chan *peer
   120  	quitSync    chan struct{}
   121  	noMorePeers chan struct{}
   122  
   123  	// wait group is used for graceful shutdowns during downloading
   124  	// and processing
   125  	wg *sync.WaitGroup
   126  }
   127  
   128  // NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
   129  // with the ethereum network.
   130  func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, protocolVersions []uint, networkId uint64, mux *event.TypeMux, engine consensus.Engine, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, txrelay *LesTxRelay, quitSync chan struct{}, wg *sync.WaitGroup) (*ProtocolManager, error) {
   131  	// Create the protocol manager with the base fields
   132  	manager := &ProtocolManager{
   133  		lightSync:   lightSync,
   134  		eventMux:    mux,
   135  		blockchain:  blockchain,
   136  		chainConfig: chainConfig,
   137  		chainDb:     chainDb,
   138  		odr:         odr,
   139  		networkId:   networkId,
   140  		txpool:      txpool,
   141  		txrelay:     txrelay,
   142  		peers:       peers,
   143  		newPeerCh:   make(chan *peer),
   144  		quitSync:    quitSync,
   145  		wg:          wg,
   146  		noMorePeers: make(chan struct{}),
   147  	}
   148  	if odr != nil {
   149  		manager.retriever = odr.retriever
   150  		manager.reqDist = odr.retriever.dist
   151  	}
   152  
   153  	// Initiate a sub-protocol for every implemented version we can handle
   154  	manager.SubProtocols = make([]p2p.Protocol, 0, len(protocolVersions))
   155  	for _, version := range protocolVersions {
   156  		// Compatible, initialize the sub-protocol
   157  		version := version // Closure for the run
   158  		manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{
   159  			Name:    "les",
   160  			Version: version,
   161  			Length:  ProtocolLengths[version],
   162  			Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
   163  				var entry *poolEntry
   164  				peer := manager.newPeer(int(version), networkId, p, rw)
   165  				if manager.serverPool != nil {
   166  					addr := p.RemoteAddr().(*net.TCPAddr)
   167  					entry = manager.serverPool.connect(peer, addr.IP, uint16(addr.Port))
   168  				}
   169  				peer.poolEntry = entry
   170  				select {
   171  				case manager.newPeerCh <- peer:
   172  					manager.wg.Add(1)
   173  					defer manager.wg.Done()
   174  					err := manager.handle(peer)
   175  					if entry != nil {
   176  						manager.serverPool.disconnect(entry)
   177  					}
   178  					return err
   179  				case <-manager.quitSync:
   180  					if entry != nil {
   181  						manager.serverPool.disconnect(entry)
   182  					}
   183  					return p2p.DiscQuitting
   184  				}
   185  			},
   186  			NodeInfo: func() interface{} {
   187  				return manager.NodeInfo()
   188  			},
   189  			PeerInfo: func(id discover.NodeID) interface{} {
   190  				if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
   191  					return p.Info()
   192  				}
   193  				return nil
   194  			},
   195  		})
   196  	}
   197  	if len(manager.SubProtocols) == 0 {
   198  		return nil, errIncompatibleConfig
   199  	}
   200  
   201  	removePeer := manager.removePeer
   202  	if disableClientRemovePeer {
   203  		removePeer = func(id string) {}
   204  	}
   205  
   206  	if lightSync {
   207  		manager.downloader = downloader.New(downloader.LightSync, chainDb, manager.eventMux, nil, blockchain, removePeer)
   208  		manager.peers.notify((*downloaderPeerNotify)(manager))
   209  		manager.fetcher = newLightFetcher(manager)
   210  	}
   211  
   212  	return manager, nil
   213  }
   214  
   215  // removePeer initiates disconnection from a peer by removing it from the peer set
   216  func (pm *ProtocolManager) removePeer(id string) {
   217  	pm.peers.Unregister(id)
   218  }
   219  
   220  func (pm *ProtocolManager) Start(maxPeers int) {
   221  	pm.maxPeers = maxPeers
   222  
   223  	if pm.lightSync {
   224  		go pm.syncer()
   225  	} else {
   226  		go func() {
   227  			for range pm.newPeerCh {
   228  			}
   229  		}()
   230  	}
   231  }
   232  
   233  func (pm *ProtocolManager) Stop() {
   234  	// Showing a log message. During download / process this could actually
   235  	// take between 5 to 10 seconds and therefor feedback is required.
   236  	log.Info("Stopping light Ethereum protocol")
   237  
   238  	// Quit the sync loop.
   239  	// After this send has completed, no new peers will be accepted.
   240  	pm.noMorePeers <- struct{}{}
   241  
   242  	close(pm.quitSync) // quits syncer, fetcher
   243  
   244  	// Disconnect existing sessions.
   245  	// This also closes the gate for any new registrations on the peer set.
   246  	// sessions which are already established but not added to pm.peers yet
   247  	// will exit when they try to register.
   248  	pm.peers.Close()
   249  
   250  	// Wait for any process action
   251  	pm.wg.Wait()
   252  
   253  	log.Info("Light Ethereum protocol stopped")
   254  }
   255  
   256  func (pm *ProtocolManager) newPeer(pv int, nv uint64, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
   257  	return newPeer(pv, nv, p, newMeteredMsgWriter(rw))
   258  }
   259  
   260  // handle is the callback invoked to manage the life cycle of a les peer. When
   261  // this function terminates, the peer is disconnected.
   262  func (pm *ProtocolManager) handle(p *peer) error {
   263  	// Ignore maxPeers if this is a trusted peer
   264  	if pm.peers.Len() >= pm.maxPeers && !p.Peer.Info().Network.Trusted {
   265  		return p2p.DiscTooManyPeers
   266  	}
   267  
   268  	p.Log().Debug("Light Ethereum peer connected", "name", p.Name())
   269  
   270  	// Execute the LES handshake
   271  	var (
   272  		genesis = pm.blockchain.Genesis()
   273  		head    = pm.blockchain.CurrentHeader()
   274  		hash    = head.Hash()
   275  		number  = head.Number.Uint64()
   276  		td      = pm.blockchain.GetTd(hash, number)
   277  	)
   278  	if err := p.Handshake(td, hash, number, genesis.Hash(), pm.server); err != nil {
   279  		p.Log().Debug("Light Ethereum handshake failed", "err", err)
   280  		return err
   281  	}
   282  	if rw, ok := p.rw.(*meteredMsgReadWriter); ok {
   283  		rw.Init(p.version)
   284  	}
   285  	// Register the peer locally
   286  	if err := pm.peers.Register(p); err != nil {
   287  		p.Log().Error("Light Ethereum peer registration failed", "err", err)
   288  		return err
   289  	}
   290  	defer func() {
   291  		if pm.server != nil && pm.server.fcManager != nil && p.fcClient != nil {
   292  			p.fcClient.Remove(pm.server.fcManager)
   293  		}
   294  		pm.removePeer(p.id)
   295  	}()
   296  	// Register the peer in the downloader. If the downloader considers it banned, we disconnect
   297  	if pm.lightSync {
   298  		p.lock.Lock()
   299  		head := p.headInfo
   300  		p.lock.Unlock()
   301  		if pm.fetcher != nil {
   302  			pm.fetcher.announce(p, head)
   303  		}
   304  
   305  		if p.poolEntry != nil {
   306  			pm.serverPool.registered(p.poolEntry)
   307  		}
   308  	}
   309  
   310  	stop := make(chan struct{})
   311  	defer close(stop)
   312  	go func() {
   313  		// new block announce loop
   314  		for {
   315  			select {
   316  			case announce := <-p.announceChn:
   317  				p.SendAnnounce(announce)
   318  			case <-stop:
   319  				return
   320  			}
   321  		}
   322  	}()
   323  
   324  	// main loop. handle incoming messages.
   325  	for {
   326  		if err := pm.handleMsg(p); err != nil {
   327  			p.Log().Debug("Light Ethereum message handling failed", "err", err)
   328  			return err
   329  		}
   330  	}
   331  }
   332  
   333  var reqList = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsV1Msg, SendTxMsg, SendTxV2Msg, GetTxStatusMsg, GetHeaderProofsMsg, GetProofsV2Msg, GetHelperTrieProofsMsg}
   334  
   335  // handleMsg is invoked whenever an inbound message is received from a remote
   336  // peer. The remote connection is torn down upon returning any error.
   337  func (pm *ProtocolManager) handleMsg(p *peer) error {
   338  	// Read the next message from the remote peer, and ensure it's fully consumed
   339  	msg, err := p.rw.ReadMsg()
   340  	if err != nil {
   341  		return err
   342  	}
   343  	p.Log().Trace("Light Ethereum message arrived", "code", msg.Code, "bytes", msg.Size)
   344  
   345  	costs := p.fcCosts[msg.Code]
   346  	reject := func(reqCnt, maxCnt uint64) bool {
   347  		if p.fcClient == nil || reqCnt > maxCnt {
   348  			return true
   349  		}
   350  		bufValue, _ := p.fcClient.AcceptRequest()
   351  		cost := costs.baseCost + reqCnt*costs.reqCost
   352  		if cost > pm.server.defParams.BufLimit {
   353  			cost = pm.server.defParams.BufLimit
   354  		}
   355  		if cost > bufValue {
   356  			recharge := time.Duration((cost - bufValue) * 1000000 / pm.server.defParams.MinRecharge)
   357  			p.Log().Error("Request came too early", "recharge", common.PrettyDuration(recharge))
   358  			return true
   359  		}
   360  		return false
   361  	}
   362  
   363  	if msg.Size > ProtocolMaxMsgSize {
   364  		return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize)
   365  	}
   366  	defer msg.Discard()
   367  
   368  	var deliverMsg *Msg
   369  
   370  	// Handle the message depending on its contents
   371  	switch msg.Code {
   372  	case StatusMsg:
   373  		p.Log().Trace("Received status message")
   374  		// Status messages should never arrive after the handshake
   375  		return errResp(ErrExtraStatusMsg, "uncontrolled status message")
   376  
   377  	// Block header query, collect the requested headers and reply
   378  	case AnnounceMsg:
   379  		p.Log().Trace("Received announce message")
   380  		if p.requestAnnounceType == announceTypeNone {
   381  			return errResp(ErrUnexpectedResponse, "")
   382  		}
   383  
   384  		var req announceData
   385  		if err := msg.Decode(&req); err != nil {
   386  			return errResp(ErrDecode, "%v: %v", msg, err)
   387  		}
   388  
   389  		if p.requestAnnounceType == announceTypeSigned {
   390  			if err := req.checkSignature(p.pubKey); err != nil {
   391  				p.Log().Trace("Invalid announcement signature", "err", err)
   392  				return err
   393  			}
   394  			p.Log().Trace("Valid announcement signature")
   395  		}
   396  
   397  		p.Log().Trace("Announce message content", "number", req.Number, "hash", req.Hash, "td", req.Td, "reorg", req.ReorgDepth)
   398  		if pm.fetcher != nil {
   399  			pm.fetcher.announce(p, &req)
   400  		}
   401  
   402  	case GetBlockHeadersMsg:
   403  		p.Log().Trace("Received block header request")
   404  		// Decode the complex header query
   405  		var req struct {
   406  			ReqID uint64
   407  			Query getBlockHeadersData
   408  		}
   409  		if err := msg.Decode(&req); err != nil {
   410  			return errResp(ErrDecode, "%v: %v", msg, err)
   411  		}
   412  
   413  		query := req.Query
   414  		if reject(query.Amount, MaxHeaderFetch) {
   415  			return errResp(ErrRequestRejected, "")
   416  		}
   417  
   418  		hashMode := query.Origin.Hash != (common.Hash{})
   419  
   420  		// Gather headers until the fetch or network limits is reached
   421  		var (
   422  			bytes   common.StorageSize
   423  			headers []*types.Header
   424  			unknown bool
   425  		)
   426  		for !unknown && len(headers) < int(query.Amount) && bytes < softResponseLimit {
   427  			// Retrieve the next header satisfying the query
   428  			var origin *types.Header
   429  			if hashMode {
   430  				origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash)
   431  			} else {
   432  				origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number)
   433  			}
   434  			if origin == nil {
   435  				break
   436  			}
   437  			number := origin.Number.Uint64()
   438  			headers = append(headers, origin)
   439  			bytes += estHeaderRlpSize
   440  
   441  			// Advance to the next header of the query
   442  			switch {
   443  			case query.Origin.Hash != (common.Hash{}) && query.Reverse:
   444  				// Hash based traversal towards the genesis block
   445  				for i := 0; i < int(query.Skip)+1; i++ {
   446  					if header := pm.blockchain.GetHeader(query.Origin.Hash, number); header != nil {
   447  						query.Origin.Hash = header.ParentHash
   448  						number--
   449  					} else {
   450  						unknown = true
   451  						break
   452  					}
   453  				}
   454  			case query.Origin.Hash != (common.Hash{}) && !query.Reverse:
   455  				// Hash based traversal towards the leaf block
   456  				if header := pm.blockchain.GetHeaderByNumber(origin.Number.Uint64() + query.Skip + 1); header != nil {
   457  					if pm.blockchain.GetBlockHashesFromHash(header.Hash(), query.Skip+1)[query.Skip] == query.Origin.Hash {
   458  						query.Origin.Hash = header.Hash()
   459  					} else {
   460  						unknown = true
   461  					}
   462  				} else {
   463  					unknown = true
   464  				}
   465  			case query.Reverse:
   466  				// Number based traversal towards the genesis block
   467  				if query.Origin.Number >= query.Skip+1 {
   468  					query.Origin.Number -= query.Skip + 1
   469  				} else {
   470  					unknown = true
   471  				}
   472  
   473  			case !query.Reverse:
   474  				// Number based traversal towards the leaf block
   475  				query.Origin.Number += query.Skip + 1
   476  			}
   477  		}
   478  
   479  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + query.Amount*costs.reqCost)
   480  		pm.server.fcCostStats.update(msg.Code, query.Amount, rcost)
   481  		return p.SendBlockHeaders(req.ReqID, bv, headers)
   482  
   483  	case BlockHeadersMsg:
   484  		if pm.downloader == nil {
   485  			return errResp(ErrUnexpectedResponse, "")
   486  		}
   487  
   488  		p.Log().Trace("Received block header response message")
   489  		// A batch of headers arrived to one of our previous requests
   490  		var resp struct {
   491  			ReqID, BV uint64
   492  			Headers   []*types.Header
   493  		}
   494  		if err := msg.Decode(&resp); err != nil {
   495  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   496  		}
   497  		p.fcServer.GotReply(resp.ReqID, resp.BV)
   498  		if pm.fetcher != nil && pm.fetcher.requestedID(resp.ReqID) {
   499  			pm.fetcher.deliverHeaders(p, resp.ReqID, resp.Headers)
   500  		} else {
   501  			err := pm.downloader.DeliverHeaders(p.id, resp.Headers)
   502  			if err != nil {
   503  				log.Debug(fmt.Sprint(err))
   504  			}
   505  		}
   506  
   507  	case GetBlockBodiesMsg:
   508  		p.Log().Trace("Received block bodies request")
   509  		// Decode the retrieval message
   510  		var req struct {
   511  			ReqID  uint64
   512  			Hashes []common.Hash
   513  		}
   514  		if err := msg.Decode(&req); err != nil {
   515  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   516  		}
   517  		// Gather blocks until the fetch or network limits is reached
   518  		var (
   519  			bytes  int
   520  			bodies []rlp.RawValue
   521  		)
   522  		reqCnt := len(req.Hashes)
   523  		if reject(uint64(reqCnt), MaxBodyFetch) {
   524  			return errResp(ErrRequestRejected, "")
   525  		}
   526  		for _, hash := range req.Hashes {
   527  			if bytes >= softResponseLimit {
   528  				break
   529  			}
   530  			// Retrieve the requested block body, stopping if enough was found
   531  			if data := core.GetBodyRLP(pm.chainDb, hash, core.GetBlockNumber(pm.chainDb, hash)); len(data) != 0 {
   532  				bodies = append(bodies, data)
   533  				bytes += len(data)
   534  			}
   535  		}
   536  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
   537  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
   538  		return p.SendBlockBodiesRLP(req.ReqID, bv, bodies)
   539  
   540  	case BlockBodiesMsg:
   541  		if pm.odr == nil {
   542  			return errResp(ErrUnexpectedResponse, "")
   543  		}
   544  
   545  		p.Log().Trace("Received block bodies response")
   546  		// A batch of block bodies arrived to one of our previous requests
   547  		var resp struct {
   548  			ReqID, BV uint64
   549  			Data      []*types.Body
   550  		}
   551  		if err := msg.Decode(&resp); err != nil {
   552  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   553  		}
   554  		p.fcServer.GotReply(resp.ReqID, resp.BV)
   555  		deliverMsg = &Msg{
   556  			MsgType: MsgBlockBodies,
   557  			ReqID:   resp.ReqID,
   558  			Obj:     resp.Data,
   559  		}
   560  
   561  	case GetCodeMsg:
   562  		p.Log().Trace("Received code request")
   563  		// Decode the retrieval message
   564  		var req struct {
   565  			ReqID uint64
   566  			Reqs  []CodeReq
   567  		}
   568  		if err := msg.Decode(&req); err != nil {
   569  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   570  		}
   571  		// Gather state data until the fetch or network limits is reached
   572  		var (
   573  			bytes int
   574  			data  [][]byte
   575  		)
   576  		reqCnt := len(req.Reqs)
   577  		if reject(uint64(reqCnt), MaxCodeFetch) {
   578  			return errResp(ErrRequestRejected, "")
   579  		}
   580  		for _, req := range req.Reqs {
   581  			// Retrieve the requested state entry, stopping if enough was found
   582  			if header := core.GetHeader(pm.chainDb, req.BHash, core.GetBlockNumber(pm.chainDb, req.BHash)); header != nil {
   583  				statedb, err := pm.blockchain.State()
   584  				if err != nil {
   585  					continue
   586  				}
   587  				account, err := pm.getAccount(statedb, header.Root, common.BytesToHash(req.AccKey))
   588  				if err != nil {
   589  					continue
   590  				}
   591  				code, _ := statedb.Database().TrieDB().Node(common.BytesToHash(account.CodeHash))
   592  
   593  				data = append(data, code)
   594  				if bytes += len(code); bytes >= softResponseLimit {
   595  					break
   596  				}
   597  			}
   598  		}
   599  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
   600  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
   601  		return p.SendCode(req.ReqID, bv, data)
   602  
   603  	case CodeMsg:
   604  		if pm.odr == nil {
   605  			return errResp(ErrUnexpectedResponse, "")
   606  		}
   607  
   608  		p.Log().Trace("Received code response")
   609  		// A batch of node state data arrived to one of our previous requests
   610  		var resp struct {
   611  			ReqID, BV uint64
   612  			Data      [][]byte
   613  		}
   614  		if err := msg.Decode(&resp); err != nil {
   615  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   616  		}
   617  		p.fcServer.GotReply(resp.ReqID, resp.BV)
   618  		deliverMsg = &Msg{
   619  			MsgType: MsgCode,
   620  			ReqID:   resp.ReqID,
   621  			Obj:     resp.Data,
   622  		}
   623  
   624  	case GetReceiptsMsg:
   625  		p.Log().Trace("Received receipts request")
   626  		// Decode the retrieval message
   627  		var req struct {
   628  			ReqID  uint64
   629  			Hashes []common.Hash
   630  		}
   631  		if err := msg.Decode(&req); err != nil {
   632  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   633  		}
   634  		// Gather state data until the fetch or network limits is reached
   635  		var (
   636  			bytes    int
   637  			receipts []rlp.RawValue
   638  		)
   639  		reqCnt := len(req.Hashes)
   640  		if reject(uint64(reqCnt), MaxReceiptFetch) {
   641  			return errResp(ErrRequestRejected, "")
   642  		}
   643  		for _, hash := range req.Hashes {
   644  			if bytes >= softResponseLimit {
   645  				break
   646  			}
   647  			// Retrieve the requested block's receipts, skipping if unknown to us
   648  			results := core.GetBlockReceipts(pm.chainDb, hash, core.GetBlockNumber(pm.chainDb, hash))
   649  			if results == nil {
   650  				if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
   651  					continue
   652  				}
   653  			}
   654  			// If known, encode and queue for response packet
   655  			if encoded, err := rlp.EncodeToBytes(results); err != nil {
   656  				log.Error("Failed to encode receipt", "err", err)
   657  			} else {
   658  				receipts = append(receipts, encoded)
   659  				bytes += len(encoded)
   660  			}
   661  		}
   662  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
   663  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
   664  		return p.SendReceiptsRLP(req.ReqID, bv, receipts)
   665  
   666  	case ReceiptsMsg:
   667  		if pm.odr == nil {
   668  			return errResp(ErrUnexpectedResponse, "")
   669  		}
   670  
   671  		p.Log().Trace("Received receipts response")
   672  		// A batch of receipts arrived to one of our previous requests
   673  		var resp struct {
   674  			ReqID, BV uint64
   675  			Receipts  []types.Receipts
   676  		}
   677  		if err := msg.Decode(&resp); err != nil {
   678  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   679  		}
   680  		p.fcServer.GotReply(resp.ReqID, resp.BV)
   681  		deliverMsg = &Msg{
   682  			MsgType: MsgReceipts,
   683  			ReqID:   resp.ReqID,
   684  			Obj:     resp.Receipts,
   685  		}
   686  
   687  	case GetProofsV1Msg:
   688  		p.Log().Trace("Received proofs request")
   689  		// Decode the retrieval message
   690  		var req struct {
   691  			ReqID uint64
   692  			Reqs  []ProofReq
   693  		}
   694  		if err := msg.Decode(&req); err != nil {
   695  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   696  		}
   697  		// Gather state data until the fetch or network limits is reached
   698  		var (
   699  			bytes  int
   700  			proofs proofsData
   701  		)
   702  		reqCnt := len(req.Reqs)
   703  		if reject(uint64(reqCnt), MaxProofsFetch) {
   704  			return errResp(ErrRequestRejected, "")
   705  		}
   706  		for _, req := range req.Reqs {
   707  			// Retrieve the requested state entry, stopping if enough was found
   708  			if header := core.GetHeader(pm.chainDb, req.BHash, core.GetBlockNumber(pm.chainDb, req.BHash)); header != nil {
   709  				statedb, err := pm.blockchain.State()
   710  				if err != nil {
   711  					continue
   712  				}
   713  				var trie state.Trie
   714  				if len(req.AccKey) > 0 {
   715  					account, err := pm.getAccount(statedb, header.Root, common.BytesToHash(req.AccKey))
   716  					if err != nil {
   717  						continue
   718  					}
   719  					trie, _ = statedb.Database().OpenStorageTrie(common.BytesToHash(req.AccKey), account.Root)
   720  				} else {
   721  					trie, _ = statedb.Database().OpenTrie(header.Root)
   722  				}
   723  				if trie != nil {
   724  					var proof light.NodeList
   725  					trie.Prove(req.Key, 0, &proof)
   726  
   727  					proofs = append(proofs, proof)
   728  					if bytes += proof.DataSize(); bytes >= softResponseLimit {
   729  						break
   730  					}
   731  				}
   732  			}
   733  		}
   734  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
   735  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
   736  		return p.SendProofs(req.ReqID, bv, proofs)
   737  
   738  	case GetProofsV2Msg:
   739  		p.Log().Trace("Received les/2 proofs request")
   740  		// Decode the retrieval message
   741  		var req struct {
   742  			ReqID uint64
   743  			Reqs  []ProofReq
   744  		}
   745  		if err := msg.Decode(&req); err != nil {
   746  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   747  		}
   748  		// Gather state data until the fetch or network limits is reached
   749  		var (
   750  			lastBHash common.Hash
   751  			statedb   *state.StateDB
   752  			root      common.Hash
   753  		)
   754  		reqCnt := len(req.Reqs)
   755  		if reject(uint64(reqCnt), MaxProofsFetch) {
   756  			return errResp(ErrRequestRejected, "")
   757  		}
   758  
   759  		nodes := light.NewNodeSet()
   760  
   761  		for _, req := range req.Reqs {
   762  			// Look up the state belonging to the request
   763  			if statedb == nil || req.BHash != lastBHash {
   764  				statedb, root, lastBHash = nil, common.Hash{}, req.BHash
   765  
   766  				if header := core.GetHeader(pm.chainDb, req.BHash, core.GetBlockNumber(pm.chainDb, req.BHash)); header != nil {
   767  					statedb, _ = pm.blockchain.State()
   768  					root = header.Root
   769  				}
   770  			}
   771  			if statedb == nil {
   772  				continue
   773  			}
   774  			// Pull the account or storage trie of the request
   775  			var trie state.Trie
   776  			if len(req.AccKey) > 0 {
   777  				account, err := pm.getAccount(statedb, root, common.BytesToHash(req.AccKey))
   778  				if err != nil {
   779  					continue
   780  				}
   781  				trie, _ = statedb.Database().OpenStorageTrie(common.BytesToHash(req.AccKey), account.Root)
   782  			} else {
   783  				trie, _ = statedb.Database().OpenTrie(root)
   784  			}
   785  			if trie == nil {
   786  				continue
   787  			}
   788  			// Prove the user's request from the account or stroage trie
   789  			trie.Prove(req.Key, req.FromLevel, nodes)
   790  			if nodes.DataSize() >= softResponseLimit {
   791  				break
   792  			}
   793  		}
   794  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
   795  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
   796  		return p.SendProofsV2(req.ReqID, bv, nodes.NodeList())
   797  
   798  	case ProofsV1Msg:
   799  		if pm.odr == nil {
   800  			return errResp(ErrUnexpectedResponse, "")
   801  		}
   802  
   803  		p.Log().Trace("Received proofs response")
   804  		// A batch of merkle proofs arrived to one of our previous requests
   805  		var resp struct {
   806  			ReqID, BV uint64
   807  			Data      []light.NodeList
   808  		}
   809  		if err := msg.Decode(&resp); err != nil {
   810  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   811  		}
   812  		p.fcServer.GotReply(resp.ReqID, resp.BV)
   813  		deliverMsg = &Msg{
   814  			MsgType: MsgProofsV1,
   815  			ReqID:   resp.ReqID,
   816  			Obj:     resp.Data,
   817  		}
   818  
   819  	case ProofsV2Msg:
   820  		if pm.odr == nil {
   821  			return errResp(ErrUnexpectedResponse, "")
   822  		}
   823  
   824  		p.Log().Trace("Received les/2 proofs response")
   825  		// A batch of merkle proofs arrived to one of our previous requests
   826  		var resp struct {
   827  			ReqID, BV uint64
   828  			Data      light.NodeList
   829  		}
   830  		if err := msg.Decode(&resp); err != nil {
   831  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   832  		}
   833  		p.fcServer.GotReply(resp.ReqID, resp.BV)
   834  		deliverMsg = &Msg{
   835  			MsgType: MsgProofsV2,
   836  			ReqID:   resp.ReqID,
   837  			Obj:     resp.Data,
   838  		}
   839  
   840  	case GetHeaderProofsMsg:
   841  		p.Log().Trace("Received headers proof request")
   842  		// Decode the retrieval message
   843  		var req struct {
   844  			ReqID uint64
   845  			Reqs  []ChtReq
   846  		}
   847  		if err := msg.Decode(&req); err != nil {
   848  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   849  		}
   850  		// Gather state data until the fetch or network limits is reached
   851  		var (
   852  			bytes  int
   853  			proofs []ChtResp
   854  		)
   855  		reqCnt := len(req.Reqs)
   856  		if reject(uint64(reqCnt), MaxHelperTrieProofsFetch) {
   857  			return errResp(ErrRequestRejected, "")
   858  		}
   859  		trieDb := trie.NewDatabase(ethdb.NewTable(pm.chainDb, light.ChtTablePrefix))
   860  		for _, req := range req.Reqs {
   861  			if header := pm.blockchain.GetHeaderByNumber(req.BlockNum); header != nil {
   862  				sectionHead := core.GetCanonicalHash(pm.chainDb, req.ChtNum*light.CHTFrequencyServer-1)
   863  				if root := light.GetChtRoot(pm.chainDb, req.ChtNum-1, sectionHead); root != (common.Hash{}) {
   864  					trie, err := trie.New(root, trieDb)
   865  					if err != nil {
   866  						continue
   867  					}
   868  					var encNumber [8]byte
   869  					binary.BigEndian.PutUint64(encNumber[:], req.BlockNum)
   870  
   871  					var proof light.NodeList
   872  					trie.Prove(encNumber[:], 0, &proof)
   873  
   874  					proofs = append(proofs, ChtResp{Header: header, Proof: proof})
   875  					if bytes += proof.DataSize() + estHeaderRlpSize; bytes >= softResponseLimit {
   876  						break
   877  					}
   878  				}
   879  			}
   880  		}
   881  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
   882  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
   883  		return p.SendHeaderProofs(req.ReqID, bv, proofs)
   884  
   885  	case GetHelperTrieProofsMsg:
   886  		p.Log().Trace("Received helper trie proof request")
   887  		// Decode the retrieval message
   888  		var req struct {
   889  			ReqID uint64
   890  			Reqs  []HelperTrieReq
   891  		}
   892  		if err := msg.Decode(&req); err != nil {
   893  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   894  		}
   895  		// Gather state data until the fetch or network limits is reached
   896  		var (
   897  			auxBytes int
   898  			auxData  [][]byte
   899  		)
   900  		reqCnt := len(req.Reqs)
   901  		if reject(uint64(reqCnt), MaxHelperTrieProofsFetch) {
   902  			return errResp(ErrRequestRejected, "")
   903  		}
   904  
   905  		var (
   906  			lastIdx  uint64
   907  			lastType uint
   908  			root     common.Hash
   909  			auxTrie  *trie.Trie
   910  		)
   911  		nodes := light.NewNodeSet()
   912  		for _, req := range req.Reqs {
   913  			if auxTrie == nil || req.Type != lastType || req.TrieIdx != lastIdx {
   914  				auxTrie, lastType, lastIdx = nil, req.Type, req.TrieIdx
   915  
   916  				var prefix string
   917  				if root, prefix = pm.getHelperTrie(req.Type, req.TrieIdx); root != (common.Hash{}) {
   918  					auxTrie, _ = trie.New(root, trie.NewDatabase(ethdb.NewTable(pm.chainDb, prefix)))
   919  				}
   920  			}
   921  			if req.AuxReq == auxRoot {
   922  				var data []byte
   923  				if root != (common.Hash{}) {
   924  					data = root[:]
   925  				}
   926  				auxData = append(auxData, data)
   927  				auxBytes += len(data)
   928  			} else {
   929  				if auxTrie != nil {
   930  					auxTrie.Prove(req.Key, req.FromLevel, nodes)
   931  				}
   932  				if req.AuxReq != 0 {
   933  					data := pm.getHelperTrieAuxData(req)
   934  					auxData = append(auxData, data)
   935  					auxBytes += len(data)
   936  				}
   937  			}
   938  			if nodes.DataSize()+auxBytes >= softResponseLimit {
   939  				break
   940  			}
   941  		}
   942  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
   943  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
   944  		return p.SendHelperTrieProofs(req.ReqID, bv, HelperTrieResps{Proofs: nodes.NodeList(), AuxData: auxData})
   945  
   946  	case HeaderProofsMsg:
   947  		if pm.odr == nil {
   948  			return errResp(ErrUnexpectedResponse, "")
   949  		}
   950  
   951  		p.Log().Trace("Received headers proof response")
   952  		var resp struct {
   953  			ReqID, BV uint64
   954  			Data      []ChtResp
   955  		}
   956  		if err := msg.Decode(&resp); err != nil {
   957  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   958  		}
   959  		p.fcServer.GotReply(resp.ReqID, resp.BV)
   960  		deliverMsg = &Msg{
   961  			MsgType: MsgHeaderProofs,
   962  			ReqID:   resp.ReqID,
   963  			Obj:     resp.Data,
   964  		}
   965  
   966  	case HelperTrieProofsMsg:
   967  		if pm.odr == nil {
   968  			return errResp(ErrUnexpectedResponse, "")
   969  		}
   970  
   971  		p.Log().Trace("Received helper trie proof response")
   972  		var resp struct {
   973  			ReqID, BV uint64
   974  			Data      HelperTrieResps
   975  		}
   976  		if err := msg.Decode(&resp); err != nil {
   977  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   978  		}
   979  
   980  		p.fcServer.GotReply(resp.ReqID, resp.BV)
   981  		deliverMsg = &Msg{
   982  			MsgType: MsgHelperTrieProofs,
   983  			ReqID:   resp.ReqID,
   984  			Obj:     resp.Data,
   985  		}
   986  
   987  	case SendTxMsg:
   988  		if pm.txpool == nil {
   989  			return errResp(ErrRequestRejected, "")
   990  		}
   991  		// Transactions arrived, parse all of them and deliver to the pool
   992  		var txs []*types.Transaction
   993  		if err := msg.Decode(&txs); err != nil {
   994  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   995  		}
   996  		reqCnt := len(txs)
   997  		if reject(uint64(reqCnt), MaxTxSend) {
   998  			return errResp(ErrRequestRejected, "")
   999  		}
  1000  		pm.txpool.AddRemotes(txs)
  1001  
  1002  		_, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
  1003  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
  1004  
  1005  	case SendTxV2Msg:
  1006  		if pm.txpool == nil {
  1007  			return errResp(ErrRequestRejected, "")
  1008  		}
  1009  		// Transactions arrived, parse all of them and deliver to the pool
  1010  		var req struct {
  1011  			ReqID uint64
  1012  			Txs   []*types.Transaction
  1013  		}
  1014  		if err := msg.Decode(&req); err != nil {
  1015  			return errResp(ErrDecode, "msg %v: %v", msg, err)
  1016  		}
  1017  		reqCnt := len(req.Txs)
  1018  		if reject(uint64(reqCnt), MaxTxSend) {
  1019  			return errResp(ErrRequestRejected, "")
  1020  		}
  1021  
  1022  		hashes := make([]common.Hash, len(req.Txs))
  1023  		for i, tx := range req.Txs {
  1024  			hashes[i] = tx.Hash()
  1025  		}
  1026  		stats := pm.txStatus(hashes)
  1027  		for i, stat := range stats {
  1028  			if stat.Status == core.TxStatusUnknown {
  1029  				if errs := pm.txpool.AddRemotes([]*types.Transaction{req.Txs[i]}); errs[0] != nil {
  1030  					stats[i].Error = errs[0].Error()
  1031  					continue
  1032  				}
  1033  				stats[i] = pm.txStatus([]common.Hash{hashes[i]})[0]
  1034  			}
  1035  		}
  1036  
  1037  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
  1038  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
  1039  
  1040  		return p.SendTxStatus(req.ReqID, bv, stats)
  1041  
  1042  	case GetTxStatusMsg:
  1043  		if pm.txpool == nil {
  1044  			return errResp(ErrUnexpectedResponse, "")
  1045  		}
  1046  		// Transactions arrived, parse all of them and deliver to the pool
  1047  		var req struct {
  1048  			ReqID  uint64
  1049  			Hashes []common.Hash
  1050  		}
  1051  		if err := msg.Decode(&req); err != nil {
  1052  			return errResp(ErrDecode, "msg %v: %v", msg, err)
  1053  		}
  1054  		reqCnt := len(req.Hashes)
  1055  		if reject(uint64(reqCnt), MaxTxStatus) {
  1056  			return errResp(ErrRequestRejected, "")
  1057  		}
  1058  		bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
  1059  		pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
  1060  
  1061  		return p.SendTxStatus(req.ReqID, bv, pm.txStatus(req.Hashes))
  1062  
  1063  	case TxStatusMsg:
  1064  		if pm.odr == nil {
  1065  			return errResp(ErrUnexpectedResponse, "")
  1066  		}
  1067  
  1068  		p.Log().Trace("Received tx status response")
  1069  		var resp struct {
  1070  			ReqID, BV uint64
  1071  			Status    []txStatus
  1072  		}
  1073  		if err := msg.Decode(&resp); err != nil {
  1074  			return errResp(ErrDecode, "msg %v: %v", msg, err)
  1075  		}
  1076  
  1077  		p.fcServer.GotReply(resp.ReqID, resp.BV)
  1078  
  1079  	default:
  1080  		p.Log().Trace("Received unknown message", "code", msg.Code)
  1081  		return errResp(ErrInvalidMsgCode, "%v", msg.Code)
  1082  	}
  1083  
  1084  	if deliverMsg != nil {
  1085  		err := pm.retriever.deliver(p, deliverMsg)
  1086  		if err != nil {
  1087  			p.responseErrors++
  1088  			if p.responseErrors > maxResponseErrors {
  1089  				return err
  1090  			}
  1091  		}
  1092  	}
  1093  	return nil
  1094  }
  1095  
  1096  // getAccount retrieves an account from the state based at root.
  1097  func (pm *ProtocolManager) getAccount(statedb *state.StateDB, root, hash common.Hash) (state.Account, error) {
  1098  	trie, err := trie.New(root, statedb.Database().TrieDB())
  1099  	if err != nil {
  1100  		return state.Account{}, err
  1101  	}
  1102  	blob, err := trie.TryGet(hash[:])
  1103  	if err != nil {
  1104  		return state.Account{}, err
  1105  	}
  1106  	var account state.Account
  1107  	if err = rlp.DecodeBytes(blob, &account); err != nil {
  1108  		return state.Account{}, err
  1109  	}
  1110  	return account, nil
  1111  }
  1112  
  1113  // getHelperTrie returns the post-processed trie root for the given trie ID and section index
  1114  func (pm *ProtocolManager) getHelperTrie(id uint, idx uint64) (common.Hash, string) {
  1115  	switch id {
  1116  	case htCanonical:
  1117  		sectionHead := core.GetCanonicalHash(pm.chainDb, (idx+1)*light.CHTFrequencyClient-1)
  1118  		return light.GetChtV2Root(pm.chainDb, idx, sectionHead), light.ChtTablePrefix
  1119  	case htBloomBits:
  1120  		sectionHead := core.GetCanonicalHash(pm.chainDb, (idx+1)*light.BloomTrieFrequency-1)
  1121  		return light.GetBloomTrieRoot(pm.chainDb, idx, sectionHead), light.BloomTrieTablePrefix
  1122  	}
  1123  	return common.Hash{}, ""
  1124  }
  1125  
  1126  // getHelperTrieAuxData returns requested auxiliary data for the given HelperTrie request
  1127  func (pm *ProtocolManager) getHelperTrieAuxData(req HelperTrieReq) []byte {
  1128  	switch {
  1129  	case req.Type == htCanonical && req.AuxReq == auxHeader && len(req.Key) == 8:
  1130  		blockNum := binary.BigEndian.Uint64(req.Key)
  1131  		hash := core.GetCanonicalHash(pm.chainDb, blockNum)
  1132  		return core.GetHeaderRLP(pm.chainDb, hash, blockNum)
  1133  	}
  1134  	return nil
  1135  }
  1136  
  1137  func (pm *ProtocolManager) txStatus(hashes []common.Hash) []txStatus {
  1138  	stats := make([]txStatus, len(hashes))
  1139  	for i, stat := range pm.txpool.Status(hashes) {
  1140  		// Save the status we've got from the transaction pool
  1141  		stats[i].Status = stat
  1142  
  1143  		// If the transaction is unknown to the pool, try looking it up locally
  1144  		if stat == core.TxStatusUnknown {
  1145  			if block, number, index := core.GetTxLookupEntry(pm.chainDb, hashes[i]); block != (common.Hash{}) {
  1146  				stats[i].Status = core.TxStatusIncluded
  1147  				stats[i].Lookup = &core.TxLookupEntry{BlockHash: block, BlockIndex: number, Index: index}
  1148  			}
  1149  		}
  1150  	}
  1151  	return stats
  1152  }
  1153  
  1154  // NodeInfo represents a short summary of the Ethereum sub-protocol metadata
  1155  // known about the host peer.
  1156  type NodeInfo struct {
  1157  	Network    uint64              `json:"network"`    // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
  1158  	Difficulty *big.Int            `json:"difficulty"` // Total difficulty of the host's blockchain
  1159  	Genesis    common.Hash         `json:"genesis"`    // SHA3 hash of the host's genesis block
  1160  	Config     *params.ChainConfig `json:"config"`     // Chain configuration for the fork rules
  1161  	Head       common.Hash         `json:"head"`       // SHA3 hash of the host's best owned block
  1162  }
  1163  
  1164  // NodeInfo retrieves some protocol metadata about the running host node.
  1165  func (self *ProtocolManager) NodeInfo() *NodeInfo {
  1166  	head := self.blockchain.CurrentHeader()
  1167  	hash := head.Hash()
  1168  
  1169  	return &NodeInfo{
  1170  		Network:    self.networkId,
  1171  		Difficulty: self.blockchain.GetTd(hash, head.Number.Uint64()),
  1172  		Genesis:    self.blockchain.Genesis().Hash(),
  1173  		Config:     self.blockchain.Config(),
  1174  		Head:       hash,
  1175  	}
  1176  }
  1177  
  1178  // downloaderPeerNotify implements peerSetNotify
  1179  type downloaderPeerNotify ProtocolManager
  1180  
  1181  type peerConnection struct {
  1182  	manager *ProtocolManager
  1183  	peer    *peer
  1184  }
  1185  
  1186  func (pc *peerConnection) Head() (common.Hash, *big.Int) {
  1187  	return pc.peer.HeadAndTd()
  1188  }
  1189  
  1190  func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error {
  1191  	reqID := genReqID()
  1192  	rq := &distReq{
  1193  		getCost: func(dp distPeer) uint64 {
  1194  			peer := dp.(*peer)
  1195  			return peer.GetRequestCost(GetBlockHeadersMsg, amount)
  1196  		},
  1197  		canSend: func(dp distPeer) bool {
  1198  			return dp.(*peer) == pc.peer
  1199  		},
  1200  		request: func(dp distPeer) func() {
  1201  			peer := dp.(*peer)
  1202  			cost := peer.GetRequestCost(GetBlockHeadersMsg, amount)
  1203  			peer.fcServer.QueueRequest(reqID, cost)
  1204  			return func() { peer.RequestHeadersByHash(reqID, cost, origin, amount, skip, reverse) }
  1205  		},
  1206  	}
  1207  	_, ok := <-pc.manager.reqDist.queue(rq)
  1208  	if !ok {
  1209  		return ErrNoPeers
  1210  	}
  1211  	return nil
  1212  }
  1213  
  1214  func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error {
  1215  	reqID := genReqID()
  1216  	rq := &distReq{
  1217  		getCost: func(dp distPeer) uint64 {
  1218  			peer := dp.(*peer)
  1219  			return peer.GetRequestCost(GetBlockHeadersMsg, amount)
  1220  		},
  1221  		canSend: func(dp distPeer) bool {
  1222  			return dp.(*peer) == pc.peer
  1223  		},
  1224  		request: func(dp distPeer) func() {
  1225  			peer := dp.(*peer)
  1226  			cost := peer.GetRequestCost(GetBlockHeadersMsg, amount)
  1227  			peer.fcServer.QueueRequest(reqID, cost)
  1228  			return func() { peer.RequestHeadersByNumber(reqID, cost, origin, amount, skip, reverse) }
  1229  		},
  1230  	}
  1231  	_, ok := <-pc.manager.reqDist.queue(rq)
  1232  	if !ok {
  1233  		return ErrNoPeers
  1234  	}
  1235  	return nil
  1236  }
  1237  
  1238  func (d *downloaderPeerNotify) registerPeer(p *peer) {
  1239  	pm := (*ProtocolManager)(d)
  1240  	pc := &peerConnection{
  1241  		manager: pm,
  1242  		peer:    p,
  1243  	}
  1244  	pm.downloader.RegisterLightPeer(p.id, ethVersion, pc)
  1245  }
  1246  
  1247  func (d *downloaderPeerNotify) unregisterPeer(p *peer) {
  1248  	pm := (*ProtocolManager)(d)
  1249  	pm.downloader.UnregisterPeer(p.id)
  1250  }