github.com/juliankolbe/go-ethereum@v1.9.992/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  	"errors"
    21  	"math"
    22  	"math/big"
    23  	"sync"
    24  	"sync/atomic"
    25  	"time"
    26  
    27  	"github.com/juliankolbe/go-ethereum/common"
    28  	"github.com/juliankolbe/go-ethereum/core"
    29  	"github.com/juliankolbe/go-ethereum/core/forkid"
    30  	"github.com/juliankolbe/go-ethereum/core/types"
    31  	"github.com/juliankolbe/go-ethereum/eth/downloader"
    32  	"github.com/juliankolbe/go-ethereum/eth/fetcher"
    33  	"github.com/juliankolbe/go-ethereum/eth/protocols/eth"
    34  	"github.com/juliankolbe/go-ethereum/eth/protocols/snap"
    35  	"github.com/juliankolbe/go-ethereum/ethdb"
    36  	"github.com/juliankolbe/go-ethereum/event"
    37  	"github.com/juliankolbe/go-ethereum/log"
    38  	"github.com/juliankolbe/go-ethereum/p2p"
    39  	"github.com/juliankolbe/go-ethereum/params"
    40  	"github.com/juliankolbe/go-ethereum/trie"
    41  )
    42  
    43  const (
    44  	// txChanSize is the size of channel listening to NewTxsEvent.
    45  	// The number is referenced from the size of tx pool.
    46  	txChanSize = 4096
    47  )
    48  
    49  var (
    50  	syncChallengeTimeout = 15 * time.Second // Time allowance for a node to reply to the sync progress challenge
    51  )
    52  
    53  // txPool defines the methods needed from a transaction pool implementation to
    54  // support all the operations needed by the Ethereum chain protocols.
    55  type txPool interface {
    56  	// Has returns an indicator whether txpool has a transaction
    57  	// cached with the given hash.
    58  	Has(hash common.Hash) bool
    59  
    60  	// Get retrieves the transaction from local txpool with given
    61  	// tx hash.
    62  	Get(hash common.Hash) *types.Transaction
    63  
    64  	// AddRemotes should add the given transactions to the pool.
    65  	AddRemotes([]*types.Transaction) []error
    66  
    67  	// Pending should return pending transactions.
    68  	// The slice should be modifiable by the caller.
    69  	Pending() (map[common.Address]types.Transactions, error)
    70  
    71  	// SubscribeNewTxsEvent should return an event subscription of
    72  	// NewTxsEvent and send events to the given channel.
    73  	SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
    74  }
    75  
    76  // handlerConfig is the collection of initialization parameters to create a full
    77  // node network handler.
    78  type handlerConfig struct {
    79  	Database   ethdb.Database            // Database for direct sync insertions
    80  	Chain      *core.BlockChain          // Blockchain to serve data from
    81  	TxPool     txPool                    // Transaction pool to propagate from
    82  	Network    uint64                    // Network identifier to adfvertise
    83  	Sync       downloader.SyncMode       // Whether to fast or full sync
    84  	BloomCache uint64                    // Megabytes to alloc for fast sync bloom
    85  	EventMux   *event.TypeMux            // Legacy event mux, deprecate for `feed`
    86  	Checkpoint *params.TrustedCheckpoint // Hard coded checkpoint for sync challenges
    87  	Whitelist  map[uint64]common.Hash    // Hard coded whitelist for sync challenged
    88  }
    89  
    90  type handler struct {
    91  	networkID  uint64
    92  	forkFilter forkid.Filter // Fork ID filter, constant across the lifetime of the node
    93  
    94  	fastSync  uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
    95  	snapSync  uint32 // Flag whether fast sync should operate on top of the snap protocol
    96  	acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
    97  
    98  	checkpointNumber uint64      // Block number for the sync progress validator to cross reference
    99  	checkpointHash   common.Hash // Block hash for the sync progress validator to cross reference
   100  
   101  	database ethdb.Database
   102  	txpool   txPool
   103  	chain    *core.BlockChain
   104  	maxPeers int
   105  
   106  	downloader   *downloader.Downloader
   107  	stateBloom   *trie.SyncBloom
   108  	blockFetcher *fetcher.BlockFetcher
   109  	txFetcher    *fetcher.TxFetcher
   110  	peers        *peerSet
   111  
   112  	eventMux      *event.TypeMux
   113  	txsCh         chan core.NewTxsEvent
   114  	txsSub        event.Subscription
   115  	minedBlockSub *event.TypeMuxSubscription
   116  
   117  	whitelist map[uint64]common.Hash
   118  
   119  	// channels for fetcher, syncer, txsyncLoop
   120  	txsyncCh chan *txsync
   121  	quitSync chan struct{}
   122  
   123  	chainSync *chainSyncer
   124  	wg        sync.WaitGroup
   125  	peerWG    sync.WaitGroup
   126  }
   127  
   128  // newHandler returns a handler for all Ethereum chain management protocol.
   129  func newHandler(config *handlerConfig) (*handler, error) {
   130  	// Create the protocol manager with the base fields
   131  	if config.EventMux == nil {
   132  		config.EventMux = new(event.TypeMux) // Nicety initialization for tests
   133  	}
   134  	h := &handler{
   135  		networkID:  config.Network,
   136  		forkFilter: forkid.NewFilter(config.Chain),
   137  		eventMux:   config.EventMux,
   138  		database:   config.Database,
   139  		txpool:     config.TxPool,
   140  		chain:      config.Chain,
   141  		peers:      newPeerSet(),
   142  		whitelist:  config.Whitelist,
   143  		txsyncCh:   make(chan *txsync),
   144  		quitSync:   make(chan struct{}),
   145  	}
   146  	if config.Sync == downloader.FullSync {
   147  		// The database seems empty as the current block is the genesis. Yet the fast
   148  		// block is ahead, so fast sync was enabled for this node at a certain point.
   149  		// The scenarios where this can happen is
   150  		// * if the user manually (or via a bad block) rolled back a fast sync node
   151  		//   below the sync point.
   152  		// * the last fast sync is not finished while user specifies a full sync this
   153  		//   time. But we don't have any recent state for full sync.
   154  		// In these cases however it's safe to reenable fast sync.
   155  		fullBlock, fastBlock := h.chain.CurrentBlock(), h.chain.CurrentFastBlock()
   156  		if fullBlock.NumberU64() == 0 && fastBlock.NumberU64() > 0 {
   157  			h.fastSync = uint32(1)
   158  			log.Warn("Switch sync mode from full sync to fast sync")
   159  		}
   160  	} else {
   161  		if h.chain.CurrentBlock().NumberU64() > 0 {
   162  			// Print warning log if database is not empty to run fast sync.
   163  			log.Warn("Switch sync mode from fast sync to full sync")
   164  		} else {
   165  			// If fast sync was requested and our database is empty, grant it
   166  			h.fastSync = uint32(1)
   167  			if config.Sync == downloader.SnapSync {
   168  				h.snapSync = uint32(1)
   169  			}
   170  		}
   171  	}
   172  	// If we have trusted checkpoints, enforce them on the chain
   173  	if config.Checkpoint != nil {
   174  		h.checkpointNumber = (config.Checkpoint.SectionIndex+1)*params.CHTFrequency - 1
   175  		h.checkpointHash = config.Checkpoint.SectionHead
   176  	}
   177  	// Construct the downloader (long sync) and its backing state bloom if fast
   178  	// sync is requested. The downloader is responsible for deallocating the state
   179  	// bloom when it's done.
   180  	if atomic.LoadUint32(&h.fastSync) == 1 {
   181  		h.stateBloom = trie.NewSyncBloom(config.BloomCache, config.Database)
   182  	}
   183  	h.downloader = downloader.New(h.checkpointNumber, config.Database, h.stateBloom, h.eventMux, h.chain, nil, h.removePeer)
   184  
   185  	// Construct the fetcher (short sync)
   186  	validator := func(header *types.Header) error {
   187  		return h.chain.Engine().VerifyHeader(h.chain, header, true)
   188  	}
   189  	heighter := func() uint64 {
   190  		return h.chain.CurrentBlock().NumberU64()
   191  	}
   192  	inserter := func(blocks types.Blocks) (int, error) {
   193  		// If sync hasn't reached the checkpoint yet, deny importing weird blocks.
   194  		//
   195  		// Ideally we would also compare the head block's timestamp and similarly reject
   196  		// the propagated block if the head is too old. Unfortunately there is a corner
   197  		// case when starting new networks, where the genesis might be ancient (0 unix)
   198  		// which would prevent full nodes from accepting it.
   199  		if h.chain.CurrentBlock().NumberU64() < h.checkpointNumber {
   200  			log.Warn("Unsynced yet, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
   201  			return 0, nil
   202  		}
   203  		// If fast sync is running, deny importing weird blocks. This is a problematic
   204  		// clause when starting up a new network, because fast-syncing miners might not
   205  		// accept each others' blocks until a restart. Unfortunately we haven't figured
   206  		// out a way yet where nodes can decide unilaterally whether the network is new
   207  		// or not. This should be fixed if we figure out a solution.
   208  		if atomic.LoadUint32(&h.fastSync) == 1 {
   209  			log.Warn("Fast syncing, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
   210  			return 0, nil
   211  		}
   212  		n, err := h.chain.InsertChain(blocks)
   213  		if err == nil {
   214  			atomic.StoreUint32(&h.acceptTxs, 1) // Mark initial sync done on any fetcher import
   215  		}
   216  		return n, err
   217  	}
   218  	h.blockFetcher = fetcher.NewBlockFetcher(false, nil, h.chain.GetBlockByHash, validator, h.BroadcastBlock, heighter, nil, inserter, h.removePeer)
   219  
   220  	fetchTx := func(peer string, hashes []common.Hash) error {
   221  		p := h.peers.peer(peer)
   222  		if p == nil {
   223  			return errors.New("unknown peer")
   224  		}
   225  		return p.RequestTxs(hashes)
   226  	}
   227  	h.txFetcher = fetcher.NewTxFetcher(h.txpool.Has, h.txpool.AddRemotes, fetchTx)
   228  	h.chainSync = newChainSyncer(h)
   229  	return h, nil
   230  }
   231  
   232  // runEthPeer registers an eth peer into the joint eth/snap peerset, adds it to
   233  // various subsistems and starts handling messages.
   234  func (h *handler) runEthPeer(peer *eth.Peer, handler eth.Handler) error {
   235  	// If the peer has a `snap` extension, wait for it to connect so we can have
   236  	// a uniform initialization/teardown mechanism
   237  	snap, err := h.peers.waitSnapExtension(peer)
   238  	if err != nil {
   239  		peer.Log().Error("Snapshot extension barrier failed", "err", err)
   240  		return err
   241  	}
   242  	// TODO(karalabe): Not sure why this is needed
   243  	if !h.chainSync.handlePeerEvent(peer) {
   244  		return p2p.DiscQuitting
   245  	}
   246  	h.peerWG.Add(1)
   247  	defer h.peerWG.Done()
   248  
   249  	// Execute the Ethereum handshake
   250  	var (
   251  		genesis = h.chain.Genesis()
   252  		head    = h.chain.CurrentHeader()
   253  		hash    = head.Hash()
   254  		number  = head.Number.Uint64()
   255  		td      = h.chain.GetTd(hash, number)
   256  	)
   257  	forkID := forkid.NewID(h.chain.Config(), h.chain.Genesis().Hash(), h.chain.CurrentHeader().Number.Uint64())
   258  	if err := peer.Handshake(h.networkID, td, hash, genesis.Hash(), forkID, h.forkFilter); err != nil {
   259  		peer.Log().Debug("Ethereum handshake failed", "err", err)
   260  		return err
   261  	}
   262  	reject := false // reserved peer slots
   263  	if atomic.LoadUint32(&h.snapSync) == 1 {
   264  		if snap == nil {
   265  			// If we are running snap-sync, we want to reserve roughly half the peer
   266  			// slots for peers supporting the snap protocol.
   267  			// The logic here is; we only allow up to 5 more non-snap peers than snap-peers.
   268  			if all, snp := h.peers.len(), h.peers.snapLen(); all-snp > snp+5 {
   269  				reject = true
   270  			}
   271  		}
   272  	}
   273  	// Ignore maxPeers if this is a trusted peer
   274  	if !peer.Peer.Info().Network.Trusted {
   275  		if reject || h.peers.len() >= h.maxPeers {
   276  			return p2p.DiscTooManyPeers
   277  		}
   278  	}
   279  	peer.Log().Debug("Ethereum peer connected", "name", peer.Name())
   280  
   281  	// Register the peer locally
   282  	if err := h.peers.registerPeer(peer, snap); err != nil {
   283  		peer.Log().Error("Ethereum peer registration failed", "err", err)
   284  		return err
   285  	}
   286  	defer h.removePeer(peer.ID())
   287  
   288  	p := h.peers.peer(peer.ID())
   289  	if p == nil {
   290  		return errors.New("peer dropped during handling")
   291  	}
   292  	// Register the peer in the downloader. If the downloader considers it banned, we disconnect
   293  	if err := h.downloader.RegisterPeer(peer.ID(), peer.Version(), peer); err != nil {
   294  		peer.Log().Error("Failed to register peer in eth syncer", "err", err)
   295  		return err
   296  	}
   297  	if snap != nil {
   298  		if err := h.downloader.SnapSyncer.Register(snap); err != nil {
   299  			peer.Log().Error("Failed to register peer in snap syncer", "err", err)
   300  			return err
   301  		}
   302  	}
   303  	h.chainSync.handlePeerEvent(peer)
   304  
   305  	// Propagate existing transactions. new transactions appearing
   306  	// after this will be sent via broadcasts.
   307  	h.syncTransactions(peer)
   308  
   309  	// If we have a trusted CHT, reject all peers below that (avoid fast sync eclipse)
   310  	if h.checkpointHash != (common.Hash{}) {
   311  		// Request the peer's checkpoint header for chain height/weight validation
   312  		if err := peer.RequestHeadersByNumber(h.checkpointNumber, 1, 0, false); err != nil {
   313  			return err
   314  		}
   315  		// Start a timer to disconnect if the peer doesn't reply in time
   316  		p.syncDrop = time.AfterFunc(syncChallengeTimeout, func() {
   317  			peer.Log().Warn("Checkpoint challenge timed out, dropping", "addr", peer.RemoteAddr(), "type", peer.Name())
   318  			h.removePeer(peer.ID())
   319  		})
   320  		// Make sure it's cleaned up if the peer dies off
   321  		defer func() {
   322  			if p.syncDrop != nil {
   323  				p.syncDrop.Stop()
   324  				p.syncDrop = nil
   325  			}
   326  		}()
   327  	}
   328  	// If we have any explicit whitelist block hashes, request them
   329  	for number := range h.whitelist {
   330  		if err := peer.RequestHeadersByNumber(number, 1, 0, false); err != nil {
   331  			return err
   332  		}
   333  	}
   334  	// Handle incoming messages until the connection is torn down
   335  	return handler(peer)
   336  }
   337  
   338  // runSnapExtension registers a `snap` peer into the joint eth/snap peerset and
   339  // starts handling inbound messages. As `snap` is only a satellite protocol to
   340  // `eth`, all subsystem registrations and lifecycle management will be done by
   341  // the main `eth` handler to prevent strange races.
   342  func (h *handler) runSnapExtension(peer *snap.Peer, handler snap.Handler) error {
   343  	h.peerWG.Add(1)
   344  	defer h.peerWG.Done()
   345  
   346  	if err := h.peers.registerSnapExtension(peer); err != nil {
   347  		peer.Log().Error("Snapshot extension registration failed", "err", err)
   348  		return err
   349  	}
   350  	return handler(peer)
   351  }
   352  
   353  // removePeer unregisters a peer from the downloader and fetchers, removes it from
   354  // the set of tracked peers and closes the network connection to it.
   355  func (h *handler) removePeer(id string) {
   356  	// Create a custom logger to avoid printing the entire id
   357  	var logger log.Logger
   358  	if len(id) < 16 {
   359  		// Tests use short IDs, don't choke on them
   360  		logger = log.New("peer", id)
   361  	} else {
   362  		logger = log.New("peer", id[:8])
   363  	}
   364  	// Abort if the peer does not exist
   365  	peer := h.peers.peer(id)
   366  	if peer == nil {
   367  		logger.Error("Ethereum peer removal failed", "err", errPeerNotRegistered)
   368  		return
   369  	}
   370  	// Remove the `eth` peer if it exists
   371  	logger.Debug("Removing Ethereum peer", "snap", peer.snapExt != nil)
   372  
   373  	// Remove the `snap` extension if it exists
   374  	if peer.snapExt != nil {
   375  		h.downloader.SnapSyncer.Unregister(id)
   376  	}
   377  	h.downloader.UnregisterPeer(id)
   378  	h.txFetcher.Drop(id)
   379  
   380  	if err := h.peers.unregisterPeer(id); err != nil {
   381  		logger.Error("Ethereum peer removal failed", "err", err)
   382  	}
   383  	// Hard disconnect at the networking layer
   384  	peer.Peer.Disconnect(p2p.DiscUselessPeer)
   385  }
   386  
   387  func (h *handler) Start(maxPeers int) {
   388  	h.maxPeers = maxPeers
   389  
   390  	// broadcast transactions
   391  	h.wg.Add(1)
   392  	h.txsCh = make(chan core.NewTxsEvent, txChanSize)
   393  	h.txsSub = h.txpool.SubscribeNewTxsEvent(h.txsCh)
   394  	go h.txBroadcastLoop()
   395  
   396  	// broadcast mined blocks
   397  	h.wg.Add(1)
   398  	h.minedBlockSub = h.eventMux.Subscribe(core.NewMinedBlockEvent{})
   399  	go h.minedBroadcastLoop()
   400  
   401  	// start sync handlers
   402  	h.wg.Add(2)
   403  	go h.chainSync.loop()
   404  	go h.txsyncLoop64() // TODO(karalabe): Legacy initial tx echange, drop with eth/64.
   405  }
   406  
   407  func (h *handler) Stop() {
   408  	h.txsSub.Unsubscribe()        // quits txBroadcastLoop
   409  	h.minedBlockSub.Unsubscribe() // quits blockBroadcastLoop
   410  
   411  	// Quit chainSync and txsync64.
   412  	// After this is done, no new peers will be accepted.
   413  	close(h.quitSync)
   414  	h.wg.Wait()
   415  
   416  	// Disconnect existing sessions.
   417  	// This also closes the gate for any new registrations on the peer set.
   418  	// sessions which are already established but not added to h.peers yet
   419  	// will exit when they try to register.
   420  	h.peers.close()
   421  	h.peerWG.Wait()
   422  
   423  	log.Info("Ethereum protocol stopped")
   424  }
   425  
   426  // BroadcastBlock will either propagate a block to a subset of its peers, or
   427  // will only announce its availability (depending what's requested).
   428  func (h *handler) BroadcastBlock(block *types.Block, propagate bool) {
   429  	hash := block.Hash()
   430  	peers := h.peers.peersWithoutBlock(hash)
   431  
   432  	// If propagation is requested, send to a subset of the peer
   433  	if propagate {
   434  		// Calculate the TD of the block (it's not imported yet, so block.Td is not valid)
   435  		var td *big.Int
   436  		if parent := h.chain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent != nil {
   437  			td = new(big.Int).Add(block.Difficulty(), h.chain.GetTd(block.ParentHash(), block.NumberU64()-1))
   438  		} else {
   439  			log.Error("Propagating dangling block", "number", block.Number(), "hash", hash)
   440  			return
   441  		}
   442  		// Send the block to a subset of our peers
   443  		transfer := peers[:int(math.Sqrt(float64(len(peers))))]
   444  		for _, peer := range transfer {
   445  			peer.AsyncSendNewBlock(block, td)
   446  		}
   447  		log.Trace("Propagated block", "hash", hash, "recipients", len(transfer), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
   448  		return
   449  	}
   450  	// Otherwise if the block is indeed in out own chain, announce it
   451  	if h.chain.HasBlock(hash, block.NumberU64()) {
   452  		for _, peer := range peers {
   453  			peer.AsyncSendNewBlockHash(block)
   454  		}
   455  		log.Trace("Announced block", "hash", hash, "recipients", len(peers), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
   456  	}
   457  }
   458  
   459  // BroadcastTransactions will propagate a batch of transactions
   460  // - To a square root of all peers
   461  // - And, separately, as announcements to all peers which are not known to
   462  // already have the given transaction.
   463  func (h *handler) BroadcastTransactions(txs types.Transactions) {
   464  	var (
   465  		annoCount   int // Count of announcements made
   466  		annoPeers   int
   467  		directCount int // Count of the txs sent directly to peers
   468  		directPeers int // Count of the peers that were sent transactions directly
   469  
   470  		txset = make(map[*ethPeer][]common.Hash) // Set peer->hash to transfer directly
   471  		annos = make(map[*ethPeer][]common.Hash) // Set peer->hash to announce
   472  
   473  	)
   474  	// Broadcast transactions to a batch of peers not knowing about it
   475  	for _, tx := range txs {
   476  		peers := h.peers.peersWithoutTransaction(tx.Hash())
   477  		// Send the tx unconditionally to a subset of our peers
   478  		numDirect := int(math.Sqrt(float64(len(peers))))
   479  		for _, peer := range peers[:numDirect] {
   480  			txset[peer] = append(txset[peer], tx.Hash())
   481  		}
   482  		// For the remaining peers, send announcement only
   483  		for _, peer := range peers[numDirect:] {
   484  			annos[peer] = append(annos[peer], tx.Hash())
   485  		}
   486  	}
   487  	for peer, hashes := range txset {
   488  		directPeers++
   489  		directCount += len(hashes)
   490  		peer.AsyncSendTransactions(hashes)
   491  	}
   492  	for peer, hashes := range annos {
   493  		annoPeers++
   494  		annoCount += len(hashes)
   495  		if peer.Version() >= eth.ETH65 {
   496  			peer.AsyncSendPooledTransactionHashes(hashes)
   497  		} else {
   498  			peer.AsyncSendTransactions(hashes)
   499  		}
   500  	}
   501  	log.Debug("Transaction broadcast", "txs", len(txs),
   502  		"announce packs", annoPeers, "announced hashes", annoCount,
   503  		"tx packs", directPeers, "broadcast txs", directCount)
   504  }
   505  
   506  // minedBroadcastLoop sends mined blocks to connected peers.
   507  func (h *handler) minedBroadcastLoop() {
   508  	defer h.wg.Done()
   509  
   510  	for obj := range h.minedBlockSub.Chan() {
   511  		if ev, ok := obj.Data.(core.NewMinedBlockEvent); ok {
   512  			h.BroadcastBlock(ev.Block, true)  // First propagate block to peers
   513  			h.BroadcastBlock(ev.Block, false) // Only then announce to the rest
   514  		}
   515  	}
   516  }
   517  
   518  // txBroadcastLoop announces new transactions to connected peers.
   519  func (h *handler) txBroadcastLoop() {
   520  	defer h.wg.Done()
   521  	for {
   522  		select {
   523  		case event := <-h.txsCh:
   524  			h.BroadcastTransactions(event.Txs)
   525  		case <-h.txsSub.Err():
   526  			return
   527  		}
   528  	}
   529  }