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

Commit 8838ae0

Browse files
committed
feat(core/types): Body hooks for RLP encoding
1 parent 06f2672 commit 8838ae0

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

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/libevm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var (
1313
extras = ethtypes.RegisterExtras[
1414
HeaderExtra, *HeaderExtra,
1515
BlockExtra, *BlockExtra,
16+
BodyExtra, *BodyExtra,
1617
isMultiCoin]()
1718
IsMultiCoinPayloads = extras.StateAccount
1819
)

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-20250116120922-5227c55beea5
139+
replace github.com/ava-labs/libevm => github.com/ava-labs/libevm v0.0.0-20250120102234-7ec5d041c250

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-20250116120922-5227c55beea5 h1:ULu6EoaYUrHuE1qr84jIPyylBzFtsQFx6HK4nys8FrM=
62-
github.com/ava-labs/libevm v0.0.0-20250116120922-5227c55beea5/go.mod h1:M8TCw2g1D5GBB7hu7g1F4aot5bRHGSxnBawNVmHE9Z0=
61+
github.com/ava-labs/libevm v0.0.0-20250120102234-7ec5d041c250 h1:qOWsbdH7Nmv2xS9H2qSfDUVhIzEwMxyzGxSoXZjmYm4=
62+
github.com/ava-labs/libevm v0.0.0-20250120102234-7ec5d041c250/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=

0 commit comments

Comments
 (0)