Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement block_to_light_client_header #14433

Merged
merged 13 commits into from
Sep 12, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
- Light client support: Implement `ComputeFieldRootsForBlockBody`.
- Light client support: Add light client database changes.
- Light client support: Implement capella and deneb changes.
- Light client support: Implement `BlockToLightClientHeaderXXX` functions upto Deneb

### Changed

Expand Down
218 changes: 218 additions & 0 deletions beacon-chain/rpc/eth/light-client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,221 @@ func IsBetterUpdate(newUpdate, oldUpdate *v2.LightClientUpdate) (bool, error) {
}
return newUpdate.SignatureSlot < oldUpdate.SignatureSlot, nil
}

func BlockToLightClientHeaderAltair(block interfaces.ReadOnlySignedBeaconBlock) (*structs.LightClientHeader, error) {
parentRootArray := block.Block().ParentRoot()
rupam-04 marked this conversation as resolved.
Show resolved Hide resolved
parentRoot := parentRootArray[:]
rupam-04 marked this conversation as resolved.
Show resolved Hide resolved

stateRootArray := block.Block().StateRoot()
stateRoot := stateRootArray[:]
rupam-04 marked this conversation as resolved.
Show resolved Hide resolved

bodyRootArray, err := block.Block().Body().HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "could not get body root")
}
bodyRoot := bodyRootArray[:]

return &structs.LightClientHeader{
Beacon: &structs.BeaconBlockHeader{
Slot: hexutil.EncodeUint64(uint64(block.Block().Slot())),
ProposerIndex: hexutil.EncodeUint64(uint64(block.Block().ProposerIndex())),
ParentRoot: hexutil.Encode(parentRoot),
StateRoot: hexutil.Encode(stateRoot),
BodyRoot: hexutil.Encode(bodyRoot),
},
}, nil
}

func BlockToLightClientHeaderCapella(ctx context.Context, block interfaces.ReadOnlySignedBeaconBlock) (*structs.LightClientHeaderCapella, error) {
epoch := slots.ToEpoch(block.Block().Slot())
if epoch < params.BeaconConfig().CapellaForkEpoch {
return nil, fmt.Errorf("creating Capella light client header is not supported before Capella, invalid slot %d", block.Block().Slot())
}

payload, err := block.Block().Body().Execution()
if err != nil {
return nil, errors.Wrap(err, "could not get execution payload")
}

transactionsRoot, err := payload.TransactionsRoot()
if errors.Is(err, consensus_types.ErrUnsupportedField) {
transactions, err := payload.Transactions()
if err != nil {
return nil, errors.Wrap(err, "could not get transactions")
}
transactionsRootArray, err := ssz.TransactionsRoot(transactions)
if err != nil {
return nil, errors.Wrap(err, "could not get transactions root")
}
transactionsRoot = transactionsRootArray[:]
} else if err != nil {
return nil, errors.Wrap(err, "could not get transactions root")
}
withdrawalsRoot, err := payload.WithdrawalsRoot()
if errors.Is(err, consensus_types.ErrUnsupportedField) {
withdrawals, err := payload.Withdrawals()
if err != nil {
return nil, errors.Wrap(err, "could not get withdrawals")
}
withdrawalsRootArray, err := ssz.WithdrawalSliceRoot(withdrawals, fieldparams.MaxWithdrawalsPerPayload)
if err != nil {
return nil, errors.Wrap(err, "could not get withdrawals root")
}
withdrawalsRoot = withdrawalsRootArray[:]
} else if err != nil {
return nil, errors.Wrap(err, "could not get withdrawals root")
}

executionHeader := &structs.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: hexutil.EncodeUint64(payload.BlockNumber()),
GasLimit: hexutil.EncodeUint64(payload.GasLimit()),
GasUsed: hexutil.EncodeUint64(payload.GasUsed()),
Timestamp: hexutil.EncodeUint64(payload.Timestamp()),
ExtraData: hexutil.Encode(payload.ExtraData()),
BaseFeePerGas: hexutil.Encode(payload.BaseFeePerGas()),
BlockHash: hexutil.Encode(payload.BlockHash()),
TransactionsRoot: hexutil.Encode(transactionsRoot),
WithdrawalsRoot: hexutil.Encode(withdrawalsRoot),
}

executionPayloadProof, err := blocks.PayloadProof(ctx, block.Block())
if err != nil {
return nil, errors.Wrap(err, "could not get execution payload proof")
}
executionPayloadProofStr := make([]string, len(executionPayloadProof))
for i, proof := range executionPayloadProof {
executionPayloadProofStr[i] = hexutil.Encode(proof)
}

parentRootArray := block.Block().ParentRoot()
parentRoot := parentRootArray[:]

stateRootArray := block.Block().StateRoot()
stateRoot := stateRootArray[:]

bodyRootArray, err := block.Block().Body().HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "could not get body root")
}
bodyRoot := bodyRootArray[:]

return &structs.LightClientHeaderCapella{
Beacon: &structs.BeaconBlockHeader{
Slot: hexutil.EncodeUint64(uint64(block.Block().Slot())),
ProposerIndex: hexutil.EncodeUint64(uint64(block.Block().ProposerIndex())),
ParentRoot: hexutil.Encode(parentRoot),
StateRoot: hexutil.Encode(stateRoot),
BodyRoot: hexutil.Encode(bodyRoot),
},
Execution: executionHeader,
ExecutionBranch: executionPayloadProofStr,
}, nil
}

func BlockToLightClientHeaderDeneb(ctx context.Context, block interfaces.ReadOnlySignedBeaconBlock) (*structs.LightClientHeaderDeneb, error) {
epoch := slots.ToEpoch(block.Block().Slot())
if epoch < params.BeaconConfig().DenebForkEpoch {
return nil, fmt.Errorf("creating Deneb light client header is not supported before Deneb, invalid slot %d", block.Block().Slot())
}

payload, err := block.Block().Body().Execution()
if err != nil {
return nil, errors.Wrap(err, "could not get execution payload")
}

transactionsRoot, err := payload.TransactionsRoot()
if errors.Is(err, consensus_types.ErrUnsupportedField) {
transactions, err := payload.Transactions()
if err != nil {
return nil, errors.Wrap(err, "could not get transactions")
}
transactionsRootArray, err := ssz.TransactionsRoot(transactions)
if err != nil {
return nil, errors.Wrap(err, "could not get transactions root")
}
transactionsRoot = transactionsRootArray[:]
} else if err != nil {
return nil, errors.Wrap(err, "could not get transactions root")
}
withdrawalsRoot, err := payload.WithdrawalsRoot()
if errors.Is(err, consensus_types.ErrUnsupportedField) {
withdrawals, err := payload.Withdrawals()
if err != nil {
return nil, errors.Wrap(err, "could not get withdrawals")
}
withdrawalsRootArray, err := ssz.WithdrawalSliceRoot(withdrawals, fieldparams.MaxWithdrawalsPerPayload)
if err != nil {
return nil, errors.Wrap(err, "could not get withdrawals root")
}
withdrawalsRoot = withdrawalsRootArray[:]
} else if err != nil {
return nil, errors.Wrap(err, "could not get withdrawals root")
}
blobGasUsed, err := payload.BlobGasUsed()
if err != nil {
return nil, errors.Wrap(err, "could not get blob gas used")
}
excessBlobGas, err := payload.ExcessBlobGas()
if err != nil {
return nil, errors.Wrap(err, "could not get excess blob gas")
}

executionHeader := &structs.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: hexutil.EncodeUint64(payload.BlockNumber()),
GasLimit: hexutil.EncodeUint64(payload.GasLimit()),
GasUsed: hexutil.EncodeUint64(payload.GasUsed()),
Timestamp: hexutil.EncodeUint64(payload.Timestamp()),
ExtraData: hexutil.Encode(payload.ExtraData()),
BaseFeePerGas: hexutil.Encode(payload.BaseFeePerGas()),
BlockHash: hexutil.Encode(payload.BlockHash()),
TransactionsRoot: hexutil.Encode(transactionsRoot),
WithdrawalsRoot: hexutil.Encode(withdrawalsRoot),
BlobGasUsed: hexutil.EncodeUint64(blobGasUsed),
ExcessBlobGas: hexutil.EncodeUint64(excessBlobGas),
}

executionPayloadProof, err := blocks.PayloadProof(ctx, block.Block())
if err != nil {
return nil, errors.Wrap(err, "could not get execution payload proof")
}
executionPayloadProofStr := make([]string, len(executionPayloadProof))
for i, proof := range executionPayloadProof {
executionPayloadProofStr[i] = hexutil.Encode(proof)
}

parentRootArray := block.Block().ParentRoot()
parentRoot := parentRootArray[:]

stateRootArray := block.Block().StateRoot()
stateRoot := stateRootArray[:]

bodyRootArray, err := block.Block().Body().HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "could not get body root")
}
bodyRoot := bodyRootArray[:]

return &structs.LightClientHeaderDeneb{
Beacon: &structs.BeaconBlockHeader{
Slot: hexutil.EncodeUint64(uint64(block.Block().Slot())),
ProposerIndex: hexutil.EncodeUint64(uint64(block.Block().ProposerIndex())),
ParentRoot: hexutil.Encode(parentRoot),
StateRoot: hexutil.Encode(stateRoot),
BodyRoot: hexutil.Encode(bodyRoot),
},
Execution: executionHeader,
ExecutionBranch: executionPayloadProofStr,
}, nil
}
Loading