Skip to content

Commit

Permalink
Set correct block-hash in receipts after finishing the block (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbertJordan authored Nov 28, 2024
1 parent bc48edd commit d5ae861
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion gossip/c_block_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
49 changes: 47 additions & 2 deletions tests/block_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d5ae861

Please sign in to comment.