github.com/daethereum/go-dae@v2.2.3+incompatible/core/blockchain.go (about) 1 // Copyright 2014 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 core implements the Ethereum consensus protocol. 18 package core 19 20 import ( 21 "errors" 22 "fmt" 23 "io" 24 "math/big" 25 "sort" 26 "sync" 27 "sync/atomic" 28 "time" 29 30 "github.com/daethereum/go-dae/common" 31 "github.com/daethereum/go-dae/common/mclock" 32 "github.com/daethereum/go-dae/common/prque" 33 "github.com/daethereum/go-dae/consensus" 34 "github.com/daethereum/go-dae/core/rawdb" 35 "github.com/daethereum/go-dae/core/state" 36 "github.com/daethereum/go-dae/core/state/snapshot" 37 "github.com/daethereum/go-dae/core/types" 38 "github.com/daethereum/go-dae/core/vm" 39 "github.com/daethereum/go-dae/ethdb" 40 "github.com/daethereum/go-dae/event" 41 "github.com/daethereum/go-dae/internal/syncx" 42 "github.com/daethereum/go-dae/log" 43 "github.com/daethereum/go-dae/metrics" 44 "github.com/daethereum/go-dae/params" 45 "github.com/daethereum/go-dae/trie" 46 lru "github.com/hashicorp/golang-lru" 47 ) 48 49 var ( 50 headBlockGauge = metrics.NewRegisteredGauge("chain/head/block", nil) 51 headHeaderGauge = metrics.NewRegisteredGauge("chain/head/header", nil) 52 headFastBlockGauge = metrics.NewRegisteredGauge("chain/head/receipt", nil) 53 headFinalizedBlockGauge = metrics.NewRegisteredGauge("chain/head/finalized", nil) 54 headSafeBlockGauge = metrics.NewRegisteredGauge("chain/head/safe", nil) 55 56 accountReadTimer = metrics.NewRegisteredTimer("chain/account/reads", nil) 57 accountHashTimer = metrics.NewRegisteredTimer("chain/account/hashes", nil) 58 accountUpdateTimer = metrics.NewRegisteredTimer("chain/account/updates", nil) 59 accountCommitTimer = metrics.NewRegisteredTimer("chain/account/commits", nil) 60 61 storageReadTimer = metrics.NewRegisteredTimer("chain/storage/reads", nil) 62 storageHashTimer = metrics.NewRegisteredTimer("chain/storage/hashes", nil) 63 storageUpdateTimer = metrics.NewRegisteredTimer("chain/storage/updates", nil) 64 storageCommitTimer = metrics.NewRegisteredTimer("chain/storage/commits", nil) 65 66 snapshotAccountReadTimer = metrics.NewRegisteredTimer("chain/snapshot/account/reads", nil) 67 snapshotStorageReadTimer = metrics.NewRegisteredTimer("chain/snapshot/storage/reads", nil) 68 snapshotCommitTimer = metrics.NewRegisteredTimer("chain/snapshot/commits", nil) 69 70 blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil) 71 blockValidationTimer = metrics.NewRegisteredTimer("chain/validation", nil) 72 blockExecutionTimer = metrics.NewRegisteredTimer("chain/execution", nil) 73 blockWriteTimer = metrics.NewRegisteredTimer("chain/write", nil) 74 75 blockReorgMeter = metrics.NewRegisteredMeter("chain/reorg/executes", nil) 76 blockReorgAddMeter = metrics.NewRegisteredMeter("chain/reorg/add", nil) 77 blockReorgDropMeter = metrics.NewRegisteredMeter("chain/reorg/drop", nil) 78 blockReorgInvalidatedTx = metrics.NewRegisteredMeter("chain/reorg/invalidTx", nil) 79 80 blockPrefetchExecuteTimer = metrics.NewRegisteredTimer("chain/prefetch/executes", nil) 81 blockPrefetchInterruptMeter = metrics.NewRegisteredMeter("chain/prefetch/interrupts", nil) 82 83 errInsertionInterrupted = errors.New("insertion is interrupted") 84 errChainStopped = errors.New("blockchain is stopped") 85 ) 86 87 const ( 88 bodyCacheLimit = 256 89 blockCacheLimit = 256 90 receiptsCacheLimit = 32 91 txLookupCacheLimit = 1024 92 maxFutureBlocks = 256 93 maxTimeFutureBlocks = 30 94 TriesInMemory = 128 95 96 // BlockChainVersion ensures that an incompatible database forces a resync from scratch. 97 // 98 // Changelog: 99 // 100 // - Version 4 101 // The following incompatible database changes were added: 102 // * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted 103 // * the `Bloom` field of receipt is deleted 104 // * the `BlockIndex` and `TxIndex` fields of txlookup are deleted 105 // - Version 5 106 // The following incompatible database changes were added: 107 // * the `TxHash`, `GasCost`, and `ContractAddress` fields are no longer stored for a receipt 108 // * the `TxHash`, `GasCost`, and `ContractAddress` fields are computed by looking up the 109 // receipts' corresponding block 110 // - Version 6 111 // The following incompatible database changes were added: 112 // * Transaction lookup information stores the corresponding block number instead of block hash 113 // - Version 7 114 // The following incompatible database changes were added: 115 // * Use freezer as the ancient database to maintain all ancient data 116 // - Version 8 117 // The following incompatible database changes were added: 118 // * New scheme for contract code in order to separate the codes and trie nodes 119 BlockChainVersion uint64 = 8 120 ) 121 122 // CacheConfig contains the configuration values for the trie caching/pruning 123 // that's resident in a blockchain. 124 type CacheConfig struct { 125 TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory 126 TrieCleanJournal string // Disk journal for saving clean cache entries. 127 TrieCleanRejournal time.Duration // Time interval to dump clean cache to disk periodically 128 TrieCleanNoPrefetch bool // Whether to disable heuristic state prefetching for followup blocks 129 TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk 130 TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node) 131 TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk 132 SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory 133 Preimages bool // Whether to store preimage of trie key to the disk 134 135 SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it 136 } 137 138 // defaultCacheConfig are the default caching values if none are specified by the 139 // user (also used during testing). 140 var defaultCacheConfig = &CacheConfig{ 141 TrieCleanLimit: 256, 142 TrieDirtyLimit: 256, 143 TrieTimeLimit: 5 * time.Minute, 144 SnapshotLimit: 256, 145 SnapshotWait: true, 146 } 147 148 // BlockChain represents the canonical chain given a database with a genesis 149 // block. The Blockchain manages chain imports, reverts, chain reorganisations. 150 // 151 // Importing blocks in to the block chain happens according to the set of rules 152 // defined by the two stage Validator. Processing of blocks is done using the 153 // Processor which processes the included transaction. The validation of the state 154 // is done in the second part of the Validator. Failing results in aborting of 155 // the import. 156 // 157 // The BlockChain also helps in returning blocks from **any** chain included 158 // in the database as well as blocks that represents the canonical chain. It's 159 // important to note that GetBlock can return any block and does not need to be 160 // included in the canonical one where as GetBlockByNumber always represents the 161 // canonical chain. 162 type BlockChain struct { 163 chainConfig *params.ChainConfig // Chain & network configuration 164 cacheConfig *CacheConfig // Cache configuration for pruning 165 166 db ethdb.Database // Low level persistent database to store final content in 167 snaps *snapshot.Tree // Snapshot tree for fast trie leaf access 168 triegc *prque.Prque // Priority queue mapping block numbers to tries to gc 169 gcproc time.Duration // Accumulates canonical block processing for trie dumping 170 171 // txLookupLimit is the maximum number of blocks from head whose tx indices 172 // are reserved: 173 // * 0: means no limit and regenerate any missing indexes 174 // * N: means N block limit [HEAD-N+1, HEAD] and delete extra indexes 175 // * nil: disable tx reindexer/deleter, but still index new blocks 176 txLookupLimit uint64 177 178 hc *HeaderChain 179 rmLogsFeed event.Feed 180 chainFeed event.Feed 181 chainSideFeed event.Feed 182 chainHeadFeed event.Feed 183 logsFeed event.Feed 184 blockProcFeed event.Feed 185 scope event.SubscriptionScope 186 genesisBlock *types.Block 187 188 // This mutex synchronizes chain write operations. 189 // Readers don't need to take it, they can just read the database. 190 chainmu *syncx.ClosableMutex 191 192 currentBlock atomic.Value // Current head of the block chain 193 currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!) 194 currentFinalizedBlock atomic.Value // Current finalized head 195 currentSafeBlock atomic.Value // Current safe head 196 197 stateCache state.Database // State database to reuse between imports (contains state cache) 198 bodyCache *lru.Cache // Cache for the most recent block bodies 199 bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format 200 receiptsCache *lru.Cache // Cache for the most recent receipts per block 201 blockCache *lru.Cache // Cache for the most recent entire blocks 202 txLookupCache *lru.Cache // Cache for the most recent transaction lookup data. 203 futureBlocks *lru.Cache // future blocks are blocks added for later processing 204 205 wg sync.WaitGroup // 206 quit chan struct{} // shutdown signal, closed in Stop. 207 running int32 // 0 if chain is running, 1 when stopped 208 procInterrupt int32 // interrupt signaler for block processing 209 210 engine consensus.Engine 211 validator Validator // Block and state validator interface 212 prefetcher Prefetcher 213 processor Processor // Block transaction processor interface 214 forker *ForkChoice 215 vmConfig vm.Config 216 } 217 218 // NewBlockChain returns a fully initialised block chain using information 219 // available in the database. It initialises the default Ethereum Validator 220 // and Processor. 221 func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config, shouldPreserve func(header *types.Header) bool, txLookupLimit *uint64) (*BlockChain, error) { 222 if cacheConfig == nil { 223 cacheConfig = defaultCacheConfig 224 } 225 bodyCache, _ := lru.New(bodyCacheLimit) 226 bodyRLPCache, _ := lru.New(bodyCacheLimit) 227 receiptsCache, _ := lru.New(receiptsCacheLimit) 228 blockCache, _ := lru.New(blockCacheLimit) 229 txLookupCache, _ := lru.New(txLookupCacheLimit) 230 futureBlocks, _ := lru.New(maxFutureBlocks) 231 232 bc := &BlockChain{ 233 chainConfig: chainConfig, 234 cacheConfig: cacheConfig, 235 db: db, 236 triegc: prque.New(nil), 237 stateCache: state.NewDatabaseWithConfig(db, &trie.Config{ 238 Cache: cacheConfig.TrieCleanLimit, 239 Journal: cacheConfig.TrieCleanJournal, 240 Preimages: cacheConfig.Preimages, 241 }), 242 quit: make(chan struct{}), 243 chainmu: syncx.NewClosableMutex(), 244 bodyCache: bodyCache, 245 bodyRLPCache: bodyRLPCache, 246 receiptsCache: receiptsCache, 247 blockCache: blockCache, 248 txLookupCache: txLookupCache, 249 futureBlocks: futureBlocks, 250 engine: engine, 251 vmConfig: vmConfig, 252 } 253 bc.forker = NewForkChoice(bc, shouldPreserve) 254 bc.validator = NewBlockValidator(chainConfig, bc, engine) 255 bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine) 256 bc.processor = NewStateProcessor(chainConfig, bc, engine) 257 258 var err error 259 bc.hc, err = NewHeaderChain(db, chainConfig, engine, bc.insertStopped) 260 if err != nil { 261 return nil, err 262 } 263 bc.genesisBlock = bc.GetBlockByNumber(0) 264 if bc.genesisBlock == nil { 265 return nil, ErrNoGenesis 266 } 267 268 var nilBlock *types.Block 269 bc.currentBlock.Store(nilBlock) 270 bc.currentFastBlock.Store(nilBlock) 271 bc.currentFinalizedBlock.Store(nilBlock) 272 bc.currentSafeBlock.Store(nilBlock) 273 274 // Initialize the chain with ancient data if it isn't empty. 275 var txIndexBlock uint64 276 277 if bc.empty() { 278 rawdb.InitDatabaseFromFreezer(bc.db) 279 // If ancient database is not empty, reconstruct all missing 280 // indices in the background. 281 frozen, _ := bc.db.Ancients() 282 if frozen > 0 { 283 txIndexBlock = frozen 284 } 285 } 286 if err := bc.loadLastState(); err != nil { 287 return nil, err 288 } 289 290 // Make sure the state associated with the block is available 291 head := bc.CurrentBlock() 292 if _, err := state.New(head.Root(), bc.stateCache, bc.snaps); err != nil { 293 // Head state is missing, before the state recovery, find out the 294 // disk layer point of snapshot(if it's enabled). Make sure the 295 // rewound point is lower than disk layer. 296 var diskRoot common.Hash 297 if bc.cacheConfig.SnapshotLimit > 0 { 298 diskRoot = rawdb.ReadSnapshotRoot(bc.db) 299 } 300 if diskRoot != (common.Hash{}) { 301 log.Warn("Head state missing, repairing", "number", head.Number(), "hash", head.Hash(), "snaproot", diskRoot) 302 303 snapDisk, err := bc.setHeadBeyondRoot(head.NumberU64(), diskRoot, true) 304 if err != nil { 305 return nil, err 306 } 307 // Chain rewound, persist old snapshot number to indicate recovery procedure 308 if snapDisk != 0 { 309 rawdb.WriteSnapshotRecoveryNumber(bc.db, snapDisk) 310 } 311 } else { 312 log.Warn("Head state missing, repairing", "number", head.Number(), "hash", head.Hash()) 313 if _, err := bc.setHeadBeyondRoot(head.NumberU64(), common.Hash{}, true); err != nil { 314 return nil, err 315 } 316 } 317 } 318 319 // Ensure that a previous crash in SetHead doesn't leave extra ancients 320 if frozen, err := bc.db.Ancients(); err == nil && frozen > 0 { 321 var ( 322 needRewind bool 323 low uint64 324 ) 325 // The head full block may be rolled back to a very low height due to 326 // blockchain repair. If the head full block is even lower than the ancient 327 // chain, truncate the ancient store. 328 fullBlock := bc.CurrentBlock() 329 if fullBlock != nil && fullBlock.Hash() != bc.genesisBlock.Hash() && fullBlock.NumberU64() < frozen-1 { 330 needRewind = true 331 low = fullBlock.NumberU64() 332 } 333 // In fast sync, it may happen that ancient data has been written to the 334 // ancient store, but the LastFastBlock has not been updated, truncate the 335 // extra data here. 336 fastBlock := bc.CurrentFastBlock() 337 if fastBlock != nil && fastBlock.NumberU64() < frozen-1 { 338 needRewind = true 339 if fastBlock.NumberU64() < low || low == 0 { 340 low = fastBlock.NumberU64() 341 } 342 } 343 if needRewind { 344 log.Error("Truncating ancient chain", "from", bc.CurrentHeader().Number.Uint64(), "to", low) 345 if err := bc.SetHead(low); err != nil { 346 return nil, err 347 } 348 } 349 } 350 // The first thing the node will do is reconstruct the verification data for 351 // the head block (ethash cache or clique voting snapshot). Might as well do 352 // it in advance. 353 bc.engine.VerifyHeader(bc, bc.CurrentHeader(), true) 354 355 // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain 356 for hash := range BadHashes { 357 if header := bc.GetHeaderByHash(hash); header != nil { 358 // get the canonical block corresponding to the offending header's number 359 headerByNumber := bc.GetHeaderByNumber(header.Number.Uint64()) 360 // make sure the headerByNumber (if present) is in our current canonical chain 361 if headerByNumber != nil && headerByNumber.Hash() == header.Hash() { 362 log.Error("Found bad hash, rewinding chain", "number", header.Number, "hash", header.ParentHash) 363 if err := bc.SetHead(header.Number.Uint64() - 1); err != nil { 364 return nil, err 365 } 366 log.Error("Chain rewind was successful, resuming normal operation") 367 } 368 } 369 } 370 371 // Load any existing snapshot, regenerating it if loading failed 372 if bc.cacheConfig.SnapshotLimit > 0 { 373 // If the chain was rewound past the snapshot persistent layer (causing 374 // a recovery block number to be persisted to disk), check if we're still 375 // in recovery mode and in that case, don't invalidate the snapshot on a 376 // head mismatch. 377 var recover bool 378 379 head := bc.CurrentBlock() 380 if layer := rawdb.ReadSnapshotRecoveryNumber(bc.db); layer != nil && *layer > head.NumberU64() { 381 log.Warn("Enabling snapshot recovery", "chainhead", head.NumberU64(), "diskbase", *layer) 382 recover = true 383 } 384 bc.snaps, _ = snapshot.New(bc.db, bc.stateCache.TrieDB(), bc.cacheConfig.SnapshotLimit, head.Root(), !bc.cacheConfig.SnapshotWait, true, recover) 385 } 386 387 // Start future block processor. 388 bc.wg.Add(1) 389 go bc.updateFutureBlocks() 390 391 // Start tx indexer/unindexer. 392 if txLookupLimit != nil { 393 bc.txLookupLimit = *txLookupLimit 394 395 bc.wg.Add(1) 396 go bc.maintainTxIndex(txIndexBlock) 397 } 398 399 // If periodic cache journal is required, spin it up. 400 if bc.cacheConfig.TrieCleanRejournal > 0 { 401 if bc.cacheConfig.TrieCleanRejournal < time.Minute { 402 log.Warn("Sanitizing invalid trie cache journal time", "provided", bc.cacheConfig.TrieCleanRejournal, "updated", time.Minute) 403 bc.cacheConfig.TrieCleanRejournal = time.Minute 404 } 405 triedb := bc.stateCache.TrieDB() 406 bc.wg.Add(1) 407 go func() { 408 defer bc.wg.Done() 409 triedb.SaveCachePeriodically(bc.cacheConfig.TrieCleanJournal, bc.cacheConfig.TrieCleanRejournal, bc.quit) 410 }() 411 } 412 return bc, nil 413 } 414 415 // empty returns an indicator whether the blockchain is empty. 416 // Note, it's a special case that we connect a non-empty ancient 417 // database with an empty node, so that we can plugin the ancient 418 // into node seamlessly. 419 func (bc *BlockChain) empty() bool { 420 genesis := bc.genesisBlock.Hash() 421 for _, hash := range []common.Hash{rawdb.ReadHeadBlockHash(bc.db), rawdb.ReadHeadHeaderHash(bc.db), rawdb.ReadHeadFastBlockHash(bc.db)} { 422 if hash != genesis { 423 return false 424 } 425 } 426 return true 427 } 428 429 // loadLastState loads the last known chain state from the database. This method 430 // assumes that the chain manager mutex is held. 431 func (bc *BlockChain) loadLastState() error { 432 // Restore the last known head block 433 head := rawdb.ReadHeadBlockHash(bc.db) 434 if head == (common.Hash{}) { 435 // Corrupt or empty database, init from scratch 436 log.Warn("Empty database, resetting chain") 437 return bc.Reset() 438 } 439 // Make sure the entire head block is available 440 currentBlock := bc.GetBlockByHash(head) 441 if currentBlock == nil { 442 // Corrupt or empty database, init from scratch 443 log.Warn("Head block missing, resetting chain", "hash", head) 444 return bc.Reset() 445 } 446 // Everything seems to be fine, set as the head block 447 bc.currentBlock.Store(currentBlock) 448 headBlockGauge.Update(int64(currentBlock.NumberU64())) 449 450 // Restore the last known head header 451 currentHeader := currentBlock.Header() 452 if head := rawdb.ReadHeadHeaderHash(bc.db); head != (common.Hash{}) { 453 if header := bc.GetHeaderByHash(head); header != nil { 454 currentHeader = header 455 } 456 } 457 bc.hc.SetCurrentHeader(currentHeader) 458 459 // Restore the last known head fast block 460 bc.currentFastBlock.Store(currentBlock) 461 headFastBlockGauge.Update(int64(currentBlock.NumberU64())) 462 463 if head := rawdb.ReadHeadFastBlockHash(bc.db); head != (common.Hash{}) { 464 if block := bc.GetBlockByHash(head); block != nil { 465 bc.currentFastBlock.Store(block) 466 headFastBlockGauge.Update(int64(block.NumberU64())) 467 } 468 } 469 470 // Restore the last known finalized block and safe block 471 // Note: the safe block is not stored on disk and it is set to the last 472 // known finalized block on startup 473 if head := rawdb.ReadFinalizedBlockHash(bc.db); head != (common.Hash{}) { 474 if block := bc.GetBlockByHash(head); block != nil { 475 bc.currentFinalizedBlock.Store(block) 476 headFinalizedBlockGauge.Update(int64(block.NumberU64())) 477 bc.currentSafeBlock.Store(block) 478 headSafeBlockGauge.Update(int64(block.NumberU64())) 479 } 480 } 481 // Issue a status log for the user 482 currentFastBlock := bc.CurrentFastBlock() 483 currentFinalizedBlock := bc.CurrentFinalizedBlock() 484 485 headerTd := bc.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64()) 486 blockTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) 487 fastTd := bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64()) 488 489 log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(currentHeader.Time), 0))) 490 log.Info("Loaded most recent local full block", "number", currentBlock.Number(), "hash", currentBlock.Hash(), "td", blockTd, "age", common.PrettyAge(time.Unix(int64(currentBlock.Time()), 0))) 491 log.Info("Loaded most recent local fast block", "number", currentFastBlock.Number(), "hash", currentFastBlock.Hash(), "td", fastTd, "age", common.PrettyAge(time.Unix(int64(currentFastBlock.Time()), 0))) 492 493 if currentFinalizedBlock != nil { 494 finalTd := bc.GetTd(currentFinalizedBlock.Hash(), currentFinalizedBlock.NumberU64()) 495 log.Info("Loaded most recent local finalized block", "number", currentFinalizedBlock.Number(), "hash", currentFinalizedBlock.Hash(), "td", finalTd, "age", common.PrettyAge(time.Unix(int64(currentFinalizedBlock.Time()), 0))) 496 } 497 if pivot := rawdb.ReadLastPivotNumber(bc.db); pivot != nil { 498 log.Info("Loaded last fast-sync pivot marker", "number", *pivot) 499 } 500 return nil 501 } 502 503 // SetHead rewinds the local chain to a new head. Depending on whether the node 504 // was fast synced or full synced and in which state, the method will try to 505 // delete minimal data from disk whilst retaining chain consistency. 506 func (bc *BlockChain) SetHead(head uint64) error { 507 _, err := bc.setHeadBeyondRoot(head, common.Hash{}, false) 508 return err 509 } 510 511 // SetFinalized sets the finalized block. 512 func (bc *BlockChain) SetFinalized(block *types.Block) { 513 bc.currentFinalizedBlock.Store(block) 514 if block != nil { 515 rawdb.WriteFinalizedBlockHash(bc.db, block.Hash()) 516 headFinalizedBlockGauge.Update(int64(block.NumberU64())) 517 } else { 518 rawdb.WriteFinalizedBlockHash(bc.db, common.Hash{}) 519 headFinalizedBlockGauge.Update(0) 520 } 521 } 522 523 // SetSafe sets the safe block. 524 func (bc *BlockChain) SetSafe(block *types.Block) { 525 bc.currentSafeBlock.Store(block) 526 if block != nil { 527 headSafeBlockGauge.Update(int64(block.NumberU64())) 528 } else { 529 headSafeBlockGauge.Update(0) 530 } 531 } 532 533 // setHeadBeyondRoot rewinds the local chain to a new head with the extra condition 534 // that the rewind must pass the specified state root. This method is meant to be 535 // used when rewinding with snapshots enabled to ensure that we go back further than 536 // persistent disk layer. Depending on whether the node was fast synced or full, and 537 // in which state, the method will try to delete minimal data from disk whilst 538 // retaining chain consistency. 539 // 540 // The method returns the block number where the requested root cap was found. 541 func (bc *BlockChain) setHeadBeyondRoot(head uint64, root common.Hash, repair bool) (uint64, error) { 542 if !bc.chainmu.TryLock() { 543 return 0, errChainStopped 544 } 545 defer bc.chainmu.Unlock() 546 547 // Track the block number of the requested root hash 548 var rootNumber uint64 // (no root == always 0) 549 550 // Retrieve the last pivot block to short circuit rollbacks beyond it and the 551 // current freezer limit to start nuking id underflown 552 pivot := rawdb.ReadLastPivotNumber(bc.db) 553 frozen, _ := bc.db.Ancients() 554 555 updateFn := func(db ethdb.KeyValueWriter, header *types.Header) (uint64, bool) { 556 // Rewind the blockchain, ensuring we don't end up with a stateless head 557 // block. Note, depth equality is permitted to allow using SetHead as a 558 // chain reparation mechanism without deleting any data! 559 if currentBlock := bc.CurrentBlock(); currentBlock != nil && header.Number.Uint64() <= currentBlock.NumberU64() { 560 newHeadBlock := bc.GetBlock(header.Hash(), header.Number.Uint64()) 561 if newHeadBlock == nil { 562 log.Error("Gap in the chain, rewinding to genesis", "number", header.Number, "hash", header.Hash()) 563 newHeadBlock = bc.genesisBlock 564 } else { 565 // Block exists, keep rewinding until we find one with state, 566 // keeping rewinding until we exceed the optional threshold 567 // root hash 568 beyondRoot := (root == common.Hash{}) // Flag whether we're beyond the requested root (no root, always true) 569 570 for { 571 // If a root threshold was requested but not yet crossed, check 572 if root != (common.Hash{}) && !beyondRoot && newHeadBlock.Root() == root { 573 beyondRoot, rootNumber = true, newHeadBlock.NumberU64() 574 } 575 if _, err := state.New(newHeadBlock.Root(), bc.stateCache, bc.snaps); err != nil { 576 log.Trace("Block state missing, rewinding further", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash()) 577 if pivot == nil || newHeadBlock.NumberU64() > *pivot { 578 parent := bc.GetBlock(newHeadBlock.ParentHash(), newHeadBlock.NumberU64()-1) 579 if parent != nil { 580 newHeadBlock = parent 581 continue 582 } 583 log.Error("Missing block in the middle, aiming genesis", "number", newHeadBlock.NumberU64()-1, "hash", newHeadBlock.ParentHash()) 584 newHeadBlock = bc.genesisBlock 585 } else { 586 log.Trace("Rewind passed pivot, aiming genesis", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash(), "pivot", *pivot) 587 newHeadBlock = bc.genesisBlock 588 } 589 } 590 if beyondRoot || newHeadBlock.NumberU64() == 0 { 591 if newHeadBlock.NumberU64() == 0 { 592 // Recommit the genesis state into disk in case the rewinding destination 593 // is genesis block and the relevant state is gone. In the future this 594 // rewinding destination can be the earliest block stored in the chain 595 // if the historical chain pruning is enabled. In that case the logic 596 // needs to be improved here. 597 if !bc.HasState(bc.genesisBlock.Root()) { 598 if err := CommitGenesisState(bc.db, bc.genesisBlock.Hash()); err != nil { 599 log.Crit("Failed to commit genesis state", "err", err) 600 } 601 log.Debug("Recommitted genesis state to disk") 602 } 603 } 604 log.Debug("Rewound to block with state", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash()) 605 break 606 } 607 log.Debug("Skipping block with threshold state", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash(), "root", newHeadBlock.Root()) 608 newHeadBlock = bc.GetBlock(newHeadBlock.ParentHash(), newHeadBlock.NumberU64()-1) // Keep rewinding 609 } 610 } 611 rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash()) 612 613 // Degrade the chain markers if they are explicitly reverted. 614 // In theory we should update all in-memory markers in the 615 // last step, however the direction of SetHead is from high 616 // to low, so it's safe to update in-memory markers directly. 617 bc.currentBlock.Store(newHeadBlock) 618 headBlockGauge.Update(int64(newHeadBlock.NumberU64())) 619 } 620 // Rewind the fast block in a simpleton way to the target head 621 if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock != nil && header.Number.Uint64() < currentFastBlock.NumberU64() { 622 newHeadFastBlock := bc.GetBlock(header.Hash(), header.Number.Uint64()) 623 // If either blocks reached nil, reset to the genesis state 624 if newHeadFastBlock == nil { 625 newHeadFastBlock = bc.genesisBlock 626 } 627 rawdb.WriteHeadFastBlockHash(db, newHeadFastBlock.Hash()) 628 629 // Degrade the chain markers if they are explicitly reverted. 630 // In theory we should update all in-memory markers in the 631 // last step, however the direction of SetHead is from high 632 // to low, so it's safe the update in-memory markers directly. 633 bc.currentFastBlock.Store(newHeadFastBlock) 634 headFastBlockGauge.Update(int64(newHeadFastBlock.NumberU64())) 635 } 636 head := bc.CurrentBlock().NumberU64() 637 638 // If setHead underflown the freezer threshold and the block processing 639 // intent afterwards is full block importing, delete the chain segment 640 // between the stateful-block and the sethead target. 641 var wipe bool 642 if head+1 < frozen { 643 wipe = pivot == nil || head >= *pivot 644 } 645 return head, wipe // Only force wipe if full synced 646 } 647 // Rewind the header chain, deleting all block bodies until then 648 delFn := func(db ethdb.KeyValueWriter, hash common.Hash, num uint64) { 649 // Ignore the error here since light client won't hit this path 650 frozen, _ := bc.db.Ancients() 651 if num+1 <= frozen { 652 // Truncate all relative data(header, total difficulty, body, receipt 653 // and canonical hash) from ancient store. 654 if err := bc.db.TruncateHead(num); err != nil { 655 log.Crit("Failed to truncate ancient data", "number", num, "err", err) 656 } 657 // Remove the hash <-> number mapping from the active store. 658 rawdb.DeleteHeaderNumber(db, hash) 659 } else { 660 // Remove relative body and receipts from the active store. 661 // The header, total difficulty and canonical hash will be 662 // removed in the hc.SetHead function. 663 rawdb.DeleteBody(db, hash, num) 664 rawdb.DeleteReceipts(db, hash, num) 665 } 666 // Todo(rjl493456442) txlookup, bloombits, etc 667 } 668 // If SetHead was only called as a chain reparation method, try to skip 669 // touching the header chain altogether, unless the freezer is broken 670 if repair { 671 if target, force := updateFn(bc.db, bc.CurrentBlock().Header()); force { 672 bc.hc.SetHead(target, updateFn, delFn) 673 } 674 } else { 675 // Rewind the chain to the requested head and keep going backwards until a 676 // block with a state is found or fast sync pivot is passed 677 log.Warn("Rewinding blockchain", "target", head) 678 bc.hc.SetHead(head, updateFn, delFn) 679 } 680 // Clear out any stale content from the caches 681 bc.bodyCache.Purge() 682 bc.bodyRLPCache.Purge() 683 bc.receiptsCache.Purge() 684 bc.blockCache.Purge() 685 bc.txLookupCache.Purge() 686 bc.futureBlocks.Purge() 687 688 // Clear safe block, finalized block if needed 689 if safe := bc.CurrentSafeBlock(); safe != nil && head < safe.NumberU64() { 690 log.Warn("SetHead invalidated safe block") 691 bc.SetSafe(nil) 692 } 693 if finalized := bc.CurrentFinalizedBlock(); finalized != nil && head < finalized.NumberU64() { 694 log.Error("SetHead invalidated finalized block") 695 bc.SetFinalized(nil) 696 } 697 698 return rootNumber, bc.loadLastState() 699 } 700 701 // SnapSyncCommitHead sets the current head block to the one defined by the hash 702 // irrelevant what the chain contents were prior. 703 func (bc *BlockChain) SnapSyncCommitHead(hash common.Hash) error { 704 // Make sure that both the block as well at its state trie exists 705 block := bc.GetBlockByHash(hash) 706 if block == nil { 707 return fmt.Errorf("non existent block [%x..]", hash[:4]) 708 } 709 if _, err := trie.NewSecure(common.Hash{}, block.Root(), bc.stateCache.TrieDB()); err != nil { 710 return err 711 } 712 713 // If all checks out, manually set the head block. 714 if !bc.chainmu.TryLock() { 715 return errChainStopped 716 } 717 bc.currentBlock.Store(block) 718 headBlockGauge.Update(int64(block.NumberU64())) 719 bc.chainmu.Unlock() 720 721 // Destroy any existing state snapshot and regenerate it in the background, 722 // also resuming the normal maintenance of any previously paused snapshot. 723 if bc.snaps != nil { 724 bc.snaps.Rebuild(block.Root()) 725 } 726 log.Info("Committed new head block", "number", block.Number(), "hash", hash) 727 return nil 728 } 729 730 // Reset purges the entire blockchain, restoring it to its genesis state. 731 func (bc *BlockChain) Reset() error { 732 return bc.ResetWithGenesisBlock(bc.genesisBlock) 733 } 734 735 // ResetWithGenesisBlock purges the entire blockchain, restoring it to the 736 // specified genesis state. 737 func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error { 738 // Dump the entire block chain and purge the caches 739 if err := bc.SetHead(0); err != nil { 740 return err 741 } 742 if !bc.chainmu.TryLock() { 743 return errChainStopped 744 } 745 defer bc.chainmu.Unlock() 746 747 // Prepare the genesis block and reinitialise the chain 748 batch := bc.db.NewBatch() 749 rawdb.WriteTd(batch, genesis.Hash(), genesis.NumberU64(), genesis.Difficulty()) 750 rawdb.WriteBlock(batch, genesis) 751 if err := batch.Write(); err != nil { 752 log.Crit("Failed to write genesis block", "err", err) 753 } 754 bc.writeHeadBlock(genesis) 755 756 // Last update all in-memory chain markers 757 bc.genesisBlock = genesis 758 bc.currentBlock.Store(bc.genesisBlock) 759 headBlockGauge.Update(int64(bc.genesisBlock.NumberU64())) 760 bc.hc.SetGenesis(bc.genesisBlock.Header()) 761 bc.hc.SetCurrentHeader(bc.genesisBlock.Header()) 762 bc.currentFastBlock.Store(bc.genesisBlock) 763 headFastBlockGauge.Update(int64(bc.genesisBlock.NumberU64())) 764 return nil 765 } 766 767 // Export writes the active chain to the given writer. 768 func (bc *BlockChain) Export(w io.Writer) error { 769 return bc.ExportN(w, uint64(0), bc.CurrentBlock().NumberU64()) 770 } 771 772 // ExportN writes a subset of the active chain to the given writer. 773 func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error { 774 if first > last { 775 return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last) 776 } 777 log.Info("Exporting batch of blocks", "count", last-first+1) 778 779 var ( 780 parentHash common.Hash 781 start = time.Now() 782 reported = time.Now() 783 ) 784 for nr := first; nr <= last; nr++ { 785 block := bc.GetBlockByNumber(nr) 786 if block == nil { 787 return fmt.Errorf("export failed on #%d: not found", nr) 788 } 789 if nr > first && block.ParentHash() != parentHash { 790 return fmt.Errorf("export failed: chain reorg during export") 791 } 792 parentHash = block.Hash() 793 if err := block.EncodeRLP(w); err != nil { 794 return err 795 } 796 if time.Since(reported) >= statsReportLimit { 797 log.Info("Exporting blocks", "exported", block.NumberU64()-first, "elapsed", common.PrettyDuration(time.Since(start))) 798 reported = time.Now() 799 } 800 } 801 return nil 802 } 803 804 // writeHeadBlock injects a new head block into the current block chain. This method 805 // assumes that the block is indeed a true head. It will also reset the head 806 // header and the head fast sync block to this very same block if they are older 807 // or if they are on a different side chain. 808 // 809 // Note, this function assumes that the `mu` mutex is held! 810 func (bc *BlockChain) writeHeadBlock(block *types.Block) { 811 // Add the block to the canonical chain number scheme and mark as the head 812 batch := bc.db.NewBatch() 813 rawdb.WriteHeadHeaderHash(batch, block.Hash()) 814 rawdb.WriteHeadFastBlockHash(batch, block.Hash()) 815 rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64()) 816 rawdb.WriteTxLookupEntriesByBlock(batch, block) 817 rawdb.WriteHeadBlockHash(batch, block.Hash()) 818 819 // Flush the whole batch into the disk, exit the node if failed 820 if err := batch.Write(); err != nil { 821 log.Crit("Failed to update chain indexes and markers", "err", err) 822 } 823 // Update all in-memory chain markers in the last step 824 bc.hc.SetCurrentHeader(block.Header()) 825 826 bc.currentFastBlock.Store(block) 827 headFastBlockGauge.Update(int64(block.NumberU64())) 828 829 bc.currentBlock.Store(block) 830 headBlockGauge.Update(int64(block.NumberU64())) 831 } 832 833 // Stop stops the blockchain service. If any imports are currently in progress 834 // it will abort them using the procInterrupt. 835 func (bc *BlockChain) Stop() { 836 if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) { 837 return 838 } 839 840 // Unsubscribe all subscriptions registered from blockchain. 841 bc.scope.Close() 842 843 // Signal shutdown to all goroutines. 844 close(bc.quit) 845 bc.StopInsert() 846 847 // Now wait for all chain modifications to end and persistent goroutines to exit. 848 // 849 // Note: Close waits for the mutex to become available, i.e. any running chain 850 // modification will have exited when Close returns. Since we also called StopInsert, 851 // the mutex should become available quickly. It cannot be taken again after Close has 852 // returned. 853 bc.chainmu.Close() 854 bc.wg.Wait() 855 856 // Ensure that the entirety of the state snapshot is journalled to disk. 857 var snapBase common.Hash 858 if bc.snaps != nil { 859 var err error 860 if snapBase, err = bc.snaps.Journal(bc.CurrentBlock().Root()); err != nil { 861 log.Error("Failed to journal state snapshot", "err", err) 862 } 863 } 864 865 // Ensure the state of a recent block is also stored to disk before exiting. 866 // We're writing three different states to catch different restart scenarios: 867 // - HEAD: So we don't need to reprocess any blocks in the general case 868 // - HEAD-1: So we don't do large reorgs if our HEAD becomes an uncle 869 // - HEAD-127: So we have a hard limit on the number of blocks reexecuted 870 if !bc.cacheConfig.TrieDirtyDisabled { 871 triedb := bc.stateCache.TrieDB() 872 873 for _, offset := range []uint64{0, 1, TriesInMemory - 1} { 874 if number := bc.CurrentBlock().NumberU64(); number > offset { 875 recent := bc.GetBlockByNumber(number - offset) 876 877 log.Info("Writing cached state to disk", "block", recent.Number(), "hash", recent.Hash(), "root", recent.Root()) 878 if err := triedb.Commit(recent.Root(), true, nil); err != nil { 879 log.Error("Failed to commit recent state trie", "err", err) 880 } 881 } 882 } 883 if snapBase != (common.Hash{}) { 884 log.Info("Writing snapshot state to disk", "root", snapBase) 885 if err := triedb.Commit(snapBase, true, nil); err != nil { 886 log.Error("Failed to commit recent state trie", "err", err) 887 } 888 } 889 for !bc.triegc.Empty() { 890 triedb.Dereference(bc.triegc.PopItem().(common.Hash)) 891 } 892 if size, _ := triedb.Size(); size != 0 { 893 log.Error("Dangling trie nodes after full cleanup") 894 } 895 } 896 // Ensure all live cached entries be saved into disk, so that we can skip 897 // cache warmup when node restarts. 898 if bc.cacheConfig.TrieCleanJournal != "" { 899 triedb := bc.stateCache.TrieDB() 900 triedb.SaveCache(bc.cacheConfig.TrieCleanJournal) 901 } 902 log.Info("Blockchain stopped") 903 } 904 905 // StopInsert interrupts all insertion methods, causing them to return 906 // errInsertionInterrupted as soon as possible. Insertion is permanently disabled after 907 // calling this method. 908 func (bc *BlockChain) StopInsert() { 909 atomic.StoreInt32(&bc.procInterrupt, 1) 910 } 911 912 // insertStopped returns true after StopInsert has been called. 913 func (bc *BlockChain) insertStopped() bool { 914 return atomic.LoadInt32(&bc.procInterrupt) == 1 915 } 916 917 func (bc *BlockChain) procFutureBlocks() { 918 blocks := make([]*types.Block, 0, bc.futureBlocks.Len()) 919 for _, hash := range bc.futureBlocks.Keys() { 920 if block, exist := bc.futureBlocks.Peek(hash); exist { 921 blocks = append(blocks, block.(*types.Block)) 922 } 923 } 924 if len(blocks) > 0 { 925 sort.Slice(blocks, func(i, j int) bool { 926 return blocks[i].NumberU64() < blocks[j].NumberU64() 927 }) 928 // Insert one by one as chain insertion needs contiguous ancestry between blocks 929 for i := range blocks { 930 bc.InsertChain(blocks[i : i+1]) 931 } 932 } 933 } 934 935 // WriteStatus status of write 936 type WriteStatus byte 937 938 const ( 939 NonStatTy WriteStatus = iota 940 CanonStatTy 941 SideStatTy 942 ) 943 944 // InsertReceiptChain attempts to complete an already existing header chain with 945 // transaction and receipt data. 946 func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts, ancientLimit uint64) (int, error) { 947 // We don't require the chainMu here since we want to maximize the 948 // concurrency of header insertion and receipt insertion. 949 bc.wg.Add(1) 950 defer bc.wg.Done() 951 952 var ( 953 ancientBlocks, liveBlocks types.Blocks 954 ancientReceipts, liveReceipts []types.Receipts 955 ) 956 // Do a sanity check that the provided chain is actually ordered and linked 957 for i := 0; i < len(blockChain); i++ { 958 if i != 0 { 959 if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() { 960 log.Error("Non contiguous receipt insert", "number", blockChain[i].Number(), "hash", blockChain[i].Hash(), "parent", blockChain[i].ParentHash(), 961 "prevnumber", blockChain[i-1].Number(), "prevhash", blockChain[i-1].Hash()) 962 return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x..], item %d is #%d [%x..] (parent [%x..])", i-1, blockChain[i-1].NumberU64(), 963 blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4]) 964 } 965 } 966 if blockChain[i].NumberU64() <= ancientLimit { 967 ancientBlocks, ancientReceipts = append(ancientBlocks, blockChain[i]), append(ancientReceipts, receiptChain[i]) 968 } else { 969 liveBlocks, liveReceipts = append(liveBlocks, blockChain[i]), append(liveReceipts, receiptChain[i]) 970 } 971 } 972 973 var ( 974 stats = struct{ processed, ignored int32 }{} 975 start = time.Now() 976 size = int64(0) 977 ) 978 979 // updateHead updates the head fast sync block if the inserted blocks are better 980 // and returns an indicator whether the inserted blocks are canonical. 981 updateHead := func(head *types.Block) bool { 982 if !bc.chainmu.TryLock() { 983 return false 984 } 985 defer bc.chainmu.Unlock() 986 987 // Rewind may have occurred, skip in that case. 988 if bc.CurrentHeader().Number.Cmp(head.Number()) >= 0 { 989 reorg, err := bc.forker.ReorgNeeded(bc.CurrentFastBlock().Header(), head.Header()) 990 if err != nil { 991 log.Warn("Reorg failed", "err", err) 992 return false 993 } else if !reorg { 994 return false 995 } 996 rawdb.WriteHeadFastBlockHash(bc.db, head.Hash()) 997 bc.currentFastBlock.Store(head) 998 headFastBlockGauge.Update(int64(head.NumberU64())) 999 return true 1000 } 1001 return false 1002 } 1003 1004 // writeAncient writes blockchain and corresponding receipt chain into ancient store. 1005 // 1006 // this function only accepts canonical chain data. All side chain will be reverted 1007 // eventually. 1008 writeAncient := func(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) { 1009 first := blockChain[0] 1010 last := blockChain[len(blockChain)-1] 1011 1012 // Ensure genesis is in ancients. 1013 if first.NumberU64() == 1 { 1014 if frozen, _ := bc.db.Ancients(); frozen == 0 { 1015 b := bc.genesisBlock 1016 td := bc.genesisBlock.Difficulty() 1017 writeSize, err := rawdb.WriteAncientBlocks(bc.db, []*types.Block{b}, []types.Receipts{nil}, td) 1018 size += writeSize 1019 if err != nil { 1020 log.Error("Error writing genesis to ancients", "err", err) 1021 return 0, err 1022 } 1023 log.Info("Wrote genesis to ancients") 1024 } 1025 } 1026 // Before writing the blocks to the ancients, we need to ensure that 1027 // they correspond to the what the headerchain 'expects'. 1028 // We only check the last block/header, since it's a contiguous chain. 1029 if !bc.HasHeader(last.Hash(), last.NumberU64()) { 1030 return 0, fmt.Errorf("containing header #%d [%x..] unknown", last.Number(), last.Hash().Bytes()[:4]) 1031 } 1032 1033 // Write all chain data to ancients. 1034 td := bc.GetTd(first.Hash(), first.NumberU64()) 1035 writeSize, err := rawdb.WriteAncientBlocks(bc.db, blockChain, receiptChain, td) 1036 size += writeSize 1037 if err != nil { 1038 log.Error("Error importing chain data to ancients", "err", err) 1039 return 0, err 1040 } 1041 1042 // Write tx indices if any condition is satisfied: 1043 // * If user requires to reserve all tx indices(txlookuplimit=0) 1044 // * If all ancient tx indices are required to be reserved(txlookuplimit is even higher than ancientlimit) 1045 // * If block number is large enough to be regarded as a recent block 1046 // It means blocks below the ancientLimit-txlookupLimit won't be indexed. 1047 // 1048 // But if the `TxIndexTail` is not nil, e.g. Geth is initialized with 1049 // an external ancient database, during the setup, blockchain will start 1050 // a background routine to re-indexed all indices in [ancients - txlookupLimit, ancients) 1051 // range. In this case, all tx indices of newly imported blocks should be 1052 // generated. 1053 var batch = bc.db.NewBatch() 1054 for i, block := range blockChain { 1055 if bc.txLookupLimit == 0 || ancientLimit <= bc.txLookupLimit || block.NumberU64() >= ancientLimit-bc.txLookupLimit { 1056 rawdb.WriteTxLookupEntriesByBlock(batch, block) 1057 } else if rawdb.ReadTxIndexTail(bc.db) != nil { 1058 rawdb.WriteTxLookupEntriesByBlock(batch, block) 1059 } 1060 stats.processed++ 1061 1062 if batch.ValueSize() > ethdb.IdealBatchSize || i == len(blockChain)-1 { 1063 size += int64(batch.ValueSize()) 1064 if err = batch.Write(); err != nil { 1065 fastBlock := bc.CurrentFastBlock().NumberU64() 1066 if err := bc.db.TruncateHead(fastBlock + 1); err != nil { 1067 log.Error("Can't truncate ancient store after failed insert", "err", err) 1068 } 1069 return 0, err 1070 } 1071 batch.Reset() 1072 } 1073 } 1074 1075 // Sync the ancient store explicitly to ensure all data has been flushed to disk. 1076 if err := bc.db.Sync(); err != nil { 1077 return 0, err 1078 } 1079 // Update the current fast block because all block data is now present in DB. 1080 previousFastBlock := bc.CurrentFastBlock().NumberU64() 1081 if !updateHead(blockChain[len(blockChain)-1]) { 1082 // We end up here if the header chain has reorg'ed, and the blocks/receipts 1083 // don't match the canonical chain. 1084 if err := bc.db.TruncateHead(previousFastBlock + 1); err != nil { 1085 log.Error("Can't truncate ancient store after failed insert", "err", err) 1086 } 1087 return 0, errSideChainReceipts 1088 } 1089 1090 // Delete block data from the main database. 1091 batch.Reset() 1092 canonHashes := make(map[common.Hash]struct{}) 1093 for _, block := range blockChain { 1094 canonHashes[block.Hash()] = struct{}{} 1095 if block.NumberU64() == 0 { 1096 continue 1097 } 1098 rawdb.DeleteCanonicalHash(batch, block.NumberU64()) 1099 rawdb.DeleteBlockWithoutNumber(batch, block.Hash(), block.NumberU64()) 1100 } 1101 // Delete side chain hash-to-number mappings. 1102 for _, nh := range rawdb.ReadAllHashesInRange(bc.db, first.NumberU64(), last.NumberU64()) { 1103 if _, canon := canonHashes[nh.Hash]; !canon { 1104 rawdb.DeleteHeader(batch, nh.Hash, nh.Number) 1105 } 1106 } 1107 if err := batch.Write(); err != nil { 1108 return 0, err 1109 } 1110 return 0, nil 1111 } 1112 1113 // writeLive writes blockchain and corresponding receipt chain into active store. 1114 writeLive := func(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) { 1115 skipPresenceCheck := false 1116 batch := bc.db.NewBatch() 1117 for i, block := range blockChain { 1118 // Short circuit insertion if shutting down or processing failed 1119 if bc.insertStopped() { 1120 return 0, errInsertionInterrupted 1121 } 1122 // Short circuit if the owner header is unknown 1123 if !bc.HasHeader(block.Hash(), block.NumberU64()) { 1124 return i, fmt.Errorf("containing header #%d [%x..] unknown", block.Number(), block.Hash().Bytes()[:4]) 1125 } 1126 if !skipPresenceCheck { 1127 // Ignore if the entire data is already known 1128 if bc.HasBlock(block.Hash(), block.NumberU64()) { 1129 stats.ignored++ 1130 continue 1131 } else { 1132 // If block N is not present, neither are the later blocks. 1133 // This should be true, but if we are mistaken, the shortcut 1134 // here will only cause overwriting of some existing data 1135 skipPresenceCheck = true 1136 } 1137 } 1138 // Write all the data out into the database 1139 rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body()) 1140 rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receiptChain[i]) 1141 rawdb.WriteTxLookupEntriesByBlock(batch, block) // Always write tx indices for live blocks, we assume they are needed 1142 1143 // Write everything belongs to the blocks into the database. So that 1144 // we can ensure all components of body is completed(body, receipts, 1145 // tx indexes) 1146 if batch.ValueSize() >= ethdb.IdealBatchSize { 1147 if err := batch.Write(); err != nil { 1148 return 0, err 1149 } 1150 size += int64(batch.ValueSize()) 1151 batch.Reset() 1152 } 1153 stats.processed++ 1154 } 1155 // Write everything belongs to the blocks into the database. So that 1156 // we can ensure all components of body is completed(body, receipts, 1157 // tx indexes) 1158 if batch.ValueSize() > 0 { 1159 size += int64(batch.ValueSize()) 1160 if err := batch.Write(); err != nil { 1161 return 0, err 1162 } 1163 } 1164 updateHead(blockChain[len(blockChain)-1]) 1165 return 0, nil 1166 } 1167 1168 // Write downloaded chain data and corresponding receipt chain data 1169 if len(ancientBlocks) > 0 { 1170 if n, err := writeAncient(ancientBlocks, ancientReceipts); err != nil { 1171 if err == errInsertionInterrupted { 1172 return 0, nil 1173 } 1174 return n, err 1175 } 1176 } 1177 // Write the tx index tail (block number from where we index) before write any live blocks 1178 if len(liveBlocks) > 0 && liveBlocks[0].NumberU64() == ancientLimit+1 { 1179 // The tx index tail can only be one of the following two options: 1180 // * 0: all ancient blocks have been indexed 1181 // * ancient-limit: the indices of blocks before ancient-limit are ignored 1182 if tail := rawdb.ReadTxIndexTail(bc.db); tail == nil { 1183 if bc.txLookupLimit == 0 || ancientLimit <= bc.txLookupLimit { 1184 rawdb.WriteTxIndexTail(bc.db, 0) 1185 } else { 1186 rawdb.WriteTxIndexTail(bc.db, ancientLimit-bc.txLookupLimit) 1187 } 1188 } 1189 } 1190 if len(liveBlocks) > 0 { 1191 if n, err := writeLive(liveBlocks, liveReceipts); err != nil { 1192 if err == errInsertionInterrupted { 1193 return 0, nil 1194 } 1195 return n, err 1196 } 1197 } 1198 1199 head := blockChain[len(blockChain)-1] 1200 context := []interface{}{ 1201 "count", stats.processed, "elapsed", common.PrettyDuration(time.Since(start)), 1202 "number", head.Number(), "hash", head.Hash(), "age", common.PrettyAge(time.Unix(int64(head.Time()), 0)), 1203 "size", common.StorageSize(size), 1204 } 1205 if stats.ignored > 0 { 1206 context = append(context, []interface{}{"ignored", stats.ignored}...) 1207 } 1208 log.Info("Imported new block receipts", context...) 1209 1210 return 0, nil 1211 } 1212 1213 var lastWrite uint64 1214 1215 // writeBlockWithoutState writes only the block and its metadata to the database, 1216 // but does not write any state. This is used to construct competing side forks 1217 // up to the point where they exceed the canonical total difficulty. 1218 func (bc *BlockChain) writeBlockWithoutState(block *types.Block, td *big.Int) (err error) { 1219 if bc.insertStopped() { 1220 return errInsertionInterrupted 1221 } 1222 1223 batch := bc.db.NewBatch() 1224 rawdb.WriteTd(batch, block.Hash(), block.NumberU64(), td) 1225 rawdb.WriteBlock(batch, block) 1226 if err := batch.Write(); err != nil { 1227 log.Crit("Failed to write block into disk", "err", err) 1228 } 1229 return nil 1230 } 1231 1232 // writeKnownBlock updates the head block flag with a known block 1233 // and introduces chain reorg if necessary. 1234 func (bc *BlockChain) writeKnownBlock(block *types.Block) error { 1235 current := bc.CurrentBlock() 1236 if block.ParentHash() != current.Hash() { 1237 if err := bc.reorg(current, block); err != nil { 1238 return err 1239 } 1240 } 1241 bc.writeHeadBlock(block) 1242 return nil 1243 } 1244 1245 // writeBlockWithState writes block, metadata and corresponding state data to the 1246 // database. 1247 func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB) error { 1248 // Calculate the total difficulty of the block 1249 ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1) 1250 if ptd == nil { 1251 return consensus.ErrUnknownAncestor 1252 } 1253 // Make sure no inconsistent state is leaked during insertion 1254 externTd := new(big.Int).Add(block.Difficulty(), ptd) 1255 1256 // Irrelevant of the canonical status, write the block itself to the database. 1257 // 1258 // Note all the components of block(td, hash->number map, header, body, receipts) 1259 // should be written atomically. BlockBatch is used for containing all components. 1260 blockBatch := bc.db.NewBatch() 1261 rawdb.WriteTd(blockBatch, block.Hash(), block.NumberU64(), externTd) 1262 rawdb.WriteBlock(blockBatch, block) 1263 rawdb.WriteReceipts(blockBatch, block.Hash(), block.NumberU64(), receipts) 1264 rawdb.WritePreimages(blockBatch, state.Preimages()) 1265 if err := blockBatch.Write(); err != nil { 1266 log.Crit("Failed to write block into disk", "err", err) 1267 } 1268 // Commit all cached state changes into underlying memory database. 1269 root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number())) 1270 if err != nil { 1271 return err 1272 } 1273 triedb := bc.stateCache.TrieDB() 1274 1275 // If we're running an archive node, always flush 1276 if bc.cacheConfig.TrieDirtyDisabled { 1277 return triedb.Commit(root, false, nil) 1278 } else { 1279 // Full but not archive node, do proper garbage collection 1280 triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive 1281 bc.triegc.Push(root, -int64(block.NumberU64())) 1282 1283 if current := block.NumberU64(); current > TriesInMemory { 1284 // If we exceeded our memory allowance, flush matured singleton nodes to disk 1285 var ( 1286 nodes, imgs = triedb.Size() 1287 limit = common.StorageSize(bc.cacheConfig.TrieDirtyLimit) * 1024 * 1024 1288 ) 1289 if nodes > limit || imgs > 4*1024*1024 { 1290 triedb.Cap(limit - ethdb.IdealBatchSize) 1291 } 1292 // Find the next state trie we need to commit 1293 chosen := current - TriesInMemory 1294 1295 // If we exceeded out time allowance, flush an entire trie to disk 1296 if bc.gcproc > bc.cacheConfig.TrieTimeLimit { 1297 // If the header is missing (canonical chain behind), we're reorging a low 1298 // diff sidechain. Suspend committing until this operation is completed. 1299 header := bc.GetHeaderByNumber(chosen) 1300 if header == nil { 1301 log.Warn("Reorg in progress, trie commit postponed", "number", chosen) 1302 } else { 1303 // If we're exceeding limits but haven't reached a large enough memory gap, 1304 // warn the user that the system is becoming unstable. 1305 if chosen < lastWrite+TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit { 1306 log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/TriesInMemory) 1307 } 1308 // Flush an entire trie and restart the counters 1309 triedb.Commit(header.Root, true, nil) 1310 lastWrite = chosen 1311 bc.gcproc = 0 1312 } 1313 } 1314 // Garbage collect anything below our required write retention 1315 for !bc.triegc.Empty() { 1316 root, number := bc.triegc.Pop() 1317 if uint64(-number) > chosen { 1318 bc.triegc.Push(root, number) 1319 break 1320 } 1321 triedb.Dereference(root.(common.Hash)) 1322 } 1323 } 1324 } 1325 return nil 1326 } 1327 1328 // WriteBlockAndSetHead writes the given block and all associated state to the database, 1329 // and applies the block as the new chain head. 1330 func (bc *BlockChain) WriteBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { 1331 if !bc.chainmu.TryLock() { 1332 return NonStatTy, errChainStopped 1333 } 1334 defer bc.chainmu.Unlock() 1335 1336 return bc.writeBlockAndSetHead(block, receipts, logs, state, emitHeadEvent) 1337 } 1338 1339 // writeBlockAndSetHead is the internal implementation of WriteBlockAndSetHead. 1340 // This function expects the chain mutex to be held. 1341 func (bc *BlockChain) writeBlockAndSetHead(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { 1342 if err := bc.writeBlockWithState(block, receipts, logs, state); err != nil { 1343 return NonStatTy, err 1344 } 1345 currentBlock := bc.CurrentBlock() 1346 reorg, err := bc.forker.ReorgNeeded(currentBlock.Header(), block.Header()) 1347 if err != nil { 1348 return NonStatTy, err 1349 } 1350 if reorg { 1351 // Reorganise the chain if the parent is not the head block 1352 if block.ParentHash() != currentBlock.Hash() { 1353 if err := bc.reorg(currentBlock, block); err != nil { 1354 return NonStatTy, err 1355 } 1356 } 1357 status = CanonStatTy 1358 } else { 1359 status = SideStatTy 1360 } 1361 // Set new head. 1362 if status == CanonStatTy { 1363 bc.writeHeadBlock(block) 1364 } 1365 bc.futureBlocks.Remove(block.Hash()) 1366 1367 if status == CanonStatTy { 1368 bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs}) 1369 if len(logs) > 0 { 1370 bc.logsFeed.Send(logs) 1371 } 1372 // In theory we should fire a ChainHeadEvent when we inject 1373 // a canonical block, but sometimes we can insert a batch of 1374 // canonicial blocks. Avoid firing too many ChainHeadEvents, 1375 // we will fire an accumulated ChainHeadEvent and disable fire 1376 // event here. 1377 if emitHeadEvent { 1378 bc.chainHeadFeed.Send(ChainHeadEvent{Block: block}) 1379 } 1380 } else { 1381 bc.chainSideFeed.Send(ChainSideEvent{Block: block}) 1382 } 1383 return status, nil 1384 } 1385 1386 // addFutureBlock checks if the block is within the max allowed window to get 1387 // accepted for future processing, and returns an error if the block is too far 1388 // ahead and was not added. 1389 // 1390 // TODO after the transition, the future block shouldn't be kept. Because 1391 // it's not checked in the Geth side anymore. 1392 func (bc *BlockChain) addFutureBlock(block *types.Block) error { 1393 max := uint64(time.Now().Unix() + maxTimeFutureBlocks) 1394 if block.Time() > max { 1395 return fmt.Errorf("future block timestamp %v > allowed %v", block.Time(), max) 1396 } 1397 if block.Difficulty().Cmp(common.Big0) == 0 { 1398 // Never add PoS blocks into the future queue 1399 return nil 1400 } 1401 bc.futureBlocks.Add(block.Hash(), block) 1402 return nil 1403 } 1404 1405 // InsertChain attempts to insert the given batch of blocks in to the canonical 1406 // chain or, otherwise, create a fork. If an error is returned it will return 1407 // the index number of the failing block as well an error describing what went 1408 // wrong. After insertion is done, all accumulated events will be fired. 1409 func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) { 1410 // Sanity check that we have something meaningful to import 1411 if len(chain) == 0 { 1412 return 0, nil 1413 } 1414 bc.blockProcFeed.Send(true) 1415 defer bc.blockProcFeed.Send(false) 1416 1417 // Do a sanity check that the provided chain is actually ordered and linked. 1418 for i := 1; i < len(chain); i++ { 1419 block, prev := chain[i], chain[i-1] 1420 if block.NumberU64() != prev.NumberU64()+1 || block.ParentHash() != prev.Hash() { 1421 log.Error("Non contiguous block insert", 1422 "number", block.Number(), 1423 "hash", block.Hash(), 1424 "parent", block.ParentHash(), 1425 "prevnumber", prev.Number(), 1426 "prevhash", prev.Hash(), 1427 ) 1428 return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x..], item %d is #%d [%x..] (parent [%x..])", i-1, prev.NumberU64(), 1429 prev.Hash().Bytes()[:4], i, block.NumberU64(), block.Hash().Bytes()[:4], block.ParentHash().Bytes()[:4]) 1430 } 1431 } 1432 // Pre-checks passed, start the full block imports 1433 if !bc.chainmu.TryLock() { 1434 return 0, errChainStopped 1435 } 1436 defer bc.chainmu.Unlock() 1437 return bc.insertChain(chain, true, true) 1438 } 1439 1440 // insertChain is the internal implementation of InsertChain, which assumes that 1441 // 1) chains are contiguous, and 2) The chain mutex is held. 1442 // 1443 // This method is split out so that import batches that require re-injecting 1444 // historical blocks can do so without releasing the lock, which could lead to 1445 // racey behaviour. If a sidechain import is in progress, and the historic state 1446 // is imported, but then new canon-head is added before the actual sidechain 1447 // completes, then the historic state could be pruned again 1448 func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool) (int, error) { 1449 // If the chain is terminating, don't even bother starting up. 1450 if bc.insertStopped() { 1451 return 0, nil 1452 } 1453 1454 // Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss) 1455 senderCacher.recoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number()), chain) 1456 1457 var ( 1458 stats = insertStats{startTime: mclock.Now()} 1459 lastCanon *types.Block 1460 ) 1461 // Fire a single chain head event if we've progressed the chain 1462 defer func() { 1463 if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() { 1464 bc.chainHeadFeed.Send(ChainHeadEvent{lastCanon}) 1465 } 1466 }() 1467 // Start the parallel header verifier 1468 headers := make([]*types.Header, len(chain)) 1469 seals := make([]bool, len(chain)) 1470 1471 for i, block := range chain { 1472 headers[i] = block.Header() 1473 seals[i] = verifySeals 1474 } 1475 abort, results := bc.engine.VerifyHeaders(bc, headers, seals) 1476 defer close(abort) 1477 1478 // Peek the error for the first block to decide the directing import logic 1479 it := newInsertIterator(chain, results, bc.validator) 1480 block, err := it.next() 1481 1482 // Left-trim all the known blocks that don't need to build snapshot 1483 if bc.skipBlock(err, it) { 1484 // First block (and state) is known 1485 // 1. We did a roll-back, and should now do a re-import 1486 // 2. The block is stored as a sidechain, and is lying about it's stateroot, and passes a stateroot 1487 // from the canonical chain, which has not been verified. 1488 // Skip all known blocks that are behind us. 1489 var ( 1490 reorg bool 1491 current = bc.CurrentBlock() 1492 ) 1493 for block != nil && bc.skipBlock(err, it) { 1494 reorg, err = bc.forker.ReorgNeeded(current.Header(), block.Header()) 1495 if err != nil { 1496 return it.index, err 1497 } 1498 if reorg { 1499 // Switch to import mode if the forker says the reorg is necessary 1500 // and also the block is not on the canonical chain. 1501 // In eth2 the forker always returns true for reorg decision (blindly trusting 1502 // the external consensus engine), but in order to prevent the unnecessary 1503 // reorgs when importing known blocks, the special case is handled here. 1504 if block.NumberU64() > current.NumberU64() || bc.GetCanonicalHash(block.NumberU64()) != block.Hash() { 1505 break 1506 } 1507 } 1508 log.Debug("Ignoring already known block", "number", block.Number(), "hash", block.Hash()) 1509 stats.ignored++ 1510 1511 block, err = it.next() 1512 } 1513 // The remaining blocks are still known blocks, the only scenario here is: 1514 // During the fast sync, the pivot point is already submitted but rollback 1515 // happens. Then node resets the head full block to a lower height via `rollback` 1516 // and leaves a few known blocks in the database. 1517 // 1518 // When node runs a fast sync again, it can re-import a batch of known blocks via 1519 // `insertChain` while a part of them have higher total difficulty than current 1520 // head full block(new pivot point). 1521 for block != nil && bc.skipBlock(err, it) { 1522 log.Debug("Writing previously known block", "number", block.Number(), "hash", block.Hash()) 1523 if err := bc.writeKnownBlock(block); err != nil { 1524 return it.index, err 1525 } 1526 lastCanon = block 1527 1528 block, err = it.next() 1529 } 1530 // Falls through to the block import 1531 } 1532 switch { 1533 // First block is pruned 1534 case errors.Is(err, consensus.ErrPrunedAncestor): 1535 if setHead { 1536 // First block is pruned, insert as sidechain and reorg only if TD grows enough 1537 log.Debug("Pruned ancestor, inserting as sidechain", "number", block.Number(), "hash", block.Hash()) 1538 return bc.insertSideChain(block, it) 1539 } else { 1540 // We're post-merge and the parent is pruned, try to recover the parent state 1541 log.Debug("Pruned ancestor", "number", block.Number(), "hash", block.Hash()) 1542 _, err := bc.recoverAncestors(block) 1543 return it.index, err 1544 } 1545 // First block is future, shove it (and all children) to the future queue (unknown ancestor) 1546 case errors.Is(err, consensus.ErrFutureBlock) || (errors.Is(err, consensus.ErrUnknownAncestor) && bc.futureBlocks.Contains(it.first().ParentHash())): 1547 for block != nil && (it.index == 0 || errors.Is(err, consensus.ErrUnknownAncestor)) { 1548 log.Debug("Future block, postponing import", "number", block.Number(), "hash", block.Hash()) 1549 if err := bc.addFutureBlock(block); err != nil { 1550 return it.index, err 1551 } 1552 block, err = it.next() 1553 } 1554 stats.queued += it.processed() 1555 stats.ignored += it.remaining() 1556 1557 // If there are any still remaining, mark as ignored 1558 return it.index, err 1559 1560 // Some other error(except ErrKnownBlock) occurred, abort. 1561 // ErrKnownBlock is allowed here since some known blocks 1562 // still need re-execution to generate snapshots that are missing 1563 case err != nil && !errors.Is(err, ErrKnownBlock): 1564 bc.futureBlocks.Remove(block.Hash()) 1565 stats.ignored += len(it.chain) 1566 bc.reportBlock(block, nil, err) 1567 return it.index, err 1568 } 1569 // No validation errors for the first block (or chain prefix skipped) 1570 var activeState *state.StateDB 1571 defer func() { 1572 // The chain importer is starting and stopping trie prefetchers. If a bad 1573 // block or other error is hit however, an early return may not properly 1574 // terminate the background threads. This defer ensures that we clean up 1575 // and dangling prefetcher, without defering each and holding on live refs. 1576 if activeState != nil { 1577 activeState.StopPrefetcher() 1578 } 1579 }() 1580 1581 for ; block != nil && err == nil || errors.Is(err, ErrKnownBlock); block, err = it.next() { 1582 // If the chain is terminating, stop processing blocks 1583 if bc.insertStopped() { 1584 log.Debug("Abort during block processing") 1585 break 1586 } 1587 // If the header is a banned one, straight out abort 1588 if BadHashes[block.Hash()] { 1589 bc.reportBlock(block, nil, ErrBannedHash) 1590 return it.index, ErrBannedHash 1591 } 1592 // If the block is known (in the middle of the chain), it's a special case for 1593 // Clique blocks where they can share state among each other, so importing an 1594 // older block might complete the state of the subsequent one. In this case, 1595 // just skip the block (we already validated it once fully (and crashed), since 1596 // its header and body was already in the database). But if the corresponding 1597 // snapshot layer is missing, forcibly rerun the execution to build it. 1598 if bc.skipBlock(err, it) { 1599 logger := log.Debug 1600 if bc.chainConfig.Clique == nil { 1601 logger = log.Warn 1602 } 1603 logger("Inserted known block", "number", block.Number(), "hash", block.Hash(), 1604 "uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(), 1605 "root", block.Root()) 1606 1607 // Special case. Commit the empty receipt slice if we meet the known 1608 // block in the middle. It can only happen in the clique chain. Whenever 1609 // we insert blocks via `insertSideChain`, we only commit `td`, `header` 1610 // and `body` if it's non-existent. Since we don't have receipts without 1611 // reexecution, so nothing to commit. But if the sidechain will be adpoted 1612 // as the canonical chain eventually, it needs to be reexecuted for missing 1613 // state, but if it's this special case here(skip reexecution) we will lose 1614 // the empty receipt entry. 1615 if len(block.Transactions()) == 0 { 1616 rawdb.WriteReceipts(bc.db, block.Hash(), block.NumberU64(), nil) 1617 } else { 1618 log.Error("Please file an issue, skip known block execution without receipt", 1619 "hash", block.Hash(), "number", block.NumberU64()) 1620 } 1621 if err := bc.writeKnownBlock(block); err != nil { 1622 return it.index, err 1623 } 1624 stats.processed++ 1625 1626 // We can assume that logs are empty here, since the only way for consecutive 1627 // Clique blocks to have the same state is if there are no transactions. 1628 lastCanon = block 1629 continue 1630 } 1631 1632 // Retrieve the parent block and it's state to execute on top 1633 start := time.Now() 1634 parent := it.previous() 1635 if parent == nil { 1636 parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1) 1637 } 1638 statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps) 1639 if err != nil { 1640 return it.index, err 1641 } 1642 1643 // Enable prefetching to pull in trie node paths while processing transactions 1644 statedb.StartPrefetcher("chain") 1645 activeState = statedb 1646 1647 // If we have a followup block, run that against the current state to pre-cache 1648 // transactions and probabilistically some of the account/storage trie nodes. 1649 var followupInterrupt uint32 1650 if !bc.cacheConfig.TrieCleanNoPrefetch { 1651 if followup, err := it.peek(); followup != nil && err == nil { 1652 throwaway, _ := state.New(parent.Root, bc.stateCache, bc.snaps) 1653 1654 go func(start time.Time, followup *types.Block, throwaway *state.StateDB, interrupt *uint32) { 1655 bc.prefetcher.Prefetch(followup, throwaway, bc.vmConfig, &followupInterrupt) 1656 1657 blockPrefetchExecuteTimer.Update(time.Since(start)) 1658 if atomic.LoadUint32(interrupt) == 1 { 1659 blockPrefetchInterruptMeter.Mark(1) 1660 } 1661 }(time.Now(), followup, throwaway, &followupInterrupt) 1662 } 1663 } 1664 1665 // Process block using the parent state as reference point 1666 substart := time.Now() 1667 receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig) 1668 if err != nil { 1669 bc.reportBlock(block, receipts, err) 1670 atomic.StoreUint32(&followupInterrupt, 1) 1671 return it.index, err 1672 } 1673 1674 // Update the metrics touched during block processing 1675 accountReadTimer.Update(statedb.AccountReads) // Account reads are complete, we can mark them 1676 storageReadTimer.Update(statedb.StorageReads) // Storage reads are complete, we can mark them 1677 accountUpdateTimer.Update(statedb.AccountUpdates) // Account updates are complete, we can mark them 1678 storageUpdateTimer.Update(statedb.StorageUpdates) // Storage updates are complete, we can mark them 1679 snapshotAccountReadTimer.Update(statedb.SnapshotAccountReads) // Account reads are complete, we can mark them 1680 snapshotStorageReadTimer.Update(statedb.SnapshotStorageReads) // Storage reads are complete, we can mark them 1681 triehash := statedb.AccountHashes + statedb.StorageHashes // Save to not double count in validation 1682 trieproc := statedb.SnapshotAccountReads + statedb.AccountReads + statedb.AccountUpdates 1683 trieproc += statedb.SnapshotStorageReads + statedb.StorageReads + statedb.StorageUpdates 1684 1685 blockExecutionTimer.Update(time.Since(substart) - trieproc - triehash) 1686 1687 // Validate the state using the default validator 1688 substart = time.Now() 1689 if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil { 1690 bc.reportBlock(block, receipts, err) 1691 atomic.StoreUint32(&followupInterrupt, 1) 1692 return it.index, err 1693 } 1694 proctime := time.Since(start) 1695 1696 // Update the metrics touched during block validation 1697 accountHashTimer.Update(statedb.AccountHashes) // Account hashes are complete, we can mark them 1698 storageHashTimer.Update(statedb.StorageHashes) // Storage hashes are complete, we can mark them 1699 blockValidationTimer.Update(time.Since(substart) - (statedb.AccountHashes + statedb.StorageHashes - triehash)) 1700 1701 // Write the block to the chain and get the status. 1702 substart = time.Now() 1703 var status WriteStatus 1704 if !setHead { 1705 // Don't set the head, only insert the block 1706 err = bc.writeBlockWithState(block, receipts, logs, statedb) 1707 } else { 1708 status, err = bc.writeBlockAndSetHead(block, receipts, logs, statedb, false) 1709 } 1710 atomic.StoreUint32(&followupInterrupt, 1) 1711 if err != nil { 1712 return it.index, err 1713 } 1714 // Update the metrics touched during block commit 1715 accountCommitTimer.Update(statedb.AccountCommits) // Account commits are complete, we can mark them 1716 storageCommitTimer.Update(statedb.StorageCommits) // Storage commits are complete, we can mark them 1717 snapshotCommitTimer.Update(statedb.SnapshotCommits) // Snapshot commits are complete, we can mark them 1718 1719 blockWriteTimer.Update(time.Since(substart) - statedb.AccountCommits - statedb.StorageCommits - statedb.SnapshotCommits) 1720 blockInsertTimer.UpdateSince(start) 1721 1722 // Report the import stats before returning the various results 1723 stats.processed++ 1724 stats.usedGas += usedGas 1725 1726 dirty, _ := bc.stateCache.TrieDB().Size() 1727 stats.report(chain, it.index, dirty, setHead) 1728 1729 if !setHead { 1730 return it.index, nil // Direct block insertion of a single block 1731 } 1732 switch status { 1733 case CanonStatTy: 1734 log.Debug("Inserted new block", "number", block.Number(), "hash", block.Hash(), 1735 "uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(), 1736 "elapsed", common.PrettyDuration(time.Since(start)), 1737 "root", block.Root()) 1738 1739 lastCanon = block 1740 1741 // Only count canonical blocks for GC processing time 1742 bc.gcproc += proctime 1743 1744 case SideStatTy: 1745 log.Debug("Inserted forked block", "number", block.Number(), "hash", block.Hash(), 1746 "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)), 1747 "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()), 1748 "root", block.Root()) 1749 1750 default: 1751 // This in theory is impossible, but lets be nice to our future selves and leave 1752 // a log, instead of trying to track down blocks imports that don't emit logs. 1753 log.Warn("Inserted block with unknown status", "number", block.Number(), "hash", block.Hash(), 1754 "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)), 1755 "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()), 1756 "root", block.Root()) 1757 } 1758 } 1759 1760 // Any blocks remaining here? The only ones we care about are the future ones 1761 if block != nil && errors.Is(err, consensus.ErrFutureBlock) { 1762 if err := bc.addFutureBlock(block); err != nil { 1763 return it.index, err 1764 } 1765 block, err = it.next() 1766 1767 for ; block != nil && errors.Is(err, consensus.ErrUnknownAncestor); block, err = it.next() { 1768 if err := bc.addFutureBlock(block); err != nil { 1769 return it.index, err 1770 } 1771 stats.queued++ 1772 } 1773 } 1774 stats.ignored += it.remaining() 1775 1776 return it.index, err 1777 } 1778 1779 // insertSideChain is called when an import batch hits upon a pruned ancestor 1780 // error, which happens when a sidechain with a sufficiently old fork-block is 1781 // found. 1782 // 1783 // The method writes all (header-and-body-valid) blocks to disk, then tries to 1784 // switch over to the new chain if the TD exceeded the current chain. 1785 // insertSideChain is only used pre-merge. 1786 func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (int, error) { 1787 var ( 1788 externTd *big.Int 1789 lastBlock = block 1790 current = bc.CurrentBlock() 1791 ) 1792 // The first sidechain block error is already verified to be ErrPrunedAncestor. 1793 // Since we don't import them here, we expect ErrUnknownAncestor for the remaining 1794 // ones. Any other errors means that the block is invalid, and should not be written 1795 // to disk. 1796 err := consensus.ErrPrunedAncestor 1797 for ; block != nil && errors.Is(err, consensus.ErrPrunedAncestor); block, err = it.next() { 1798 // Check the canonical state root for that number 1799 if number := block.NumberU64(); current.NumberU64() >= number { 1800 canonical := bc.GetBlockByNumber(number) 1801 if canonical != nil && canonical.Hash() == block.Hash() { 1802 // Not a sidechain block, this is a re-import of a canon block which has it's state pruned 1803 1804 // Collect the TD of the block. Since we know it's a canon one, 1805 // we can get it directly, and not (like further below) use 1806 // the parent and then add the block on top 1807 externTd = bc.GetTd(block.Hash(), block.NumberU64()) 1808 continue 1809 } 1810 if canonical != nil && canonical.Root() == block.Root() { 1811 // This is most likely a shadow-state attack. When a fork is imported into the 1812 // database, and it eventually reaches a block height which is not pruned, we 1813 // just found that the state already exist! This means that the sidechain block 1814 // refers to a state which already exists in our canon chain. 1815 // 1816 // If left unchecked, we would now proceed importing the blocks, without actually 1817 // having verified the state of the previous blocks. 1818 log.Warn("Sidechain ghost-state attack detected", "number", block.NumberU64(), "sideroot", block.Root(), "canonroot", canonical.Root()) 1819 1820 // If someone legitimately side-mines blocks, they would still be imported as usual. However, 1821 // we cannot risk writing unverified blocks to disk when they obviously target the pruning 1822 // mechanism. 1823 return it.index, errors.New("sidechain ghost-state attack") 1824 } 1825 } 1826 if externTd == nil { 1827 externTd = bc.GetTd(block.ParentHash(), block.NumberU64()-1) 1828 } 1829 externTd = new(big.Int).Add(externTd, block.Difficulty()) 1830 1831 if !bc.HasBlock(block.Hash(), block.NumberU64()) { 1832 start := time.Now() 1833 if err := bc.writeBlockWithoutState(block, externTd); err != nil { 1834 return it.index, err 1835 } 1836 log.Debug("Injected sidechain block", "number", block.Number(), "hash", block.Hash(), 1837 "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)), 1838 "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()), 1839 "root", block.Root()) 1840 } 1841 lastBlock = block 1842 } 1843 // At this point, we've written all sidechain blocks to database. Loop ended 1844 // either on some other error or all were processed. If there was some other 1845 // error, we can ignore the rest of those blocks. 1846 // 1847 // If the externTd was larger than our local TD, we now need to reimport the previous 1848 // blocks to regenerate the required state 1849 reorg, err := bc.forker.ReorgNeeded(current.Header(), lastBlock.Header()) 1850 if err != nil { 1851 return it.index, err 1852 } 1853 if !reorg { 1854 localTd := bc.GetTd(current.Hash(), current.NumberU64()) 1855 log.Info("Sidechain written to disk", "start", it.first().NumberU64(), "end", it.previous().Number, "sidetd", externTd, "localtd", localTd) 1856 return it.index, err 1857 } 1858 // Gather all the sidechain hashes (full blocks may be memory heavy) 1859 var ( 1860 hashes []common.Hash 1861 numbers []uint64 1862 ) 1863 parent := it.previous() 1864 for parent != nil && !bc.HasState(parent.Root) { 1865 hashes = append(hashes, parent.Hash()) 1866 numbers = append(numbers, parent.Number.Uint64()) 1867 1868 parent = bc.GetHeader(parent.ParentHash, parent.Number.Uint64()-1) 1869 } 1870 if parent == nil { 1871 return it.index, errors.New("missing parent") 1872 } 1873 // Import all the pruned blocks to make the state available 1874 var ( 1875 blocks []*types.Block 1876 memory common.StorageSize 1877 ) 1878 for i := len(hashes) - 1; i >= 0; i-- { 1879 // Append the next block to our batch 1880 block := bc.GetBlock(hashes[i], numbers[i]) 1881 1882 blocks = append(blocks, block) 1883 memory += block.Size() 1884 1885 // If memory use grew too large, import and continue. Sadly we need to discard 1886 // all raised events and logs from notifications since we're too heavy on the 1887 // memory here. 1888 if len(blocks) >= 2048 || memory > 64*1024*1024 { 1889 log.Info("Importing heavy sidechain segment", "blocks", len(blocks), "start", blocks[0].NumberU64(), "end", block.NumberU64()) 1890 if _, err := bc.insertChain(blocks, false, true); err != nil { 1891 return 0, err 1892 } 1893 blocks, memory = blocks[:0], 0 1894 1895 // If the chain is terminating, stop processing blocks 1896 if bc.insertStopped() { 1897 log.Debug("Abort during blocks processing") 1898 return 0, nil 1899 } 1900 } 1901 } 1902 if len(blocks) > 0 { 1903 log.Info("Importing sidechain segment", "start", blocks[0].NumberU64(), "end", blocks[len(blocks)-1].NumberU64()) 1904 return bc.insertChain(blocks, false, true) 1905 } 1906 return 0, nil 1907 } 1908 1909 // recoverAncestors finds the closest ancestor with available state and re-execute 1910 // all the ancestor blocks since that. 1911 // recoverAncestors is only used post-merge. 1912 // We return the hash of the latest block that we could correctly validate. 1913 func (bc *BlockChain) recoverAncestors(block *types.Block) (common.Hash, error) { 1914 // Gather all the sidechain hashes (full blocks may be memory heavy) 1915 var ( 1916 hashes []common.Hash 1917 numbers []uint64 1918 parent = block 1919 ) 1920 for parent != nil && !bc.HasState(parent.Root()) { 1921 hashes = append(hashes, parent.Hash()) 1922 numbers = append(numbers, parent.NumberU64()) 1923 parent = bc.GetBlock(parent.ParentHash(), parent.NumberU64()-1) 1924 1925 // If the chain is terminating, stop iteration 1926 if bc.insertStopped() { 1927 log.Debug("Abort during blocks iteration") 1928 return common.Hash{}, errInsertionInterrupted 1929 } 1930 } 1931 if parent == nil { 1932 return common.Hash{}, errors.New("missing parent") 1933 } 1934 // Import all the pruned blocks to make the state available 1935 for i := len(hashes) - 1; i >= 0; i-- { 1936 // If the chain is terminating, stop processing blocks 1937 if bc.insertStopped() { 1938 log.Debug("Abort during blocks processing") 1939 return common.Hash{}, errInsertionInterrupted 1940 } 1941 var b *types.Block 1942 if i == 0 { 1943 b = block 1944 } else { 1945 b = bc.GetBlock(hashes[i], numbers[i]) 1946 } 1947 if _, err := bc.insertChain(types.Blocks{b}, false, false); err != nil { 1948 return b.ParentHash(), err 1949 } 1950 } 1951 return block.Hash(), nil 1952 } 1953 1954 // collectLogs collects the logs that were generated or removed during 1955 // the processing of the block that corresponds with the given hash. 1956 // These logs are later announced as deleted or reborn. 1957 func (bc *BlockChain) collectLogs(hash common.Hash, removed bool) []*types.Log { 1958 number := bc.hc.GetBlockNumber(hash) 1959 if number == nil { 1960 return nil 1961 } 1962 receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig) 1963 1964 var logs []*types.Log 1965 for _, receipt := range receipts { 1966 for _, log := range receipt.Logs { 1967 l := *log 1968 if removed { 1969 l.Removed = true 1970 } 1971 logs = append(logs, &l) 1972 } 1973 } 1974 return logs 1975 } 1976 1977 // mergeLogs returns a merged log slice with specified sort order. 1978 func mergeLogs(logs [][]*types.Log, reverse bool) []*types.Log { 1979 var ret []*types.Log 1980 if reverse { 1981 for i := len(logs) - 1; i >= 0; i-- { 1982 ret = append(ret, logs[i]...) 1983 } 1984 } else { 1985 for i := 0; i < len(logs); i++ { 1986 ret = append(ret, logs[i]...) 1987 } 1988 } 1989 return ret 1990 } 1991 1992 // reorg takes two blocks, an old chain and a new chain and will reconstruct the 1993 // blocks and inserts them to be part of the new canonical chain and accumulates 1994 // potential missing transactions and post an event about them. 1995 // Note the new head block won't be processed here, callers need to handle it 1996 // externally. 1997 func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { 1998 var ( 1999 newChain types.Blocks 2000 oldChain types.Blocks 2001 commonBlock *types.Block 2002 2003 deletedTxs []common.Hash 2004 addedTxs []common.Hash 2005 2006 deletedLogs [][]*types.Log 2007 rebirthLogs [][]*types.Log 2008 ) 2009 // Reduce the longer chain to the same number as the shorter one 2010 if oldBlock.NumberU64() > newBlock.NumberU64() { 2011 // Old chain is longer, gather all transactions and logs as deleted ones 2012 for ; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) { 2013 oldChain = append(oldChain, oldBlock) 2014 for _, tx := range oldBlock.Transactions() { 2015 deletedTxs = append(deletedTxs, tx.Hash()) 2016 } 2017 2018 // Collect deleted logs for notification 2019 logs := bc.collectLogs(oldBlock.Hash(), true) 2020 if len(logs) > 0 { 2021 deletedLogs = append(deletedLogs, logs) 2022 } 2023 } 2024 } else { 2025 // New chain is longer, stash all blocks away for subsequent insertion 2026 for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) { 2027 newChain = append(newChain, newBlock) 2028 } 2029 } 2030 if oldBlock == nil { 2031 return fmt.Errorf("invalid old chain") 2032 } 2033 if newBlock == nil { 2034 return fmt.Errorf("invalid new chain") 2035 } 2036 // Both sides of the reorg are at the same number, reduce both until the common 2037 // ancestor is found 2038 for { 2039 // If the common ancestor was found, bail out 2040 if oldBlock.Hash() == newBlock.Hash() { 2041 commonBlock = oldBlock 2042 break 2043 } 2044 // Remove an old block as well as stash away a new block 2045 oldChain = append(oldChain, oldBlock) 2046 for _, tx := range oldBlock.Transactions() { 2047 deletedTxs = append(deletedTxs, tx.Hash()) 2048 } 2049 2050 // Collect deleted logs for notification 2051 logs := bc.collectLogs(oldBlock.Hash(), true) 2052 if len(logs) > 0 { 2053 deletedLogs = append(deletedLogs, logs) 2054 } 2055 newChain = append(newChain, newBlock) 2056 2057 // Step back with both chains 2058 oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) 2059 if oldBlock == nil { 2060 return fmt.Errorf("invalid old chain") 2061 } 2062 newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) 2063 if newBlock == nil { 2064 return fmt.Errorf("invalid new chain") 2065 } 2066 } 2067 2068 // Ensure the user sees large reorgs 2069 if len(oldChain) > 0 && len(newChain) > 0 { 2070 logFn := log.Info 2071 msg := "Chain reorg detected" 2072 if len(oldChain) > 63 { 2073 msg = "Large chain reorg detected" 2074 logFn = log.Warn 2075 } 2076 logFn(msg, "number", commonBlock.Number(), "hash", commonBlock.Hash(), 2077 "drop", len(oldChain), "dropfrom", oldChain[0].Hash(), "add", len(newChain), "addfrom", newChain[0].Hash()) 2078 blockReorgAddMeter.Mark(int64(len(newChain))) 2079 blockReorgDropMeter.Mark(int64(len(oldChain))) 2080 blockReorgMeter.Mark(1) 2081 } else if len(newChain) > 0 { 2082 // Special case happens in the post merge stage that current head is 2083 // the ancestor of new head while these two blocks are not consecutive 2084 log.Info("Extend chain", "add", len(newChain), "number", newChain[0].Number(), "hash", newChain[0].Hash()) 2085 blockReorgAddMeter.Mark(int64(len(newChain))) 2086 } else { 2087 // len(newChain) == 0 && len(oldChain) > 0 2088 // rewind the canonical chain to a lower point. 2089 log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "oldblocks", len(oldChain), "newnum", newBlock.Number(), "newhash", newBlock.Hash(), "newblocks", len(newChain)) 2090 } 2091 // Insert the new chain(except the head block(reverse order)), 2092 // taking care of the proper incremental order. 2093 for i := len(newChain) - 1; i >= 1; i-- { 2094 // Insert the block in the canonical way, re-writing history 2095 bc.writeHeadBlock(newChain[i]) 2096 2097 // Collect the new added transactions. 2098 for _, tx := range newChain[i].Transactions() { 2099 addedTxs = append(addedTxs, tx.Hash()) 2100 } 2101 } 2102 2103 // Delete useless indexes right now which includes the non-canonical 2104 // transaction indexes, canonical chain indexes which above the head. 2105 indexesBatch := bc.db.NewBatch() 2106 for _, tx := range types.HashDifference(deletedTxs, addedTxs) { 2107 rawdb.DeleteTxLookupEntry(indexesBatch, tx) 2108 } 2109 2110 // Delete all hash markers that are not part of the new canonical chain. 2111 // Because the reorg function does not handle new chain head, all hash 2112 // markers greater than or equal to new chain head should be deleted. 2113 number := commonBlock.NumberU64() 2114 if len(newChain) > 1 { 2115 number = newChain[1].NumberU64() 2116 } 2117 for i := number + 1; ; i++ { 2118 hash := rawdb.ReadCanonicalHash(bc.db, i) 2119 if hash == (common.Hash{}) { 2120 break 2121 } 2122 rawdb.DeleteCanonicalHash(indexesBatch, i) 2123 } 2124 if err := indexesBatch.Write(); err != nil { 2125 log.Crit("Failed to delete useless indexes", "err", err) 2126 } 2127 2128 // Collect the logs 2129 for i := len(newChain) - 1; i >= 1; i-- { 2130 // Collect reborn logs due to chain reorg 2131 logs := bc.collectLogs(newChain[i].Hash(), false) 2132 if len(logs) > 0 { 2133 rebirthLogs = append(rebirthLogs, logs) 2134 } 2135 } 2136 // If any logs need to be fired, do it now. In theory we could avoid creating 2137 // this goroutine if there are no events to fire, but realistcally that only 2138 // ever happens if we're reorging empty blocks, which will only happen on idle 2139 // networks where performance is not an issue either way. 2140 if len(deletedLogs) > 0 { 2141 bc.rmLogsFeed.Send(RemovedLogsEvent{mergeLogs(deletedLogs, true)}) 2142 } 2143 if len(rebirthLogs) > 0 { 2144 bc.logsFeed.Send(mergeLogs(rebirthLogs, false)) 2145 } 2146 if len(oldChain) > 0 { 2147 for i := len(oldChain) - 1; i >= 0; i-- { 2148 bc.chainSideFeed.Send(ChainSideEvent{Block: oldChain[i]}) 2149 } 2150 } 2151 return nil 2152 } 2153 2154 // InsertBlockWithoutSetHead executes the block, runs the necessary verification 2155 // upon it and then persist the block and the associate state into the database. 2156 // The key difference between the InsertChain is it won't do the canonical chain 2157 // updating. It relies on the additional SetCanonical call to finalize the entire 2158 // procedure. 2159 func (bc *BlockChain) InsertBlockWithoutSetHead(block *types.Block) error { 2160 if !bc.chainmu.TryLock() { 2161 return errChainStopped 2162 } 2163 defer bc.chainmu.Unlock() 2164 2165 _, err := bc.insertChain(types.Blocks{block}, true, false) 2166 return err 2167 } 2168 2169 // SetCanonical rewinds the chain to set the new head block as the specified 2170 // block. It's possible that the state of the new head is missing, and it will 2171 // be recovered in this function as well. 2172 func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) { 2173 if !bc.chainmu.TryLock() { 2174 return common.Hash{}, errChainStopped 2175 } 2176 defer bc.chainmu.Unlock() 2177 2178 // Re-execute the reorged chain in case the head state is missing. 2179 if !bc.HasState(head.Root()) { 2180 if latestValidHash, err := bc.recoverAncestors(head); err != nil { 2181 return latestValidHash, err 2182 } 2183 log.Info("Recovered head state", "number", head.Number(), "hash", head.Hash()) 2184 } 2185 // Run the reorg if necessary and set the given block as new head. 2186 start := time.Now() 2187 if head.ParentHash() != bc.CurrentBlock().Hash() { 2188 if err := bc.reorg(bc.CurrentBlock(), head); err != nil { 2189 return common.Hash{}, err 2190 } 2191 } 2192 bc.writeHeadBlock(head) 2193 2194 // Emit events 2195 logs := bc.collectLogs(head.Hash(), false) 2196 bc.chainFeed.Send(ChainEvent{Block: head, Hash: head.Hash(), Logs: logs}) 2197 if len(logs) > 0 { 2198 bc.logsFeed.Send(logs) 2199 } 2200 bc.chainHeadFeed.Send(ChainHeadEvent{Block: head}) 2201 2202 context := []interface{}{ 2203 "number", head.Number(), 2204 "hash", head.Hash(), 2205 "root", head.Root(), 2206 "elapsed", time.Since(start), 2207 } 2208 if timestamp := time.Unix(int64(head.Time()), 0); time.Since(timestamp) > time.Minute { 2209 context = append(context, []interface{}{"age", common.PrettyAge(timestamp)}...) 2210 } 2211 log.Info("Chain head was updated", context...) 2212 return head.Hash(), nil 2213 } 2214 2215 func (bc *BlockChain) updateFutureBlocks() { 2216 futureTimer := time.NewTicker(5 * time.Second) 2217 defer futureTimer.Stop() 2218 defer bc.wg.Done() 2219 for { 2220 select { 2221 case <-futureTimer.C: 2222 bc.procFutureBlocks() 2223 case <-bc.quit: 2224 return 2225 } 2226 } 2227 } 2228 2229 // skipBlock returns 'true', if the block being imported can be skipped over, meaning 2230 // that the block does not need to be processed but can be considered already fully 'done'. 2231 func (bc *BlockChain) skipBlock(err error, it *insertIterator) bool { 2232 // We can only ever bypass processing if the only error returned by the validator 2233 // is ErrKnownBlock, which means all checks passed, but we already have the block 2234 // and state. 2235 if !errors.Is(err, ErrKnownBlock) { 2236 return false 2237 } 2238 // If we're not using snapshots, we can skip this, since we have both block 2239 // and (trie-) state 2240 if bc.snaps == nil { 2241 return true 2242 } 2243 var ( 2244 header = it.current() // header can't be nil 2245 parentRoot common.Hash 2246 ) 2247 // If we also have the snapshot-state, we can skip the processing. 2248 if bc.snaps.Snapshot(header.Root) != nil { 2249 return true 2250 } 2251 // In this case, we have the trie-state but not snapshot-state. If the parent 2252 // snapshot-state exists, we need to process this in order to not get a gap 2253 // in the snapshot layers. 2254 // Resolve parent block 2255 if parent := it.previous(); parent != nil { 2256 parentRoot = parent.Root 2257 } else if parent = bc.GetHeaderByHash(header.ParentHash); parent != nil { 2258 parentRoot = parent.Root 2259 } 2260 if parentRoot == (common.Hash{}) { 2261 return false // Theoretically impossible case 2262 } 2263 // Parent is also missing snapshot: we can skip this. Otherwise process. 2264 if bc.snaps.Snapshot(parentRoot) == nil { 2265 return true 2266 } 2267 return false 2268 } 2269 2270 // maintainTxIndex is responsible for the construction and deletion of the 2271 // transaction index. 2272 // 2273 // User can use flag `txlookuplimit` to specify a "recentness" block, below 2274 // which ancient tx indices get deleted. If `txlookuplimit` is 0, it means 2275 // all tx indices will be reserved. 2276 // 2277 // The user can adjust the txlookuplimit value for each launch after fast 2278 // sync, Geth will automatically construct the missing indices and delete 2279 // the extra indices. 2280 func (bc *BlockChain) maintainTxIndex(ancients uint64) { 2281 defer bc.wg.Done() 2282 2283 // Before starting the actual maintenance, we need to handle a special case, 2284 // where user might init Geth with an external ancient database. If so, we 2285 // need to reindex all necessary transactions before starting to process any 2286 // pruning requests. 2287 if ancients > 0 { 2288 var from = uint64(0) 2289 if bc.txLookupLimit != 0 && ancients > bc.txLookupLimit { 2290 from = ancients - bc.txLookupLimit 2291 } 2292 rawdb.IndexTransactions(bc.db, from, ancients, bc.quit) 2293 } 2294 2295 // indexBlocks reindexes or unindexes transactions depending on user configuration 2296 indexBlocks := func(tail *uint64, head uint64, done chan struct{}) { 2297 defer func() { done <- struct{}{} }() 2298 2299 // If the user just upgraded Geth to a new version which supports transaction 2300 // index pruning, write the new tail and remove anything older. 2301 if tail == nil { 2302 if bc.txLookupLimit == 0 || head < bc.txLookupLimit { 2303 // Nothing to delete, write the tail and return 2304 rawdb.WriteTxIndexTail(bc.db, 0) 2305 } else { 2306 // Prune all stale tx indices and record the tx index tail 2307 rawdb.UnindexTransactions(bc.db, 0, head-bc.txLookupLimit+1, bc.quit) 2308 } 2309 return 2310 } 2311 // If a previous indexing existed, make sure that we fill in any missing entries 2312 if bc.txLookupLimit == 0 || head < bc.txLookupLimit { 2313 if *tail > 0 { 2314 // It can happen when chain is rewound to a historical point which 2315 // is even lower than the indexes tail, recap the indexing target 2316 // to new head to avoid reading non-existent block bodies. 2317 end := *tail 2318 if end > head+1 { 2319 end = head + 1 2320 } 2321 rawdb.IndexTransactions(bc.db, 0, end, bc.quit) 2322 } 2323 return 2324 } 2325 // Update the transaction index to the new chain state 2326 if head-bc.txLookupLimit+1 < *tail { 2327 // Reindex a part of missing indices and rewind index tail to HEAD-limit 2328 rawdb.IndexTransactions(bc.db, head-bc.txLookupLimit+1, *tail, bc.quit) 2329 } else { 2330 // Unindex a part of stale indices and forward index tail to HEAD-limit 2331 rawdb.UnindexTransactions(bc.db, *tail, head-bc.txLookupLimit+1, bc.quit) 2332 } 2333 } 2334 2335 // Any reindexing done, start listening to chain events and moving the index window 2336 var ( 2337 done chan struct{} // Non-nil if background unindexing or reindexing routine is active. 2338 headCh = make(chan ChainHeadEvent, 1) // Buffered to avoid locking up the event feed 2339 ) 2340 sub := bc.SubscribeChainHeadEvent(headCh) 2341 if sub == nil { 2342 return 2343 } 2344 defer sub.Unsubscribe() 2345 2346 for { 2347 select { 2348 case head := <-headCh: 2349 if done == nil { 2350 done = make(chan struct{}) 2351 go indexBlocks(rawdb.ReadTxIndexTail(bc.db), head.Block.NumberU64(), done) 2352 } 2353 case <-done: 2354 done = nil 2355 case <-bc.quit: 2356 if done != nil { 2357 log.Info("Waiting background transaction indexer to exit") 2358 <-done 2359 } 2360 return 2361 } 2362 } 2363 } 2364 2365 // reportBlock logs a bad block error. 2366 func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, err error) { 2367 rawdb.WriteBadBlock(bc.db, block) 2368 2369 var receiptString string 2370 for i, receipt := range receipts { 2371 receiptString += fmt.Sprintf("\t %d: cumulative: %v gas: %v contract: %v status: %v tx: %v logs: %v bloom: %x state: %x\n", 2372 i, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.ContractAddress.Hex(), 2373 receipt.Status, receipt.TxHash.Hex(), receipt.Logs, receipt.Bloom, receipt.PostState) 2374 } 2375 log.Error(fmt.Sprintf(` 2376 ########## BAD BLOCK ######### 2377 Chain config: %v 2378 2379 Number: %v 2380 Hash: %#x 2381 %v 2382 2383 Error: %v 2384 ############################## 2385 `, bc.chainConfig, block.Number(), block.Hash(), receiptString, err)) 2386 } 2387 2388 // InsertHeaderChain attempts to insert the given header chain in to the local 2389 // chain, possibly creating a reorg. If an error is returned, it will return the 2390 // index number of the failing header as well an error describing what went wrong. 2391 // 2392 // The verify parameter can be used to fine tune whether nonce verification 2393 // should be done or not. The reason behind the optional check is because some 2394 // of the header retrieval mechanisms already need to verify nonces, as well as 2395 // because nonces can be verified sparsely, not needing to check each. 2396 func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { 2397 if len(chain) == 0 { 2398 return 0, nil 2399 } 2400 start := time.Now() 2401 if i, err := bc.hc.ValidateHeaderChain(chain, checkFreq); err != nil { 2402 return i, err 2403 } 2404 2405 if !bc.chainmu.TryLock() { 2406 return 0, errChainStopped 2407 } 2408 defer bc.chainmu.Unlock() 2409 _, err := bc.hc.InsertHeaderChain(chain, start, bc.forker) 2410 return 0, err 2411 } 2412 2413 // SetBlockValidatorAndProcessorForTesting sets the current validator and processor. 2414 // This method can be used to force an invalid blockchain to be verified for tests. 2415 // This method is unsafe and should only be used before block import starts. 2416 func (bc *BlockChain) SetBlockValidatorAndProcessorForTesting(v Validator, p Processor) { 2417 bc.validator = v 2418 bc.processor = p 2419 }