Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

align types/block.go with subnet-evm #463

Merged
merged 7 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion consensus/dummy/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/accessors_indexes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions core/rawdb/chain_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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())
}
Expand Down Expand Up @@ -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())

Expand All @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions core/state_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion core/txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
54 changes: 2 additions & 52 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}

Expand Down Expand Up @@ -221,7 +220,6 @@ func NewBlock(
}
}

b.setExtData(extdata, recalc)
return b
}

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

Expand Down
67 changes: 67 additions & 0 deletions core/types/block_ext.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion eth/filters/filter_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
5 changes: 3 additions & 2 deletions eth/filters/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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))
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion eth/tracers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugin/evm/syncervm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading
Loading