github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/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/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/core"
    31  	"github.com/ethereum/go-ethereum/core/types"
    32  	"github.com/ethereum/go-ethereum/eth/downloader"
    33  	"github.com/ethereum/go-ethereum/eth/fetcher"
    34  	"github.com/ethereum/go-ethereum/ethdb"
    35  	"github.com/ethereum/go-ethereum/event"
    36  	"github.com/ethereum/go-ethereum/log"
    37  	"github.com/ethereum/go-ethereum/p2p"
    38  	"github.com/ethereum/go-ethereum/p2p/discover"
    39  	"github.com/ethereum/go-ethereum/params"
    40  	"github.com/ethereum/go-ethereum/pow"
    41  	"github.com/ethereum/go-ethereum/rlp"
    42  )
    43  
    44  const (
    45  	softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data.
    46  	estHeaderRlpSize  = 500             // Approximate size of an RLP encoded block header
    47  )
    48  
    49  var (
    50  	daoChallengeTimeout = 15 * time.Second // Time allowance for a node to reply to the DAO handshake challenge
    51  )
    52  
    53  // errIncompatibleConfig is returned if the requested protocols and configs are
    54  // not compatible (low protocol version restrictions and high requirements).
    55  var errIncompatibleConfig = errors.New("incompatible configuration")
    56  
    57  func errResp(code errCode, format string, v ...interface{}) error {
    58  	return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
    59  }
    60  
    61  type ProtocolManager struct {
    62  	networkId int
    63  
    64  	fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
    65  	synced   uint32 // Flag whether we're considered synchronised (enables transaction processing)
    66  
    67  	txpool      txPool
    68  	blockchain  *core.BlockChain
    69  	chaindb     ethdb.Database
    70  	chainconfig *params.ChainConfig
    71  	maxPeers    int
    72  
    73  	downloader *downloader.Downloader
    74  	fetcher    *fetcher.Fetcher
    75  	peers      *peerSet
    76  
    77  	SubProtocols []p2p.Protocol
    78  
    79  	eventMux      *event.TypeMux
    80  	txSub         *event.TypeMuxSubscription
    81  	minedBlockSub *event.TypeMuxSubscription
    82  
    83  	// channels for fetcher, syncer, txsyncLoop
    84  	newPeerCh   chan *peer
    85  	txsyncCh    chan *txsync
    86  	quitSync    chan struct{}
    87  	noMorePeers chan struct{}
    88  
    89  	lesServer LesServer
    90  
    91  	// wait group is used for graceful shutdowns during downloading
    92  	// and processing
    93  	wg sync.WaitGroup
    94  
    95  	badBlockReportingEnabled bool
    96  }
    97  
    98  // NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
    99  // with the ethereum network.
   100  func NewProtocolManager(config *params.ChainConfig, fastSync bool, networkId int, maxPeers int, mux *event.TypeMux, txpool txPool, pow pow.PoW, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
   101  	// Create the protocol manager with the base fields
   102  	manager := &ProtocolManager{
   103  		networkId:   networkId,
   104  		eventMux:    mux,
   105  		txpool:      txpool,
   106  		blockchain:  blockchain,
   107  		chaindb:     chaindb,
   108  		chainconfig: config,
   109  		maxPeers:    maxPeers,
   110  		peers:       newPeerSet(),
   111  		newPeerCh:   make(chan *peer),
   112  		noMorePeers: make(chan struct{}),
   113  		txsyncCh:    make(chan *txsync),
   114  		quitSync:    make(chan struct{}),
   115  	}
   116  	// Figure out whether to allow fast sync or not
   117  	if fastSync && blockchain.CurrentBlock().NumberU64() > 0 {
   118  		log.Warn("Blockchain not empty, fast sync disabled")
   119  		fastSync = false
   120  	}
   121  	if fastSync {
   122  		manager.fastSync = uint32(1)
   123  	}
   124  	// Initiate a sub-protocol for every implemented version we can handle
   125  	manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions))
   126  	for i, version := range ProtocolVersions {
   127  		// Skip protocol version if incompatible with the mode of operation
   128  		if fastSync && version < eth63 {
   129  			continue
   130  		}
   131  		// Compatible; initialise the sub-protocol
   132  		version := version // Closure for the run
   133  		manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{
   134  			Name:    ProtocolName,
   135  			Version: version,
   136  			Length:  ProtocolLengths[i],
   137  			Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
   138  				peer := manager.newPeer(int(version), p, rw)
   139  				select {
   140  				case manager.newPeerCh <- peer:
   141  					manager.wg.Add(1)
   142  					defer manager.wg.Done()
   143  					return manager.handle(peer)
   144  				case <-manager.quitSync:
   145  					return p2p.DiscQuitting
   146  				}
   147  			},
   148  			NodeInfo: func() interface{} {
   149  				return manager.NodeInfo()
   150  			},
   151  			PeerInfo: func(id discover.NodeID) interface{} {
   152  				if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
   153  					return p.Info()
   154  				}
   155  				return nil
   156  			},
   157  		})
   158  	}
   159  	if len(manager.SubProtocols) == 0 {
   160  		return nil, errIncompatibleConfig
   161  	}
   162  	// Construct the different synchronisation mechanisms
   163  	manager.downloader = downloader.New(downloader.FullSync, chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeaderByHash,
   164  		blockchain.GetBlockByHash, blockchain.CurrentHeader, blockchain.CurrentBlock, blockchain.CurrentFastBlock, blockchain.FastSyncCommitHead,
   165  		blockchain.GetTdByHash, blockchain.InsertHeaderChain, manager.insertChain, blockchain.InsertReceiptChain, blockchain.Rollback,
   166  		manager.removePeer)
   167  
   168  	validator := func(block *types.Block, parent *types.Block) error {
   169  		return core.ValidateHeader(config, pow, block.Header(), parent.Header(), true, false)
   170  	}
   171  	heighter := func() uint64 {
   172  		return blockchain.CurrentBlock().NumberU64()
   173  	}
   174  	inserter := func(blocks types.Blocks) (int, error) {
   175  		atomic.StoreUint32(&manager.synced, 1) // Mark initial sync done on any fetcher import
   176  		return manager.insertChain(blocks)
   177  	}
   178  	manager.fetcher = fetcher.New(blockchain.GetBlockByHash, validator, manager.BroadcastBlock, heighter, inserter, manager.removePeer)
   179  
   180  	if blockchain.Genesis().Hash().Hex() == defaultGenesisHash && networkId == 1 {
   181  		log.Debug("Bad block reporting is enabled")
   182  		manager.badBlockReportingEnabled = true
   183  	}
   184  
   185  	return manager, nil
   186  }
   187  
   188  func (pm *ProtocolManager) insertChain(blocks types.Blocks) (i int, err error) {
   189  	i, err = pm.blockchain.InsertChain(blocks)
   190  	if pm.badBlockReportingEnabled && core.IsValidationErr(err) && i < len(blocks) {
   191  		go sendBadBlockReport(blocks[i], err)
   192  	}
   193  	return i, err
   194  }
   195  
   196  func (pm *ProtocolManager) removePeer(id string) {
   197  	// Short circuit if the peer was already removed
   198  	peer := pm.peers.Peer(id)
   199  	if peer == nil {
   200  		return
   201  	}
   202  	log.Debug("Removing Ethereum peer", "peer", id)
   203  
   204  	// Unregister the peer from the downloader and Ethereum peer set
   205  	pm.downloader.UnregisterPeer(id)
   206  	if err := pm.peers.Unregister(id); err != nil {
   207  		log.Error("Peer removal failed", "peer", id, "err", err)
   208  	}
   209  	// Hard disconnect at the networking layer
   210  	if peer != nil {
   211  		peer.Peer.Disconnect(p2p.DiscUselessPeer)
   212  	}
   213  }
   214  
   215  func (pm *ProtocolManager) Start() {
   216  	// broadcast transactions
   217  	pm.txSub = pm.eventMux.Subscribe(core.TxPreEvent{})
   218  	go pm.txBroadcastLoop()
   219  	// broadcast mined blocks
   220  	pm.minedBlockSub = pm.eventMux.Subscribe(core.NewMinedBlockEvent{})
   221  	go pm.minedBroadcastLoop()
   222  
   223  	// start sync handlers
   224  	go pm.syncer()
   225  	go pm.txsyncLoop()
   226  }
   227  
   228  func (pm *ProtocolManager) Stop() {
   229  	log.Info("Stopping Ethereum protocol")
   230  
   231  	pm.txSub.Unsubscribe()         // quits txBroadcastLoop
   232  	pm.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
   233  
   234  	// Quit the sync loop.
   235  	// After this send has completed, no new peers will be accepted.
   236  	pm.noMorePeers <- struct{}{}
   237  
   238  	// Quit fetcher, txsyncLoop.
   239  	close(pm.quitSync)
   240  
   241  	// Disconnect existing sessions.
   242  	// This also closes the gate for any new registrations on the peer set.
   243  	// sessions which are already established but not added to pm.peers yet
   244  	// will exit when they try to register.
   245  	pm.peers.Close()
   246  
   247  	// Wait for all peer handler goroutines and the loops to come down.
   248  	pm.wg.Wait()
   249  
   250  	log.Info("Ethereum protocol stopped")
   251  }
   252  
   253  func (pm *ProtocolManager) newPeer(pv int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
   254  	return newPeer(pv, p, newMeteredMsgWriter(rw))
   255  }
   256  
   257  // handle is the callback invoked to manage the life cycle of an eth peer. When
   258  // this function terminates, the peer is disconnected.
   259  func (pm *ProtocolManager) handle(p *peer) error {
   260  	if pm.peers.Len() >= pm.maxPeers {
   261  		return p2p.DiscTooManyPeers
   262  	}
   263  	p.Log().Debug("Ethereum peer connected", "name", p.Name())
   264  
   265  	// Execute the Ethereum handshake
   266  	td, head, genesis := pm.blockchain.Status()
   267  	if err := p.Handshake(pm.networkId, td, head, genesis); err != nil {
   268  		p.Log().Debug("Ethereum handshake failed", "err", err)
   269  		return err
   270  	}
   271  	if rw, ok := p.rw.(*meteredMsgReadWriter); ok {
   272  		rw.Init(p.version)
   273  	}
   274  	// Register the peer locally
   275  	if err := pm.peers.Register(p); err != nil {
   276  		p.Log().Error("Ethereum peer registration failed", "err", err)
   277  		return err
   278  	}
   279  	defer pm.removePeer(p.id)
   280  
   281  	// Register the peer in the downloader. If the downloader considers it banned, we disconnect
   282  	if err := pm.downloader.RegisterPeer(p.id, p.version, p.Head, p.RequestHeadersByHash, p.RequestHeadersByNumber, p.RequestBodies, p.RequestReceipts, p.RequestNodeData); err != nil {
   283  		return err
   284  	}
   285  	// Propagate existing transactions. new transactions appearing
   286  	// after this will be sent via broadcasts.
   287  	pm.syncTransactions(p)
   288  
   289  	// If we're DAO hard-fork aware, validate any remote peer with regard to the hard-fork
   290  	if daoBlock := pm.chainconfig.DAOForkBlock; daoBlock != nil {
   291  		// Request the peer's DAO fork header for extra-data validation
   292  		if err := p.RequestHeadersByNumber(daoBlock.Uint64(), 1, 0, false); err != nil {
   293  			return err
   294  		}
   295  		// Start a timer to disconnect if the peer doesn't reply in time
   296  		p.forkDrop = time.AfterFunc(daoChallengeTimeout, func() {
   297  			p.Log().Debug("Timed out DAO fork-check, dropping")
   298  			pm.removePeer(p.id)
   299  		})
   300  		// Make sure it's cleaned up if the peer dies off
   301  		defer func() {
   302  			if p.forkDrop != nil {
   303  				p.forkDrop.Stop()
   304  				p.forkDrop = nil
   305  			}
   306  		}()
   307  	}
   308  	// main loop. handle incoming messages.
   309  	for {
   310  		if err := pm.handleMsg(p); err != nil {
   311  			p.Log().Debug("Ethereum message handling failed", "err", err)
   312  			return err
   313  		}
   314  	}
   315  }
   316  
   317  // handleMsg is invoked whenever an inbound message is received from a remote
   318  // peer. The remote connection is torn down upon returning any error.
   319  func (pm *ProtocolManager) handleMsg(p *peer) error {
   320  	// Read the next message from the remote peer, and ensure it's fully consumed
   321  	msg, err := p.rw.ReadMsg()
   322  	if err != nil {
   323  		return err
   324  	}
   325  	if msg.Size > ProtocolMaxMsgSize {
   326  		return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize)
   327  	}
   328  	defer msg.Discard()
   329  
   330  	// Handle the message depending on its contents
   331  	switch {
   332  	case msg.Code == StatusMsg:
   333  		// Status messages should never arrive after the handshake
   334  		return errResp(ErrExtraStatusMsg, "uncontrolled status message")
   335  
   336  	// Block header query, collect the requested headers and reply
   337  	case msg.Code == GetBlockHeadersMsg:
   338  		// Decode the complex header query
   339  		var query getBlockHeadersData
   340  		if err := msg.Decode(&query); err != nil {
   341  			return errResp(ErrDecode, "%v: %v", msg, err)
   342  		}
   343  		hashMode := query.Origin.Hash != (common.Hash{})
   344  
   345  		// Gather headers until the fetch or network limits is reached
   346  		var (
   347  			bytes   common.StorageSize
   348  			headers []*types.Header
   349  			unknown bool
   350  		)
   351  		for !unknown && len(headers) < int(query.Amount) && bytes < softResponseLimit && len(headers) < downloader.MaxHeaderFetch {
   352  			// Retrieve the next header satisfying the query
   353  			var origin *types.Header
   354  			if hashMode {
   355  				origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash)
   356  			} else {
   357  				origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number)
   358  			}
   359  			if origin == nil {
   360  				break
   361  			}
   362  			number := origin.Number.Uint64()
   363  			headers = append(headers, origin)
   364  			bytes += estHeaderRlpSize
   365  
   366  			// Advance to the next header of the query
   367  			switch {
   368  			case query.Origin.Hash != (common.Hash{}) && query.Reverse:
   369  				// Hash based traversal towards the genesis block
   370  				for i := 0; i < int(query.Skip)+1; i++ {
   371  					if header := pm.blockchain.GetHeader(query.Origin.Hash, number); header != nil {
   372  						query.Origin.Hash = header.ParentHash
   373  						number--
   374  					} else {
   375  						unknown = true
   376  						break
   377  					}
   378  				}
   379  			case query.Origin.Hash != (common.Hash{}) && !query.Reverse:
   380  				// Hash based traversal towards the leaf block
   381  				var (
   382  					current = origin.Number.Uint64()
   383  					next    = current + query.Skip + 1
   384  				)
   385  				if next <= current {
   386  					infos, _ := json.MarshalIndent(p.Peer.Info(), "", "  ")
   387  					p.Log().Warn("GetBlockHeaders skip overflow attack", "current", current, "skip", query.Skip, "next", next, "attacker", infos)
   388  					unknown = true
   389  				} else {
   390  					if header := pm.blockchain.GetHeaderByNumber(next); header != nil {
   391  						if pm.blockchain.GetBlockHashesFromHash(header.Hash(), query.Skip+1)[query.Skip] == query.Origin.Hash {
   392  							query.Origin.Hash = header.Hash()
   393  						} else {
   394  							unknown = true
   395  						}
   396  					} else {
   397  						unknown = true
   398  					}
   399  				}
   400  			case query.Reverse:
   401  				// Number based traversal towards the genesis block
   402  				if query.Origin.Number >= query.Skip+1 {
   403  					query.Origin.Number -= (query.Skip + 1)
   404  				} else {
   405  					unknown = true
   406  				}
   407  
   408  			case !query.Reverse:
   409  				// Number based traversal towards the leaf block
   410  				query.Origin.Number += (query.Skip + 1)
   411  			}
   412  		}
   413  		return p.SendBlockHeaders(headers)
   414  
   415  	case msg.Code == BlockHeadersMsg:
   416  		// A batch of headers arrived to one of our previous requests
   417  		var headers []*types.Header
   418  		if err := msg.Decode(&headers); err != nil {
   419  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   420  		}
   421  		// If no headers were received, but we're expending a DAO fork check, maybe it's that
   422  		if len(headers) == 0 && p.forkDrop != nil {
   423  			// Possibly an empty reply to the fork header checks, sanity check TDs
   424  			verifyDAO := true
   425  
   426  			// If we already have a DAO header, we can check the peer's TD against it. If
   427  			// the peer's ahead of this, it too must have a reply to the DAO check
   428  			if daoHeader := pm.blockchain.GetHeaderByNumber(pm.chainconfig.DAOForkBlock.Uint64()); daoHeader != nil {
   429  				if _, td := p.Head(); td.Cmp(pm.blockchain.GetTd(daoHeader.Hash(), daoHeader.Number.Uint64())) >= 0 {
   430  					verifyDAO = false
   431  				}
   432  			}
   433  			// If we're seemingly on the same chain, disable the drop timer
   434  			if verifyDAO {
   435  				p.Log().Debug("Seems to be on the same side of the DAO fork")
   436  				p.forkDrop.Stop()
   437  				p.forkDrop = nil
   438  				return nil
   439  			}
   440  		}
   441  		// Filter out any explicitly requested headers, deliver the rest to the downloader
   442  		filter := len(headers) == 1
   443  		if filter {
   444  			// If it's a potential DAO fork check, validate against the rules
   445  			if p.forkDrop != nil && pm.chainconfig.DAOForkBlock.Cmp(headers[0].Number) == 0 {
   446  				// Disable the fork drop timer
   447  				p.forkDrop.Stop()
   448  				p.forkDrop = nil
   449  
   450  				// Validate the header and either drop the peer or continue
   451  				if err := core.ValidateDAOHeaderExtraData(pm.chainconfig, headers[0]); err != nil {
   452  					p.Log().Debug("Verified to be on the other side of the DAO fork, dropping")
   453  					return err
   454  				}
   455  				p.Log().Debug("Verified to be on the same side of the DAO fork")
   456  				return nil
   457  			}
   458  			// Irrelevant of the fork checks, send the header to the fetcher just in case
   459  			headers = pm.fetcher.FilterHeaders(headers, time.Now())
   460  		}
   461  		if len(headers) > 0 || !filter {
   462  			err := pm.downloader.DeliverHeaders(p.id, headers)
   463  			if err != nil {
   464  				log.Debug("Failed to deliver headers", "err", err)
   465  			}
   466  		}
   467  
   468  	case msg.Code == GetBlockBodiesMsg:
   469  		// Decode the retrieval message
   470  		msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
   471  		if _, err := msgStream.List(); err != nil {
   472  			return err
   473  		}
   474  		// Gather blocks until the fetch or network limits is reached
   475  		var (
   476  			hash   common.Hash
   477  			bytes  int
   478  			bodies []rlp.RawValue
   479  		)
   480  		for bytes < softResponseLimit && len(bodies) < downloader.MaxBlockFetch {
   481  			// Retrieve the hash of the next block
   482  			if err := msgStream.Decode(&hash); err == rlp.EOL {
   483  				break
   484  			} else if err != nil {
   485  				return errResp(ErrDecode, "msg %v: %v", msg, err)
   486  			}
   487  			// Retrieve the requested block body, stopping if enough was found
   488  			if data := pm.blockchain.GetBodyRLP(hash); len(data) != 0 {
   489  				bodies = append(bodies, data)
   490  				bytes += len(data)
   491  			}
   492  		}
   493  		return p.SendBlockBodiesRLP(bodies)
   494  
   495  	case msg.Code == BlockBodiesMsg:
   496  		// A batch of block bodies arrived to one of our previous requests
   497  		var request blockBodiesData
   498  		if err := msg.Decode(&request); err != nil {
   499  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   500  		}
   501  		// Deliver them all to the downloader for queuing
   502  		trasactions := make([][]*types.Transaction, len(request))
   503  		uncles := make([][]*types.Header, len(request))
   504  
   505  		for i, body := range request {
   506  			trasactions[i] = body.Transactions
   507  			uncles[i] = body.Uncles
   508  		}
   509  		// Filter out any explicitly requested bodies, deliver the rest to the downloader
   510  		filter := len(trasactions) > 0 || len(uncles) > 0
   511  		if filter {
   512  			trasactions, uncles = pm.fetcher.FilterBodies(trasactions, uncles, time.Now())
   513  		}
   514  		if len(trasactions) > 0 || len(uncles) > 0 || !filter {
   515  			err := pm.downloader.DeliverBodies(p.id, trasactions, uncles)
   516  			if err != nil {
   517  				log.Debug("Failed to deliver bodies", "err", err)
   518  			}
   519  		}
   520  
   521  	case p.version >= eth63 && msg.Code == GetNodeDataMsg:
   522  		// Decode the retrieval message
   523  		msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
   524  		if _, err := msgStream.List(); err != nil {
   525  			return err
   526  		}
   527  		// Gather state data until the fetch or network limits is reached
   528  		var (
   529  			hash  common.Hash
   530  			bytes int
   531  			data  [][]byte
   532  		)
   533  		for bytes < softResponseLimit && len(data) < downloader.MaxStateFetch {
   534  			// Retrieve the hash of the next state entry
   535  			if err := msgStream.Decode(&hash); err == rlp.EOL {
   536  				break
   537  			} else if err != nil {
   538  				return errResp(ErrDecode, "msg %v: %v", msg, err)
   539  			}
   540  			// Retrieve the requested state entry, stopping if enough was found
   541  			if entry, err := pm.chaindb.Get(hash.Bytes()); err == nil {
   542  				data = append(data, entry)
   543  				bytes += len(entry)
   544  			}
   545  		}
   546  		return p.SendNodeData(data)
   547  
   548  	case p.version >= eth63 && msg.Code == NodeDataMsg:
   549  		// A batch of node state data arrived to one of our previous requests
   550  		var data [][]byte
   551  		if err := msg.Decode(&data); err != nil {
   552  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   553  		}
   554  		// Deliver all to the downloader
   555  		if err := pm.downloader.DeliverNodeData(p.id, data); err != nil {
   556  			log.Debug("Failed to deliver node state data", "err", err)
   557  		}
   558  
   559  	case p.version >= eth63 && msg.Code == GetReceiptsMsg:
   560  		// Decode the retrieval message
   561  		msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
   562  		if _, err := msgStream.List(); err != nil {
   563  			return err
   564  		}
   565  		// Gather state data until the fetch or network limits is reached
   566  		var (
   567  			hash     common.Hash
   568  			bytes    int
   569  			receipts []rlp.RawValue
   570  		)
   571  		for bytes < softResponseLimit && len(receipts) < downloader.MaxReceiptFetch {
   572  			// Retrieve the hash of the next block
   573  			if err := msgStream.Decode(&hash); err == rlp.EOL {
   574  				break
   575  			} else if err != nil {
   576  				return errResp(ErrDecode, "msg %v: %v", msg, err)
   577  			}
   578  			// Retrieve the requested block's receipts, skipping if unknown to us
   579  			results := core.GetBlockReceipts(pm.chaindb, hash, core.GetBlockNumber(pm.chaindb, hash))
   580  			if results == nil {
   581  				if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
   582  					continue
   583  				}
   584  			}
   585  			// If known, encode and queue for response packet
   586  			if encoded, err := rlp.EncodeToBytes(results); err != nil {
   587  				log.Error("Failed to encode receipt", "err", err)
   588  			} else {
   589  				receipts = append(receipts, encoded)
   590  				bytes += len(encoded)
   591  			}
   592  		}
   593  		return p.SendReceiptsRLP(receipts)
   594  
   595  	case p.version >= eth63 && msg.Code == ReceiptsMsg:
   596  		// A batch of receipts arrived to one of our previous requests
   597  		var receipts [][]*types.Receipt
   598  		if err := msg.Decode(&receipts); err != nil {
   599  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   600  		}
   601  		// Deliver all to the downloader
   602  		if err := pm.downloader.DeliverReceipts(p.id, receipts); err != nil {
   603  			log.Debug("Failed to deliver receipts", "err", err)
   604  		}
   605  
   606  	case msg.Code == NewBlockHashesMsg:
   607  		var announces newBlockHashesData
   608  		if err := msg.Decode(&announces); err != nil {
   609  			return errResp(ErrDecode, "%v: %v", msg, err)
   610  		}
   611  		// Mark the hashes as present at the remote node
   612  		for _, block := range announces {
   613  			p.MarkBlock(block.Hash)
   614  		}
   615  		// Schedule all the unknown hashes for retrieval
   616  		unknown := make(newBlockHashesData, 0, len(announces))
   617  		for _, block := range announces {
   618  			if !pm.blockchain.HasBlock(block.Hash) {
   619  				unknown = append(unknown, block)
   620  			}
   621  		}
   622  		for _, block := range unknown {
   623  			pm.fetcher.Notify(p.id, block.Hash, block.Number, time.Now(), p.RequestOneHeader, p.RequestBodies)
   624  		}
   625  
   626  	case msg.Code == NewBlockMsg:
   627  		// Retrieve and decode the propagated block
   628  		var request newBlockData
   629  		if err := msg.Decode(&request); err != nil {
   630  			return errResp(ErrDecode, "%v: %v", msg, err)
   631  		}
   632  		request.Block.ReceivedAt = msg.ReceivedAt
   633  		request.Block.ReceivedFrom = p
   634  
   635  		// Mark the peer as owning the block and schedule it for import
   636  		p.MarkBlock(request.Block.Hash())
   637  		pm.fetcher.Enqueue(p.id, request.Block)
   638  
   639  		// Assuming the block is importable by the peer, but possibly not yet done so,
   640  		// calculate the head hash and TD that the peer truly must have.
   641  		var (
   642  			trueHead = request.Block.ParentHash()
   643  			trueTD   = new(big.Int).Sub(request.TD, request.Block.Difficulty())
   644  		)
   645  		// Update the peers total difficulty if better than the previous
   646  		if _, td := p.Head(); trueTD.Cmp(td) > 0 {
   647  			p.SetHead(trueHead, trueTD)
   648  
   649  			// Schedule a sync if above ours. Note, this will not fire a sync for a gap of
   650  			// a singe block (as the true TD is below the propagated block), however this
   651  			// scenario should easily be covered by the fetcher.
   652  			currentBlock := pm.blockchain.CurrentBlock()
   653  			if trueTD.Cmp(pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())) > 0 {
   654  				go pm.synchronise(p)
   655  			}
   656  		}
   657  
   658  	case msg.Code == TxMsg:
   659  		// Transactions arrived, make sure we have a valid and fresh chain to handle them
   660  		if atomic.LoadUint32(&pm.synced) == 0 {
   661  			break
   662  		}
   663  		// Transactions can be processed, parse all of them and deliver to the pool
   664  		var txs []*types.Transaction
   665  		if err := msg.Decode(&txs); err != nil {
   666  			return errResp(ErrDecode, "msg %v: %v", msg, err)
   667  		}
   668  		for i, tx := range txs {
   669  			// Validate and mark the remote transaction
   670  			if tx == nil {
   671  				return errResp(ErrDecode, "transaction %d is nil", i)
   672  			}
   673  			p.MarkTransaction(tx.Hash())
   674  		}
   675  		pm.txpool.AddBatch(txs)
   676  
   677  	default:
   678  		return errResp(ErrInvalidMsgCode, "%v", msg.Code)
   679  	}
   680  	return nil
   681  }
   682  
   683  // BroadcastBlock will either propagate a block to a subset of it's peers, or
   684  // will only announce it's availability (depending what's requested).
   685  func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
   686  	hash := block.Hash()
   687  	peers := pm.peers.PeersWithoutBlock(hash)
   688  
   689  	// If propagation is requested, send to a subset of the peer
   690  	if propagate {
   691  		// Calculate the TD of the block (it's not imported yet, so block.Td is not valid)
   692  		var td *big.Int
   693  		if parent := pm.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent != nil {
   694  			td = new(big.Int).Add(block.Difficulty(), pm.blockchain.GetTd(block.ParentHash(), block.NumberU64()-1))
   695  		} else {
   696  			log.Error("Propagating dangling block", "number", block.Number(), "hash", hash)
   697  			return
   698  		}
   699  		// Send the block to a subset of our peers
   700  		transfer := peers[:int(math.Sqrt(float64(len(peers))))]
   701  		for _, peer := range transfer {
   702  			peer.SendNewBlock(block, td)
   703  		}
   704  		log.Trace("Propagated block", "hash", hash, "recipients", len(transfer), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
   705  	}
   706  	// Otherwise if the block is indeed in out own chain, announce it
   707  	if pm.blockchain.HasBlock(hash) {
   708  		for _, peer := range peers {
   709  			peer.SendNewBlockHashes([]common.Hash{hash}, []uint64{block.NumberU64()})
   710  		}
   711  		log.Trace("Announced block", "hash", hash, "recipients", len(peers), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
   712  	}
   713  }
   714  
   715  // BroadcastTx will propagate a transaction to all peers which are not known to
   716  // already have the given transaction.
   717  func (pm *ProtocolManager) BroadcastTx(hash common.Hash, tx *types.Transaction) {
   718  	// Broadcast transaction to a batch of peers not knowing about it
   719  	peers := pm.peers.PeersWithoutTx(hash)
   720  	//FIXME include this again: peers = peers[:int(math.Sqrt(float64(len(peers))))]
   721  	for _, peer := range peers {
   722  		peer.SendTransactions(types.Transactions{tx})
   723  	}
   724  	log.Trace("Broadcast transaction", "hash", hash, "recipients", len(peers))
   725  }
   726  
   727  // Mined broadcast loop
   728  func (self *ProtocolManager) minedBroadcastLoop() {
   729  	// automatically stops if unsubscribe
   730  	for obj := range self.minedBlockSub.Chan() {
   731  		switch ev := obj.Data.(type) {
   732  		case core.NewMinedBlockEvent:
   733  			self.BroadcastBlock(ev.Block, true)  // First propagate block to peers
   734  			self.BroadcastBlock(ev.Block, false) // Only then announce to the rest
   735  		}
   736  	}
   737  }
   738  
   739  func (self *ProtocolManager) txBroadcastLoop() {
   740  	// automatically stops if unsubscribe
   741  	for obj := range self.txSub.Chan() {
   742  		event := obj.Data.(core.TxPreEvent)
   743  		self.BroadcastTx(event.Tx.Hash(), event.Tx)
   744  	}
   745  }
   746  
   747  // EthNodeInfo represents a short summary of the Ethereum sub-protocol metadata known
   748  // about the host peer.
   749  type EthNodeInfo struct {
   750  	Network    int         `json:"network"`    // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3)
   751  	Difficulty *big.Int    `json:"difficulty"` // Total difficulty of the host's blockchain
   752  	Genesis    common.Hash `json:"genesis"`    // SHA3 hash of the host's genesis block
   753  	Head       common.Hash `json:"head"`       // SHA3 hash of the host's best owned block
   754  }
   755  
   756  // NodeInfo retrieves some protocol metadata about the running host node.
   757  func (self *ProtocolManager) NodeInfo() *EthNodeInfo {
   758  	currentBlock := self.blockchain.CurrentBlock()
   759  	return &EthNodeInfo{
   760  		Network:    self.networkId,
   761  		Difficulty: self.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()),
   762  		Genesis:    self.blockchain.Genesis().Hash(),
   763  		Head:       currentBlock.Hash(),
   764  	}
   765  }