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