Skip to content

Commit

Permalink
wip adding electra block and state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
james-prysm committed Apr 23, 2024
1 parent e21928f commit d9ee653
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 0 deletions.
5 changes: 5 additions & 0 deletions beacon-chain/state/state-native/state_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func InitializeFromProtoDeneb(st *ethpb.BeaconStateDeneb) (state.BeaconState, er
return InitializeFromProtoUnsafeDeneb(proto.Clone(st).(*ethpb.BeaconStateDeneb))
}

// InitializeFromProtoElectra the beacon state from a protobuf representation.
func InitializeFromProtoElectra(st *ethpb.BeaconStateElectra) (state.BeaconState, error) {
return InitializeFromProtoUnsafeElectra(proto.Clone(st).(*ethpb.BeaconStateElectra))
}

// InitializeFromProtoUnsafePhase0 directly uses the beacon state protobuf fields
// and sets them as fields of the BeaconState type.
func InitializeFromProtoUnsafePhase0(st *ethpb.BeaconState) (state.BeaconState, error) {
Expand Down
2 changes: 2 additions & 0 deletions consensus-types/blocks/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func NewSignedBeaconBlock(i interface{}) (interfaces.SignedBeaconBlock, error) {
return initBlindedSignedBlockFromProtoDeneb(b)
case *eth.GenericSignedBeaconBlock_BlindedDeneb:
return initBlindedSignedBlockFromProtoDeneb(b.BlindedDeneb)
case *eth.SignedBeaconBlockElectra:
return initSignedBlockFromProtoElectra(b)
default:
return nil, errors.Wrapf(ErrUnsupportedSignedBeaconBlock, "unable to create block from type %T", i)
}
Expand Down
65 changes: 65 additions & 0 deletions consensus-types/blocks/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,23 @@ func initSignedBlockFromProtoDeneb(pb *eth.SignedBeaconBlockDeneb) (*SignedBeaco
return b, nil
}

func initSignedBlockFromProtoElectra(pb *eth.SignedBeaconBlockElectra) (*SignedBeaconBlock, error) {
if pb == nil {
return nil, errNilBlock
}

block, err := initBlockFromProtoElectra(pb.Block)
if err != nil {
return nil, err
}
b := &SignedBeaconBlock{
version: version.Electra,
block: block,
signature: bytesutil.ToBytes96(pb.Signature),
}
return b, nil
}

func initBlindedSignedBlockFromProtoBellatrix(pb *eth.SignedBlindedBeaconBlockBellatrix) (*SignedBeaconBlock, error) {
if pb == nil {
return nil, errNilBlock
Expand Down Expand Up @@ -710,6 +727,26 @@ func initBlockFromProtoDeneb(pb *eth.BeaconBlockDeneb) (*BeaconBlock, error) {
return b, nil
}

func initBlockFromProtoElectra(pb *eth.BeaconBlockElectra) (*BeaconBlock, error) {
if pb == nil {
return nil, errNilBlock
}

body, err := initBlockBodyFromProtoElectra(pb.Body)
if err != nil {
return nil, err
}
b := &BeaconBlock{
version: version.Electra,
slot: pb.Slot,
proposerIndex: pb.ProposerIndex,
parentRoot: bytesutil.ToBytes32(pb.ParentRoot),
stateRoot: bytesutil.ToBytes32(pb.StateRoot),
body: body,
}
return b, nil
}

func initBlindedBlockFromProtoCapella(pb *eth.BlindedBeaconBlockCapella) (*BeaconBlock, error) {
if pb == nil {
return nil, errNilBlock
Expand Down Expand Up @@ -950,3 +987,31 @@ func initBlindedBlockBodyFromProtoDeneb(pb *eth.BlindedBeaconBlockBodyDeneb) (*B
}
return b, nil
}

func initBlockBodyFromProtoElectra(pb *eth.BeaconBlockBodyElectra) (*BeaconBlockBody, error) {
if pb == nil {
return nil, errNilBlockBody
}

p, err := WrappedExecutionPayloadElectra(pb.ExecutionPayload, big.NewInt(0))
// We allow the payload to be nil
if err != nil && err != consensus_types.ErrNilObjectWrapped {
return nil, err
}
b := &BeaconBlockBody{
version: version.Electra,
randaoReveal: bytesutil.ToBytes96(pb.RandaoReveal),
eth1Data: pb.Eth1Data,
graffiti: bytesutil.ToBytes32(pb.Graffiti),
proposerSlashings: pb.ProposerSlashings,
attesterSlashings: pb.AttesterSlashings,

Check failure on line 1007 in consensus-types/blocks/proto.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use pb.AttesterSlashings (variable of type []*eth.AttesterSlashingElectra) as []*eth.AttesterSlashing value in struct literal

Check failure on line 1007 in consensus-types/blocks/proto.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use pb.AttesterSlashings (variable of type []*eth.AttesterSlashingElectra) as []*eth.AttesterSlashing value in struct literal

Check failure on line 1007 in consensus-types/blocks/proto.go

View workflow job for this annotation

GitHub Actions / Build

cannot use pb.AttesterSlashings (variable of type []*eth.AttesterSlashingElectra) as []*eth.AttesterSlashing value in struct literal

Check failure on line 1007 in consensus-types/blocks/proto.go

View workflow job for this annotation

GitHub Actions / Gosec scan

cannot use pb.AttesterSlashings (variable of type []*eth.AttesterSlashingElectra) as []*eth.AttesterSlashing value in struct literal
attestations: pb.Attestations,

Check failure on line 1008 in consensus-types/blocks/proto.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use pb.Attestations (variable of type []*eth.AttestationElectra) as []*eth.Attestation value in struct literal) (typecheck)

Check failure on line 1008 in consensus-types/blocks/proto.go

View workflow job for this annotation

GitHub Actions / Lint

cannot use pb.Attestations (variable of type []*eth.AttestationElectra) as []*eth.Attestation value in struct literal) (typecheck)

Check failure on line 1008 in consensus-types/blocks/proto.go

View workflow job for this annotation

GitHub Actions / Build

cannot use pb.Attestations (variable of type []*eth.AttestationElectra) as []*eth.Attestation value in struct literal

Check failure on line 1008 in consensus-types/blocks/proto.go

View workflow job for this annotation

GitHub Actions / Gosec scan

cannot use pb.Attestations (variable of type []*eth.AttestationElectra) as []*eth.Attestation value in struct literal
deposits: pb.Deposits,
voluntaryExits: pb.VoluntaryExits,
syncAggregate: pb.SyncAggregate,
executionPayload: p,
blsToExecutionChanges: pb.BlsToExecutionChanges,
blobKzgCommitments: pb.BlobKzgCommitments,
}
return b, nil
}
2 changes: 2 additions & 0 deletions runtime/version/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
Bellatrix
Capella
Deneb
Electra
)

var versionToString = map[int]string{
Expand All @@ -16,6 +17,7 @@ var versionToString = map[int]string{
Bellatrix: "bellatrix",
Capella: "capella",
Deneb: "deneb",
Electra: "electra",
}

// stringToVersion and allVersions are populated in init()
Expand Down
73 changes: 73 additions & 0 deletions testing/util/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,3 +1408,76 @@ func HydrateV2BlindedBeaconBlockBodyDeneb(b *v2.BlindedBeaconBlockBodyDeneb) *v2
}
return b
}

// HydrateSignedBeaconBlockElectra hydrates a signed beacon block with correct field length sizes
// to comply with fssz marshalling and unmarshalling rules.
func HydrateSignedBeaconBlockElectra(b *ethpb.SignedBeaconBlockElectra) *ethpb.SignedBeaconBlockElectra {
if b == nil {
b = &ethpb.SignedBeaconBlockElectra{}
}
if b.Signature == nil {
b.Signature = make([]byte, fieldparams.BLSSignatureLength)
}
b.Block = HydrateBeaconBlockElectra(b.Block)
return b
}

// HydrateBeaconBlockElectra hydrates a beacon block with correct field length sizes
// to comply with fssz marshalling and unmarshalling rules.
func HydrateBeaconBlockElectra(b *ethpb.BeaconBlockElectra) *ethpb.BeaconBlockElectra {
if b == nil {
b = &ethpb.BeaconBlockElectra{}
}
if b.ParentRoot == nil {
b.ParentRoot = make([]byte, fieldparams.RootLength)
}
if b.StateRoot == nil {
b.StateRoot = make([]byte, fieldparams.RootLength)
}
b.Body = HydrateBeaconBlockBodyElectra(b.Body)
return b
}

// HydrateBeaconBlockBodyElectra hydrates a beacon block body with correct field length sizes
// to comply with fssz marshalling and unmarshalling rules.
func HydrateBeaconBlockBodyElectra(b *ethpb.BeaconBlockBodyElectra) *ethpb.BeaconBlockBodyElectra {
if b == nil {
b = &ethpb.BeaconBlockBodyElectra{}
}
if b.RandaoReveal == nil {
b.RandaoReveal = make([]byte, fieldparams.BLSSignatureLength)
}
if b.Graffiti == nil {
b.Graffiti = make([]byte, fieldparams.RootLength)
}
if b.Eth1Data == nil {
b.Eth1Data = &ethpb.Eth1Data{
DepositRoot: make([]byte, fieldparams.RootLength),
BlockHash: make([]byte, fieldparams.RootLength),
}
}
if b.SyncAggregate == nil {
b.SyncAggregate = &ethpb.SyncAggregate{
SyncCommitteeBits: make([]byte, fieldparams.SyncAggregateSyncCommitteeBytesLength),
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength),
}
}
if b.ExecutionPayload == nil {
b.ExecutionPayload = &enginev1.ExecutionPayloadElectra{
ParentHash: make([]byte, fieldparams.RootLength),
FeeRecipient: make([]byte, 20),
StateRoot: make([]byte, fieldparams.RootLength),
ReceiptsRoot: make([]byte, fieldparams.RootLength),
LogsBloom: make([]byte, 256),
PrevRandao: make([]byte, fieldparams.RootLength),
ExtraData: make([]byte, 0),
BaseFeePerGas: make([]byte, fieldparams.RootLength),
BlockHash: make([]byte, fieldparams.RootLength),
Transactions: make([][]byte, 0),
Withdrawals: make([]*enginev1.Withdrawal, 0),
DepositReceipts: make([]*enginev1.DepositReceipt, 0),
WithdrawalRequests: make([]*enginev1.ExecutionLayerWithdrawalRequest, 0),
}
}
return b
}
5 changes: 5 additions & 0 deletions testing/util/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ func NewBlindedBeaconBlockDeneb() *ethpb.SignedBlindedBeaconBlockDeneb {
func NewBlindedBeaconBlockCapellaV2() *v2.SignedBlindedBeaconBlockCapella {
return HydrateV2SignedBlindedBeaconBlockCapella(&v2.SignedBlindedBeaconBlockCapella{})
}

// NewBeaconBlockElectra creates a beacon block with minimum marshalable fields.
func NewBeaconBlockElectra() *ethpb.SignedBeaconBlockElectra {
return HydrateSignedBeaconBlockElectra(&ethpb.SignedBeaconBlockElectra{})
}

0 comments on commit d9ee653

Please sign in to comment.