github.com/Debrief-BC/go-debrief@v0.0.0-20200420203408-0c26ca968123/eth/handler.go (about)

     1  // Copyright 2015 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 eth
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"math"
    24  	"math/big"
    25  	"sync"
    26  	"sync/atomic"
    27  	"time"
    28  
    29  	"github.com/Debrief-BC/go-debrief/common"
    30  	"github.com/Debrief-BC/go-debrief/consensus"
    31  	"github.com/Debrief-BC/go-debrief/core"
    32  	"github.com/Debrief-BC/go-debrief/core/forkid"
    33  	"github.com/Debrief-BC/go-debrief/core/types"
    34  	"github.com/Debrief-BC/go-debrief/eth/downloader"
    35  	"github.com/Debrief-BC/go-debrief/eth/fetcher"
    36  	"github.com/Debrief-BC/go-debrief/ethdb"
    37  	"github.com/Debrief-BC/go-debrief/event"
    38  	"github.com/Debrief-BC/go-debrief/log"
    39  	"github.com/Debrief-BC/go-debrief/p2p"
    40  	"github.com/Debrief-BC/go-debrief/p2p/enode"
    41  	"github.com/Debrief-BC/go-debrief/params"
    42  	"github.com/Debrief-BC/go-debrief/rlp"
    43  	"github.com/Debrief-BC/go-debrief/trie"
    44  )
    45  
    46  const (
    47  	softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data.
    48  	estHeaderRlpSize  = 500             // Approximate size of an RLP encoded block header
    49  
    50  	// txChanSize is the size of channel listening to NewTxsEvent.
    51  	// The number is referenced from the size of tx pool.
    52  	txChanSize = 4096
    53  )
    54  
    55  var (
    56  	syncChallengeTimeout = 15 * time.Second // Time allowance for a node to reply to the sync progress challenge
    57  )
    58  
    59  func errResp(code errCode, format string, v ...interface{}) error {
    60  	return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
    61  }
    62  
    63  type ProtocolManager struct {
    64  	networkID  uint64
    65  	forkFilter forkid.Filter // Fork ID filter, constant across the lifetime of the node
    66  
    67  	fastSync  uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
    68  	acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
    69  
    70  	checkpointNumber uint64      // Block number for the sync progress validator to cross reference
    71  	checkpointHash   common.Hash // Block hash for the sync progress validator to cross reference
    72  
    73  	txpool     txPool
    74  	blockchain *core.BlockChain
    75  	maxPeers   int
    76  
    77  	downloader   *downloader.Downloader
    78  	blockFetcher *fetcher.BlockFetcher
    79  	txFetcher    *fetcher.TxFetcher
    80  	peers        *peerSet
    81  
    82  	eventMux      *event.TypeMux
    83  	txsCh         chan core.NewTxsEvent
    84  	txsSub        event.Subscription
    85  	minedBlockSub *event.TypeMuxSubscription
    86  
    87  	whitelist map[uint64]common.Hash
    88  
    89  	// channels for fetcher, syncer, txsyncLoop
    90  	txsyncCh chan *txsync
    91  	quitSync chan struct{}
    92  
    93  	chainSync *chainSyncer
    94  	wg        sync.WaitGroup
    95  	peerWG    sync.WaitGroup
    96  
    97  	// Test fields or hooks
    98  	broadcastTxAnnouncesOnly bool // Testing field, disable transaction propagation
    99  }
   100  
   101  // NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
   102  // with the Ethereum network.
   103  func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCheckpoint, mode downloader.SyncMode, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database, cacheLimit int, whitelist map[uint64]common.Hash) (*ProtocolManager, error) {
   104  	// Create the protocol manager with the base fields
   105  	manager := &ProtocolManager{
   106  		networkID:  networkID,
   107  		forkFilter: forkid.NewFilter(blockchain),
   108  		eventMux:   mux,
   109  		txpool:     txpool,
   110  		blockchain: blockchain,
   111  		peers:      newPeerSet(),
   112  		whitelist:  whitelist,
   113  		txsyncCh:   make(chan *txsync),
   114  		quitSync:   make(chan struct{}),
   115  	}
   116  
   117  	if mode == downloader.FullSync {
   118  		// The database seems empty as the current block is the genesis. Yet the fast
   119  		// block is ahead, so fast sync was enabled for this node at a certain point.
   120  		// The scenarios where this can happen is
   121  		// * if the user manually (or via a bad block) rolled back a fast sync node
   122  		//   below the sync point.
   123  		// * the last fast sync is not finished while user specifies a full sync this
   124  		//   time. But we don't have any recent state for full sync.
   125  		// In these cases however it's safe to reenable fast sync.
   126  		fullBlock, fastBlock := blockchain.CurrentBlock(), blockchain.CurrentFastBlock()
   127  		if fullBlock.NumberU64() == 0 && fastBlock.NumberU64() > 0 {
   128  			manager.fastSync = uint32(1)
   129  			log.Warn("Switch sync mode from full sync to fast sync")
   130  		}
   131  	} else {
   132  		if blockchain.CurrentBlock().NumberU64() > 0 {
   133  			// Print warning log if database is not empty to run fast sync.
   134  			log.Warn("Switch sync mode from fast sync to full sync")
   135  		} else {
   136  			// If fast sync was requested and our database is empty, grant it
   137  			manager.fastSync = uint32(1)
   138  		}
   139  	}
   140  
   141  	// If we have trusted checkpoints, enforce them on the chain
   142  	if checkpoint != nil {
   143  		manager.checkpointNumber = (checkpoint.SectionIndex+1)*params.CHTFrequency - 1
   144  		manager.checkpointHash = checkpoint.SectionHead
   145  	}
   146  
   147  	// Construct the downloader (long sync) and its backing state bloom if fast
   148  	// sync is requested. The downloader is responsible for deallocating the state
   149  	// bloom when it's done.
   150  	var stateBloom *trie.SyncBloom
   151  	if atomic.LoadUint32(&manager.fastSync) == 1 {
   152  		stateBloom = trie.NewSyncBloom(uint64(cacheLimit), chaindb)
   153  	}
   154  	manager.downloader = downloader.New(manager.checkpointNumber, chaindb, stateBloom, manager.eventMux, blockchain, nil, manager.removePeer)
   155  
   156  	// Construct the fetcher (short sync)
   157  	validator := func(header *types.Header) error {
   158  		return engine.VerifyHeader(blockchain, header, true)
   159  	}
   160  	heighter := func() uint64 {
   161  		return blockchain.CurrentBlock().NumberU64()
   162  	}
   163  	inserter := func(blocks types.Blocks) (int, error) {
   164  		// If sync hasn't reached the checkpoint yet, deny importing weird blocks.
   165  		//
   166  		// Ideally we would also compare the head block's timestamp and similarly reject
   167  		// the propagated block if the head is too old. Unfortunately there is a corner
   168  		// case when starting new networks, where the genesis might be ancient (0 unix)
   169  		// which would prevent full nodes from accepting it.
   170  		if manager.blockchain.CurrentBlock().NumberU64() < manager.checkpointNumber {
   171  			log.Warn("Unsynced yet, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
   172  			return 0, nil
   173  		}
   174  		// If fast sync is running, deny importing weird blocks. This is a problematic
   175  		// clause when starting up a new network, because fast-syncing miners might not
   176  		// accept each others' blocks until a restart. Unfortunately we haven't figured
   177  		// out a way yet where nodes can decide unilaterally whether the network is new
   178  		// or not. This should be fixed if we figure out a solution.
   179  		if atomic.LoadUint32(&manager.fastSync) == 1 {
   180  			log.Warn("Fast syncing, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
   181  			return 0, nil
   182  		}
   183  		n, err := manager.blockchain.InsertChain(blocks)
   184  		if err == nil {
   185  			atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
   186  		}
   187  		return n, err
   188  	}
   189  	manager.blockFetcher = fetcher.NewBlockFetcher(blockchain.GetBlockByHash, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)
   190  
   191  	fetchTx := func(peer string, hashes []common.Hash) error {
   192  		p := manager.peers.Peer(peer)
   193  		if p == nil {
   194  			return errors.New("unknown peer")
   195  		}
   196  		return p.RequestTxs(hashes)
   197  	}
   198  	manager.txFetcher = fetcher.NewTxFetcher(txpool.Has, txpool.AddRemotes, fetchTx)
   199  
   200  	manager.chainSync = newChainSyncer(manager)
   201  
   202  	return manager, nil
   203  }
   204  
   205  func (pm *ProtocolManager) makeProtocol(version uint) p2p.Protocol {
   206  	length, ok := protocolLengths[version]
   207  	if !ok {
   208  		panic("makeProtocol for unknown version")
   209  	}
   210  
   211  	return p2p.Protocol{
   212  		Name:    protocolName,
   213  		Version: version,
   214  		Length:  length,
   215  		Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
   216  			return pm.runPeer(pm.newPeer(int(version), p, rw, pm.txpool.Get))
   217  		},
   218  		NodeInfo: func() interface{} {
   219  			return pm.NodeInfo()
   220  		},
   221  		PeerInfo: func(id enode.ID) interface{} {
   222  			if p := pm.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
   223  				return p.Info()
   224  			}
   225  			return nil
   226  		},
   227  	}
   228  }
   229  
   230  func (pm *ProtocolManager) removePeer(id string) {
   231  	// Short circuit if the peer was already removed
   232  	peer := pm.peers.Peer(id)
   233  	if peer == nil {
   234  		return
   235  	}
   236  	log.Debug("Removing Ethereum peer", "peer", id)
   237  
   238  	// Unregister the peer from the downloader and Ethereum peer set
   239  	pm.downloader.UnregisterPeer(id)
   240  	pm.txFetcher.Drop(id)
   241  
   242  	if err := pm.peers.Unregister(id); err != nil {
   243  		log.Error("Peer removal failed", "peer", id, "err", err)
   244  	}
   245  	// Hard disconnect at the networking layer
   246  	if peer != nil {
   247  		peer.Peer.Disconnect(p2p.DiscUselessPeer)
   248  	}
   249  }
   250  
   251  func (pm *ProtocolManager) Start(maxPeers int) {
   252  	pm.maxPeers = maxPeers
   253  
   254  	// broadcast transactions
   255  	pm.wg.Add(1)
   256  	pm.txsCh = make(chan core.NewTxsEvent, txChanSize)
   257  	pm.txsSub = pm.txpool.SubscribeNewTxsEvent(pm.txsCh)
   258  	go pm.txBroadcastLoop()
   259  
   260  	// broadcast mined blocks
   261  	pm.wg.Add(1)
   262  	pm.minedBlockSub = pm.eventMux.Subscribe(core.NewMinedBlockEvent{})
   263  	go pm.minedBroadcastLoop()
   264  
   265  	// start sync handlers
   266  	pm.wg.Add(2)
   267  	go pm.chainSync.loop()
   268  	go pm.txsyncLoop64() // TODO(karalabe): Legacy initial tx echange, drop with eth/64.
   269  }
   270  
   271  func (pm *ProtocolManager) Stop() {
   272  	pm.txsSub.Unsubscribe()        // quits txBroadcastLoop
   273  	pm.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
   274  
   275  	// Quit chainSync and txsync64.
   276  	// After this is done, no new peers will be accepted.
   277  	close(pm.quitSync)
   278  	pm.wg.Wait()
   279  
   280  	// Disconnect existing sessions.
   281  	// This also closes the gate for any new registrations on the peer set.
   282  	// sessions which are already established but not added to pm.peers yet
   283  	// will exit when they try to register.
   284  	pm.peers.Close()
   285  	pm.peerWG.Wait()
   286  
   287  	log.Info("Ethereum protocol stopped")
   288  }
   289  
   290  func (pm *ProtocolManager) newPeer(pv int, p *p2p.Peer, rw p2p.MsgReadWriter, getPooledTx func(hash common.Hash) *types.Transaction) *peer {
   291  	return newPeer(pv, p, rw, getPooledTx)
   292  }
   293  
   294  func (pm *ProtocolManager) runPeer(p *peer) error {
   295  	if !pm.chainSync.handlePeerEvent(p) {
   296  		return p2p.DiscQuitting
   297  	}
   298  	pm.peerWG.Add(1)
   299  	defer pm.peerWG.Done()
   300  	return pm.handle(p)
   301  }
   302  
   303  // handle is the callback invoked to manage the life cycle of an eth peer. When
   304  // this function terminates, the peer is disconnected.
   305  func (pm *ProtocolManager) handle(p *peer) error {
   306  	// Ignore maxPeers if this is a trusted peer
   307  	if pm.peers.Len() >= pm.maxPeers && !p.Peer.Info().Network.Trusted {
   308  		return p2p.DiscTooManyPeers
   309  	}
   310  	p.Log().Debug("Ethereum peer connected", "name", p.Name())
   311  
   312  	// Execute the Ethereum handshake
   313  	var (
   314  		genesis = pm.blockchain.Genesis()
   315  		head    = pm.blockchain.CurrentHeader()
   316  		hash    = head.Hash()
   317  		number  = head.Number.Uint64()
   318  		td      = pm.blockchain.GetTd(hash, number)
   319  	)
   320  	if err := p.Handshake(pm.networkID, td, hash, genesis.Hash(), forkid.NewID(pm.blockchain), pm.forkFilter); err != nil {
   321  		p.Log().Debug("Ethereum handshake failed", "err", err)
   322  		return err
   323  	}
   324  
   325  	// Register the peer locally
   326  	if err := pm.peers.Register(p); err != nil {
   327  		p.Log().Error("Ethereum peer registration failed", "err", err)
   328  		return err
   329  	}
   330  	defer pm.removePeer(p.id)
   331  
   332  	// Register the peer in the downloader. If the downloader considers it banned, we disconnect
   333  	if err := pm.downloader.RegisterPeer(p.id, p.version, p); err != nil {
   334  		return err
   335  	}
   336  	pm.chainSync.handlePeerEvent(p)
   337  
   338  	// Propagate existing transactions. new transactions appearing
   339  	// after this will be sent via broadcasts.
   340  	pm.syncTransactions(p)
   341  
   342  	// If we have a trusted CHT, reject all peers below that (avoid fast sync eclipse)
   343  	if pm.checkpointHash != (common.Hash{}) {
   344  		// Request the peer's checkpoint header for chain height/weight validation
   345  		if err := p.RequestHeadersByNumber(pm.checkpointNumber, 1, 0, false); err != nil {
   346  			return err
   347  		}
   348  		// Start a timer to disconnect if the peer doesn't reply in time
   349  		p.syncDrop = time.AfterFunc(syncChallengeTimeout, func() {
   350  			p.Log().Warn("Checkpoint challenge timed out, dropping", "addr", p.RemoteAddr(), "type", p.Name())
   351  			pm.removePeer(p.id)
   352  		})
   353  		// Make sure it's cleaned up if the peer dies off
   354  		defer func() {
   355  			if p.syncDrop != nil {
   356  				p.syncDrop.Stop()
   357  				p.syncDrop = nil
   358  			}
   359  		}()
   360  	}
   361  	// If we have any explicit whitelist block hashes, request them
   362  	for number := range pm.whitelist {
   363  		if err := p.RequestHeadersByNumber(number, 1, 0, false); err != nil {
   364  			return err
   365  		}
   366  	}
   367  	// Handle incoming messages until the connection is torn down
   368  	for {
   369  		if err := pm.handleMsg(p); err != nil {
   370  			p.Log().Debug("Ethereum message handling failed", "err", err)
   371  			return err
   372  		}
   373  	}
   374  }
   375  
   376  // handleMsg is invoked whenever an inbound message is received from a remote
   377  // peer. The remote connection is torn down upon returning any error.
   378  func (pm *ProtocolManager) handleMsg(p *peer) error {
   379  	// Read the next message from the remote peer, and ensure it's fully consumed
   380  	msg, err := p.rw.ReadMsg()
   381  	if err != nil {
   382  		return err
   383  	}
   384  	if msg.Size > protocolMaxMsgSize {
   385  		return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, protocolMaxMsgSize)
   386  	}
   387  	defer msg.Discard()
   388  
   389  	// Handle the message depending on its contents
   390  	switch {
   391  	case msg.Code == StatusMsg:
   392  		// Status messages should never arrive after the handshake
   393  		return errResp(ErrExtraStatusMsg, "uncontrolled status message")
   394  
   395  	// Block header query, collect the requested headers and reply
   396  	case msg.Code == GetBlockHeadersMsg:
   397  		// Decode the complex header query
   398  		var query getBlockHeadersData
   399  		if err := msg.Decode(&query); err != nil {
   400  			return errResp(ErrDecode, "%v: %v", msg, err)
   401  		}
   402  		hashMode := query.Origin.Hash != (common.Hash{})
   403  		first := true
   404  		maxNonCanonical := uint64(100)
   405  
   406  		// Gather headers until the fetch or network limits is reached
   407  		var (
   408  			bytes   common.StorageSize
   409  			headers []*types.Header
   410  			unknown bool
   411  		)
   412  		for !unknown && len(headers) < int(query.Amount) && bytes < softResponseLimit && len(headers) < downloader.MaxHeaderFetch {
   413  			// Retrieve the next header satisfying the query
   414  			var origin *types.Header
   415  			if hashMode {
   416  				if first {
   417  					first = false
   418  					origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash)
   419  					if origin != nil {
   420  						query.Origin.Number = origin.Number.Uint64()
   421  					}
   422  				} else {
   423  					origin = pm.blockchain.GetHeader(query.Origin.Hash, query.Origin.Number)
   424  				}
   425  			} else {
   426  				origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number)
   427  			}
   428  			if origin == nil {
   429  				break
   430  			}
   431  			headers = append(headers, origin)
   432  			bytes += estHeaderRlpSize
   433  
   434  			// Advance to the next header of the query
   435  			switch {
   436  			case hashMode && query.Reverse:
   437  				// Hash based traversal towards the genesis block
   438  				ancestor := query.Skip + 1
   439  				if ancestor == 0 {
   440  					unknown = true
   441  				} else {
   442  					query.Origin.Hash, query.Origin.Number = pm.blockchain.GetAncestor(query.Origin.Hash, query.Origin.Number, ancestor, &maxNonCanonical)
   443  					unknown = (query.Origin.Hash == common.Hash{})
   444  				}
   445  			case hashMode && !query.Reverse:
   446  				// Hash based traversal towards the leaf block
   447  				var (
   448  					current = origin.Number.Uint64()
   449  					next    = current + query.Skip + 1
   450  				)
   451  				if next <= current {
   452  					infos, _ := json.MarshalIndent(p.Peer.Info(), "", "  ")
   453  					p.Log().Warn("GetBlockHeaders skip overflow attack", "current", current, "skip", query.Skip, "next", next, "attacker", infos)
   454  					unknown = true
   455  				} else {
   456  					if header := pm.blockchain.GetHeaderByNumber(next); header != nil {
   457  						nextHash := header.Hash()
   458  						expOldHash, _ := pm.blockchain.GetAncestor(nextHash, next, query.Skip+1, &maxNonCanonical)
   459  						if expOldHash == query.Origin.Hash {
   460  							query.Origin.Hash, query.Origin.Number = nextHash, next
   461  						} else {
   462  							unknown = true
   463  						}
   464  					} else {
   465  						unknown = true
   466  					}
   467  				}
   468  			case query.Reverse:
   469  				// Number based traversal towards the genesis block
   470  				if query.Origin.Number >= query.Skip+1 {
   471  					query.Origin.Number -= query.Skip + 1
   472  				} else {
   473  					unknown = true
   474  				}
   475  
   476  			case !query.Reverse:
   477  				// Number based traversal towards the leaf block
   478  				query.Origin.Number += query.Skip + 1
   479  			}
   480  		}
   481  		return p.SendBlockHeaders(headers)
   482  
   483  	case msg.Code == BlockHeadersMsg:
   484  		// A batch of headers arrived to one of our previous requests
   485  		var headers []*types.Header
   486  		if err := msg.Decode(&headers); err != nil {
   487  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   488  		}
   489  		// If no headers were received, but we're expencting a checkpoint header, consider it that
   490  		if len(headers) == 0 && p.syncDrop != nil {
   491  			// Stop the timer either way, decide later to drop or not
   492  			p.syncDrop.Stop()
   493  			p.syncDrop = nil
   494  
   495  			// If we're doing a fast sync, we must enforce the checkpoint block to avoid
   496  			// eclipse attacks. Unsynced nodes are welcome to connect after we're done
   497  			// joining the network
   498  			if atomic.LoadUint32(&pm.fastSync) == 1 {
   499  				p.Log().Warn("Dropping unsynced node during fast sync", "addr", p.RemoteAddr(), "type", p.Name())
   500  				return errors.New("unsynced node cannot serve fast sync")
   501  			}
   502  		}
   503  		// Filter out any explicitly requested headers, deliver the rest to the downloader
   504  		filter := len(headers) == 1
   505  		if filter {
   506  			// If it's a potential sync progress check, validate the content and advertised chain weight
   507  			if p.syncDrop != nil && headers[0].Number.Uint64() == pm.checkpointNumber {
   508  				// Disable the sync drop timer
   509  				p.syncDrop.Stop()
   510  				p.syncDrop = nil
   511  
   512  				// Validate the header and either drop the peer or continue
   513  				if headers[0].Hash() != pm.checkpointHash {
   514  					return errors.New("checkpoint hash mismatch")
   515  				}
   516  				return nil
   517  			}
   518  			// Otherwise if it's a whitelisted block, validate against the set
   519  			if want, ok := pm.whitelist[headers[0].Number.Uint64()]; ok {
   520  				if hash := headers[0].Hash(); want != hash {
   521  					p.Log().Info("Whitelist mismatch, dropping peer", "number", headers[0].Number.Uint64(), "hash", hash, "want", want)
   522  					return errors.New("whitelist block mismatch")
   523  				}
   524  				p.Log().Debug("Whitelist block verified", "number", headers[0].Number.Uint64(), "hash", want)
   525  			}
   526  			// Irrelevant of the fork checks, send the header to the fetcher just in case
   527  			headers = pm.blockFetcher.FilterHeaders(p.id, headers, time.Now())
   528  		}
   529  		if len(headers) > 0 || !filter {
   530  			err := pm.downloader.DeliverHeaders(p.id, headers)
   531  			if err != nil {
   532  				log.Debug("Failed to deliver headers", "err", err)
   533  			}
   534  		}
   535  
   536  	case msg.Code == GetBlockBodiesMsg:
   537  		// Decode the retrieval message
   538  		msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
   539  		if _, err := msgStream.List(); err != nil {
   540  			return err
   541  		}
   542  		// Gather blocks until the fetch or network limits is reached
   543  		var (
   544  			hash   common.Hash
   545  			bytes  int
   546  			bodies []rlp.RawValue
   547  		)
   548  		for bytes < softResponseLimit && len(bodies) < downloader.MaxBlockFetch {
   549  			// Retrieve the hash of the next block
   550  			if err := msgStream.Decode(&hash); err == rlp.EOL {
   551  				break
   552  			} else if err != nil {
   553  				return errResp(ErrDecode, "msg %v: %v", msg, err)
   554  			}
   555  			// Retrieve the requested block body, stopping if enough was found
   556  			if data := pm.blockchain.GetBodyRLP(hash); len(data) != 0 {
   557  				bodies = append(bodies, data)
   558  				bytes += len(data)
   559  			}
   560  		}
   561  		return p.SendBlockBodiesRLP(bodies)
   562  
   563  	case msg.Code == BlockBodiesMsg:
   564  		// A batch of block bodies arrived to one of our previous requests
   565  		var request blockBodiesData
   566  		if err := msg.Decode(&request); err != nil {
   567  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   568  		}
   569  		// Deliver them all to the downloader for queuing
   570  		transactions := make([][]*types.Transaction, len(request))
   571  		uncles := make([][]*types.Header, len(request))
   572  
   573  		for i, body := range request {
   574  			transactions[i] = body.Transactions
   575  			uncles[i] = body.Uncles
   576  		}
   577  		// Filter out any explicitly requested bodies, deliver the rest to the downloader
   578  		filter := len(transactions) > 0 || len(uncles) > 0
   579  		if filter {
   580  			transactions, uncles = pm.blockFetcher.FilterBodies(p.id, transactions, uncles, time.Now())
   581  		}
   582  		if len(transactions) > 0 || len(uncles) > 0 || !filter {
   583  			err := pm.downloader.DeliverBodies(p.id, transactions, uncles)
   584  			if err != nil {
   585  				log.Debug("Failed to deliver bodies", "err", err)
   586  			}
   587  		}
   588  
   589  	case p.version >= eth63 && msg.Code == GetNodeDataMsg:
   590  		// Decode the retrieval message
   591  		msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
   592  		if _, err := msgStream.List(); err != nil {
   593  			return err
   594  		}
   595  		// Gather state data until the fetch or network limits is reached
   596  		var (
   597  			hash  common.Hash
   598  			bytes int
   599  			data  [][]byte
   600  		)
   601  		for bytes < softResponseLimit && len(data) < downloader.MaxStateFetch {
   602  			// Retrieve the hash of the next state entry
   603  			if err := msgStream.Decode(&hash); err == rlp.EOL {
   604  				break
   605  			} else if err != nil {
   606  				return errResp(ErrDecode, "msg %v: %v", msg, err)
   607  			}
   608  			// Retrieve the requested state entry, stopping if enough was found
   609  			if entry, err := pm.blockchain.TrieNode(hash); err == nil {
   610  				data = append(data, entry)
   611  				bytes += len(entry)
   612  			}
   613  		}
   614  		return p.SendNodeData(data)
   615  
   616  	case p.version >= eth63 && msg.Code == NodeDataMsg:
   617  		// A batch of node state data arrived to one of our previous requests
   618  		var data [][]byte
   619  		if err := msg.Decode(&data); err != nil {
   620  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   621  		}
   622  		// Deliver all to the downloader
   623  		if err := pm.downloader.DeliverNodeData(p.id, data); err != nil {
   624  			log.Debug("Failed to deliver node state data", "err", err)
   625  		}
   626  
   627  	case p.version >= eth63 && msg.Code == GetReceiptsMsg:
   628  		// Decode the retrieval message
   629  		msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
   630  		if _, err := msgStream.List(); err != nil {
   631  			return err
   632  		}
   633  		// Gather state data until the fetch or network limits is reached
   634  		var (
   635  			hash     common.Hash
   636  			bytes    int
   637  			receipts []rlp.RawValue
   638  		)
   639  		for bytes < softResponseLimit && len(receipts) < downloader.MaxReceiptFetch {
   640  			// Retrieve the hash of the next block
   641  			if err := msgStream.Decode(&hash); err == rlp.EOL {
   642  				break
   643  			} else if err != nil {
   644  				return errResp(ErrDecode, "msg %v: %v", msg, err)
   645  			}
   646  			// Retrieve the requested block's receipts, skipping if unknown to us
   647  			results := pm.blockchain.GetReceiptsByHash(hash)
   648  			if results == nil {
   649  				if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
   650  					continue
   651  				}
   652  			}
   653  			// If known, encode and queue for response packet
   654  			if encoded, err := rlp.EncodeToBytes(results); err != nil {
   655  				log.Error("Failed to encode receipt", "err", err)
   656  			} else {
   657  				receipts = append(receipts, encoded)
   658  				bytes += len(encoded)
   659  			}
   660  		}
   661  		return p.SendReceiptsRLP(receipts)
   662  
   663  	case p.version >= eth63 && msg.Code == ReceiptsMsg:
   664  		// A batch of receipts arrived to one of our previous requests
   665  		var receipts [][]*types.Receipt
   666  		if err := msg.Decode(&receipts); err != nil {
   667  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   668  		}
   669  		// Deliver all to the downloader
   670  		if err := pm.downloader.DeliverReceipts(p.id, receipts); err != nil {
   671  			log.Debug("Failed to deliver receipts", "err", err)
   672  		}
   673  
   674  	case msg.Code == NewBlockHashesMsg:
   675  		var announces newBlockHashesData
   676  		if err := msg.Decode(&announces); err != nil {
   677  			return errResp(ErrDecode, "%v: %v", msg, err)
   678  		}
   679  		// Mark the hashes as present at the remote node
   680  		for _, block := range announces {
   681  			p.MarkBlock(block.Hash)
   682  		}
   683  		// Schedule all the unknown hashes for retrieval
   684  		unknown := make(newBlockHashesData, 0, len(announces))
   685  		for _, block := range announces {
   686  			if !pm.blockchain.HasBlock(block.Hash, block.Number) {
   687  				unknown = append(unknown, block)
   688  			}
   689  		}
   690  		for _, block := range unknown {
   691  			pm.blockFetcher.Notify(p.id, block.Hash, block.Number, time.Now(), p.RequestOneHeader, p.RequestBodies)
   692  		}
   693  
   694  	case msg.Code == NewBlockMsg:
   695  		// Retrieve and decode the propagated block
   696  		var request newBlockData
   697  		if err := msg.Decode(&request); err != nil {
   698  			return errResp(ErrDecode, "%v: %v", msg, err)
   699  		}
   700  		if hash := types.CalcUncleHash(request.Block.Uncles()); hash != request.Block.UncleHash() {
   701  			log.Warn("Propagated block has invalid uncles", "have", hash, "exp", request.Block.UncleHash())
   702  			break // TODO(karalabe): return error eventually, but wait a few releases
   703  		}
   704  		if hash := types.DeriveSha(request.Block.Transactions()); hash != request.Block.TxHash() {
   705  			log.Warn("Propagated block has invalid body", "have", hash, "exp", request.Block.TxHash())
   706  			break // TODO(karalabe): return error eventually, but wait a few releases
   707  		}
   708  		if err := request.sanityCheck(); err != nil {
   709  			return err
   710  		}
   711  		request.Block.ReceivedAt = msg.ReceivedAt
   712  		request.Block.ReceivedFrom = p
   713  
   714  		// Mark the peer as owning the block and schedule it for import
   715  		p.MarkBlock(request.Block.Hash())
   716  		pm.blockFetcher.Enqueue(p.id, request.Block)
   717  
   718  		// Assuming the block is importable by the peer, but possibly not yet done so,
   719  		// calculate the head hash and TD that the peer truly must have.
   720  		var (
   721  			trueHead = request.Block.ParentHash()
   722  			trueTD   = new(big.Int).Sub(request.TD, request.Block.Difficulty())
   723  		)
   724  		// Update the peer's total difficulty if better than the previous
   725  		if _, td := p.Head(); trueTD.Cmp(td) > 0 {
   726  			p.SetHead(trueHead, trueTD)
   727  			pm.chainSync.handlePeerEvent(p)
   728  		}
   729  
   730  	case msg.Code == NewPooledTransactionHashesMsg && p.version >= eth65:
   731  		// New transaction announcement arrived, make sure we have
   732  		// a valid and fresh chain to handle them
   733  		if atomic.LoadUint32(&pm.acceptTxs) == 0 {
   734  			break
   735  		}
   736  		var hashes []common.Hash
   737  		if err := msg.Decode(&hashes); err != nil {
   738  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   739  		}
   740  		// Schedule all the unknown hashes for retrieval
   741  		for _, hash := range hashes {
   742  			p.MarkTransaction(hash)
   743  		}
   744  		pm.txFetcher.Notify(p.id, hashes)
   745  
   746  	case msg.Code == GetPooledTransactionsMsg && p.version >= eth65:
   747  		// Decode the retrieval message
   748  		msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
   749  		if _, err := msgStream.List(); err != nil {
   750  			return err
   751  		}
   752  		// Gather transactions until the fetch or network limits is reached
   753  		var (
   754  			hash   common.Hash
   755  			bytes  int
   756  			hashes []common.Hash
   757  			txs    []rlp.RawValue
   758  		)
   759  		for bytes < softResponseLimit {
   760  			// Retrieve the hash of the next block
   761  			if err := msgStream.Decode(&hash); err == rlp.EOL {
   762  				break
   763  			} else if err != nil {
   764  				return errResp(ErrDecode, "msg %v: %v", msg, err)
   765  			}
   766  			// Retrieve the requested transaction, skipping if unknown to us
   767  			tx := pm.txpool.Get(hash)
   768  			if tx == nil {
   769  				continue
   770  			}
   771  			// If known, encode and queue for response packet
   772  			if encoded, err := rlp.EncodeToBytes(tx); err != nil {
   773  				log.Error("Failed to encode transaction", "err", err)
   774  			} else {
   775  				hashes = append(hashes, hash)
   776  				txs = append(txs, encoded)
   777  				bytes += len(encoded)
   778  			}
   779  		}
   780  		return p.SendPooledTransactionsRLP(hashes, txs)
   781  
   782  	case msg.Code == TransactionMsg || (msg.Code == PooledTransactionsMsg && p.version >= eth65):
   783  		// Transactions arrived, make sure we have a valid and fresh chain to handle them
   784  		if atomic.LoadUint32(&pm.acceptTxs) == 0 {
   785  			break
   786  		}
   787  		// Transactions can be processed, parse all of them and deliver to the pool
   788  		var txs []*types.Transaction
   789  		if err := msg.Decode(&txs); err != nil {
   790  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   791  		}
   792  		for i, tx := range txs {
   793  			// Validate and mark the remote transaction
   794  			if tx == nil {
   795  				return errResp(ErrDecode, "transaction %d is nil", i)
   796  			}
   797  			p.MarkTransaction(tx.Hash())
   798  		}
   799  		pm.txFetcher.Enqueue(p.id, txs, msg.Code == PooledTransactionsMsg)
   800  
   801  	default:
   802  		return errResp(ErrInvalidMsgCode, "%v", msg.Code)
   803  	}
   804  	return nil
   805  }
   806  
   807  // BroadcastBlock will either propagate a block to a subset of its peers, or
   808  // will only announce its availability (depending what's requested).
   809  func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
   810  	hash := block.Hash()
   811  	peers := pm.peers.PeersWithoutBlock(hash)
   812  
   813  	// If propagation is requested, send to a subset of the peer
   814  	if propagate {
   815  		// Calculate the TD of the block (it's not imported yet, so block.Td is not valid)
   816  		var td *big.Int
   817  		if parent := pm.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent != nil {
   818  			td = new(big.Int).Add(block.Difficulty(), pm.blockchain.GetTd(block.ParentHash(), block.NumberU64()-1))
   819  		} else {
   820  			log.Error("Propagating dangling block", "number", block.Number(), "hash", hash)
   821  			return
   822  		}
   823  		// Send the block to a subset of our peers
   824  		transfer := peers[:int(math.Sqrt(float64(len(peers))))]
   825  		for _, peer := range transfer {
   826  			peer.AsyncSendNewBlock(block, td)
   827  		}
   828  		log.Trace("Propagated block", "hash", hash, "recipients", len(transfer), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
   829  		return
   830  	}
   831  	// Otherwise if the block is indeed in out own chain, announce it
   832  	if pm.blockchain.HasBlock(hash, block.NumberU64()) {
   833  		for _, peer := range peers {
   834  			peer.AsyncSendNewBlockHash(block)
   835  		}
   836  		log.Trace("Announced block", "hash", hash, "recipients", len(peers), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
   837  	}
   838  }
   839  
   840  // BroadcastTransactions will propagate a batch of transactions to all peers which are not known to
   841  // already have the given transaction.
   842  func (pm *ProtocolManager) BroadcastTransactions(txs types.Transactions, propagate bool) {
   843  	var (
   844  		txset = make(map[*peer][]common.Hash)
   845  		annos = make(map[*peer][]common.Hash)
   846  	)
   847  	// Broadcast transactions to a batch of peers not knowing about it
   848  	if propagate {
   849  		for _, tx := range txs {
   850  			peers := pm.peers.PeersWithoutTx(tx.Hash())
   851  
   852  			// Send the block to a subset of our peers
   853  			transfer := peers[:int(math.Sqrt(float64(len(peers))))]
   854  			for _, peer := range transfer {
   855  				txset[peer] = append(txset[peer], tx.Hash())
   856  			}
   857  			log.Trace("Broadcast transaction", "hash", tx.Hash(), "recipients", len(peers))
   858  		}
   859  		for peer, hashes := range txset {
   860  			peer.AsyncSendTransactions(hashes)
   861  		}
   862  		return
   863  	}
   864  	// Otherwise only broadcast the announcement to peers
   865  	for _, tx := range txs {
   866  		peers := pm.peers.PeersWithoutTx(tx.Hash())
   867  		for _, peer := range peers {
   868  			annos[peer] = append(annos[peer], tx.Hash())
   869  		}
   870  	}
   871  	for peer, hashes := range annos {
   872  		if peer.version >= eth65 {
   873  			peer.AsyncSendPooledTransactionHashes(hashes)
   874  		} else {
   875  			peer.AsyncSendTransactions(hashes)
   876  		}
   877  	}
   878  }
   879  
   880  // minedBroadcastLoop sends mined blocks to connected peers.
   881  func (pm *ProtocolManager) minedBroadcastLoop() {
   882  	defer pm.wg.Done()
   883  
   884  	for obj := range pm.minedBlockSub.Chan() {
   885  		if ev, ok := obj.Data.(core.NewMinedBlockEvent); ok {
   886  			pm.BroadcastBlock(ev.Block, true)  // First propagate block to peers
   887  			pm.BroadcastBlock(ev.Block, false) // Only then announce to the rest
   888  		}
   889  	}
   890  }
   891  
   892  // txBroadcastLoop announces new transactions to connected peers.
   893  func (pm *ProtocolManager) txBroadcastLoop() {
   894  	defer pm.wg.Done()
   895  
   896  	for {
   897  		select {
   898  		case event := <-pm.txsCh:
   899  			// For testing purpose only, disable propagation
   900  			if pm.broadcastTxAnnouncesOnly {
   901  				pm.BroadcastTransactions(event.Txs, false)
   902  				continue
   903  			}
   904  			pm.BroadcastTransactions(event.Txs, true)  // First propagate transactions to peers
   905  			pm.BroadcastTransactions(event.Txs, false) // Only then announce to the rest
   906  
   907  		case <-pm.txsSub.Err():
   908  			return
   909  		}
   910  	}
   911  }
   912  
   913  // NodeInfo represents a short summary of the Ethereum sub-protocol metadata
   914  // known about the host peer.
   915  type NodeInfo struct {
   916  	Network    uint64              `json:"network"`    // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
   917  	Difficulty *big.Int            `json:"difficulty"` // Total difficulty of the host's blockchain
   918  	Genesis    common.Hash         `json:"genesis"`    // SHA3 hash of the host's genesis block
   919  	Config     *params.ChainConfig `json:"config"`     // Chain configuration for the fork rules
   920  	Head       common.Hash         `json:"head"`       // SHA3 hash of the host's best owned block
   921  }
   922  
   923  // NodeInfo retrieves some protocol metadata about the running host node.
   924  func (pm *ProtocolManager) NodeInfo() *NodeInfo {
   925  	currentBlock := pm.blockchain.CurrentBlock()
   926  	return &NodeInfo{
   927  		Network:    pm.networkID,
   928  		Difficulty: pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()),
   929  		Genesis:    pm.blockchain.Genesis().Hash(),
   930  		Config:     pm.blockchain.Config(),
   931  		Head:       currentBlock.Hash(),
   932  	}
   933  }