Skip to content

Commit

Permalink
bet
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Jul 25, 2024
1 parent 201979a commit 485f069
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 532 deletions.
1 change: 1 addition & 0 deletions beacond/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ replace (
cosmossdk.io/x/consensus => cosmossdk.io/x/consensus v0.0.0-20240623110059-dec2d5583e39
cosmossdk.io/x/staking => cosmossdk.io/x/staking v0.0.0-20240623110059-dec2d5583e39
github.com/cosmos/cosmos-sdk => github.com/berachain/cosmos-sdk v0.46.0-beta2.0.20240624014538-75ba469b1881
github.com/karalabe/ssz => github.com/itsdevbear/ssz v0.0.0-20240725155830-5bd985129878
)

require (
Expand Down
134 changes: 125 additions & 9 deletions mod/da/pkg/types/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ package types

import (
"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/eip4844"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/berachain/beacon-kit/mod/primitives/pkg/merkle"
"github.com/karalabe/ssz"
)

// BlobSidecar as per the Ethereum 2.0 specification:
Expand All @@ -42,10 +44,10 @@ type BlobSidecar struct {
KzgProof eip4844.KZGProof
// BeaconBlockHeader represents the beacon block header for which this blob
// is being included.
BeaconBlockHeader *types.BeaconBlockHeader
BeaconBlockHeader *BeaconBlockHeader
// InclusionProof is the inclusion proof of the blob in the beacon block
// body.
InclusionProof [][32]byte `ssz-size:"8,32"`
InclusionProof [][32]byte
}

// BuildBlobSidecar creates a blob sidecar from the given blobs and
Expand All @@ -59,12 +61,14 @@ func BuildBlobSidecar(
inclusionProof [][32]byte,
) *BlobSidecar {
return &BlobSidecar{
Index: index.Unwrap(),
Blob: *blob,
KzgCommitment: commitment,
KzgProof: proof,
BeaconBlockHeader: header,
InclusionProof: inclusionProof,
Index: index.Unwrap(),
Blob: *blob,
KzgCommitment: commitment,
KzgProof: proof,
BeaconBlockHeader: &BeaconBlockHeader{
BeaconBlockHeader: header,
},
InclusionProof: inclusionProof,
}
}

Expand All @@ -82,7 +86,7 @@ func (b *BlobSidecar) HasValidInclusionProof(
gIndex := kzgOffset + b.Index

// Verify the inclusion proof.
return merkle.IsValidMerkleBranch(
result := merkle.IsValidMerkleBranch(
leaf,
b.InclusionProof,
//#nosec:G701 // safe.
Expand All @@ -92,4 +96,116 @@ func (b *BlobSidecar) HasValidInclusionProof(
gIndex,
b.BeaconBlockHeader.BodyRoot,
)
return result
}

// DefineSSZ defines the SSZ encoding for the BlobSidecar object.
func (b *BlobSidecar) DefineSSZ(codec *ssz.Codec) {
ssz.DefineUint64(codec, &b.Index)
ssz.DefineStaticBytes(codec, &b.Blob)
ssz.DefineStaticBytes(codec, &b.KzgCommitment)
ssz.DefineStaticBytes(codec, &b.KzgProof)
ssz.DefineStaticObject(codec, &b.BeaconBlockHeader)
ssz.DefineCheckedArrayOfStaticBytes(codec, &b.InclusionProof, 8)
}

// SizeSSZ returns the size of the BlobSidecar object in SSZ encoding.
func (b *BlobSidecar) SizeSSZ() uint32 {
return 8 + // Index
131072 + // Blob
48 + // KzgCommitment
48 + // KzgProof
112 + // BeaconBlockHeader
8*32 // InclusionProof
}

// MarshalSSZ marshals the BlobSidecar object to SSZ format.
func (b *BlobSidecar) MarshalSSZ() ([]byte, error) {
buf := make([]byte, b.SizeSSZ())
return buf, ssz.EncodeToBytes(buf, b)
}

// UnmarshalSSZ unmarshals the BlobSidecar object from SSZ format.
func (b *BlobSidecar) UnmarshalSSZ(buf []byte) error {
if b.BeaconBlockHeader == nil {
b.BeaconBlockHeader = &BeaconBlockHeader{}
}
return ssz.DecodeFromBytes(buf, b)
}

// MarshalSSZTo marshals the BlobSidecar object to the provided buffer in SSZ format.
func (b *BlobSidecar) MarshalSSZTo(buf []byte) ([]byte, error) {
return buf, ssz.EncodeToBytes(buf, b)
}

// HashTreeRoot computes the SSZ hash tree root of the BlobSidecar object.
func (b *BlobSidecar) HashTreeRoot() ([32]byte, error) {
return ssz.HashSequential(b), nil
}

type BeaconBlockHeader struct {
*types.BeaconBlockHeader
}

// SizeSSZ returns the size of the BeaconBlockHeader object in SSZ encoding.
func (b *BeaconBlockHeader) SizeSSZ() uint32 {
return 112 // Total size: Slot (8) + ProposerIndex (8) + ParentBlockRoot (32) + StateRoot (32) + BodyRoot (32)
}

// DefineSSZ defines the SSZ encoding for the BeaconBlockHeader object.
func (b *BeaconBlockHeader) DefineSSZ(codec *ssz.Codec) {
if b.BeaconBlockHeader == nil {
b.BeaconBlockHeader = &types.BeaconBlockHeader{}
}
ssz.DefineUint64(codec, &b.Slot)
ssz.DefineUint64(codec, &b.ProposerIndex)
ssz.DefineStaticBytes(codec, &b.ParentBlockRoot)
ssz.DefineStaticBytes(codec, &b.StateRoot)
ssz.DefineStaticBytes(codec, &b.BodyRoot)
}

// MarshalSSZToBytes marshals the BeaconBlockHeader object to SSZ format.
func (b *BeaconBlockHeader) MarshalSSZTo(buf []byte) ([]byte, error) {
return buf, ssz.EncodeToBytes(buf, b)
}

// MarshalSSZ marshals the BeaconBlockBody object to SSZ format.
func (b *BeaconBlockHeader) MarshalSSZ() ([]byte, error) {
buf := make([]byte, b.SizeSSZ())
return buf, ssz.EncodeToBytes(buf, b)
}

// UnmarshalSSZ unmarshals the BeaconBlockBody object from SSZ format.
func (b *BeaconBlockHeader) UnmarshalSSZ(buf []byte) error {
return ssz.DecodeFromBytes(buf, b)
}

// HashTreeRoot computes the SSZ hash tree root of the BeaconBlockHeader object.
func (b *BeaconBlockHeader) HashTreeRoot() ([32]byte, error) {
return ssz.HashSequential(b), nil
}

// GetSlot retrieves the slot of the BeaconBlockBase.
func (b *BeaconBlockHeader) GetSlot() math.Slot {
return math.Slot(b.Slot)
}

// GetSlot retrieves the slot of the BeaconBlockBase.
func (b *BeaconBlockHeader) GetProposerIndex() math.ValidatorIndex {
return math.ValidatorIndex(b.ProposerIndex)
}

// GetParentBlockRoot retrieves the parent block root of the BeaconBlockBase.
func (b *BeaconBlockHeader) GetParentBlockRoot() common.Root {
return b.ParentBlockRoot
}

// GetStateRoot retrieves the state root of the BeaconBlockDeneb.
func (b *BeaconBlockHeader) GetStateRoot() common.Root {
return b.StateRoot
}

// SetStateRoot sets the state root of the BeaconBlockHeader.
func (b *BeaconBlockHeader) SetStateRoot(stateRoot common.Root) {
b.StateRoot = stateRoot
}
144 changes: 0 additions & 144 deletions mod/da/pkg/types/sidecar.ssz.go

This file was deleted.

Loading

0 comments on commit 485f069

Please sign in to comment.