Skip to content

Commit

Permalink
feat(core/types): add Block hook WithBody
Browse files Browse the repository at this point in the history
- Break WithBody method to accept a body (as done upstream in geth)
  • Loading branch information
qdm12 committed Feb 10, 2025
1 parent 11c780f commit 16cfcdf
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion beacon/engine/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func ExecutableDataToBlock(params ExecutableData, versionedHashes []common.Hash,
BlobGasUsed: params.BlobGasUsed,
ParentBeaconRoot: beaconRoot,
}
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */).WithWithdrawals(params.Withdrawals)
block := types.NewBlockWithHeader(header).WithBody(&types.Body{Transactions: txs}).WithWithdrawals(params.Withdrawals)
if block.Hash() != params.BlockHash {
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/evm/internal/t8ntool/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ func (i *bbInput) ToBlock() *types.Block {
if i.Header.Difficulty != nil {
header.Difficulty = i.Header.Difficulty
}
return types.NewBlockWithHeader(header).WithBody(i.Txs, i.Ommers).WithWithdrawals(i.Withdrawals)
body := &types.Body{Transactions: i.Txs, Uncles: i.Ommers}
return types.NewBlockWithHeader(header).WithBody(body).WithWithdrawals(i.Withdrawals)
}

// SealBlock seals the given block using the configured engine.
Expand Down
6 changes: 3 additions & 3 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,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).WithWithdrawals(body.Withdrawals)
return types.NewBlockWithHeader(header).WithBody(body).WithWithdrawals(body.Withdrawals)
}

// WriteBlock serializes a block into the database, header and body separately.
Expand Down Expand Up @@ -843,7 +843,7 @@ func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
}
for _, bad := range badBlocks {
if bad.Header.Hash() == hash {
return types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles).WithWithdrawals(bad.Body.Withdrawals)
return types.NewBlockWithHeader(bad.Header).WithBody(bad.Body).WithWithdrawals(bad.Body.Withdrawals)
}
}
return nil
Expand All @@ -862,7 +862,7 @@ func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
}
var blocks []*types.Block
for _, bad := range badBlocks {
blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles).WithWithdrawals(bad.Body.Withdrawals))
blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody(bad.Body).WithWithdrawals(bad.Body.Withdrawals))
}
return blocks
}
Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ func makeTestBlocks(nblock int, txsPerBlock int) []*types.Block {
Number: big.NewInt(int64(i)),
Extra: []byte("test block"),
}
blocks[i] = types.NewBlockWithHeader(header).WithBody(txs, nil)
blocks[i] = types.NewBlockWithHeader(header).WithBody(&types.Body{Transactions: txs})
blocks[i].Hash() // pre-cache the block hash
}
return blocks
Expand Down
13 changes: 7 additions & 6 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,17 +469,18 @@ func (b *Block) WithSeal(header *Header) *Block {
}

// WithBody returns a copy of the block with the given transaction and uncle contents.
func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
func (b *Block) WithBody(body *Body) *Block {
block := &Block{
header: b.header,
transactions: make([]*Transaction, len(transactions)),
uncles: make([]*Header, len(uncles)),
transactions: make([]*Transaction, len(body.Transactions)),
uncles: make([]*Header, len(body.Uncles)),
withdrawals: b.withdrawals,
}
copy(block.transactions, transactions)
for i := range uncles {
block.uncles[i] = CopyHeader(uncles[i])
copy(block.transactions, body.Transactions)
for i := range body.Uncles {
block.uncles[i] = CopyHeader(body.Uncles[i])
}
block = b.hooks().WithBody(b, body)
return block
}

Expand Down
5 changes: 5 additions & 0 deletions core/types/block.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ type BlockHooks interface {
EncodeRLP(*Block, io.Writer) error
DecodeRLP(*Block, *rlp.Stream) error
Body(*Block) *Body
WithBody(block *Block, body *Body) *Block
}

// hooks returns the Block's registered BlockHooks, if any, otherwise a
Expand Down Expand Up @@ -248,6 +249,10 @@ func (*NOOPBlockHooks) Body(b *Block) *Body {
return b.EthBody()
}

func (*NOOPBlockHooks) WithBody(block *Block, body *Body) *Block {
return block
}

func (b *Block) SetHeader(header *Header) {
b.header = header
}
Expand Down
4 changes: 4 additions & 0 deletions core/types/block.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func (bh *stubBlockHooks) Body(b *Block) *Body {
return b.EthBody()
}

func (bh *stubBlockHooks) WithBody(block *Block, body *Body) *Block {
return block
}

func TestHeaderHooks(t *testing.T) {
TestOnlyClearRegisteredExtras()
defer TestOnlyClearRegisteredExtras()
Expand Down
4 changes: 2 additions & 2 deletions eth/catalyst/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ func setBlockhash(data *engine.ExecutableData) *engine.ExecutableData {
Extra: data.ExtraData,
MixDigest: data.Random,
}
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
block := types.NewBlockWithHeader(header).WithBody(&types.Body{Transactions: txs})
data.BlockHash = block.Hash()
return data
}
Expand Down Expand Up @@ -935,7 +935,7 @@ func TestNewPayloadOnInvalidTerminalBlock(t *testing.T) {
Extra: data.ExtraData,
MixDigest: data.Random,
}
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
block := types.NewBlockWithHeader(header).WithBody(&types.Body{Transactions: txs})
data.BlockHash = block.Hash()
// Send the new payload
resp2, err := api.NewPayloadV1(data)
Expand Down
11 changes: 7 additions & 4 deletions eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"sync/atomic"
"time"

"github.com/ava-labs/libevm"
ethereum "github.com/ava-labs/libevm"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/rawdb"
"github.com/ava-labs/libevm/core/state/snapshot"
Expand Down Expand Up @@ -1500,7 +1500,8 @@ func (d *Downloader) importBlockResults(results []*fetchResult) error {
)
blocks := make([]*types.Block, len(results))
for i, result := range results {
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles).WithWithdrawals(result.Withdrawals)
body := &types.Body{Transactions: result.Transactions, Uncles: result.Uncles}
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(body).WithWithdrawals(result.Withdrawals)
}
// Downloaded blocks are always regarded as trusted after the
// transition. Because the downloaded chain is guided by the
Expand Down Expand Up @@ -1718,7 +1719,8 @@ func (d *Downloader) commitSnapSyncData(results []*fetchResult, stateSync *state
blocks := make([]*types.Block, len(results))
receipts := make([]types.Receipts, len(results))
for i, result := range results {
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles).WithWithdrawals(result.Withdrawals)
body := &types.Body{Transactions: result.Transactions, Uncles: result.Uncles}
blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(body).WithWithdrawals(result.Withdrawals)
receipts[i] = result.Receipts
}
if index, err := d.blockchain.InsertReceiptChain(blocks, receipts, d.ancientLimit); err != nil {
Expand All @@ -1729,7 +1731,8 @@ func (d *Downloader) commitSnapSyncData(results []*fetchResult, stateSync *state
}

func (d *Downloader) commitPivotBlock(result *fetchResult) error {
block := types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles).WithWithdrawals(result.Withdrawals)
body := &types.Body{Transactions: result.Transactions, Uncles: result.Uncles}
block := types.NewBlockWithHeader(result.Header).WithBody(body).WithWithdrawals(result.Withdrawals)
log.Debug("Committing snap sync pivot as new head", "number", block.Number(), "hash", block.Hash())

// Commit the pivot block as the new head, will require full sync from here on
Expand Down
3 changes: 2 additions & 1 deletion eth/fetcher/block_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,8 @@ func (f *BlockFetcher) loop() {
// Mark the body matched, reassemble if still unknown
matched = true
if f.getBlock(hash) == nil {
block := types.NewBlockWithHeader(announce.header).WithBody(task.transactions[i], task.uncles[i])
body := &types.Body{Transactions: task.transactions[i], Uncles: task.uncles[i]}
block := types.NewBlockWithHeader(announce.header).WithBody(body)
block.ReceivedAt = task.time
blocks = append(blocks, block)
} else {
Expand Down
2 changes: 1 addition & 1 deletion eth/handler_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func testBroadcastMalformedBlock(t *testing.T, protocol uint) {

// Try to broadcast all malformations and ensure they all get discarded
for _, header := range []*types.Header{malformedUncles, malformedTransactions, malformedEverything} {
block := types.NewBlockWithHeader(header).WithBody(block.Transactions(), block.Uncles())
block := types.NewBlockWithHeader(header).WithBody(block.Body())
if err := src.SendNewBlock(block, big.NewInt(131136)); err != nil {
t.Fatalf("failed to broadcast block: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"math/big"

"github.com/ava-labs/libevm"
ethereum "github.com/ava-labs/libevm"
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/common/hexutil"
"github.com/ava-labs/libevm/core/types"
Expand Down Expand Up @@ -191,7 +191,8 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface
}
txs[i] = tx.tx
}
return types.NewBlockWithHeader(head).WithBody(txs, uncles).WithWithdrawals(body.Withdrawals), nil
blockBody := &types.Body{Transactions: txs, Uncles: uncles}
return types.NewBlockWithHeader(head).WithBody(blockBody).WithWithdrawals(body.Withdrawals), nil
}

// HeaderByHash returns the block header with the given hash.
Expand Down
2 changes: 1 addition & 1 deletion internal/era/era.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (e *Era) GetBlockByNumber(num uint64) (*types.Block, error) {
if err := rlp.Decode(r, &body); err != nil {
return nil, err
}
return types.NewBlockWithHeader(&header).WithBody(body.Transactions, body.Uncles), nil
return types.NewBlockWithHeader(&header).WithBody(&body), nil
}

// Accumulator reads the accumulator entry in the Era1 file.
Expand Down
2 changes: 1 addition & 1 deletion internal/era/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (it *Iterator) Block() (*types.Block, error) {
if err := rlp.Decode(it.inner.Body, &body); err != nil {
return nil, err
}
return types.NewBlockWithHeader(&header).WithBody(body.Transactions, body.Uncles), nil
return types.NewBlockWithHeader(&header).WithBody(&body), nil
}

// Receipts returns the receipts for the iterator's current position.
Expand Down

0 comments on commit 16cfcdf

Please sign in to comment.