Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit d65a0d2

Browse files
committed
feat(core/types): Body hooks for RLP encoding
1 parent 58f1171 commit d65a0d2

File tree

9 files changed

+107
-17
lines changed

9 files changed

+107
-17
lines changed

accounts/abi/bind/bind_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,7 @@ func golangBindings(t *testing.T, overload bool) {
21792179
if out, err := replacer.CombinedOutput(); err != nil {
21802180
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)
21812181
}
2182-
replacer = exec.Command(gocmd, "mod", "edit", "-x", "-require", "github.com/ava-labs/libevm@v0.0.0", "-replace", "github.com/ava-labs/libevm=github.com/ava-labs/libevm@v0.0.0-20250121162040-b9e5186290df")
2182+
replacer = exec.Command(gocmd, "mod", "edit", "-x", "-require", "github.com/ava-labs/libevm@v0.0.0", "-replace", "github.com/ava-labs/libevm=github.com/ava-labs/libevm@v0.0.0-20250122094011-7f6afca5658f")
21832183
replacer.Dir = pkg
21842184
if out, err := replacer.CombinedOutput(); err != nil {
21852185
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)

core/rawdb/accessors_chain.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block {
522522
if body == nil {
523523
return nil
524524
}
525-
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithExtData(body.Version, body.ExtData)
525+
bodyExtra := types.GetBodyExtra(body)
526+
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles).WithExtData(bodyExtra.Version, bodyExtra.ExtData)
526527
}
527528

528529
// WriteBlock serializes a block into the database, header and body separately.

core/types/block.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ import (
3737
"github.com/ava-labs/libevm/rlp"
3838
)
3939

40-
// Body is a simple (mutable, non-safe) data container for storing and moving
41-
// a block's data contents (transactions and uncles) together.
42-
type Body struct {
43-
Transactions []*Transaction
44-
Uncles []*Header
45-
Version uint32
46-
ExtData *[]byte `rlp:"nil"`
47-
}
48-
4940
// Block represents an Ethereum block.
5041
//
5142
// Note the Block type tries to be 'immutable', and contains certain caches that rely
@@ -152,7 +143,15 @@ func (b *Block) EncodeRLP(w io.Writer) error {
152143
// Body returns the non-header content of the block.
153144
// Note the returned data is not an independent copy.
154145
func (b *Block) Body() *Body {
155-
return &Body{b.transactions, b.uncles, b.version, b.extdata}
146+
body := &Body{
147+
Transactions: b.transactions,
148+
Uncles: b.uncles,
149+
}
150+
extra := &BodyExtra{
151+
Version: b.version,
152+
ExtData: b.extdata,
153+
}
154+
return WithBodyExtra(body, extra)
156155
}
157156

158157
// Accessors for body data. These do not return a copy because the content

core/types/body_ext.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// (c) 2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package types
5+
6+
import (
7+
"io"
8+
9+
ethtypes "github.com/ava-labs/libevm/core/types"
10+
"github.com/ava-labs/libevm/rlp"
11+
)
12+
13+
// BodyExtra is a struct that contains extra fields used by Avalanche
14+
// in the body.
15+
// This type uses BodySerializable to encode and decode the extra fields
16+
// along with the upstream type for compatibility with existing network blocks.
17+
type BodyExtra struct {
18+
Version uint32
19+
ExtData *[]byte
20+
21+
// Fields removed from geth:
22+
// - withdrawals Withdrawals
23+
}
24+
25+
func (b *BodyExtra) EncodeRLP(eth *ethtypes.Body, writer io.Writer) error {
26+
out := new(bodySerializable)
27+
28+
out.updateFromEth(eth)
29+
out.updateFromExtras(b)
30+
31+
return rlp.Encode(writer, out)
32+
}
33+
34+
func (b *BodyExtra) DecodeRLP(eth *ethtypes.Body, stream *rlp.Stream) error {
35+
in := new(bodySerializable)
36+
if err := stream.Decode(in); err != nil {
37+
return err
38+
}
39+
40+
in.updateToEth(eth)
41+
in.updateToExtras(b)
42+
43+
return nil
44+
}
45+
46+
// bodySerializable defines the body in the Ethereum blockchain,
47+
// as it is to be serialized into RLP.
48+
type bodySerializable struct {
49+
Transactions []*Transaction
50+
Uncles []*Header
51+
Version uint32
52+
ExtData *[]byte `rlp:"nil"`
53+
}
54+
55+
// updateFromEth updates the [*bodySerializable] from the [*ethtypes.Body].
56+
func (b *bodySerializable) updateFromEth(eth *ethtypes.Body) {
57+
b.Transactions = eth.Transactions
58+
b.Uncles = eth.Uncles
59+
}
60+
61+
// updateToEth updates the [*ethtypes.Body] from the [*bodySerializable].
62+
func (b *bodySerializable) updateToEth(eth *ethtypes.Body) {
63+
eth.Transactions = b.Transactions
64+
eth.Uncles = b.Uncles
65+
}
66+
67+
// updateFromExtras updates the [*bodySerializable] from the [*BodyExtra].
68+
func (b *bodySerializable) updateFromExtras(extras *BodyExtra) {
69+
b.Version = extras.Version
70+
b.ExtData = extras.ExtData
71+
}
72+
73+
// updateToExtras updates the [*BodyExtra] from the [*bodySerializable].
74+
func (b *bodySerializable) updateToExtras(extras *BodyExtra) {
75+
extras.Version = b.Version
76+
extras.ExtData = b.ExtData
77+
}

core/types/imports.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type (
1414
AccessTuple = ethtypes.AccessTuple
1515
AccessListTx = ethtypes.AccessListTx
1616
Bloom = ethtypes.Bloom
17+
Body = ethtypes.Body
1718
Receipt = ethtypes.Receipt
1819
Receipts = ethtypes.Receipts
1920
ReceiptForStorage = ethtypes.ReceiptForStorage

core/types/libevm.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
type isMultiCoin bool
1111

1212
var (
13-
extras = ethtypes.RegisterExtras[HeaderExtra, *HeaderExtra, isMultiCoin]()
13+
extras = ethtypes.RegisterExtras[
14+
HeaderExtra, *HeaderExtra,
15+
BodyExtra, *BodyExtra,
16+
isMultiCoin]()
1417
IsMultiCoinPayloads = extras.StateAccount
1518
)
1619

@@ -26,3 +29,12 @@ func WithHeaderExtras(h *Header, extra *HeaderExtra) *Header {
2629
extras.Header.Set(h, extra)
2730
return h
2831
}
32+
33+
func GetBodyExtra(b *Body) *BodyExtra {
34+
return extras.Body.Get(b)
35+
}
36+
37+
func WithBodyExtra(b *Body, extra *BodyExtra) *Body {
38+
extras.Body.Set(b, extra)
39+
return b
40+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,4 @@ require (
136136
rsc.io/tmplfunc v0.0.3 // indirect
137137
)
138138

139-
replace github.com/ava-labs/libevm => github.com/ava-labs/libevm v0.0.0-20250121162040-b9e5186290df
139+
replace github.com/ava-labs/libevm => github.com/ava-labs/libevm v0.0.0-20250122094011-7f6afca5658f

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax
5858
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
5959
github.com/ava-labs/avalanchego v1.12.1-0.20250107220127-32f58b4fa9c8 h1:qN3MOBHB//Ynhgt5Vys3iVe42Sr0EWSeN18VL3ecXzE=
6060
github.com/ava-labs/avalanchego v1.12.1-0.20250107220127-32f58b4fa9c8/go.mod h1:2B7+E5neLvkOr2zursGhebjU26d4AfB7RazPxBs8hHg=
61-
github.com/ava-labs/libevm v0.0.0-20250121162040-b9e5186290df h1:qCro69yW9HuKksYBitRy68Ana8w+621MXEW+0+PySio=
62-
github.com/ava-labs/libevm v0.0.0-20250121162040-b9e5186290df/go.mod h1:M8TCw2g1D5GBB7hu7g1F4aot5bRHGSxnBawNVmHE9Z0=
61+
github.com/ava-labs/libevm v0.0.0-20250122094011-7f6afca5658f h1:AZV16b9/W4SwXZHkuwjzNpsCSLyMvZMu9qt/KR9C2uU=
62+
github.com/ava-labs/libevm v0.0.0-20250122094011-7f6afca5658f/go.mod h1:M8TCw2g1D5GBB7hu7g1F4aot5bRHGSxnBawNVmHE9Z0=
6363
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
6464
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
6565
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=

scripts/tests.e2e.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ git checkout -B "test-${AVALANCHE_VERSION}" "${AVALANCHE_VERSION}"
4545

4646
echo "updating coreth dependency to point to ${CORETH_PATH}"
4747
go mod edit -replace "github.com/ava-labs/coreth=${CORETH_PATH}"
48-
go mod edit -replace "github.com/ava-labs/libevm=github.com/ava-labs/libevm@v0.0.0-20250121162040-b9e5186290df"
48+
go mod edit -replace "github.com/ava-labs/libevm=github.com/ava-labs/libevm@v0.0.0-20250122094011-7f6afca5658f"
4949
go mod tidy
5050

5151
echo "building avalanchego"

0 commit comments

Comments
 (0)