gitlab.com/flarenetwork/coreth@v0.1.1/core/blockchain.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2014 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 // Package core implements the Ethereum consensus protocol. 28 package core 29 30 import ( 31 "errors" 32 "fmt" 33 "io" 34 "math/big" 35 "sync" 36 "sync/atomic" 37 "time" 38 39 "github.com/ethereum/go-ethereum/common" 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/trie" 44 lru "github.com/hashicorp/golang-lru" 45 "gitlab.com/flarenetwork/coreth/consensus" 46 "gitlab.com/flarenetwork/coreth/core/rawdb" 47 "gitlab.com/flarenetwork/coreth/core/state" 48 "gitlab.com/flarenetwork/coreth/core/state/snapshot" 49 "gitlab.com/flarenetwork/coreth/core/types" 50 "gitlab.com/flarenetwork/coreth/core/vm" 51 "gitlab.com/flarenetwork/coreth/params" 52 ) 53 54 var ( 55 removeTxIndicesKey = []byte("removed_tx_indices") 56 57 errFutureBlockUnsupported = errors.New("future block insertion not supported") 58 errCacheConfigNotSpecified = errors.New("must specify cache config") 59 ) 60 61 const ( 62 bodyCacheLimit = 256 63 blockCacheLimit = 256 64 receiptsCacheLimit = 32 65 txLookupCacheLimit = 1024 66 badBlockLimit = 10 67 TriesInMemory = 128 68 69 // BlockChainVersion ensures that an incompatible database forces a resync from scratch. 70 // 71 // Changelog: 72 // 73 // - Version 4 74 // The following incompatible database changes were added: 75 // * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted 76 // * the `Bloom` field of receipt is deleted 77 // * the `BlockIndex` and `TxIndex` fields of txlookup are deleted 78 // - Version 5 79 // The following incompatible database changes were added: 80 // * the `TxHash`, `GasCost`, and `ContractAddress` fields are no longer stored for a receipt 81 // * the `TxHash`, `GasCost`, and `ContractAddress` fields are computed by looking up the 82 // receipts' corresponding block 83 // - Version 6 84 // The following incompatible database changes were added: 85 // * Transaction lookup information stores the corresponding block number instead of block hash 86 // - Version 7 87 // The following incompatible database changes were added: 88 // * Use freezer as the ancient database to maintain all ancient data 89 // - Version 8 90 // The following incompatible database changes were added: 91 // * New scheme for contract code in order to separate the codes and trie nodes 92 BlockChainVersion uint64 = 8 93 94 // statsReportLimit is the time limit during import and export after which we 95 // always print out progress. This avoids the user wondering what's going on. 96 statsReportLimit = 8 * time.Second 97 ) 98 99 // CacheConfig contains the configuration values for the trie caching/pruning 100 // that's resident in a blockchain. 101 type CacheConfig struct { 102 TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory 103 TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk 104 Pruning bool // Whether to disable trie write caching and GC altogether (archive node) 105 SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory 106 SnapshotAsync bool // Generate snapshot tree async 107 SnapshotVerify bool // Verify generated snapshots 108 Preimages bool // Whether to store preimage of trie key to the disk 109 } 110 111 var DefaultCacheConfig = &CacheConfig{ 112 TrieCleanLimit: 256, 113 TrieDirtyLimit: 256, 114 SnapshotLimit: 256, 115 } 116 117 // BlockChain represents the canonical chain given a database with a genesis 118 // block. The Blockchain manages chain imports, reverts, chain reorganisations. 119 // 120 // Importing blocks in to the block chain happens according to the set of rules 121 // defined by the two stage Validator. Processing of blocks is done using the 122 // Processor which processes the included transaction. The validation of the state 123 // is done in the second part of the Validator. Failing results in aborting of 124 // the import. 125 // 126 // The BlockChain also helps in returning blocks from **any** chain included 127 // in the database as well as blocks that represents the canonical chain. It's 128 // important to note that GetBlock can return any block and does not need to be 129 // included in the canonical one where as GetBlockByNumber always represents the 130 // canonical chain. 131 type BlockChain struct { 132 chainConfig *params.ChainConfig // Chain & network configuration 133 cacheConfig *CacheConfig // Cache configuration for pruning 134 135 db ethdb.Database // Low level persistent database to store final content in 136 137 snaps *snapshot.Tree // Snapshot tree for fast trie leaf access 138 139 hc *HeaderChain 140 rmLogsFeed event.Feed 141 chainFeed event.Feed 142 chainSideFeed event.Feed 143 chainHeadFeed event.Feed 144 chainAcceptedFeed event.Feed 145 logsFeed event.Feed 146 logsAcceptedFeed event.Feed 147 blockProcFeed event.Feed 148 txAcceptedFeed event.Feed 149 scope event.SubscriptionScope 150 genesisBlock *types.Block 151 152 chainmu sync.RWMutex // blockchain insertion lock 153 154 currentBlock atomic.Value // Current head of the block chain 155 156 stateCache state.Database // State database to reuse between imports (contains state cache) 157 stateManager TrieWriter 158 bodyCache *lru.Cache // Cache for the most recent block bodies 159 receiptsCache *lru.Cache // Cache for the most recent receipts per block 160 blockCache *lru.Cache // Cache for the most recent entire blocks 161 txLookupCache *lru.Cache // Cache for the most recent transaction lookup data. 162 163 quit chan struct{} // blockchain quit channel 164 wg sync.WaitGroup // chain processing wait group for shutting down 165 running int32 // 0 if chain is running, 1 when stopped 166 167 engine consensus.Engine 168 validator Validator // Block and state validator interface 169 prefetcher Prefetcher // Block state prefetcher interface 170 processor Processor // Block transaction processor interface 171 vmConfig vm.Config 172 173 badBlocks *lru.Cache // Bad block cache 174 175 lastAccepted *types.Block // Prevents reorgs past this height 176 } 177 178 // NewBlockChain returns a fully initialised block chain using information 179 // available in the database. It initialises the default Ethereum Validator and 180 // Processor. 181 func NewBlockChain( 182 db ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, 183 vmConfig vm.Config, lastAcceptedHash common.Hash, 184 ) (*BlockChain, error) { 185 if cacheConfig == nil { 186 return nil, errCacheConfigNotSpecified 187 } 188 bodyCache, _ := lru.New(bodyCacheLimit) 189 receiptsCache, _ := lru.New(receiptsCacheLimit) 190 blockCache, _ := lru.New(blockCacheLimit) 191 txLookupCache, _ := lru.New(txLookupCacheLimit) 192 badBlocks, _ := lru.New(badBlockLimit) 193 194 bc := &BlockChain{ 195 chainConfig: chainConfig, 196 cacheConfig: cacheConfig, 197 db: db, 198 stateCache: state.NewDatabaseWithConfig(db, &trie.Config{ 199 Cache: cacheConfig.TrieCleanLimit, 200 Preimages: cacheConfig.Preimages, 201 }), 202 quit: make(chan struct{}), 203 bodyCache: bodyCache, 204 receiptsCache: receiptsCache, 205 blockCache: blockCache, 206 txLookupCache: txLookupCache, 207 engine: engine, 208 vmConfig: vmConfig, 209 badBlocks: badBlocks, 210 } 211 bc.validator = NewBlockValidator(chainConfig, bc, engine) 212 bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine) 213 bc.processor = NewStateProcessor(chainConfig, bc, engine) 214 215 var err error 216 bc.hc, err = NewHeaderChain(db, chainConfig, engine) 217 if err != nil { 218 return nil, err 219 } 220 bc.genesisBlock = bc.GetBlockByNumber(0) 221 if bc.genesisBlock == nil { 222 return nil, ErrNoGenesis 223 } 224 225 var nilBlock *types.Block 226 bc.currentBlock.Store(nilBlock) 227 228 if err := bc.loadLastState(lastAcceptedHash); err != nil { 229 return nil, err 230 } 231 // Create the state manager 232 bc.stateManager = NewTrieWriter(bc.stateCache.TrieDB(), cacheConfig) 233 234 // Make sure the state associated with the block is available 235 head := bc.CurrentBlock() 236 if _, err := state.New(head.Root(), bc.stateCache, nil); err != nil { 237 return nil, fmt.Errorf("head state missing %d:%s", head.Number(), head.Hash()) 238 } 239 240 // Load any existing snapshot, regenerating it if loading failed 241 if bc.cacheConfig.SnapshotLimit > 0 { 242 // If we are starting from genesis, generate the original snapshot disk layer 243 // up front, so we can use it while executing blocks in bootstrapping. This 244 // also avoids a costly async generation process when reaching tip. 245 async := bc.cacheConfig.SnapshotAsync && head.NumberU64() > 0 246 log.Info("Initializing snapshots", "async", async) 247 bc.snaps, err = snapshot.New(bc.db, bc.stateCache.TrieDB(), bc.cacheConfig.SnapshotLimit, head.Hash(), head.Root(), async, true, bc.cacheConfig.SnapshotVerify) 248 if err != nil { 249 log.Error("failed to initialize snapshots", "headHash", head.Hash(), "headRoot", head.Root(), "err", err, "async", async) 250 } 251 } 252 253 return bc, nil 254 } 255 256 // GetVMConfig returns the block chain VM config. 257 func (bc *BlockChain) GetVMConfig() *vm.Config { 258 return &bc.vmConfig 259 } 260 261 // empty returns an indicator whether the blockchain is empty. 262 // Note, it's a special case that we connect a non-empty ancient 263 // database with an empty node, so that we can plugin the ancient 264 // into node seamlessly. 265 func (bc *BlockChain) empty() bool { 266 genesis := bc.genesisBlock.Hash() 267 for _, hash := range []common.Hash{rawdb.ReadHeadBlockHash(bc.db), rawdb.ReadHeadHeaderHash(bc.db), rawdb.ReadHeadFastBlockHash(bc.db)} { 268 if hash != genesis { 269 return false 270 } 271 } 272 return true 273 } 274 275 // loadLastState loads the last known chain state from the database. This method 276 // assumes that the chain manager mutex is held. 277 func (bc *BlockChain) loadLastState(lastAcceptedHash common.Hash) error { 278 // Initialize genesis state 279 if lastAcceptedHash == (common.Hash{}) { 280 return bc.loadGenesisState() 281 } 282 283 // Restore the last known head block 284 head := rawdb.ReadHeadBlockHash(bc.db) 285 if head == (common.Hash{}) { 286 return errors.New("could not read head block hash") 287 } 288 // Make sure the entire head block is available 289 currentBlock := bc.GetBlockByHash(head) 290 if currentBlock == nil { 291 return fmt.Errorf("could not load head block %s", head.Hex()) 292 } 293 // Everything seems to be fine, set as the head block 294 bc.currentBlock.Store(currentBlock) 295 296 // Restore the last known head header 297 currentHeader := currentBlock.Header() 298 if head := rawdb.ReadHeadHeaderHash(bc.db); head != (common.Hash{}) { 299 if header := bc.GetHeaderByHash(head); header != nil { 300 currentHeader = header 301 } 302 } 303 bc.hc.SetCurrentHeader(currentHeader) 304 305 headerTd := bc.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64()) 306 blockTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) 307 308 log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd, "age", common.PrettyAge(time.Unix(int64(currentHeader.Time), 0))) 309 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))) 310 311 // Otherwise, set the last accepted block and perform a re-org. 312 bc.lastAccepted = bc.GetBlockByHash(lastAcceptedHash) 313 if bc.lastAccepted == nil { 314 return fmt.Errorf("could not load last accepted block") 315 } 316 317 // Remove all processing transaction indices leftover from when we used to 318 // write transaction indices as soon as a block was verified. 319 indicesRemoved, err := bc.db.Has(removeTxIndicesKey) 320 if err != nil { 321 return fmt.Errorf("unable to determine if transaction indices removed: %w", err) 322 } 323 if !indicesRemoved { 324 indicesRemoved, err := bc.removeIndices(currentBlock.NumberU64(), bc.lastAccepted.NumberU64()) 325 if err != nil { 326 return err 327 } 328 if err := bc.db.Put(removeTxIndicesKey, bc.lastAccepted.Number().Bytes()); err != nil { 329 return fmt.Errorf("unable to mark indices removed: %w", err) 330 } 331 log.Debug("removed processing transaction indices", "count", indicesRemoved, "currentBlock", currentBlock.NumberU64(), "lastAccepted", bc.lastAccepted.NumberU64()) 332 } 333 334 // This ensures that the head block is updated to the last accepted block on startup 335 if err := bc.setPreference(bc.lastAccepted); err != nil { 336 return fmt.Errorf("failed to set preference to last accepted block while loading last state: %w", err) 337 } 338 339 // reprocessState as necessary to ensure that the last accepted state is 340 // available. The state may not be available if it was not committed due 341 // to an unclean shutdown. 342 return bc.reprocessState(bc.lastAccepted, 2*commitInterval, true) 343 } 344 345 // removeIndices removes all transaction lookup entries for the transactions contained in the canonical chain 346 // from block at height [to] to block at height [from]. Blocks are traversed in reverse order. 347 func (bc *BlockChain) removeIndices(from, to uint64) (int, error) { 348 indicesRemoved := 0 349 batch := bc.db.NewBatch() 350 for i := from; i > to; i-- { 351 b := bc.GetBlockByNumber(i) 352 if b == nil { 353 return indicesRemoved, fmt.Errorf("could not load canonical block at height %d", i) 354 } 355 for _, tx := range b.Transactions() { 356 rawdb.DeleteTxLookupEntry(batch, tx.Hash()) 357 indicesRemoved++ 358 } 359 } 360 if err := batch.Write(); err != nil { 361 return 0, fmt.Errorf("failed to write batch while removing indices (from: %d, to: %d): %w", from, to, err) 362 } 363 return indicesRemoved, nil 364 } 365 366 // GasLimit returns the gas limit of the current HEAD block. 367 func (bc *BlockChain) GasLimit() uint64 { 368 return bc.CurrentBlock().GasLimit() 369 } 370 371 // CurrentBlock retrieves the current head block of the canonical chain. The 372 // block is retrieved from the blockchain's internal cache. 373 func (bc *BlockChain) CurrentBlock() *types.Block { 374 return bc.currentBlock.Load().(*types.Block) 375 } 376 377 // Snapshots returns the blockchain snapshot tree. 378 func (bc *BlockChain) Snapshots() *snapshot.Tree { 379 return bc.snaps 380 } 381 382 // Validator returns the current validator. 383 func (bc *BlockChain) Validator() Validator { 384 return bc.validator 385 } 386 387 // Processor returns the current processor. 388 func (bc *BlockChain) Processor() Processor { 389 return bc.processor 390 } 391 392 // State returns a new mutable state based on the current HEAD block. 393 func (bc *BlockChain) State() (*state.StateDB, error) { 394 return bc.StateAt(bc.CurrentBlock().Root()) 395 } 396 397 // StateAt returns a new mutable state based on a particular point in time. 398 func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) { 399 return state.New(root, bc.stateCache, bc.snaps) 400 } 401 402 // StateCache returns the caching database underpinning the blockchain instance. 403 func (bc *BlockChain) StateCache() state.Database { 404 return bc.stateCache 405 } 406 407 func (bc *BlockChain) loadGenesisState() error { 408 // Prepare the genesis block and reinitialise the chain 409 batch := bc.db.NewBatch() 410 rawdb.WriteTd(batch, bc.genesisBlock.Hash(), bc.genesisBlock.NumberU64(), bc.genesisBlock.Difficulty()) 411 rawdb.WriteBlock(batch, bc.genesisBlock) 412 if err := batch.Write(); err != nil { 413 log.Crit("Failed to write genesis block", "err", err) 414 } 415 bc.writeHeadBlock(bc.genesisBlock) 416 417 // Last update all in-memory chain markers 418 bc.lastAccepted = bc.genesisBlock 419 bc.currentBlock.Store(bc.genesisBlock) 420 bc.hc.SetGenesis(bc.genesisBlock.Header()) 421 bc.hc.SetCurrentHeader(bc.genesisBlock.Header()) 422 return nil 423 } 424 425 // Export writes the active chain to the given writer. 426 func (bc *BlockChain) Export(w io.Writer) error { 427 return bc.ExportN(w, uint64(0), bc.CurrentBlock().NumberU64()) 428 } 429 430 // ExportN writes a subset of the active chain to the given writer. 431 func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error { 432 bc.chainmu.RLock() 433 defer bc.chainmu.RUnlock() 434 435 if first > last { 436 return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last) 437 } 438 log.Info("Exporting batch of blocks", "count", last-first+1) 439 440 start, reported := time.Now(), time.Now() 441 for nr := first; nr <= last; nr++ { 442 block := bc.GetBlockByNumber(nr) 443 if block == nil { 444 return fmt.Errorf("export failed on #%d: not found", nr) 445 } 446 if err := block.EncodeRLP(w); err != nil { 447 return err 448 } 449 if time.Since(reported) >= statsReportLimit { 450 log.Info("Exporting blocks", "exported", block.NumberU64()-first, "elapsed", common.PrettyDuration(time.Since(start))) 451 reported = time.Now() 452 } 453 } 454 return nil 455 } 456 457 // writeHeadBlock injects a new head block into the current block chain. This method 458 // assumes that the block is indeed a true head. It will also reset the head 459 // header and the head fast sync block to this very same block if they are older 460 // or if they are on a different side chain. 461 // 462 // Note, this function assumes that the `mu` mutex is held! 463 func (bc *BlockChain) writeHeadBlock(block *types.Block) { 464 // If the block is on a side chain or an unknown one, force other heads onto it too 465 updateHeads := rawdb.ReadCanonicalHash(bc.db, block.NumberU64()) != block.Hash() 466 467 // Add the block to the canonical chain number scheme and mark as the head 468 batch := bc.db.NewBatch() 469 rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64()) 470 rawdb.WriteHeadBlockHash(batch, block.Hash()) 471 472 // If the block is better than our head or is on a different chain, force update heads 473 if updateHeads { 474 rawdb.WriteHeadHeaderHash(batch, block.Hash()) 475 rawdb.WriteHeadFastBlockHash(batch, block.Hash()) 476 } 477 // Flush the whole batch into the disk, exit the node if failed 478 if err := batch.Write(); err != nil { 479 log.Crit("Failed to update chain indexes and markers", "err", err) 480 } 481 // Update all in-memory chain markers in the last step 482 if updateHeads { 483 bc.hc.SetCurrentHeader(block.Header()) 484 } 485 bc.currentBlock.Store(block) 486 } 487 488 // Genesis retrieves the chain's genesis block. 489 func (bc *BlockChain) Genesis() *types.Block { 490 return bc.genesisBlock 491 } 492 493 // GetBody retrieves a block body (transactions and uncles) from the database by 494 // hash, caching it if found. 495 func (bc *BlockChain) GetBody(hash common.Hash) *types.Body { 496 // Short circuit if the body's already in the cache, retrieve otherwise 497 if cached, ok := bc.bodyCache.Get(hash); ok { 498 body := cached.(*types.Body) 499 return body 500 } 501 number := bc.hc.GetBlockNumber(hash) 502 if number == nil { 503 return nil 504 } 505 body := rawdb.ReadBody(bc.db, hash, *number) 506 if body == nil { 507 return nil 508 } 509 // Cache the found body for next time and return 510 bc.bodyCache.Add(hash, body) 511 return body 512 } 513 514 // HasBlock checks if a block is fully present in the database or not. 515 func (bc *BlockChain) HasBlock(hash common.Hash, number uint64) bool { 516 if bc.blockCache.Contains(hash) { 517 return true 518 } 519 return rawdb.HasBody(bc.db, hash, number) 520 } 521 522 // HasState checks if state trie is fully present in the database or not. 523 func (bc *BlockChain) HasState(hash common.Hash) bool { 524 _, err := bc.stateCache.OpenTrie(hash) 525 return err == nil 526 } 527 528 // HasBlockAndState checks if a block and associated state trie is fully present 529 // in the database or not, caching it if present. 530 func (bc *BlockChain) HasBlockAndState(hash common.Hash, number uint64) bool { 531 // Check first that the block itself is known 532 block := bc.GetBlock(hash, number) 533 if block == nil { 534 return false 535 } 536 return bc.HasState(block.Root()) 537 } 538 539 // GetBlock retrieves a block from the database by hash and number, 540 // caching it if found. 541 func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { 542 // Short circuit if the block's already in the cache, retrieve otherwise 543 if block, ok := bc.blockCache.Get(hash); ok { 544 return block.(*types.Block) 545 } 546 block := rawdb.ReadBlock(bc.db, hash, number) 547 if block == nil { 548 return nil 549 } 550 // Cache the found block for next time and return 551 bc.blockCache.Add(block.Hash(), block) 552 return block 553 } 554 555 // GetBlockByHash retrieves a block from the database by hash, caching it if found. 556 func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { 557 number := bc.hc.GetBlockNumber(hash) 558 if number == nil { 559 return nil 560 } 561 return bc.GetBlock(hash, *number) 562 } 563 564 // GetBlockByNumber retrieves a block from the database by number, caching it 565 // (associated with its hash) if found. 566 func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block { 567 hash := rawdb.ReadCanonicalHash(bc.db, number) 568 if hash == (common.Hash{}) { 569 return nil 570 } 571 return bc.GetBlock(hash, number) 572 } 573 574 // ValidateCanonicalChain confirms a canonical chain is well-formed. 575 func (bc *BlockChain) ValidateCanonicalChain() error { 576 current := bc.CurrentBlock() 577 i := 0 578 log.Info("Beginning to validate canonical chain", "startBlock", current.NumberU64()) 579 580 for current.Hash() != bc.genesisBlock.Hash() { 581 blkByHash := bc.GetBlockByHash(current.Hash()) 582 if blkByHash == nil { 583 return fmt.Errorf("couldn't find block by hash %s at height %d", current.Hash().String(), current.Number()) 584 } 585 if blkByHash.Hash() != current.Hash() { 586 return fmt.Errorf("blockByHash returned a block with an unexpected hash: %s, expected: %s", blkByHash.Hash().String(), current.Hash().String()) 587 } 588 blkByNumber := bc.GetBlockByNumber(current.Number().Uint64()) 589 if blkByNumber == nil { 590 return fmt.Errorf("couldn't find block by number at height %d", current.Number()) 591 } 592 if blkByNumber.Hash() != current.Hash() { 593 return fmt.Errorf("blockByNumber returned a block with unexpected hash: %s, expected: %s", blkByNumber.Hash().String(), current.Hash().String()) 594 } 595 596 hdrByHash := bc.GetHeaderByHash(current.Hash()) 597 if hdrByHash == nil { 598 return fmt.Errorf("couldn't find block header by hash %s at height %d", current.Hash().String(), current.Number()) 599 } 600 if hdrByHash.Hash() != current.Hash() { 601 return fmt.Errorf("hdrByHash returned a block header with an unexpected hash: %s, expected: %s", hdrByHash.Hash().String(), current.Hash().String()) 602 } 603 hdrByNumber := bc.GetHeaderByNumber(current.Number().Uint64()) 604 if hdrByNumber == nil { 605 return fmt.Errorf("couldn't find block header by number at height %d", current.Number()) 606 } 607 if hdrByNumber.Hash() != current.Hash() { 608 return fmt.Errorf("hdrByNumber returned a block header with unexpected hash: %s, expected: %s", hdrByNumber.Hash().String(), current.Hash().String()) 609 } 610 611 txs := current.Body().Transactions 612 613 // Transactions are only indexed beneath the last accepted block, so we only check 614 // that the transactions have been indexed, if we are checking below the last accepted 615 // block. 616 if current.NumberU64() <= bc.lastAccepted.NumberU64() { 617 // Ensure that all of the transactions have been stored correctly in the canonical 618 // chain 619 for txIndex, tx := range txs { 620 txLookup := bc.GetTransactionLookup(tx.Hash()) 621 if txLookup == nil { 622 return fmt.Errorf("failed to find transaction %s", tx.Hash().String()) 623 } 624 if txLookup.BlockHash != current.Hash() { 625 return fmt.Errorf("tx lookup returned with incorrect block hash: %s, expected: %s", txLookup.BlockHash.String(), current.Hash().String()) 626 } 627 if txLookup.BlockIndex != current.Number().Uint64() { 628 return fmt.Errorf("tx lookup returned with incorrect block index: %d, expected: %d", txLookup.BlockIndex, current.Number().Uint64()) 629 } 630 if txLookup.Index != uint64(txIndex) { 631 return fmt.Errorf("tx lookup returned with incorrect transaction index: %d, expected: %d", txLookup.Index, txIndex) 632 } 633 } 634 } 635 636 blkReceipts := bc.GetReceiptsByHash(current.Hash()) 637 if blkReceipts.Len() != len(txs) { 638 return fmt.Errorf("found %d transaction receipts, expected %d", blkReceipts.Len(), len(txs)) 639 } 640 for index, txReceipt := range blkReceipts { 641 if txReceipt.TxHash != txs[index].Hash() { 642 return fmt.Errorf("transaction receipt mismatch, expected %s, but found: %s", txs[index].Hash().String(), txReceipt.TxHash.String()) 643 } 644 if txReceipt.BlockHash != current.Hash() { 645 return fmt.Errorf("transaction receipt had block hash %s, but expected %s", txReceipt.BlockHash.String(), current.Hash().String()) 646 } 647 if txReceipt.BlockNumber.Uint64() != current.NumberU64() { 648 return fmt.Errorf("transaction receipt had block number %d, but expected %d", txReceipt.BlockNumber.Uint64(), current.NumberU64()) 649 } 650 } 651 652 i += 1 653 if i%1000 == 0 { 654 log.Info("Validate Canonical Chain Update", "totalBlocks", i) 655 } 656 657 parent := bc.GetBlockByHash(current.ParentHash()) 658 if parent.Hash() != current.ParentHash() { 659 return fmt.Errorf("getBlockByHash retrieved parent block with incorrect hash, found %s, expected: %s", parent.Hash().String(), current.ParentHash().String()) 660 } 661 current = parent 662 } 663 664 return nil 665 } 666 667 // GetReceiptsByHash retrieves the receipts for all transactions in a given block. 668 func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts { 669 if receipts, ok := bc.receiptsCache.Get(hash); ok { 670 return receipts.(types.Receipts) 671 } 672 number := rawdb.ReadHeaderNumber(bc.db, hash) 673 if number == nil { 674 return nil 675 } 676 receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig) 677 if receipts == nil { 678 return nil 679 } 680 bc.receiptsCache.Add(hash, receipts) 681 return receipts 682 } 683 684 // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors. 685 // [deprecated by eth/62] 686 func (bc *BlockChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) { 687 number := bc.hc.GetBlockNumber(hash) 688 if number == nil { 689 return nil 690 } 691 for i := 0; i < n; i++ { 692 block := bc.GetBlock(hash, *number) 693 if block == nil { 694 break 695 } 696 blocks = append(blocks, block) 697 hash = block.ParentHash() 698 *number-- 699 } 700 return 701 } 702 703 // GetUnclesInChain retrieves all the uncles from a given block backwards until 704 // a specific distance is reached. 705 func (bc *BlockChain) GetUnclesInChain(block *types.Block, length int) []*types.Header { 706 uncles := []*types.Header{} 707 for i := 0; block != nil && i < length; i++ { 708 uncles = append(uncles, block.Uncles()...) 709 block = bc.GetBlock(block.ParentHash(), block.NumberU64()-1) 710 } 711 return uncles 712 } 713 714 // TrieNode retrieves a blob of data associated with a trie node 715 // either from ephemeral in-memory cache, or from persistent storage. 716 func (bc *BlockChain) TrieNode(hash common.Hash) ([]byte, error) { 717 return bc.stateCache.TrieDB().Node(hash) 718 } 719 720 // ContractCode retrieves a blob of data associated with a contract hash 721 // either from ephemeral in-memory cache, or from persistent storage. 722 func (bc *BlockChain) ContractCode(hash common.Hash) ([]byte, error) { 723 return bc.stateCache.ContractCode(common.Hash{}, hash) 724 } 725 726 // Stop stops the blockchain service. If any imports are currently in progress 727 // it will abort them using the procInterrupt. 728 func (bc *BlockChain) Stop() { 729 if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) { 730 return 731 } 732 // Unsubscribe all subscriptions registered from blockchain 733 bc.scope.Close() 734 close(bc.quit) 735 bc.wg.Wait() 736 737 if err := bc.stateManager.Shutdown(); err != nil { 738 log.Error("Failed to Shutdown state manager", "err", err) 739 } 740 741 log.Info("Blockchain stopped") 742 } 743 744 // WriteStatus status of write 745 type WriteStatus byte 746 747 const ( 748 NonStatTy WriteStatus = iota 749 CanonStatTy 750 SideStatTy 751 ) 752 753 // SetPreference attempts to update the head block to be the provided block and 754 // emits a ChainHeadEvent if successful. This function will handle all reorg 755 // side effects, if necessary. 756 // 757 // Note: This function should ONLY be called on blocks that have already been 758 // inserted into the chain. 759 // 760 // Assumes [bc.chainmu] is not held by the caller. 761 func (bc *BlockChain) SetPreference(block *types.Block) error { 762 bc.chainmu.Lock() 763 defer bc.chainmu.Unlock() 764 765 return bc.setPreference(block) 766 } 767 768 // setPreference attempts to update the head block to be the provided block and 769 // emits a ChainHeadEvent if successful. This function will handle all reorg 770 // side effects, if necessary. 771 // 772 // Assumes [bc.chainmu] is held by the caller. 773 func (bc *BlockChain) setPreference(block *types.Block) error { 774 current := bc.CurrentBlock() 775 776 // Return early if the current block is already the block 777 // we are trying to write. 778 if current.Hash() == block.Hash() { 779 return nil 780 } 781 782 log.Debug("Setting preference", "number", block.Number(), "hash", block.Hash()) 783 784 // writeKnownBlock updates the head block and will handle any reorg side 785 // effects automatically. 786 if err := bc.writeKnownBlock(block); err != nil { 787 return fmt.Errorf("unable to invoke writeKnownBlock: %w", err) 788 } 789 790 // Send an ChainHeadEvent if we end up altering 791 // the head block. Many internal aysnc processes rely on 792 // receiving these events (i.e. the TxPool). 793 bc.chainHeadFeed.Send(ChainHeadEvent{Block: block}) 794 return nil 795 } 796 797 // LastAcceptedBlock returns the last block to be marked as accepted. 798 func (bc *BlockChain) LastAcceptedBlock() *types.Block { 799 bc.chainmu.Lock() 800 defer bc.chainmu.Unlock() 801 802 return bc.lastAccepted 803 } 804 805 // Accept sets a minimum height at which no reorg can pass. Additionally, 806 // this function may trigger a reorg if the block being accepted is not in the 807 // canonical chain. 808 // 809 // Assumes [bc.chainmu] is not held by the caller. 810 func (bc *BlockChain) Accept(block *types.Block) error { 811 bc.chainmu.Lock() 812 defer bc.chainmu.Unlock() 813 814 // The parent of [block] must be the last accepted block. 815 if bc.lastAccepted.Hash() != block.ParentHash() { 816 return fmt.Errorf( 817 "expected accepted block to have parent %s:%d but got %s:%d", 818 bc.lastAccepted.Hash().Hex(), 819 bc.lastAccepted.NumberU64(), 820 block.ParentHash().Hex(), 821 block.NumberU64()-1, 822 ) 823 } 824 825 // If the canonical hash at the block height does not match the block we are 826 // accepting, we need to trigger a reorg. 827 canonical := bc.GetCanonicalHash(block.NumberU64()) 828 if canonical != block.Hash() { 829 log.Debug("Accepting block in non-canonical chain", "number", block.Number(), "hash", block.Hash()) 830 if err := bc.setPreference(block); err != nil { 831 return fmt.Errorf("could not set new preferred block %d:%s as preferred: %w", block.Number(), block.Hash(), err) 832 } 833 } 834 835 bc.lastAccepted = block 836 837 // Abort snapshot generation before pruning anything from trie database 838 // (could occur in AcceptTrie) 839 if bc.snaps != nil { 840 bc.snaps.AbortGeneration() 841 } 842 843 // Accept Trie 844 if err := bc.stateManager.AcceptTrie(block); err != nil { 845 return fmt.Errorf("unable to accept trie: %w", err) 846 } 847 848 // Flatten the entire snap Trie to disk 849 if bc.snaps != nil { 850 if err := bc.snaps.Flatten(block.Hash()); err != nil { 851 return fmt.Errorf("unable to flatten trie: %w", err) 852 } 853 } 854 855 // Update transaction lookup index 856 batch := bc.db.NewBatch() 857 rawdb.WriteTxLookupEntriesByBlock(batch, block) 858 if err := batch.Write(); err != nil { 859 return fmt.Errorf("failed to write tx lookup entries batch: %w", err) 860 } 861 862 // Fetch block logs 863 logs := bc.gatherBlockLogs(block.Hash(), block.NumberU64(), false) 864 865 // Update accepted feeds 866 bc.chainAcceptedFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs}) 867 if len(logs) > 0 { 868 bc.logsAcceptedFeed.Send(logs) 869 } 870 if len(block.Transactions()) != 0 { 871 bc.txAcceptedFeed.Send(NewTxsEvent{block.Transactions()}) 872 } 873 874 return nil 875 } 876 877 func (bc *BlockChain) Reject(block *types.Block) error { 878 bc.chainmu.Lock() 879 defer bc.chainmu.Unlock() 880 881 // Reject Trie 882 if err := bc.stateManager.RejectTrie(block); err != nil { 883 return fmt.Errorf("unable to reject trie: %w", err) 884 } 885 886 if bc.snaps != nil { 887 if err := bc.snaps.Discard(block.Hash()); err != nil { 888 log.Error("unable to discard snap from rejected block", "block", block.Hash(), "number", block.NumberU64(), "root", block.Root()) 889 } 890 } 891 892 // Remove the block since its data is no longer needed 893 batch := bc.db.NewBatch() 894 rawdb.DeleteBlock(batch, block.Hash(), block.NumberU64()) 895 if err := batch.Write(); err != nil { 896 return fmt.Errorf("failed to write delete block batch: %w", err) 897 } 898 899 return nil 900 } 901 902 // writeKnownBlock updates the head block flag with a known block 903 // and introduces chain reorg if necessary. 904 func (bc *BlockChain) writeKnownBlock(block *types.Block) error { 905 bc.wg.Add(1) 906 defer bc.wg.Done() 907 908 current := bc.CurrentBlock() 909 if block.ParentHash() != current.Hash() { 910 if err := bc.reorg(current, block); err != nil { 911 return err 912 } 913 } 914 bc.writeHeadBlock(block) 915 return nil 916 } 917 918 // writeCanonicalBlockWithLogs writes the new head [block] and emits events 919 // for the new head block. 920 func (bc *BlockChain) writeCanonicalBlockWithLogs(block *types.Block, logs []*types.Log) { 921 bc.writeHeadBlock(block) 922 bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs}) 923 if len(logs) > 0 { 924 bc.logsFeed.Send(logs) 925 } 926 bc.chainHeadFeed.Send(ChainHeadEvent{Block: block}) 927 } 928 929 // newTip returns a boolean indicating if the block should be appended to 930 // the canonical chain. 931 func (bc *BlockChain) newTip(block *types.Block) bool { 932 return block.ParentHash() == bc.CurrentBlock().Hash() 933 } 934 935 // writeBlockWithState writes the block and all associated state to the database, 936 // but it expects the chain mutex to be held. 937 // writeBlockWithState expects to be the last verification step during InsertBlock 938 // since it creates a reference that will only be cleaned up by Accept/Reject. 939 func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB) (WriteStatus, error) { 940 bc.wg.Add(1) 941 defer bc.wg.Done() 942 943 // Calculate the total difficulty of the block 944 ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1) 945 if ptd == nil { 946 return NonStatTy, consensus.ErrUnknownAncestor 947 } 948 // Make sure no inconsistent state is leaked during insertion 949 // currentBlock := bc.CurrentBlock() 950 // localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) 951 externTd := new(big.Int).Add(block.Difficulty(), ptd) 952 953 // Irrelevant of the canonical status, write the block itself to the database. 954 // 955 // Note all the components of block(td, hash->number map, header, body, receipts) 956 // should be written atomically. BlockBatch is used for containing all components. 957 blockBatch := bc.db.NewBatch() 958 rawdb.WriteTd(blockBatch, block.Hash(), block.NumberU64(), externTd) 959 rawdb.WriteBlock(blockBatch, block) 960 rawdb.WriteReceipts(blockBatch, block.Hash(), block.NumberU64(), receipts) 961 rawdb.WritePreimages(blockBatch, state.Preimages()) 962 if err := blockBatch.Write(); err != nil { 963 log.Crit("Failed to write block into disk", "err", err) 964 } 965 // Commit all cached state changes into underlying memory database. 966 // If snapshots are enabled, call CommitWithSnaps to explicitly create a snapshot 967 // diff layer for the block. 968 var err error 969 if bc.snaps == nil { 970 _, err = state.Commit(bc.chainConfig.IsEIP158(block.Number())) 971 } else { 972 _, err = state.CommitWithSnap(bc.chainConfig.IsEIP158(block.Number()), bc.snaps, block.Hash(), block.ParentHash()) 973 } 974 if err != nil { 975 return NonStatTy, err 976 } 977 978 // Note: if InsertTrie must be the last step in verification that can return an error. 979 // This allows [stateManager] to assume that if it inserts a trie without returning an 980 // error then the block has passed verification and either AcceptTrie/RejectTrie will 981 // eventually be called on [root] unless a fatal error occurs. It does not assume that 982 // the node will not shutdown before either AcceptTrie/RejectTrie is called. 983 if err := bc.stateManager.InsertTrie(block); err != nil { 984 if bc.snaps != nil { 985 discardErr := bc.snaps.Discard(block.Hash()) 986 if discardErr != nil { 987 log.Debug("failed to discard snapshot after being unable to insert block trie", "block", block.Hash(), "root", block.Root()) 988 } 989 } 990 return NonStatTy, err 991 } 992 993 // If [block] represents a new tip of the canonical chain, we optimistically add it before 994 // setPreference is called. Otherwise, we consider it a side chain block. 995 if bc.newTip(block) { 996 bc.writeCanonicalBlockWithLogs(block, logs) 997 return CanonStatTy, nil 998 } 999 1000 bc.chainSideFeed.Send(ChainSideEvent{Block: block}) 1001 return SideStatTy, nil 1002 } 1003 1004 // InsertChain attempts to insert the given batch of blocks in to the canonical 1005 // chain or, otherwise, create a fork. If an error is returned it will return 1006 // the index number of the failing block as well an error describing what went 1007 // wrong. 1008 // 1009 // After insertion is done, all accumulated events will be fired. 1010 func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) { 1011 // Sanity check that we have something meaningful to import 1012 if len(chain) == 0 { 1013 return 0, nil 1014 } 1015 1016 bc.blockProcFeed.Send(true) 1017 defer bc.blockProcFeed.Send(false) 1018 1019 // Remove already known canon-blocks 1020 var ( 1021 block, prev *types.Block 1022 ) 1023 // Do a sanity check that the provided chain is actually ordered and linked 1024 for i := 1; i < len(chain); i++ { 1025 block = chain[i] 1026 prev = chain[i-1] 1027 if block.NumberU64() != prev.NumberU64()+1 || block.ParentHash() != prev.Hash() { 1028 // Chain broke ancestry, log a message (programming error) and skip insertion 1029 log.Error("Non contiguous block insert", "number", block.Number(), "hash", block.Hash(), 1030 "parent", block.ParentHash(), "prevnumber", prev.Number(), "prevhash", prev.Hash()) 1031 1032 return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, prev.NumberU64(), 1033 prev.Hash().Bytes()[:4], i, block.NumberU64(), block.Hash().Bytes()[:4], block.ParentHash().Bytes()[:4]) 1034 } 1035 } 1036 // Pre-checks passed, start the full block imports 1037 bc.wg.Add(1) 1038 defer bc.wg.Done() 1039 1040 bc.chainmu.Lock() 1041 defer bc.chainmu.Unlock() 1042 for n, block := range chain { 1043 if err := bc.insertBlock(block, true); err != nil { 1044 return n, err 1045 } 1046 } 1047 1048 return len(chain), nil 1049 } 1050 1051 func (bc *BlockChain) InsertBlock(block *types.Block) error { 1052 return bc.InsertBlockManual(block, true) 1053 } 1054 1055 func (bc *BlockChain) InsertBlockManual(block *types.Block, writes bool) error { 1056 bc.blockProcFeed.Send(true) 1057 defer bc.blockProcFeed.Send(false) 1058 1059 bc.wg.Add(1) 1060 bc.chainmu.Lock() 1061 err := bc.insertBlock(block, writes) 1062 bc.chainmu.Unlock() 1063 bc.wg.Done() 1064 1065 return err 1066 } 1067 1068 // gatherBlockLogs fetches logs from a previously inserted block. 1069 func (bc *BlockChain) gatherBlockLogs(hash common.Hash, number uint64, removed bool) []*types.Log { 1070 receipts := rawdb.ReadReceipts(bc.db, hash, number, bc.chainConfig) 1071 var logs []*types.Log 1072 for _, receipt := range receipts { 1073 for _, log := range receipt.Logs { 1074 l := *log 1075 if removed { 1076 l.Removed = true 1077 } 1078 logs = append(logs, &l) 1079 } 1080 } 1081 1082 return logs 1083 } 1084 1085 func (bc *BlockChain) insertBlock(block *types.Block, writes bool) error { 1086 senderCacher.recover(types.MakeSigner(bc.chainConfig, block.Number(), new(big.Int).SetUint64(block.Time())), block.Transactions()) 1087 1088 err := bc.engine.VerifyHeader(bc, block.Header()) 1089 if err == nil { 1090 err = bc.validator.ValidateBody(block) 1091 } 1092 1093 switch { 1094 case errors.Is(err, ErrKnownBlock): 1095 // even if the block is already known, we still need to generate the 1096 // snapshot layer and add a reference to the triedb, so we re-execute 1097 // the block. Note that insertBlock should only be called on a block 1098 // once if it returns nil 1099 if bc.newTip(block) { 1100 log.Debug("Setting head to be known block", "number", block.Number(), "hash", block.Hash()) 1101 } else { 1102 log.Debug("Reprocessing already known block", "number", block.Number(), "hash", block.Hash()) 1103 } 1104 1105 // If an ancestor has been pruned, then this block cannot be acceptable. 1106 case errors.Is(err, consensus.ErrPrunedAncestor): 1107 return errors.New("side chain insertion is not supported") 1108 1109 // Future blocks are not supported, but should not be reported, so we return an error 1110 // early here 1111 case errors.Is(err, consensus.ErrFutureBlock): 1112 return errFutureBlockUnsupported 1113 1114 // Some other error occurred, abort 1115 case err != nil: 1116 bc.reportBlock(block, nil, err) 1117 return err 1118 } 1119 // No validation errors for the block 1120 var activeState *state.StateDB 1121 defer func() { 1122 // The chain importer is starting and stopping trie prefetchers. If a bad 1123 // block or other error is hit however, an early return may not properly 1124 // terminate the background threads. This defer ensures that we clean up 1125 // and dangling prefetcher, without defering each and holding on live refs. 1126 if activeState != nil { 1127 activeState.StopPrefetcher() 1128 } 1129 }() 1130 1131 // Retrieve the parent block and it's state to execute on top 1132 start := time.Now() 1133 1134 parent := bc.GetHeader(block.ParentHash(), block.NumberU64()-1) 1135 1136 var statedb *state.StateDB 1137 if bc.snaps == nil { 1138 statedb, err = state.New(parent.Root, bc.stateCache, nil) 1139 } else { 1140 snap := bc.snaps.Snapshot(parent.Root) 1141 if snap == nil { 1142 return fmt.Errorf("failed to get snapshot for parent root: %s", parent.Root) 1143 } 1144 statedb, err = state.NewWithSnapshot(parent.Root, bc.stateCache, snap) 1145 } 1146 if err != nil { 1147 return err 1148 } 1149 1150 // Enable prefetching to pull in trie node paths while processing transactions 1151 statedb.StartPrefetcher("chain") 1152 activeState = statedb 1153 1154 // If we have a followup block, run that against the current state to pre-cache 1155 // transactions and probabilistically some of the account/storage trie nodes. 1156 // Process block using the parent state as reference point 1157 receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig) 1158 if err != nil { 1159 bc.reportBlock(block, receipts, err) 1160 return err 1161 } 1162 1163 // Validate the state using the default validator 1164 if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil { 1165 bc.reportBlock(block, receipts, err) 1166 return err 1167 } 1168 1169 // If [writes] are disabled, skip [writeBlockWithState] so that we do not write the block 1170 // or the state trie to disk. 1171 // Note: in pruning mode, this prevents us from generating a reference to the state root. 1172 if !writes { 1173 return nil 1174 } 1175 1176 // Write the block to the chain and get the status. 1177 // writeBlockWithState creates a reference that will be cleaned up in Accept/Reject 1178 // so we need to ensure an error cannot occur later in verification, since that would 1179 // cause the referenced root to never be dereferenced. 1180 status, err := bc.writeBlockWithState(block, receipts, logs, statedb) 1181 if err != nil { 1182 return err 1183 } 1184 1185 switch status { 1186 case CanonStatTy: 1187 log.Debug("Inserted new block", "number", block.Number(), "hash", block.Hash(), 1188 "parentHash", block.ParentHash(), 1189 "uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(), 1190 "elapsed", common.PrettyDuration(time.Since(start)), 1191 "root", block.Root()) 1192 // Only count canonical blocks for GC processing time 1193 case SideStatTy: 1194 log.Debug("Inserted forked block", "number", block.Number(), "hash", block.Hash(), 1195 "parentHash", block.ParentHash(), 1196 "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)), 1197 "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()), 1198 "root", block.Root()) 1199 default: 1200 // This in theory is impossible, but lets be nice to our future selves and leave 1201 // a log, instead of trying to track down blocks imports that don't emit logs. 1202 log.Warn("Inserted block with unknown status", "number", block.Number(), "hash", block.Hash(), 1203 "parentHash", block.ParentHash(), 1204 "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(start)), 1205 "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()), 1206 "root", block.Root()) 1207 } 1208 1209 return err 1210 } 1211 1212 // reorg takes two blocks, an old chain and a new chain and will reconstruct the 1213 // blocks and inserts them to be part of the new canonical chain and accumulates 1214 // potential missing transactions and post an event about them. 1215 func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { 1216 var ( 1217 newHead = newBlock 1218 oldHead = oldBlock 1219 1220 newChain types.Blocks 1221 oldChain types.Blocks 1222 commonBlock *types.Block 1223 1224 deletedLogs [][]*types.Log 1225 rebirthLogs [][]*types.Log 1226 1227 // collectLogs collects the logs that were generated or removed during 1228 // the processing of the block that corresponds with the given hash. 1229 // These logs are later announced as deleted or reborn 1230 collectLogs = func(hash common.Hash, removed bool) { 1231 number := bc.hc.GetBlockNumber(hash) 1232 if number == nil { 1233 return 1234 } 1235 logs := bc.gatherBlockLogs(hash, *number, removed) 1236 if len(logs) > 0 { 1237 if removed { 1238 deletedLogs = append(deletedLogs, logs) 1239 } else { 1240 rebirthLogs = append(rebirthLogs, logs) 1241 } 1242 } 1243 } 1244 // mergeLogs returns a merged log slice with specified sort order. 1245 mergeLogs = func(logs [][]*types.Log, reverse bool) []*types.Log { 1246 var ret []*types.Log 1247 if reverse { 1248 for i := len(logs) - 1; i >= 0; i-- { 1249 ret = append(ret, logs[i]...) 1250 } 1251 } else { 1252 for i := 0; i < len(logs); i++ { 1253 ret = append(ret, logs[i]...) 1254 } 1255 } 1256 return ret 1257 } 1258 ) 1259 // Reduce the longer chain to the same number as the shorter one 1260 if oldBlock.NumberU64() > newBlock.NumberU64() { 1261 // Old chain is longer, gather all transactions and logs as deleted ones 1262 for ; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) { 1263 oldChain = append(oldChain, oldBlock) 1264 collectLogs(oldBlock.Hash(), true) 1265 } 1266 } else { 1267 // New chain is longer, stash all blocks away for subsequent insertion 1268 for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) { 1269 newChain = append(newChain, newBlock) 1270 } 1271 } 1272 if oldBlock == nil { 1273 return fmt.Errorf("invalid old chain") 1274 } 1275 if newBlock == nil { 1276 return fmt.Errorf("invalid new chain") 1277 } 1278 // Both sides of the reorg are at the same number, reduce both until the common 1279 // ancestor is found 1280 for { 1281 // If the common ancestor was found, bail out 1282 if oldBlock.Hash() == newBlock.Hash() { 1283 commonBlock = oldBlock 1284 break 1285 } 1286 // Remove an old block as well as stash away a new block 1287 oldChain = append(oldChain, oldBlock) 1288 collectLogs(oldBlock.Hash(), true) 1289 1290 newChain = append(newChain, newBlock) 1291 1292 // Step back with both chains 1293 oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) 1294 if oldBlock == nil { 1295 return fmt.Errorf("invalid old chain") 1296 } 1297 newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) 1298 if newBlock == nil { 1299 return fmt.Errorf("invalid new chain") 1300 } 1301 } 1302 1303 // If the commonBlock is less than the last accepted height, we return an error 1304 // because performing a reorg would mean removing an accepted block from the 1305 // canonical chain. 1306 if commonBlock.NumberU64() < bc.lastAccepted.NumberU64() { 1307 return errors.New("cannot orphan finalized block") 1308 } 1309 1310 // Ensure the user sees large reorgs 1311 if len(oldChain) > 0 && len(newChain) > 0 { 1312 logFn := log.Info 1313 msg := "Chain reorg detected" 1314 if len(oldChain) > 63 { 1315 msg = "Large chain reorg detected" 1316 logFn = log.Warn 1317 } 1318 logFn(msg, "number", commonBlock.Number(), "hash", commonBlock.Hash(), 1319 "drop", len(oldChain), "dropfrom", oldChain[0].Hash(), "add", len(newChain), "addfrom", newChain[0].Hash()) 1320 } else { 1321 log.Warn("Unlikely reorg (rewind to ancestor) occurred", "oldnum", oldHead.Number(), "oldhash", oldHead.Hash(), "newnum", newHead.Number(), "newhash", newHead.Hash()) 1322 } 1323 // Insert the new chain(except the head block(reverse order)), 1324 // taking care of the proper incremental order. 1325 for i := len(newChain) - 1; i >= 1; i-- { 1326 // Insert the block in the canonical way, re-writing history 1327 bc.writeHeadBlock(newChain[i]) 1328 1329 // Collect reborn logs due to chain reorg 1330 collectLogs(newChain[i].Hash(), false) 1331 } 1332 // Delete any canonical number assignments above the new head 1333 indexesBatch := bc.db.NewBatch() 1334 1335 // Use the height of [newHead] to determine which canonical hashes to remove 1336 // in case the new chain is shorter than the old chain, in which case 1337 // there may be hashes set on the canonical chain that were invalidated 1338 // but not yet overwritten by the re-org. 1339 for i := newHead.NumberU64() + 1; ; i++ { 1340 hash := rawdb.ReadCanonicalHash(bc.db, i) 1341 if hash == (common.Hash{}) { 1342 break 1343 } 1344 rawdb.DeleteCanonicalHash(indexesBatch, i) 1345 } 1346 if err := indexesBatch.Write(); err != nil { 1347 log.Crit("Failed to delete useless indexes", "err", err) 1348 } 1349 // If any logs need to be fired, do it now. In theory we could avoid creating 1350 // this goroutine if there are no events to fire, but realistcally that only 1351 // ever happens if we're reorging empty blocks, which will only happen on idle 1352 // networks where performance is not an issue either way. 1353 if len(deletedLogs) > 0 { 1354 bc.rmLogsFeed.Send(RemovedLogsEvent{mergeLogs(deletedLogs, true)}) 1355 } 1356 if len(rebirthLogs) > 0 { 1357 bc.logsFeed.Send(mergeLogs(rebirthLogs, false)) 1358 } 1359 if len(oldChain) > 0 { 1360 for i := len(oldChain) - 1; i >= 0; i-- { 1361 bc.chainSideFeed.Send(ChainSideEvent{Block: oldChain[i]}) 1362 } 1363 } 1364 return nil 1365 } 1366 1367 // BadBlocks returns a list of the last 'bad blocks' that the client has seen on the network 1368 func (bc *BlockChain) BadBlocks() []*types.Block { 1369 blocks := make([]*types.Block, 0, bc.badBlocks.Len()) 1370 for _, hash := range bc.badBlocks.Keys() { 1371 if blk, exist := bc.badBlocks.Peek(hash); exist { 1372 block := blk.(*types.Block) 1373 blocks = append(blocks, block) 1374 } 1375 } 1376 return blocks 1377 } 1378 1379 // addBadBlock adds a bad block to the bad-block LRU cache 1380 func (bc *BlockChain) addBadBlock(block *types.Block) { 1381 bc.badBlocks.Add(block.Hash(), block) 1382 } 1383 1384 // reportBlock logs a bad block error. 1385 func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, err error) { 1386 bc.addBadBlock(block) 1387 1388 var receiptString string 1389 for i, receipt := range receipts { 1390 receiptString += fmt.Sprintf("\t %d: cumulative: %v gas: %v contract: %v status: %v tx: %v logs: %v bloom: %x state: %x\n", 1391 i, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.ContractAddress.Hex(), 1392 receipt.Status, receipt.TxHash.Hex(), receipt.Logs, receipt.Bloom, receipt.PostState) 1393 } 1394 log.Error(fmt.Sprintf(` 1395 ########## BAD BLOCK ######### 1396 Chain config: %v 1397 1398 Number: %v 1399 Hash: 0x%x 1400 %v 1401 1402 Error: %v 1403 ############################## 1404 `, bc.chainConfig, block.Number(), block.Hash(), receiptString, err)) 1405 } 1406 1407 // CurrentHeader retrieves the current head header of the canonical chain. The 1408 // header is retrieved from the HeaderChain's internal cache. 1409 func (bc *BlockChain) CurrentHeader() *types.Header { 1410 return bc.hc.CurrentHeader() 1411 } 1412 1413 // GetTd retrieves a block's total difficulty in the canonical chain from the 1414 // database by hash and number, caching it if found. 1415 func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int { 1416 return bc.hc.GetTd(hash, number) 1417 } 1418 1419 // GetTdByHash retrieves a block's total difficulty in the canonical chain from the 1420 // database by hash, caching it if found. 1421 func (bc *BlockChain) GetTdByHash(hash common.Hash) *big.Int { 1422 return bc.hc.GetTdByHash(hash) 1423 } 1424 1425 // GetHeader retrieves a block header from the database by hash and number, 1426 // caching it if found. 1427 func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header { 1428 // Blockchain might have cached the whole block, only if not go to headerchain 1429 if block, ok := bc.blockCache.Get(hash); ok { 1430 return block.(*types.Block).Header() 1431 } 1432 1433 return bc.hc.GetHeader(hash, number) 1434 } 1435 1436 // GetHeaderByHash retrieves a block header from the database by hash, caching it if 1437 // found. 1438 func (bc *BlockChain) GetHeaderByHash(hash common.Hash) *types.Header { 1439 // Blockchain might have cached the whole block, only if not go to headerchain 1440 if block, ok := bc.blockCache.Get(hash); ok { 1441 return block.(*types.Block).Header() 1442 } 1443 1444 return bc.hc.GetHeaderByHash(hash) 1445 } 1446 1447 // HasHeader checks if a block header is present in the database or not, caching 1448 // it if present. 1449 func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool { 1450 return bc.hc.HasHeader(hash, number) 1451 } 1452 1453 // GetCanonicalHash returns the canonical hash for a given block number 1454 func (bc *BlockChain) GetCanonicalHash(number uint64) common.Hash { 1455 return bc.hc.GetCanonicalHash(number) 1456 } 1457 1458 // GetHeaderByNumber retrieves a block header from the database by number, 1459 // caching it (associated with its hash) if found. 1460 func (bc *BlockChain) GetHeaderByNumber(number uint64) *types.Header { 1461 return bc.hc.GetHeaderByNumber(number) 1462 } 1463 1464 // GetTransactionLookup retrieves the lookup associate with the given transaction 1465 // hash from the cache or database. 1466 func (bc *BlockChain) GetTransactionLookup(hash common.Hash) *rawdb.LegacyTxLookupEntry { 1467 // Short circuit if the txlookup already in the cache, retrieve otherwise 1468 if lookup, exist := bc.txLookupCache.Get(hash); exist { 1469 return lookup.(*rawdb.LegacyTxLookupEntry) 1470 } 1471 tx, blockHash, blockNumber, txIndex := rawdb.ReadTransaction(bc.db, hash) 1472 if tx == nil { 1473 return nil 1474 } 1475 lookup := &rawdb.LegacyTxLookupEntry{BlockHash: blockHash, BlockIndex: blockNumber, Index: txIndex} 1476 bc.txLookupCache.Add(hash, lookup) 1477 return lookup 1478 } 1479 1480 // Config retrieves the chain's fork configuration. 1481 func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig } 1482 1483 // Engine retrieves the blockchain's consensus engine. 1484 func (bc *BlockChain) Engine() consensus.Engine { return bc.engine } 1485 1486 // SubscribeRemovedLogsEvent registers a subscription of RemovedLogsEvent. 1487 func (bc *BlockChain) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription { 1488 return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch)) 1489 } 1490 1491 // SubscribeChainEvent registers a subscription of ChainEvent. 1492 func (bc *BlockChain) SubscribeChainEvent(ch chan<- ChainEvent) event.Subscription { 1493 return bc.scope.Track(bc.chainFeed.Subscribe(ch)) 1494 } 1495 1496 // SubscribeChainAcceptedEvent registers a subscription of ChainEvent. 1497 func (bc *BlockChain) SubscribeChainAcceptedEvent(ch chan<- ChainEvent) event.Subscription { 1498 return bc.scope.Track(bc.chainAcceptedFeed.Subscribe(ch)) 1499 } 1500 1501 // SubscribeChainHeadEvent registers a subscription of ChainHeadEvent. 1502 func (bc *BlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription { 1503 return bc.scope.Track(bc.chainHeadFeed.Subscribe(ch)) 1504 } 1505 1506 // SubscribeChainSideEvent registers a subscription of ChainSideEvent. 1507 func (bc *BlockChain) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscription { 1508 return bc.scope.Track(bc.chainSideFeed.Subscribe(ch)) 1509 } 1510 1511 // SubscribeLogsEvent registers a subscription of []*types.Log. 1512 func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 1513 return bc.scope.Track(bc.logsFeed.Subscribe(ch)) 1514 } 1515 1516 // SubscribeAcceptedLogsEvent registers a subscription of accepted []*types.Log. 1517 func (bc *BlockChain) SubscribeAcceptedLogsEvent(ch chan<- []*types.Log) event.Subscription { 1518 return bc.scope.Track(bc.logsAcceptedFeed.Subscribe(ch)) 1519 } 1520 1521 // SubscribeBlockProcessingEvent registers a subscription of bool where true means 1522 // block processing has started while false means it has stopped. 1523 func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscription { 1524 return bc.scope.Track(bc.blockProcFeed.Subscribe(ch)) 1525 } 1526 1527 // SubscribeAcceptedTransactionEvent registers a subscription of accepted transactions 1528 func (bc *BlockChain) SubscribeAcceptedTransactionEvent(ch chan<- NewTxsEvent) event.Subscription { 1529 return bc.scope.Track(bc.txAcceptedFeed.Subscribe(ch)) 1530 } 1531 1532 func (bc *BlockChain) RemoveRejectedBlocks(start, end uint64) error { 1533 batch := bc.db.NewBatch() 1534 1535 for i := start; i < end; i++ { 1536 hashes := rawdb.ReadAllHashes(bc.db, i) 1537 canonicalBlock := bc.GetBlockByNumber((i)) 1538 if canonicalBlock == nil { 1539 return fmt.Errorf("failed to retrieve block by number at height %d", i) 1540 } 1541 canonicalHash := canonicalBlock.Hash() 1542 for _, hash := range hashes { 1543 if hash == canonicalHash { 1544 continue 1545 } 1546 rawdb.DeleteBlock(batch, hash, i) 1547 } 1548 1549 if err := batch.Write(); err != nil { 1550 return fmt.Errorf("failed to write delete rejected block batch at height %d", i) 1551 } 1552 batch.Reset() 1553 } 1554 1555 return nil 1556 } 1557 1558 // reprocessState reprocesses the state up to [block], iterating through its ancestors until 1559 // it reaches a block with a state committed to the database. reprocessState does not use 1560 // snapshots since the disk layer for snapshots will most likely be above the last committed 1561 // state that reprocessing will start from. 1562 func (bc *BlockChain) reprocessState(current *types.Block, reexec uint64, report bool) error { 1563 var ( 1564 origin = current.NumberU64() 1565 ) 1566 // If the state is already available, skip re-processing 1567 statedb, err := state.New(current.Root(), bc.stateCache, nil) 1568 if err == nil { 1569 return nil 1570 } 1571 // Check how far back we need to re-execute, capped at [reexec] 1572 for i := 0; i < int(reexec); i++ { 1573 if current.NumberU64() == 0 { 1574 return errors.New("genesis state is missing") 1575 } 1576 parent := bc.GetBlock(current.ParentHash(), current.NumberU64()-1) 1577 if parent == nil { 1578 return fmt.Errorf("missing block %s:%d", current.ParentHash().Hex(), current.NumberU64()-1) 1579 } 1580 current = parent 1581 1582 statedb, err = state.New(current.Root(), bc.stateCache, nil) 1583 if err == nil { 1584 break 1585 } 1586 } 1587 if err != nil { 1588 switch err.(type) { 1589 case *trie.MissingNodeError: 1590 return fmt.Errorf("required historical state unavailable (reexec=%d)", reexec) 1591 default: 1592 return err 1593 } 1594 } 1595 1596 // State was available at historical point, regenerate 1597 var ( 1598 start = time.Now() 1599 logged time.Time 1600 previousRoot common.Hash 1601 triedb = bc.stateCache.TrieDB() 1602 ) 1603 // Note: we add 1 since in each iteration, we attempt to re-execute the next block. 1604 log.Info("Re-executing blocks to generate state for last accepted block", "from", current.NumberU64()+1, "to", origin) 1605 for current.NumberU64() < origin { 1606 // Print progress logs if long enough time elapsed 1607 if time.Since(logged) > 8*time.Second && report { 1608 log.Info("Regenerating historical state", "block", current.NumberU64()+1, "target", origin, "remaining", origin-current.NumberU64(), "elapsed", time.Since(start)) 1609 logged = time.Now() 1610 } 1611 // Retrieve the next block to regenerate and process it 1612 next := current.NumberU64() + 1 1613 if current = bc.GetBlockByNumber(next); current == nil { 1614 return fmt.Errorf("failed to retrieve block %d while re-generating state", next) 1615 } 1616 receipts, _, usedGas, err := bc.processor.Process(current, statedb, vm.Config{}) 1617 if err != nil { 1618 return fmt.Errorf("failed to re-process block (%s: %d): %v", current.Hash().Hex(), current.NumberU64(), err) 1619 } 1620 // Validate the state using the default validator 1621 if err := bc.validator.ValidateState(current, statedb, receipts, usedGas); err != nil { 1622 return fmt.Errorf("failed to validate state while re-processing block (%s: %d): %v", current.Hash().Hex(), current.NumberU64(), err) 1623 } 1624 log.Debug("processed block", "block", current.Hash(), "number", current.NumberU64()) 1625 // Finalize the state so any modifications are written to the trie 1626 root, err := statedb.Commit(bc.chainConfig.IsEIP158(current.Number())) 1627 if err != nil { 1628 return err 1629 } 1630 statedb, err = state.New(root, bc.stateCache, nil) 1631 if err != nil { 1632 return fmt.Errorf("state reset after block %d failed: %v", current.NumberU64(), err) 1633 } 1634 1635 triedb.Reference(root, common.Hash{}) 1636 if previousRoot != (common.Hash{}) { 1637 triedb.Dereference(previousRoot) 1638 } 1639 previousRoot = root 1640 } 1641 if report { 1642 nodes, imgs := triedb.Size() 1643 log.Info("Historical state regenerated", "block", current.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs) 1644 } 1645 if previousRoot != (common.Hash{}) { 1646 return triedb.Commit(previousRoot, report, nil) 1647 } 1648 return nil 1649 }