Skip to content

Commit 63687f0

Browse files
wraith7karalabe
authored andcommitted
core: check transaction/receipt count match when reconstructing blocks (#16272)
1 parent d43ffdb commit 63687f0

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

‎core/blockchain.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,13 @@ func (bc *BlockChain) Rollback(chain []common.Hash) {
723723
}
724724

725725
// SetReceiptsData computes all the non-consensus fields of the receipts
726-
func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts types.Receipts) {
726+
func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts types.Receipts) error {
727727
signer := types.MakeSigner(config, block.Number())
728728

729729
transactions, logIndex := block.Transactions(), uint(0)
730+
if len(transactions) != len(receipts) {
731+
return errors.New("transaction and receipt count mismatch")
732+
}
730733

731734
for j := 0; j < len(receipts); j++ {
732735
// The transaction hash can be retrieved from the transaction itself
@@ -754,6 +757,7 @@ func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts ty
754757
logIndex++
755758
}
756759
}
760+
return nil
757761
}
758762

759763
// InsertReceiptChain attempts to complete an already existing header chain with
@@ -794,7 +798,9 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
794798
continue
795799
}
796800
// Compute all the non-consensus fields of the receipts
797-
SetReceiptsData(bc.chainConfig, block, receipts)
801+
if err := SetReceiptsData(bc.chainConfig, block, receipts); err != nil {
802+
return i, fmt.Errorf("failed to set receipts data: %v", err)
803+
}
798804
// Write all the data out into the database
799805
if err := WriteBody(batch, block.Hash(), block.NumberU64(), block.Body()); err != nil {
800806
return i, fmt.Errorf("failed to write block body: %v", err)

‎light/odr_util.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num
144144
genesis := core.GetCanonicalHash(odr.Database(), 0)
145145
config, _ := core.GetChainConfig(odr.Database(), genesis)
146146

147-
core.SetReceiptsData(config, block, receipts)
147+
if err := core.SetReceiptsData(config, block, receipts); err != nil {
148+
return nil, err
149+
}
148150
core.WriteBlockReceipts(odr.Database(), hash, number, receipts)
149151
}
150152
return receipts, nil

0 commit comments

Comments
 (0)