core/types: only compute receipts bloom once

This commit is contained in:
Marius van der Wijden 2024-11-26 02:40:04 +01:00
parent 3ed070bc36
commit 3c5ea20184
1 changed files with 156 additions and 145 deletions

View File

@ -22,6 +22,7 @@ import (
"math"
"math/big"
"reflect"
"sync"
"testing"
"github.com/ethereum/go-ethereum/common"
@ -154,9 +155,17 @@ var (
blockNumber = big.NewInt(1)
blockTime = uint64(2)
blockHash = common.BytesToHash([]byte{0x03, 0x14})
)
var receiptsOnce sync.Once
var testReceipts Receipts
func getTestReceipts() Receipts {
// Compute the blooms only once
receiptsOnce.Do(func() {
// Create the corresponding receipts
receipts = Receipts{
r := Receipts{
&Receipt{
Status: ReceiptStatusFailed,
CumulativeGasUsed: 1,
@ -294,13 +303,12 @@ var (
TransactionIndex: 6,
},
}
)
func init() {
// Correctly compute the bloom filters
for _, receipt := range receipts {
for _, receipt := range r {
receipt.Bloom = CreateBloom(Receipts{receipt})
}
testReceipts = r
})
return testReceipts
}
func TestDecodeEmptyTypedReceipt(t *testing.T) {
@ -317,6 +325,7 @@ func TestDeriveFields(t *testing.T) {
// Re-derive receipts.
basefee := big.NewInt(1000)
blobGasPrice := big.NewInt(920)
receipts := getTestReceipts()
derivedReceipts := clearComputedFieldsOnReceipts(receipts)
err := Receipts(derivedReceipts).DeriveFields(params.TestChainConfig, blockHash, blockNumber.Uint64(), blockTime, basefee, blobGasPrice, txs)
if err != nil {
@ -342,6 +351,7 @@ func TestDeriveFields(t *testing.T) {
// Test that we can marshal/unmarshal receipts to/from json without errors.
// This also confirms that our test receipts contain all the required fields.
func TestReceiptJSON(t *testing.T) {
receipts := getTestReceipts()
for i := range receipts {
b, err := receipts[i].MarshalJSON()
if err != nil {
@ -358,6 +368,7 @@ func TestReceiptJSON(t *testing.T) {
// Test we can still parse receipt without EffectiveGasPrice for backwards compatibility, even
// though it is required per the spec.
func TestEffectiveGasPriceNotRequired(t *testing.T) {
receipts := getTestReceipts()
r := *receipts[0]
r.EffectiveGasPrice = nil
b, err := r.MarshalJSON()