Skip to content

Commit

Permalink
Block Body hook
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 22, 2025
1 parent e27ce12 commit 11c780f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,10 @@ func (b *Block) encodeRLP(w io.Writer) error {
})
}

// Body returns the non-header content of the block.
// EthBody returns the non-header content of an Ethereum block.
// Note the returned data is not an independent copy.
func (b *Block) Body() *Body {
// Use [Block.Body] instead if your block has any registered extra.
func (b *Block) EthBody() *Body {
return &Body{
Transactions: b.transactions,
Uncles: b.uncles,
Expand Down
11 changes: 11 additions & 0 deletions core/types/block.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func (*NOOPBodyHooks) DecodeRLP(b *Body, s *rlp.Stream) error {
type BlockHooks interface {
EncodeRLP(*Block, io.Writer) error
DecodeRLP(*Block, *rlp.Stream) error
Body(*Block) *Body
}

// hooks returns the Block's registered BlockHooks, if any, otherwise a
Expand Down Expand Up @@ -243,6 +244,10 @@ func (*NOOPBlockHooks) DecodeRLP(b *Block, s *rlp.Stream) error {
return b.decodeRLP(s)
}

func (*NOOPBlockHooks) Body(b *Block) *Body {
return b.EthBody()
}

func (b *Block) SetHeader(header *Header) {
b.header = header
}
Expand All @@ -254,3 +259,9 @@ func (b *Block) SetUncles(uncles []*Header) {
func (b *Block) SetTransactions(transactions Transactions) {
b.transactions = transactions
}

// Body returns the non-header content of the block.
// Note the returned data is not an independent copy.
func (b *Block) Body() *Body {
return b.hooks().Body(b)
}
4 changes: 4 additions & 0 deletions core/types/block.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func (bh *stubBlockHooks) DecodeRLP(b *Block, s *rlp.Stream) error {
return bh.errDecode
}

func (bh *stubBlockHooks) Body(b *Block) *Body {
return b.EthBody()
}

func TestHeaderHooks(t *testing.T) {
TestOnlyClearRegisteredExtras()
defer TestOnlyClearRegisteredExtras()
Expand Down

0 comments on commit 11c780f

Please sign in to comment.