github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/les/fetcher.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"math/big"
    21  	"math/rand"
    22  	"sync"
    23  	"time"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/consensus"
    27  	"github.com/ethereum/go-ethereum/core"
    28  	"github.com/ethereum/go-ethereum/core/rawdb"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/ethereum/go-ethereum/ethdb"
    31  	"github.com/ethereum/go-ethereum/les/fetcher"
    32  	"github.com/ethereum/go-ethereum/light"
    33  	"github.com/ethereum/go-ethereum/log"
    34  	"github.com/ethereum/go-ethereum/p2p/enode"
    35  )
    36  
    37  const (
    38  	blockDelayTimeout    = 10 * time.Second       // Timeout for retrieving the headers from the peer
    39  	gatherSlack          = 100 * time.Millisecond // Interval used to collate almost-expired requests
    40  	cachedAnnosThreshold = 64                     // The maximum queued announcements
    41  )
    42  
    43  // announce represents an new block announcement from the les server.
    44  type announce struct {
    45  	data   *announceData
    46  	trust  bool
    47  	peerid enode.ID
    48  }
    49  
    50  // request represents a record when the header request is sent.
    51  type request struct {
    52  	reqid  uint64
    53  	peerid enode.ID
    54  	sendAt time.Time
    55  	hash   common.Hash
    56  }
    57  
    58  // response represents a response packet from network as well as a channel
    59  // to return all un-requested data.
    60  type response struct {
    61  	reqid   uint64
    62  	headers []*types.Header
    63  	peerid  enode.ID
    64  	remain  chan []*types.Header
    65  }
    66  
    67  // fetcherPeer holds the fetcher-specific information for each active peer
    68  type fetcherPeer struct {
    69  	latest *announceData // The latest announcement sent from the peer
    70  
    71  	// These following two fields can track the latest announces
    72  	// from the peer with limited size for caching. We hold the
    73  	// assumption that all enqueued announces are td-monotonic.
    74  	announces map[common.Hash]*announce // Announcement map
    75  	fifo      []common.Hash             // FIFO announces list
    76  }
    77  
    78  // addAnno enqueues an new trusted announcement. If the queued announces overflow,
    79  // evict from the oldest.
    80  func (fp *fetcherPeer) addAnno(anno *announce) {
    81  	// Short circuit if the anno already exists. In normal case it should
    82  	// never happen since only monotonic anno is accepted. But the adversary
    83  	// may feed us fake announces with higher td but same hash. In this case,
    84  	// ignore the anno anyway.
    85  	hash := anno.data.Hash
    86  	if _, exist := fp.announces[hash]; exist {
    87  		return
    88  	}
    89  	fp.announces[hash] = anno
    90  	fp.fifo = append(fp.fifo, hash)
    91  
    92  	// Evict oldest if the announces are oversized.
    93  	if len(fp.fifo)-cachedAnnosThreshold > 0 {
    94  		for i := 0; i < len(fp.fifo)-cachedAnnosThreshold; i++ {
    95  			delete(fp.announces, fp.fifo[i])
    96  		}
    97  		copy(fp.fifo, fp.fifo[len(fp.fifo)-cachedAnnosThreshold:])
    98  		fp.fifo = fp.fifo[:cachedAnnosThreshold]
    99  	}
   100  }
   101  
   102  // forwardAnno removes all announces from the map with a number lower than
   103  // the provided threshold.
   104  func (fp *fetcherPeer) forwardAnno(td *big.Int) []*announce {
   105  	var (
   106  		cutset  int
   107  		evicted []*announce
   108  	)
   109  	for ; cutset < len(fp.fifo); cutset++ {
   110  		anno := fp.announces[fp.fifo[cutset]]
   111  		if anno == nil {
   112  			continue // In theory it should never ever happen
   113  		}
   114  		if anno.data.Td.Cmp(td) > 0 {
   115  			break
   116  		}
   117  		evicted = append(evicted, anno)
   118  		delete(fp.announces, anno.data.Hash)
   119  	}
   120  	if cutset > 0 {
   121  		copy(fp.fifo, fp.fifo[cutset:])
   122  		fp.fifo = fp.fifo[:len(fp.fifo)-cutset]
   123  	}
   124  	return evicted
   125  }
   126  
   127  // lightFetcher implements retrieval of newly announced headers. It reuses
   128  // the eth.BlockFetcher as the underlying fetcher but adding more additional
   129  // rules: e.g. evict "timeout" peers.
   130  type lightFetcher struct {
   131  	// Various handlers
   132  	ulc     *ulc
   133  	chaindb ethdb.Database
   134  	reqDist *requestDistributor
   135  	peerset *serverPeerSet        // The global peerset of light client which shared by all components
   136  	chain   *light.LightChain     // The local light chain which maintains the canonical header chain.
   137  	fetcher *fetcher.BlockFetcher // The underlying fetcher which takes care block header retrieval.
   138  
   139  	// Peerset maintained by fetcher
   140  	plock sync.RWMutex
   141  	peers map[enode.ID]*fetcherPeer
   142  
   143  	// Various channels
   144  	announceCh chan *announce
   145  	requestCh  chan *request
   146  	deliverCh  chan *response
   147  	syncDone   chan *types.Header
   148  
   149  	closeCh chan struct{}
   150  	wg      sync.WaitGroup
   151  
   152  	// Callback
   153  	synchronise func(peer *serverPeer)
   154  
   155  	// Test fields or hooks
   156  	newHeadHook func(*types.Header)
   157  }
   158  
   159  // newLightFetcher creates a light fetcher instance.
   160  func newLightFetcher(chain *light.LightChain, engine consensus.Engine, peers *serverPeerSet, ulc *ulc, chaindb ethdb.Database, reqDist *requestDistributor, syncFn func(p *serverPeer)) *lightFetcher {
   161  	// Construct the fetcher by offering all necessary APIs
   162  	validator := func(header *types.Header) error {
   163  		// Disable seal verification explicitly if we are running in ulc mode.
   164  		return engine.VerifyHeader(chain, header, ulc == nil)
   165  	}
   166  	heighter := func() uint64 { return chain.CurrentHeader().Number.Uint64() }
   167  	dropper := func(id string) { peers.unregister(id) }
   168  	inserter := func(headers []*types.Header) (int, error) {
   169  		// Disable PoW checking explicitly if we are running in ulc mode.
   170  		checkFreq := 1
   171  		if ulc != nil {
   172  			checkFreq = 0
   173  		}
   174  		return chain.InsertHeaderChain(headers, checkFreq)
   175  	}
   176  	f := &lightFetcher{
   177  		ulc:         ulc,
   178  		peerset:     peers,
   179  		chaindb:     chaindb,
   180  		chain:       chain,
   181  		reqDist:     reqDist,
   182  		fetcher:     fetcher.NewBlockFetcher(true, chain.GetHeaderByHash, nil, validator, nil, heighter, inserter, nil, dropper),
   183  		peers:       make(map[enode.ID]*fetcherPeer),
   184  		synchronise: syncFn,
   185  		announceCh:  make(chan *announce),
   186  		requestCh:   make(chan *request),
   187  		deliverCh:   make(chan *response),
   188  		syncDone:    make(chan *types.Header),
   189  		closeCh:     make(chan struct{}),
   190  	}
   191  	peers.subscribe(f)
   192  	return f
   193  }
   194  
   195  func (f *lightFetcher) start() {
   196  	f.wg.Add(1)
   197  	f.fetcher.Start()
   198  	go f.mainloop()
   199  }
   200  
   201  func (f *lightFetcher) stop() {
   202  	close(f.closeCh)
   203  	f.fetcher.Stop()
   204  	f.wg.Wait()
   205  }
   206  
   207  // registerPeer adds an new peer to the fetcher's peer set
   208  func (f *lightFetcher) registerPeer(p *serverPeer) {
   209  	f.plock.Lock()
   210  	defer f.plock.Unlock()
   211  
   212  	f.peers[p.ID()] = &fetcherPeer{announces: make(map[common.Hash]*announce)}
   213  }
   214  
   215  // unregisterPeer removes the specified peer from the fetcher's peer set
   216  func (f *lightFetcher) unregisterPeer(p *serverPeer) {
   217  	f.plock.Lock()
   218  	defer f.plock.Unlock()
   219  
   220  	delete(f.peers, p.ID())
   221  }
   222  
   223  // peer returns the peer from the fetcher peerset.
   224  func (f *lightFetcher) peer(id enode.ID) *fetcherPeer {
   225  	f.plock.RLock()
   226  	defer f.plock.RUnlock()
   227  
   228  	return f.peers[id]
   229  }
   230  
   231  // forEachPeer iterates the fetcher peerset, abort the iteration if the
   232  // callback returns false.
   233  func (f *lightFetcher) forEachPeer(check func(id enode.ID, p *fetcherPeer) bool) {
   234  	f.plock.RLock()
   235  	defer f.plock.RUnlock()
   236  
   237  	for id, peer := range f.peers {
   238  		if !check(id, peer) {
   239  			return
   240  		}
   241  	}
   242  }
   243  
   244  // mainloop is the main event loop of the light fetcher, which is responsible for
   245  // - announcement maintenance(ulc)
   246  //   If we are running in ultra light client mode, then all announcements from
   247  //   the trusted servers are maintained. If the same announcements from trusted
   248  //   servers reach the threshold, then the relevant header is requested for retrieval.
   249  //
   250  // - block header retrieval
   251  //   Whenever we receive announce with higher td compared with local chain, the
   252  //   request will be made for header retrieval.
   253  //
   254  // - re-sync trigger
   255  //   If the local chain lags too much, then the fetcher will enter "synnchronise"
   256  //   mode to retrieve missing headers in batch.
   257  func (f *lightFetcher) mainloop() {
   258  	defer f.wg.Done()
   259  
   260  	var (
   261  		syncInterval = uint64(1) // Interval used to trigger a light resync.
   262  		syncing      bool        // Indicator whether the client is syncing
   263  
   264  		ulc          = f.ulc != nil
   265  		headCh       = make(chan core.ChainHeadEvent, 100)
   266  		fetching     = make(map[uint64]*request)
   267  		requestTimer = time.NewTimer(0)
   268  
   269  		// Local status
   270  		localHead = f.chain.CurrentHeader()
   271  		localTd   = f.chain.GetTd(localHead.Hash(), localHead.Number.Uint64())
   272  	)
   273  	sub := f.chain.SubscribeChainHeadEvent(headCh)
   274  	defer sub.Unsubscribe()
   275  
   276  	// reset updates the local status with given header.
   277  	reset := func(header *types.Header) {
   278  		localHead = header
   279  		localTd = f.chain.GetTd(header.Hash(), header.Number.Uint64())
   280  	}
   281  	// trustedHeader returns an indicator whether the header is regarded as
   282  	// trusted. If we are running in the ulc mode, only when we receive enough
   283  	// same announcement from trusted server, the header will be trusted.
   284  	trustedHeader := func(hash common.Hash, number uint64) (bool, []enode.ID) {
   285  		var (
   286  			agreed  []enode.ID
   287  			trusted bool
   288  		)
   289  		f.forEachPeer(func(id enode.ID, p *fetcherPeer) bool {
   290  			if anno := p.announces[hash]; anno != nil && anno.trust && anno.data.Number == number {
   291  				agreed = append(agreed, id)
   292  				if 100*len(agreed)/len(f.ulc.keys) >= f.ulc.fraction {
   293  					trusted = true
   294  					return false // abort iteration
   295  				}
   296  			}
   297  			return true
   298  		})
   299  		return trusted, agreed
   300  	}
   301  	for {
   302  		select {
   303  		case anno := <-f.announceCh:
   304  			peerid, data := anno.peerid, anno.data
   305  			log.Debug("Received new announce", "peer", peerid, "number", data.Number, "hash", data.Hash, "reorg", data.ReorgDepth)
   306  
   307  			peer := f.peer(peerid)
   308  			if peer == nil {
   309  				log.Debug("Receive announce from unknown peer", "peer", peerid)
   310  				continue
   311  			}
   312  			// Announced tds should be strictly monotonic, drop the peer if
   313  			// the announce is out-of-order.
   314  			if peer.latest != nil && data.Td.Cmp(peer.latest.Td) <= 0 {
   315  				f.peerset.unregister(peerid.String())
   316  				log.Debug("Non-monotonic td", "peer", peerid, "current", data.Td, "previous", peer.latest.Td)
   317  				continue
   318  			}
   319  			peer.latest = data
   320  
   321  			// Filter out any stale announce, the local chain is ahead of announce
   322  			if localTd != nil && data.Td.Cmp(localTd) <= 0 {
   323  				continue
   324  			}
   325  			peer.addAnno(anno)
   326  
   327  			// If we are not syncing, try to trigger a single retrieval or re-sync
   328  			if !ulc && !syncing {
   329  				// Two scenarios lead to re-sync:
   330  				// - reorg happens
   331  				// - local chain lags
   332  				// We can't retrieve the parent of the announce by single retrieval
   333  				// in both cases, so resync is necessary.
   334  				if data.Number > localHead.Number.Uint64()+syncInterval || data.ReorgDepth > 0 {
   335  					syncing = true
   336  					go f.startSync(peerid)
   337  					log.Debug("Trigger light sync", "peer", peerid, "local", localHead.Number, "localhash", localHead.Hash(), "remote", data.Number, "remotehash", data.Hash)
   338  					continue
   339  				}
   340  				f.fetcher.Notify(peerid.String(), data.Hash, data.Number, time.Now(), f.requestHeaderByHash(peerid), nil)
   341  				log.Debug("Trigger header retrieval", "peer", peerid, "number", data.Number, "hash", data.Hash)
   342  			}
   343  			// Keep collecting announces from trusted server even we are syncing.
   344  			if ulc && anno.trust {
   345  				// Notify underlying fetcher to retrieve header or trigger a resync if
   346  				// we have receive enough announcements from trusted server.
   347  				trusted, agreed := trustedHeader(data.Hash, data.Number)
   348  				if trusted && !syncing {
   349  					if data.Number > localHead.Number.Uint64()+syncInterval || data.ReorgDepth > 0 {
   350  						syncing = true
   351  						go f.startSync(peerid)
   352  						log.Debug("Trigger trusted light sync", "local", localHead.Number, "localhash", localHead.Hash(), "remote", data.Number, "remotehash", data.Hash)
   353  						continue
   354  					}
   355  					p := agreed[rand.Intn(len(agreed))]
   356  					f.fetcher.Notify(p.String(), data.Hash, data.Number, time.Now(), f.requestHeaderByHash(p), nil)
   357  					log.Debug("Trigger trusted header retrieval", "number", data.Number, "hash", data.Hash)
   358  				}
   359  			}
   360  
   361  		case req := <-f.requestCh:
   362  			fetching[req.reqid] = req // Tracking all in-flight requests for response latency statistic.
   363  			if len(fetching) == 1 {
   364  				f.rescheduleTimer(fetching, requestTimer)
   365  			}
   366  
   367  		case <-requestTimer.C:
   368  			for reqid, request := range fetching {
   369  				if time.Since(request.sendAt) > blockDelayTimeout-gatherSlack {
   370  					delete(fetching, reqid)
   371  					f.peerset.unregister(request.peerid.String())
   372  					log.Debug("Request timeout", "peer", request.peerid, "reqid", reqid)
   373  				}
   374  			}
   375  			f.rescheduleTimer(fetching, requestTimer)
   376  
   377  		case resp := <-f.deliverCh:
   378  			if req := fetching[resp.reqid]; req != nil {
   379  				delete(fetching, resp.reqid)
   380  				f.rescheduleTimer(fetching, requestTimer)
   381  
   382  				// The underlying fetcher does not check the consistency of request and response.
   383  				// The adversary can send the fake announces with invalid hash and number but always
   384  				// delivery some mismatched header. So it can't be punished by the underlying fetcher.
   385  				// We have to add two more rules here to detect.
   386  				if len(resp.headers) != 1 {
   387  					f.peerset.unregister(req.peerid.String())
   388  					log.Debug("Deliver more than requested", "peer", req.peerid, "reqid", req.reqid)
   389  					continue
   390  				}
   391  				if resp.headers[0].Hash() != req.hash {
   392  					f.peerset.unregister(req.peerid.String())
   393  					log.Debug("Deliver invalid header", "peer", req.peerid, "reqid", req.reqid)
   394  					continue
   395  				}
   396  				resp.remain <- f.fetcher.FilterHeaders(resp.peerid.String(), resp.headers, time.Now())
   397  			} else {
   398  				// Discard the entire packet no matter it's a timeout response or unexpected one.
   399  				resp.remain <- resp.headers
   400  			}
   401  
   402  		case ev := <-headCh:
   403  			// Short circuit if we are still syncing.
   404  			if syncing {
   405  				continue
   406  			}
   407  			reset(ev.Block.Header())
   408  
   409  			// Clean stale announcements from les-servers.
   410  			var droplist []enode.ID
   411  			f.forEachPeer(func(id enode.ID, p *fetcherPeer) bool {
   412  				removed := p.forwardAnno(localTd)
   413  				for _, anno := range removed {
   414  					if header := f.chain.GetHeaderByHash(anno.data.Hash); header != nil {
   415  						if header.Number.Uint64() != anno.data.Number {
   416  							droplist = append(droplist, id)
   417  							break
   418  						}
   419  						// In theory td should exists.
   420  						td := f.chain.GetTd(anno.data.Hash, anno.data.Number)
   421  						if td != nil && td.Cmp(anno.data.Td) != 0 {
   422  							droplist = append(droplist, id)
   423  							break
   424  						}
   425  					}
   426  				}
   427  				return true
   428  			})
   429  			for _, id := range droplist {
   430  				f.peerset.unregister(id.String())
   431  				log.Debug("Kicked out peer for invalid announcement")
   432  			}
   433  			if f.newHeadHook != nil {
   434  				f.newHeadHook(localHead)
   435  			}
   436  
   437  		case origin := <-f.syncDone:
   438  			syncing = false // Reset the status
   439  
   440  			// Rewind all untrusted headers for ulc mode.
   441  			if ulc {
   442  				head := f.chain.CurrentHeader()
   443  				ancestor := rawdb.FindCommonAncestor(f.chaindb, origin, head)
   444  
   445  				// Recap the ancestor with genesis header in case the ancestor
   446  				// is not found. It can happen the original head is before the
   447  				// checkpoint while the synced headers are after it. In this
   448  				// case there is no ancestor between them.
   449  				if ancestor == nil {
   450  					ancestor = f.chain.Genesis().Header()
   451  				}
   452  				var untrusted []common.Hash
   453  				for head.Number.Cmp(ancestor.Number) > 0 {
   454  					hash, number := head.Hash(), head.Number.Uint64()
   455  					if trusted, _ := trustedHeader(hash, number); trusted {
   456  						break
   457  					}
   458  					untrusted = append(untrusted, hash)
   459  					head = f.chain.GetHeader(head.ParentHash, number-1)
   460  					if head == nil {
   461  						break // all the synced headers will be dropped
   462  					}
   463  				}
   464  				if len(untrusted) > 0 {
   465  					for i, j := 0, len(untrusted)-1; i < j; i, j = i+1, j-1 {
   466  						untrusted[i], untrusted[j] = untrusted[j], untrusted[i]
   467  					}
   468  					f.chain.Rollback(untrusted)
   469  				}
   470  			}
   471  			// Reset local status.
   472  			reset(f.chain.CurrentHeader())
   473  			if f.newHeadHook != nil {
   474  				f.newHeadHook(localHead)
   475  			}
   476  			log.Debug("light sync finished", "number", localHead.Number, "hash", localHead.Hash())
   477  
   478  		case <-f.closeCh:
   479  			return
   480  		}
   481  	}
   482  }
   483  
   484  // announce processes a new announcement message received from a peer.
   485  func (f *lightFetcher) announce(p *serverPeer, head *announceData) {
   486  	select {
   487  	case f.announceCh <- &announce{peerid: p.ID(), trust: p.trusted, data: head}:
   488  	case <-f.closeCh:
   489  		return
   490  	}
   491  }
   492  
   493  // trackRequest sends a reqID to main loop for in-flight request tracking.
   494  func (f *lightFetcher) trackRequest(peerid enode.ID, reqid uint64, hash common.Hash) {
   495  	select {
   496  	case f.requestCh <- &request{reqid: reqid, peerid: peerid, sendAt: time.Now(), hash: hash}:
   497  	case <-f.closeCh:
   498  	}
   499  }
   500  
   501  // requestHeaderByHash constructs a header retrieval request and sends it to
   502  // local request distributor.
   503  //
   504  // Note, we rely on the underlying eth/fetcher to retrieve and validate the
   505  // response, so that we have to obey the rule of eth/fetcher which only accepts
   506  // the response from given peer.
   507  func (f *lightFetcher) requestHeaderByHash(peerid enode.ID) func(common.Hash) error {
   508  	return func(hash common.Hash) error {
   509  		req := &distReq{
   510  			getCost: func(dp distPeer) uint64 { return dp.(*serverPeer).getRequestCost(GetBlockHeadersMsg, 1) },
   511  			canSend: func(dp distPeer) bool { return dp.(*serverPeer).ID() == peerid },
   512  			request: func(dp distPeer) func() {
   513  				peer, id := dp.(*serverPeer), rand.Uint64()
   514  				cost := peer.getRequestCost(GetBlockHeadersMsg, 1)
   515  				peer.fcServer.QueuedRequest(id, cost)
   516  
   517  				return func() {
   518  					f.trackRequest(peer.ID(), id, hash)
   519  					peer.requestHeadersByHash(id, hash, 1, 0, false)
   520  				}
   521  			},
   522  		}
   523  		f.reqDist.queue(req)
   524  		return nil
   525  	}
   526  }
   527  
   528  // startSync invokes synchronisation callback to start syncing.
   529  func (f *lightFetcher) startSync(id enode.ID) {
   530  	defer func(header *types.Header) {
   531  		f.syncDone <- header
   532  	}(f.chain.CurrentHeader())
   533  
   534  	peer := f.peerset.peer(id.String())
   535  	if peer == nil || peer.onlyAnnounce {
   536  		return
   537  	}
   538  	f.synchronise(peer)
   539  }
   540  
   541  // deliverHeaders delivers header download request responses for processing
   542  func (f *lightFetcher) deliverHeaders(peer *serverPeer, reqid uint64, headers []*types.Header) []*types.Header {
   543  	remain := make(chan []*types.Header, 1)
   544  	select {
   545  	case f.deliverCh <- &response{reqid: reqid, headers: headers, peerid: peer.ID(), remain: remain}:
   546  	case <-f.closeCh:
   547  		return nil
   548  	}
   549  	return <-remain
   550  }
   551  
   552  // rescheduleTimer resets the specified timeout timer to the next request timeout.
   553  func (f *lightFetcher) rescheduleTimer(requests map[uint64]*request, timer *time.Timer) {
   554  	// Short circuit if no inflight requests
   555  	if len(requests) == 0 {
   556  		timer.Stop()
   557  		return
   558  	}
   559  	// Otherwise find the earliest expiring request
   560  	earliest := time.Now()
   561  	for _, req := range requests {
   562  		if earliest.After(req.sendAt) {
   563  			earliest = req.sendAt
   564  		}
   565  	}
   566  	timer.Reset(blockDelayTimeout - time.Since(earliest))
   567  }