@@ -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?
677681func (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).
845861func (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 {
0 commit comments