From 85d710906565753b2cd48e9f580bff05ac819c37 Mon Sep 17 00:00:00 2001 From: Agnish Ghosh <80243668+agnxsh@users.noreply.github.com> Date: Mon, 23 Sep 2024 03:05:57 +0530 Subject: [PATCH] add: `get_data_column_sidecars` (#6566) * add: get_data_column_sidecars * remove redundant type conversions * remove redundant for loop * review 2 * review 1 --- beacon_chain/spec/crypto.nim | 3 + beacon_chain/spec/datatypes/eip7594.nim | 1 + beacon_chain/spec/eip7594_helpers.nim | 127 ++++++++++++++++++++++++ 3 files changed, 131 insertions(+) diff --git a/beacon_chain/spec/crypto.nim b/beacon_chain/spec/crypto.nim index 4efac1688..3fac973e4 100644 --- a/beacon_chain/spec/crypto.nim +++ b/beacon_chain/spec/crypto.nim @@ -403,6 +403,9 @@ func toHex*(x: CookedPubKey): string = func `$`*(x: CookedPubKey): string = $(x.toPubKey()) +func toValidatorSig*(x: TrustedSig): ValidatorSig = + ValidatorSig(blob: x.blob) + func toValidatorSig*(x: CookedSig): ValidatorSig = ValidatorSig(blob: blscurve.Signature(x).exportRaw()) diff --git a/beacon_chain/spec/datatypes/eip7594.nim b/beacon_chain/spec/datatypes/eip7594.nim index 0bcef4ef3..5ca9c9644 100644 --- a/beacon_chain/spec/datatypes/eip7594.nim +++ b/beacon_chain/spec/datatypes/eip7594.nim @@ -28,6 +28,7 @@ const # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.5/specs/_features/eip7594/p2p-interface.md#preset KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH* = 4 + KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH_GINDEX* = 27 type # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.6/specs/_features/eip7594/polynomial-commitments-sampling.md#custom-types diff --git a/beacon_chain/spec/eip7594_helpers.nim b/beacon_chain/spec/eip7594_helpers.nim index 0ecaefe85..e1824d610 100644 --- a/beacon_chain/spec/eip7594_helpers.nim +++ b/beacon_chain/spec/eip7594_helpers.nim @@ -12,9 +12,18 @@ import std/algorithm, results, eth/p2p/discoveryv5/[node], + kzg4844/[kzg], + ssz_serialization/[ + proofs, + types], + ./crypto, ./[helpers, digest], ./datatypes/[eip7594] +type + CellBytes = array[eip7594.CELLS_PER_EXT_BLOB, Cell] + ProofBytes = array[eip7594.CELLS_PER_EXT_BLOB, KzgProof] + func sortedColumnIndices*(columnsPerSubnet: ColumnIndex, subnetIds: HashSet[uint64]): seq[ColumnIndex] = @@ -155,6 +164,124 @@ proc recover_matrix*(partial_matrix: seq[MatrixEntry], ok(extended_matrix) +# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.6/specs/_features/eip7594/das-core.md#get_data_column_sidecars +proc get_data_column_sidecars*(signed_beacon_block: electra.TrustedSignedBeaconBlock, + cellsAndProofs: seq[CellsAndProofs]): + seq[DataColumnSidecar] = + ## Given a trusted signed beacon block and the cells/proofs associated + ## with each data column (thereby blob as well) corresponding to the block, + ## this function assembles the sidecars which can be distributed to + ## the peers post data column reconstruction at every slot start. + ## + ## Note: this function only accepts `TrustedSignedBeaconBlock` as + ## during practice we would be computing cells and proofs from + ## data columns only after retrieving them from the database, where + ## they we were already verified and persisted. + template blck(): auto = signed_beacon_block.message + let + beacon_block_header = + BeaconBlockHeader( + slot: blck.slot, + proposer_index: blck.proposer_index, + parent_root: blck.parent_root, + state_root: blck.state_root, + body_root: hash_tree_root(blck.body)) + + signed_beacon_block_header = + SignedBeaconBlockHeader( + message: beacon_block_header, + signature: signed_beacon_block.signature.toValidatorSig) + + var + sidecars = + newSeqOfCap[DataColumnSidecar](CELLS_PER_EXT_BLOB) + + for column_index in 0..