core/types: only compute receipts bloom once

pull/29158/head
Marius van der Wijden 2 months ago
parent 3ed070bc36
commit 3c5ea20184
  1. 295
      core/types/receipt_test.go

@ -22,6 +22,7 @@ import (
"math" "math"
"math/big" "math/big"
"reflect" "reflect"
"sync"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -154,153 +155,160 @@ var (
blockNumber = big.NewInt(1) blockNumber = big.NewInt(1)
blockTime = uint64(2) blockTime = uint64(2)
blockHash = common.BytesToHash([]byte{0x03, 0x14}) blockHash = common.BytesToHash([]byte{0x03, 0x14})
)
// Create the corresponding receipts var receiptsOnce sync.Once
receipts = Receipts{ var testReceipts Receipts
&Receipt{
Status: ReceiptStatusFailed, func getTestReceipts() Receipts {
CumulativeGasUsed: 1,
Logs: []*Log{ // Compute the blooms only once
{ receiptsOnce.Do(func() {
Address: common.BytesToAddress([]byte{0x11}), // Create the corresponding receipts
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, r := Receipts{
// derived fields: &Receipt{
BlockNumber: blockNumber.Uint64(), Status: ReceiptStatusFailed,
TxHash: txs[0].Hash(), CumulativeGasUsed: 1,
TxIndex: 0, Logs: []*Log{
BlockHash: blockHash, {
Index: 0, Address: common.BytesToAddress([]byte{0x11}),
}, Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
{ // derived fields:
Address: common.BytesToAddress([]byte{0x01, 0x11}), BlockNumber: blockNumber.Uint64(),
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, TxHash: txs[0].Hash(),
// derived fields: TxIndex: 0,
BlockNumber: blockNumber.Uint64(), BlockHash: blockHash,
TxHash: txs[0].Hash(), Index: 0,
TxIndex: 0, },
BlockHash: blockHash, {
Index: 1, Address: common.BytesToAddress([]byte{0x01, 0x11}),
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
// derived fields:
BlockNumber: blockNumber.Uint64(),
TxHash: txs[0].Hash(),
TxIndex: 0,
BlockHash: blockHash,
Index: 1,
},
}, },
// derived fields:
TxHash: txs[0].Hash(),
ContractAddress: common.HexToAddress("0x5a443704dd4b594b382c22a083e2bd3090a6fef3"),
GasUsed: 1,
EffectiveGasPrice: big.NewInt(11),
BlockHash: blockHash,
BlockNumber: blockNumber,
TransactionIndex: 0,
}, },
// derived fields: &Receipt{
TxHash: txs[0].Hash(), PostState: common.Hash{2}.Bytes(),
ContractAddress: common.HexToAddress("0x5a443704dd4b594b382c22a083e2bd3090a6fef3"), CumulativeGasUsed: 3,
GasUsed: 1, Logs: []*Log{
EffectiveGasPrice: big.NewInt(11), {
BlockHash: blockHash, Address: common.BytesToAddress([]byte{0x22}),
BlockNumber: blockNumber, Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
TransactionIndex: 0, // derived fields:
}, BlockNumber: blockNumber.Uint64(),
&Receipt{ TxHash: txs[1].Hash(),
PostState: common.Hash{2}.Bytes(), TxIndex: 1,
CumulativeGasUsed: 3, BlockHash: blockHash,
Logs: []*Log{ Index: 2,
{ },
Address: common.BytesToAddress([]byte{0x22}), {
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, Address: common.BytesToAddress([]byte{0x02, 0x22}),
// derived fields: Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
BlockNumber: blockNumber.Uint64(), // derived fields:
TxHash: txs[1].Hash(), BlockNumber: blockNumber.Uint64(),
TxIndex: 1, TxHash: txs[1].Hash(),
BlockHash: blockHash, TxIndex: 1,
Index: 2, BlockHash: blockHash,
}, Index: 3,
{ },
Address: common.BytesToAddress([]byte{0x02, 0x22}),
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
// derived fields:
BlockNumber: blockNumber.Uint64(),
TxHash: txs[1].Hash(),
TxIndex: 1,
BlockHash: blockHash,
Index: 3,
}, },
// derived fields:
TxHash: txs[1].Hash(),
GasUsed: 2,
EffectiveGasPrice: big.NewInt(22),
BlockHash: blockHash,
BlockNumber: blockNumber,
TransactionIndex: 1,
}, },
// derived fields: &Receipt{
TxHash: txs[1].Hash(), Type: AccessListTxType,
GasUsed: 2, PostState: common.Hash{3}.Bytes(),
EffectiveGasPrice: big.NewInt(22), CumulativeGasUsed: 6,
BlockHash: blockHash, Logs: []*Log{},
BlockNumber: blockNumber, // derived fields:
TransactionIndex: 1, TxHash: txs[2].Hash(),
}, GasUsed: 3,
&Receipt{ EffectiveGasPrice: big.NewInt(33),
Type: AccessListTxType, BlockHash: blockHash,
PostState: common.Hash{3}.Bytes(), BlockNumber: blockNumber,
CumulativeGasUsed: 6, TransactionIndex: 2,
Logs: []*Log{}, },
// derived fields: &Receipt{
TxHash: txs[2].Hash(), Type: DynamicFeeTxType,
GasUsed: 3, PostState: common.Hash{4}.Bytes(),
EffectiveGasPrice: big.NewInt(33), CumulativeGasUsed: 10,
BlockHash: blockHash, Logs: []*Log{},
BlockNumber: blockNumber, // derived fields:
TransactionIndex: 2, TxHash: txs[3].Hash(),
}, GasUsed: 4,
&Receipt{ EffectiveGasPrice: big.NewInt(1044),
Type: DynamicFeeTxType, BlockHash: blockHash,
PostState: common.Hash{4}.Bytes(), BlockNumber: blockNumber,
CumulativeGasUsed: 10, TransactionIndex: 3,
Logs: []*Log{}, },
// derived fields: &Receipt{
TxHash: txs[3].Hash(), Type: DynamicFeeTxType,
GasUsed: 4, PostState: common.Hash{5}.Bytes(),
EffectiveGasPrice: big.NewInt(1044), CumulativeGasUsed: 15,
BlockHash: blockHash, Logs: []*Log{},
BlockNumber: blockNumber, // derived fields:
TransactionIndex: 3, TxHash: txs[4].Hash(),
}, GasUsed: 5,
&Receipt{ EffectiveGasPrice: big.NewInt(1055),
Type: DynamicFeeTxType, BlockHash: blockHash,
PostState: common.Hash{5}.Bytes(), BlockNumber: blockNumber,
CumulativeGasUsed: 15, TransactionIndex: 4,
Logs: []*Log{}, },
// derived fields: &Receipt{
TxHash: txs[4].Hash(), Type: BlobTxType,
GasUsed: 5, PostState: common.Hash{6}.Bytes(),
EffectiveGasPrice: big.NewInt(1055), CumulativeGasUsed: 21,
BlockHash: blockHash, Logs: []*Log{},
BlockNumber: blockNumber, // derived fields:
TransactionIndex: 4, TxHash: txs[5].Hash(),
}, GasUsed: 6,
&Receipt{ EffectiveGasPrice: big.NewInt(1066),
Type: BlobTxType, BlobGasUsed: params.BlobTxBlobGasPerBlob,
PostState: common.Hash{6}.Bytes(), BlobGasPrice: big.NewInt(920),
CumulativeGasUsed: 21, BlockHash: blockHash,
Logs: []*Log{}, BlockNumber: blockNumber,
// derived fields: TransactionIndex: 5,
TxHash: txs[5].Hash(), },
GasUsed: 6, &Receipt{
EffectiveGasPrice: big.NewInt(1066), Type: BlobTxType,
BlobGasUsed: params.BlobTxBlobGasPerBlob, PostState: common.Hash{7}.Bytes(),
BlobGasPrice: big.NewInt(920), CumulativeGasUsed: 28,
BlockHash: blockHash, Logs: []*Log{},
BlockNumber: blockNumber, // derived fields:
TransactionIndex: 5, TxHash: txs[6].Hash(),
}, GasUsed: 7,
&Receipt{ EffectiveGasPrice: big.NewInt(1077),
Type: BlobTxType, BlobGasUsed: 3 * params.BlobTxBlobGasPerBlob,
PostState: common.Hash{7}.Bytes(), BlobGasPrice: big.NewInt(920),
CumulativeGasUsed: 28, BlockHash: blockHash,
Logs: []*Log{}, BlockNumber: blockNumber,
// derived fields: TransactionIndex: 6,
TxHash: txs[6].Hash(), },
GasUsed: 7, }
EffectiveGasPrice: big.NewInt(1077), for _, receipt := range r {
BlobGasUsed: 3 * params.BlobTxBlobGasPerBlob, receipt.Bloom = CreateBloom(Receipts{receipt})
BlobGasPrice: big.NewInt(920), }
BlockHash: blockHash, testReceipts = r
BlockNumber: blockNumber, })
TransactionIndex: 6, return testReceipts
},
}
)
func init() {
// Correctly compute the bloom filters
for _, receipt := range receipts {
receipt.Bloom = CreateBloom(Receipts{receipt})
}
} }
func TestDecodeEmptyTypedReceipt(t *testing.T) { func TestDecodeEmptyTypedReceipt(t *testing.T) {
@ -317,6 +325,7 @@ func TestDeriveFields(t *testing.T) {
// Re-derive receipts. // Re-derive receipts.
basefee := big.NewInt(1000) basefee := big.NewInt(1000)
blobGasPrice := big.NewInt(920) blobGasPrice := big.NewInt(920)
receipts := getTestReceipts()
derivedReceipts := clearComputedFieldsOnReceipts(receipts) derivedReceipts := clearComputedFieldsOnReceipts(receipts)
err := Receipts(derivedReceipts).DeriveFields(params.TestChainConfig, blockHash, blockNumber.Uint64(), blockTime, basefee, blobGasPrice, txs) err := Receipts(derivedReceipts).DeriveFields(params.TestChainConfig, blockHash, blockNumber.Uint64(), blockTime, basefee, blobGasPrice, txs)
if err != nil { if err != nil {
@ -342,6 +351,7 @@ func TestDeriveFields(t *testing.T) {
// Test that we can marshal/unmarshal receipts to/from json without errors. // Test that we can marshal/unmarshal receipts to/from json without errors.
// This also confirms that our test receipts contain all the required fields. // This also confirms that our test receipts contain all the required fields.
func TestReceiptJSON(t *testing.T) { func TestReceiptJSON(t *testing.T) {
receipts := getTestReceipts()
for i := range receipts { for i := range receipts {
b, err := receipts[i].MarshalJSON() b, err := receipts[i].MarshalJSON()
if err != nil { if err != nil {
@ -358,6 +368,7 @@ func TestReceiptJSON(t *testing.T) {
// Test we can still parse receipt without EffectiveGasPrice for backwards compatibility, even // Test we can still parse receipt without EffectiveGasPrice for backwards compatibility, even
// though it is required per the spec. // though it is required per the spec.
func TestEffectiveGasPriceNotRequired(t *testing.T) { func TestEffectiveGasPriceNotRequired(t *testing.T) {
receipts := getTestReceipts()
r := *receipts[0] r := *receipts[0]
r.EffectiveGasPrice = nil r.EffectiveGasPrice = nil
b, err := r.MarshalJSON() b, err := r.MarshalJSON()

Loading…
Cancel
Save