github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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 VNT consensus protocol. 18 package core 19 20 import ( 21 "errors" 22 "fmt" 23 "io" 24 "math/big" 25 "sync" 26 "sync/atomic" 27 "time" 28 29 lru "github.com/hashicorp/golang-lru" 30 "github.com/vntchain/go-vnt/common" 31 "github.com/vntchain/go-vnt/common/mclock" 32 "github.com/vntchain/go-vnt/consensus" 33 "github.com/vntchain/go-vnt/core/rawdb" 34 "github.com/vntchain/go-vnt/core/state" 35 "github.com/vntchain/go-vnt/core/types" 36 "github.com/vntchain/go-vnt/core/vm" 37 "github.com/vntchain/go-vnt/crypto" 38 "github.com/vntchain/go-vnt/event" 39 "github.com/vntchain/go-vnt/log" 40 "github.com/vntchain/go-vnt/metrics" 41 "github.com/vntchain/go-vnt/params" 42 "github.com/vntchain/go-vnt/rlp" 43 "github.com/vntchain/go-vnt/trie" 44 "github.com/vntchain/go-vnt/vntdb" 45 "gopkg.in/karalabe/cookiejar.v2/collections/prque" 46 ) 47 48 var ( 49 blockInsertTimer = metrics.NewRegisteredTimer("chain/inserts", nil) 50 51 ErrNoGenesis = errors.New("Genesis not found in chain") 52 ErrBlockIsNil = errors.New("block is nil") 53 ErrParentIsNil = errors.New("parent is nil") 54 ErrNotVoteForkBlock = errors.New("not vote for block on other forked chain") 55 ) 56 57 const ( 58 bodyCacheLimit = 256 59 blockCacheLimit = 256 60 maxFutureBlocks = 256 61 maxTimeFutureBlocks = 30 62 badBlockLimit = 10 63 triesInMemory = 128 64 65 // BlockChainVersion ensures that an incompatible database forces a resync from scratch. 66 BlockChainVersion = 3 67 ) 68 69 // CacheConfig contains the configuration values for the trie caching/pruning 70 // that's resident in a blockchain. 71 type CacheConfig struct { 72 Disabled bool // Whether to disable trie write caching (archive node) 73 TrieNodeLimit int // Memory limit (MB) at which to flush the current in-memory trie to disk 74 TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk 75 } 76 77 // BlockChain represents the canonical chain given a database with a genesis 78 // block. The Blockchain manages chain imports, reverts, chain reorganisations. 79 // 80 // Importing blocks in to the block chain happens according to the set of rules 81 // defined by the two stage Validator. Processing of blocks is done using the 82 // Processor which processes the included transaction. The validation of the state 83 // is done in the second part of the Validator. Failing results in aborting of 84 // the import. 85 // 86 // The BlockChain also helps in returning blocks from **any** chain included 87 // in the database as well as blocks that represents the canonical chain. It's 88 // important to note that GetBlock can return any block and does not need to be 89 // included in the canonical one where as GetBlockByNumber always represents the 90 // canonical chain. 91 type BlockChain struct { 92 chainConfig *params.ChainConfig // Chain & network configuration 93 cacheConfig *CacheConfig // Cache configuration for pruning 94 95 db vntdb.Database // Low level persistent database to store final content in 96 triegc *prque.Prque // Priority queue mapping block numbers to tries to gc 97 gcproc time.Duration // Accumulates canonical block processing for trie dumping 98 99 hc *HeaderChain 100 rmLogsFeed event.Feed 101 chainFeed event.Feed 102 chainSideFeed event.Feed 103 chainHeadFeed event.Feed 104 logsFeed event.Feed 105 scope event.SubscriptionScope 106 genesisBlock *types.Block 107 108 mu sync.RWMutex // global mutex for locking chain operations 109 chainmu sync.RWMutex // blockchain insertion lock 110 procmu sync.RWMutex // block processor lock 111 112 checkpoint int // checkpoint counts towards the new checkpoint 113 currentBlock atomic.Value // Current head of the block chain 114 currentFastBlock atomic.Value // Current head of the fast-sync chain (may be above the block chain!) 115 116 stateCache state.Database // State database to reuse between imports (contains state cache) 117 bodyCache *lru.Cache // Cache for the most recent block bodies 118 bodyRLPCache *lru.Cache // Cache for the most recent block bodies in RLP encoded format 119 blockCache *lru.Cache // Cache for the most recent entire blocks 120 futureBlocks *lru.Cache // future blocks are blocks added for later processing 121 122 quit chan struct{} // blockchain quit channel 123 running int32 // running must be called atomically 124 // procInterrupt must be atomically called 125 procInterrupt int32 // interrupt signaler for block processing 126 wg sync.WaitGroup // chain processing wait group for shutting down 127 128 engine consensus.Engine 129 processor Processor // block processor interface 130 validator Validator // block and state validator interface 131 vmConfig vm.Config 132 133 badBlocks *lru.Cache // Bad block cache 134 } 135 136 // NewBlockChain returns a fully initialised block chain using information 137 // available in the database. It initialises the default VNT Validator and 138 // Processor. 139 func NewBlockChain(db vntdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) { 140 if cacheConfig == nil { 141 cacheConfig = &CacheConfig{ 142 TrieNodeLimit: 256 * 1024 * 1024, 143 TrieTimeLimit: 5 * time.Minute, 144 } 145 } 146 bodyCache, _ := lru.New(bodyCacheLimit) 147 bodyRLPCache, _ := lru.New(bodyCacheLimit) 148 blockCache, _ := lru.New(blockCacheLimit) 149 futureBlocks, _ := lru.New(maxFutureBlocks) 150 badBlocks, _ := lru.New(badBlockLimit) 151 152 bc := &BlockChain{ 153 chainConfig: chainConfig, 154 cacheConfig: cacheConfig, 155 db: db, 156 triegc: prque.New(), 157 stateCache: state.NewDatabase(db), 158 quit: make(chan struct{}), 159 bodyCache: bodyCache, 160 bodyRLPCache: bodyRLPCache, 161 blockCache: blockCache, 162 futureBlocks: futureBlocks, 163 engine: engine, 164 vmConfig: vmConfig, 165 badBlocks: badBlocks, 166 } 167 bc.SetValidator(NewBlockValidator(chainConfig, bc, engine)) 168 bc.SetProcessor(NewStateProcessor(chainConfig, bc, engine)) 169 170 var err error 171 bc.hc, err = NewHeaderChain(db, chainConfig, engine, bc.getProcInterrupt) 172 if err != nil { 173 return nil, err 174 } 175 bc.genesisBlock = bc.GetBlockByNumber(0) 176 if bc.genesisBlock == nil { 177 return nil, ErrNoGenesis 178 } 179 if err := bc.loadLastState(); err != nil { 180 return nil, err 181 } 182 // Check the current state of the block hashes and make sure that we do not have any of the bad blocks in our chain 183 for hash := range BadHashes { 184 if header := bc.GetHeaderByHash(hash); header != nil { 185 // get the canonical block corresponding to the offending header's number 186 headerByNumber := bc.GetHeaderByNumber(header.Number.Uint64()) 187 // make sure the headerByNumber (if present) is in our current canonical chain 188 if headerByNumber != nil && headerByNumber.Hash() == header.Hash() { 189 log.Error("Found bad hash, rewinding chain", "number", header.Number, "hash", header.ParentHash) 190 bc.SetHead(header.Number.Uint64() - 1) 191 log.Error("Chain rewind was successful, resuming normal operation") 192 } 193 } 194 } 195 // Take ownership of this particular state 196 go bc.update() 197 return bc, nil 198 } 199 200 func (bc *BlockChain) getProcInterrupt() bool { 201 return atomic.LoadInt32(&bc.procInterrupt) == 1 202 } 203 204 // loadLastState loads the last known chain state from the database. This method 205 // assumes that the chain manager mutex is held. 206 func (bc *BlockChain) loadLastState() error { 207 // Restore the last known head block 208 head := rawdb.ReadHeadBlockHash(bc.db) 209 if head == (common.Hash{}) { 210 // Corrupt or empty database, init from scratch 211 log.Warn("Empty database, resetting chain") 212 return bc.Reset() 213 } 214 // Make sure the entire head block is available 215 currentBlock := bc.GetBlockByHash(head) 216 if currentBlock == nil { 217 // Corrupt or empty database, init from scratch 218 log.Warn("Head block missing, resetting chain", "hash", head) 219 return bc.Reset() 220 } 221 // Make sure the state associated with the block is available 222 if _, err := state.New(currentBlock.Root(), bc.stateCache); err != nil { 223 // Dangling block without a state associated, init from scratch 224 log.Warn("Head state missing, repairing chain", "number", currentBlock.Number(), "hash", currentBlock.Hash()) 225 if err := bc.repair(¤tBlock); err != nil { 226 return err 227 } 228 } 229 // Everything seems to be fine, set as the head block 230 bc.currentBlock.Store(currentBlock) 231 232 // Restore the last known head header 233 currentHeader := currentBlock.Header() 234 if head := rawdb.ReadHeadHeaderHash(bc.db); head != (common.Hash{}) { 235 if header := bc.GetHeaderByHash(head); header != nil { 236 currentHeader = header 237 } 238 } 239 bc.hc.SetCurrentHeader(currentHeader) 240 241 // Restore the last known head fast block 242 bc.currentFastBlock.Store(currentBlock) 243 if head := rawdb.ReadHeadFastBlockHash(bc.db); head != (common.Hash{}) { 244 if block := bc.GetBlockByHash(head); block != nil { 245 bc.currentFastBlock.Store(block) 246 } 247 } 248 249 // Issue a status log for the user 250 currentFastBlock := bc.CurrentFastBlock() 251 252 headerTd := bc.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64()) 253 blockTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) 254 fastTd := bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64()) 255 256 log.Info("Loaded most recent local header", "number", currentHeader.Number, "hash", currentHeader.Hash(), "td", headerTd) 257 log.Info("Loaded most recent local full block", "number", currentBlock.Number(), "hash", currentBlock.Hash(), "td", blockTd) 258 log.Info("Loaded most recent local fast block", "number", currentFastBlock.Number(), "hash", currentFastBlock.Hash(), "td", fastTd) 259 260 return nil 261 } 262 263 // SetHead rewinds the local chain to a new head. In the case of headers, everything 264 // above the new head will be deleted and the new one set. In the case of blocks 265 // though, the head may be further rewound if block bodies are missing (non-archive 266 // nodes after a fast sync). 267 func (bc *BlockChain) SetHead(head uint64) error { 268 log.Warn("Rewinding blockchain", "target", head) 269 270 bc.mu.Lock() 271 defer bc.mu.Unlock() 272 273 // Rewind the header chain, deleting all block bodies until then 274 delFn := func(hash common.Hash, num uint64) { 275 rawdb.DeleteBody(bc.db, hash, num) 276 } 277 bc.hc.SetHead(head, delFn) 278 currentHeader := bc.hc.CurrentHeader() 279 280 // Clear out any stale content from the caches 281 bc.bodyCache.Purge() 282 bc.bodyRLPCache.Purge() 283 bc.blockCache.Purge() 284 bc.futureBlocks.Purge() 285 286 // Rewind the block chain, ensuring we don't end up with a stateless head block 287 if currentBlock := bc.CurrentBlock(); currentBlock != nil && currentHeader.Number.Uint64() < currentBlock.NumberU64() { 288 bc.currentBlock.Store(bc.GetBlock(currentHeader.Hash(), currentHeader.Number.Uint64())) 289 } 290 if currentBlock := bc.CurrentBlock(); currentBlock != nil { 291 if _, err := state.New(currentBlock.Root(), bc.stateCache); err != nil { 292 // Rewound state missing, rolled back to before pivot, reset to genesis 293 bc.currentBlock.Store(bc.genesisBlock) 294 } 295 } 296 // Rewind the fast block in a simpleton way to the target head 297 if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock != nil && currentHeader.Number.Uint64() < currentFastBlock.NumberU64() { 298 bc.currentFastBlock.Store(bc.GetBlock(currentHeader.Hash(), currentHeader.Number.Uint64())) 299 } 300 // If either blocks reached nil, reset to the genesis state 301 if currentBlock := bc.CurrentBlock(); currentBlock == nil { 302 bc.currentBlock.Store(bc.genesisBlock) 303 } 304 if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock == nil { 305 bc.currentFastBlock.Store(bc.genesisBlock) 306 } 307 currentBlock := bc.CurrentBlock() 308 currentFastBlock := bc.CurrentFastBlock() 309 310 rawdb.WriteHeadBlockHash(bc.db, currentBlock.Hash()) 311 rawdb.WriteHeadFastBlockHash(bc.db, currentFastBlock.Hash()) 312 313 return bc.loadLastState() 314 } 315 316 // FastSyncCommitHead sets the current head block to the one defined by the hash 317 // irrelevant what the chain contents were prior. 318 func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error { 319 // Make sure that both the block as well at its state trie exists 320 block := bc.GetBlockByHash(hash) 321 if block == nil { 322 return fmt.Errorf("non existent block [%x…]", hash[:4]) 323 } 324 if _, err := trie.NewSecure(block.Root(), bc.stateCache.TrieDB(), 0); err != nil { 325 return err 326 } 327 // If all checks out, manually set the head block 328 bc.mu.Lock() 329 bc.currentBlock.Store(block) 330 bc.mu.Unlock() 331 332 log.Info("Committed new head block", "number", block.Number(), "hash", hash) 333 return nil 334 } 335 336 // GasLimit returns the gas limit of the current HEAD block. 337 func (bc *BlockChain) GasLimit() uint64 { 338 return bc.CurrentBlock().GasLimit() 339 } 340 341 // CurrentBlock retrieves the current head block of the canonical chain. The 342 // block is retrieved from the blockchain's internal cache. 343 func (bc *BlockChain) CurrentBlock() *types.Block { 344 return bc.currentBlock.Load().(*types.Block) 345 } 346 347 // CurrentFastBlock retrieves the current fast-sync head block of the canonical 348 // chain. The block is retrieved from the blockchain's internal cache. 349 func (bc *BlockChain) CurrentFastBlock() *types.Block { 350 return bc.currentFastBlock.Load().(*types.Block) 351 } 352 353 // SetProcessor sets the processor required for making state modifications. 354 func (bc *BlockChain) SetProcessor(processor Processor) { 355 bc.procmu.Lock() 356 defer bc.procmu.Unlock() 357 bc.processor = processor 358 } 359 360 // SetValidator sets the validator which is used to validate incoming blocks. 361 func (bc *BlockChain) SetValidator(validator Validator) { 362 bc.procmu.Lock() 363 defer bc.procmu.Unlock() 364 bc.validator = validator 365 } 366 367 // Validator returns the current validator. 368 func (bc *BlockChain) Validator() Validator { 369 bc.procmu.RLock() 370 defer bc.procmu.RUnlock() 371 return bc.validator 372 } 373 374 // Processor returns the current processor. 375 func (bc *BlockChain) Processor() Processor { 376 bc.procmu.RLock() 377 defer bc.procmu.RUnlock() 378 return bc.processor 379 } 380 381 // State returns a new mutable state based on the current HEAD block. 382 func (bc *BlockChain) State() (*state.StateDB, error) { 383 return bc.StateAt(bc.CurrentBlock().Root()) 384 } 385 386 // StateAt returns a new mutable state based on a particular point in time. 387 func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) { 388 return state.New(root, bc.stateCache) 389 } 390 391 // Reset purges the entire blockchain, restoring it to its genesis state. 392 func (bc *BlockChain) Reset() error { 393 return bc.ResetWithGenesisBlock(bc.genesisBlock) 394 } 395 396 // ResetWithGenesisBlock purges the entire blockchain, restoring it to the 397 // specified genesis state. 398 func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error { 399 // Dump the entire block chain and purge the caches 400 if err := bc.SetHead(0); err != nil { 401 return err 402 } 403 bc.mu.Lock() 404 defer bc.mu.Unlock() 405 406 // Prepare the genesis block and reinitialise the chain 407 if err := bc.hc.WriteTd(genesis.Hash(), genesis.NumberU64(), genesis.Difficulty()); err != nil { 408 log.Crit("Failed to write genesis block TD", "err", err) 409 } 410 rawdb.WriteBlock(bc.db, genesis) 411 412 bc.genesisBlock = genesis 413 bc.insert(bc.genesisBlock) 414 bc.currentBlock.Store(bc.genesisBlock) 415 bc.hc.SetGenesis(bc.genesisBlock.Header()) 416 bc.hc.SetCurrentHeader(bc.genesisBlock.Header()) 417 bc.currentFastBlock.Store(bc.genesisBlock) 418 419 return nil 420 } 421 422 // repair tries to repair the current blockchain by rolling back the current block 423 // until one with associated state is found. This is needed to fix incomplete db 424 // writes caused either by crashes/power outages, or simply non-committed tries. 425 // 426 // This method only rolls back the current block. The current header and current 427 // fast block are left intact. 428 func (bc *BlockChain) repair(head **types.Block) error { 429 for { 430 // Abort if we've rewound to a head block that does have associated state 431 if _, err := state.New((*head).Root(), bc.stateCache); err == nil { 432 log.Info("Rewound blockchain to past state", "number", (*head).Number(), "hash", (*head).Hash()) 433 return nil 434 } 435 // Otherwise rewind one block and recheck state availability there 436 (*head) = bc.GetBlock((*head).ParentHash(), (*head).NumberU64()-1) 437 } 438 } 439 440 // Export writes the active chain to the given writer. 441 func (bc *BlockChain) Export(w io.Writer) error { 442 return bc.ExportN(w, uint64(0), bc.CurrentBlock().NumberU64()) 443 } 444 445 // ExportN writes a subset of the active chain to the given writer. 446 func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error { 447 bc.mu.RLock() 448 defer bc.mu.RUnlock() 449 450 if first > last { 451 return fmt.Errorf("export failed: first (%d) is greater than last (%d)", first, last) 452 } 453 log.Info("Exporting batch of blocks", "count", last-first+1) 454 455 for nr := first; nr <= last; nr++ { 456 block := bc.GetBlockByNumber(nr) 457 if block == nil { 458 return fmt.Errorf("export failed on #%d: not found", nr) 459 } 460 461 if err := block.EncodeRLP(w); err != nil { 462 return err 463 } 464 } 465 466 return nil 467 } 468 469 // insert injects a new head block into the current block chain. This method 470 // assumes that the block is indeed a true head. It will also reset the head 471 // header and the head fast sync block to this very same block if they are older 472 // or if they are on a different side chain. 473 // 474 // Note, this function assumes that the `mu` mutex is held! 475 func (bc *BlockChain) insert(block *types.Block) { 476 // If the block is on a side chain or an unknown one, force other heads onto it too 477 updateHeads := rawdb.ReadCanonicalHash(bc.db, block.NumberU64()) != block.Hash() 478 479 // Add the block to the canonical chain number scheme and mark as the head 480 rawdb.WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64()) 481 rawdb.WriteHeadBlockHash(bc.db, block.Hash()) 482 483 bc.currentBlock.Store(block) 484 485 // If the block is better than our head or is on a different chain, force update heads 486 if updateHeads { 487 bc.hc.SetCurrentHeader(block.Header()) 488 rawdb.WriteHeadFastBlockHash(bc.db, block.Hash()) 489 490 bc.currentFastBlock.Store(block) 491 } 492 } 493 494 // Genesis retrieves the chain's genesis block. 495 func (bc *BlockChain) Genesis() *types.Block { 496 return bc.genesisBlock 497 } 498 499 // GetBody retrieves a block body (transactions) from the database by 500 // hash, caching it if found. 501 func (bc *BlockChain) GetBody(hash common.Hash) *types.Body { 502 // Short circuit if the body's already in the cache, retrieve otherwise 503 if cached, ok := bc.bodyCache.Get(hash); ok { 504 body := cached.(*types.Body) 505 return body 506 } 507 number := bc.hc.GetBlockNumber(hash) 508 if number == nil { 509 return nil 510 } 511 body := rawdb.ReadBody(bc.db, hash, *number) 512 if body == nil { 513 return nil 514 } 515 // Cache the found body for next time and return 516 bc.bodyCache.Add(hash, body) 517 return body 518 } 519 520 // GetBodyRLP retrieves a block body in RLP encoding from the database by hash, 521 // caching it if found. 522 func (bc *BlockChain) GetBodyRLP(hash common.Hash) rlp.RawValue { 523 // Short circuit if the body's already in the cache, retrieve otherwise 524 if cached, ok := bc.bodyRLPCache.Get(hash); ok { 525 return cached.(rlp.RawValue) 526 } 527 number := bc.hc.GetBlockNumber(hash) 528 if number == nil { 529 return nil 530 } 531 body := rawdb.ReadBodyRLP(bc.db, hash, *number) 532 if len(body) == 0 { 533 return nil 534 } 535 // Cache the found body for next time and return 536 bc.bodyRLPCache.Add(hash, body) 537 return body 538 } 539 540 // HasBlock checks if a block is fully present in the database or not. 541 func (bc *BlockChain) HasBlock(hash common.Hash, number uint64) bool { 542 if bc.blockCache.Contains(hash) { 543 return true 544 } 545 return rawdb.HasBody(bc.db, hash, number) 546 } 547 548 // HasState checks if state trie is fully present in the database or not. 549 func (bc *BlockChain) HasState(hash common.Hash) bool { 550 _, err := bc.stateCache.OpenTrie(hash) 551 return err == nil 552 } 553 554 // HasBlockAndState checks if a block and associated state trie is fully present 555 // in the database or not, caching it if present. 556 func (bc *BlockChain) HasBlockAndState(hash common.Hash, number uint64) bool { 557 // Check first that the block itself is known 558 block := bc.GetBlock(hash, number) 559 if block == nil { 560 return false 561 } 562 return bc.HasState(block.Root()) 563 } 564 565 // GetBlock retrieves a block from the database by hash and number, 566 // caching it if found. 567 func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { 568 // Short circuit if the block's already in the cache, retrieve otherwise 569 if block, ok := bc.blockCache.Get(hash); ok { 570 return block.(*types.Block) 571 } 572 block := rawdb.ReadBlock(bc.db, hash, number) 573 if block == nil { 574 return nil 575 } 576 // Cache the found block for next time and return 577 bc.blockCache.Add(block.Hash(), block) 578 return block 579 } 580 581 // GetBlockByHash retrieves a block from the database by hash, caching it if found. 582 func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { 583 number := bc.hc.GetBlockNumber(hash) 584 if number == nil { 585 return nil 586 } 587 return bc.GetBlock(hash, *number) 588 } 589 590 // GetBlockByNumber retrieves a block from the database by number, caching it 591 // (associated with its hash) if found. 592 func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block { 593 hash := rawdb.ReadCanonicalHash(bc.db, number) 594 if hash == (common.Hash{}) { 595 return nil 596 } 597 return bc.GetBlock(hash, number) 598 } 599 600 // GetReceiptsByHash retrieves the receipts for all transactions in a given block. 601 func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts { 602 number := rawdb.ReadHeaderNumber(bc.db, hash) 603 if number == nil { 604 return nil 605 } 606 return rawdb.ReadReceipts(bc.db, hash, *number) 607 } 608 609 // GetBlocksFromHash returns the block corresponding to hash and up to n-1 ancestors. 610 // [deprecated by vnt/62] 611 func (bc *BlockChain) GetBlocksFromHash(hash common.Hash, n int) (blocks []*types.Block) { 612 number := bc.hc.GetBlockNumber(hash) 613 if number == nil { 614 return nil 615 } 616 var emptyHash common.Hash 617 for i := 0; i < n && hash != emptyHash; i++ { 618 block := bc.GetBlock(hash, *number) 619 if block == nil { 620 break 621 } 622 blocks = append(blocks, block) 623 hash = block.ParentHash() 624 *number-- 625 } 626 return 627 } 628 629 // TrieNode retrieves a blob of data associated with a trie node (or code hash) 630 // either from ephemeral in-memory cache, or from persistent storage. 631 func (bc *BlockChain) TrieNode(hash common.Hash) ([]byte, error) { 632 return bc.stateCache.TrieDB().Node(hash) 633 } 634 635 // Stop stops the blockchain service. If any imports are currently in progress 636 // it will abort them using the procInterrupt. 637 func (bc *BlockChain) Stop() { 638 if !atomic.CompareAndSwapInt32(&bc.running, 0, 1) { 639 return 640 } 641 // Unsubscribe all subscriptions registered from blockchain 642 bc.scope.Close() 643 close(bc.quit) 644 atomic.StoreInt32(&bc.procInterrupt, 1) 645 646 bc.wg.Wait() 647 648 // Ensure the state of a recent block is also stored to disk before exiting. 649 // We're writing three different states to catch different restart scenarios: 650 // - HEAD: So we don't need to reprocess any blocks in the general case 651 // - HEAD-1: So we don't do large reorgs if our HEAD becomes an uncle, never happen 652 // - HEAD-127: So we have a hard limit on the number of blocks reexecuted 653 if !bc.cacheConfig.Disabled { 654 triedb := bc.stateCache.TrieDB() 655 656 for _, offset := range []uint64{0, 1, triesInMemory - 1} { 657 if number := bc.CurrentBlock().NumberU64(); number > offset { 658 recent := bc.GetBlockByNumber(number - offset) 659 660 log.Info("Writing cached state to disk", "block", recent.Number(), "hash", recent.Hash(), "root", recent.Root()) 661 if err := triedb.Commit(recent.Root(), true); err != nil { 662 log.Error("Failed to commit recent state trie", "err", err) 663 } 664 } 665 } 666 for !bc.triegc.Empty() { 667 triedb.Dereference(bc.triegc.PopItem().(common.Hash), common.Hash{}) 668 } 669 if size, _ := triedb.Size(); size != 0 { 670 log.Error("Dangling trie nodes after full cleanup") 671 } 672 } 673 log.Info("Blockchain manager stopped") 674 } 675 676 func (bc *BlockChain) procFutureBlocks() { 677 blocks := make([]*types.Block, 0, bc.futureBlocks.Len()) 678 for _, hash := range bc.futureBlocks.Keys() { 679 if block, exist := bc.futureBlocks.Peek(hash); exist { 680 blocks = append(blocks, block.(*types.Block)) 681 } 682 } 683 if len(blocks) > 0 { 684 types.BlockBy(types.Number).Sort(blocks) 685 686 // Insert one by one as chain insertion needs contiguous ancestry between blocks 687 for i := range blocks { 688 bc.InsertChain(blocks[i : i+1]) 689 } 690 } 691 } 692 693 // WriteStatus status of write 694 type WriteStatus byte 695 696 const ( 697 NonStatTy WriteStatus = iota 698 CanonStatTy 699 SideStatTy 700 ) 701 702 // Rollback is designed to remove a chain of links from the database that aren't 703 // certain enough to be valid. 704 func (bc *BlockChain) Rollback(chain []common.Hash) { 705 bc.mu.Lock() 706 defer bc.mu.Unlock() 707 708 for i := len(chain) - 1; i >= 0; i-- { 709 hash := chain[i] 710 711 currentHeader := bc.hc.CurrentHeader() 712 if currentHeader.Hash() == hash { 713 bc.hc.SetCurrentHeader(bc.GetHeader(currentHeader.ParentHash, currentHeader.Number.Uint64()-1)) 714 } 715 if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock.Hash() == hash { 716 newFastBlock := bc.GetBlock(currentFastBlock.ParentHash(), currentFastBlock.NumberU64()-1) 717 bc.currentFastBlock.Store(newFastBlock) 718 rawdb.WriteHeadFastBlockHash(bc.db, newFastBlock.Hash()) 719 } 720 if currentBlock := bc.CurrentBlock(); currentBlock.Hash() == hash { 721 newBlock := bc.GetBlock(currentBlock.ParentHash(), currentBlock.NumberU64()-1) 722 bc.currentBlock.Store(newBlock) 723 rawdb.WriteHeadBlockHash(bc.db, newBlock.Hash()) 724 } 725 } 726 } 727 728 // SetReceiptsData computes all the non-consensus fields of the receipts 729 func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts types.Receipts) error { 730 signer := types.MakeSigner(config, block.Number()) 731 732 transactions, logIndex := block.Transactions(), uint(0) 733 if len(transactions) != len(receipts) { 734 return errors.New("transaction and receipt count mismatch") 735 } 736 737 for j := 0; j < len(receipts); j++ { 738 // The transaction hash can be retrieved from the transaction itself 739 receipts[j].TxHash = transactions[j].Hash() 740 741 // The contract address can be derived from the transaction itself 742 if transactions[j].To() == nil { 743 // Deriving the signer is expensive, only do if it's actually needed 744 from, _ := types.Sender(signer, transactions[j]) 745 receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce()) 746 } 747 // The used gas can be calculated based on previous receipts 748 if j == 0 { 749 receipts[j].GasUsed = receipts[j].CumulativeGasUsed 750 } else { 751 receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed 752 } 753 // The derived log fields can simply be set from the block and transaction 754 for k := 0; k < len(receipts[j].Logs); k++ { 755 receipts[j].Logs[k].BlockNumber = block.NumberU64() 756 receipts[j].Logs[k].BlockHash = block.Hash() 757 receipts[j].Logs[k].TxHash = receipts[j].TxHash 758 receipts[j].Logs[k].TxIndex = uint(j) 759 receipts[j].Logs[k].Index = logIndex 760 logIndex++ 761 } 762 } 763 return nil 764 } 765 766 // InsertReceiptChain attempts to complete an already existing header chain with 767 // transaction and receipt data. 768 func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) { 769 bc.wg.Add(1) 770 defer bc.wg.Done() 771 772 // Do a sanity check that the provided chain is actually ordered and linked 773 for i := 1; i < len(blockChain); i++ { 774 if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() { 775 log.Error("Non contiguous receipt insert", "number", blockChain[i].Number(), "hash", blockChain[i].Hash(), "parent", blockChain[i].ParentHash(), 776 "prevnumber", blockChain[i-1].Number(), "prevhash", blockChain[i-1].Hash()) 777 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(), 778 blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4]) 779 } 780 } 781 782 var ( 783 stats = struct{ processed, ignored int32 }{} 784 start = time.Now() 785 bytes = 0 786 batch = bc.db.NewBatch() 787 ) 788 for i, block := range blockChain { 789 receipts := receiptChain[i] 790 // Short circuit insertion if shutting down or processing failed 791 if atomic.LoadInt32(&bc.procInterrupt) == 1 { 792 return 0, nil 793 } 794 // Short circuit if the owner header is unknown 795 if !bc.HasHeader(block.Hash(), block.NumberU64()) { 796 return i, fmt.Errorf("containing header #%d [%x…] unknown", block.Number(), block.Hash().Bytes()[:4]) 797 } 798 // Skip if the entire data is already known 799 if bc.HasBlock(block.Hash(), block.NumberU64()) { 800 stats.ignored++ 801 continue 802 } 803 // Compute all the non-consensus fields of the receipts 804 if err := SetReceiptsData(bc.chainConfig, block, receipts); err != nil { 805 return i, fmt.Errorf("failed to set receipts data: %v", err) 806 } 807 // Write all the data out into the database 808 rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body()) 809 rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts) 810 rawdb.WriteTxLookupEntries(batch, block) 811 812 stats.processed++ 813 814 if batch.ValueSize() >= vntdb.IdealBatchSize { 815 if err := batch.Write(); err != nil { 816 return 0, err 817 } 818 bytes += batch.ValueSize() 819 batch.Reset() 820 } 821 } 822 if batch.ValueSize() > 0 { 823 bytes += batch.ValueSize() 824 if err := batch.Write(); err != nil { 825 return 0, err 826 } 827 } 828 829 // Update the head fast sync block if better 830 bc.mu.Lock() 831 head := blockChain[len(blockChain)-1] 832 if td := bc.GetTd(head.Hash(), head.NumberU64()); td != nil { // Rewind may have occurred, skip in that case 833 currentFastBlock := bc.CurrentFastBlock() 834 if bc.GetTd(currentFastBlock.Hash(), currentFastBlock.NumberU64()).Cmp(td) < 0 { 835 rawdb.WriteHeadFastBlockHash(bc.db, head.Hash()) 836 bc.currentFastBlock.Store(head) 837 } 838 } 839 bc.mu.Unlock() 840 841 log.Info("Imported new block receipts", 842 "count", stats.processed, 843 "elapsed", common.PrettyDuration(time.Since(start)), 844 "number", head.Number(), 845 "hash", head.Hash(), 846 "size", common.StorageSize(bytes), 847 "ignored", stats.ignored) 848 return 0, nil 849 } 850 851 var lastWrite uint64 852 853 // WriteBlockWithoutState writes only the block and its metadata to the database, 854 // but does not write any state. This is used to construct competing side forks 855 // up to the point where they exceed the canonical total difficulty. 856 func (bc *BlockChain) WriteBlockWithoutState(block *types.Block, td *big.Int) (err error) { 857 bc.wg.Add(1) 858 defer bc.wg.Done() 859 860 if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), td); err != nil { 861 return err 862 } 863 rawdb.WriteBlock(bc.db, block) 864 865 return nil 866 } 867 868 // WriteBlockWithState writes the block and all associated state to the database. 869 func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) (status WriteStatus, err error) { 870 bc.wg.Add(1) 871 defer bc.wg.Done() 872 873 // Calculate the total difficulty of the block 874 ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1) 875 if ptd == nil { 876 return NonStatTy, consensus.ErrUnknownAncestor 877 } 878 // Make sure no inconsistent state is leaked during insertion 879 bc.mu.Lock() 880 defer bc.mu.Unlock() 881 882 currentBlock := bc.CurrentBlock() 883 localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) 884 externTd := new(big.Int).Add(block.Difficulty(), ptd) 885 886 // Irrelevant of the canonical status, write the block itself to the database 887 if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil { 888 return NonStatTy, err 889 } 890 // Write other block data using a batch. 891 batch := bc.db.NewBatch() 892 rawdb.WriteBlock(batch, block) 893 894 root, err := state.Commit(true) 895 if err != nil { 896 return NonStatTy, err 897 } 898 triedb := bc.stateCache.TrieDB() 899 900 // If we're running an archive node, always flush 901 if bc.cacheConfig.Disabled { 902 if err := triedb.Commit(root, false); err != nil { 903 return NonStatTy, err 904 } 905 } else { 906 // Full but not archive node, do proper garbage collection 907 triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive 908 bc.triegc.Push(root, -float32(block.NumberU64())) 909 910 if current := block.NumberU64(); current > triesInMemory { 911 // If we exceeded our memory allowance, flush matured singleton nodes to disk 912 var ( 913 nodes, imgs = triedb.Size() 914 limit = common.StorageSize(bc.cacheConfig.TrieNodeLimit) * 1024 * 1024 915 ) 916 if nodes > limit || imgs > 4*1024*1024 { 917 triedb.Cap(limit - vntdb.IdealBatchSize) 918 } 919 // Find the next state trie we need to commit 920 header := bc.GetHeaderByNumber(current - triesInMemory) 921 chosen := header.Number.Uint64() 922 923 // If we exceeded out time allowance, flush an entire trie to disk 924 if bc.gcproc > bc.cacheConfig.TrieTimeLimit { 925 // If we're exceeding limits but haven't reached a large enough memory gap, 926 // warn the user that the system is becoming unstable. 927 if chosen < lastWrite+triesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit { 928 log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/triesInMemory) 929 } 930 // Flush an entire trie and restart the counters 931 triedb.Commit(header.Root, true) 932 lastWrite = chosen 933 bc.gcproc = 0 934 } 935 // Garbage collect anything below our required write retention 936 for !bc.triegc.Empty() { 937 root, number := bc.triegc.Pop() 938 if uint64(-number) > chosen { 939 bc.triegc.Push(root, number) 940 break 941 } 942 triedb.Dereference(root.(common.Hash), common.Hash{}) 943 } 944 } 945 } 946 rawdb.WriteReceipts(batch, block.Hash(), block.NumberU64(), receipts) 947 948 // Choose the longest after LIB. 949 // If same length choose the early produced block. 950 // But block still can write to db, because it is after lib. 951 // reorg is true means this block will be head block after this block inserted. 952 reorg := externTd.Cmp(localTd) > 0 953 currentBlock = bc.CurrentBlock() 954 if !reorg && externTd.Cmp(localTd) == 0 { 955 if block.Hash() == currentBlock.Hash() { 956 return NonStatTy, fmt.Errorf("block already in chain") 957 } 958 959 // two blocks choose by timestamp 960 ret := block.Time().Cmp(currentBlock.Time()) 961 if ret < 0 { 962 reorg = true 963 log.Info("Two blocks have same difficulty this block is early", "new head block", block.Hash(), 964 "time", block.Time().String(), "old head block", currentBlock.Hash(), "time", currentBlock.Time().String()) 965 } else if ret > 0 { 966 reorg = false 967 log.Info("Two blocks have same difficulty this block is later", "head block", 968 currentBlock.Hash(), "time", currentBlock.Time().String(), "this block", block.Hash(), "time", block.Time().String()) 969 } else { 970 log.Info("Two blocks have same difficulty and same timestamp", "head block", 971 currentBlock.Hash(), "time", currentBlock.Time().String(), "bad block", block.Hash(), "time", block.Time().String()) 972 return NonStatTy, fmt.Errorf("two blocks have same height and same tiemstamp") 973 } 974 } 975 if reorg { 976 // Reorganise the chain if the parent is not the head block 977 if block.ParentHash() != currentBlock.Hash() { 978 if err := bc.reorg(currentBlock, block); err != nil { 979 return NonStatTy, err 980 } 981 } 982 // Write the positional metadata for transaction/receipt lookups and preimages 983 rawdb.WriteTxLookupEntries(batch, block) 984 rawdb.WritePreimages(batch, block.NumberU64(), state.Preimages()) 985 986 status = CanonStatTy 987 } else { 988 status = SideStatTy 989 } 990 if err := batch.Write(); err != nil { 991 return NonStatTy, err 992 } 993 994 // Set new head. 995 if status == CanonStatTy { 996 bc.insert(block) 997 } 998 bc.futureBlocks.Remove(block.Hash()) 999 return status, nil 1000 } 1001 1002 // InsertChain attempts to insert the given batch of blocks in to the canonical 1003 // chain or, otherwise, create a fork. If an error is returned it will return 1004 // the index number of the failing block as well an error describing what went 1005 // wrong. 1006 // 1007 // After insertion is done, all accumulated events will be fired. 1008 func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) { 1009 n, events, logs, err := bc.insertChain(chain) 1010 bc.PostChainEvents(events, logs) 1011 return n, err 1012 } 1013 1014 // insertChain will execute the actual chain insertion and event aggregation. The 1015 // only reason this method exists as a separate one is to make locking cleaner 1016 // with deferred statements. 1017 func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*types.Log, error) { 1018 // Sanity check that we have something meaningful to import 1019 if len(chain) == 0 { 1020 return 0, nil, nil, nil 1021 } 1022 // Do a sanity check that the provided chain is actually ordered and linked 1023 for i := 1; i < len(chain); i++ { 1024 if chain[i].NumberU64() != chain[i-1].NumberU64()+1 || chain[i].ParentHash() != chain[i-1].Hash() { 1025 // Chain broke ancestry, log a messge (programming error) and skip insertion 1026 log.Error("Non contiguous block insert", "number", chain[i].Number(), "hash", chain[i].Hash(), 1027 "parent", chain[i].ParentHash(), "prevnumber", chain[i-1].Number(), "prevhash", chain[i-1].Hash()) 1028 1029 return 0, nil, nil, fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, chain[i-1].NumberU64(), 1030 chain[i-1].Hash().Bytes()[:4], i, chain[i].NumberU64(), chain[i].Hash().Bytes()[:4], chain[i].ParentHash().Bytes()[:4]) 1031 } 1032 } 1033 // Pre-checks passed, start the full block imports 1034 bc.wg.Add(1) 1035 defer bc.wg.Done() 1036 1037 bc.chainmu.Lock() 1038 defer bc.chainmu.Unlock() 1039 1040 // A queued approach to delivering events. This is generally 1041 // faster than direct delivery and requires much less mutex 1042 // acquiring. 1043 var ( 1044 stats = insertStats{startTime: mclock.Now()} 1045 events = make([]interface{}, 0, len(chain)) 1046 lastCanon *types.Block 1047 coalescedLogs []*types.Log 1048 ) 1049 // Start the parallel header verifier 1050 headers := make([]*types.Header, len(chain)) 1051 seals := make([]bool, len(chain)) 1052 1053 for i, block := range chain { 1054 headers[i] = block.Header() 1055 seals[i] = true 1056 } 1057 abort, results := bc.engine.VerifyHeaders(bc, headers, seals) 1058 defer close(abort) 1059 1060 // Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss) 1061 senderCacher.recoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number()), chain) 1062 1063 // Iterate over the blocks and insert when the verifier permits 1064 for i, block := range chain { 1065 // If the chain is terminating, stop processing blocks 1066 if atomic.LoadInt32(&bc.procInterrupt) == 1 { 1067 log.Debug("Premature abort during blocks processing") 1068 break 1069 } 1070 // If the header is a banned one, straight out abort 1071 if BadHashes[block.Hash()] { 1072 bc.reportBlock(block, nil, ErrBlacklistedHash) 1073 return i, events, coalescedLogs, ErrBlacklistedHash 1074 } 1075 // Wait for the block's verification to complete 1076 bstart := time.Now() 1077 1078 // Skip blocks already in the chain 1079 if b := bc.GetBlockByHash(block.Hash()); b != nil { 1080 continue 1081 } 1082 1083 // Block before last irreversible block can not be inserted 1084 // chainmu is required. current block would not change before 1085 // this block write finish. 1086 // Using current block to check block instead of using 1087 // lastIrreversibleBlk is to avoid this scenario that 1088 // current block is genesis block and it has no parent 1089 // block as LIB. 1090 cur := bc.CurrentBlock() 1091 if block.NumberU64() < cur.NumberU64() { 1092 return i, events, coalescedLogs, ErrBlockBeforeLIB 1093 } 1094 if block.NumberU64() == cur.NumberU64() && block.ParentHash() != cur.ParentHash() { 1095 return i, events, coalescedLogs, ErrForkBlockParentIsNotLIB 1096 } 1097 1098 err := <-results 1099 if err == nil { 1100 err = bc.Validator().ValidateBody(block) 1101 } 1102 switch { 1103 case err == ErrKnownBlock: 1104 // Block and stateDb both already known. However if the current block is below 1105 // this number we did a rollback and we should reimport it nonetheless. 1106 if bc.CurrentBlock().NumberU64() >= block.NumberU64() { 1107 stats.ignored++ 1108 continue 1109 } 1110 1111 case err == consensus.ErrFutureBlock: 1112 // Allow up to MaxFuture second in the future blocks. If this limit is exceeded 1113 // the chain is discarded and processed at a later time if given. 1114 max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks) 1115 if block.Time().Cmp(max) > 0 { 1116 return i, events, coalescedLogs, fmt.Errorf("future block: %v > %v", block.Time(), max) 1117 } 1118 bc.futureBlocks.Add(block.Hash(), block) 1119 stats.queued++ 1120 continue 1121 1122 case err == consensus.ErrUnknownAncestor && bc.futureBlocks.Contains(block.ParentHash()): 1123 bc.futureBlocks.Add(block.Hash(), block) 1124 stats.queued++ 1125 continue 1126 1127 case err == consensus.ErrPrunedAncestor: 1128 // Block competing with the canonical chain, store in the db, but don't process 1129 // until the competitor TD goes above the canonical TD 1130 currentBlock := bc.CurrentBlock() 1131 localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) 1132 externTd := new(big.Int).Add(bc.GetTd(block.ParentHash(), block.NumberU64()-1), block.Difficulty()) 1133 if localTd.Cmp(externTd) > 0 { 1134 if err = bc.WriteBlockWithoutState(block, externTd); err != nil { 1135 return i, events, coalescedLogs, err 1136 } 1137 continue 1138 } 1139 // Competitor chain beat canonical, gather all blocks from the common ancestor 1140 var winner []*types.Block 1141 1142 parent := bc.GetBlock(block.ParentHash(), block.NumberU64()-1) 1143 for !bc.HasState(parent.Root()) { 1144 winner = append(winner, parent) 1145 parent = bc.GetBlock(parent.ParentHash(), parent.NumberU64()-1) 1146 } 1147 for j := 0; j < len(winner)/2; j++ { 1148 winner[j], winner[len(winner)-1-j] = winner[len(winner)-1-j], winner[j] 1149 } 1150 // Import all the pruned blocks to make the stateDb available 1151 bc.chainmu.Unlock() 1152 _, evs, logs, err := bc.insertChain(winner) 1153 bc.chainmu.Lock() 1154 events, coalescedLogs = evs, logs 1155 1156 if err != nil { 1157 return i, events, coalescedLogs, err 1158 } 1159 1160 case err != nil: 1161 bc.reportBlock(block, nil, err) 1162 return i, events, coalescedLogs, err 1163 } 1164 // Create a new statedb using the parent block and report an 1165 // error if it fails. 1166 var parent *types.Block 1167 if i == 0 { 1168 parent = bc.GetBlock(block.ParentHash(), block.NumberU64()-1) 1169 } else { 1170 parent = chain[i-1] 1171 } 1172 stateDb, err := state.New(parent.Root(), bc.stateCache) 1173 if err != nil { 1174 return i, events, coalescedLogs, err 1175 } 1176 1177 // Verify the witness list using the parent's state 1178 if err := bc.engine.VerifyWitnesses(block.Header(), stateDb, parent.Header()); err != nil { 1179 return i, events, coalescedLogs, err 1180 } 1181 1182 // Verify commit msg 1183 if err := bc.engine.VerifyCommitMsg(block); err != nil { 1184 return i, events, coalescedLogs, fmt.Errorf("commit msg error: %s", err) 1185 } 1186 1187 // Process block using the parent state as reference point. 1188 receipts, logs, usedGas, err := bc.processor.Process(block, stateDb, bc.vmConfig) 1189 if err != nil { 1190 bc.reportBlock(block, receipts, err) 1191 return i, events, coalescedLogs, err 1192 } 1193 // Validate the state using the default validator 1194 err = bc.Validator().ValidateState(block, parent, stateDb, receipts, usedGas) 1195 if err != nil { 1196 bc.reportBlock(block, receipts, err) 1197 return i, events, coalescedLogs, err 1198 } 1199 proctime := time.Since(bstart) 1200 1201 // Write the block to the chain and get the status. 1202 status, err := bc.WriteBlockWithState(block, receipts, stateDb) 1203 if err != nil { 1204 return i, events, coalescedLogs, err 1205 } 1206 switch status { 1207 case CanonStatTy: 1208 log.Debug("Inserted new block", "number", block.Number(), "hash", block.Hash().String(), 1209 "txs", len(block.Transactions()), "gas", block.GasUsed(), "elapsed", common.PrettyDuration(time.Since(bstart))) 1210 1211 coalescedLogs = append(coalescedLogs, logs...) 1212 blockInsertTimer.UpdateSince(bstart) 1213 events = append(events, ChainEvent{block, block.Hash(), logs}) 1214 lastCanon = block 1215 1216 // Only count canonical blocks for GC processing time 1217 bc.gcproc += proctime 1218 1219 case SideStatTy: 1220 log.Debug("Inserted forked block", "number", block.Number(), "hash", block.Hash(), "diff", block.Difficulty(), "elapsed", 1221 common.PrettyDuration(time.Since(bstart)), "txs", len(block.Transactions()), "gas", block.GasUsed()) 1222 1223 blockInsertTimer.UpdateSince(bstart) 1224 events = append(events, ChainSideEvent{block}) 1225 } 1226 1227 stats.processed++ 1228 stats.usedGas += usedGas 1229 1230 cache, _ := bc.stateCache.TrieDB().Size() 1231 stats.report(chain, i, cache) 1232 } 1233 // Append a single chain head event if we've progressed the chain 1234 if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() { 1235 events = append(events, ChainHeadEvent{lastCanon}) 1236 } 1237 return 0, events, coalescedLogs, nil 1238 } 1239 1240 // lastIrreversibleBlk returns the last irreversible block. 1241 // Last irreversible block is the parent block of current block. 1242 func (bc *BlockChain) lastIrreversibleBlk() *types.Block { 1243 return bc.GetBlockByHash(bc.CurrentBlock().ParentHash()) 1244 } 1245 1246 // insertStats tracks and reports on block insertion. 1247 type insertStats struct { 1248 queued, processed, ignored int 1249 usedGas uint64 1250 lastIndex int 1251 startTime mclock.AbsTime 1252 } 1253 1254 // statsReportLimit is the time limit during import after which we always print 1255 // out progress. This avoids the user wondering what's going on. 1256 const statsReportLimit = 8 * time.Second 1257 1258 // report prints statistics if some number of blocks have been processed 1259 // or more than a few seconds have passed since the last message. 1260 func (st *insertStats) report(chain []*types.Block, index int, cache common.StorageSize) { 1261 // Fetch the timings for the batch 1262 var ( 1263 now = mclock.Now() 1264 elapsed = time.Duration(now) - time.Duration(st.startTime) 1265 ) 1266 // If we're at the last block of the batch or report period reached, log 1267 if index == len(chain)-1 || elapsed >= statsReportLimit { 1268 var ( 1269 end = chain[index] 1270 txs = countTransactions(chain[st.lastIndex : index+1]) 1271 ) 1272 context := []interface{}{ 1273 "blocks", st.processed, "txs", txs, "mgas", float64(st.usedGas) / 1000000, 1274 "elapsed", common.PrettyDuration(elapsed), "mgasps", float64(st.usedGas) * 1000 / float64(elapsed), 1275 "number", end.Number(), "hash", end.Hash(), "cache", cache, 1276 } 1277 if st.queued > 0 { 1278 context = append(context, []interface{}{"queued", st.queued}...) 1279 } 1280 if st.ignored > 0 { 1281 context = append(context, []interface{}{"ignored", st.ignored}...) 1282 } 1283 log.Info("Imported new chain segment", context...) 1284 1285 *st = insertStats{startTime: now, lastIndex: index + 1} 1286 } 1287 } 1288 1289 func countTransactions(chain []*types.Block) (c int) { 1290 for _, b := range chain { 1291 c += len(b.Transactions()) 1292 } 1293 return c 1294 } 1295 1296 // reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them 1297 // to be part of the new canonical chain and accumulates potential missing transactions and post an 1298 // event about them 1299 func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { 1300 var ( 1301 newChain types.Blocks 1302 oldChain types.Blocks 1303 commonBlock *types.Block 1304 deletedTxs types.Transactions 1305 deletedLogs []*types.Log 1306 // collectLogs collects the logs that were generated during the 1307 // processing of the block that corresponds with the given hash. 1308 // These logs are later announced as deleted. 1309 collectLogs = func(hash common.Hash) { 1310 // Coalesce logs and set 'Removed'. 1311 number := bc.hc.GetBlockNumber(hash) 1312 if number == nil { 1313 return 1314 } 1315 receipts := rawdb.ReadReceipts(bc.db, hash, *number) 1316 for _, receipt := range receipts { 1317 for _, log := range receipt.Logs { 1318 del := *log 1319 del.Removed = true 1320 deletedLogs = append(deletedLogs, &del) 1321 } 1322 } 1323 } 1324 ) 1325 1326 lastIrreversibleBlk := bc.lastIrreversibleBlk() 1327 // first reduce whoever is higher bound 1328 if oldBlock.NumberU64() > newBlock.NumberU64() { 1329 // reduce old chain 1330 for ; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1) { 1331 oldChain = append(oldChain, oldBlock) 1332 deletedTxs = append(deletedTxs, oldBlock.Transactions()...) 1333 1334 collectLogs(oldBlock.Hash()) 1335 } 1336 } else { 1337 // reduce new chain and append new chain blocks for inserting later on 1338 for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) { 1339 newChain = append(newChain, newBlock) 1340 } 1341 } 1342 if oldBlock == nil { 1343 return fmt.Errorf("Invalid old chain") 1344 } 1345 if newBlock == nil { 1346 return fmt.Errorf("Invalid new chain") 1347 } 1348 1349 for { 1350 if oldBlock.Hash() == newBlock.Hash() { 1351 commonBlock = oldBlock 1352 break 1353 } 1354 1355 oldChain = append(oldChain, oldBlock) 1356 newChain = append(newChain, newBlock) 1357 deletedTxs = append(deletedTxs, oldBlock.Transactions()...) 1358 collectLogs(oldBlock.Hash()) 1359 1360 oldBlock, newBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1), bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) 1361 if oldBlock == nil { 1362 return fmt.Errorf("Invalid old chain") 1363 } 1364 if newBlock == nil { 1365 return fmt.Errorf("Invalid new chain") 1366 } 1367 } 1368 1369 // Ensure blocks before last irreversible block can not be reorg 1370 if oldChain[len(oldChain)-1].NumberU64() <= lastIrreversibleBlk.NumberU64() { 1371 return fmt.Errorf("Invalid reorganization to rollback the irreversible blocks") 1372 } 1373 1374 // Ensure the user sees large reorgs 1375 if len(oldChain) > 0 && len(newChain) > 0 { 1376 logFn := log.Debug 1377 if len(oldChain) > 63 { 1378 logFn = log.Warn 1379 } 1380 logFn("Chain split detected", "number", commonBlock.Number(), "hash", commonBlock.Hash(), 1381 "drop", len(oldChain), "dropfrom", oldChain[0].Hash(), "add", len(newChain), "addfrom", newChain[0].Hash()) 1382 } else { 1383 log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "newnum", newBlock.Number(), "newhash", newBlock.Hash()) 1384 } 1385 // Insert the new chain, taking care of the proper incremental order 1386 var addedTxs types.Transactions 1387 for i := len(newChain) - 1; i >= 0; i-- { 1388 // insert the block in the canonical way, re-writing history 1389 bc.insert(newChain[i]) 1390 // write lookup entries for hash based transaction/receipt searches 1391 rawdb.WriteTxLookupEntries(bc.db, newChain[i]) 1392 addedTxs = append(addedTxs, newChain[i].Transactions()...) 1393 } 1394 // calculate the difference between deleted and added transactions 1395 diff := types.TxDifference(deletedTxs, addedTxs) 1396 // When transactions get deleted from the database that means the 1397 // receipts that were created in the fork must also be deleted 1398 for _, tx := range diff { 1399 rawdb.DeleteTxLookupEntry(bc.db, tx.Hash()) 1400 } 1401 if len(deletedLogs) > 0 { 1402 go bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs}) 1403 } 1404 if len(oldChain) > 0 { 1405 go func() { 1406 for _, block := range oldChain { 1407 bc.chainSideFeed.Send(ChainSideEvent{Block: block}) 1408 } 1409 }() 1410 } 1411 1412 return nil 1413 } 1414 1415 // PostChainEvents iterates over the events generated by a chain insertion and 1416 // posts them into the event feed. 1417 // TODO: Should not expose PostChainEvents. The chain events should be posted in WriteBlock. 1418 func (bc *BlockChain) PostChainEvents(events []interface{}, logs []*types.Log) { 1419 // post event logs for further processing 1420 if logs != nil { 1421 bc.logsFeed.Send(logs) 1422 } 1423 for _, event := range events { 1424 switch ev := event.(type) { 1425 case ChainEvent: 1426 bc.chainFeed.Send(ev) 1427 1428 case ChainHeadEvent: 1429 bc.chainHeadFeed.Send(ev) 1430 1431 case ChainSideEvent: 1432 bc.chainSideFeed.Send(ev) 1433 } 1434 } 1435 } 1436 1437 func (bc *BlockChain) update() { 1438 futureTimer := time.NewTicker(5 * time.Second) 1439 defer futureTimer.Stop() 1440 for { 1441 select { 1442 case <-futureTimer.C: 1443 bc.procFutureBlocks() 1444 case <-bc.quit: 1445 return 1446 } 1447 } 1448 } 1449 1450 // BadBlocks returns a list of the last 'bad blocks' that the client has seen on the network 1451 func (bc *BlockChain) BadBlocks() []*types.Block { 1452 blocks := make([]*types.Block, 0, bc.badBlocks.Len()) 1453 for _, hash := range bc.badBlocks.Keys() { 1454 if blk, exist := bc.badBlocks.Peek(hash); exist { 1455 block := blk.(*types.Block) 1456 blocks = append(blocks, block) 1457 } 1458 } 1459 return blocks 1460 } 1461 1462 // addBadBlock adds a bad block to the bad-block LRU cache 1463 func (bc *BlockChain) addBadBlock(block *types.Block) { 1464 bc.badBlocks.Add(block.Hash(), block) 1465 } 1466 1467 // reportBlock logs a bad block error. 1468 func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, err error) { 1469 bc.addBadBlock(block) 1470 1471 var receiptString string 1472 for _, receipt := range receipts { 1473 receiptString += fmt.Sprintf("\t%v\n", receipt) 1474 } 1475 log.Error(fmt.Sprintf(` 1476 ########## BAD BLOCK ######### 1477 Chain config: %v 1478 1479 Number: %v 1480 Hash: 0x%x 1481 %v 1482 1483 Error: %v 1484 ############################## 1485 `, bc.chainConfig, block.Number(), block.Hash(), receiptString, err)) 1486 } 1487 1488 // InsertHeaderChain attempts to insert the given header chain in to the local 1489 // chain, possibly creating a reorg. If an error is returned, it will return the 1490 // index number of the failing header as well an error describing what went wrong. 1491 // 1492 // The verify parameter can be used to fine tune whether nonce verification 1493 // should be done or not. The reason behind the optional check is because some 1494 // of the header retrieval mechanisms already need to verify nonces, as well as 1495 // because nonces can be verified sparsely, not needing to check each. 1496 func (bc *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) { 1497 start := time.Now() 1498 if i, err := bc.hc.ValidateHeaderChain(chain, checkFreq); err != nil { 1499 return i, err 1500 } 1501 1502 // Make sure only one thread manipulates the chain at once 1503 bc.chainmu.Lock() 1504 defer bc.chainmu.Unlock() 1505 1506 bc.wg.Add(1) 1507 defer bc.wg.Done() 1508 1509 whFunc := func(header *types.Header) error { 1510 bc.mu.Lock() 1511 defer bc.mu.Unlock() 1512 1513 _, err := bc.hc.WriteHeader(header) 1514 return err 1515 } 1516 1517 return bc.hc.InsertHeaderChain(chain, whFunc, start) 1518 } 1519 1520 // writeHeader writes a header into the local chain, given that its parent is 1521 // already known. If the total difficulty of the newly inserted header becomes 1522 // greater than the current known TD, the canonical chain is re-routed. 1523 // 1524 // Note: This method is not concurrent-safe with inserting blocks simultaneously 1525 // into the chain, as side effects caused by reorganisations cannot be emulated 1526 // without the real blocks. Hence, writing headers directly should only be done 1527 // in two scenarios: pure-header mode of operation (light clients), or properly 1528 // separated header/block phases (non-archive clients). 1529 func (bc *BlockChain) writeHeader(header *types.Header) error { 1530 bc.wg.Add(1) 1531 defer bc.wg.Done() 1532 1533 bc.mu.Lock() 1534 defer bc.mu.Unlock() 1535 1536 _, err := bc.hc.WriteHeader(header) 1537 return err 1538 } 1539 1540 // CurrentHeader retrieves the current head header of the canonical chain. The 1541 // header is retrieved from the HeaderChain's internal cache. 1542 func (bc *BlockChain) CurrentHeader() *types.Header { 1543 return bc.hc.CurrentHeader() 1544 } 1545 1546 // GetTd retrieves a block's total difficulty in the canonical chain from the 1547 // database by hash and number, caching it if found. 1548 func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int { 1549 return bc.hc.GetTd(hash, number) 1550 } 1551 1552 // GetTdByHash retrieves a block's total difficulty in the canonical chain from the 1553 // database by hash, caching it if found. 1554 func (bc *BlockChain) GetTdByHash(hash common.Hash) *big.Int { 1555 return bc.hc.GetTdByHash(hash) 1556 } 1557 1558 // GetHeader retrieves a block header from the database by hash and number, 1559 // caching it if found. 1560 func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header { 1561 return bc.hc.GetHeader(hash, number) 1562 } 1563 1564 // GetHeaderByHash retrieves a block header from the database by hash, caching it if 1565 // found. 1566 func (bc *BlockChain) GetHeaderByHash(hash common.Hash) *types.Header { 1567 return bc.hc.GetHeaderByHash(hash) 1568 } 1569 1570 // HasHeader checks if a block header is present in the database or not, caching 1571 // it if present. 1572 func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool { 1573 return bc.hc.HasHeader(hash, number) 1574 } 1575 1576 // GetBlockHashesFromHash retrieves a number of block hashes starting at a given 1577 // hash, fetching towards the genesis block. 1578 func (bc *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash { 1579 return bc.hc.GetBlockHashesFromHash(hash, max) 1580 } 1581 1582 // GetAncestor retrieves the Nth ancestor of a given block. It assumes that either the given block or 1583 // a close ancestor of it is canonical. maxNonCanonical points to a downwards counter limiting the 1584 // number of blocks to be individually checked before we reach the canonical chain. 1585 // 1586 // Note: ancestor == 0 returns the same block, 1 returns its parent and so on. 1587 func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, maxNonCanonical *uint64) (common.Hash, uint64) { 1588 bc.chainmu.Lock() 1589 defer bc.chainmu.Unlock() 1590 1591 return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical) 1592 } 1593 1594 // GetHeaderByNumber retrieves a block header from the database by number, 1595 // caching it (associated with its hash) if found. 1596 func (bc *BlockChain) GetHeaderByNumber(number uint64) *types.Header { 1597 return bc.hc.GetHeaderByNumber(number) 1598 } 1599 1600 // Config retrieves the blockchain's chain configuration. 1601 func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig } 1602 1603 // Engine retrieves the blockchain's consensus engine. 1604 func (bc *BlockChain) Engine() consensus.Engine { return bc.engine } 1605 1606 // SubscribeRemovedLogsEvent registers a subscription of RemovedLogsEvent. 1607 func (bc *BlockChain) SubscribeRemovedLogsEvent(ch chan<- RemovedLogsEvent) event.Subscription { 1608 return bc.scope.Track(bc.rmLogsFeed.Subscribe(ch)) 1609 } 1610 1611 // SubscribeChainEvent registers a subscription of ChainEvent. 1612 func (bc *BlockChain) SubscribeChainEvent(ch chan<- ChainEvent) event.Subscription { 1613 return bc.scope.Track(bc.chainFeed.Subscribe(ch)) 1614 } 1615 1616 // SubscribeChainHeadEvent registers a subscription of ChainHeadEvent. 1617 func (bc *BlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription { 1618 return bc.scope.Track(bc.chainHeadFeed.Subscribe(ch)) 1619 } 1620 1621 // SubscribeChainSideEvent registers a subscription of ChainSideEvent. 1622 func (bc *BlockChain) SubscribeChainSideEvent(ch chan<- ChainSideEvent) event.Subscription { 1623 return bc.scope.Track(bc.chainSideFeed.Subscribe(ch)) 1624 } 1625 1626 // SubscribeLogsEvent registers a subscription of []*types.Log. 1627 func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { 1628 return bc.scope.Track(bc.logsFeed.Subscribe(ch)) 1629 } 1630 1631 // VerifyBlockForBft only verify block behind the local canonical chain. 1632 func (bc *BlockChain) VerifyBlockForBft(block *types.Block) (types.Receipts, []*types.Log, uint64, error) { 1633 if block == nil { 1634 return nil, nil, 0, ErrBlockIsNil 1635 } 1636 1637 // Parent should be head block 1638 headBlock := bc.CurrentBlock() 1639 parentHash := block.ParentHash() 1640 if headBlock.Hash() != parentHash { 1641 return nil, nil, 0, ErrNotVoteForkBlock 1642 } 1643 1644 err := bc.engine.VerifyHeader(bc, block.Header(), true) 1645 if err != nil { 1646 return nil, nil, 0, err 1647 } 1648 1649 // Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss) 1650 senderCacher.recoverFromBlocks(types.MakeSigner(bc.chainConfig, block.Number()), []*types.Block{block}) 1651 1652 if BadHashes[block.Hash()] { 1653 return nil, nil, 0, ErrBlacklistedHash 1654 } 1655 1656 if err = bc.Validator().ValidateBody(block); err != nil { 1657 return nil, nil, 0, err 1658 } 1659 1660 parent := bc.GetBlock(parentHash, block.NumberU64()-1) 1661 if parent == nil { 1662 return nil, nil, 0, ErrParentIsNil 1663 } 1664 stateDb, err := state.New(parent.Root(), bc.stateCache) 1665 if err != nil { 1666 return nil, nil, 0, err 1667 } 1668 1669 // Verify the witness list using the parent's state 1670 if err = bc.engine.VerifyWitnesses(block.Header(), stateDb, parent.Header()); err != nil { 1671 return nil, nil, 0, err 1672 } 1673 1674 // Process block using the parent state as reference point. 1675 receipts, logs, usedGas, err := bc.processor.Process(block, stateDb, bc.vmConfig) 1676 if err != nil { 1677 return receipts, logs, usedGas, err 1678 } 1679 1680 // Validate the state using the default validator 1681 if err = bc.Validator().ValidateState(block, parent, stateDb, receipts, usedGas); err != nil { 1682 return receipts, logs, usedGas, err 1683 1684 } 1685 return receipts, logs, usedGas, nil 1686 } 1687 1688 func (bc *BlockChain) WriteBlock(block *types.Block) error { 1689 bc.chainmu.Lock() 1690 defer bc.chainmu.Unlock() 1691 1692 bstart := time.Now() 1693 parent := bc.GetBlock(block.ParentHash(), block.NumberU64()-1) 1694 if parent == nil { 1695 return ErrParentIsNil 1696 } 1697 stateDb, err := bc.StateAt(parent.Root()) 1698 if err != nil { 1699 return err 1700 } 1701 1702 // Process block using the parent state as reference point. 1703 receipts, logs, _, err := bc.processor.Process(block, stateDb, bc.vmConfig) 1704 if err != nil { 1705 bc.reportBlock(block, receipts, err) 1706 return err 1707 } 1708 1709 proctime := time.Since(bstart) 1710 status, err := bc.WriteBlockWithState(block, receipts, stateDb) 1711 if err != nil { 1712 return err 1713 } 1714 1715 var events []interface{} 1716 var lastCanon *types.Block 1717 1718 switch status { 1719 case CanonStatTy: 1720 log.Debug("Inserted new block", "number", block.Number(), "hash", block.Hash(), 1721 "txs", len(block.Transactions()), "gas", block.GasUsed(), "elapsed", common.PrettyDuration(time.Since(bstart))) 1722 1723 blockInsertTimer.UpdateSince(bstart) 1724 events = append(events, ChainEvent{block, block.Hash(), logs}) 1725 lastCanon = block 1726 bc.gcproc += proctime 1727 1728 // Only count canonical blocks for GC processing time 1729 case SideStatTy: 1730 log.Debug("Inserted forked block", "number", block.Number(), "hash", block.Hash(), "diff", block.Difficulty(), "elapsed", 1731 common.PrettyDuration(time.Since(bstart)), "txs", len(block.Transactions()), "gas", block.GasUsed()) 1732 1733 blockInsertTimer.UpdateSince(bstart) 1734 events = append(events, ChainSideEvent{block}) 1735 } 1736 if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() { 1737 events = append(events, ChainHeadEvent{lastCanon}) 1738 } 1739 1740 bc.PostChainEvents(events, logs) 1741 return nil 1742 }