From 80e121a101229720e7c5ad9d2c02ca0a0fc40824 Mon Sep 17 00:00:00 2001 From: Manu NALEPA Date: Mon, 30 Dec 2024 15:44:57 +0100 Subject: [PATCH] Simplify next Fork boilerplate creation. --- api/server/structs/block.go | 299 +-- api/server/structs/conversions_block.go | 1745 +++++++++-------- api/server/structs/conversions_lightclient.go | 12 +- beacon-chain/blockchain/execution_engine.go | 10 +- beacon-chain/core/transition/transition.go | 16 +- beacon-chain/core/validators/slashing.go | 34 +- beacon-chain/execution/engine_client.go | 113 +- .../operations/attestations/kv/aggregated.go | 2 +- .../attestations/kv/unaggregated.go | 2 +- beacon-chain/rpc/eth/beacon/handlers.go | 16 +- .../state/state-native/spec_parameters.go | 24 +- proto/engine/v1/electra.go | 6 +- 12 files changed, 1182 insertions(+), 1097 deletions(-) diff --git a/api/server/structs/block.go b/api/server/structs/block.go index c53ce6a663ae..6500b2c08046 100644 --- a/api/server/structs/block.go +++ b/api/server/structs/block.go @@ -14,6 +14,10 @@ type SignedMessageJsoner interface { SigString() string } +// ---------------------------------------------------------------------------- +// Phase 0 +// ---------------------------------------------------------------------------- + type SignedBeaconBlock struct { Message *BeaconBlock `json:"message"` Signature string `json:"signature"` @@ -48,6 +52,29 @@ type BeaconBlockBody struct { VoluntaryExits []*SignedVoluntaryExit `json:"voluntary_exits"` } +type SignedBeaconBlockHeaderContainer struct { + Header *SignedBeaconBlockHeader `json:"header"` + Root string `json:"root"` + Canonical bool `json:"canonical"` +} + +type SignedBeaconBlockHeader struct { + Message *BeaconBlockHeader `json:"message"` + Signature string `json:"signature"` +} + +type BeaconBlockHeader struct { + Slot string `json:"slot"` + ProposerIndex string `json:"proposer_index"` + ParentRoot string `json:"parent_root"` + StateRoot string `json:"state_root"` + BodyRoot string `json:"body_root"` +} + +// ---------------------------------------------------------------------------- +// Altair +// ---------------------------------------------------------------------------- + type SignedBeaconBlockAltair struct { Message *BeaconBlockAltair `json:"message"` Signature string `json:"signature"` @@ -83,6 +110,10 @@ type BeaconBlockBodyAltair struct { SyncAggregate *SyncAggregate `json:"sync_aggregate"` } +// ---------------------------------------------------------------------------- +// Bellatrix +// ---------------------------------------------------------------------------- + type SignedBeaconBlockBellatrix struct { Message *BeaconBlockBellatrix `json:"message"` Signature string `json:"signature"` @@ -155,6 +186,44 @@ type BlindedBeaconBlockBodyBellatrix struct { ExecutionPayloadHeader *ExecutionPayloadHeader `json:"execution_payload_header"` } +type ExecutionPayload struct { + ParentHash string `json:"parent_hash"` + FeeRecipient string `json:"fee_recipient"` + StateRoot string `json:"state_root"` + ReceiptsRoot string `json:"receipts_root"` + LogsBloom string `json:"logs_bloom"` + PrevRandao string `json:"prev_randao"` + BlockNumber string `json:"block_number"` + GasLimit string `json:"gas_limit"` + GasUsed string `json:"gas_used"` + Timestamp string `json:"timestamp"` + ExtraData string `json:"extra_data"` + BaseFeePerGas string `json:"base_fee_per_gas"` + BlockHash string `json:"block_hash"` + Transactions []string `json:"transactions"` +} + +type ExecutionPayloadHeader struct { + ParentHash string `json:"parent_hash"` + FeeRecipient string `json:"fee_recipient"` + StateRoot string `json:"state_root"` + ReceiptsRoot string `json:"receipts_root"` + LogsBloom string `json:"logs_bloom"` + PrevRandao string `json:"prev_randao"` + BlockNumber string `json:"block_number"` + GasLimit string `json:"gas_limit"` + GasUsed string `json:"gas_used"` + Timestamp string `json:"timestamp"` + ExtraData string `json:"extra_data"` + BaseFeePerGas string `json:"base_fee_per_gas"` + BlockHash string `json:"block_hash"` + TransactionsRoot string `json:"transactions_root"` +} + +// ---------------------------------------------------------------------------- +// Capella +// ---------------------------------------------------------------------------- + type SignedBeaconBlockCapella struct { Message *BeaconBlockCapella `json:"message"` Signature string `json:"signature"` @@ -229,6 +298,46 @@ type BlindedBeaconBlockBodyCapella struct { BLSToExecutionChanges []*SignedBLSToExecutionChange `json:"bls_to_execution_changes"` } +type ExecutionPayloadCapella struct { + ParentHash string `json:"parent_hash"` + FeeRecipient string `json:"fee_recipient"` + StateRoot string `json:"state_root"` + ReceiptsRoot string `json:"receipts_root"` + LogsBloom string `json:"logs_bloom"` + PrevRandao string `json:"prev_randao"` + BlockNumber string `json:"block_number"` + GasLimit string `json:"gas_limit"` + GasUsed string `json:"gas_used"` + Timestamp string `json:"timestamp"` + ExtraData string `json:"extra_data"` + BaseFeePerGas string `json:"base_fee_per_gas"` + BlockHash string `json:"block_hash"` + Transactions []string `json:"transactions"` + Withdrawals []*Withdrawal `json:"withdrawals"` +} + +type ExecutionPayloadHeaderCapella struct { + ParentHash string `json:"parent_hash"` + FeeRecipient string `json:"fee_recipient"` + StateRoot string `json:"state_root"` + ReceiptsRoot string `json:"receipts_root"` + LogsBloom string `json:"logs_bloom"` + PrevRandao string `json:"prev_randao"` + BlockNumber string `json:"block_number"` + GasLimit string `json:"gas_limit"` + GasUsed string `json:"gas_used"` + Timestamp string `json:"timestamp"` + ExtraData string `json:"extra_data"` + BaseFeePerGas string `json:"base_fee_per_gas"` + BlockHash string `json:"block_hash"` + TransactionsRoot string `json:"transactions_root"` + WithdrawalsRoot string `json:"withdrawals_root"` +} + +// ---------------------------------------------------------------------------- +// Deneb +// ---------------------------------------------------------------------------- + type SignedBeaconBlockContentsDeneb struct { SignedBlock *SignedBeaconBlockDeneb `json:"signed_block"` KzgProofs []string `json:"kzg_proofs"` @@ -317,6 +426,50 @@ type BlindedBeaconBlockBodyDeneb struct { BlobKzgCommitments []string `json:"blob_kzg_commitments"` } +type ExecutionPayloadDeneb struct { + ParentHash string `json:"parent_hash"` + FeeRecipient string `json:"fee_recipient"` + StateRoot string `json:"state_root"` + ReceiptsRoot string `json:"receipts_root"` + LogsBloom string `json:"logs_bloom"` + PrevRandao string `json:"prev_randao"` + BlockNumber string `json:"block_number"` + GasLimit string `json:"gas_limit"` + GasUsed string `json:"gas_used"` + Timestamp string `json:"timestamp"` + ExtraData string `json:"extra_data"` + BaseFeePerGas string `json:"base_fee_per_gas"` + BlockHash string `json:"block_hash"` + Transactions []string `json:"transactions"` + Withdrawals []*Withdrawal `json:"withdrawals"` + BlobGasUsed string `json:"blob_gas_used"` + ExcessBlobGas string `json:"excess_blob_gas"` +} + +type ExecutionPayloadHeaderDeneb struct { + ParentHash string `json:"parent_hash"` + FeeRecipient string `json:"fee_recipient"` + StateRoot string `json:"state_root"` + ReceiptsRoot string `json:"receipts_root"` + LogsBloom string `json:"logs_bloom"` + PrevRandao string `json:"prev_randao"` + BlockNumber string `json:"block_number"` + GasLimit string `json:"gas_limit"` + GasUsed string `json:"gas_used"` + Timestamp string `json:"timestamp"` + ExtraData string `json:"extra_data"` + BaseFeePerGas string `json:"base_fee_per_gas"` + BlockHash string `json:"block_hash"` + TransactionsRoot string `json:"transactions_root"` + WithdrawalsRoot string `json:"withdrawals_root"` + BlobGasUsed string `json:"blob_gas_used"` + ExcessBlobGas string `json:"excess_blob_gas"` +} + +// ---------------------------------------------------------------------------- +// Electra +// ---------------------------------------------------------------------------- + type SignedBeaconBlockContentsElectra struct { SignedBlock *SignedBeaconBlockElectra `json:"signed_block"` KzgProofs []string `json:"kzg_proofs"` @@ -407,141 +560,13 @@ type BlindedBeaconBlockBodyElectra struct { ExecutionRequests *ExecutionRequests `json:"execution_requests"` } -type SignedBeaconBlockHeaderContainer struct { - Header *SignedBeaconBlockHeader `json:"header"` - Root string `json:"root"` - Canonical bool `json:"canonical"` -} - -type SignedBeaconBlockHeader struct { - Message *BeaconBlockHeader `json:"message"` - Signature string `json:"signature"` -} - -type BeaconBlockHeader struct { - Slot string `json:"slot"` - ProposerIndex string `json:"proposer_index"` - ParentRoot string `json:"parent_root"` - StateRoot string `json:"state_root"` - BodyRoot string `json:"body_root"` -} +type ( + ExecutionRequests struct { + Deposits []*DepositRequest `json:"deposits"` + Withdrawals []*WithdrawalRequest `json:"withdrawals"` + Consolidations []*ConsolidationRequest `json:"consolidations"` + } -type ExecutionPayload struct { - ParentHash string `json:"parent_hash"` - FeeRecipient string `json:"fee_recipient"` - StateRoot string `json:"state_root"` - ReceiptsRoot string `json:"receipts_root"` - LogsBloom string `json:"logs_bloom"` - PrevRandao string `json:"prev_randao"` - BlockNumber string `json:"block_number"` - GasLimit string `json:"gas_limit"` - GasUsed string `json:"gas_used"` - Timestamp string `json:"timestamp"` - ExtraData string `json:"extra_data"` - BaseFeePerGas string `json:"base_fee_per_gas"` - BlockHash string `json:"block_hash"` - Transactions []string `json:"transactions"` -} - -type ExecutionPayloadHeader struct { - ParentHash string `json:"parent_hash"` - FeeRecipient string `json:"fee_recipient"` - StateRoot string `json:"state_root"` - ReceiptsRoot string `json:"receipts_root"` - LogsBloom string `json:"logs_bloom"` - PrevRandao string `json:"prev_randao"` - BlockNumber string `json:"block_number"` - GasLimit string `json:"gas_limit"` - GasUsed string `json:"gas_used"` - Timestamp string `json:"timestamp"` - ExtraData string `json:"extra_data"` - BaseFeePerGas string `json:"base_fee_per_gas"` - BlockHash string `json:"block_hash"` - TransactionsRoot string `json:"transactions_root"` -} - -type ExecutionPayloadCapella struct { - ParentHash string `json:"parent_hash"` - FeeRecipient string `json:"fee_recipient"` - StateRoot string `json:"state_root"` - ReceiptsRoot string `json:"receipts_root"` - LogsBloom string `json:"logs_bloom"` - PrevRandao string `json:"prev_randao"` - BlockNumber string `json:"block_number"` - GasLimit string `json:"gas_limit"` - GasUsed string `json:"gas_used"` - Timestamp string `json:"timestamp"` - ExtraData string `json:"extra_data"` - BaseFeePerGas string `json:"base_fee_per_gas"` - BlockHash string `json:"block_hash"` - Transactions []string `json:"transactions"` - Withdrawals []*Withdrawal `json:"withdrawals"` -} - -type ExecutionPayloadHeaderCapella struct { - ParentHash string `json:"parent_hash"` - FeeRecipient string `json:"fee_recipient"` - StateRoot string `json:"state_root"` - ReceiptsRoot string `json:"receipts_root"` - LogsBloom string `json:"logs_bloom"` - PrevRandao string `json:"prev_randao"` - BlockNumber string `json:"block_number"` - GasLimit string `json:"gas_limit"` - GasUsed string `json:"gas_used"` - Timestamp string `json:"timestamp"` - ExtraData string `json:"extra_data"` - BaseFeePerGas string `json:"base_fee_per_gas"` - BlockHash string `json:"block_hash"` - TransactionsRoot string `json:"transactions_root"` - WithdrawalsRoot string `json:"withdrawals_root"` -} - -type ExecutionPayloadDeneb struct { - ParentHash string `json:"parent_hash"` - FeeRecipient string `json:"fee_recipient"` - StateRoot string `json:"state_root"` - ReceiptsRoot string `json:"receipts_root"` - LogsBloom string `json:"logs_bloom"` - PrevRandao string `json:"prev_randao"` - BlockNumber string `json:"block_number"` - GasLimit string `json:"gas_limit"` - GasUsed string `json:"gas_used"` - Timestamp string `json:"timestamp"` - ExtraData string `json:"extra_data"` - BaseFeePerGas string `json:"base_fee_per_gas"` - BlockHash string `json:"block_hash"` - Transactions []string `json:"transactions"` - Withdrawals []*Withdrawal `json:"withdrawals"` - BlobGasUsed string `json:"blob_gas_used"` - ExcessBlobGas string `json:"excess_blob_gas"` -} - -type ExecutionPayloadElectra = ExecutionPayloadDeneb - -type ExecutionPayloadHeaderDeneb struct { - ParentHash string `json:"parent_hash"` - FeeRecipient string `json:"fee_recipient"` - StateRoot string `json:"state_root"` - ReceiptsRoot string `json:"receipts_root"` - LogsBloom string `json:"logs_bloom"` - PrevRandao string `json:"prev_randao"` - BlockNumber string `json:"block_number"` - GasLimit string `json:"gas_limit"` - GasUsed string `json:"gas_used"` - Timestamp string `json:"timestamp"` - ExtraData string `json:"extra_data"` - BaseFeePerGas string `json:"base_fee_per_gas"` - BlockHash string `json:"block_hash"` - TransactionsRoot string `json:"transactions_root"` - WithdrawalsRoot string `json:"withdrawals_root"` - BlobGasUsed string `json:"blob_gas_used"` - ExcessBlobGas string `json:"excess_blob_gas"` -} - -type ExecutionPayloadHeaderElectra = ExecutionPayloadHeaderDeneb - -type ExecutionRequests struct { - Deposits []*DepositRequest `json:"deposits"` - Withdrawals []*WithdrawalRequest `json:"withdrawals"` - Consolidations []*ConsolidationRequest `json:"consolidations"` -} + ExecutionPayloadElectra = ExecutionPayloadDeneb + ExecutionPayloadHeaderElectra = ExecutionPayloadHeaderDeneb +) diff --git a/api/server/structs/conversions_block.go b/api/server/structs/conversions_block.go index de61ed3bda9a..190a54030f4f 100644 --- a/api/server/structs/conversions_block.go +++ b/api/server/structs/conversions_block.go @@ -19,6 +19,10 @@ import ( var ErrUnsupportedConversion = errors.New("Could not determine api struct type to use for value") +// ---------------------------------------------------------------------------- +// Phase 0 +// ---------------------------------------------------------------------------- + func (h *SignedBeaconBlockHeader) ToConsensus() (*eth.SignedBeaconBlockHeader, error) { if h == nil { return nil, errNilValue @@ -205,6 +209,129 @@ func (b *BeaconBlock) ToConsensus() (*eth.BeaconBlock, error) { }, nil } +func BeaconBlockHeaderFromConsensus(h *eth.BeaconBlockHeader) *BeaconBlockHeader { + return &BeaconBlockHeader{ + Slot: fmt.Sprintf("%d", h.Slot), + ProposerIndex: fmt.Sprintf("%d", h.ProposerIndex), + ParentRoot: hexutil.Encode(h.ParentRoot), + StateRoot: hexutil.Encode(h.StateRoot), + BodyRoot: hexutil.Encode(h.BodyRoot), + } +} + +func BeaconBlockFromConsensus(b *eth.BeaconBlock) *BeaconBlock { + return &BeaconBlock{ + Slot: fmt.Sprintf("%d", b.Slot), + ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), + ParentRoot: hexutil.Encode(b.ParentRoot), + StateRoot: hexutil.Encode(b.StateRoot), + Body: &BeaconBlockBody{ + RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), + Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), + Graffiti: hexutil.Encode(b.Body.Graffiti), + ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), + AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), + Attestations: AttsFromConsensus(b.Body.Attestations), + Deposits: DepositsFromConsensus(b.Body.Deposits), + VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), + }, + } +} + +func SignedBeaconBlockMessageJsoner(block interfaces.ReadOnlySignedBeaconBlock) (SignedMessageJsoner, error) { + pb, err := block.Proto() + if err != nil { + return nil, err + } + switch pbStruct := pb.(type) { + case *eth.SignedBeaconBlock: + return SignedBeaconBlockPhase0FromConsensus(pbStruct), nil + case *eth.SignedBeaconBlockAltair: + return SignedBeaconBlockAltairFromConsensus(pbStruct), nil + case *eth.SignedBlindedBeaconBlockBellatrix: + return SignedBlindedBeaconBlockBellatrixFromConsensus(pbStruct) + case *eth.SignedBeaconBlockBellatrix: + return SignedBeaconBlockBellatrixFromConsensus(pbStruct) + case *eth.SignedBlindedBeaconBlockCapella: + return SignedBlindedBeaconBlockCapellaFromConsensus(pbStruct) + case *eth.SignedBeaconBlockCapella: + return SignedBeaconBlockCapellaFromConsensus(pbStruct) + case *eth.SignedBlindedBeaconBlockDeneb: + return SignedBlindedBeaconBlockDenebFromConsensus(pbStruct) + case *eth.SignedBeaconBlockDeneb: + return SignedBeaconBlockDenebFromConsensus(pbStruct) + case *eth.SignedBlindedBeaconBlockElectra: + return SignedBlindedBeaconBlockElectraFromConsensus(pbStruct) + case *eth.SignedBeaconBlockElectra: + return SignedBeaconBlockElectraFromConsensus(pbStruct) + default: + return nil, ErrUnsupportedConversion + } +} + +func SignedBeaconBlockPhase0FromConsensus(b *eth.SignedBeaconBlock) *SignedBeaconBlock { + return &SignedBeaconBlock{ + Message: BeaconBlockFromConsensus(b.Block), + Signature: hexutil.Encode(b.Signature), + } +} + +func ExecutionPayloadFromConsensus(payload *enginev1.ExecutionPayload) (*ExecutionPayload, error) { + baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) + if err != nil { + return nil, err + } + transactions := make([]string, len(payload.Transactions)) + for i, tx := range payload.Transactions { + transactions[i] = hexutil.Encode(tx) + } + + return &ExecutionPayload{ + ParentHash: hexutil.Encode(payload.ParentHash), + FeeRecipient: hexutil.Encode(payload.FeeRecipient), + StateRoot: hexutil.Encode(payload.StateRoot), + ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), + LogsBloom: hexutil.Encode(payload.LogsBloom), + PrevRandao: hexutil.Encode(payload.PrevRandao), + BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), + GasLimit: fmt.Sprintf("%d", payload.GasLimit), + GasUsed: fmt.Sprintf("%d", payload.GasUsed), + Timestamp: fmt.Sprintf("%d", payload.Timestamp), + ExtraData: hexutil.Encode(payload.ExtraData), + BaseFeePerGas: baseFeePerGas, + BlockHash: hexutil.Encode(payload.BlockHash), + Transactions: transactions, + }, nil +} + +func ExecutionPayloadHeaderFromConsensus(payload *enginev1.ExecutionPayloadHeader) (*ExecutionPayloadHeader, error) { + baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) + if err != nil { + return nil, err + } + + return &ExecutionPayloadHeader{ + ParentHash: hexutil.Encode(payload.ParentHash), + FeeRecipient: hexutil.Encode(payload.FeeRecipient), + StateRoot: hexutil.Encode(payload.StateRoot), + ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), + LogsBloom: hexutil.Encode(payload.LogsBloom), + PrevRandao: hexutil.Encode(payload.PrevRandao), + BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), + GasLimit: fmt.Sprintf("%d", payload.GasLimit), + GasUsed: fmt.Sprintf("%d", payload.GasUsed), + Timestamp: fmt.Sprintf("%d", payload.Timestamp), + ExtraData: hexutil.Encode(payload.ExtraData), + BaseFeePerGas: baseFeePerGas, + BlockHash: hexutil.Encode(payload.BlockHash), + TransactionsRoot: hexutil.Encode(payload.TransactionsRoot), + }, nil +} + +// ---------------------------------------------------------------------------- +// Altair +// ---------------------------------------------------------------------------- + func (b *SignedBeaconBlockAltair) ToGeneric() (*eth.GenericSignedBeaconBlock, error) { if b == nil { return nil, errNilValue @@ -337,6 +464,40 @@ func (b *BeaconBlockAltair) ToConsensus() (*eth.BeaconBlockAltair, error) { }, nil } +func BeaconBlockAltairFromConsensus(b *eth.BeaconBlockAltair) *BeaconBlockAltair { + return &BeaconBlockAltair{ + Slot: fmt.Sprintf("%d", b.Slot), + ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), + ParentRoot: hexutil.Encode(b.ParentRoot), + StateRoot: hexutil.Encode(b.StateRoot), + Body: &BeaconBlockBodyAltair{ + RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), + Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), + Graffiti: hexutil.Encode(b.Body.Graffiti), + ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), + AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), + Attestations: AttsFromConsensus(b.Body.Attestations), + Deposits: DepositsFromConsensus(b.Body.Deposits), + VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), + SyncAggregate: &SyncAggregate{ + SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), + }, + }, + } +} + +func SignedBeaconBlockAltairFromConsensus(b *eth.SignedBeaconBlockAltair) *SignedBeaconBlockAltair { + return &SignedBeaconBlockAltair{ + Message: BeaconBlockAltairFromConsensus(b.Block), + Signature: hexutil.Encode(b.Signature), + } +} + +// ---------------------------------------------------------------------------- +// Bellatrix +// ---------------------------------------------------------------------------- + func (b *SignedBeaconBlockBellatrix) ToGeneric() (*eth.GenericSignedBeaconBlock, error) { if b == nil { return nil, errNilValue @@ -759,6 +920,90 @@ func (b *BlindedBeaconBlockBellatrix) ToConsensus() (*eth.BlindedBeaconBlockBell }, nil } +func BlindedBeaconBlockBellatrixFromConsensus(b *eth.BlindedBeaconBlockBellatrix) (*BlindedBeaconBlockBellatrix, error) { + payload, err := ExecutionPayloadHeaderFromConsensus(b.Body.ExecutionPayloadHeader) + if err != nil { + return nil, err + } + + return &BlindedBeaconBlockBellatrix{ + Slot: fmt.Sprintf("%d", b.Slot), + ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), + ParentRoot: hexutil.Encode(b.ParentRoot), + StateRoot: hexutil.Encode(b.StateRoot), + Body: &BlindedBeaconBlockBodyBellatrix{ + RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), + Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), + Graffiti: hexutil.Encode(b.Body.Graffiti), + ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), + AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), + Attestations: AttsFromConsensus(b.Body.Attestations), + Deposits: DepositsFromConsensus(b.Body.Deposits), + VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), + SyncAggregate: &SyncAggregate{ + SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), + }, + ExecutionPayloadHeader: payload, + }, + }, nil +} + +func SignedBlindedBeaconBlockBellatrixFromConsensus(b *eth.SignedBlindedBeaconBlockBellatrix) (*SignedBlindedBeaconBlockBellatrix, error) { + blindedBlock, err := BlindedBeaconBlockBellatrixFromConsensus(b.Block) + if err != nil { + return nil, err + } + return &SignedBlindedBeaconBlockBellatrix{ + Message: blindedBlock, + Signature: hexutil.Encode(b.Signature), + }, nil +} + +func BeaconBlockBellatrixFromConsensus(b *eth.BeaconBlockBellatrix) (*BeaconBlockBellatrix, error) { + payload, err := ExecutionPayloadFromConsensus(b.Body.ExecutionPayload) + if err != nil { + return nil, err + } + + return &BeaconBlockBellatrix{ + Slot: fmt.Sprintf("%d", b.Slot), + ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), + ParentRoot: hexutil.Encode(b.ParentRoot), + StateRoot: hexutil.Encode(b.StateRoot), + Body: &BeaconBlockBodyBellatrix{ + RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), + Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), + Graffiti: hexutil.Encode(b.Body.Graffiti), + ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), + AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), + Attestations: AttsFromConsensus(b.Body.Attestations), + Deposits: DepositsFromConsensus(b.Body.Deposits), + VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), + SyncAggregate: &SyncAggregate{ + SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), + }, + ExecutionPayload: payload, + }, + }, nil +} + +func SignedBeaconBlockBellatrixFromConsensus(b *eth.SignedBeaconBlockBellatrix) (*SignedBeaconBlockBellatrix, error) { + block, err := BeaconBlockBellatrixFromConsensus(b.Block) + if err != nil { + return nil, err + } + return &SignedBeaconBlockBellatrix{ + Message: block, + Signature: hexutil.Encode(b.Signature), + }, nil +} + +// ---------------------------------------------------------------------------- +// Capella +// ---------------------------------------------------------------------------- + func (b *SignedBeaconBlockCapella) ToGeneric() (*eth.GenericSignedBeaconBlock, error) { if b == nil { return nil, errNilValue @@ -1227,28 +1472,168 @@ func (b *BlindedBeaconBlockCapella) ToConsensus() (*eth.BlindedBeaconBlockCapell }, nil } -func (b *SignedBeaconBlockContentsDeneb) ToGeneric() (*eth.GenericSignedBeaconBlock, error) { - if b == nil { - return nil, errNilValue - } - - signedDenebBlock, err := b.SignedBlock.ToConsensus() +func BlindedBeaconBlockCapellaFromConsensus(b *eth.BlindedBeaconBlockCapella) (*BlindedBeaconBlockCapella, error) { + payload, err := ExecutionPayloadHeaderCapellaFromConsensus(b.Body.ExecutionPayloadHeader) if err != nil { - return nil, server.NewDecodeError(err, "SignedBlock") - } - proofs := make([][]byte, len(b.KzgProofs)) - for i, proof := range b.KzgProofs { - proofs[i], err = bytesutil.DecodeHexWithLength(proof, fieldparams.BLSPubkeyLength) - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("KzgProofs[%d]", i)) - } + return nil, err } - blbs := make([][]byte, len(b.Blobs)) - for i, blob := range b.Blobs { - blbs[i], err = bytesutil.DecodeHexWithLength(blob, fieldparams.BlobLength) - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("Blobs[%d]", i)) - } + + return &BlindedBeaconBlockCapella{ + Slot: fmt.Sprintf("%d", b.Slot), + ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), + ParentRoot: hexutil.Encode(b.ParentRoot), + StateRoot: hexutil.Encode(b.StateRoot), + Body: &BlindedBeaconBlockBodyCapella{ + RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), + Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), + Graffiti: hexutil.Encode(b.Body.Graffiti), + ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), + AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), + Attestations: AttsFromConsensus(b.Body.Attestations), + Deposits: DepositsFromConsensus(b.Body.Deposits), + VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), + SyncAggregate: &SyncAggregate{ + SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), + }, + ExecutionPayloadHeader: payload, + BLSToExecutionChanges: SignedBLSChangesFromConsensus(b.Body.BlsToExecutionChanges), + }, + }, nil +} + +func SignedBlindedBeaconBlockCapellaFromConsensus(b *eth.SignedBlindedBeaconBlockCapella) (*SignedBlindedBeaconBlockCapella, error) { + blindedBlock, err := BlindedBeaconBlockCapellaFromConsensus(b.Block) + if err != nil { + return nil, err + } + return &SignedBlindedBeaconBlockCapella{ + Message: blindedBlock, + Signature: hexutil.Encode(b.Signature), + }, nil +} + +func BeaconBlockCapellaFromConsensus(b *eth.BeaconBlockCapella) (*BeaconBlockCapella, error) { + payload, err := ExecutionPayloadCapellaFromConsensus(b.Body.ExecutionPayload) + if err != nil { + return nil, err + } + + return &BeaconBlockCapella{ + Slot: fmt.Sprintf("%d", b.Slot), + ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), + ParentRoot: hexutil.Encode(b.ParentRoot), + StateRoot: hexutil.Encode(b.StateRoot), + Body: &BeaconBlockBodyCapella{ + RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), + Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), + Graffiti: hexutil.Encode(b.Body.Graffiti), + ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), + AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), + Attestations: AttsFromConsensus(b.Body.Attestations), + Deposits: DepositsFromConsensus(b.Body.Deposits), + VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), + SyncAggregate: &SyncAggregate{ + SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), + }, + ExecutionPayload: payload, + BLSToExecutionChanges: SignedBLSChangesFromConsensus(b.Body.BlsToExecutionChanges), + }, + }, nil +} + +func SignedBeaconBlockCapellaFromConsensus(b *eth.SignedBeaconBlockCapella) (*SignedBeaconBlockCapella, error) { + block, err := BeaconBlockCapellaFromConsensus(b.Block) + if err != nil { + return nil, err + } + return &SignedBeaconBlockCapella{ + Message: block, + Signature: hexutil.Encode(b.Signature), + }, nil +} + +func ExecutionPayloadCapellaFromConsensus(payload *enginev1.ExecutionPayloadCapella) (*ExecutionPayloadCapella, error) { + baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) + if err != nil { + return nil, err + } + transactions := make([]string, len(payload.Transactions)) + for i, tx := range payload.Transactions { + transactions[i] = hexutil.Encode(tx) + } + + return &ExecutionPayloadCapella{ + ParentHash: hexutil.Encode(payload.ParentHash), + FeeRecipient: hexutil.Encode(payload.FeeRecipient), + StateRoot: hexutil.Encode(payload.StateRoot), + ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), + LogsBloom: hexutil.Encode(payload.LogsBloom), + PrevRandao: hexutil.Encode(payload.PrevRandao), + BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), + GasLimit: fmt.Sprintf("%d", payload.GasLimit), + GasUsed: fmt.Sprintf("%d", payload.GasUsed), + Timestamp: fmt.Sprintf("%d", payload.Timestamp), + ExtraData: hexutil.Encode(payload.ExtraData), + BaseFeePerGas: baseFeePerGas, + BlockHash: hexutil.Encode(payload.BlockHash), + Transactions: transactions, + Withdrawals: WithdrawalsFromConsensus(payload.Withdrawals), + }, nil +} + +func ExecutionPayloadHeaderCapellaFromConsensus(payload *enginev1.ExecutionPayloadHeaderCapella) (*ExecutionPayloadHeaderCapella, error) { + baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) + if err != nil { + return nil, err + } + + return &ExecutionPayloadHeaderCapella{ + ParentHash: hexutil.Encode(payload.ParentHash), + FeeRecipient: hexutil.Encode(payload.FeeRecipient), + StateRoot: hexutil.Encode(payload.StateRoot), + ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), + LogsBloom: hexutil.Encode(payload.LogsBloom), + PrevRandao: hexutil.Encode(payload.PrevRandao), + BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), + GasLimit: fmt.Sprintf("%d", payload.GasLimit), + GasUsed: fmt.Sprintf("%d", payload.GasUsed), + Timestamp: fmt.Sprintf("%d", payload.Timestamp), + ExtraData: hexutil.Encode(payload.ExtraData), + BaseFeePerGas: baseFeePerGas, + BlockHash: hexutil.Encode(payload.BlockHash), + TransactionsRoot: hexutil.Encode(payload.TransactionsRoot), + WithdrawalsRoot: hexutil.Encode(payload.WithdrawalsRoot), + }, nil +} + +// ---------------------------------------------------------------------------- +// Deneb +// ---------------------------------------------------------------------------- + +func (b *SignedBeaconBlockContentsDeneb) ToGeneric() (*eth.GenericSignedBeaconBlock, error) { + if b == nil { + return nil, errNilValue + } + + signedDenebBlock, err := b.SignedBlock.ToConsensus() + if err != nil { + return nil, server.NewDecodeError(err, "SignedBlock") + } + proofs := make([][]byte, len(b.KzgProofs)) + for i, proof := range b.KzgProofs { + proofs[i], err = bytesutil.DecodeHexWithLength(proof, fieldparams.BLSPubkeyLength) + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("KzgProofs[%d]", i)) + } + } + blbs := make([][]byte, len(b.Blobs)) + for i, blob := range b.Blobs { + blbs[i], err = bytesutil.DecodeHexWithLength(blob, fieldparams.BlobLength) + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("Blobs[%d]", i)) + } } blk := ð.SignedBeaconBlockContentsDeneb{ Block: signedDenebBlock, @@ -1833,184 +2218,403 @@ func (b *BlindedBeaconBlockDeneb) ToGeneric() (*eth.GenericBeaconBlock, error) { return ð.GenericBeaconBlock{Block: ð.GenericBeaconBlock_BlindedDeneb{BlindedDeneb: blindedBlock}, IsBlinded: true}, nil } -func (b *SignedBeaconBlockContentsElectra) ToGeneric() (*eth.GenericSignedBeaconBlock, error) { - if b == nil { - return nil, errNilValue - } - - signedElectraBlock, err := b.SignedBlock.ToConsensus() +func BeaconBlockContentsDenebFromConsensus(b *eth.BeaconBlockContentsDeneb) (*BeaconBlockContentsDeneb, error) { + block, err := BeaconBlockDenebFromConsensus(b.Block) if err != nil { - return nil, server.NewDecodeError(err, "SignedBlock") + return nil, err } - proofs := make([][]byte, len(b.KzgProofs)) + proofs := make([]string, len(b.KzgProofs)) for i, proof := range b.KzgProofs { - proofs[i], err = bytesutil.DecodeHexWithLength(proof, fieldparams.BLSPubkeyLength) - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("KzgProofs[%d]", i)) - } + proofs[i] = hexutil.Encode(proof) } - blbs := make([][]byte, len(b.Blobs)) + blbs := make([]string, len(b.Blobs)) for i, blob := range b.Blobs { - blbs[i], err = bytesutil.DecodeHexWithLength(blob, fieldparams.BlobLength) - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("Blobs[%d]", i)) - } + blbs[i] = hexutil.Encode(blob) } - blk := ð.SignedBeaconBlockContentsElectra{ - Block: signedElectraBlock, + return &BeaconBlockContentsDeneb{ + Block: block, KzgProofs: proofs, Blobs: blbs, - } - return ð.GenericSignedBeaconBlock{Block: ð.GenericSignedBeaconBlock_Electra{Electra: blk}}, nil -} - -func (b *SignedBeaconBlockContentsElectra) ToUnsigned() *BeaconBlockContentsElectra { - return &BeaconBlockContentsElectra{ - Block: b.SignedBlock.Message, - KzgProofs: b.KzgProofs, - Blobs: b.Blobs, - } + }, nil } -func (b *BeaconBlockContentsElectra) ToGeneric() (*eth.GenericBeaconBlock, error) { - block, err := b.ToConsensus() +func SignedBeaconBlockContentsDenebFromConsensus(b *eth.SignedBeaconBlockContentsDeneb) (*SignedBeaconBlockContentsDeneb, error) { + block, err := SignedBeaconBlockDenebFromConsensus(b.Block) if err != nil { return nil, err } - return ð.GenericBeaconBlock{Block: ð.GenericBeaconBlock_Electra{Electra: block}}, nil -} - -func (b *BeaconBlockContentsElectra) ToConsensus() (*eth.BeaconBlockContentsElectra, error) { - if b == nil { - return nil, errNilValue - } - - electraBlock, err := b.Block.ToConsensus() - if err != nil { - return nil, server.NewDecodeError(err, "Block") - } - proofs := make([][]byte, len(b.KzgProofs)) + proofs := make([]string, len(b.KzgProofs)) for i, proof := range b.KzgProofs { - proofs[i], err = bytesutil.DecodeHexWithLength(proof, fieldparams.BLSPubkeyLength) - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("KzgProofs[%d]", i)) - } + proofs[i] = hexutil.Encode(proof) } - blbs := make([][]byte, len(b.Blobs)) + + blbs := make([]string, len(b.Blobs)) for i, blob := range b.Blobs { - blbs[i], err = bytesutil.DecodeHexWithLength(blob, fieldparams.BlobLength) - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("Blobs[%d]", i)) - } + blbs[i] = hexutil.Encode(blob) } - return ð.BeaconBlockContentsElectra{ - Block: electraBlock, - KzgProofs: proofs, - Blobs: blbs, + + return &SignedBeaconBlockContentsDeneb{ + SignedBlock: block, + KzgProofs: proofs, + Blobs: blbs, }, nil } -func (b *BeaconBlockElectra) ToConsensus() (*eth.BeaconBlockElectra, error) { - if b == nil { - return nil, errNilValue - } - if b.Body == nil { - return nil, server.NewDecodeError(errNilValue, "Body") - } - if b.Body.Eth1Data == nil { - return nil, server.NewDecodeError(errNilValue, "Body.Eth1Data") - } - if b.Body.SyncAggregate == nil { - return nil, server.NewDecodeError(errNilValue, "Body.SyncAggregate") +func BlindedBeaconBlockDenebFromConsensus(b *eth.BlindedBeaconBlockDeneb) (*BlindedBeaconBlockDeneb, error) { + blobKzgCommitments := make([]string, len(b.Body.BlobKzgCommitments)) + for i := range b.Body.BlobKzgCommitments { + blobKzgCommitments[i] = hexutil.Encode(b.Body.BlobKzgCommitments[i]) } - if b.Body.ExecutionPayload == nil { - return nil, server.NewDecodeError(errNilValue, "Body.ExecutionPayload") + payload, err := ExecutionPayloadHeaderDenebFromConsensus(b.Body.ExecutionPayloadHeader) + if err != nil { + return nil, err } - slot, err := strconv.ParseUint(b.Slot, 10, 64) - if err != nil { - return nil, server.NewDecodeError(err, "Slot") - } - proposerIndex, err := strconv.ParseUint(b.ProposerIndex, 10, 64) - if err != nil { - return nil, server.NewDecodeError(err, "ProposerIndex") - } - parentRoot, err := bytesutil.DecodeHexWithLength(b.ParentRoot, fieldparams.RootLength) + return &BlindedBeaconBlockDeneb{ + Slot: fmt.Sprintf("%d", b.Slot), + ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), + ParentRoot: hexutil.Encode(b.ParentRoot), + StateRoot: hexutil.Encode(b.StateRoot), + Body: &BlindedBeaconBlockBodyDeneb{ + RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), + Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), + Graffiti: hexutil.Encode(b.Body.Graffiti), + ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), + AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), + Attestations: AttsFromConsensus(b.Body.Attestations), + Deposits: DepositsFromConsensus(b.Body.Deposits), + VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), + SyncAggregate: &SyncAggregate{ + SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), + }, + ExecutionPayloadHeader: payload, + BLSToExecutionChanges: SignedBLSChangesFromConsensus(b.Body.BlsToExecutionChanges), + BlobKzgCommitments: blobKzgCommitments, + }, + }, nil +} + +func SignedBlindedBeaconBlockDenebFromConsensus(b *eth.SignedBlindedBeaconBlockDeneb) (*SignedBlindedBeaconBlockDeneb, error) { + block, err := BlindedBeaconBlockDenebFromConsensus(b.Message) if err != nil { - return nil, server.NewDecodeError(err, "ParentRoot") + return nil, err } - stateRoot, err := bytesutil.DecodeHexWithLength(b.StateRoot, fieldparams.RootLength) + return &SignedBlindedBeaconBlockDeneb{ + Message: block, + Signature: hexutil.Encode(b.Signature), + }, nil +} + +func BeaconBlockDenebFromConsensus(b *eth.BeaconBlockDeneb) (*BeaconBlockDeneb, error) { + baseFeePerGas, err := sszBytesToUint256String(b.Body.ExecutionPayload.BaseFeePerGas) if err != nil { - return nil, server.NewDecodeError(err, "StateRoot") + return nil, err } - randaoReveal, err := bytesutil.DecodeHexWithLength(b.Body.RandaoReveal, fieldparams.BLSSignatureLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.RandaoReveal") + transactions := make([]string, len(b.Body.ExecutionPayload.Transactions)) + for i, tx := range b.Body.ExecutionPayload.Transactions { + transactions[i] = hexutil.Encode(tx) } - depositRoot, err := bytesutil.DecodeHexWithLength(b.Body.Eth1Data.DepositRoot, fieldparams.RootLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.Eth1Data.DepositRoot") + blobKzgCommitments := make([]string, len(b.Body.BlobKzgCommitments)) + for i := range b.Body.BlobKzgCommitments { + blobKzgCommitments[i] = hexutil.Encode(b.Body.BlobKzgCommitments[i]) } - depositCount, err := strconv.ParseUint(b.Body.Eth1Data.DepositCount, 10, 64) + + return &BeaconBlockDeneb{ + Slot: fmt.Sprintf("%d", b.Slot), + ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), + ParentRoot: hexutil.Encode(b.ParentRoot), + StateRoot: hexutil.Encode(b.StateRoot), + Body: &BeaconBlockBodyDeneb{ + RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), + Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), + Graffiti: hexutil.Encode(b.Body.Graffiti), + ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), + AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), + Attestations: AttsFromConsensus(b.Body.Attestations), + Deposits: DepositsFromConsensus(b.Body.Deposits), + VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), + SyncAggregate: &SyncAggregate{ + SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), + SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), + }, + ExecutionPayload: &ExecutionPayloadDeneb{ + ParentHash: hexutil.Encode(b.Body.ExecutionPayload.ParentHash), + FeeRecipient: hexutil.Encode(b.Body.ExecutionPayload.FeeRecipient), + StateRoot: hexutil.Encode(b.Body.ExecutionPayload.StateRoot), + ReceiptsRoot: hexutil.Encode(b.Body.ExecutionPayload.ReceiptsRoot), + LogsBloom: hexutil.Encode(b.Body.ExecutionPayload.LogsBloom), + PrevRandao: hexutil.Encode(b.Body.ExecutionPayload.PrevRandao), + BlockNumber: fmt.Sprintf("%d", b.Body.ExecutionPayload.BlockNumber), + GasLimit: fmt.Sprintf("%d", b.Body.ExecutionPayload.GasLimit), + GasUsed: fmt.Sprintf("%d", b.Body.ExecutionPayload.GasUsed), + Timestamp: fmt.Sprintf("%d", b.Body.ExecutionPayload.Timestamp), + ExtraData: hexutil.Encode(b.Body.ExecutionPayload.ExtraData), + BaseFeePerGas: baseFeePerGas, + BlockHash: hexutil.Encode(b.Body.ExecutionPayload.BlockHash), + Transactions: transactions, + Withdrawals: WithdrawalsFromConsensus(b.Body.ExecutionPayload.Withdrawals), + BlobGasUsed: fmt.Sprintf("%d", b.Body.ExecutionPayload.BlobGasUsed), + ExcessBlobGas: fmt.Sprintf("%d", b.Body.ExecutionPayload.ExcessBlobGas), + }, + BLSToExecutionChanges: SignedBLSChangesFromConsensus(b.Body.BlsToExecutionChanges), + BlobKzgCommitments: blobKzgCommitments, + }, + }, nil +} + +func SignedBeaconBlockDenebFromConsensus(b *eth.SignedBeaconBlockDeneb) (*SignedBeaconBlockDeneb, error) { + block, err := BeaconBlockDenebFromConsensus(b.Block) if err != nil { - return nil, server.NewDecodeError(err, "Body.Eth1Data.DepositCount") + return nil, err } - blockHash, err := bytesutil.DecodeHexWithLength(b.Body.Eth1Data.BlockHash, common.HashLength) + return &SignedBeaconBlockDeneb{ + Message: block, + Signature: hexutil.Encode(b.Signature), + }, nil +} + +func ExecutionPayloadDenebFromConsensus(payload *enginev1.ExecutionPayloadDeneb) (*ExecutionPayloadDeneb, error) { + baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) if err != nil { - return nil, server.NewDecodeError(err, "Body.Eth1Data.BlockHash") + return nil, err } - graffiti, err := bytesutil.DecodeHexWithLength(b.Body.Graffiti, fieldparams.RootLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.Graffiti") + transactions := make([]string, len(payload.Transactions)) + for i, tx := range payload.Transactions { + transactions[i] = hexutil.Encode(tx) } - proposerSlashings, err := ProposerSlashingsToConsensus(b.Body.ProposerSlashings) + + return &ExecutionPayloadDeneb{ + ParentHash: hexutil.Encode(payload.ParentHash), + FeeRecipient: hexutil.Encode(payload.FeeRecipient), + StateRoot: hexutil.Encode(payload.StateRoot), + ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), + LogsBloom: hexutil.Encode(payload.LogsBloom), + PrevRandao: hexutil.Encode(payload.PrevRandao), + BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), + GasLimit: fmt.Sprintf("%d", payload.GasLimit), + GasUsed: fmt.Sprintf("%d", payload.GasUsed), + Timestamp: fmt.Sprintf("%d", payload.Timestamp), + ExtraData: hexutil.Encode(payload.ExtraData), + BaseFeePerGas: baseFeePerGas, + BlockHash: hexutil.Encode(payload.BlockHash), + Transactions: transactions, + Withdrawals: WithdrawalsFromConsensus(payload.Withdrawals), + BlobGasUsed: fmt.Sprintf("%d", payload.BlobGasUsed), + ExcessBlobGas: fmt.Sprintf("%d", payload.ExcessBlobGas), + }, nil +} + +func ExecutionPayloadHeaderDenebFromConsensus(payload *enginev1.ExecutionPayloadHeaderDeneb) (*ExecutionPayloadHeaderDeneb, error) { + baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) if err != nil { - return nil, server.NewDecodeError(err, "Body.ProposerSlashings") + return nil, err } - attesterSlashings, err := AttesterSlashingsElectraToConsensus(b.Body.AttesterSlashings) - if err != nil { - return nil, server.NewDecodeError(err, "Body.AttesterSlashings") + + return &ExecutionPayloadHeaderDeneb{ + ParentHash: hexutil.Encode(payload.ParentHash), + FeeRecipient: hexutil.Encode(payload.FeeRecipient), + StateRoot: hexutil.Encode(payload.StateRoot), + ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), + LogsBloom: hexutil.Encode(payload.LogsBloom), + PrevRandao: hexutil.Encode(payload.PrevRandao), + BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), + GasLimit: fmt.Sprintf("%d", payload.GasLimit), + GasUsed: fmt.Sprintf("%d", payload.GasUsed), + Timestamp: fmt.Sprintf("%d", payload.Timestamp), + ExtraData: hexutil.Encode(payload.ExtraData), + BaseFeePerGas: baseFeePerGas, + BlockHash: hexutil.Encode(payload.BlockHash), + TransactionsRoot: hexutil.Encode(payload.TransactionsRoot), + WithdrawalsRoot: hexutil.Encode(payload.WithdrawalsRoot), + BlobGasUsed: fmt.Sprintf("%d", payload.BlobGasUsed), + ExcessBlobGas: fmt.Sprintf("%d", payload.ExcessBlobGas), + }, nil +} + +// ---------------------------------------------------------------------------- +// Electra +// ---------------------------------------------------------------------------- + +func (b *SignedBeaconBlockContentsElectra) ToGeneric() (*eth.GenericSignedBeaconBlock, error) { + if b == nil { + return nil, errNilValue } - atts, err := AttsElectraToConsensus(b.Body.Attestations) + + signedElectraBlock, err := b.SignedBlock.ToConsensus() if err != nil { - return nil, server.NewDecodeError(err, "Body.Attestations") + return nil, server.NewDecodeError(err, "SignedBlock") } - deposits, err := DepositsToConsensus(b.Body.Deposits) - if err != nil { - return nil, server.NewDecodeError(err, "Body.Deposits") + proofs := make([][]byte, len(b.KzgProofs)) + for i, proof := range b.KzgProofs { + proofs[i], err = bytesutil.DecodeHexWithLength(proof, fieldparams.BLSPubkeyLength) + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("KzgProofs[%d]", i)) + } } - exits, err := SignedExitsToConsensus(b.Body.VoluntaryExits) - if err != nil { - return nil, server.NewDecodeError(err, "Body.VoluntaryExits") + blbs := make([][]byte, len(b.Blobs)) + for i, blob := range b.Blobs { + blbs[i], err = bytesutil.DecodeHexWithLength(blob, fieldparams.BlobLength) + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("Blobs[%d]", i)) + } } - syncCommitteeBits, err := bytesutil.DecodeHexWithLength(b.Body.SyncAggregate.SyncCommitteeBits, fieldparams.SyncAggregateSyncCommitteeBytesLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.SyncAggregate.SyncCommitteeBits") + blk := ð.SignedBeaconBlockContentsElectra{ + Block: signedElectraBlock, + KzgProofs: proofs, + Blobs: blbs, } - syncCommitteeSig, err := bytesutil.DecodeHexWithLength(b.Body.SyncAggregate.SyncCommitteeSignature, fieldparams.BLSSignatureLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.SyncAggregate.SyncCommitteeSignature") + return ð.GenericSignedBeaconBlock{Block: ð.GenericSignedBeaconBlock_Electra{Electra: blk}}, nil +} + +func (b *SignedBeaconBlockContentsElectra) ToUnsigned() *BeaconBlockContentsElectra { + return &BeaconBlockContentsElectra{ + Block: b.SignedBlock.Message, + KzgProofs: b.KzgProofs, + Blobs: b.Blobs, } - payloadParentHash, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.ParentHash, common.HashLength) +} + +func (b *BeaconBlockContentsElectra) ToGeneric() (*eth.GenericBeaconBlock, error) { + block, err := b.ToConsensus() if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayload.ParentHash") + return nil, err } - payloadFeeRecipient, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.FeeRecipient, fieldparams.FeeRecipientLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayload.FeeRecipient") + + return ð.GenericBeaconBlock{Block: ð.GenericBeaconBlock_Electra{Electra: block}}, nil +} + +func (b *BeaconBlockContentsElectra) ToConsensus() (*eth.BeaconBlockContentsElectra, error) { + if b == nil { + return nil, errNilValue } - payloadStateRoot, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.StateRoot, fieldparams.RootLength) + + electraBlock, err := b.Block.ToConsensus() if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayload.StateRoot") + return nil, server.NewDecodeError(err, "Block") } - payloadReceiptsRoot, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.ReceiptsRoot, fieldparams.RootLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayload.ReceiptsRoot") + proofs := make([][]byte, len(b.KzgProofs)) + for i, proof := range b.KzgProofs { + proofs[i], err = bytesutil.DecodeHexWithLength(proof, fieldparams.BLSPubkeyLength) + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("KzgProofs[%d]", i)) + } } - payloadLogsBloom, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.LogsBloom, fieldparams.LogsBloomLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayload.LogsBloom") + blbs := make([][]byte, len(b.Blobs)) + for i, blob := range b.Blobs { + blbs[i], err = bytesutil.DecodeHexWithLength(blob, fieldparams.BlobLength) + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("Blobs[%d]", i)) + } + } + return ð.BeaconBlockContentsElectra{ + Block: electraBlock, + KzgProofs: proofs, + Blobs: blbs, + }, nil +} + +func (b *BeaconBlockElectra) ToConsensus() (*eth.BeaconBlockElectra, error) { + if b == nil { + return nil, errNilValue + } + if b.Body == nil { + return nil, server.NewDecodeError(errNilValue, "Body") + } + if b.Body.Eth1Data == nil { + return nil, server.NewDecodeError(errNilValue, "Body.Eth1Data") + } + if b.Body.SyncAggregate == nil { + return nil, server.NewDecodeError(errNilValue, "Body.SyncAggregate") + } + if b.Body.ExecutionPayload == nil { + return nil, server.NewDecodeError(errNilValue, "Body.ExecutionPayload") + } + + slot, err := strconv.ParseUint(b.Slot, 10, 64) + if err != nil { + return nil, server.NewDecodeError(err, "Slot") + } + proposerIndex, err := strconv.ParseUint(b.ProposerIndex, 10, 64) + if err != nil { + return nil, server.NewDecodeError(err, "ProposerIndex") + } + parentRoot, err := bytesutil.DecodeHexWithLength(b.ParentRoot, fieldparams.RootLength) + if err != nil { + return nil, server.NewDecodeError(err, "ParentRoot") + } + stateRoot, err := bytesutil.DecodeHexWithLength(b.StateRoot, fieldparams.RootLength) + if err != nil { + return nil, server.NewDecodeError(err, "StateRoot") + } + randaoReveal, err := bytesutil.DecodeHexWithLength(b.Body.RandaoReveal, fieldparams.BLSSignatureLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.RandaoReveal") + } + depositRoot, err := bytesutil.DecodeHexWithLength(b.Body.Eth1Data.DepositRoot, fieldparams.RootLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.Eth1Data.DepositRoot") + } + depositCount, err := strconv.ParseUint(b.Body.Eth1Data.DepositCount, 10, 64) + if err != nil { + return nil, server.NewDecodeError(err, "Body.Eth1Data.DepositCount") + } + blockHash, err := bytesutil.DecodeHexWithLength(b.Body.Eth1Data.BlockHash, common.HashLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.Eth1Data.BlockHash") + } + graffiti, err := bytesutil.DecodeHexWithLength(b.Body.Graffiti, fieldparams.RootLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.Graffiti") + } + proposerSlashings, err := ProposerSlashingsToConsensus(b.Body.ProposerSlashings) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ProposerSlashings") + } + attesterSlashings, err := AttesterSlashingsElectraToConsensus(b.Body.AttesterSlashings) + if err != nil { + return nil, server.NewDecodeError(err, "Body.AttesterSlashings") + } + atts, err := AttsElectraToConsensus(b.Body.Attestations) + if err != nil { + return nil, server.NewDecodeError(err, "Body.Attestations") + } + deposits, err := DepositsToConsensus(b.Body.Deposits) + if err != nil { + return nil, server.NewDecodeError(err, "Body.Deposits") + } + exits, err := SignedExitsToConsensus(b.Body.VoluntaryExits) + if err != nil { + return nil, server.NewDecodeError(err, "Body.VoluntaryExits") + } + syncCommitteeBits, err := bytesutil.DecodeHexWithLength(b.Body.SyncAggregate.SyncCommitteeBits, fieldparams.SyncAggregateSyncCommitteeBytesLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.SyncAggregate.SyncCommitteeBits") + } + syncCommitteeSig, err := bytesutil.DecodeHexWithLength(b.Body.SyncAggregate.SyncCommitteeSignature, fieldparams.BLSSignatureLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.SyncAggregate.SyncCommitteeSignature") + } + payloadParentHash, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.ParentHash, common.HashLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayload.ParentHash") + } + payloadFeeRecipient, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.FeeRecipient, fieldparams.FeeRecipientLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayload.FeeRecipient") + } + payloadStateRoot, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.StateRoot, fieldparams.RootLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayload.StateRoot") + } + payloadReceiptsRoot, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.ReceiptsRoot, fieldparams.RootLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayload.ReceiptsRoot") + } + payloadLogsBloom, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.LogsBloom, fieldparams.LogsBloomLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayload.LogsBloom") } payloadPrevRandao, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayload.PrevRandao, fieldparams.RootLength) if err != nil { @@ -2354,589 +2958,153 @@ func (b *BlindedBeaconBlockElectra) ToConsensus() (*eth.BlindedBeaconBlockElectr payloadBlockNumber, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.BlockNumber, 10, 64) if err != nil { return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.BlockNumber") - } - payloadGasLimit, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.GasLimit, 10, 64) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.GasLimit") - } - payloadGasUsed, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.GasUsed, 10, 64) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.GasUsed") - } - payloadTimestamp, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.Timestamp, 10, 64) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.Timestamp") - } - payloadExtraData, err := bytesutil.DecodeHexWithMaxLength(b.Body.ExecutionPayloadHeader.ExtraData, fieldparams.RootLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.ExtraData") - } - payloadBaseFeePerGas, err := bytesutil.Uint256ToSSZBytes(b.Body.ExecutionPayloadHeader.BaseFeePerGas) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.BaseFeePerGas") - } - payloadBlockHash, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayloadHeader.BlockHash, common.HashLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.BlockHash") - } - payloadTxsRoot, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayloadHeader.TransactionsRoot, fieldparams.RootLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.TransactionsRoot") - } - payloadWithdrawalsRoot, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayloadHeader.WithdrawalsRoot, fieldparams.RootLength) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.WithdrawalsRoot") - } - payloadBlobGasUsed, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.BlobGasUsed, 10, 64) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayload.BlobGasUsed") - } - payloadExcessBlobGas, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.ExcessBlobGas, 10, 64) - if err != nil { - return nil, server.NewDecodeError(err, "Body.ExecutionPayload.ExcessBlobGas") - } - if b.Body.ExecutionRequests == nil { - return nil, server.NewDecodeError(errors.New("nil execution requests"), "Body.ExecutionRequests") - } - depositRequests := make([]*enginev1.DepositRequest, len(b.Body.ExecutionRequests.Deposits)) - for i, d := range b.Body.ExecutionRequests.Deposits { - depositRequests[i], err = d.ToConsensus() - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("Body.ExecutionRequests.Deposits[%d]", i)) - } - } - - withdrawalRequests := make([]*enginev1.WithdrawalRequest, len(b.Body.ExecutionRequests.Withdrawals)) - for i, w := range b.Body.ExecutionRequests.Withdrawals { - withdrawalRequests[i], err = w.ToConsensus() - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("Body.ExecutionRequests.Withdrawals[%d]", i)) - } - } - - consolidationRequests := make([]*enginev1.ConsolidationRequest, len(b.Body.ExecutionRequests.Consolidations)) - for i, c := range b.Body.ExecutionRequests.Consolidations { - consolidationRequests[i], err = c.ToConsensus() - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("Body.ExecutionRequests.Consolidations[%d]", i)) - } - } - - blsChanges, err := SignedBLSChangesToConsensus(b.Body.BLSToExecutionChanges) - if err != nil { - return nil, server.NewDecodeError(err, "Body.BLSToExecutionChanges") - } - err = slice.VerifyMaxLength(b.Body.BlobKzgCommitments, fieldparams.MaxBlobCommitmentsPerBlock) - if err != nil { - return nil, server.NewDecodeError(err, "Body.BlobKzgCommitments") - } - blobKzgCommitments := make([][]byte, len(b.Body.BlobKzgCommitments)) - for i, b := range b.Body.BlobKzgCommitments { - kzg, err := bytesutil.DecodeHexWithLength(b, fieldparams.BLSPubkeyLength) - if err != nil { - return nil, server.NewDecodeError(err, fmt.Sprintf("Body.BlobKzgCommitments[%d]", i)) - } - blobKzgCommitments[i] = kzg - } - - return ð.BlindedBeaconBlockElectra{ - Slot: primitives.Slot(slot), - ProposerIndex: primitives.ValidatorIndex(proposerIndex), - ParentRoot: parentRoot, - StateRoot: stateRoot, - Body: ð.BlindedBeaconBlockBodyElectra{ - RandaoReveal: randaoReveal, - Eth1Data: ð.Eth1Data{ - DepositRoot: depositRoot, - DepositCount: depositCount, - BlockHash: blockHash, - }, - Graffiti: graffiti, - ProposerSlashings: proposerSlashings, - AttesterSlashings: attesterSlashings, - Attestations: atts, - Deposits: deposits, - VoluntaryExits: exits, - SyncAggregate: ð.SyncAggregate{ - SyncCommitteeBits: syncCommitteeBits, - SyncCommitteeSignature: syncCommitteeSig, - }, - ExecutionPayloadHeader: &enginev1.ExecutionPayloadHeaderElectra{ - ParentHash: payloadParentHash, - FeeRecipient: payloadFeeRecipient, - StateRoot: payloadStateRoot, - ReceiptsRoot: payloadReceiptsRoot, - LogsBloom: payloadLogsBloom, - PrevRandao: payloadPrevRandao, - BlockNumber: payloadBlockNumber, - GasLimit: payloadGasLimit, - GasUsed: payloadGasUsed, - Timestamp: payloadTimestamp, - ExtraData: payloadExtraData, - BaseFeePerGas: payloadBaseFeePerGas, - BlockHash: payloadBlockHash, - TransactionsRoot: payloadTxsRoot, - WithdrawalsRoot: payloadWithdrawalsRoot, - BlobGasUsed: payloadBlobGasUsed, - ExcessBlobGas: payloadExcessBlobGas, - }, - BlsToExecutionChanges: blsChanges, - BlobKzgCommitments: blobKzgCommitments, - ExecutionRequests: &enginev1.ExecutionRequests{ - Deposits: depositRequests, - Withdrawals: withdrawalRequests, - Consolidations: consolidationRequests, - }, - }, - }, nil -} - -func (b *BlindedBeaconBlockElectra) ToGeneric() (*eth.GenericBeaconBlock, error) { - if b == nil { - return nil, errNilValue - } - - blindedBlock, err := b.ToConsensus() - if err != nil { - return nil, err - } - return ð.GenericBeaconBlock{Block: ð.GenericBeaconBlock_BlindedElectra{BlindedElectra: blindedBlock}, IsBlinded: true}, nil -} - -func BeaconBlockHeaderFromConsensus(h *eth.BeaconBlockHeader) *BeaconBlockHeader { - return &BeaconBlockHeader{ - Slot: fmt.Sprintf("%d", h.Slot), - ProposerIndex: fmt.Sprintf("%d", h.ProposerIndex), - ParentRoot: hexutil.Encode(h.ParentRoot), - StateRoot: hexutil.Encode(h.StateRoot), - BodyRoot: hexutil.Encode(h.BodyRoot), - } -} - -func BeaconBlockFromConsensus(b *eth.BeaconBlock) *BeaconBlock { - return &BeaconBlock{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: hexutil.Encode(b.ParentRoot), - StateRoot: hexutil.Encode(b.StateRoot), - Body: &BeaconBlockBody{ - RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), - Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), - Graffiti: hexutil.Encode(b.Body.Graffiti), - ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), - AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), - Attestations: AttsFromConsensus(b.Body.Attestations), - Deposits: DepositsFromConsensus(b.Body.Deposits), - VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), - }, - } -} - -func SignedBeaconBlockMessageJsoner(block interfaces.ReadOnlySignedBeaconBlock) (SignedMessageJsoner, error) { - pb, err := block.Proto() - if err != nil { - return nil, err - } - switch pbStruct := pb.(type) { - case *eth.SignedBeaconBlock: - return SignedBeaconBlockPhase0FromConsensus(pbStruct), nil - case *eth.SignedBeaconBlockAltair: - return SignedBeaconBlockAltairFromConsensus(pbStruct), nil - case *eth.SignedBlindedBeaconBlockBellatrix: - return SignedBlindedBeaconBlockBellatrixFromConsensus(pbStruct) - case *eth.SignedBeaconBlockBellatrix: - return SignedBeaconBlockBellatrixFromConsensus(pbStruct) - case *eth.SignedBlindedBeaconBlockCapella: - return SignedBlindedBeaconBlockCapellaFromConsensus(pbStruct) - case *eth.SignedBeaconBlockCapella: - return SignedBeaconBlockCapellaFromConsensus(pbStruct) - case *eth.SignedBlindedBeaconBlockDeneb: - return SignedBlindedBeaconBlockDenebFromConsensus(pbStruct) - case *eth.SignedBeaconBlockDeneb: - return SignedBeaconBlockDenebFromConsensus(pbStruct) - case *eth.SignedBlindedBeaconBlockElectra: - return SignedBlindedBeaconBlockElectraFromConsensus(pbStruct) - case *eth.SignedBeaconBlockElectra: - return SignedBeaconBlockElectraFromConsensus(pbStruct) - default: - return nil, ErrUnsupportedConversion - } -} - -func SignedBeaconBlockPhase0FromConsensus(b *eth.SignedBeaconBlock) *SignedBeaconBlock { - return &SignedBeaconBlock{ - Message: BeaconBlockFromConsensus(b.Block), - Signature: hexutil.Encode(b.Signature), - } -} - -func BeaconBlockAltairFromConsensus(b *eth.BeaconBlockAltair) *BeaconBlockAltair { - return &BeaconBlockAltair{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: hexutil.Encode(b.ParentRoot), - StateRoot: hexutil.Encode(b.StateRoot), - Body: &BeaconBlockBodyAltair{ - RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), - Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), - Graffiti: hexutil.Encode(b.Body.Graffiti), - ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), - AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), - Attestations: AttsFromConsensus(b.Body.Attestations), - Deposits: DepositsFromConsensus(b.Body.Deposits), - VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), - SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), - SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), - }, - }, - } -} - -func SignedBeaconBlockAltairFromConsensus(b *eth.SignedBeaconBlockAltair) *SignedBeaconBlockAltair { - return &SignedBeaconBlockAltair{ - Message: BeaconBlockAltairFromConsensus(b.Block), - Signature: hexutil.Encode(b.Signature), - } -} - -func BlindedBeaconBlockBellatrixFromConsensus(b *eth.BlindedBeaconBlockBellatrix) (*BlindedBeaconBlockBellatrix, error) { - payload, err := ExecutionPayloadHeaderFromConsensus(b.Body.ExecutionPayloadHeader) - if err != nil { - return nil, err - } - - return &BlindedBeaconBlockBellatrix{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: hexutil.Encode(b.ParentRoot), - StateRoot: hexutil.Encode(b.StateRoot), - Body: &BlindedBeaconBlockBodyBellatrix{ - RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), - Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), - Graffiti: hexutil.Encode(b.Body.Graffiti), - ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), - AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), - Attestations: AttsFromConsensus(b.Body.Attestations), - Deposits: DepositsFromConsensus(b.Body.Deposits), - VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), - SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), - SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), - }, - ExecutionPayloadHeader: payload, - }, - }, nil -} - -func SignedBlindedBeaconBlockBellatrixFromConsensus(b *eth.SignedBlindedBeaconBlockBellatrix) (*SignedBlindedBeaconBlockBellatrix, error) { - blindedBlock, err := BlindedBeaconBlockBellatrixFromConsensus(b.Block) - if err != nil { - return nil, err - } - return &SignedBlindedBeaconBlockBellatrix{ - Message: blindedBlock, - Signature: hexutil.Encode(b.Signature), - }, nil -} - -func BeaconBlockBellatrixFromConsensus(b *eth.BeaconBlockBellatrix) (*BeaconBlockBellatrix, error) { - payload, err := ExecutionPayloadFromConsensus(b.Body.ExecutionPayload) - if err != nil { - return nil, err - } - - return &BeaconBlockBellatrix{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: hexutil.Encode(b.ParentRoot), - StateRoot: hexutil.Encode(b.StateRoot), - Body: &BeaconBlockBodyBellatrix{ - RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), - Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), - Graffiti: hexutil.Encode(b.Body.Graffiti), - ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), - AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), - Attestations: AttsFromConsensus(b.Body.Attestations), - Deposits: DepositsFromConsensus(b.Body.Deposits), - VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), - SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), - SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), - }, - ExecutionPayload: payload, - }, - }, nil -} - -func SignedBeaconBlockBellatrixFromConsensus(b *eth.SignedBeaconBlockBellatrix) (*SignedBeaconBlockBellatrix, error) { - block, err := BeaconBlockBellatrixFromConsensus(b.Block) - if err != nil { - return nil, err - } - return &SignedBeaconBlockBellatrix{ - Message: block, - Signature: hexutil.Encode(b.Signature), - }, nil -} - -func BlindedBeaconBlockCapellaFromConsensus(b *eth.BlindedBeaconBlockCapella) (*BlindedBeaconBlockCapella, error) { - payload, err := ExecutionPayloadHeaderCapellaFromConsensus(b.Body.ExecutionPayloadHeader) + } + payloadGasLimit, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.GasLimit, 10, 64) if err != nil { - return nil, err + return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.GasLimit") } - - return &BlindedBeaconBlockCapella{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: hexutil.Encode(b.ParentRoot), - StateRoot: hexutil.Encode(b.StateRoot), - Body: &BlindedBeaconBlockBodyCapella{ - RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), - Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), - Graffiti: hexutil.Encode(b.Body.Graffiti), - ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), - AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), - Attestations: AttsFromConsensus(b.Body.Attestations), - Deposits: DepositsFromConsensus(b.Body.Deposits), - VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), - SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), - SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), - }, - ExecutionPayloadHeader: payload, - BLSToExecutionChanges: SignedBLSChangesFromConsensus(b.Body.BlsToExecutionChanges), - }, - }, nil -} - -func SignedBlindedBeaconBlockCapellaFromConsensus(b *eth.SignedBlindedBeaconBlockCapella) (*SignedBlindedBeaconBlockCapella, error) { - blindedBlock, err := BlindedBeaconBlockCapellaFromConsensus(b.Block) + payloadGasUsed, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.GasUsed, 10, 64) if err != nil { - return nil, err + return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.GasUsed") } - return &SignedBlindedBeaconBlockCapella{ - Message: blindedBlock, - Signature: hexutil.Encode(b.Signature), - }, nil -} - -func BeaconBlockCapellaFromConsensus(b *eth.BeaconBlockCapella) (*BeaconBlockCapella, error) { - payload, err := ExecutionPayloadCapellaFromConsensus(b.Body.ExecutionPayload) + payloadTimestamp, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.Timestamp, 10, 64) if err != nil { - return nil, err + return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.Timestamp") } - - return &BeaconBlockCapella{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: hexutil.Encode(b.ParentRoot), - StateRoot: hexutil.Encode(b.StateRoot), - Body: &BeaconBlockBodyCapella{ - RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), - Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), - Graffiti: hexutil.Encode(b.Body.Graffiti), - ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), - AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), - Attestations: AttsFromConsensus(b.Body.Attestations), - Deposits: DepositsFromConsensus(b.Body.Deposits), - VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), - SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), - SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), - }, - ExecutionPayload: payload, - BLSToExecutionChanges: SignedBLSChangesFromConsensus(b.Body.BlsToExecutionChanges), - }, - }, nil -} - -func SignedBeaconBlockCapellaFromConsensus(b *eth.SignedBeaconBlockCapella) (*SignedBeaconBlockCapella, error) { - block, err := BeaconBlockCapellaFromConsensus(b.Block) + payloadExtraData, err := bytesutil.DecodeHexWithMaxLength(b.Body.ExecutionPayloadHeader.ExtraData, fieldparams.RootLength) if err != nil { - return nil, err + return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.ExtraData") } - return &SignedBeaconBlockCapella{ - Message: block, - Signature: hexutil.Encode(b.Signature), - }, nil -} - -func BeaconBlockContentsDenebFromConsensus(b *eth.BeaconBlockContentsDeneb) (*BeaconBlockContentsDeneb, error) { - block, err := BeaconBlockDenebFromConsensus(b.Block) + payloadBaseFeePerGas, err := bytesutil.Uint256ToSSZBytes(b.Body.ExecutionPayloadHeader.BaseFeePerGas) if err != nil { - return nil, err + return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.BaseFeePerGas") } - proofs := make([]string, len(b.KzgProofs)) - for i, proof := range b.KzgProofs { - proofs[i] = hexutil.Encode(proof) + payloadBlockHash, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayloadHeader.BlockHash, common.HashLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.BlockHash") } - blbs := make([]string, len(b.Blobs)) - for i, blob := range b.Blobs { - blbs[i] = hexutil.Encode(blob) + payloadTxsRoot, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayloadHeader.TransactionsRoot, fieldparams.RootLength) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.TransactionsRoot") } - return &BeaconBlockContentsDeneb{ - Block: block, - KzgProofs: proofs, - Blobs: blbs, - }, nil -} - -func SignedBeaconBlockContentsDenebFromConsensus(b *eth.SignedBeaconBlockContentsDeneb) (*SignedBeaconBlockContentsDeneb, error) { - block, err := SignedBeaconBlockDenebFromConsensus(b.Block) + payloadWithdrawalsRoot, err := bytesutil.DecodeHexWithLength(b.Body.ExecutionPayloadHeader.WithdrawalsRoot, fieldparams.RootLength) if err != nil { - return nil, err + return nil, server.NewDecodeError(err, "Body.ExecutionPayloadHeader.WithdrawalsRoot") } - - proofs := make([]string, len(b.KzgProofs)) - for i, proof := range b.KzgProofs { - proofs[i] = hexutil.Encode(proof) + payloadBlobGasUsed, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.BlobGasUsed, 10, 64) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayload.BlobGasUsed") } - - blbs := make([]string, len(b.Blobs)) - for i, blob := range b.Blobs { - blbs[i] = hexutil.Encode(blob) + payloadExcessBlobGas, err := strconv.ParseUint(b.Body.ExecutionPayloadHeader.ExcessBlobGas, 10, 64) + if err != nil { + return nil, server.NewDecodeError(err, "Body.ExecutionPayload.ExcessBlobGas") } - - return &SignedBeaconBlockContentsDeneb{ - SignedBlock: block, - KzgProofs: proofs, - Blobs: blbs, - }, nil -} - -func BlindedBeaconBlockDenebFromConsensus(b *eth.BlindedBeaconBlockDeneb) (*BlindedBeaconBlockDeneb, error) { - blobKzgCommitments := make([]string, len(b.Body.BlobKzgCommitments)) - for i := range b.Body.BlobKzgCommitments { - blobKzgCommitments[i] = hexutil.Encode(b.Body.BlobKzgCommitments[i]) + if b.Body.ExecutionRequests == nil { + return nil, server.NewDecodeError(errors.New("nil execution requests"), "Body.ExecutionRequests") } - payload, err := ExecutionPayloadHeaderDenebFromConsensus(b.Body.ExecutionPayloadHeader) - if err != nil { - return nil, err + depositRequests := make([]*enginev1.DepositRequest, len(b.Body.ExecutionRequests.Deposits)) + for i, d := range b.Body.ExecutionRequests.Deposits { + depositRequests[i], err = d.ToConsensus() + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("Body.ExecutionRequests.Deposits[%d]", i)) + } } - return &BlindedBeaconBlockDeneb{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: hexutil.Encode(b.ParentRoot), - StateRoot: hexutil.Encode(b.StateRoot), - Body: &BlindedBeaconBlockBodyDeneb{ - RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), - Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), - Graffiti: hexutil.Encode(b.Body.Graffiti), - ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), - AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), - Attestations: AttsFromConsensus(b.Body.Attestations), - Deposits: DepositsFromConsensus(b.Body.Deposits), - VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), - SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), - SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), - }, - ExecutionPayloadHeader: payload, - BLSToExecutionChanges: SignedBLSChangesFromConsensus(b.Body.BlsToExecutionChanges), - BlobKzgCommitments: blobKzgCommitments, - }, - }, nil -} + withdrawalRequests := make([]*enginev1.WithdrawalRequest, len(b.Body.ExecutionRequests.Withdrawals)) + for i, w := range b.Body.ExecutionRequests.Withdrawals { + withdrawalRequests[i], err = w.ToConsensus() + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("Body.ExecutionRequests.Withdrawals[%d]", i)) + } + } -func SignedBlindedBeaconBlockDenebFromConsensus(b *eth.SignedBlindedBeaconBlockDeneb) (*SignedBlindedBeaconBlockDeneb, error) { - block, err := BlindedBeaconBlockDenebFromConsensus(b.Message) - if err != nil { - return nil, err + consolidationRequests := make([]*enginev1.ConsolidationRequest, len(b.Body.ExecutionRequests.Consolidations)) + for i, c := range b.Body.ExecutionRequests.Consolidations { + consolidationRequests[i], err = c.ToConsensus() + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("Body.ExecutionRequests.Consolidations[%d]", i)) + } } - return &SignedBlindedBeaconBlockDeneb{ - Message: block, - Signature: hexutil.Encode(b.Signature), - }, nil -} -func BeaconBlockDenebFromConsensus(b *eth.BeaconBlockDeneb) (*BeaconBlockDeneb, error) { - baseFeePerGas, err := sszBytesToUint256String(b.Body.ExecutionPayload.BaseFeePerGas) + blsChanges, err := SignedBLSChangesToConsensus(b.Body.BLSToExecutionChanges) if err != nil { - return nil, err + return nil, server.NewDecodeError(err, "Body.BLSToExecutionChanges") } - transactions := make([]string, len(b.Body.ExecutionPayload.Transactions)) - for i, tx := range b.Body.ExecutionPayload.Transactions { - transactions[i] = hexutil.Encode(tx) + err = slice.VerifyMaxLength(b.Body.BlobKzgCommitments, fieldparams.MaxBlobCommitmentsPerBlock) + if err != nil { + return nil, server.NewDecodeError(err, "Body.BlobKzgCommitments") } - blobKzgCommitments := make([]string, len(b.Body.BlobKzgCommitments)) - for i := range b.Body.BlobKzgCommitments { - blobKzgCommitments[i] = hexutil.Encode(b.Body.BlobKzgCommitments[i]) + blobKzgCommitments := make([][]byte, len(b.Body.BlobKzgCommitments)) + for i, b := range b.Body.BlobKzgCommitments { + kzg, err := bytesutil.DecodeHexWithLength(b, fieldparams.BLSPubkeyLength) + if err != nil { + return nil, server.NewDecodeError(err, fmt.Sprintf("Body.BlobKzgCommitments[%d]", i)) + } + blobKzgCommitments[i] = kzg } - return &BeaconBlockDeneb{ - Slot: fmt.Sprintf("%d", b.Slot), - ProposerIndex: fmt.Sprintf("%d", b.ProposerIndex), - ParentRoot: hexutil.Encode(b.ParentRoot), - StateRoot: hexutil.Encode(b.StateRoot), - Body: &BeaconBlockBodyDeneb{ - RandaoReveal: hexutil.Encode(b.Body.RandaoReveal), - Eth1Data: Eth1DataFromConsensus(b.Body.Eth1Data), - Graffiti: hexutil.Encode(b.Body.Graffiti), - ProposerSlashings: ProposerSlashingsFromConsensus(b.Body.ProposerSlashings), - AttesterSlashings: AttesterSlashingsFromConsensus(b.Body.AttesterSlashings), - Attestations: AttsFromConsensus(b.Body.Attestations), - Deposits: DepositsFromConsensus(b.Body.Deposits), - VoluntaryExits: SignedExitsFromConsensus(b.Body.VoluntaryExits), - SyncAggregate: &SyncAggregate{ - SyncCommitteeBits: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeBits), - SyncCommitteeSignature: hexutil.Encode(b.Body.SyncAggregate.SyncCommitteeSignature), - }, - ExecutionPayload: &ExecutionPayloadDeneb{ - ParentHash: hexutil.Encode(b.Body.ExecutionPayload.ParentHash), - FeeRecipient: hexutil.Encode(b.Body.ExecutionPayload.FeeRecipient), - StateRoot: hexutil.Encode(b.Body.ExecutionPayload.StateRoot), - ReceiptsRoot: hexutil.Encode(b.Body.ExecutionPayload.ReceiptsRoot), - LogsBloom: hexutil.Encode(b.Body.ExecutionPayload.LogsBloom), - PrevRandao: hexutil.Encode(b.Body.ExecutionPayload.PrevRandao), - BlockNumber: fmt.Sprintf("%d", b.Body.ExecutionPayload.BlockNumber), - GasLimit: fmt.Sprintf("%d", b.Body.ExecutionPayload.GasLimit), - GasUsed: fmt.Sprintf("%d", b.Body.ExecutionPayload.GasUsed), - Timestamp: fmt.Sprintf("%d", b.Body.ExecutionPayload.Timestamp), - ExtraData: hexutil.Encode(b.Body.ExecutionPayload.ExtraData), - BaseFeePerGas: baseFeePerGas, - BlockHash: hexutil.Encode(b.Body.ExecutionPayload.BlockHash), - Transactions: transactions, - Withdrawals: WithdrawalsFromConsensus(b.Body.ExecutionPayload.Withdrawals), - BlobGasUsed: fmt.Sprintf("%d", b.Body.ExecutionPayload.BlobGasUsed), - ExcessBlobGas: fmt.Sprintf("%d", b.Body.ExecutionPayload.ExcessBlobGas), + return ð.BlindedBeaconBlockElectra{ + Slot: primitives.Slot(slot), + ProposerIndex: primitives.ValidatorIndex(proposerIndex), + ParentRoot: parentRoot, + StateRoot: stateRoot, + Body: ð.BlindedBeaconBlockBodyElectra{ + RandaoReveal: randaoReveal, + Eth1Data: ð.Eth1Data{ + DepositRoot: depositRoot, + DepositCount: depositCount, + BlockHash: blockHash, }, - BLSToExecutionChanges: SignedBLSChangesFromConsensus(b.Body.BlsToExecutionChanges), + Graffiti: graffiti, + ProposerSlashings: proposerSlashings, + AttesterSlashings: attesterSlashings, + Attestations: atts, + Deposits: deposits, + VoluntaryExits: exits, + SyncAggregate: ð.SyncAggregate{ + SyncCommitteeBits: syncCommitteeBits, + SyncCommitteeSignature: syncCommitteeSig, + }, + ExecutionPayloadHeader: &enginev1.ExecutionPayloadHeaderElectra{ + ParentHash: payloadParentHash, + FeeRecipient: payloadFeeRecipient, + StateRoot: payloadStateRoot, + ReceiptsRoot: payloadReceiptsRoot, + LogsBloom: payloadLogsBloom, + PrevRandao: payloadPrevRandao, + BlockNumber: payloadBlockNumber, + GasLimit: payloadGasLimit, + GasUsed: payloadGasUsed, + Timestamp: payloadTimestamp, + ExtraData: payloadExtraData, + BaseFeePerGas: payloadBaseFeePerGas, + BlockHash: payloadBlockHash, + TransactionsRoot: payloadTxsRoot, + WithdrawalsRoot: payloadWithdrawalsRoot, + BlobGasUsed: payloadBlobGasUsed, + ExcessBlobGas: payloadExcessBlobGas, + }, + BlsToExecutionChanges: blsChanges, BlobKzgCommitments: blobKzgCommitments, + ExecutionRequests: &enginev1.ExecutionRequests{ + Deposits: depositRequests, + Withdrawals: withdrawalRequests, + Consolidations: consolidationRequests, + }, }, }, nil } -func SignedBeaconBlockDenebFromConsensus(b *eth.SignedBeaconBlockDeneb) (*SignedBeaconBlockDeneb, error) { - block, err := BeaconBlockDenebFromConsensus(b.Block) - if err != nil { - return nil, err +func (b *BlindedBeaconBlockElectra) ToGeneric() (*eth.GenericBeaconBlock, error) { + if b == nil { + return nil, errNilValue } - return &SignedBeaconBlockDeneb{ - Message: block, - Signature: hexutil.Encode(b.Signature), - }, nil -} -func BeaconBlockContentsElectraFromConsensus(b *eth.BeaconBlockContentsElectra) (*BeaconBlockContentsElectra, error) { - block, err := BeaconBlockElectraFromConsensus(b.Block) + blindedBlock, err := b.ToConsensus() if err != nil { return nil, err } - proofs := make([]string, len(b.KzgProofs)) - for i, proof := range b.KzgProofs { - proofs[i] = hexutil.Encode(proof) - } - blbs := make([]string, len(b.Blobs)) - for i, blob := range b.Blobs { - blbs[i] = hexutil.Encode(blob) - } - return &BeaconBlockContentsElectra{ - Block: block, - KzgProofs: proofs, - Blobs: blbs, - }, nil + return ð.GenericBeaconBlock{Block: ð.GenericBeaconBlock_BlindedElectra{BlindedElectra: blindedBlock}, IsBlinded: true}, nil } func SignedBeaconBlockContentsElectraFromConsensus(b *eth.SignedBeaconBlockContentsElectra) (*SignedBeaconBlockContentsElectra, error) { @@ -2998,6 +3166,26 @@ func BlindedBeaconBlockElectraFromConsensus(b *eth.BlindedBeaconBlockElectra) (* }, nil } +func BeaconBlockContentsElectraFromConsensus(b *eth.BeaconBlockContentsElectra) (*BeaconBlockContentsElectra, error) { + block, err := BeaconBlockElectraFromConsensus(b.Block) + if err != nil { + return nil, err + } + proofs := make([]string, len(b.KzgProofs)) + for i, proof := range b.KzgProofs { + proofs[i] = hexutil.Encode(proof) + } + blbs := make([]string, len(b.Blobs)) + for i, blob := range b.Blobs { + blbs[i] = hexutil.Encode(blob) + } + return &BeaconBlockContentsElectra{ + Block: block, + KzgProofs: proofs, + Blobs: blbs, + }, nil +} + func ExecutionRequestsFromConsensus(er *enginev1.ExecutionRequests) *ExecutionRequests { return &ExecutionRequests{ Deposits: DepositRequestsFromConsensus(er.Deposits), @@ -3064,170 +3252,7 @@ func SignedBeaconBlockElectraFromConsensus(b *eth.SignedBeaconBlockElectra) (*Si }, nil } -func ExecutionPayloadFromConsensus(payload *enginev1.ExecutionPayload) (*ExecutionPayload, error) { - baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) - if err != nil { - return nil, err - } - transactions := make([]string, len(payload.Transactions)) - for i, tx := range payload.Transactions { - transactions[i] = hexutil.Encode(tx) - } - - return &ExecutionPayload{ - ParentHash: hexutil.Encode(payload.ParentHash), - FeeRecipient: hexutil.Encode(payload.FeeRecipient), - StateRoot: hexutil.Encode(payload.StateRoot), - ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), - LogsBloom: hexutil.Encode(payload.LogsBloom), - PrevRandao: hexutil.Encode(payload.PrevRandao), - BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), - GasLimit: fmt.Sprintf("%d", payload.GasLimit), - GasUsed: fmt.Sprintf("%d", payload.GasUsed), - Timestamp: fmt.Sprintf("%d", payload.Timestamp), - ExtraData: hexutil.Encode(payload.ExtraData), - BaseFeePerGas: baseFeePerGas, - BlockHash: hexutil.Encode(payload.BlockHash), - Transactions: transactions, - }, nil -} - -func ExecutionPayloadCapellaFromConsensus(payload *enginev1.ExecutionPayloadCapella) (*ExecutionPayloadCapella, error) { - baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) - if err != nil { - return nil, err - } - transactions := make([]string, len(payload.Transactions)) - for i, tx := range payload.Transactions { - transactions[i] = hexutil.Encode(tx) - } - - return &ExecutionPayloadCapella{ - ParentHash: hexutil.Encode(payload.ParentHash), - FeeRecipient: hexutil.Encode(payload.FeeRecipient), - StateRoot: hexutil.Encode(payload.StateRoot), - ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), - LogsBloom: hexutil.Encode(payload.LogsBloom), - PrevRandao: hexutil.Encode(payload.PrevRandao), - BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), - GasLimit: fmt.Sprintf("%d", payload.GasLimit), - GasUsed: fmt.Sprintf("%d", payload.GasUsed), - Timestamp: fmt.Sprintf("%d", payload.Timestamp), - ExtraData: hexutil.Encode(payload.ExtraData), - BaseFeePerGas: baseFeePerGas, - BlockHash: hexutil.Encode(payload.BlockHash), - Transactions: transactions, - Withdrawals: WithdrawalsFromConsensus(payload.Withdrawals), - }, nil -} - -func ExecutionPayloadDenebFromConsensus(payload *enginev1.ExecutionPayloadDeneb) (*ExecutionPayloadDeneb, error) { - baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) - if err != nil { - return nil, err - } - transactions := make([]string, len(payload.Transactions)) - for i, tx := range payload.Transactions { - transactions[i] = hexutil.Encode(tx) - } - - return &ExecutionPayloadDeneb{ - ParentHash: hexutil.Encode(payload.ParentHash), - FeeRecipient: hexutil.Encode(payload.FeeRecipient), - StateRoot: hexutil.Encode(payload.StateRoot), - ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), - LogsBloom: hexutil.Encode(payload.LogsBloom), - PrevRandao: hexutil.Encode(payload.PrevRandao), - BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), - GasLimit: fmt.Sprintf("%d", payload.GasLimit), - GasUsed: fmt.Sprintf("%d", payload.GasUsed), - Timestamp: fmt.Sprintf("%d", payload.Timestamp), - ExtraData: hexutil.Encode(payload.ExtraData), - BaseFeePerGas: baseFeePerGas, - BlockHash: hexutil.Encode(payload.BlockHash), - Transactions: transactions, - Withdrawals: WithdrawalsFromConsensus(payload.Withdrawals), - BlobGasUsed: fmt.Sprintf("%d", payload.BlobGasUsed), - ExcessBlobGas: fmt.Sprintf("%d", payload.ExcessBlobGas), - }, nil -} - -var ExecutionPayloadElectraFromConsensus = ExecutionPayloadDenebFromConsensus - -func ExecutionPayloadHeaderFromConsensus(payload *enginev1.ExecutionPayloadHeader) (*ExecutionPayloadHeader, error) { - baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) - if err != nil { - return nil, err - } - - return &ExecutionPayloadHeader{ - ParentHash: hexutil.Encode(payload.ParentHash), - FeeRecipient: hexutil.Encode(payload.FeeRecipient), - StateRoot: hexutil.Encode(payload.StateRoot), - ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), - LogsBloom: hexutil.Encode(payload.LogsBloom), - PrevRandao: hexutil.Encode(payload.PrevRandao), - BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), - GasLimit: fmt.Sprintf("%d", payload.GasLimit), - GasUsed: fmt.Sprintf("%d", payload.GasUsed), - Timestamp: fmt.Sprintf("%d", payload.Timestamp), - ExtraData: hexutil.Encode(payload.ExtraData), - BaseFeePerGas: baseFeePerGas, - BlockHash: hexutil.Encode(payload.BlockHash), - TransactionsRoot: hexutil.Encode(payload.TransactionsRoot), - }, nil -} - -func ExecutionPayloadHeaderCapellaFromConsensus(payload *enginev1.ExecutionPayloadHeaderCapella) (*ExecutionPayloadHeaderCapella, error) { - baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) - if err != nil { - return nil, err - } - - return &ExecutionPayloadHeaderCapella{ - ParentHash: hexutil.Encode(payload.ParentHash), - FeeRecipient: hexutil.Encode(payload.FeeRecipient), - StateRoot: hexutil.Encode(payload.StateRoot), - ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), - LogsBloom: hexutil.Encode(payload.LogsBloom), - PrevRandao: hexutil.Encode(payload.PrevRandao), - BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), - GasLimit: fmt.Sprintf("%d", payload.GasLimit), - GasUsed: fmt.Sprintf("%d", payload.GasUsed), - Timestamp: fmt.Sprintf("%d", payload.Timestamp), - ExtraData: hexutil.Encode(payload.ExtraData), - BaseFeePerGas: baseFeePerGas, - BlockHash: hexutil.Encode(payload.BlockHash), - TransactionsRoot: hexutil.Encode(payload.TransactionsRoot), - WithdrawalsRoot: hexutil.Encode(payload.WithdrawalsRoot), - }, nil -} - -func ExecutionPayloadHeaderDenebFromConsensus(payload *enginev1.ExecutionPayloadHeaderDeneb) (*ExecutionPayloadHeaderDeneb, error) { - baseFeePerGas, err := sszBytesToUint256String(payload.BaseFeePerGas) - if err != nil { - return nil, err - } - - return &ExecutionPayloadHeaderDeneb{ - ParentHash: hexutil.Encode(payload.ParentHash), - FeeRecipient: hexutil.Encode(payload.FeeRecipient), - StateRoot: hexutil.Encode(payload.StateRoot), - ReceiptsRoot: hexutil.Encode(payload.ReceiptsRoot), - LogsBloom: hexutil.Encode(payload.LogsBloom), - PrevRandao: hexutil.Encode(payload.PrevRandao), - BlockNumber: fmt.Sprintf("%d", payload.BlockNumber), - GasLimit: fmt.Sprintf("%d", payload.GasLimit), - GasUsed: fmt.Sprintf("%d", payload.GasUsed), - Timestamp: fmt.Sprintf("%d", payload.Timestamp), - ExtraData: hexutil.Encode(payload.ExtraData), - BaseFeePerGas: baseFeePerGas, - BlockHash: hexutil.Encode(payload.BlockHash), - TransactionsRoot: hexutil.Encode(payload.TransactionsRoot), - WithdrawalsRoot: hexutil.Encode(payload.WithdrawalsRoot), - BlobGasUsed: fmt.Sprintf("%d", payload.BlobGasUsed), - ExcessBlobGas: fmt.Sprintf("%d", payload.ExcessBlobGas), - }, nil -} - -var ExecutionPayloadHeaderElectraFromConsensus = ExecutionPayloadHeaderDenebFromConsensus +var ( + ExecutionPayloadElectraFromConsensus = ExecutionPayloadDenebFromConsensus + ExecutionPayloadHeaderElectraFromConsensus = ExecutionPayloadHeaderDenebFromConsensus +) diff --git a/api/server/structs/conversions_lightclient.go b/api/server/structs/conversions_lightclient.go index a0479f6e39f9..fbf29c2245d1 100644 --- a/api/server/structs/conversions_lightclient.go +++ b/api/server/structs/conversions_lightclient.go @@ -124,10 +124,12 @@ func lightClientHeaderToJSON(header interfaces.LightClientHeader) (json.RawMessa var result any - switch v := header.Version(); v { - case version.Altair: + v := header.Version() + + switch { + case v == version.Altair: result = &LightClientHeader{Beacon: BeaconBlockHeaderFromConsensus(header.Beacon())} - case version.Capella: + case v == version.Capella: exInterface, err := header.Execution() if err != nil { return nil, err @@ -149,7 +151,7 @@ func lightClientHeaderToJSON(header interfaces.LightClientHeader) (json.RawMessa Execution: execution, ExecutionBranch: branchToJSON(executionBranch[:]), } - case version.Deneb: + case v == version.Deneb: exInterface, err := header.Execution() if err != nil { return nil, err @@ -171,7 +173,7 @@ func lightClientHeaderToJSON(header interfaces.LightClientHeader) (json.RawMessa Execution: execution, ExecutionBranch: branchToJSON(executionBranch[:]), } - case version.Electra: + case v >= version.Electra: exInterface, err := header.Execution() if err != nil { return nil, err diff --git a/beacon-chain/blockchain/execution_engine.go b/beacon-chain/blockchain/execution_engine.go index c95a928b2830..152626c5f16f 100644 --- a/beacon-chain/blockchain/execution_engine.go +++ b/beacon-chain/blockchain/execution_engine.go @@ -363,8 +363,10 @@ func (s *Service) getPayloadAttribute(ctx context.Context, st state.BeaconState, } var attr payloadattribute.Attributer - switch st.Version() { - case version.Deneb, version.Electra: + + v := st.Version() + switch { + case v >= version.Deneb: withdrawals, _, err := st.ExpectedWithdrawals() if err != nil { log.WithError(err).Error("Could not get expected withdrawals to get payload attribute") @@ -381,7 +383,7 @@ func (s *Service) getPayloadAttribute(ctx context.Context, st state.BeaconState, log.WithError(err).Error("Could not get payload attribute") return emptyAttri } - case version.Capella: + case v == version.Capella: withdrawals, _, err := st.ExpectedWithdrawals() if err != nil { log.WithError(err).Error("Could not get expected withdrawals to get payload attribute") @@ -397,7 +399,7 @@ func (s *Service) getPayloadAttribute(ctx context.Context, st state.BeaconState, log.WithError(err).Error("Could not get payload attribute") return emptyAttri } - case version.Bellatrix: + case v == version.Bellatrix: attr, err = payloadattribute.New(&enginev1.PayloadAttributes{ Timestamp: uint64(t.Unix()), PrevRandao: prevRando, diff --git a/beacon-chain/core/transition/transition.go b/beacon-chain/core/transition/transition.go index 0ffe051795e7..8297361b6302 100644 --- a/beacon-chain/core/transition/transition.go +++ b/beacon-chain/core/transition/transition.go @@ -298,7 +298,7 @@ func ProcessSlotsCore(ctx context.Context, span trace.Span, state state.BeaconSt func ProcessEpoch(ctx context.Context, state state.BeaconState) (state.BeaconState, error) { var err error if time.CanProcessEpoch(state) { - if state.Version() == version.Electra { + if state.Version() >= version.Electra { if err = electra.ProcessEpoch(ctx, state); err != nil { return nil, errors.Wrap(err, fmt.Sprintf("could not process %s epoch", version.String(state.Version()))) } @@ -322,9 +322,11 @@ func UpgradeState(ctx context.Context, state state.BeaconState) (state.BeaconSta defer span.End() var err error + + slot := state.Slot() upgraded := false - if time.CanUpgradeToAltair(state.Slot()) { + if time.CanUpgradeToAltair(slot) { state, err = altair.UpgradeToAltair(ctx, state) if err != nil { tracing.AnnotateError(span, err) @@ -333,7 +335,7 @@ func UpgradeState(ctx context.Context, state state.BeaconState) (state.BeaconSta upgraded = true } - if time.CanUpgradeToBellatrix(state.Slot()) { + if time.CanUpgradeToBellatrix(slot) { state, err = execution.UpgradeToBellatrix(state) if err != nil { tracing.AnnotateError(span, err) @@ -342,7 +344,7 @@ func UpgradeState(ctx context.Context, state state.BeaconState) (state.BeaconSta upgraded = true } - if time.CanUpgradeToCapella(state.Slot()) { + if time.CanUpgradeToCapella(slot) { state, err = capella.UpgradeToCapella(state) if err != nil { tracing.AnnotateError(span, err) @@ -351,7 +353,7 @@ func UpgradeState(ctx context.Context, state state.BeaconState) (state.BeaconSta upgraded = true } - if time.CanUpgradeToDeneb(state.Slot()) { + if time.CanUpgradeToDeneb(slot) { state, err = deneb.UpgradeToDeneb(state) if err != nil { tracing.AnnotateError(span, err) @@ -360,7 +362,7 @@ func UpgradeState(ctx context.Context, state state.BeaconState) (state.BeaconSta upgraded = true } - if time.CanUpgradeToElectra(state.Slot()) { + if time.CanUpgradeToElectra(slot) { state, err = electra.UpgradeToElectra(state) if err != nil { tracing.AnnotateError(span, err) @@ -370,7 +372,7 @@ func UpgradeState(ctx context.Context, state state.BeaconState) (state.BeaconSta } if upgraded { - log.Debugf("upgraded state to %s", version.String(state.Version())) + log.WithField("version", version.String(state.Version())).Debug("Upgraded state to") } return state, nil diff --git a/beacon-chain/core/validators/slashing.go b/beacon-chain/core/validators/slashing.go index 1149ac78773a..14c72b1929c1 100644 --- a/beacon-chain/core/validators/slashing.go +++ b/beacon-chain/core/validators/slashing.go @@ -9,25 +9,35 @@ import ( // SlashingParamsPerVersion returns the slashing parameters for the given state version. func SlashingParamsPerVersion(v int) (slashingQuotient, proposerRewardQuotient, whistleblowerRewardQuotient uint64, err error) { cfg := params.BeaconConfig() - switch v { - case version.Phase0: - slashingQuotient = cfg.MinSlashingPenaltyQuotient + + if v >= version.Electra { + slashingQuotient = cfg.MinSlashingPenaltyQuotientElectra + proposerRewardQuotient = cfg.ProposerRewardQuotient + whistleblowerRewardQuotient = cfg.WhistleBlowerRewardQuotientElectra + return + } + + if v >= version.Bellatrix { + slashingQuotient = cfg.MinSlashingPenaltyQuotientBellatrix proposerRewardQuotient = cfg.ProposerRewardQuotient whistleblowerRewardQuotient = cfg.WhistleBlowerRewardQuotient - case version.Altair: + return + } + + if v >= version.Altair { slashingQuotient = cfg.MinSlashingPenaltyQuotientAltair proposerRewardQuotient = cfg.ProposerRewardQuotient whistleblowerRewardQuotient = cfg.WhistleBlowerRewardQuotient - case version.Bellatrix, version.Capella, version.Deneb: - slashingQuotient = cfg.MinSlashingPenaltyQuotientBellatrix + return + } + + if v >= version.Phase0 { + slashingQuotient = cfg.MinSlashingPenaltyQuotient proposerRewardQuotient = cfg.ProposerRewardQuotient whistleblowerRewardQuotient = cfg.WhistleBlowerRewardQuotient - case version.Electra: - slashingQuotient = cfg.MinSlashingPenaltyQuotientElectra - proposerRewardQuotient = cfg.ProposerRewardQuotient - whistleblowerRewardQuotient = cfg.WhistleBlowerRewardQuotientElectra - default: - err = errors.New("unknown state version") + return } + + err = errors.New("unknown state version") return } diff --git a/beacon-chain/execution/engine_client.go b/beacon-chain/execution/engine_client.go index 002378ad5b0b..a9b6503192b2 100644 --- a/beacon-chain/execution/engine_client.go +++ b/beacon-chain/execution/engine_client.go @@ -643,9 +643,17 @@ func fullPayloadFromPayloadBody( return nil, errors.New("execution block and header cannot be nil") } - switch bVersion { - case version.Bellatrix: - return blocks.WrappedExecutionPayload(&pb.ExecutionPayload{ + if bVersion >= version.Deneb { + ebg, err := header.ExcessBlobGas() + if err != nil { + return nil, errors.Wrap(err, "unable to extract ExcessBlobGas attribute from execution payload header") + } + bgu, err := header.BlobGasUsed() + if err != nil { + return nil, errors.Wrap(err, "unable to extract BlobGasUsed attribute from execution payload header") + } + return blocks.WrappedExecutionPayloadDeneb( +&pb.ExecutionPayloadDeneb{ ParentHash: header.ParentHash(), FeeRecipient: header.FeeRecipient(), StateRoot: header.StateRoot(), @@ -660,8 +668,13 @@ func fullPayloadFromPayloadBody( BaseFeePerGas: header.BaseFeePerGas(), BlockHash: header.BlockHash(), Transactions: pb.RecastHexutilByteSlice(body.Transactions), - }) - case version.Capella: + Withdrawals: body.Withdrawals, + ExcessBlobGas: ebg, + BlobGasUsed: bgu, + }) // We can't get the block value and don't care about the block value for this instance + } + + if bVersion >= version.Capella { return blocks.WrappedExecutionPayloadCapella(&pb.ExecutionPayloadCapella{ ParentHash: header.ParentHash(), FeeRecipient: header.FeeRecipient(), @@ -679,17 +692,10 @@ func fullPayloadFromPayloadBody( Transactions: pb.RecastHexutilByteSlice(body.Transactions), Withdrawals: body.Withdrawals, }) // We can't get the block value and don't care about the block value for this instance - case version.Deneb, version.Electra: - ebg, err := header.ExcessBlobGas() - if err != nil { - return nil, errors.Wrap(err, "unable to extract ExcessBlobGas attribute from execution payload header") - } - bgu, err := header.BlobGasUsed() - if err != nil { - return nil, errors.Wrap(err, "unable to extract BlobGasUsed attribute from execution payload header") - } - return blocks.WrappedExecutionPayloadDeneb( - &pb.ExecutionPayloadDeneb{ + } + + if bVersion >= version.Bellatrix { + return blocks.WrappedExecutionPayload(&pb.ExecutionPayload{ ParentHash: header.ParentHash(), FeeRecipient: header.FeeRecipient(), StateRoot: header.StateRoot(), @@ -704,14 +710,11 @@ func fullPayloadFromPayloadBody( BaseFeePerGas: header.BaseFeePerGas(), BlockHash: header.BlockHash(), Transactions: pb.RecastHexutilByteSlice(body.Transactions), - Withdrawals: body.Withdrawals, - ExcessBlobGas: ebg, - BlobGasUsed: bgu, - }) // We can't get the block value and don't care about the block value for this instance - default: + }) + } + return nil, fmt.Errorf("unknown execution block version for payload %d", bVersion) } -} // Handles errors received from the RPC server according to the specification. func handleRPCError(err error) error { @@ -802,9 +805,8 @@ func tDStringToUint256(td string) (*uint256.Int, error) { } func EmptyExecutionPayload(v int) (proto.Message, error) { - switch v { - case version.Bellatrix: - return &pb.ExecutionPayload{ + if v >= version.Deneb { + return &pb.ExecutionPayloadDeneb{ ParentHash: make([]byte, fieldparams.RootLength), FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), StateRoot: make([]byte, fieldparams.RootLength), @@ -815,8 +817,11 @@ func EmptyExecutionPayload(v int) (proto.Message, error) { BaseFeePerGas: make([]byte, fieldparams.RootLength), BlockHash: make([]byte, fieldparams.RootLength), Transactions: make([][]byte, 0), +Withdrawals: make([]*pb.Withdrawal, 0), }, nil - case version.Capella: + } + + if v >= version.Capella { return &pb.ExecutionPayloadCapella{ ParentHash: make([]byte, fieldparams.RootLength), FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), @@ -830,8 +835,10 @@ func EmptyExecutionPayload(v int) (proto.Message, error) { Transactions: make([][]byte, 0), Withdrawals: make([]*pb.Withdrawal, 0), }, nil - case version.Deneb, version.Electra: - return &pb.ExecutionPayloadDeneb{ + } + + if v >= version.Bellatrix { + return &pb.ExecutionPayload{ ParentHash: make([]byte, fieldparams.RootLength), FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), StateRoot: make([]byte, fieldparams.RootLength), @@ -842,29 +849,15 @@ func EmptyExecutionPayload(v int) (proto.Message, error) { BaseFeePerGas: make([]byte, fieldparams.RootLength), BlockHash: make([]byte, fieldparams.RootLength), Transactions: make([][]byte, 0), - Withdrawals: make([]*pb.Withdrawal, 0), - }, nil - default: + }, nil + } + return nil, errors.Wrapf(ErrUnsupportedVersion, "version=%s", version.String(v)) } -} func EmptyExecutionPayloadHeader(v int) (proto.Message, error) { - switch v { - case version.Bellatrix: - return &pb.ExecutionPayloadHeader{ - ParentHash: make([]byte, fieldparams.RootLength), - FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), - StateRoot: make([]byte, fieldparams.RootLength), - ReceiptsRoot: make([]byte, fieldparams.RootLength), - LogsBloom: make([]byte, fieldparams.LogsBloomLength), - PrevRandao: make([]byte, fieldparams.RootLength), - ExtraData: make([]byte, 0), - BaseFeePerGas: make([]byte, fieldparams.RootLength), - BlockHash: make([]byte, fieldparams.RootLength), - }, nil - case version.Capella: - return &pb.ExecutionPayloadHeaderCapella{ + if v >= version.Deneb { + return &pb.ExecutionPayloadHeaderDeneb{ ParentHash: make([]byte, fieldparams.RootLength), FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), StateRoot: make([]byte, fieldparams.RootLength), @@ -877,8 +870,10 @@ func EmptyExecutionPayloadHeader(v int) (proto.Message, error) { TransactionsRoot: make([]byte, fieldparams.RootLength), WithdrawalsRoot: make([]byte, fieldparams.RootLength), }, nil - case version.Deneb, version.Electra: - return &pb.ExecutionPayloadHeaderDeneb{ + } + + if v >= version.Capella { + return &pb.ExecutionPayloadHeaderCapella{ ParentHash: make([]byte, fieldparams.RootLength), FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), StateRoot: make([]byte, fieldparams.RootLength), @@ -891,10 +886,24 @@ func EmptyExecutionPayloadHeader(v int) (proto.Message, error) { TransactionsRoot: make([]byte, fieldparams.RootLength), WithdrawalsRoot: make([]byte, fieldparams.RootLength), }, nil - default: - return nil, errors.Wrapf(ErrUnsupportedVersion, "version=%s", version.String(v)) } -} + + if v >= version.Bellatrix { + return &pb.ExecutionPayloadHeader{ + ParentHash: make([]byte, fieldparams.RootLength), + FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), + StateRoot: make([]byte, fieldparams.RootLength), + ReceiptsRoot: make([]byte, fieldparams.RootLength), + LogsBloom: make([]byte, fieldparams.LogsBloomLength), + PrevRandao: make([]byte, fieldparams.RootLength), + ExtraData: make([]byte, 0), + BaseFeePerGas: make([]byte, fieldparams.RootLength), + BlockHash: make([]byte, fieldparams.RootLength), + }, nil + } + + return nil, errors.Wrapf(ErrUnsupportedVersion, "version=%s", version.String(v)) + } func toBlockNumArg(number *big.Int) string { if number == nil { diff --git a/beacon-chain/operations/attestations/kv/aggregated.go b/beacon-chain/operations/attestations/kv/aggregated.go index cc1319eb4f67..32d329dd1f4f 100644 --- a/beacon-chain/operations/attestations/kv/aggregated.go +++ b/beacon-chain/operations/attestations/kv/aggregated.go @@ -236,7 +236,7 @@ func (c *AttCaches) AggregatedAttestationsBySlotIndexElectra( c.aggregatedAttLock.RLock() defer c.aggregatedAttLock.RUnlock() for _, as := range c.aggregatedAtt { - if as[0].Version() == version.Electra && slot == as[0].GetData().Slot && as[0].CommitteeBitsVal().BitAt(uint64(committeeIndex)) { + if as[0].Version() >= version.Electra && slot == as[0].GetData().Slot && as[0].CommitteeBitsVal().BitAt(uint64(committeeIndex)) { for _, a := range as { att, ok := a.(*ethpb.AttestationElectra) // This will never fail in practice because we asserted the version diff --git a/beacon-chain/operations/attestations/kv/unaggregated.go b/beacon-chain/operations/attestations/kv/unaggregated.go index 6bf6cb888c75..bbaabbd306c8 100644 --- a/beacon-chain/operations/attestations/kv/unaggregated.go +++ b/beacon-chain/operations/attestations/kv/unaggregated.go @@ -115,7 +115,7 @@ func (c *AttCaches) UnaggregatedAttestationsBySlotIndexElectra( unAggregatedAtts := c.unAggregatedAtt for _, a := range unAggregatedAtts { - if a.Version() == version.Electra && slot == a.GetData().Slot && a.CommitteeBitsVal().BitAt(uint64(committeeIndex)) { + if a.Version() >= version.Electra && slot == a.GetData().Slot && a.CommitteeBitsVal().BitAt(uint64(committeeIndex)) { att, ok := a.(*ethpb.AttestationElectra) // This will never fail in practice because we asserted the version if ok { diff --git a/beacon-chain/rpc/eth/beacon/handlers.go b/beacon-chain/rpc/eth/beacon/handlers.go index 04ace1525715..456c247b7bc6 100644 --- a/beacon-chain/rpc/eth/beacon/handlers.go +++ b/beacon-chain/rpc/eth/beacon/handlers.go @@ -368,10 +368,10 @@ func (s *Server) publishBlindedBlockSSZ(ctx context.Context, w http.ResponseWrit s.proposeBlock(ctx, w, genericBlock) return } - if versionHeader == version.String(version.Electra) { + if versionHeader >= version.String(version.Electra) { httputil.HandleError( w, - fmt.Sprintf("Could not decode request body into %s consensus block: %v", version.String(version.Electra), err.Error()), + fmt.Sprintf("Could not decode request body into %s consensus block: %v", versionHeader, err.Error()), http.StatusBadRequest, ) return @@ -520,10 +520,10 @@ func (s *Server) publishBlindedBlock(ctx context.Context, w http.ResponseWriter, return } } - if versionHeader == version.String(version.Electra) { + if versionHeader >= version.String(version.Electra) { httputil.HandleError( w, - fmt.Sprintf("Could not decode request body into %s consensus block: %v", version.String(version.Electra), err.Error()), + fmt.Sprintf("Could not decode request body into %s consensus block: %v", versionHeader, err.Error()), http.StatusBadRequest, ) return @@ -716,10 +716,10 @@ func (s *Server) publishBlockSSZ(ctx context.Context, w http.ResponseWriter, r * s.proposeBlock(ctx, w, genericBlock) return } - if versionHeader == version.String(version.Electra) { + if versionHeader >= version.String(version.Electra) { httputil.HandleError( w, - fmt.Sprintf("Could not decode request body into %s consensus block: %v", version.String(version.Electra), err.Error()), + fmt.Sprintf("Could not decode request body into %s consensus block: %v", versionHeader, err.Error()), http.StatusBadRequest, ) return @@ -889,10 +889,10 @@ func (s *Server) publishBlock(ctx context.Context, w http.ResponseWriter, r *htt return } } - if versionHeader == version.String(version.Electra) { + if versionHeader >= version.String(version.Electra) { httputil.HandleError( w, - fmt.Sprintf("Could not decode request body into %s consensus block: %v", version.String(version.Electra), err.Error()), + fmt.Sprintf("Could not decode request body into %s consensus block: %v", versionHeader, err.Error()), http.StatusBadRequest, ) return diff --git a/beacon-chain/state/state-native/spec_parameters.go b/beacon-chain/state/state-native/spec_parameters.go index 7c955e92c1d2..36c8fe3c2cda 100644 --- a/beacon-chain/state/state-native/spec_parameters.go +++ b/beacon-chain/state/state-native/spec_parameters.go @@ -6,25 +6,33 @@ import ( ) func (b *BeaconState) ProportionalSlashingMultiplier() (uint64, error) { - switch b.version { - case version.Bellatrix, version.Capella, version.Deneb, version.Electra: + if b.version >= version.Bellatrix { return params.BeaconConfig().ProportionalSlashingMultiplierBellatrix, nil - case version.Altair: + } + + if b.version == version.Altair { return params.BeaconConfig().ProportionalSlashingMultiplierAltair, nil - case version.Phase0: + } + + if b.version == version.Phase0 { return params.BeaconConfig().ProportionalSlashingMultiplier, nil } + return 0, errNotSupported("ProportionalSlashingMultiplier", b.version) } func (b *BeaconState) InactivityPenaltyQuotient() (uint64, error) { - switch b.version { - case version.Bellatrix, version.Capella, version.Deneb, version.Electra: + if b.version >= version.Bellatrix { return params.BeaconConfig().InactivityPenaltyQuotientBellatrix, nil - case version.Altair: + } + + if b.version == version.Altair { return params.BeaconConfig().InactivityPenaltyQuotientAltair, nil - case version.Phase0: + } + + if b.version == version.Phase0 { return params.BeaconConfig().InactivityPenaltyQuotient, nil } + return 0, errNotSupported("InactivityPenaltyQuotient", b.version) } diff --git a/proto/engine/v1/electra.go b/proto/engine/v1/electra.go index c6e3af5554ab..5f75ffc41fa6 100644 --- a/proto/engine/v1/electra.go +++ b/proto/engine/v1/electra.go @@ -8,8 +8,10 @@ import ( "github.com/prysmaticlabs/prysm/v5/config/params" ) -type ExecutionPayloadElectra = ExecutionPayloadDeneb -type ExecutionPayloadHeaderElectra = ExecutionPayloadHeaderDeneb +type ( + ExecutionPayloadElectra = ExecutionPayloadDeneb + ExecutionPayloadHeaderElectra = ExecutionPayloadHeaderDeneb +) var ( drExample = &DepositRequest{}