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