downloader.go 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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. // Package downloader contains the manual full chain synchronisation.
  17. package downloader
  18. import (
  19. "errors"
  20. "fmt"
  21. "math/big"
  22. "sync"
  23. "sync/atomic"
  24. "time"
  25. "github.com/ethereum/go-ethereum"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/rawdb"
  28. "github.com/ethereum/go-ethereum/core/state/snapshot"
  29. "github.com/ethereum/go-ethereum/core/types"
  30. "github.com/ethereum/go-ethereum/eth/protocols/snap"
  31. "github.com/ethereum/go-ethereum/ethdb"
  32. "github.com/ethereum/go-ethereum/event"
  33. "github.com/ethereum/go-ethereum/log"
  34. "github.com/ethereum/go-ethereum/params"
  35. )
  36. var (
  37. MaxBlockFetch = 128 // Amount of blocks to be fetched per retrieval request
  38. MaxHeaderFetch = 192 // Amount of block headers to be fetched per retrieval request
  39. MaxSkeletonSize = 128 // Number of header fetches to need for a skeleton assembly
  40. MaxReceiptFetch = 256 // Amount of transaction receipts to allow fetching per request
  41. maxQueuedHeaders = 32 * 1024 // [eth/62] Maximum number of headers to queue for import (DOS protection)
  42. maxHeadersProcess = 2048 // Number of header download results to import at once into the chain
  43. maxResultsProcess = 2048 // Number of content download results to import at once into the chain
  44. fullMaxForkAncestry uint64 = params.FullImmutabilityThreshold // Maximum chain reorganisation (locally redeclared so tests can reduce it)
  45. lightMaxForkAncestry uint64 = params.LightImmutabilityThreshold // Maximum chain reorganisation (locally redeclared so tests can reduce it)
  46. reorgProtThreshold = 48 // Threshold number of recent blocks to disable mini reorg protection
  47. reorgProtHeaderDelay = 2 // Number of headers to delay delivering to cover mini reorgs
  48. fsHeaderCheckFrequency = 100 // Verification frequency of the downloaded headers during snap sync
  49. fsHeaderSafetyNet = 2048 // Number of headers to discard in case a chain violation is detected
  50. fsHeaderForceVerify = 24 // Number of headers to verify before and after the pivot to accept it
  51. fsHeaderContCheck = 3 * time.Second // Time interval to check for header continuations during state download
  52. fsMinFullBlocks = 64 // Number of blocks to retrieve fully even in snap sync
  53. )
  54. var (
  55. errBusy = errors.New("busy")
  56. errUnknownPeer = errors.New("peer is unknown or unhealthy")
  57. errBadPeer = errors.New("action from bad peer ignored")
  58. errStallingPeer = errors.New("peer is stalling")
  59. errUnsyncedPeer = errors.New("unsynced peer")
  60. errNoPeers = errors.New("no peers to keep download active")
  61. errTimeout = errors.New("timeout")
  62. errEmptyHeaderSet = errors.New("empty header set by peer")
  63. errPeersUnavailable = errors.New("no peers available or all tried for download")
  64. errInvalidAncestor = errors.New("retrieved ancestor is invalid")
  65. errInvalidChain = errors.New("retrieved hash chain is invalid")
  66. errInvalidBody = errors.New("retrieved block body is invalid")
  67. errInvalidReceipt = errors.New("retrieved receipt is invalid")
  68. errCancelStateFetch = errors.New("state data download canceled (requested)")
  69. errCancelContentProcessing = errors.New("content processing canceled (requested)")
  70. errCanceled = errors.New("syncing canceled (requested)")
  71. errTooOld = errors.New("peer's protocol version too old")
  72. errNoAncestorFound = errors.New("no common ancestor found")
  73. errNoPivotHeader = errors.New("pivot header is not found")
  74. ErrMergeTransition = errors.New("legacy sync reached the merge")
  75. )
  76. // peerDropFn is a callback type for dropping a peer detected as malicious.
  77. type peerDropFn func(id string)
  78. // badBlockFn is a callback for the async beacon sync to notify the caller that
  79. // the origin header requested to sync to, produced a chain with a bad block.
  80. type badBlockFn func(invalid *types.Header, origin *types.Header)
  81. // headerTask is a set of downloaded headers to queue along with their precomputed
  82. // hashes to avoid constant rehashing.
  83. type headerTask struct {
  84. headers []*types.Header
  85. hashes []common.Hash
  86. }
  87. type Downloader struct {
  88. mode uint32 // Synchronisation mode defining the strategy used (per sync cycle), use d.getMode() to get the SyncMode
  89. mux *event.TypeMux // Event multiplexer to announce sync operation events
  90. checkpoint uint64 // Checkpoint block number to enforce head against (e.g. snap sync)
  91. genesis uint64 // Genesis block number to limit sync to (e.g. light client CHT)
  92. queue *queue // Scheduler for selecting the hashes to download
  93. peers *peerSet // Set of active peers from which download can proceed
  94. stateDB ethdb.Database // Database to state sync into (and deduplicate via)
  95. // Statistics
  96. syncStatsChainOrigin uint64 // Origin block number where syncing started at
  97. syncStatsChainHeight uint64 // Highest block number known when syncing started
  98. syncStatsLock sync.RWMutex // Lock protecting the sync stats fields
  99. lightchain LightChain
  100. blockchain BlockChain
  101. // Callbacks
  102. dropPeer peerDropFn // Drops a peer for misbehaving
  103. badBlock badBlockFn // Reports a block as rejected by the chain
  104. // Status
  105. synchroniseMock func(id string, hash common.Hash) error // Replacement for synchronise during testing
  106. synchronising int32
  107. notified int32
  108. committed int32
  109. ancientLimit uint64 // The maximum block number which can be regarded as ancient data.
  110. // Channels
  111. headerProcCh chan *headerTask // Channel to feed the header processor new tasks
  112. // Skeleton sync
  113. skeleton *skeleton // Header skeleton to backfill the chain with (eth2 mode)
  114. // State sync
  115. pivotHeader *types.Header // Pivot block header to dynamically push the syncing state root
  116. pivotLock sync.RWMutex // Lock protecting pivot header reads from updates
  117. SnapSyncer *snap.Syncer // TODO(karalabe): make private! hack for now
  118. stateSyncStart chan *stateSync
  119. // Cancellation and termination
  120. cancelPeer string // Identifier of the peer currently being used as the master (cancel on drop)
  121. cancelCh chan struct{} // Channel to cancel mid-flight syncs
  122. cancelLock sync.RWMutex // Lock to protect the cancel channel and peer in delivers
  123. cancelWg sync.WaitGroup // Make sure all fetcher goroutines have exited.
  124. quitCh chan struct{} // Quit channel to signal termination
  125. quitLock sync.Mutex // Lock to prevent double closes
  126. // Testing hooks
  127. syncInitHook func(uint64, uint64) // Method to call upon initiating a new sync run
  128. bodyFetchHook func([]*types.Header) // Method to call upon starting a block body fetch
  129. receiptFetchHook func([]*types.Header) // Method to call upon starting a receipt fetch
  130. chainInsertHook func([]*fetchResult) // Method to call upon inserting a chain of blocks (possibly in multiple invocations)
  131. }
  132. // LightChain encapsulates functions required to synchronise a light chain.
  133. type LightChain interface {
  134. // HasHeader verifies a header's presence in the local chain.
  135. HasHeader(common.Hash, uint64) bool
  136. // GetHeaderByHash retrieves a header from the local chain.
  137. GetHeaderByHash(common.Hash) *types.Header
  138. // CurrentHeader retrieves the head header from the local chain.
  139. CurrentHeader() *types.Header
  140. // GetTd returns the total difficulty of a local block.
  141. GetTd(common.Hash, uint64) *big.Int
  142. // InsertHeaderChain inserts a batch of headers into the local chain.
  143. InsertHeaderChain([]*types.Header, int) (int, error)
  144. // SetHead rewinds the local chain to a new head.
  145. SetHead(uint64) error
  146. }
  147. // BlockChain encapsulates functions required to sync a (full or snap) blockchain.
  148. type BlockChain interface {
  149. LightChain
  150. // HasBlock verifies a block's presence in the local chain.
  151. HasBlock(common.Hash, uint64) bool
  152. // HasFastBlock verifies a snap block's presence in the local chain.
  153. HasFastBlock(common.Hash, uint64) bool
  154. // GetBlockByHash retrieves a block from the local chain.
  155. GetBlockByHash(common.Hash) *types.Block
  156. // CurrentBlock retrieves the head block from the local chain.
  157. CurrentBlock() *types.Block
  158. // CurrentFastBlock retrieves the head snap block from the local chain.
  159. CurrentFastBlock() *types.Block
  160. // SnapSyncCommitHead directly commits the head block to a certain entity.
  161. SnapSyncCommitHead(common.Hash) error
  162. // InsertChain inserts a batch of blocks into the local chain.
  163. InsertChain(types.Blocks) (int, error)
  164. // InsertReceiptChain inserts a batch of receipts into the local chain.
  165. InsertReceiptChain(types.Blocks, []types.Receipts, uint64) (int, error)
  166. // Snapshots returns the blockchain snapshot tree to paused it during sync.
  167. Snapshots() *snapshot.Tree
  168. }
  169. // New creates a new downloader to fetch hashes and blocks from remote peers.
  170. func New(checkpoint uint64, stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, lightchain LightChain, dropPeer peerDropFn, success func()) *Downloader {
  171. if lightchain == nil {
  172. lightchain = chain
  173. }
  174. dl := &Downloader{
  175. stateDB: stateDb,
  176. mux: mux,
  177. checkpoint: checkpoint,
  178. queue: newQueue(blockCacheMaxItems, blockCacheInitialItems),
  179. peers: newPeerSet(),
  180. blockchain: chain,
  181. lightchain: lightchain,
  182. dropPeer: dropPeer,
  183. headerProcCh: make(chan *headerTask, 1),
  184. quitCh: make(chan struct{}),
  185. SnapSyncer: snap.NewSyncer(stateDb),
  186. stateSyncStart: make(chan *stateSync),
  187. }
  188. dl.skeleton = newSkeleton(stateDb, dl.peers, dropPeer, newBeaconBackfiller(dl, success))
  189. go dl.stateFetcher()
  190. return dl
  191. }
  192. // Progress retrieves the synchronisation boundaries, specifically the origin
  193. // block where synchronisation started at (may have failed/suspended); the block
  194. // or header sync is currently at; and the latest known block which the sync targets.
  195. //
  196. // In addition, during the state download phase of snap synchronisation the number
  197. // of processed and the total number of known states are also returned. Otherwise
  198. // these are zero.
  199. func (d *Downloader) Progress() ethereum.SyncProgress {
  200. // Lock the current stats and return the progress
  201. d.syncStatsLock.RLock()
  202. defer d.syncStatsLock.RUnlock()
  203. current := uint64(0)
  204. mode := d.getMode()
  205. switch {
  206. case d.blockchain != nil && mode == FullSync:
  207. current = d.blockchain.CurrentBlock().NumberU64()
  208. case d.blockchain != nil && mode == SnapSync:
  209. current = d.blockchain.CurrentFastBlock().NumberU64()
  210. case d.lightchain != nil:
  211. current = d.lightchain.CurrentHeader().Number.Uint64()
  212. default:
  213. log.Error("Unknown downloader chain/mode combo", "light", d.lightchain != nil, "full", d.blockchain != nil, "mode", mode)
  214. }
  215. progress, pending := d.SnapSyncer.Progress()
  216. return ethereum.SyncProgress{
  217. StartingBlock: d.syncStatsChainOrigin,
  218. CurrentBlock: current,
  219. HighestBlock: d.syncStatsChainHeight,
  220. SyncedAccounts: progress.AccountSynced,
  221. SyncedAccountBytes: uint64(progress.AccountBytes),
  222. SyncedBytecodes: progress.BytecodeSynced,
  223. SyncedBytecodeBytes: uint64(progress.BytecodeBytes),
  224. SyncedStorage: progress.StorageSynced,
  225. SyncedStorageBytes: uint64(progress.StorageBytes),
  226. HealedTrienodes: progress.TrienodeHealSynced,
  227. HealedTrienodeBytes: uint64(progress.TrienodeHealBytes),
  228. HealedBytecodes: progress.BytecodeHealSynced,
  229. HealedBytecodeBytes: uint64(progress.BytecodeHealBytes),
  230. HealingTrienodes: pending.TrienodeHeal,
  231. HealingBytecode: pending.BytecodeHeal,
  232. }
  233. }
  234. // Synchronising returns whether the downloader is currently retrieving blocks.
  235. func (d *Downloader) Synchronising() bool {
  236. return atomic.LoadInt32(&d.synchronising) > 0
  237. }
  238. // RegisterPeer injects a new download peer into the set of block source to be
  239. // used for fetching hashes and blocks from.
  240. func (d *Downloader) RegisterPeer(id string, version uint, peer Peer) error {
  241. var logger log.Logger
  242. if len(id) < 16 {
  243. // Tests use short IDs, don't choke on them
  244. logger = log.New("peer", id)
  245. } else {
  246. logger = log.New("peer", id[:8])
  247. }
  248. logger.Trace("Registering sync peer")
  249. if err := d.peers.Register(newPeerConnection(id, version, peer, logger)); err != nil {
  250. logger.Error("Failed to register sync peer", "err", err)
  251. return err
  252. }
  253. return nil
  254. }
  255. // RegisterLightPeer injects a light client peer, wrapping it so it appears as a regular peer.
  256. func (d *Downloader) RegisterLightPeer(id string, version uint, peer LightPeer) error {
  257. return d.RegisterPeer(id, version, &lightPeerWrapper{peer})
  258. }
  259. // UnregisterPeer remove a peer from the known list, preventing any action from
  260. // the specified peer. An effort is also made to return any pending fetches into
  261. // the queue.
  262. func (d *Downloader) UnregisterPeer(id string) error {
  263. // Unregister the peer from the active peer set and revoke any fetch tasks
  264. var logger log.Logger
  265. if len(id) < 16 {
  266. // Tests use short IDs, don't choke on them
  267. logger = log.New("peer", id)
  268. } else {
  269. logger = log.New("peer", id[:8])
  270. }
  271. logger.Trace("Unregistering sync peer")
  272. if err := d.peers.Unregister(id); err != nil {
  273. logger.Error("Failed to unregister sync peer", "err", err)
  274. return err
  275. }
  276. d.queue.Revoke(id)
  277. return nil
  278. }
  279. // LegacySync tries to sync up our local block chain with a remote peer, both
  280. // adding various sanity checks as well as wrapping it with various log entries.
  281. func (d *Downloader) LegacySync(id string, head common.Hash, td, ttd *big.Int, mode SyncMode) error {
  282. err := d.synchronise(id, head, td, ttd, mode, false, nil)
  283. switch err {
  284. case nil, errBusy, errCanceled:
  285. return err
  286. }
  287. if errors.Is(err, errInvalidChain) || errors.Is(err, errBadPeer) || errors.Is(err, errTimeout) ||
  288. errors.Is(err, errStallingPeer) || errors.Is(err, errUnsyncedPeer) || errors.Is(err, errEmptyHeaderSet) ||
  289. errors.Is(err, errPeersUnavailable) || errors.Is(err, errTooOld) || errors.Is(err, errInvalidAncestor) {
  290. log.Warn("Synchronisation failed, dropping peer", "peer", id, "err", err)
  291. if d.dropPeer == nil {
  292. // The dropPeer method is nil when `--copydb` is used for a local copy.
  293. // Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
  294. log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", id)
  295. } else {
  296. d.dropPeer(id)
  297. }
  298. return err
  299. }
  300. if errors.Is(err, ErrMergeTransition) {
  301. return err // This is an expected fault, don't keep printing it in a spin-loop
  302. }
  303. log.Warn("Synchronisation failed, retrying", "err", err)
  304. return err
  305. }
  306. // synchronise will select the peer and use it for synchronising. If an empty string is given
  307. // it will use the best peer possible and synchronize if its TD is higher than our own. If any of the
  308. // checks fail an error will be returned. This method is synchronous
  309. func (d *Downloader) synchronise(id string, hash common.Hash, td, ttd *big.Int, mode SyncMode, beaconMode bool, beaconPing chan struct{}) error {
  310. // The beacon header syncer is async. It will start this synchronization and
  311. // will continue doing other tasks. However, if synchronization needs to be
  312. // cancelled, the syncer needs to know if we reached the startup point (and
  313. // inited the cancel channel) or not yet. Make sure that we'll signal even in
  314. // case of a failure.
  315. if beaconPing != nil {
  316. defer func() {
  317. select {
  318. case <-beaconPing: // already notified
  319. default:
  320. close(beaconPing) // weird exit condition, notify that it's safe to cancel (the nothing)
  321. }
  322. }()
  323. }
  324. // Mock out the synchronisation if testing
  325. if d.synchroniseMock != nil {
  326. return d.synchroniseMock(id, hash)
  327. }
  328. // Make sure only one goroutine is ever allowed past this point at once
  329. if !atomic.CompareAndSwapInt32(&d.synchronising, 0, 1) {
  330. return errBusy
  331. }
  332. defer atomic.StoreInt32(&d.synchronising, 0)
  333. // Post a user notification of the sync (only once per session)
  334. if atomic.CompareAndSwapInt32(&d.notified, 0, 1) {
  335. log.Info("Block synchronisation started")
  336. }
  337. if mode == SnapSync {
  338. // Snap sync uses the snapshot namespace to store potentially flakey data until
  339. // sync completely heals and finishes. Pause snapshot maintenance in the mean-
  340. // time to prevent access.
  341. if snapshots := d.blockchain.Snapshots(); snapshots != nil { // Only nil in tests
  342. snapshots.Disable()
  343. }
  344. }
  345. // Reset the queue, peer set and wake channels to clean any internal leftover state
  346. d.queue.Reset(blockCacheMaxItems, blockCacheInitialItems)
  347. d.peers.Reset()
  348. for _, ch := range []chan bool{d.queue.blockWakeCh, d.queue.receiptWakeCh} {
  349. select {
  350. case <-ch:
  351. default:
  352. }
  353. }
  354. for empty := false; !empty; {
  355. select {
  356. case <-d.headerProcCh:
  357. default:
  358. empty = true
  359. }
  360. }
  361. // Create cancel channel for aborting mid-flight and mark the master peer
  362. d.cancelLock.Lock()
  363. d.cancelCh = make(chan struct{})
  364. d.cancelPeer = id
  365. d.cancelLock.Unlock()
  366. defer d.Cancel() // No matter what, we can't leave the cancel channel open
  367. // Atomically set the requested sync mode
  368. atomic.StoreUint32(&d.mode, uint32(mode))
  369. // Retrieve the origin peer and initiate the downloading process
  370. var p *peerConnection
  371. if !beaconMode { // Beacon mode doesn't need a peer to sync from
  372. p = d.peers.Peer(id)
  373. if p == nil {
  374. return errUnknownPeer
  375. }
  376. }
  377. if beaconPing != nil {
  378. close(beaconPing)
  379. }
  380. return d.syncWithPeer(p, hash, td, ttd, beaconMode)
  381. }
  382. func (d *Downloader) getMode() SyncMode {
  383. return SyncMode(atomic.LoadUint32(&d.mode))
  384. }
  385. // syncWithPeer starts a block synchronization based on the hash chain from the
  386. // specified peer and head hash.
  387. func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td, ttd *big.Int, beaconMode bool) (err error) {
  388. d.mux.Post(StartEvent{})
  389. defer func() {
  390. // reset on error
  391. if err != nil {
  392. d.mux.Post(FailedEvent{err})
  393. } else {
  394. latest := d.lightchain.CurrentHeader()
  395. d.mux.Post(DoneEvent{latest})
  396. }
  397. }()
  398. mode := d.getMode()
  399. if !beaconMode {
  400. log.Debug("Synchronising with the network", "peer", p.id, "eth", p.version, "head", hash, "td", td, "mode", mode)
  401. } else {
  402. log.Debug("Backfilling with the network", "mode", mode)
  403. }
  404. defer func(start time.Time) {
  405. log.Debug("Synchronisation terminated", "elapsed", common.PrettyDuration(time.Since(start)))
  406. }(time.Now())
  407. // Look up the sync boundaries: the common ancestor and the target block
  408. var latest, pivot *types.Header
  409. if !beaconMode {
  410. // In legacy mode, use the master peer to retrieve the headers from
  411. latest, pivot, err = d.fetchHead(p)
  412. if err != nil {
  413. return err
  414. }
  415. } else {
  416. // In beacon mode, user the skeleton chain to retrieve the headers from
  417. latest, _, err = d.skeleton.Bounds()
  418. if err != nil {
  419. return err
  420. }
  421. if latest.Number.Uint64() > uint64(fsMinFullBlocks) {
  422. number := latest.Number.Uint64() - uint64(fsMinFullBlocks)
  423. // Retrieve the pivot header from the skeleton chain segment but
  424. // fallback to local chain if it's not found in skeleton space.
  425. if pivot = d.skeleton.Header(number); pivot == nil {
  426. _, oldest, _ := d.skeleton.Bounds() // error is already checked
  427. if number < oldest.Number.Uint64() {
  428. count := int(oldest.Number.Uint64() - number) // it's capped by fsMinFullBlocks
  429. headers := d.readHeaderRange(oldest, count)
  430. if len(headers) == count {
  431. pivot = headers[len(headers)-1]
  432. log.Warn("Retrieved pivot header from local", "number", pivot.Number, "hash", pivot.Hash(), "latest", latest.Number, "oldest", oldest.Number)
  433. }
  434. }
  435. }
  436. // Print an error log and return directly in case the pivot header
  437. // is still not found. It means the skeleton chain is not linked
  438. // correctly with local chain.
  439. if pivot == nil {
  440. log.Error("Pivot header is not found", "number", number)
  441. return errNoPivotHeader
  442. }
  443. }
  444. }
  445. // If no pivot block was returned, the head is below the min full block
  446. // threshold (i.e. new chain). In that case we won't really snap sync
  447. // anyway, but still need a valid pivot block to avoid some code hitting
  448. // nil panics on access.
  449. if mode == SnapSync && pivot == nil {
  450. pivot = d.blockchain.CurrentBlock().Header()
  451. }
  452. height := latest.Number.Uint64()
  453. var origin uint64
  454. if !beaconMode {
  455. // In legacy mode, reach out to the network and find the ancestor
  456. origin, err = d.findAncestor(p, latest)
  457. if err != nil {
  458. return err
  459. }
  460. } else {
  461. // In beacon mode, use the skeleton chain for the ancestor lookup
  462. origin, err = d.findBeaconAncestor()
  463. if err != nil {
  464. return err
  465. }
  466. }
  467. d.syncStatsLock.Lock()
  468. if d.syncStatsChainHeight <= origin || d.syncStatsChainOrigin > origin {
  469. d.syncStatsChainOrigin = origin
  470. }
  471. d.syncStatsChainHeight = height
  472. d.syncStatsLock.Unlock()
  473. // Ensure our origin point is below any snap sync pivot point
  474. if mode == SnapSync {
  475. if height <= uint64(fsMinFullBlocks) {
  476. origin = 0
  477. } else {
  478. pivotNumber := pivot.Number.Uint64()
  479. if pivotNumber <= origin {
  480. origin = pivotNumber - 1
  481. }
  482. // Write out the pivot into the database so a rollback beyond it will
  483. // reenable snap sync
  484. rawdb.WriteLastPivotNumber(d.stateDB, pivotNumber)
  485. }
  486. }
  487. d.committed = 1
  488. if mode == SnapSync && pivot.Number.Uint64() != 0 {
  489. d.committed = 0
  490. }
  491. if mode == SnapSync {
  492. // Set the ancient data limitation.
  493. // If we are running snap sync, all block data older than ancientLimit will be
  494. // written to the ancient store. More recent data will be written to the active
  495. // database and will wait for the freezer to migrate.
  496. //
  497. // If there is a checkpoint available, then calculate the ancientLimit through
  498. // that. Otherwise calculate the ancient limit through the advertised height
  499. // of the remote peer.
  500. //
  501. // The reason for picking checkpoint first is that a malicious peer can give us
  502. // a fake (very high) height, forcing the ancient limit to also be very high.
  503. // The peer would start to feed us valid blocks until head, resulting in all of
  504. // the blocks might be written into the ancient store. A following mini-reorg
  505. // could cause issues.
  506. if d.checkpoint != 0 && d.checkpoint > fullMaxForkAncestry+1 {
  507. d.ancientLimit = d.checkpoint
  508. } else if height > fullMaxForkAncestry+1 {
  509. d.ancientLimit = height - fullMaxForkAncestry - 1
  510. } else {
  511. d.ancientLimit = 0
  512. }
  513. frozen, _ := d.stateDB.Ancients() // Ignore the error here since light client can also hit here.
  514. // If a part of blockchain data has already been written into active store,
  515. // disable the ancient style insertion explicitly.
  516. if origin >= frozen && frozen != 0 {
  517. d.ancientLimit = 0
  518. log.Info("Disabling direct-ancient mode", "origin", origin, "ancient", frozen-1)
  519. } else if d.ancientLimit > 0 {
  520. log.Debug("Enabling direct-ancient mode", "ancient", d.ancientLimit)
  521. }
  522. // Rewind the ancient store and blockchain if reorg happens.
  523. if origin+1 < frozen {
  524. if err := d.lightchain.SetHead(origin); err != nil {
  525. return err
  526. }
  527. }
  528. }
  529. // Initiate the sync using a concurrent header and content retrieval algorithm
  530. d.queue.Prepare(origin+1, mode)
  531. if d.syncInitHook != nil {
  532. d.syncInitHook(origin, height)
  533. }
  534. var headerFetcher func() error
  535. if !beaconMode {
  536. // In legacy mode, headers are retrieved from the network
  537. headerFetcher = func() error { return d.fetchHeaders(p, origin+1, latest.Number.Uint64()) }
  538. } else {
  539. // In beacon mode, headers are served by the skeleton syncer
  540. headerFetcher = func() error { return d.fetchBeaconHeaders(origin + 1) }
  541. }
  542. fetchers := []func() error{
  543. headerFetcher, // Headers are always retrieved
  544. func() error { return d.fetchBodies(origin+1, beaconMode) }, // Bodies are retrieved during normal and snap sync
  545. func() error { return d.fetchReceipts(origin+1, beaconMode) }, // Receipts are retrieved during snap sync
  546. func() error { return d.processHeaders(origin+1, td, ttd, beaconMode) },
  547. }
  548. if mode == SnapSync {
  549. d.pivotLock.Lock()
  550. d.pivotHeader = pivot
  551. d.pivotLock.Unlock()
  552. fetchers = append(fetchers, func() error { return d.processSnapSyncContent() })
  553. } else if mode == FullSync {
  554. fetchers = append(fetchers, func() error { return d.processFullSyncContent(ttd, beaconMode) })
  555. }
  556. return d.spawnSync(fetchers)
  557. }
  558. // spawnSync runs d.process and all given fetcher functions to completion in
  559. // separate goroutines, returning the first error that appears.
  560. func (d *Downloader) spawnSync(fetchers []func() error) error {
  561. errc := make(chan error, len(fetchers))
  562. d.cancelWg.Add(len(fetchers))
  563. for _, fn := range fetchers {
  564. fn := fn
  565. go func() { defer d.cancelWg.Done(); errc <- fn() }()
  566. }
  567. // Wait for the first error, then terminate the others.
  568. var err error
  569. for i := 0; i < len(fetchers); i++ {
  570. if i == len(fetchers)-1 {
  571. // Close the queue when all fetchers have exited.
  572. // This will cause the block processor to end when
  573. // it has processed the queue.
  574. d.queue.Close()
  575. }
  576. if err = <-errc; err != nil && err != errCanceled {
  577. break
  578. }
  579. }
  580. d.queue.Close()
  581. d.Cancel()
  582. return err
  583. }
  584. // cancel aborts all of the operations and resets the queue. However, cancel does
  585. // not wait for the running download goroutines to finish. This method should be
  586. // used when cancelling the downloads from inside the downloader.
  587. func (d *Downloader) cancel() {
  588. // Close the current cancel channel
  589. d.cancelLock.Lock()
  590. defer d.cancelLock.Unlock()
  591. if d.cancelCh != nil {
  592. select {
  593. case <-d.cancelCh:
  594. // Channel was already closed
  595. default:
  596. close(d.cancelCh)
  597. }
  598. }
  599. }
  600. // Cancel aborts all of the operations and waits for all download goroutines to
  601. // finish before returning.
  602. func (d *Downloader) Cancel() {
  603. d.cancel()
  604. d.cancelWg.Wait()
  605. }
  606. // Terminate interrupts the downloader, canceling all pending operations.
  607. // The downloader cannot be reused after calling Terminate.
  608. func (d *Downloader) Terminate() {
  609. // Close the termination channel (make sure double close is allowed)
  610. d.quitLock.Lock()
  611. select {
  612. case <-d.quitCh:
  613. default:
  614. close(d.quitCh)
  615. // Terminate the internal beacon syncer
  616. d.skeleton.Terminate()
  617. }
  618. d.quitLock.Unlock()
  619. // Cancel any pending download requests
  620. d.Cancel()
  621. }
  622. // fetchHead retrieves the head header and prior pivot block (if available) from
  623. // a remote peer.
  624. func (d *Downloader) fetchHead(p *peerConnection) (head *types.Header, pivot *types.Header, err error) {
  625. p.log.Debug("Retrieving remote chain head")
  626. mode := d.getMode()
  627. // Request the advertised remote head block and wait for the response
  628. latest, _ := p.peer.Head()
  629. fetch := 1
  630. if mode == SnapSync {
  631. fetch = 2 // head + pivot headers
  632. }
  633. headers, hashes, err := d.fetchHeadersByHash(p, latest, fetch, fsMinFullBlocks-1, true)
  634. if err != nil {
  635. return nil, nil, err
  636. }
  637. // Make sure the peer gave us at least one and at most the requested headers
  638. if len(headers) == 0 || len(headers) > fetch {
  639. return nil, nil, fmt.Errorf("%w: returned headers %d != requested %d", errBadPeer, len(headers), fetch)
  640. }
  641. // The first header needs to be the head, validate against the checkpoint
  642. // and request. If only 1 header was returned, make sure there's no pivot
  643. // or there was not one requested.
  644. head = headers[0]
  645. if (mode == SnapSync || mode == LightSync) && head.Number.Uint64() < d.checkpoint {
  646. return nil, nil, fmt.Errorf("%w: remote head %d below checkpoint %d", errUnsyncedPeer, head.Number, d.checkpoint)
  647. }
  648. if len(headers) == 1 {
  649. if mode == SnapSync && head.Number.Uint64() > uint64(fsMinFullBlocks) {
  650. return nil, nil, fmt.Errorf("%w: no pivot included along head header", errBadPeer)
  651. }
  652. p.log.Debug("Remote head identified, no pivot", "number", head.Number, "hash", hashes[0])
  653. return head, nil, nil
  654. }
  655. // At this point we have 2 headers in total and the first is the
  656. // validated head of the chain. Check the pivot number and return,
  657. pivot = headers[1]
  658. if pivot.Number.Uint64() != head.Number.Uint64()-uint64(fsMinFullBlocks) {
  659. return nil, nil, fmt.Errorf("%w: remote pivot %d != requested %d", errInvalidChain, pivot.Number, head.Number.Uint64()-uint64(fsMinFullBlocks))
  660. }
  661. return head, pivot, nil
  662. }
  663. // calculateRequestSpan calculates what headers to request from a peer when trying to determine the
  664. // common ancestor.
  665. // It returns parameters to be used for peer.RequestHeadersByNumber:
  666. // from - starting block number
  667. // count - number of headers to request
  668. // skip - number of headers to skip
  669. // and also returns 'max', the last block which is expected to be returned by the remote peers,
  670. // given the (from,count,skip)
  671. func calculateRequestSpan(remoteHeight, localHeight uint64) (int64, int, int, uint64) {
  672. var (
  673. from int
  674. count int
  675. MaxCount = MaxHeaderFetch / 16
  676. )
  677. // requestHead is the highest block that we will ask for. If requestHead is not offset,
  678. // the highest block that we will get is 16 blocks back from head, which means we
  679. // will fetch 14 or 15 blocks unnecessarily in the case the height difference
  680. // between us and the peer is 1-2 blocks, which is most common
  681. requestHead := int(remoteHeight) - 1
  682. if requestHead < 0 {
  683. requestHead = 0
  684. }
  685. // requestBottom is the lowest block we want included in the query
  686. // Ideally, we want to include the one just below our own head
  687. requestBottom := int(localHeight - 1)
  688. if requestBottom < 0 {
  689. requestBottom = 0
  690. }
  691. totalSpan := requestHead - requestBottom
  692. span := 1 + totalSpan/MaxCount
  693. if span < 2 {
  694. span = 2
  695. }
  696. if span > 16 {
  697. span = 16
  698. }
  699. count = 1 + totalSpan/span
  700. if count > MaxCount {
  701. count = MaxCount
  702. }
  703. if count < 2 {
  704. count = 2
  705. }
  706. from = requestHead - (count-1)*span
  707. if from < 0 {
  708. from = 0
  709. }
  710. max := from + (count-1)*span
  711. return int64(from), count, span - 1, uint64(max)
  712. }
  713. // findAncestor tries to locate the common ancestor link of the local chain and
  714. // a remote peers blockchain. In the general case when our node was in sync and
  715. // on the correct chain, checking the top N links should already get us a match.
  716. // In the rare scenario when we ended up on a long reorganisation (i.e. none of
  717. // the head links match), we do a binary search to find the common ancestor.
  718. func (d *Downloader) findAncestor(p *peerConnection, remoteHeader *types.Header) (uint64, error) {
  719. // Figure out the valid ancestor range to prevent rewrite attacks
  720. var (
  721. floor = int64(-1)
  722. localHeight uint64
  723. remoteHeight = remoteHeader.Number.Uint64()
  724. )
  725. mode := d.getMode()
  726. switch mode {
  727. case FullSync:
  728. localHeight = d.blockchain.CurrentBlock().NumberU64()
  729. case SnapSync:
  730. localHeight = d.blockchain.CurrentFastBlock().NumberU64()
  731. default:
  732. localHeight = d.lightchain.CurrentHeader().Number.Uint64()
  733. }
  734. p.log.Debug("Looking for common ancestor", "local", localHeight, "remote", remoteHeight)
  735. // Recap floor value for binary search
  736. maxForkAncestry := fullMaxForkAncestry
  737. if d.getMode() == LightSync {
  738. maxForkAncestry = lightMaxForkAncestry
  739. }
  740. if localHeight >= maxForkAncestry {
  741. // We're above the max reorg threshold, find the earliest fork point
  742. floor = int64(localHeight - maxForkAncestry)
  743. }
  744. // If we're doing a light sync, ensure the floor doesn't go below the CHT, as
  745. // all headers before that point will be missing.
  746. if mode == LightSync {
  747. // If we don't know the current CHT position, find it
  748. if d.genesis == 0 {
  749. header := d.lightchain.CurrentHeader()
  750. for header != nil {
  751. d.genesis = header.Number.Uint64()
  752. if floor >= int64(d.genesis)-1 {
  753. break
  754. }
  755. header = d.lightchain.GetHeaderByHash(header.ParentHash)
  756. }
  757. }
  758. // We already know the "genesis" block number, cap floor to that
  759. if floor < int64(d.genesis)-1 {
  760. floor = int64(d.genesis) - 1
  761. }
  762. }
  763. ancestor, err := d.findAncestorSpanSearch(p, mode, remoteHeight, localHeight, floor)
  764. if err == nil {
  765. return ancestor, nil
  766. }
  767. // The returned error was not nil.
  768. // If the error returned does not reflect that a common ancestor was not found, return it.
  769. // If the error reflects that a common ancestor was not found, continue to binary search,
  770. // where the error value will be reassigned.
  771. if !errors.Is(err, errNoAncestorFound) {
  772. return 0, err
  773. }
  774. ancestor, err = d.findAncestorBinarySearch(p, mode, remoteHeight, floor)
  775. if err != nil {
  776. return 0, err
  777. }
  778. return ancestor, nil
  779. }
  780. func (d *Downloader) findAncestorSpanSearch(p *peerConnection, mode SyncMode, remoteHeight, localHeight uint64, floor int64) (uint64, error) {
  781. from, count, skip, max := calculateRequestSpan(remoteHeight, localHeight)
  782. p.log.Trace("Span searching for common ancestor", "count", count, "from", from, "skip", skip)
  783. headers, hashes, err := d.fetchHeadersByNumber(p, uint64(from), count, skip, false)
  784. if err != nil {
  785. return 0, err
  786. }
  787. // Wait for the remote response to the head fetch
  788. number, hash := uint64(0), common.Hash{}
  789. // Make sure the peer actually gave something valid
  790. if len(headers) == 0 {
  791. p.log.Warn("Empty head header set")
  792. return 0, errEmptyHeaderSet
  793. }
  794. // Make sure the peer's reply conforms to the request
  795. for i, header := range headers {
  796. expectNumber := from + int64(i)*int64(skip+1)
  797. if number := header.Number.Int64(); number != expectNumber {
  798. p.log.Warn("Head headers broke chain ordering", "index", i, "requested", expectNumber, "received", number)
  799. return 0, fmt.Errorf("%w: %v", errInvalidChain, errors.New("head headers broke chain ordering"))
  800. }
  801. }
  802. // Check if a common ancestor was found
  803. for i := len(headers) - 1; i >= 0; i-- {
  804. // Skip any headers that underflow/overflow our requested set
  805. if headers[i].Number.Int64() < from || headers[i].Number.Uint64() > max {
  806. continue
  807. }
  808. // Otherwise check if we already know the header or not
  809. h := hashes[i]
  810. n := headers[i].Number.Uint64()
  811. var known bool
  812. switch mode {
  813. case FullSync:
  814. known = d.blockchain.HasBlock(h, n)
  815. case SnapSync:
  816. known = d.blockchain.HasFastBlock(h, n)
  817. default:
  818. known = d.lightchain.HasHeader(h, n)
  819. }
  820. if known {
  821. number, hash = n, h
  822. break
  823. }
  824. }
  825. // If the head fetch already found an ancestor, return
  826. if hash != (common.Hash{}) {
  827. if int64(number) <= floor {
  828. p.log.Warn("Ancestor below allowance", "number", number, "hash", hash, "allowance", floor)
  829. return 0, errInvalidAncestor
  830. }
  831. p.log.Debug("Found common ancestor", "number", number, "hash", hash)
  832. return number, nil
  833. }
  834. return 0, errNoAncestorFound
  835. }
  836. func (d *Downloader) findAncestorBinarySearch(p *peerConnection, mode SyncMode, remoteHeight uint64, floor int64) (uint64, error) {
  837. hash := common.Hash{}
  838. // Ancestor not found, we need to binary search over our chain
  839. start, end := uint64(0), remoteHeight
  840. if floor > 0 {
  841. start = uint64(floor)
  842. }
  843. p.log.Trace("Binary searching for common ancestor", "start", start, "end", end)
  844. for start+1 < end {
  845. // Split our chain interval in two, and request the hash to cross check
  846. check := (start + end) / 2
  847. headers, hashes, err := d.fetchHeadersByNumber(p, check, 1, 0, false)
  848. if err != nil {
  849. return 0, err
  850. }
  851. // Make sure the peer actually gave something valid
  852. if len(headers) != 1 {
  853. p.log.Warn("Multiple headers for single request", "headers", len(headers))
  854. return 0, fmt.Errorf("%w: multiple headers (%d) for single request", errBadPeer, len(headers))
  855. }
  856. // Modify the search interval based on the response
  857. h := hashes[0]
  858. n := headers[0].Number.Uint64()
  859. var known bool
  860. switch mode {
  861. case FullSync:
  862. known = d.blockchain.HasBlock(h, n)
  863. case SnapSync:
  864. known = d.blockchain.HasFastBlock(h, n)
  865. default:
  866. known = d.lightchain.HasHeader(h, n)
  867. }
  868. if !known {
  869. end = check
  870. continue
  871. }
  872. header := d.lightchain.GetHeaderByHash(h) // Independent of sync mode, header surely exists
  873. if header.Number.Uint64() != check {
  874. p.log.Warn("Received non requested header", "number", header.Number, "hash", header.Hash(), "request", check)
  875. return 0, fmt.Errorf("%w: non-requested header (%d)", errBadPeer, header.Number)
  876. }
  877. start = check
  878. hash = h
  879. }
  880. // Ensure valid ancestry and return
  881. if int64(start) <= floor {
  882. p.log.Warn("Ancestor below allowance", "number", start, "hash", hash, "allowance", floor)
  883. return 0, errInvalidAncestor
  884. }
  885. p.log.Debug("Found common ancestor", "number", start, "hash", hash)
  886. return start, nil
  887. }
  888. // fetchHeaders keeps retrieving headers concurrently from the number
  889. // requested, until no more are returned, potentially throttling on the way. To
  890. // facilitate concurrency but still protect against malicious nodes sending bad
  891. // headers, we construct a header chain skeleton using the "origin" peer we are
  892. // syncing with, and fill in the missing headers using anyone else. Headers from
  893. // other peers are only accepted if they map cleanly to the skeleton. If no one
  894. // can fill in the skeleton - not even the origin peer - it's assumed invalid and
  895. // the origin is dropped.
  896. func (d *Downloader) fetchHeaders(p *peerConnection, from uint64, head uint64) error {
  897. p.log.Debug("Directing header downloads", "origin", from)
  898. defer p.log.Debug("Header download terminated")
  899. // Start pulling the header chain skeleton until all is done
  900. var (
  901. skeleton = true // Skeleton assembly phase or finishing up
  902. pivoting = false // Whether the next request is pivot verification
  903. ancestor = from
  904. mode = d.getMode()
  905. )
  906. for {
  907. // Pull the next batch of headers, it either:
  908. // - Pivot check to see if the chain moved too far
  909. // - Skeleton retrieval to permit concurrent header fetches
  910. // - Full header retrieval if we're near the chain head
  911. var (
  912. headers []*types.Header
  913. hashes []common.Hash
  914. err error
  915. )
  916. switch {
  917. case pivoting:
  918. d.pivotLock.RLock()
  919. pivot := d.pivotHeader.Number.Uint64()
  920. d.pivotLock.RUnlock()
  921. p.log.Trace("Fetching next pivot header", "number", pivot+uint64(fsMinFullBlocks))
  922. headers, hashes, err = d.fetchHeadersByNumber(p, pivot+uint64(fsMinFullBlocks), 2, fsMinFullBlocks-9, false) // move +64 when it's 2x64-8 deep
  923. case skeleton:
  924. p.log.Trace("Fetching skeleton headers", "count", MaxHeaderFetch, "from", from)
  925. headers, hashes, err = d.fetchHeadersByNumber(p, from+uint64(MaxHeaderFetch)-1, MaxSkeletonSize, MaxHeaderFetch-1, false)
  926. default:
  927. p.log.Trace("Fetching full headers", "count", MaxHeaderFetch, "from", from)
  928. headers, hashes, err = d.fetchHeadersByNumber(p, from, MaxHeaderFetch, 0, false)
  929. }
  930. switch err {
  931. case nil:
  932. // Headers retrieved, continue with processing
  933. case errCanceled:
  934. // Sync cancelled, no issue, propagate up
  935. return err
  936. default:
  937. // Header retrieval either timed out, or the peer failed in some strange way
  938. // (e.g. disconnect). Consider the master peer bad and drop
  939. d.dropPeer(p.id)
  940. // Finish the sync gracefully instead of dumping the gathered data though
  941. for _, ch := range []chan bool{d.queue.blockWakeCh, d.queue.receiptWakeCh} {
  942. select {
  943. case ch <- false:
  944. case <-d.cancelCh:
  945. }
  946. }
  947. select {
  948. case d.headerProcCh <- nil:
  949. case <-d.cancelCh:
  950. }
  951. return fmt.Errorf("%w: header request failed: %v", errBadPeer, err)
  952. }
  953. // If the pivot is being checked, move if it became stale and run the real retrieval
  954. var pivot uint64
  955. d.pivotLock.RLock()
  956. if d.pivotHeader != nil {
  957. pivot = d.pivotHeader.Number.Uint64()
  958. }
  959. d.pivotLock.RUnlock()
  960. if pivoting {
  961. if len(headers) == 2 {
  962. if have, want := headers[0].Number.Uint64(), pivot+uint64(fsMinFullBlocks); have != want {
  963. log.Warn("Peer sent invalid next pivot", "have", have, "want", want)
  964. return fmt.Errorf("%w: next pivot number %d != requested %d", errInvalidChain, have, want)
  965. }
  966. if have, want := headers[1].Number.Uint64(), pivot+2*uint64(fsMinFullBlocks)-8; have != want {
  967. log.Warn("Peer sent invalid pivot confirmer", "have", have, "want", want)
  968. return fmt.Errorf("%w: next pivot confirmer number %d != requested %d", errInvalidChain, have, want)
  969. }
  970. log.Warn("Pivot seemingly stale, moving", "old", pivot, "new", headers[0].Number)
  971. pivot = headers[0].Number.Uint64()
  972. d.pivotLock.Lock()
  973. d.pivotHeader = headers[0]
  974. d.pivotLock.Unlock()
  975. // Write out the pivot into the database so a rollback beyond
  976. // it will reenable snap sync and update the state root that
  977. // the state syncer will be downloading.
  978. rawdb.WriteLastPivotNumber(d.stateDB, pivot)
  979. }
  980. // Disable the pivot check and fetch the next batch of headers
  981. pivoting = false
  982. continue
  983. }
  984. // If the skeleton's finished, pull any remaining head headers directly from the origin
  985. if skeleton && len(headers) == 0 {
  986. // A malicious node might withhold advertised headers indefinitely
  987. if from+uint64(MaxHeaderFetch)-1 <= head {
  988. p.log.Warn("Peer withheld skeleton headers", "advertised", head, "withheld", from+uint64(MaxHeaderFetch)-1)
  989. return fmt.Errorf("%w: withheld skeleton headers: advertised %d, withheld #%d", errStallingPeer, head, from+uint64(MaxHeaderFetch)-1)
  990. }
  991. p.log.Debug("No skeleton, fetching headers directly")
  992. skeleton = false
  993. continue
  994. }
  995. // If no more headers are inbound, notify the content fetchers and return
  996. if len(headers) == 0 {
  997. // Don't abort header fetches while the pivot is downloading
  998. if atomic.LoadInt32(&d.committed) == 0 && pivot <= from {
  999. p.log.Debug("No headers, waiting for pivot commit")
  1000. select {
  1001. case <-time.After(fsHeaderContCheck):
  1002. continue
  1003. case <-d.cancelCh:
  1004. return errCanceled
  1005. }
  1006. }
  1007. // Pivot done (or not in snap sync) and no more headers, terminate the process
  1008. p.log.Debug("No more headers available")
  1009. select {
  1010. case d.headerProcCh <- nil:
  1011. return nil
  1012. case <-d.cancelCh:
  1013. return errCanceled
  1014. }
  1015. }
  1016. // If we received a skeleton batch, resolve internals concurrently
  1017. var progressed bool
  1018. if skeleton {
  1019. filled, hashset, proced, err := d.fillHeaderSkeleton(from, headers)
  1020. if err != nil {
  1021. p.log.Debug("Skeleton chain invalid", "err", err)
  1022. return fmt.Errorf("%w: %v", errInvalidChain, err)
  1023. }
  1024. headers = filled[proced:]
  1025. hashes = hashset[proced:]
  1026. progressed = proced > 0
  1027. from += uint64(proced)
  1028. } else {
  1029. // A malicious node might withhold advertised headers indefinitely
  1030. if n := len(headers); n < MaxHeaderFetch && headers[n-1].Number.Uint64() < head {
  1031. p.log.Warn("Peer withheld headers", "advertised", head, "delivered", headers[n-1].Number.Uint64())
  1032. return fmt.Errorf("%w: withheld headers: advertised %d, delivered %d", errStallingPeer, head, headers[n-1].Number.Uint64())
  1033. }
  1034. // If we're closing in on the chain head, but haven't yet reached it, delay
  1035. // the last few headers so mini reorgs on the head don't cause invalid hash
  1036. // chain errors.
  1037. if n := len(headers); n > 0 {
  1038. // Retrieve the current head we're at
  1039. var head uint64
  1040. if mode == LightSync {
  1041. head = d.lightchain.CurrentHeader().Number.Uint64()
  1042. } else {
  1043. head = d.blockchain.CurrentFastBlock().NumberU64()
  1044. if full := d.blockchain.CurrentBlock().NumberU64(); head < full {
  1045. head = full
  1046. }
  1047. }
  1048. // If the head is below the common ancestor, we're actually deduplicating
  1049. // already existing chain segments, so use the ancestor as the fake head.
  1050. // Otherwise, we might end up delaying header deliveries pointlessly.
  1051. if head < ancestor {
  1052. head = ancestor
  1053. }
  1054. // If the head is way older than this batch, delay the last few headers
  1055. if head+uint64(reorgProtThreshold) < headers[n-1].Number.Uint64() {
  1056. delay := reorgProtHeaderDelay
  1057. if delay > n {
  1058. delay = n
  1059. }
  1060. headers = headers[:n-delay]
  1061. hashes = hashes[:n-delay]
  1062. }
  1063. }
  1064. }
  1065. // If no headers have bene delivered, or all of them have been delayed,
  1066. // sleep a bit and retry. Take care with headers already consumed during
  1067. // skeleton filling
  1068. if len(headers) == 0 && !progressed {
  1069. p.log.Trace("All headers delayed, waiting")
  1070. select {
  1071. case <-time.After(fsHeaderContCheck):
  1072. continue
  1073. case <-d.cancelCh:
  1074. return errCanceled
  1075. }
  1076. }
  1077. // Insert any remaining new headers and fetch the next batch
  1078. if len(headers) > 0 {
  1079. p.log.Trace("Scheduling new headers", "count", len(headers), "from", from)
  1080. select {
  1081. case d.headerProcCh <- &headerTask{
  1082. headers: headers,
  1083. hashes: hashes,
  1084. }:
  1085. case <-d.cancelCh:
  1086. return errCanceled
  1087. }
  1088. from += uint64(len(headers))
  1089. }
  1090. // If we're still skeleton filling snap sync, check pivot staleness
  1091. // before continuing to the next skeleton filling
  1092. if skeleton && pivot > 0 {
  1093. pivoting = true
  1094. }
  1095. }
  1096. }
  1097. // fillHeaderSkeleton concurrently retrieves headers from all our available peers
  1098. // and maps them to the provided skeleton header chain.
  1099. //
  1100. // Any partial results from the beginning of the skeleton is (if possible) forwarded
  1101. // immediately to the header processor to keep the rest of the pipeline full even
  1102. // in the case of header stalls.
  1103. //
  1104. // The method returns the entire filled skeleton and also the number of headers
  1105. // already forwarded for processing.
  1106. func (d *Downloader) fillHeaderSkeleton(from uint64, skeleton []*types.Header) ([]*types.Header, []common.Hash, int, error) {
  1107. log.Debug("Filling up skeleton", "from", from)
  1108. d.queue.ScheduleSkeleton(from, skeleton)
  1109. err := d.concurrentFetch((*headerQueue)(d), false)
  1110. if err != nil {
  1111. log.Debug("Skeleton fill failed", "err", err)
  1112. }
  1113. filled, hashes, proced := d.queue.RetrieveHeaders()
  1114. if err == nil {
  1115. log.Debug("Skeleton fill succeeded", "filled", len(filled), "processed", proced)
  1116. }
  1117. return filled, hashes, proced, err
  1118. }
  1119. // fetchBodies iteratively downloads the scheduled block bodies, taking any
  1120. // available peers, reserving a chunk of blocks for each, waiting for delivery
  1121. // and also periodically checking for timeouts.
  1122. func (d *Downloader) fetchBodies(from uint64, beaconMode bool) error {
  1123. log.Debug("Downloading block bodies", "origin", from)
  1124. err := d.concurrentFetch((*bodyQueue)(d), beaconMode)
  1125. log.Debug("Block body download terminated", "err", err)
  1126. return err
  1127. }
  1128. // fetchReceipts iteratively downloads the scheduled block receipts, taking any
  1129. // available peers, reserving a chunk of receipts for each, waiting for delivery
  1130. // and also periodically checking for timeouts.
  1131. func (d *Downloader) fetchReceipts(from uint64, beaconMode bool) error {
  1132. log.Debug("Downloading receipts", "origin", from)
  1133. err := d.concurrentFetch((*receiptQueue)(d), beaconMode)
  1134. log.Debug("Receipt download terminated", "err", err)
  1135. return err
  1136. }
  1137. // processHeaders takes batches of retrieved headers from an input channel and
  1138. // keeps processing and scheduling them into the header chain and downloader's
  1139. // queue until the stream ends or a failure occurs.
  1140. func (d *Downloader) processHeaders(origin uint64, td, ttd *big.Int, beaconMode bool) error {
  1141. // Keep a count of uncertain headers to roll back
  1142. var (
  1143. rollback uint64 // Zero means no rollback (fine as you can't unroll the genesis)
  1144. rollbackErr error
  1145. mode = d.getMode()
  1146. )
  1147. defer func() {
  1148. if rollback > 0 {
  1149. lastHeader, lastFastBlock, lastBlock := d.lightchain.CurrentHeader().Number, common.Big0, common.Big0
  1150. if mode != LightSync {
  1151. lastFastBlock = d.blockchain.CurrentFastBlock().Number()
  1152. lastBlock = d.blockchain.CurrentBlock().Number()
  1153. }
  1154. if err := d.lightchain.SetHead(rollback - 1); err != nil { // -1 to target the parent of the first uncertain block
  1155. // We're already unwinding the stack, only print the error to make it more visible
  1156. log.Error("Failed to roll back chain segment", "head", rollback-1, "err", err)
  1157. }
  1158. curFastBlock, curBlock := common.Big0, common.Big0
  1159. if mode != LightSync {
  1160. curFastBlock = d.blockchain.CurrentFastBlock().Number()
  1161. curBlock = d.blockchain.CurrentBlock().Number()
  1162. }
  1163. log.Warn("Rolled back chain segment",
  1164. "header", fmt.Sprintf("%d->%d", lastHeader, d.lightchain.CurrentHeader().Number),
  1165. "snap", fmt.Sprintf("%d->%d", lastFastBlock, curFastBlock),
  1166. "block", fmt.Sprintf("%d->%d", lastBlock, curBlock), "reason", rollbackErr)
  1167. }
  1168. }()
  1169. // Wait for batches of headers to process
  1170. gotHeaders := false
  1171. for {
  1172. select {
  1173. case <-d.cancelCh:
  1174. rollbackErr = errCanceled
  1175. return errCanceled
  1176. case task := <-d.headerProcCh:
  1177. // Terminate header processing if we synced up
  1178. if task == nil || len(task.headers) == 0 {
  1179. // Notify everyone that headers are fully processed
  1180. for _, ch := range []chan bool{d.queue.blockWakeCh, d.queue.receiptWakeCh} {
  1181. select {
  1182. case ch <- false:
  1183. case <-d.cancelCh:
  1184. }
  1185. }
  1186. // If we're in legacy sync mode, we need to check total difficulty
  1187. // violations from malicious peers. That is not needed in beacon
  1188. // mode and we can skip to terminating sync.
  1189. if !beaconMode {
  1190. // If no headers were retrieved at all, the peer violated its TD promise that it had a
  1191. // better chain compared to ours. The only exception is if its promised blocks were
  1192. // already imported by other means (e.g. fetcher):
  1193. //
  1194. // R <remote peer>, L <local node>: Both at block 10
  1195. // R: Mine block 11, and propagate it to L
  1196. // L: Queue block 11 for import
  1197. // L: Notice that R's head and TD increased compared to ours, start sync
  1198. // L: Import of block 11 finishes
  1199. // L: Sync begins, and finds common ancestor at 11
  1200. // L: Request new headers up from 11 (R's TD was higher, it must have something)
  1201. // R: Nothing to give
  1202. if mode != LightSync {
  1203. head := d.blockchain.CurrentBlock()
  1204. if !gotHeaders && td.Cmp(d.blockchain.GetTd(head.Hash(), head.NumberU64())) > 0 {
  1205. return errStallingPeer
  1206. }
  1207. }
  1208. // If snap or light syncing, ensure promised headers are indeed delivered. This is
  1209. // needed to detect scenarios where an attacker feeds a bad pivot and then bails out
  1210. // of delivering the post-pivot blocks that would flag the invalid content.
  1211. //
  1212. // This check cannot be executed "as is" for full imports, since blocks may still be
  1213. // queued for processing when the header download completes. However, as long as the
  1214. // peer gave us something useful, we're already happy/progressed (above check).
  1215. if mode == SnapSync || mode == LightSync {
  1216. head := d.lightchain.CurrentHeader()
  1217. if td.Cmp(d.lightchain.GetTd(head.Hash(), head.Number.Uint64())) > 0 {
  1218. return errStallingPeer
  1219. }
  1220. }
  1221. }
  1222. // Disable any rollback and return
  1223. rollback = 0
  1224. return nil
  1225. }
  1226. // Otherwise split the chunk of headers into batches and process them
  1227. headers, hashes := task.headers, task.hashes
  1228. gotHeaders = true
  1229. for len(headers) > 0 {
  1230. // Terminate if something failed in between processing chunks
  1231. select {
  1232. case <-d.cancelCh:
  1233. rollbackErr = errCanceled
  1234. return errCanceled
  1235. default:
  1236. }
  1237. // Select the next chunk of headers to import
  1238. limit := maxHeadersProcess
  1239. if limit > len(headers) {
  1240. limit = len(headers)
  1241. }
  1242. chunkHeaders := headers[:limit]
  1243. chunkHashes := hashes[:limit]
  1244. // In case of header only syncing, validate the chunk immediately
  1245. if mode == SnapSync || mode == LightSync {
  1246. // If we're importing pure headers, verify based on their recentness
  1247. var pivot uint64
  1248. d.pivotLock.RLock()
  1249. if d.pivotHeader != nil {
  1250. pivot = d.pivotHeader.Number.Uint64()
  1251. }
  1252. d.pivotLock.RUnlock()
  1253. frequency := fsHeaderCheckFrequency
  1254. if chunkHeaders[len(chunkHeaders)-1].Number.Uint64()+uint64(fsHeaderForceVerify) > pivot {
  1255. frequency = 1
  1256. }
  1257. // Although the received headers might be all valid, a legacy
  1258. // PoW/PoA sync must not accept post-merge headers. Make sure
  1259. // that any transition is rejected at this point.
  1260. var (
  1261. rejected []*types.Header
  1262. td *big.Int
  1263. )
  1264. if !beaconMode && ttd != nil {
  1265. td = d.blockchain.GetTd(chunkHeaders[0].ParentHash, chunkHeaders[0].Number.Uint64()-1)
  1266. if td == nil {
  1267. // This should never really happen, but handle gracefully for now
  1268. log.Error("Failed to retrieve parent header TD", "number", chunkHeaders[0].Number.Uint64()-1, "hash", chunkHeaders[0].ParentHash)
  1269. return fmt.Errorf("%w: parent TD missing", errInvalidChain)
  1270. }
  1271. for i, header := range chunkHeaders {
  1272. td = new(big.Int).Add(td, header.Difficulty)
  1273. if td.Cmp(ttd) >= 0 {
  1274. // Terminal total difficulty reached, allow the last header in
  1275. if new(big.Int).Sub(td, header.Difficulty).Cmp(ttd) < 0 {
  1276. chunkHeaders, rejected = chunkHeaders[:i+1], chunkHeaders[i+1:]
  1277. if len(rejected) > 0 {
  1278. // Make a nicer user log as to the first TD truly rejected
  1279. td = new(big.Int).Add(td, rejected[0].Difficulty)
  1280. }
  1281. } else {
  1282. chunkHeaders, rejected = chunkHeaders[:i], chunkHeaders[i:]
  1283. }
  1284. break
  1285. }
  1286. }
  1287. }
  1288. if len(chunkHeaders) > 0 {
  1289. if n, err := d.lightchain.InsertHeaderChain(chunkHeaders, frequency); err != nil {
  1290. rollbackErr = err
  1291. // If some headers were inserted, track them as uncertain
  1292. if (mode == SnapSync || frequency > 1) && n > 0 && rollback == 0 {
  1293. rollback = chunkHeaders[0].Number.Uint64()
  1294. }
  1295. log.Warn("Invalid header encountered", "number", chunkHeaders[n].Number, "hash", chunkHashes[n], "parent", chunkHeaders[n].ParentHash, "err", err)
  1296. return fmt.Errorf("%w: %v", errInvalidChain, err)
  1297. }
  1298. // All verifications passed, track all headers within the allowed limits
  1299. if mode == SnapSync {
  1300. head := chunkHeaders[len(chunkHeaders)-1].Number.Uint64()
  1301. if head-rollback > uint64(fsHeaderSafetyNet) {
  1302. rollback = head - uint64(fsHeaderSafetyNet)
  1303. } else {
  1304. rollback = 1
  1305. }
  1306. }
  1307. }
  1308. if len(rejected) != 0 {
  1309. // Merge threshold reached, stop importing, but don't roll back
  1310. rollback = 0
  1311. log.Info("Legacy sync reached merge threshold", "number", rejected[0].Number, "hash", rejected[0].Hash(), "td", td, "ttd", ttd)
  1312. return ErrMergeTransition
  1313. }
  1314. }
  1315. // Unless we're doing light chains, schedule the headers for associated content retrieval
  1316. if mode == FullSync || mode == SnapSync {
  1317. // If we've reached the allowed number of pending headers, stall a bit
  1318. for d.queue.PendingBodies() >= maxQueuedHeaders || d.queue.PendingReceipts() >= maxQueuedHeaders {
  1319. select {
  1320. case <-d.cancelCh:
  1321. rollbackErr = errCanceled
  1322. return errCanceled
  1323. case <-time.After(time.Second):
  1324. }
  1325. }
  1326. // Otherwise insert the headers for content retrieval
  1327. inserts := d.queue.Schedule(chunkHeaders, chunkHashes, origin)
  1328. if len(inserts) != len(chunkHeaders) {
  1329. rollbackErr = fmt.Errorf("stale headers: len inserts %v len(chunk) %v", len(inserts), len(chunkHeaders))
  1330. return fmt.Errorf("%w: stale headers", errBadPeer)
  1331. }
  1332. }
  1333. headers = headers[limit:]
  1334. hashes = hashes[limit:]
  1335. origin += uint64(limit)
  1336. }
  1337. // Update the highest block number we know if a higher one is found.
  1338. d.syncStatsLock.Lock()
  1339. if d.syncStatsChainHeight < origin {
  1340. d.syncStatsChainHeight = origin - 1
  1341. }
  1342. d.syncStatsLock.Unlock()
  1343. // Signal the content downloaders of the availability of new tasks
  1344. for _, ch := range []chan bool{d.queue.blockWakeCh, d.queue.receiptWakeCh} {
  1345. select {
  1346. case ch <- true:
  1347. default:
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }
  1353. // processFullSyncContent takes fetch results from the queue and imports them into the chain.
  1354. func (d *Downloader) processFullSyncContent(ttd *big.Int, beaconMode bool) error {
  1355. for {
  1356. results := d.queue.Results(true)
  1357. if len(results) == 0 {
  1358. return nil
  1359. }
  1360. if d.chainInsertHook != nil {
  1361. d.chainInsertHook(results)
  1362. }
  1363. // Although the received blocks might be all valid, a legacy PoW/PoA sync
  1364. // must not accept post-merge blocks. Make sure that pre-merge blocks are
  1365. // imported, but post-merge ones are rejected.
  1366. var (
  1367. rejected []*fetchResult
  1368. td *big.Int
  1369. )
  1370. if !beaconMode && ttd != nil {
  1371. td = d.blockchain.GetTd(results[0].Header.ParentHash, results[0].Header.Number.Uint64()-1)
  1372. if td == nil {
  1373. // This should never really happen, but handle gracefully for now
  1374. log.Error("Failed to retrieve parent block TD", "number", results[0].Header.Number.Uint64()-1, "hash", results[0].Header.ParentHash)
  1375. return fmt.Errorf("%w: parent TD missing", errInvalidChain)
  1376. }
  1377. for i, result := range results {
  1378. td = new(big.Int).Add(td, result.Header.Difficulty)
  1379. if td.Cmp(ttd) >= 0 {
  1380. // Terminal total difficulty reached, allow the last block in
  1381. if new(big.Int).Sub(td, result.Header.Difficulty).Cmp(ttd) < 0 {
  1382. results, rejected = results[:i+1], results[i+1:]
  1383. if len(rejected) > 0 {
  1384. // Make a nicer user log as to the first TD truly rejected
  1385. td = new(big.Int).Add(td, rejected[0].Header.Difficulty)
  1386. }
  1387. } else {
  1388. results, rejected = results[:i], results[i:]
  1389. }
  1390. break
  1391. }
  1392. }
  1393. }
  1394. if err := d.importBlockResults(results); err != nil {
  1395. return err
  1396. }
  1397. if len(rejected) != 0 {
  1398. log.Info("Legacy sync reached merge threshold", "number", rejected[0].Header.Number, "hash", rejected[0].Header.Hash(), "td", td, "ttd", ttd)
  1399. return ErrMergeTransition
  1400. }
  1401. }
  1402. }
  1403. func (d *Downloader) importBlockResults(results []*fetchResult) error {
  1404. // Check for any early termination requests
  1405. if len(results) == 0 {
  1406. return nil
  1407. }
  1408. select {
  1409. case <-d.quitCh:
  1410. return errCancelContentProcessing
  1411. default:
  1412. }
  1413. // Retrieve a batch of results to import
  1414. first, last := results[0].Header, results[len(results)-1].Header
  1415. log.Debug("Inserting downloaded chain", "items", len(results),
  1416. "firstnum", first.Number, "firsthash", first.Hash(),
  1417. "lastnum", last.Number, "lasthash", last.Hash(),
  1418. )
  1419. blocks := make([]*types.Block, len(results))
  1420. for i, result := range results {
  1421. blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1422. }
  1423. // Downloaded blocks are always regarded as trusted after the
  1424. // transition. Because the downloaded chain is guided by the
  1425. // consensus-layer.
  1426. if index, err := d.blockchain.InsertChain(blocks); err != nil {
  1427. if index < len(results) {
  1428. log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
  1429. // In post-merge, notify the engine API of encountered bad chains
  1430. if d.badBlock != nil {
  1431. head, _, err := d.skeleton.Bounds()
  1432. if err != nil {
  1433. log.Error("Failed to retrieve beacon bounds for bad block reporting", "err", err)
  1434. } else {
  1435. d.badBlock(blocks[index].Header(), head)
  1436. }
  1437. }
  1438. } else {
  1439. // The InsertChain method in blockchain.go will sometimes return an out-of-bounds index,
  1440. // when it needs to preprocess blocks to import a sidechain.
  1441. // The importer will put together a new list of blocks to import, which is a superset
  1442. // of the blocks delivered from the downloader, and the indexing will be off.
  1443. log.Debug("Downloaded item processing failed on sidechain import", "index", index, "err", err)
  1444. }
  1445. return fmt.Errorf("%w: %v", errInvalidChain, err)
  1446. }
  1447. return nil
  1448. }
  1449. // processSnapSyncContent takes fetch results from the queue and writes them to the
  1450. // database. It also controls the synchronisation of state nodes of the pivot block.
  1451. func (d *Downloader) processSnapSyncContent() error {
  1452. // Start syncing state of the reported head block. This should get us most of
  1453. // the state of the pivot block.
  1454. d.pivotLock.RLock()
  1455. sync := d.syncState(d.pivotHeader.Root)
  1456. d.pivotLock.RUnlock()
  1457. defer func() {
  1458. // The `sync` object is replaced every time the pivot moves. We need to
  1459. // defer close the very last active one, hence the lazy evaluation vs.
  1460. // calling defer sync.Cancel() !!!
  1461. sync.Cancel()
  1462. }()
  1463. closeOnErr := func(s *stateSync) {
  1464. if err := s.Wait(); err != nil && err != errCancelStateFetch && err != errCanceled && err != snap.ErrCancelled {
  1465. d.queue.Close() // wake up Results
  1466. }
  1467. }
  1468. go closeOnErr(sync)
  1469. // To cater for moving pivot points, track the pivot block and subsequently
  1470. // accumulated download results separately.
  1471. var (
  1472. oldPivot *fetchResult // Locked in pivot block, might change eventually
  1473. oldTail []*fetchResult // Downloaded content after the pivot
  1474. )
  1475. for {
  1476. // Wait for the next batch of downloaded data to be available, and if the pivot
  1477. // block became stale, move the goalpost
  1478. results := d.queue.Results(oldPivot == nil) // Block if we're not monitoring pivot staleness
  1479. if len(results) == 0 {
  1480. // If pivot sync is done, stop
  1481. if oldPivot == nil {
  1482. return sync.Cancel()
  1483. }
  1484. // If sync failed, stop
  1485. select {
  1486. case <-d.cancelCh:
  1487. sync.Cancel()
  1488. return errCanceled
  1489. default:
  1490. }
  1491. }
  1492. if d.chainInsertHook != nil {
  1493. d.chainInsertHook(results)
  1494. }
  1495. // If we haven't downloaded the pivot block yet, check pivot staleness
  1496. // notifications from the header downloader
  1497. d.pivotLock.RLock()
  1498. pivot := d.pivotHeader
  1499. d.pivotLock.RUnlock()
  1500. if oldPivot == nil {
  1501. if pivot.Root != sync.root {
  1502. sync.Cancel()
  1503. sync = d.syncState(pivot.Root)
  1504. go closeOnErr(sync)
  1505. }
  1506. } else {
  1507. results = append(append([]*fetchResult{oldPivot}, oldTail...), results...)
  1508. }
  1509. // Split around the pivot block and process the two sides via snap/full sync
  1510. if atomic.LoadInt32(&d.committed) == 0 {
  1511. latest := results[len(results)-1].Header
  1512. // If the height is above the pivot block by 2 sets, it means the pivot
  1513. // become stale in the network and it was garbage collected, move to a
  1514. // new pivot.
  1515. //
  1516. // Note, we have `reorgProtHeaderDelay` number of blocks withheld, Those
  1517. // need to be taken into account, otherwise we're detecting the pivot move
  1518. // late and will drop peers due to unavailable state!!!
  1519. if height := latest.Number.Uint64(); height >= pivot.Number.Uint64()+2*uint64(fsMinFullBlocks)-uint64(reorgProtHeaderDelay) {
  1520. log.Warn("Pivot became stale, moving", "old", pivot.Number.Uint64(), "new", height-uint64(fsMinFullBlocks)+uint64(reorgProtHeaderDelay))
  1521. pivot = results[len(results)-1-fsMinFullBlocks+reorgProtHeaderDelay].Header // must exist as lower old pivot is uncommitted
  1522. d.pivotLock.Lock()
  1523. d.pivotHeader = pivot
  1524. d.pivotLock.Unlock()
  1525. // Write out the pivot into the database so a rollback beyond it will
  1526. // reenable snap sync
  1527. rawdb.WriteLastPivotNumber(d.stateDB, pivot.Number.Uint64())
  1528. }
  1529. }
  1530. P, beforeP, afterP := splitAroundPivot(pivot.Number.Uint64(), results)
  1531. if err := d.commitSnapSyncData(beforeP, sync); err != nil {
  1532. return err
  1533. }
  1534. if P != nil {
  1535. // If new pivot block found, cancel old state retrieval and restart
  1536. if oldPivot != P {
  1537. sync.Cancel()
  1538. sync = d.syncState(P.Header.Root)
  1539. go closeOnErr(sync)
  1540. oldPivot = P
  1541. }
  1542. // Wait for completion, occasionally checking for pivot staleness
  1543. select {
  1544. case <-sync.done:
  1545. if sync.err != nil {
  1546. return sync.err
  1547. }
  1548. if err := d.commitPivotBlock(P); err != nil {
  1549. return err
  1550. }
  1551. oldPivot = nil
  1552. case <-time.After(time.Second):
  1553. oldTail = afterP
  1554. continue
  1555. }
  1556. }
  1557. // Fast sync done, pivot commit done, full import
  1558. if err := d.importBlockResults(afterP); err != nil {
  1559. return err
  1560. }
  1561. }
  1562. }
  1563. func splitAroundPivot(pivot uint64, results []*fetchResult) (p *fetchResult, before, after []*fetchResult) {
  1564. if len(results) == 0 {
  1565. return nil, nil, nil
  1566. }
  1567. if lastNum := results[len(results)-1].Header.Number.Uint64(); lastNum < pivot {
  1568. // the pivot is somewhere in the future
  1569. return nil, results, nil
  1570. }
  1571. // This can also be optimized, but only happens very seldom
  1572. for _, result := range results {
  1573. num := result.Header.Number.Uint64()
  1574. switch {
  1575. case num < pivot:
  1576. before = append(before, result)
  1577. case num == pivot:
  1578. p = result
  1579. default:
  1580. after = append(after, result)
  1581. }
  1582. }
  1583. return p, before, after
  1584. }
  1585. func (d *Downloader) commitSnapSyncData(results []*fetchResult, stateSync *stateSync) error {
  1586. // Check for any early termination requests
  1587. if len(results) == 0 {
  1588. return nil
  1589. }
  1590. select {
  1591. case <-d.quitCh:
  1592. return errCancelContentProcessing
  1593. case <-stateSync.done:
  1594. if err := stateSync.Wait(); err != nil {
  1595. return err
  1596. }
  1597. default:
  1598. }
  1599. // Retrieve the a batch of results to import
  1600. first, last := results[0].Header, results[len(results)-1].Header
  1601. log.Debug("Inserting snap-sync blocks", "items", len(results),
  1602. "firstnum", first.Number, "firsthash", first.Hash(),
  1603. "lastnumn", last.Number, "lasthash", last.Hash(),
  1604. )
  1605. blocks := make([]*types.Block, len(results))
  1606. receipts := make([]types.Receipts, len(results))
  1607. for i, result := range results {
  1608. blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1609. receipts[i] = result.Receipts
  1610. }
  1611. if index, err := d.blockchain.InsertReceiptChain(blocks, receipts, d.ancientLimit); err != nil {
  1612. log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
  1613. return fmt.Errorf("%w: %v", errInvalidChain, err)
  1614. }
  1615. return nil
  1616. }
  1617. func (d *Downloader) commitPivotBlock(result *fetchResult) error {
  1618. block := types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
  1619. log.Debug("Committing snap sync pivot as new head", "number", block.Number(), "hash", block.Hash())
  1620. // Commit the pivot block as the new head, will require full sync from here on
  1621. if _, err := d.blockchain.InsertReceiptChain([]*types.Block{block}, []types.Receipts{result.Receipts}, d.ancientLimit); err != nil {
  1622. return err
  1623. }
  1624. if err := d.blockchain.SnapSyncCommitHead(block.Hash()); err != nil {
  1625. return err
  1626. }
  1627. atomic.StoreInt32(&d.committed, 1)
  1628. return nil
  1629. }
  1630. // DeliverSnapPacket is invoked from a peer's message handler when it transmits a
  1631. // data packet for the local node to consume.
  1632. func (d *Downloader) DeliverSnapPacket(peer *snap.Peer, packet snap.Packet) error {
  1633. switch packet := packet.(type) {
  1634. case *snap.AccountRangePacket:
  1635. hashes, accounts, err := packet.Unpack()
  1636. if err != nil {
  1637. return err
  1638. }
  1639. return d.SnapSyncer.OnAccounts(peer, packet.ID, hashes, accounts, packet.Proof)
  1640. case *snap.StorageRangesPacket:
  1641. hashset, slotset := packet.Unpack()
  1642. return d.SnapSyncer.OnStorage(peer, packet.ID, hashset, slotset, packet.Proof)
  1643. case *snap.ByteCodesPacket:
  1644. return d.SnapSyncer.OnByteCodes(peer, packet.ID, packet.Codes)
  1645. case *snap.TrieNodesPacket:
  1646. return d.SnapSyncer.OnTrieNodes(peer, packet.ID, packet.Nodes)
  1647. default:
  1648. return fmt.Errorf("unexpected snap packet type: %T", packet)
  1649. }
  1650. }
  1651. // readHeaderRange returns a list of headers, using the given last header as the base,
  1652. // and going backwards towards genesis. This method assumes that the caller already has
  1653. // placed a reasonable cap on count.
  1654. func (d *Downloader) readHeaderRange(last *types.Header, count int) []*types.Header {
  1655. var (
  1656. current = last
  1657. headers []*types.Header
  1658. )
  1659. for {
  1660. parent := d.lightchain.GetHeaderByHash(current.ParentHash)
  1661. if parent == nil {
  1662. break // The chain is not continuous, or the chain is exhausted
  1663. }
  1664. headers = append(headers, parent)
  1665. if len(headers) >= count {
  1666. break
  1667. }
  1668. current = parent
  1669. }
  1670. return headers
  1671. }