diff --git a/consensus/dummy/consensus.go b/consensus/dummy/consensus.go index ee5cb29959..e141b7db6f 100644 --- a/consensus/dummy/consensus.go +++ b/consensus/dummy/consensus.go @@ -439,7 +439,7 @@ func (self *DummyEngine) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) // Header seems complete, assemble into a block and return - return types.NewBlock( + return types.NewBlockWithExtData( header, txs, uncles, receipts, trie.NewStackTrie(nil), extraData, chain.Config().IsApricotPhase1(header.Time), ), nil diff --git a/core/genesis.go b/core/genesis.go index 7550c47360..7296bb6cf6 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -307,7 +307,7 @@ func (g *Genesis) toBlock(db ethdb.Database, triedb *trie.Database) *types.Block panic(fmt.Sprintf("unable to commit genesis block: %v", err)) } } - return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil), nil, false) + return types.NewBlock(head, nil, nil, nil, trie.NewStackTrie(nil)) } // Commit writes the block and state of a genesis specification to the database. diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 00bfc07311..07c12f5ce7 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -518,7 +518,7 @@ func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block { if body == nil { return nil } - return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles, body.Version, body.ExtData) + return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithExtData(body.Version, body.ExtData) } // WriteBlock serializes a block into the database, header and body separately. diff --git a/core/rawdb/accessors_indexes_test.go b/core/rawdb/accessors_indexes_test.go index 853585d159..408b1869e7 100644 --- a/core/rawdb/accessors_indexes_test.go +++ b/core/rawdb/accessors_indexes_test.go @@ -99,7 +99,7 @@ func TestLookupStorage(t *testing.T) { tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) txs := []*types.Transaction{tx1, tx2, tx3} - block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil, newHasher(), nil, true) + block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil, newHasher()) // Check that no transactions entries are in a pristine database for i, tx := range txs { diff --git a/core/rawdb/chain_iterator_test.go b/core/rawdb/chain_iterator_test.go index 282849c600..9a8415dd24 100644 --- a/core/rawdb/chain_iterator_test.go +++ b/core/rawdb/chain_iterator_test.go @@ -44,7 +44,7 @@ func TestChainIterator(t *testing.T) { var block *types.Block var txs []*types.Transaction to := common.BytesToAddress([]byte{0x11}) - block = types.NewBlock(&types.Header{Number: big.NewInt(int64(0))}, nil, nil, nil, newHasher(), nil, true) // Empty genesis block + block = types.NewBlock(&types.Header{Number: big.NewInt(int64(0))}, nil, nil, nil, newHasher()) // Empty genesis block WriteBlock(chainDb, block) WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64()) for i := uint64(1); i <= 10; i++ { @@ -70,7 +70,7 @@ func TestChainIterator(t *testing.T) { }) } txs = append(txs, tx) - block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher(), nil, true) + block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher()) WriteBlock(chainDb, block) WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64()) } @@ -121,7 +121,7 @@ func TestIndexTransactions(t *testing.T) { to := common.BytesToAddress([]byte{0x11}) // Write empty genesis block - block = types.NewBlock(&types.Header{Number: big.NewInt(int64(0))}, nil, nil, nil, newHasher(), nil, true) + block = types.NewBlock(&types.Header{Number: big.NewInt(int64(0))}, nil, nil, nil, newHasher()) WriteBlock(chainDb, block) WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64()) @@ -148,7 +148,7 @@ func TestIndexTransactions(t *testing.T) { }) } txs = append(txs, tx) - block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher(), nil, true) + block = types.NewBlock(&types.Header{Number: big.NewInt(int64(i))}, []*types.Transaction{tx}, nil, nil, newHasher()) WriteBlock(chainDb, block) WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64()) } diff --git a/core/state_manager_test.go b/core/state_manager_test.go index d3c77f6520..6afb779df8 100644 --- a/core/state_manager_test.go +++ b/core/state_manager_test.go @@ -45,7 +45,7 @@ func TestCappedMemoryTrieWriter(t *testing.T) { Root: common.BigToHash(bigI), Number: bigI, }, - nil, nil, nil, nil, nil, true, + nil, nil, nil, nil, ) assert.NoError(w.InsertTrie(block)) @@ -84,7 +84,7 @@ func TestNoPruningTrieWriter(t *testing.T) { Root: common.BigToHash(bigI), Number: bigI, }, - nil, nil, nil, nil, nil, true, + nil, nil, nil, nil, ) assert.NoError(w.InsertTrie(block)) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index a4a623056c..18820e7d7b 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -421,5 +421,5 @@ func GenerateBadBlock(parent *types.Block, engine consensus.Engine, txs types.Tr } header.Root = common.BytesToHash(hasher.Sum(nil)) // Assemble and return the final block for sealing - return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil), nil, true) + return types.NewBlock(header, txs, nil, receipts, trie.NewStackTrie(nil)) } diff --git a/core/txpool/txpool_test.go b/core/txpool/txpool_test.go index a2f39a2447..d6baefaae8 100644 --- a/core/txpool/txpool_test.go +++ b/core/txpool/txpool_test.go @@ -104,7 +104,7 @@ func (bc *testBlockChain) CurrentBlock() *types.Header { } func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { - return types.NewBlock(bc.CurrentBlock(), nil, nil, nil, trie.NewStackTrie(nil), nil, true) + return types.NewBlock(bc.CurrentBlock(), nil, nil, nil, trie.NewStackTrie(nil)) } func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) { diff --git a/core/types/block.go b/core/types/block.go index 4cc44bc84d..7a4be25159 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -190,8 +190,7 @@ type extblock struct { // are ignored and set to values derived from the given txs, uncles // and receipts. func NewBlock( - header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt, - hasher TrieHasher, extdata []byte, recalc bool, + header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt, hasher TrieHasher, ) *Block { b := &Block{header: CopyHeader(header)} @@ -221,7 +220,6 @@ func NewBlock( } } - b.setExtData(extdata, recalc) return b } @@ -270,38 +268,6 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error { return nil } -func (b *Block) setExtDataHelper(data *[]byte, recalc bool) { - if data == nil { - b.setExtData(nil, recalc) - return - } - b.setExtData(*data, recalc) -} - -func (b *Block) setExtData(data []byte, recalc bool) { - _data := make([]byte, len(data)) - b.extdata = &_data - copy(*b.extdata, data) - if recalc { - b.header.ExtDataHash = CalcExtDataHash(*b.extdata) - } -} - -func (b *Block) ExtData() []byte { - if b.extdata == nil { - return nil - } - return *b.extdata -} - -func (b *Block) SetVersion(ver uint32) { - b.version = ver -} - -func (b *Block) Version() uint32 { - return b.version -} - // EncodeRLP serializes b into the Ethereum RLP block format. func (b *Block) EncodeRLP(w io.Writer) error { return rlp.Encode(w, extblock{ @@ -353,13 +319,6 @@ func (b *Block) BaseFee() *big.Int { return new(big.Int).Set(b.header.BaseFee) } -func (b *Block) ExtDataGasUsed() *big.Int { - if b.header.ExtDataGasUsed == nil { - return nil - } - return new(big.Int).Set(b.header.ExtDataGasUsed) -} - func (b *Block) BlockGasCost() *big.Int { if b.header.BlockGasCost == nil { return nil @@ -391,13 +350,6 @@ func (c *writeCounter) Write(b []byte) (int, error) { return len(b), nil } -func CalcExtDataHash(extdata []byte) common.Hash { - if len(extdata) == 0 { - return EmptyExtDataHash - } - return rlpHash(extdata) -} - func CalcUncleHash(uncles []*Header) common.Hash { if len(uncles) == 0 { return EmptyUncleHash @@ -418,18 +370,16 @@ func (b *Block) WithSeal(header *Header) *Block { } // WithBody returns a new block with the given transaction and uncle contents. -func (b *Block) WithBody(transactions []*Transaction, uncles []*Header, version uint32, extdata *[]byte) *Block { +func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block { block := &Block{ header: CopyHeader(b.header), transactions: make([]*Transaction, len(transactions)), uncles: make([]*Header, len(uncles)), - version: version, } copy(block.transactions, transactions) for i := range uncles { block.uncles[i] = CopyHeader(uncles[i]) } - block.setExtDataHelper(extdata, false) return block } diff --git a/core/types/block_ext.go b/core/types/block_ext.go new file mode 100644 index 0000000000..5ebb7aeb31 --- /dev/null +++ b/core/types/block_ext.go @@ -0,0 +1,67 @@ +// (c) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package types + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" +) + +func (b *Block) WithExtData(version uint32, extdata *[]byte) *Block { + b.version = version + b.setExtDataHelper(extdata, false) + return b +} + +func (b *Block) setExtDataHelper(data *[]byte, recalc bool) { + if data == nil { + b.setExtData(nil, recalc) + return + } + b.setExtData(*data, recalc) +} + +func (b *Block) setExtData(data []byte, recalc bool) { + _data := make([]byte, len(data)) + b.extdata = &_data + copy(*b.extdata, data) + if recalc { + b.header.ExtDataHash = CalcExtDataHash(*b.extdata) + } +} + +func (b *Block) ExtData() []byte { + if b.extdata == nil { + return nil + } + return *b.extdata +} + +func (b *Block) Version() uint32 { + return b.version +} + +func (b *Block) ExtDataGasUsed() *big.Int { + if b.header.ExtDataGasUsed == nil { + return nil + } + return new(big.Int).Set(b.header.ExtDataGasUsed) +} + +func CalcExtDataHash(extdata []byte) common.Hash { + if len(extdata) == 0 { + return EmptyExtDataHash + } + return rlpHash(extdata) +} + +func NewBlockWithExtData( + header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt, + hasher TrieHasher, extdata []byte, recalc bool, +) *Block { + b := NewBlock(header, txs, uncles, receipts, hasher) + b.setExtData(extdata, recalc) + return b +} diff --git a/core/types/block_test.go b/core/types/block_test.go index ace763be0b..ed0a525f62 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -363,7 +363,7 @@ func makeBenchBlock() *Block { Extra: []byte("benchmark uncle"), } } - return NewBlock(header, txs, uncles, receipts, newHasher(), nil, true) + return NewBlock(header, txs, uncles, receipts, newHasher()) } func TestAP4BlockEncoding(t *testing.T) { diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index 7d19d71b84..131c5b2712 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -222,7 +222,7 @@ func TestBlockSubscription(t *testing.T) { api = NewFilterAPI(sys) genesis = &core.Genesis{ Config: params.TestChainConfig, - BaseFee: big.NewInt(params.ApricotPhase4MinBaseFee), + BaseFee: big.NewInt(1), } _, chain, _, _ = core.GenerateChainWithGenesis(genesis, dummy.NewFaker(), 10, 10, func(i int, b *core.BlockGen) {}) chainEvents = []core.ChainEvent{} diff --git a/eth/filters/filter_test.go b/eth/filters/filter_test.go index ccfcfab8b0..bae301f1bd 100644 --- a/eth/filters/filter_test.go +++ b/eth/filters/filter_test.go @@ -65,7 +65,7 @@ func BenchmarkFilters(b *testing.B) { gspec = &core.Genesis{ Config: params.TestChainConfig, Alloc: core.GenesisAlloc{addr1: {Balance: big.NewInt(1000000)}}, - BaseFee: big.NewInt(params.ApricotPhase3InitialBaseFee), + BaseFee: big.NewInt(1), } ) defer db.Close() @@ -106,6 +106,7 @@ func BenchmarkFilters(b *testing.B) { require.NoError(b, err) for i := 0; i < b.N; i++ { + filter.begin = 0 logs, _ := filter.Logs(context.Background()) if len(logs) != 4 { b.Fatal("expected 4 logs, got", len(logs)) @@ -128,7 +129,7 @@ func TestFilters(t *testing.T) { gspec = &core.Genesis{ Config: params.TestChainConfig, Alloc: core.GenesisAlloc{addr: {Balance: big.NewInt(1000000)}}, - BaseFee: big.NewInt(params.ApricotPhase3InitialBaseFee), + BaseFee: big.NewInt(1), } ) defer db.Close() diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index 0fe9b43ebd..d33dea6232 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -232,7 +232,8 @@ func TestTraceCall(t *testing.T) { accounts[0].addr: {Balance: big.NewInt(params.Ether)}, accounts[1].addr: {Balance: big.NewInt(params.Ether)}, accounts[2].addr: {Balance: big.NewInt(params.Ether)}, - }} + }, + } genBlocks := 10 signer := types.HomesteadSigner{} backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) { diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 47e326fb7b..50805a6aff 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -260,7 +260,7 @@ func (ec *client) getBlock(ctx context.Context, method string, args ...interface } txs[i] = tx.tx } - return types.NewBlockWithHeader(head).WithBody(txs, uncles, body.Version, (*[]byte)(body.BlockExtraData)), nil + return types.NewBlockWithHeader(head).WithBody(txs, uncles).WithExtData(body.Version, (*[]byte)(body.BlockExtraData)), nil } // HeaderByHash returns the block header with the given hash. diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index 1b7bef24fc..6fca3858fb 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -709,7 +709,7 @@ func TestRPCMarshalBlock(t *testing.T) { } txs = append(txs, tx) } - block := types.NewBlock(&types.Header{Number: big.NewInt(100)}, txs, nil, nil, newHasher(), nil, false) + block := types.NewBlock(&types.Header{Number: big.NewInt(100)}, txs, nil, nil, newHasher()) var testSuite = []struct { inclTx bool diff --git a/plugin/evm/syncervm_test.go b/plugin/evm/syncervm_test.go index 3042658d00..5649332792 100644 --- a/plugin/evm/syncervm_test.go +++ b/plugin/evm/syncervm_test.go @@ -556,7 +556,7 @@ func patchBlock(blk *types.Block, root common.Hash, db ethdb.Database) *types.Bl header := blk.Header() header.Root = root receipts := rawdb.ReadRawReceipts(db, blk.Hash(), blk.NumberU64()) - newBlk := types.NewBlock( + newBlk := types.NewBlockWithExtData( header, blk.Transactions(), blk.Uncles(), receipts, trie.NewStackTrie(nil), blk.ExtData(), true, ) rawdb.WriteBlock(db, newBlk) diff --git a/plugin/evm/vm_test.go b/plugin/evm/vm_test.go index c7a793c5a7..3e7a29c6f5 100644 --- a/plugin/evm/vm_test.go +++ b/plugin/evm/vm_test.go @@ -1115,7 +1115,7 @@ func testConflictingImportTxs(t *testing.T, genesis string) { t.Fatal(err) } - conflictingAtomicTxBlock := types.NewBlock( + conflictingAtomicTxBlock := types.NewBlockWithExtData( types.CopyHeader(validEthBlock.Header()), nil, nil, @@ -1151,7 +1151,7 @@ func testConflictingImportTxs(t *testing.T, genesis string) { header := types.CopyHeader(validEthBlock.Header()) header.ExtDataGasUsed.Mul(common.Big2, header.ExtDataGasUsed) - internalConflictBlock := types.NewBlock( + internalConflictBlock := types.NewBlockWithExtData( header, nil, nil, @@ -2603,7 +2603,7 @@ func TestUncleBlock(t *testing.T) { uncleBlockHeader := types.CopyHeader(blkDEthBlock.Header()) uncleBlockHeader.UncleHash = types.CalcUncleHash(uncles) - uncleEthBlock := types.NewBlock( + uncleEthBlock := types.NewBlockWithExtData( uncleBlockHeader, blkDEthBlock.Transactions(), uncles, @@ -2660,7 +2660,7 @@ func TestEmptyBlock(t *testing.T) { // Create empty block from blkA ethBlock := blk.(*chain.BlockWrapper).Block.(*Block).ethBlock - emptyEthBlock := types.NewBlock( + emptyEthBlock := types.NewBlockWithExtData( types.CopyHeader(ethBlock.Header()), nil, nil, @@ -2941,7 +2941,7 @@ func TestFutureBlock(t *testing.T) { // Set the modified time to exceed the allowed future time modifiedTime := modifiedHeader.Time + uint64(maxFutureBlockTime.Seconds()+1) modifiedHeader.Time = modifiedTime - modifiedBlock := types.NewBlock( + modifiedBlock := types.NewBlockWithExtData( modifiedHeader, nil, nil, @@ -4047,7 +4047,7 @@ func TestExtraStateChangeAtomicGasLimitExceeded(t *testing.T) { } // Construct the new block with the extra data in the new format (slice of atomic transactions). - ethBlk2 := types.NewBlock( + ethBlk2 := types.NewBlockWithExtData( types.CopyHeader(validEthBlock.Header()), nil, nil,