Skip to content

Commit

Permalink
feat(core/types): Body hooks for RLP encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 21, 2025
1 parent f133755 commit 2be5a7b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
2 changes: 1 addition & 1 deletion accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2179,7 +2179,7 @@ func golangBindings(t *testing.T, overload bool) {
if out, err := replacer.CombinedOutput(); err != nil {
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)
}
replacer = exec.Command(gocmd, "mod", "edit", "-x", "-require", "github.com/ava-labs/[email protected]", "-replace", "github.com/ava-labs/libevm=github.com/ava-labs/[email protected]20250113230013-d10c4a3d1ff2")
replacer = exec.Command(gocmd, "mod", "edit", "-x", "-require", "github.com/ava-labs/[email protected]", "-replace", "github.com/ava-labs/libevm=github.com/ava-labs/[email protected]20250121171435-edebe134329f")
replacer.Dir = pkg
if out, err := replacer.CombinedOutput(); err != nil {
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)
Expand Down
77 changes: 77 additions & 0 deletions core/types/body_ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// (c) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package types

import (
"io"

ethtypes "github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/rlp"
)

// BodyExtra is a struct that contains extra fields used by Avalanche
// in the body.
// This type uses BodySerializable to encode and decode the extra fields
// along with the upstream type for compatibility with existing network blocks.
type BodyExtra struct {
version uint32
extData *[]byte

// Fields removed from geth:
// - withdrawals Withdrawals
}

func (b *BodyExtra) EncodeRLP(eth *ethtypes.Body, writer io.Writer) error {
out := new(bodySerializable)

out.updateFromEth(eth)
out.updateFromExtras(b)

return rlp.Encode(writer, out)
}

func (b *BodyExtra) DecodeRLP(eth *ethtypes.Body, stream *rlp.Stream) error {
in := new(bodySerializable)
if err := stream.Decode(in); err != nil {
return err
}

in.updateToEth(eth)
in.updateToExtras(b)

return nil
}

// bodySerializable defines the body in the Ethereum blockchain,
// as it is to be serialized into RLP.
type bodySerializable struct {
Transactions []*Transaction
Uncles []*Header
Version uint32
ExtData *[]byte `rlp:"nil"`
}

// updateFromEth updates the [*bodySerializable] from the [*ethtypes.Body].
func (b *bodySerializable) updateFromEth(eth *ethtypes.Body) {
b.Transactions = eth.Transactions
b.Uncles = eth.Uncles
}

// updateToEth updates the [*ethtypes.Body] from the [*bodySerializable].
func (b *bodySerializable) updateToEth(eth *ethtypes.Body) {
eth.Transactions = b.Transactions
eth.Uncles = b.Uncles
}

// updateFromExtras updates the [*bodySerializable] from the [*BodyExtra].
func (b *bodySerializable) updateFromExtras(extras *BodyExtra) {
b.Version = extras.version
b.ExtData = extras.extData
}

// updateToExtras updates the [*BodyExtra] from the [*bodySerializable].
func (b *bodySerializable) updateToExtras(extras *BodyExtra) {
extras.version = b.Version
extras.extData = b.ExtData
}
5 changes: 4 additions & 1 deletion core/types/libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
type isMultiCoin bool

var (
extras = ethtypes.RegisterExtras[HeaderExtra, *HeaderExtra, isMultiCoin]()
extras = ethtypes.RegisterExtras[
HeaderExtra, *HeaderExtra,
BodyExtra, *BodyExtra,
isMultiCoin]()
IsMultiCoinPayloads = extras.StateAccount
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/ava-labs/libevm => github.com/ava-labs/libevm v0.0.0-20250113230013-d10c4a3d1ff2
replace github.com/ava-labs/libevm => github.com/ava-labs/libevm v0.0.0-20250121171435-edebe134329f
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/ava-labs/avalanchego v1.12.1-0.20250107220127-32f58b4fa9c8 h1:qN3MOBHB//Ynhgt5Vys3iVe42Sr0EWSeN18VL3ecXzE=
github.com/ava-labs/avalanchego v1.12.1-0.20250107220127-32f58b4fa9c8/go.mod h1:2B7+E5neLvkOr2zursGhebjU26d4AfB7RazPxBs8hHg=
github.com/ava-labs/libevm v0.0.0-20250113230013-d10c4a3d1ff2 h1:eLyCq3xjpS6CDcZaVdAQaUim9qThl3buQjOlmcER8oQ=
github.com/ava-labs/libevm v0.0.0-20250113230013-d10c4a3d1ff2/go.mod h1:M8TCw2g1D5GBB7hu7g1F4aot5bRHGSxnBawNVmHE9Z0=
github.com/ava-labs/libevm v0.0.0-20250121171435-edebe134329f h1:r83FXbNY2MeQ1SaKmmVTWkDTAtZOs4mX8nme+nzpZU0=
github.com/ava-labs/libevm v0.0.0-20250121171435-edebe134329f/go.mod h1:M8TCw2g1D5GBB7hu7g1F4aot5bRHGSxnBawNVmHE9Z0=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand Down
2 changes: 1 addition & 1 deletion scripts/tests.e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ git checkout -B "test-${AVALANCHE_VERSION}" "${AVALANCHE_VERSION}"

echo "updating coreth dependency to point to ${CORETH_PATH}"
go mod edit -replace "github.com/ava-labs/coreth=${CORETH_PATH}"
go mod edit -replace "github.com/ava-labs/libevm=github.com/ava-labs/[email protected]20250113230013-d10c4a3d1ff2"
go mod edit -replace "github.com/ava-labs/libevm=github.com/ava-labs/[email protected]20250121171435-edebe134329f"
go mod tidy

echo "building avalanchego"
Expand Down

0 comments on commit 2be5a7b

Please sign in to comment.