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

Deneb container changes beacon-chain #7420

Merged
merged 13 commits into from
May 6, 2023
1 change: 1 addition & 0 deletions cl/clparams/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ type BeaconChainConfig struct {
DomainApplicationMask [4]byte `yaml:"DOMAIN_APPLICATION_MASK" spec:"true"` // DomainApplicationMask defines the BLS signature domain for application mask.
DomainApplicationBuilder [4]byte // DomainApplicationBuilder defines the BLS signature domain for application builder.
DomainBLSToExecutionChange [4]byte // DomainBLSToExecutionChange defines the BLS signature domain to change withdrawal addresses to ETH1 prefix
DomainBlobSideCar [4]byte `yaml:"DOMAIN_BLOB_SIDECAR" spec:"true"` // DomainBlobSideCar defines the BLS signature domain for blob sidecar verification

// Prysm constants.
GweiPerEth uint64 // GweiPerEth is the amount of gwei corresponding to 1 eth.
Expand Down
3 changes: 2 additions & 1 deletion cl/clparams/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const (
Phase0Version StateVersion = 0
AltairVersion StateVersion = 1
BellatrixVersion StateVersion = 2
CapellaVersion StateVersion = 3 // Unimplemented!
CapellaVersion StateVersion = 3
DenebVersion StateVersion = 4
)

// stringToClVersion converts the string to the current state version.
Expand Down
52 changes: 42 additions & 10 deletions cl/cltypes/beacon_blob_side_car.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type BlobSideCar struct {
Slot Slot
BlockParentRoot Root
ProposerIndex uint64 // validator index
Blob Blob
Blob *Blob
KZGCommitment KZGCommitment
KZGProof KZGProof
}
Expand Down Expand Up @@ -80,14 +80,14 @@ func (b *BlobSideCar) EncodingSizeSSZ() int {
return 131_256
}

func (b *BlobSideCar) HashSSZ() (libcommon.Hash, error) {
func (b *BlobSideCar) HashSSZ() ([32]byte, error) {
KZGCommitmentLeave, err := merkle_tree.PublicKeyRoot(b.KZGCommitment)
if err != nil {
return libcommon.Hash{}, err
return [32]byte{}, err
}
KZGProofLeave, err := merkle_tree.PublicKeyRoot(b.KZGProof)
if err != nil {
return libcommon.Hash{}, err
return [32]byte{}, err
}

blobLeave := [][32]byte{}
Expand All @@ -99,7 +99,7 @@ func (b *BlobSideCar) HashSSZ() (libcommon.Hash, error) {

blobRoot, err := merkle_tree.ArraysRoot(blobLeave, 4096)
if err != nil {
return libcommon.Hash{}, err
return [32]byte{}, err
}

return merkle_tree.ArraysRoot([][32]byte{
Expand All @@ -115,7 +115,7 @@ func (b *BlobSideCar) HashSSZ() (libcommon.Hash, error) {
}

type SignedBlobSideCar struct {
Message BlobSideCar
Message *BlobSideCar
Signature [96]byte
}

Expand Down Expand Up @@ -148,15 +148,15 @@ func (b *SignedBlobSideCar) EncodingSizeSSZ() int {
return b.Message.EncodingSizeSSZ() + 96
}

func (b *SignedBlobSideCar) HashSSZ() (libcommon.Hash, error) {
func (b *SignedBlobSideCar) HashSSZ() ([32]byte, error) {
messageLeave, err := b.Message.HashSSZ()
if err != nil {
return libcommon.Hash{}, err
return [32]byte{}, err
}

signatureLeave, err := merkle_tree.SignatureRoot(b.Signature)
if err != nil {
return libcommon.Hash{}, err
return [32]byte{}, err
}

return merkle_tree.ArraysRoot([][32]byte{
Expand Down Expand Up @@ -192,9 +192,41 @@ func (b *BlobIdentifier) EncodingSizeSSZ() int {
return 40
}

func (b *BlobIdentifier) HashSSZ() (libcommon.Hash, error) {
func (b *BlobIdentifier) HashSSZ() ([32]byte, error) {
return merkle_tree.ArraysRoot([][32]byte{
b.BlockRoot,
merkle_tree.Uint64Root(b.Index),
}, 2)
}

type BlobKZGCommitment struct {
Commitment KZGCommitment
}

func (b *BlobKZGCommitment) Copy() *BlobKZGCommitment {
copy := *b
return &copy
}

func (b *BlobKZGCommitment) EncodeSSZ(buf []byte) ([]byte, error) {
buf = append(buf, b.Commitment[:]...)
return buf, nil
}

func (b *BlobKZGCommitment) DecodeSSZ(buf []byte) error {
copy(b.Commitment[:], buf[:])

return nil
}

func (b *BlobKZGCommitment) DecodeSSZWithVersion(buf []byte, version int) error {
return b.DecodeSSZ(buf)
}

func (b *BlobKZGCommitment) EncodingSizeSSZ() int {
return 48
}

func (b *BlobKZGCommitment) HashSSZ() ([32]byte, error) {
return merkle_tree.PublicKeyRoot(b.Commitment)
}
10 changes: 5 additions & 5 deletions cl/cltypes/beacon_blob_side_car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestBlobSideCar_EncodeDecodeSSZ(t *testing.T) {
slot := cltypes.Slot(456)
blockParentRoot := cltypes.Root{4, 5, 6}
proposerIndex := uint64(789)
blob := cltypes.Blob{}
blob := &cltypes.Blob{1, 2, 3, 4, 5, 6, 7, 8}
kzgCommitment := cltypes.KZGCommitment{7, 8, 9}
kzgProof := cltypes.KZGProof{10, 11, 12}

Expand All @@ -34,7 +34,7 @@ func TestBlobSideCar_EncodeDecodeSSZ(t *testing.T) {
t.Fatal(err)
}

decoded := &cltypes.BlobSideCar{}
decoded := &cltypes.BlobSideCar{Blob: &cltypes.Blob{}}
err = decoded.DecodeSSZ(encoded)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -69,13 +69,13 @@ func TestBlobSideCar_EncodeDecodeSSZ(t *testing.T) {
func TestSignedBlobSideCar(t *testing.T) {
// Create a BlobSideCar to use as the message for SignedBlobSideCar
blob := cltypes.Blob{1, 2, 3, 4, 5, 6, 7, 8}
blobSideCar := cltypes.BlobSideCar{
blobSideCar := &cltypes.BlobSideCar{
BlockRoot: cltypes.Root{1},
Index: 2,
Slot: 3,
BlockParentRoot: cltypes.Root{4},
ProposerIndex: 5,
Blob: blob,
Blob: &blob,
KZGCommitment: cltypes.KZGCommitment{6},
KZGProof: cltypes.KZGProof{7},
}
Expand All @@ -92,7 +92,7 @@ func TestSignedBlobSideCar(t *testing.T) {
assert.NoError(t, err)

// Decode the encoded SignedBlobSideCar
decoded := cltypes.SignedBlobSideCar{}
decoded := cltypes.SignedBlobSideCar{Message: &cltypes.BlobSideCar{Blob: &cltypes.Blob{}}}
err = decoded.DecodeSSZ(encoded)
assert.NoError(t, err)

Expand Down
107 changes: 86 additions & 21 deletions cl/cltypes/beacon_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ type BeaconBlockForStorage struct {
ParentRoot libcommon.Hash
StateRoot libcommon.Hash
// Body fields
RandaoReveal [96]byte
Eth1Data *Eth1Data
Graffiti []byte
ProposerSlashings []*ProposerSlashing
AttesterSlashings []*AttesterSlashing
Deposits []*Deposit
VoluntaryExits []*SignedVoluntaryExit
AddressChanges []*SignedBLSToExecutionChange
SyncAggregate *SyncAggregate
RandaoReveal [96]byte
Eth1Data *Eth1Data
Graffiti []byte
ProposerSlashings []*ProposerSlashing
AttesterSlashings []*AttesterSlashing
Deposits []*Deposit
VoluntaryExits []*SignedVoluntaryExit
AddressChanges []*SignedBLSToExecutionChange
SyncAggregate *SyncAggregate
BlobKzgCommitments []*BlobKZGCommitment
// Metadatas
Eth1Number uint64
Eth1BlockHash libcommon.Hash
Expand All @@ -48,10 +49,13 @@ const (
MaxDeposits = 16
MaxVoluntaryExits = 16
MaxExecutionChanges = 16
MaxBlobsPerBlock = 4
)

func getBeaconBlockMinimumSize(v clparams.StateVersion) (size uint32) {
switch v {
case clparams.DenebVersion:
size = 392
case clparams.CapellaVersion:
size = 388
case clparams.BellatrixVersion:
Expand Down Expand Up @@ -102,6 +106,9 @@ type BeaconBody struct {
ExecutionPayload *Eth1Block
// Withdrawals Diffs for Execution Layer
ExecutionChanges []*SignedBLSToExecutionChange
// The commitments for beacon chain blobs
// With a max of 4 per block
BlobKzgCommitments []*BlobKZGCommitment
// The version of the beacon chain
Version clparams.StateVersion
}
Expand Down Expand Up @@ -223,6 +230,18 @@ func (b *BeaconBody) EncodeSSZ(dst []byte) ([]byte, error) {
}
}
}

if b.Version >= clparams.DenebVersion {
if len(b.BlobKzgCommitments) > MaxBlobsPerBlock {
return nil, fmt.Errorf("Encode(SSZ): too many blob kzg commitments in the block")
}
for _, commitment := range b.BlobKzgCommitments {
if buf, err = commitment.EncodeSSZ(buf); err != nil {
return nil, err
}
}
}

return buf, nil
}

Expand Down Expand Up @@ -256,11 +275,18 @@ func (b *BeaconBody) EncodingSizeSSZ() (size int) {
size += change.EncodingSizeSSZ()
}
}

if b.Version >= clparams.DenebVersion {
for _, commitment := range b.BlobKzgCommitments {
size += commitment.EncodingSizeSSZ()
}
}

return
}

func (b *BeaconBody) DecodeSSZ(buf []byte) error {
panic("yo")
panic("DecodeSSZ not implemented for BeaconBody")
}

func (b *BeaconBody) DecodeSSZWithVersion(buf []byte, version int) error {
Expand Down Expand Up @@ -308,6 +334,12 @@ func (b *BeaconBody) DecodeSSZWithVersion(buf []byte, version int) error {
if b.Version >= clparams.CapellaVersion {
blsChangesOffset = ssz.DecodeOffset(buf[384:])
}

var blobKzgCommitmentOffset uint32
if b.Version >= clparams.DenebVersion {
blobKzgCommitmentOffset = ssz.DecodeOffset(buf[388:])
}

// Decode Proposer slashings
proposerSlashingLength := 416
b.ProposerSlashings, err = ssz.DecodeStaticList[*ProposerSlashing](buf, offSetProposerSlashings, offsetAttesterSlashings, uint32(proposerSlashingLength), MaxProposerSlashings)
Expand Down Expand Up @@ -348,19 +380,29 @@ func (b *BeaconBody) DecodeSSZWithVersion(buf []byte, version int) error {
if b.Version >= clparams.BellatrixVersion {
b.ExecutionPayload = new(Eth1Block)
if offsetExecution > uint32(endOffset) || len(buf) < endOffset {
fmt.Println("Still")
return ssz.ErrBadOffset
}
if err := b.ExecutionPayload.DecodeSSZWithVersion(buf[offsetExecution:endOffset], int(b.Version)); err != nil {
return err
}
}

if b.Version >= clparams.DenebVersion {
endOffset = int(blobKzgCommitmentOffset)
}
if b.Version >= clparams.CapellaVersion {
if b.ExecutionChanges, err = ssz.DecodeStaticList[*SignedBLSToExecutionChange](buf, blsChangesOffset, uint32(len(buf)), 172, MaxExecutionChanges); err != nil {
if b.ExecutionChanges, err = ssz.DecodeStaticList[*SignedBLSToExecutionChange](buf, blsChangesOffset, uint32(endOffset), 172, MaxExecutionChanges); err != nil {
return err
}
}

if b.Version >= clparams.DenebVersion {
if b.BlobKzgCommitments, err = ssz.DecodeStaticList[*BlobKZGCommitment](buf, blobKzgCommitmentOffset, uint32(len(buf)), 48, MaxBlobsPerBlock); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -434,6 +476,24 @@ func (b *BeaconBody) HashSSZ() ([32]byte, error) {
}
leaves = append(leaves, blsExecutionLeaf)
}

if b.Version >= clparams.DenebVersion {
blobKzgCommitmentsRoot := make([][32]byte, len(b.BlobKzgCommitments))
for i, commitment := range b.BlobKzgCommitments {
pk, err := commitment.HashSSZ()
if err != nil {
return [32]byte{}, err
}

copy(blobKzgCommitmentsRoot[i][:], pk[:])
}
blobKzgCommitmentsLeaf, err := merkle_tree.ArraysRoot(blobKzgCommitmentsRoot, MaxBlobsPerBlock)
if err != nil {
return [32]byte{}, err
}
leaves = append(leaves, blobKzgCommitmentsLeaf)
}

if b.Version == clparams.Phase0Version {
return merkle_tree.ArraysRoot(leaves, 8)
}
Expand Down Expand Up @@ -568,6 +628,10 @@ func (b *SignedBeaconBlock) EncodeForStorage() ([]byte, error) {
storageObject.Eth1Number = eth1Block.BlockNumber
storageObject.Eth1BlockHash = eth1Block.BlockHash
}
if b.Version() >= clparams.DenebVersion {
storageObject.BlobKzgCommitments = b.Block.Body.BlobKzgCommitments
}

var buffer bytes.Buffer
if err := cbor.Marshal(&buffer, storageObject); err != nil {
return nil, err
Expand Down Expand Up @@ -598,16 +662,17 @@ func DecodeBeaconBlockForStorage(buf []byte) (block *SignedBeaconBlock, eth1Numb
ParentRoot: storageObject.ParentRoot,
StateRoot: storageObject.StateRoot,
Body: &BeaconBody{
RandaoReveal: storageObject.RandaoReveal,
Eth1Data: storageObject.Eth1Data,
Graffiti: storageObject.Graffiti,
ProposerSlashings: storageObject.ProposerSlashings,
AttesterSlashings: storageObject.AttesterSlashings,
Deposits: storageObject.Deposits,
VoluntaryExits: storageObject.VoluntaryExits,
SyncAggregate: storageObject.SyncAggregate,
ExecutionChanges: storageObject.AddressChanges,
Version: clparams.StateVersion(storageObject.Version),
RandaoReveal: storageObject.RandaoReveal,
Eth1Data: storageObject.Eth1Data,
Graffiti: storageObject.Graffiti,
ProposerSlashings: storageObject.ProposerSlashings,
AttesterSlashings: storageObject.AttesterSlashings,
Deposits: storageObject.Deposits,
VoluntaryExits: storageObject.VoluntaryExits,
SyncAggregate: storageObject.SyncAggregate,
ExecutionChanges: storageObject.AddressChanges,
BlobKzgCommitments: storageObject.BlobKzgCommitments,
Version: clparams.StateVersion(storageObject.Version),
},
},
}, storageObject.Eth1Number, storageObject.Eth1BlockHash, storageObject.Eth2BlockRoot, nil
Expand Down
Loading