Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payload): Move to holiman lib #1821

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion mod/chain-spec/pkg/chain/chain_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type Spec[
SlotT ~uint64,
CometBFTConfigT any,
] interface {

// Gwei value constants.

// MinDepositAmount returns the minimum amount of Gwei required for a
Expand Down
28 changes: 13 additions & 15 deletions mod/cli/pkg/commands/genesis/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,19 @@ func executableDataToExecutionPayloadHeader(
}

executionPayloadHeader = &types.ExecutionPayloadHeader{
ParentHash: data.ParentHash,
FeeRecipient: data.FeeRecipient,
StateRoot: common.Bytes32(data.StateRoot),
ReceiptsRoot: common.Bytes32(data.ReceiptsRoot),
LogsBloom: [256]byte(data.LogsBloom),
Random: common.Bytes32(data.Random),
Number: math.U64(data.Number),
GasLimit: math.U64(data.GasLimit),
GasUsed: math.U64(data.GasUsed),
Timestamp: math.U64(data.Timestamp),
ExtraData: data.ExtraData,
BaseFeePerGas: math.MustNewU256LFromBigInt(
data.BaseFeePerGas,
),
BlockHash: data.BlockHash,
ParentHash: data.ParentHash,
FeeRecipient: data.FeeRecipient,
StateRoot: common.Bytes32(data.StateRoot),
ReceiptsRoot: common.Bytes32(data.ReceiptsRoot),
LogsBloom: [256]byte(data.LogsBloom),
Random: common.Bytes32(data.Random),
Number: math.U64(data.Number),
GasLimit: math.U64(data.GasLimit),
GasUsed: math.U64(data.GasUsed),
Timestamp: math.U64(data.Timestamp),
ExtraData: data.ExtraData,
BaseFeePerGas: math.NewU256FromBigInt(data.BaseFeePerGas),
BlockHash: data.BlockHash,
// TODO: Decouple from broken bArtio.
TransactionsRoot: engineprimitives.
BartioTransactions(
Expand Down
2 changes: 1 addition & 1 deletion mod/consensus-types/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240705193247-d464364483df
github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240726210727-594bfb4e7157
github.com/ferranbt/fastssz v0.1.4-0.20240629094022-eac385e6ee79
github.com/holiman/uint256 v1.3.0
github.com/karalabe/ssz v0.2.1-0.20240724074312-3d1ff7a6f7c4
github.com/stretchr/testify v1.9.0

Expand Down Expand Up @@ -55,7 +56,6 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.3.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion mod/consensus-types/pkg/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func DefaultGenesisExecutionPayloadHeaderDeneb() (
Timestamp: 0,
ExtraData: make([]byte, constants.ExtraDataLength),
//nolint:mnd // default value.
BaseFeePerGas: math.MustNewU256LFromBigInt(big.NewInt(3906250)),
BaseFeePerGas: math.NewU256FromBigInt(big.NewInt(3906250)),
BlockHash: gethprimitives.HexToHash(
"0xcfff92cd918a186029a847b59aca4f83d3941df5946b06bca8de0861fc5d0850",
),
Expand Down
3 changes: 2 additions & 1 deletion mod/consensus-types/pkg/state/deneb/deneb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/state/deneb"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
karalabessz "github.com/karalabe/ssz"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -95,7 +96,7 @@ func generateValidBeaconState() *deneb.BeaconState {
GasUsed: 25000000,
Timestamp: 1625097600,
ExtraData: []byte{0x26, 0x27, 0x28},
BaseFeePerGas: [32]byte{0x29, 0x2a, 0x2b},
BaseFeePerGas: math.NewU256(3906250),
BlockHash: [32]byte{0x2c, 0x2d, 0x2e},
TransactionsRoot: [32]byte{0x2f, 0x30, 0x31},
WithdrawalsRoot: [32]byte{0x32, 0x33, 0x34},
Expand Down
1 change: 1 addition & 0 deletions mod/consensus-types/pkg/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func generateValidBeaconBlock() *types.BeaconBlock {
{Index: 0, Amount: 100},
{Index: 1, Amount: 200},
},
BaseFeePerGas: math.NewU256(0),
},
Eth1Data: &types.Eth1Data{},
Deposits: []*types.Deposit{
Expand Down
13 changes: 8 additions & 5 deletions mod/consensus-types/pkg/types/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/berachain/beacon-kit/mod/primitives/pkg/eip4844"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/berachain/beacon-kit/mod/primitives/pkg/version"
"github.com/stretchr/testify/require"
)

func generateBeaconBlockBody() types.BeaconBlockBody {
return types.BeaconBlockBody{
RandaoReveal: [96]byte{1, 2, 3},
Eth1Data: &types.Eth1Data{},
Graffiti: [32]byte{4, 5, 6},
Deposits: []*types.Deposit{},
ExecutionPayload: &types.ExecutionPayload{},
RandaoReveal: [96]byte{1, 2, 3},
Eth1Data: &types.Eth1Data{},
Graffiti: [32]byte{4, 5, 6},
Deposits: []*types.Deposit{},
ExecutionPayload: &types.ExecutionPayload{
BaseFeePerGas: math.NewU256(0),
},
BlobKzgCommitments: []eip4844.KZGCommitment{},
}
}
Expand Down
20 changes: 12 additions & 8 deletions mod/consensus-types/pkg/types/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type ExecutionPayload struct {
// ExtraData is the extra data of the block.
ExtraData bytes.Bytes `json:"extraData"`
// BaseFeePerGas is the base fee per gas.
BaseFeePerGas math.Wei `json:"baseFeePerGas"`
BaseFeePerGas *math.U256 `json:"baseFeePerGas"`
// BlockHash is the hash of the block.
BlockHash gethprimitives.ExecutionHash `json:"blockHash"`
// Transactions is the list of transactions in the block.
Expand Down Expand Up @@ -109,7 +109,7 @@ func (p *ExecutionPayload) DefineSSZ(codec *ssz.Codec) {
ssz.DefineUint64(codec, &p.GasUsed)
ssz.DefineUint64(codec, &p.Timestamp)
ssz.DefineDynamicBytesOffset(codec, (*[]byte)(&p.ExtraData), 32)
ssz.DefineStaticBytes(codec, &p.BaseFeePerGas)
ssz.DefineUint256(codec, &p.BaseFeePerGas)
ssz.DefineStaticBytes(codec, &p.BlockHash)
ssz.DefineSliceOfDynamicBytesOffset(
codec,
Expand Down Expand Up @@ -210,7 +210,11 @@ func (p *ExecutionPayload) HashTreeRootWith(hh fastssz.HashWalker) error {
}

// Field (11) 'BaseFeePerGas'
hh.PutBytes(p.BaseFeePerGas[:])
bz, err := p.BaseFeePerGas.MarshalSSZ()
if err != nil {
return err
}
hh.PutBytes(bz)

// Field (12) 'BlockHash'
hh.PutBytes(p.BlockHash[:])
Expand Down Expand Up @@ -244,7 +248,7 @@ func (p *ExecutionPayload) HashTreeRootWith(hh fastssz.HashWalker) error {
return fastssz.ErrIncorrectListSize
}
for _, elem := range p.Withdrawals {
if err := elem.HashTreeRootWith(hh); err != nil {
if err = elem.HashTreeRootWith(hh); err != nil {
return err
}
}
Expand Down Expand Up @@ -284,7 +288,7 @@ func (p *ExecutionPayload) MarshalJSON() ([]byte, error) {
GasUsed math.U64 `json:"gasUsed"`
Timestamp math.U64 `json:"timestamp"`
ExtraData bytes.Bytes `json:"extraData"`
BaseFeePerGas math.U256L `json:"baseFeePerGas"`
BaseFeePerGas *math.U256 `json:"baseFeePerGas"`
BlockHash gethprimitives.ExecutionHash `json:"blockHash"`
Transactions []bytes.Bytes `json:"transactions"`
Withdrawals []*engineprimitives.Withdrawal `json:"withdrawals"`
Expand Down Expand Up @@ -331,7 +335,7 @@ func (p *ExecutionPayload) UnmarshalJSON(input []byte) error {
GasUsed *math.U64 `json:"gasUsed"`
Timestamp *math.U64 `json:"timestamp"`
ExtraData *bytes.Bytes `json:"extraData"`
BaseFeePerGas *math.U256L `json:"baseFeePerGas"`
BaseFeePerGas *math.U256 `json:"baseFeePerGas"`
BlockHash *gethprimitives.ExecutionHash `json:"blockHash"`
Transactions []bytes.Bytes `json:"transactions"`
Withdrawals []*engineprimitives.Withdrawal `json:"withdrawals"`
Expand Down Expand Up @@ -413,7 +417,7 @@ func (p *ExecutionPayload) UnmarshalJSON(input []byte) error {
"missing required field 'baseFeePerGas' for ExecutionPayload",
)
}
p.BaseFeePerGas = *dec.BaseFeePerGas
p.BaseFeePerGas = dec.BaseFeePerGas
if dec.BlockHash == nil {
return errors.New(
"missing required field 'blockHash' for ExecutionPayload",
Expand Down Expand Up @@ -519,7 +523,7 @@ func (p *ExecutionPayload) GetExtraData() []byte {
}

// GetBaseFeePerGas returns the base fee per gas of the ExecutionPayload.
func (p *ExecutionPayload) GetBaseFeePerGas() math.Wei {
func (p *ExecutionPayload) GetBaseFeePerGas() *math.U256 {
return p.BaseFeePerGas
}

Expand Down
19 changes: 12 additions & 7 deletions mod/consensus-types/pkg/types/payload_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/berachain/beacon-kit/mod/primitives/pkg/version"
fastssz "github.com/ferranbt/fastssz"
"github.com/holiman/uint256"
"github.com/karalabe/ssz"
)

Expand Down Expand Up @@ -67,7 +68,7 @@ type ExecutionPayloadHeader struct {
// ExtraData is the extra data of the block.
ExtraData bytes.Bytes `json:"extraData"`
// BaseFeePerGas is the base fee per gas.
BaseFeePerGas math.Wei `json:"baseFeePerGas"`
BaseFeePerGas *uint256.Int `json:"baseFeePerGas"`
// BlockHash is the hash of the block.
BlockHash gethprimitives.ExecutionHash `json:"blockHash"`
// TransactionsRoot is the root of the transaction trie.
Expand Down Expand Up @@ -137,7 +138,7 @@ func (h *ExecutionPayloadHeader) DefineSSZ(codec *ssz.Codec) {
ssz.DefineUint64(codec, &h.Timestamp)
//nolint:mnd // todo fix.
ssz.DefineDynamicBytesOffset(codec, (*[]byte)(&h.ExtraData), 32)
ssz.DefineStaticBytes(codec, &h.BaseFeePerGas)
ssz.DefineUint256(codec, &h.BaseFeePerGas)
ssz.DefineStaticBytes(codec, &h.BlockHash)
ssz.DefineStaticBytes(codec, &h.TransactionsRoot)
ssz.DefineStaticBytes(codec, &h.WithdrawalsRoot)
Expand Down Expand Up @@ -238,7 +239,11 @@ func (h *ExecutionPayloadHeader) HashTreeRootWith(hh fastssz.HashWalker) error {
}

// Field (11) 'BaseFeePerGas'
hh.PutBytes(h.BaseFeePerGas[:])
bz, err := h.BaseFeePerGas.MarshalSSZ()
if err != nil {
return err
}
hh.PutBytes(bz)

// Field (12) 'BlockHash'
hh.PutBytes(h.BlockHash[:])
Expand Down Expand Up @@ -282,7 +287,7 @@ func (h ExecutionPayloadHeader) MarshalJSON() ([]byte, error) {
GasUsed math.U64 `json:"gasUsed"`
Timestamp math.U64 `json:"timestamp"`
ExtraData bytes.Bytes `json:"extraData"`
BaseFeePerGas math.U256L `json:"baseFeePerGas"`
BaseFeePerGas *math.U256 `json:"baseFeePerGas"`
BlockHash common.ExecutionHash `json:"blockHash"`
TransactionsRoot bytes.B32 `json:"transactionsRoot"`
WithdrawalsRoot bytes.B32 `json:"withdrawalsRoot"`
Expand Down Expand Up @@ -326,7 +331,7 @@ func (h *ExecutionPayloadHeader) UnmarshalJSON(input []byte) error {
GasUsed *math.U64 `json:"gasUsed"`
Timestamp *math.U64 `json:"timestamp"`
ExtraData *bytes.Bytes `json:"extraData"`
BaseFeePerGas *math.U256L `json:"baseFeePerGas"`
BaseFeePerGas *math.U256 `json:"baseFeePerGas"`
BlockHash *gethprimitives.ExecutionHash `json:"blockHash"`
TransactionsRoot *bytes.B32 `json:"transactionsRoot"`
WithdrawalsRoot *bytes.B32 `json:"withdrawalsRoot"`
Expand Down Expand Up @@ -415,7 +420,7 @@ func (h *ExecutionPayloadHeader) UnmarshalJSON(input []byte) error {
"missing required field 'baseFeePerGas' for ExecutionPayloadHeader",
)
}
h.BaseFeePerGas = *dec.BaseFeePerGas
h.BaseFeePerGas = dec.BaseFeePerGas
if dec.BlockHash == nil {
return errors.New(
"missing required field 'blockHash' for ExecutionPayloadHeader",
Expand Down Expand Up @@ -517,7 +522,7 @@ func (h *ExecutionPayloadHeader) GetExtraData() []byte {

// GetBaseFeePerGas returns the base fee per gas of the
// ExecutionPayloadHeader.
func (h *ExecutionPayloadHeader) GetBaseFeePerGas() math.Wei {
func (h *ExecutionPayloadHeader) GetBaseFeePerGas() *math.U256 {
return h.BaseFeePerGas
}

Expand Down
11 changes: 5 additions & 6 deletions mod/consensus-types/pkg/types/payload_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func generateExecutionPayloadHeader() *types.ExecutionPayloadHeader {
GasUsed: math.U64(0),
Timestamp: math.U64(0),
ExtraData: nil,
BaseFeePerGas: math.Wei{},
BaseFeePerGas: &math.U256{},
BlockHash: gethprimitives.ExecutionHash{},
TransactionsRoot: bytes.B32{},
WithdrawalsRoot: bytes.B32{},
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestExecutionPayloadHeader_Getters(t *testing.T) {
require.Equal(t, math.U64(0), header.GetGasUsed())
require.Equal(t, math.U64(0), header.GetTimestamp())
require.Equal(t, []byte(nil), header.GetExtraData())
require.Equal(t, math.Wei{}, header.GetBaseFeePerGas())
require.Equal(t, math.NewU256(0), header.GetBaseFeePerGas())
require.Equal(t, gethprimitives.ExecutionHash{}, header.GetBlockHash())
require.Equal(t, bytes.B32{}, header.GetTransactionsRoot())
require.Equal(t, bytes.B32{}, header.GetWithdrawalsRoot())
Expand Down Expand Up @@ -116,11 +116,10 @@ func TestExecutionPayloadHeader_Serialization(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, data)

var unmarshalled types.ExecutionPayloadHeader
err = unmarshalled.Empty(version.Deneb).UnmarshalSSZ(data)
var unmarshalled = new(types.ExecutionPayloadHeader).Empty(version.Deneb)
err = unmarshalled.UnmarshalSSZ(data)
require.NoError(t, err)

require.Equal(t, original, &unmarshalled)
require.Equal(t, original, unmarshalled)
}

func TestExecutionPayloadHeader_MarshalSSZTo(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions mod/consensus-types/pkg/types/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func generateExecutionPayload() *types.ExecutionPayload {
GasUsed: math.U64(0),
Timestamp: math.U64(0),
ExtraData: []byte{0x01},
BaseFeePerGas: math.Wei{},
BaseFeePerGas: &math.U256{},
BlockHash: gethprimitives.ExecutionHash{},
Transactions: transactions,
Withdrawals: withdrawals,
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestExecutionPayload_Getters(t *testing.T) {
require.Equal(t, math.U64(0), payload.GetGasUsed())
require.Equal(t, math.U64(0), payload.GetTimestamp())
require.Equal(t, []byte{0x01}, payload.GetExtraData())
require.Equal(t, math.Wei{}, payload.GetBaseFeePerGas())
require.Equal(t, &math.U256{}, payload.GetBaseFeePerGas())
require.Equal(t, gethprimitives.ExecutionHash{}, payload.GetBlockHash())
require.Equal(t, transactions, payload.GetTransactions())
require.Equal(t, withdrawals, payload.GetWithdrawals())
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestExecutionPayload_ToHeader(t *testing.T) {
GasUsed: math.U64(0),
Timestamp: math.U64(0),
ExtraData: []byte{},
BaseFeePerGas: math.Wei{},
BaseFeePerGas: &math.U256{},
BlockHash: gethprimitives.ExecutionHash{},
Transactions: [][]byte{[]byte{0x01}},
Withdrawals: []*engineprimitives.Withdrawal{},
Expand Down
2 changes: 1 addition & 1 deletion mod/engine-primitives/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240726210727-594bfb4e7157
github.com/ethereum/go-ethereum v1.14.6
github.com/ferranbt/fastssz v0.1.4-0.20240629094022-eac385e6ee79
github.com/holiman/uint256 v1.3.0
github.com/karalabe/ssz v0.2.1-0.20240724074312-3d1ff7a6f7c4
github.com/stretchr/testify v1.9.0
)
Expand Down Expand Up @@ -44,7 +45,6 @@ require (
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.3.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading