From d5ae86148ef94483abc7b02da35080595b940fdd Mon Sep 17 00:00:00 2001 From: Herbert Date: Thu, 28 Nov 2024 19:28:20 +0100 Subject: [PATCH] Set correct block-hash in receipts after finishing the block (#361) --- gossip/c_block_callbacks.go | 3 ++- tests/block_header_test.go | 49 +++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/gossip/c_block_callbacks.go b/gossip/c_block_callbacks.go index 7a375cd1..31e0fc11 100644 --- a/gossip/c_block_callbacks.go +++ b/gossip/c_block_callbacks.go @@ -292,8 +292,9 @@ func consensusCallbackBeginBlockFn( evmBlock.Hash = block.Hash() evmBlock.Duration = blockDuration - // Update block-hash references in logs. + // Update block-hash references in receipts and logs. for i := range allReceipts { + allReceipts[i].BlockHash = block.Hash() for j := range allReceipts[i].Logs { allReceipts[i].Logs[j].BlockHash = block.Hash() } diff --git a/tests/block_header_test.go b/tests/block_header_test.go index 7618e0b1..a59fa3f4 100644 --- a/tests/block_header_test.go +++ b/tests/block_header_test.go @@ -106,6 +106,14 @@ func testBlockHeadersOnNetwork(t *testing.T, net *IntegrationTestNet) { testHeaders_TransactionRootMatchesBlockTxsHash(t, headers, client) }) + t.Run("TransactionReceiptReferencesCorrectContext", func(t *testing.T) { + testHeaders_TransactionReceiptReferencesCorrectContext(t, headers, client) + }) + + t.Run("ReceiptBlockHashMatchesBlockHash", func(t *testing.T) { + testHeaders_ReceiptBlockHashMatchesBlockHash(t, headers, client) + }) + t.Run("ReceiptRootMatchesBlockReceipts", func(t *testing.T) { testHeaders_ReceiptRootMatchesBlockReceipts(t, headers, client) }) @@ -266,15 +274,50 @@ func testHeaders_BaseFeeEvolutionFollowsPricingRules(t *testing.T, headers []*ty func testHeaders_TransactionRootMatchesBlockTxsHash(t *testing.T, headers []*types.Header, client *ethclient.Client) { require := require.New(t) - for _, header := range headers { + for i, header := range headers { block, err := client.BlockByNumber(context.Background(), header.Number) - require.NoError(err, "failed to get block receipts") + require.NoError(err, "failed to get block %d", i) txsHash := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)) require.Equal(header.TxHash, txsHash, "transaction root hash mismatch") } } +func testHeaders_TransactionReceiptReferencesCorrectContext( + t *testing.T, headers []*types.Header, client *ethclient.Client, +) { + require := require.New(t) + + for i, header := range headers { + block, err := client.BlockByNumber(context.Background(), header.Number) + require.NoError(err, "failed to get block %d", i) + + for j, tx := range block.Transactions() { + receipt, err := client.TransactionReceipt(context.Background(), tx.Hash()) + require.NoError(err, "failed to get transaction receipt") + + require.Equal(tx.Hash(), receipt.TxHash, "transaction hash mismatch") + require.Equal(i, int(receipt.BlockNumber.Uint64()), "block number mismatch") + require.Equal(j, int(receipt.TransactionIndex), "transaction index mismatch") + require.Equal(header.Hash(), receipt.BlockHash, "block hash mismatch") + } + } +} + +func testHeaders_ReceiptBlockHashMatchesBlockHash(t *testing.T, headers []*types.Header, client *ethclient.Client) { + require := require.New(t) + + for _, header := range headers { + receipts, err := client.BlockReceipts(context.Background(), + rpc.BlockNumberOrHashWithHash(header.Hash(), false)) + require.NoError(err, "failed to get block receipts") + + for _, receipt := range receipts { + require.Equal(header.Hash(), receipt.BlockHash, "receipt block hash mismatch") + } + } +} + func testHeaders_ReceiptRootMatchesBlockReceipts(t *testing.T, headers []*types.Header, client *ethclient.Client) { require := require.New(t) @@ -536,6 +579,7 @@ func testHeaders_CanRetrieveLogEvents(t *testing.T, headers []*types.Header, cli require.NoError(err, "failed to get block receipts") for _, receipt := range receipts { + require.Equal(blockHash, receipt.BlockHash, "block hash mismatch") for _, log := range receipt.Logs { allLogs = append(allLogs, *log) @@ -621,6 +665,7 @@ func testHeaders_CounterStateIsVerifiable( receipts, err := client.BlockReceipts(context.Background(), rpc.BlockNumberOrHashWithHash(header.Hash(), false)) require.NoError(err, "failed to get block receipts") for _, receipt := range receipts { + require.Equal(header.Hash(), receipt.BlockHash, "block hash mismatch") for _, log := range receipt.Logs { event, err := counter.ParseCount(*log) if err != nil {