Skip to content

Commit

Permalink
Merge branch 'main' into e2e-node-api
Browse files Browse the repository at this point in the history
Signed-off-by: Nidhi Singh <[email protected]>
Signed-off-by: nidhi-singh02 <[email protected]>
  • Loading branch information
nidhi-singh02 committed Oct 17, 2024
2 parents d4caf1d + 8c2030a commit dccfde1
Show file tree
Hide file tree
Showing 83 changed files with 2,143 additions and 1,769 deletions.
8 changes: 7 additions & 1 deletion mod/beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package validator

import (
"context"
"fmt"
"time"

engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
Expand Down Expand Up @@ -303,7 +304,12 @@ func (s *Service[
))

// Set the graffiti on the block body.
body.SetGraffiti(bytes.ToBytes32([]byte(s.cfg.Graffiti)))
sizedGraffiti := bytes.ExtendToSize([]byte(s.cfg.Graffiti), bytes.B32Size)
graffiti, err := bytes.ToBytes32(sizedGraffiti)
if err != nil {
return fmt.Errorf("failed processing graffiti: %w", err)
}
body.SetGraffiti(graffiti)

// Get the epoch to find the active fork version.
epoch := s.chainSpec.SlotToEpoch(blk.GetSlot())
Expand Down
6 changes: 3 additions & 3 deletions mod/consensus-types/pkg/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ func (b *BeaconBlock) GetHeader() *BeaconBlockHeader {
}
}

// GetExecutionNumber retrieves the execution number of the BeaconBlock from
// GetTimestamp retrieves the timestamp of the BeaconBlock from
// the ExecutionPayload.
func (b *BeaconBlock) GetExecutionNumber() math.U64 {
return b.Body.ExecutionPayload.Number
func (b *BeaconBlock) GetTimestamp() math.U64 {
return b.Body.ExecutionPayload.Timestamp
}
4 changes: 2 additions & 2 deletions mod/consensus-types/pkg/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func generateValidBeaconBlock() *types.BeaconBlock {
StateRoot: common.Root{5, 4, 3, 2, 1},
Body: &types.BeaconBlockBody{
ExecutionPayload: &types.ExecutionPayload{
Number: 10,
Timestamp: 10,
ExtraData: []byte("dummy extra data for testing"),
Transactions: [][]byte{
[]byte("tx1"),
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestBeaconBlock(t *testing.T) {
block := generateValidBeaconBlock()

require.NotNil(t, block.Body)
require.Equal(t, math.U64(10), block.GetExecutionNumber())
require.Equal(t, math.U64(10), block.GetTimestamp())
require.Equal(t, version.Deneb, block.Version())
require.False(t, block.IsNil())

Expand Down
36 changes: 23 additions & 13 deletions mod/consensus-types/pkg/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package types

import (
"fmt"
"math/big"

engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
Expand Down Expand Up @@ -131,22 +132,31 @@ func DefaultGenesisDeneb() *Genesis[
func DefaultGenesisExecutionPayloadHeaderDeneb() (
*ExecutionPayloadHeader, error,
) {
stateRoot, err := byteslib.ToBytes32(
hex.MustToBytes(
"0x12965ab9cbe2d2203f61d23636eb7e998f167cb79d02e452f532535641e35bcc",
),
)
if err != nil {
return nil, fmt.Errorf("failed generating state root: %w", err)
}

receiptsRoot, err := byteslib.ToBytes32(
hex.MustToBytes(
"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
),
)
if err != nil {
return nil, fmt.Errorf("failed generating receipts root: %w", err)
}
return &ExecutionPayloadHeader{
ParentHash: common.ExecutionHash{},
FeeRecipient: common.ExecutionAddress{},
StateRoot: byteslib.ToBytes32(
hex.MustToBytes(
"0x12965ab9cbe2d2203f61d23636eb7e998f167cb79d02e452f532535641e35bcc",
),
),
ReceiptsRoot: byteslib.ToBytes32(
hex.MustToBytes(
"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
),
),
LogsBloom: [256]byte{},
Random: common.Bytes32{},
Number: 0,
StateRoot: stateRoot,
ReceiptsRoot: receiptsRoot,
LogsBloom: [256]byte{},
Random: common.Bytes32{},
Number: 0,
//nolint:mnd // default value.
GasLimit: math.U64(30000000),
GasUsed: 0,
Expand Down
120 changes: 65 additions & 55 deletions mod/da/pkg/types/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package types_test

import (
"strconv"
"testing"

ctypes "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
Expand All @@ -39,22 +40,20 @@ func TestSidecarMarshalling(t *testing.T) {
for i := range blob {
blob[i] = byte(i % 256)
}
inclusionProof := make([]common.Root, 0)
for i := int(1); i <= 8; i++ {
it := byteslib.ExtendToSize([]byte(strconv.Itoa(i)), byteslib.B32Size)
proof, err := byteslib.ToBytes32(it)
require.NoError(t, err)
inclusionProof = append(inclusionProof, common.Root(proof))
}
sidecar := types.BuildBlobSidecar(
1,
&ctypes.BeaconBlockHeader{},
&blob,
eip4844.KZGCommitment{},
eip4844.KZGProof{},
[]common.Root{
common.Root(byteslib.ToBytes32([]byte("1"))),
common.Root(byteslib.ToBytes32([]byte("2"))),
common.Root(byteslib.ToBytes32([]byte("3"))),
common.Root(byteslib.ToBytes32([]byte("4"))),
common.Root(byteslib.ToBytes32([]byte("5"))),
common.Root(byteslib.ToBytes32([]byte("6"))),
common.Root(byteslib.ToBytes32([]byte("7"))),
common.Root(byteslib.ToBytes32([]byte("8"))),
},
inclusionProof,
)

// Marshal the sidecar
Expand All @@ -79,47 +78,56 @@ func TestSidecarMarshalling(t *testing.T) {
func TestHasValidInclusionProof(t *testing.T) {
tests := []struct {
name string
sidecar *types.BlobSidecar
sidecar func(t *testing.T) *types.BlobSidecar
kzgOffset uint64
expectedResult bool
}{
{
name: "Invalid inclusion proof",
sidecar: types.BuildBlobSidecar(
math.U64(0),
&ctypes.BeaconBlockHeader{
BodyRoot: [32]byte{3},
},
&eip4844.Blob{},
eip4844.KZGCommitment{},
eip4844.KZGProof{},
[]common.Root{
common.Root(byteslib.ToBytes32([]byte("4"))),
common.Root(byteslib.ToBytes32([]byte("5"))),
common.Root(byteslib.ToBytes32([]byte("6"))),
},
),
sidecar: func(t *testing.T) *types.BlobSidecar {
t.Helper()
inclusionProof := make([]common.Root, 0)
for i := int(1); i <= 8; i++ {
it := byteslib.ExtendToSize([]byte(strconv.Itoa(i)), byteslib.B32Size)
proof, err := byteslib.ToBytes32(it)
require.NoError(t, err)
inclusionProof = append(inclusionProof, common.Root(proof))
}
return types.BuildBlobSidecar(
math.U64(0),
&ctypes.BeaconBlockHeader{
BodyRoot: [32]byte{3},
},
&eip4844.Blob{},
eip4844.KZGCommitment{},
eip4844.KZGProof{},
inclusionProof,
)
},
kzgOffset: 0,
expectedResult: false,
},
{
name: "Empty inclusion proof",
sidecar: types.BuildBlobSidecar(
math.U64(0),
&ctypes.BeaconBlockHeader{},
&eip4844.Blob{},
eip4844.KZGCommitment{},
eip4844.KZGProof{},
[]common.Root{},
),
sidecar: func(*testing.T) *types.BlobSidecar {
return types.BuildBlobSidecar(
math.U64(0),
&ctypes.BeaconBlockHeader{},
&eip4844.Blob{},
eip4844.KZGCommitment{},
eip4844.KZGProof{},
[]common.Root{},
)
},
kzgOffset: 0,
expectedResult: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.sidecar.HasValidInclusionProof(tt.kzgOffset)
sidecar := tt.sidecar(t)
result := sidecar.HasValidInclusionProof(tt.kzgOffset)
require.Equal(t, tt.expectedResult, result,
"Result should match expected value")
})
Expand All @@ -129,31 +137,32 @@ func TestHasValidInclusionProof(t *testing.T) {
func TestHashTreeRoot(t *testing.T) {
tests := []struct {
name string
sidecar *types.BlobSidecar
sidecar func(t *testing.T) *types.BlobSidecar
expectedResult common.Root
expectError bool
}{
{
name: "Valid BlobSidecar",
sidecar: types.BuildBlobSidecar(
math.U64(1),
&ctypes.BeaconBlockHeader{
BodyRoot: [32]byte{7, 8, 9},
},
&eip4844.Blob{0, 1, 2, 3, 4, 5, 6, 7},
eip4844.KZGCommitment{1, 2, 3},
eip4844.KZGProof{4, 5, 6},
[]common.Root{
common.Root(byteslib.ToBytes32([]byte("1"))),
common.Root(byteslib.ToBytes32([]byte("2"))),
common.Root(byteslib.ToBytes32([]byte("3"))),
common.Root(byteslib.ToBytes32([]byte("4"))),
common.Root(byteslib.ToBytes32([]byte("5"))),
common.Root(byteslib.ToBytes32([]byte("6"))),
common.Root(byteslib.ToBytes32([]byte("7"))),
common.Root(byteslib.ToBytes32([]byte("8"))),
},
),
sidecar: func(t *testing.T) *types.BlobSidecar {
t.Helper()
inclusionProof := make([]common.Root, 0)
for i := int(1); i <= 8; i++ {
it := byteslib.ExtendToSize([]byte(strconv.Itoa(i)), byteslib.B32Size)
proof, err := byteslib.ToBytes32(it)
require.NoError(t, err)
inclusionProof = append(inclusionProof, common.Root(proof))
}
return types.BuildBlobSidecar(
math.U64(1),
&ctypes.BeaconBlockHeader{
BodyRoot: [32]byte{7, 8, 9},
},
&eip4844.Blob{0, 1, 2, 3, 4, 5, 6, 7},
eip4844.KZGCommitment{1, 2, 3},
eip4844.KZGProof{4, 5, 6},
inclusionProof,
)
},
expectedResult: [32]uint8{
0xce, 0x75, 0x41, 0x87, 0x48, 0x46, 0x6d, 0x26, 0x9e, 0x72, 0x5d,
0xac, 0x5a, 0x6e, 0x36, 0xed, 0x8c, 0x2a, 0x98, 0x19, 0x6b, 0xe1,
Expand All @@ -165,7 +174,8 @@ func TestHashTreeRoot(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.NotPanics(t, func() {
result := tt.sidecar.HashTreeRoot()
sidecar := tt.sidecar(t)
result := sidecar.HashTreeRoot()
require.Equal(
t,
tt.expectedResult,
Expand Down
50 changes: 20 additions & 30 deletions mod/da/pkg/types/sidecars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package types_test

import (
"strconv"
"testing"

ctypes "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
Expand All @@ -34,22 +35,21 @@ import (

func TestEmptySidecarMarshalling(t *testing.T) {
// Create an empty BlobSidecar
inclusionProof := make([]common.Root, 0)
for i := int(1); i <= 8; i++ {
it := byteslib.ExtendToSize([]byte(strconv.Itoa(i)), byteslib.B32Size)
proof, err := byteslib.ToBytes32(it)
require.NoError(t, err)
inclusionProof = append(inclusionProof, common.Root(proof))
}

sidecar := types.BuildBlobSidecar(
math.U64(0),
&ctypes.BeaconBlockHeader{},
&eip4844.Blob{},
eip4844.KZGCommitment{},
[48]byte{},
[]common.Root{
common.Root(byteslib.ToBytes32([]byte("1"))),
common.Root(byteslib.ToBytes32([]byte("2"))),
common.Root(byteslib.ToBytes32([]byte("3"))),
common.Root(byteslib.ToBytes32([]byte("4"))),
common.Root(byteslib.ToBytes32([]byte("5"))),
common.Root(byteslib.ToBytes32([]byte("6"))),
common.Root(byteslib.ToBytes32([]byte("7"))),
common.Root(byteslib.ToBytes32([]byte("8"))),
},
inclusionProof,
)

// Marshal the empty sidecar
Expand Down Expand Up @@ -85,6 +85,14 @@ func TestEmptySidecarMarshalling(t *testing.T) {

func TestValidateBlockRoots(t *testing.T) {
// Create a sample BlobSidecar with valid roots
inclusionProof := make([]common.Root, 0)
for i := int(1); i <= 8; i++ {
it := byteslib.ExtendToSize([]byte(strconv.Itoa(i)), byteslib.B32Size)
proof, err := byteslib.ToBytes32(it)
require.NoError(t, err)
inclusionProof = append(inclusionProof, common.Root(proof))
}

validSidecar := types.BuildBlobSidecar(
math.U64(0),
&ctypes.BeaconBlockHeader{
Expand All @@ -95,16 +103,7 @@ func TestValidateBlockRoots(t *testing.T) {
&eip4844.Blob{},
[48]byte{},
[48]byte{},
[]common.Root{
common.Root(byteslib.ToBytes32([]byte("1"))),
common.Root(byteslib.ToBytes32([]byte("2"))),
common.Root(byteslib.ToBytes32([]byte("3"))),
common.Root(byteslib.ToBytes32([]byte("4"))),
common.Root(byteslib.ToBytes32([]byte("5"))),
common.Root(byteslib.ToBytes32([]byte("6"))),
common.Root(byteslib.ToBytes32([]byte("7"))),
common.Root(byteslib.ToBytes32([]byte("8"))),
},
inclusionProof,
)

// Validate the sidecar with valid roots
Expand All @@ -128,16 +127,7 @@ func TestValidateBlockRoots(t *testing.T) {
&eip4844.Blob{},
eip4844.KZGCommitment{},
eip4844.KZGProof{},
[]common.Root{
common.Root(byteslib.ToBytes32([]byte("1"))),
common.Root(byteslib.ToBytes32([]byte("2"))),
common.Root(byteslib.ToBytes32([]byte("3"))),
common.Root(byteslib.ToBytes32([]byte("4"))),
common.Root(byteslib.ToBytes32([]byte("5"))),
common.Root(byteslib.ToBytes32([]byte("6"))),
common.Root(byteslib.ToBytes32([]byte("7"))),
common.Root(byteslib.ToBytes32([]byte("8"))),
},
inclusionProof,
)
// Validate the sidecar with invalid roots
sidecarsInvalid := types.BlobSidecars{
Expand Down
Loading

0 comments on commit dccfde1

Please sign in to comment.