Skip to content

Commit

Permalink
add: get_data_column_sidecars (#6566)
Browse files Browse the repository at this point in the history
* add: get_data_column_sidecars

* remove redundant type conversions

* remove redundant for loop

* review 2

* review 1
  • Loading branch information
agnxsh committed Sep 22, 2024
1 parent ec831f1 commit 85d7109
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions beacon_chain/spec/crypto.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
1 change: 1 addition & 0 deletions beacon_chain/spec/datatypes/eip7594.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
127 changes: 127 additions & 0 deletions beacon_chain/spec/eip7594_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down Expand Up @@ -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..<NUMBER_OF_COLUMNS:
var
column_cells: seq[KzgCell]
column_proofs: seq[KzgProof]
for i in 0..<cellsAndProofs.len:
column_cells.add(cellsAndProofs[i].cells)
column_proofs.add(cellsAndProofs[i].proofs)

var sidecar = DataColumnSidecar(
index: ColumnIndex(column_index),
column: DataColumn.init(column_cells),
kzg_commitments: blck.body.blob_kzg_commitments,
kzg_proofs: KzgProofs.init(column_proofs),
signed_block_header: signed_beacon_block_header)
blck.body.build_proof(
KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH_GINDEX.GeneralizedIndex,
sidecar.kzg_commitments_inclusion_proof).expect("Valid gindex")
sidecars.add(sidecar)

sidecars

# Alternative approach to `get_data_column_sidecars` by directly computing
# blobs from blob bundles
proc get_data_column_sidecars*(signed_beacon_block: electra.SignedBeaconBlock,
blobs: seq[KzgBlob]):
Result[seq[DataColumnSidecar], string] =
## Given a signed beacon block and the blobs 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 `SignedBeaconBlock` as
## during practice we would be extracting data columns
## before publishing them, all of this happens during block
## production, hence the blocks are yet untrusted and have not
## yet been verified.
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)

var
sidecars =
newSeqOfCap[DataColumnSidecar](CELLS_PER_EXT_BLOB)
cells = newSeq[CellBytes](blobs.len)
proofs = newSeq[ProofBytes](blobs.len)

for i in 0..<blobs.len:
let
cell_and_proof = computeCellsAndKzgProofs(blobs[i])
if cell_and_proof.isErr():
return err("EIP7549: Could not compute cells")

cells[i] = cell_and_proof.get.cells
proofs[i] = cell_and_proof.get.proofs

for columnIndex in 0..<CELLS_PER_EXT_BLOB:
var
column: seq[KzgCell]
kzgProofOfColumn: seq[KzgProof]
for rowIndex in 0..<blobs.len:
column.add(cells[rowIndex][columnIndex])
kzgProofOfColumn.add(proofs[rowIndex][columnIndex])

var sidecar = DataColumnSidecar(
index: ColumnIndex(columnIndex),
column: DataColumn.init(column),
kzg_commitments: blck.body.blob_kzg_commitments,
kzg_proofs: KzgProofs.init(kzgProofOfColumn),
signed_block_header: signed_beacon_block_header)
blck.body.build_proof(
KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH_GINDEX.GeneralizedIndex,
sidecar.kzg_commitments_inclusion_proof).expect("Valid gindex")
sidecars.add(sidecar)

ok(sidecars)

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.6/specs/_features/eip7594/peer-sampling.md#get_extended_sample_count
func get_extended_sample_count*(samples_per_slot: int,
allowed_failures: int):
Expand Down

0 comments on commit 85d7109

Please sign in to comment.