Skip to content

Commit

Permalink
bet
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Aug 7, 2024
1 parent f5bd114 commit 49be5a8
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 38 deletions.
2 changes: 1 addition & 1 deletion mod/beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (s *Service[
var eth1Data Eth1DataT
// TODO: assemble real eth1data.
body.SetEth1Data(eth1Data.New(
common.Bytes32{},
common.Root{},
0,
common.ExecutionHash{},
))
Expand Down
3 changes: 1 addition & 2 deletions mod/consensus-types/pkg/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package types

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/berachain/beacon-kit/mod/primitives/pkg/version"
Expand Down Expand Up @@ -67,7 +66,7 @@ func (b *BeaconBlock) NewWithVersion(
Slot: slot,
ProposerIndex: proposerIndex,
ParentRoot: parentBlockRoot,
StateRoot: bytes.B32{},
StateRoot: common.Root{},
Body: &BeaconBlockBody{},
}
default:
Expand Down
12 changes: 6 additions & 6 deletions mod/consensus-types/pkg/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"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"
Expand All @@ -38,8 +38,8 @@ func generateValidBeaconBlock() *types.BeaconBlock {
return &types.BeaconBlock{
Slot: 10,
ProposerIndex: 5,
ParentRoot: bytes.B32{1, 2, 3, 4, 5},
StateRoot: bytes.B32{5, 4, 3, 2, 1},
ParentRoot: common.Root{1, 2, 3, 4, 5},
StateRoot: common.Root{5, 4, 3, 2, 1},
Body: &types.BeaconBlockBody{
ExecutionPayload: &types.ExecutionPayload{
Number: 10,
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestBeaconBlockForDeneb(t *testing.T) {
block := &types.BeaconBlock{
Slot: 10,
ProposerIndex: 5,
ParentRoot: bytes.B32{1, 2, 3, 4, 5},
ParentRoot: common.Root{1, 2, 3, 4, 5},
}
require.NotNil(t, block)
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestBeaconBlock_IsNil(t *testing.T) {
func TestNewWithVersion(t *testing.T) {
slot := math.Slot(10)
proposerIndex := math.ValidatorIndex(5)
parentBlockRoot := bytes.B32{1, 2, 3, 4, 5}
parentBlockRoot := common.Root{1, 2, 3, 4, 5}

block, err := (&types.BeaconBlock{}).NewWithVersion(
slot, proposerIndex, parentBlockRoot, version.Deneb,
Expand All @@ -181,7 +181,7 @@ func TestNewWithVersion(t *testing.T) {
func TestNewWithVersionInvalidForkVersion(t *testing.T) {
slot := math.Slot(10)
proposerIndex := math.ValidatorIndex(5)
parentBlockRoot := bytes.B32{1, 2, 3, 4, 5}
parentBlockRoot := common.Root{1, 2, 3, 4, 5}

_, err := (&types.BeaconBlock{}).NewWithVersion(
slot,
Expand Down
4 changes: 2 additions & 2 deletions mod/consensus-types/pkg/types/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ func (b *BeaconBlockBody) SetSlashingInfo(_ []*SlashingInfo) {
// GetTopLevelRoots returns the top-level roots of the BeaconBlockBody.
func (b *BeaconBlockBody) GetTopLevelRoots() []common.Root {
return []common.Root{
b.GetRandaoReveal().HashTreeRoot(),
common.Root(b.GetRandaoReveal().HashTreeRoot()),
b.Eth1Data.HashTreeRoot(),
b.GetGraffiti().HashTreeRoot(),
common.Root(b.GetGraffiti().HashTreeRoot()),
Deposits(b.GetDeposits()).HashTreeRoot(),
b.GetExecutionPayload().HashTreeRoot(),
// I think this is a bug.
Expand Down
8 changes: 4 additions & 4 deletions mod/consensus-types/pkg/types/payload_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ func (h ExecutionPayloadHeader) MarshalJSON() ([]byte, error) {
ExtraData bytes.Bytes `json:"extraData"`
BaseFeePerGas *math.U256 `json:"baseFeePerGas"`
BlockHash common.ExecutionHash `json:"blockHash"`
TransactionsRoot bytes.B32 `json:"transactionsRoot"`
WithdrawalsRoot bytes.B32 `json:"withdrawalsRoot"`
TransactionsRoot common.Root `json:"transactionsRoot"`
WithdrawalsRoot common.Root `json:"withdrawalsRoot"`
BlobGasUsed math.U64 `json:"blobGasUsed"`
ExcessBlobGas math.U64 `json:"excessBlobGas"`
}
Expand Down Expand Up @@ -328,8 +328,8 @@ func (h *ExecutionPayloadHeader) UnmarshalJSON(input []byte) error {
ExtraData *bytes.Bytes `json:"extraData"`
BaseFeePerGas *math.U256 `json:"baseFeePerGas"`
BlockHash *common.ExecutionHash `json:"blockHash"`
TransactionsRoot *bytes.B32 `json:"transactionsRoot"`
WithdrawalsRoot *bytes.B32 `json:"withdrawalsRoot"`
TransactionsRoot *common.Root `json:"transactionsRoot"`
WithdrawalsRoot *common.Root `json:"withdrawalsRoot"`
BlobGasUsed *math.U64 `json:"blobGasUsed"`
ExcessBlobGas *math.U64 `json:"excessBlobGas"`
}
Expand Down
4 changes: 2 additions & 2 deletions mod/consensus-types/pkg/types/payload_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func generateExecutionPayloadHeader() *types.ExecutionPayloadHeader {
ExtraData: nil,
BaseFeePerGas: &math.U256{},
BlockHash: common.ExecutionHash{},
TransactionsRoot: bytes.B32{},
WithdrawalsRoot: bytes.B32{},
TransactionsRoot: common.Root{},
WithdrawalsRoot: common.Root{},
BlobGasUsed: math.U64(0),
ExcessBlobGas: math.U64(0),
}
Expand Down
7 changes: 4 additions & 3 deletions mod/consensus-types/pkg/types/signing_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (

types "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/stretchr/testify/require"
)

func generateSigningData() *types.SigningData {
return &types.SigningData{
ObjectRoot: bytes.B32{
ObjectRoot: common.Root{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
Domain: bytes.B32{
Expand All @@ -55,11 +56,11 @@ func TestSigningData_MarshalSSZ_UnmarshalSSZ(t *testing.T) {
{
name: "Empty SigningData",
data: &types.SigningData{
ObjectRoot: bytes.B32{},
ObjectRoot: common.Root{},
Domain: bytes.B32{},
},
expected: &types.SigningData{
ObjectRoot: bytes.B32{},
ObjectRoot: common.Root{},
Domain: bytes.B32{},
},
err: nil,
Expand Down
6 changes: 3 additions & 3 deletions mod/node-api/handlers/beacon/types/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ type BlockHeader[BlockHeaderT any] struct {
}

type GenesisData struct {
GenesisTime string `json:"genesis_time"`
GenesisValidatorsRoot common.Bytes32 `json:"genesis_validators_root"`
GenesisForkVersion string `json:"genesis_fork_version"`
GenesisTime string `json:"genesis_time"`
GenesisValidatorsRoot common.Root `json:"genesis_validators_root"`
GenesisForkVersion string `json:"genesis_fork_version"`
}

type RootData struct {
Expand Down
6 changes: 2 additions & 4 deletions mod/node-api/handlers/utils/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ package utils
import (
"strings"

"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)

Expand Down Expand Up @@ -57,11 +55,11 @@ func SlotFromBlockID[StorageBackendT interface {
}

// We assume that the block ID is a block hash.
root, err := hex.String(blockID).ToBytes()
root, err := common.NewRootFromHex(blockID)
if err != nil {
return 0, err
}
return storage.GetSlotByRoot(bytes.ToBytes32(root))
return storage.GetSlotByRoot(root)
}

// SlotFromExecutionID returns a slot from the execution number ID.
Expand Down
2 changes: 1 addition & 1 deletion mod/payload/pkg/attributes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type BeaconState[WithdrawalT any] interface {
// ExpectedWithdrawals returns the expected withdrawals.
ExpectedWithdrawals() ([]WithdrawalT, error)
// GetRandaoMixAtIndex returns the randao mix at the given index.
GetRandaoMixAtIndex(index uint64) (common.Root, error)
GetRandaoMixAtIndex(index uint64) (common.Bytes32, error)
}

// PayloadAttributes is the interface for the payload attributes.
Expand Down
62 changes: 58 additions & 4 deletions mod/primitives/pkg/common/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
package common

import (
"encoding/json"

"github.com/berachain/beacon-kit/mod/chain-spec/pkg/chain"
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)

Expand All @@ -44,10 +47,6 @@ type (
//nolint:lll
DomainType = bytes.B4

// Root as per the Ethereum 2.0 Specification:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
Root = bytes.B32

// Hash32 as per the Ethereum 2.0 Specification:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
Hash32 = bytes.B32
Expand All @@ -60,3 +59,58 @@ type (
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
ForkDigest = bytes.B4
)

/* -------------------------------------------------------------------------- */
/* Root */
/* -------------------------------------------------------------------------- */

// Root represents a 32-byte Merkle root.
// We use this type to represent roots that come from the consensus layer.
type Root [32]byte

// NewRootFromHex creates a new root from a hex string.
func NewRootFromHex(input string) (Root, error) {
val, err := hex.ToBytes(input)
if err != nil {
return Root{}, err
}
return Root(val), nil
}

// NewRootFromBytes creates a new root from a byte slice.
func NewRootFromBytes(input []byte) Root {
var root Root
copy(root[:], input)
return root
}

// Hex converts a root to a hex string.
func (r Root) Hex() string { return string(hex.EncodeBytes(r[:])) }

// String implements the stringer interface and is used also by the logger when
// doing full logging into a file.
func (r Root) String() string {
return r.Hex()
}

// MarshalText returns the hex representation of r.
func (r Root) MarshalText() ([]byte, error) {
return []byte(r.Hex()), nil
}

// UnmarshalText parses a root in hex syntax.
func (r *Root) UnmarshalText(input []byte) error {
var err error
*r, err = NewRootFromHex(string(input))
return err
}

// MarshalJSON returns the JSON representation of r.
func (r Root) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Hex())
}

// UnmarshalJSON parses a root in hex syntax.
func (r *Root) UnmarshalJSON(input []byte) error {
return r.UnmarshalText(input[1 : len(input)-1])
}
5 changes: 3 additions & 2 deletions mod/primitives/pkg/merkle/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"

byteslib "github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/merkle"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -110,7 +111,7 @@ func TestMerkleTree_IsValidMerkleBranch(t *testing.T) {
require.False(
t,
merkle.IsValidMerkleBranch(
byteslib.ToBytes32([]byte("buzz")),
common.Root(byteslib.ToBytes32([]byte("buzz"))),
proof,
treeDepth,
3,
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestMerkleTree_VerifyProof(t *testing.T) {
t,
merkle.VerifyProof(
root,
byteslib.ToBytes32([]byte("buzz")),
common.Root(byteslib.ToBytes32([]byte("buzz"))),
3,
proof,
),
Expand Down
6 changes: 3 additions & 3 deletions mod/state-transition/pkg/core/state/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type BeaconStateMarshallable[
// New returns a new instance of the BeaconStateMarshallable.
New(
forkVersion uint32,
genesisValidatorsRoot common.Bytes32,
genesisValidatorsRoot common.Root,
slot math.U64,
fork ForkT,
latestBlockHeader BeaconBlockHeaderT,
blockRoots []common.Bytes32,
stateRoots []common.Bytes32,
blockRoots []common.Root,
stateRoots []common.Root,
eth1Data Eth1DataT,
eth1DepositIndex uint64,
latestExecutionPayloadHeader ExecutionPayloadHeaderT,
Expand Down
2 changes: 1 addition & 1 deletion mod/state-transition/pkg/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ type ExecutionPayload[
GetWithdrawals() []WithdrawalT
GetFeeRecipient() common.ExecutionAddress
GetStateRoot() common.Bytes32
GetReceiptsRoot() common.Root
GetReceiptsRoot() common.Bytes32
GetLogsBloom() bytes.B256
GetNumber() math.U64
GetGasLimit() math.U64
Expand Down

0 comments on commit 49be5a8

Please sign in to comment.