Skip to content

Commit a59fcc3

Browse files
committed
core: import future blocks one-by-one, enfore chain ancestry
1 parent f15828e commit a59fcc3

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎core/blockchain.go‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,11 @@ func (self *BlockChain) procFutureBlocks() {
601601
}
602602
if len(blocks) > 0 {
603603
types.BlockBy(types.Number).Sort(blocks)
604-
self.InsertChain(blocks)
604+
605+
// Insert one by one as chain insertion needs contiguous ancestry between blocks
606+
for i := range blocks {
607+
self.InsertChain(blocks[i : i+1])
608+
}
605609
}
606610
}
607611

@@ -675,6 +679,18 @@ func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts ty
675679
// transaction and receipt data.
676680
// XXX should this be moved to the test?
677681
func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) {
682+
// Do a sanity check that the provided chain is actually ordered and linked
683+
for i := 1; i < len(blockChain); i++ {
684+
if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() {
685+
// Chain broke ancestry, log a messge (programming error) and skip insertion
686+
failure := fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])", i-1, blockChain[i-1].NumberU64(),
687+
blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4])
688+
689+
glog.V(logger.Error).Info(failure.Error())
690+
return 0, failure
691+
}
692+
}
693+
// Pre-checks passed, start the block body and receipt imports
678694
self.wg.Add(1)
679695
defer self.wg.Done()
680696

@@ -843,6 +859,18 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status WriteStatus, err
843859
// InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
844860
// it will return the index number of the failing block as well an error describing what went wrong (for possible errors see core/errors.go).
845861
func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
862+
// Do a sanity check that the provided chain is actually ordered and linked
863+
for i := 1; i < len(chain); i++ {
864+
if chain[i].NumberU64() != chain[i-1].NumberU64()+1 || chain[i].ParentHash() != chain[i-1].Hash() {
865+
// Chain broke ancestry, log a messge (programming error) and skip insertion
866+
failure := fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])",
867+
i-1, chain[i-1].NumberU64(), chain[i-1].Hash().Bytes()[:4], i, chain[i].NumberU64(), chain[i].Hash().Bytes()[:4], chain[i].ParentHash().Bytes()[:4])
868+
869+
glog.V(logger.Error).Info(failure.Error())
870+
return 0, failure
871+
}
872+
}
873+
// Pre-checks passed, start the full block imports
846874
self.wg.Add(1)
847875
defer self.wg.Done()
848876

@@ -916,10 +944,8 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
916944
}
917945

918946
self.reportBlock(block, nil, err)
919-
920947
return i, err
921948
}
922-
923949
// Create a new statedb using the parent block and report an
924950
// error if it fails.
925951
switch {

‎core/headerchain.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,17 @@ type WhCallback func(*types.Header) error
224224
// of the header retrieval mechanisms already need to verfy nonces, as well as
225225
// because nonces can be verified sparsely, not needing to check each.
226226
func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, checkFreq int, writeHeader WhCallback) (int, error) {
227+
// Do a sanity check that the provided chain is actually ordered and linked
228+
for i := 1; i < len(chain); i++ {
229+
if chain[i].Number.Uint64() != chain[i-1].Number.Uint64()+1 || chain[i].ParentHash != chain[i-1].Hash() {
230+
// Chain broke ancestry, log a messge (programming error) and skip insertion
231+
failure := fmt.Errorf("non contiguous insert: item %d is #%d [%x…], item %d is #%d [%x…] (parent [%x…])",
232+
i-1, chain[i-1].Number.Uint64(), chain[i-1].Hash().Bytes()[:4], i, chain[i].Number.Uint64(), chain[i].Hash().Bytes()[:4], chain[i].ParentHash.Bytes()[:4])
233+
234+
glog.V(logger.Error).Info(failure.Error())
235+
return 0, failure
236+
}
237+
}
227238
// Collect some import statistics to report on
228239
stats := struct{ processed, ignored int }{}
229240
start := time.Now()

0 commit comments

Comments
 (0)