quite some changes

This commit is contained in:
Agnish Ghosh 2024-10-02 22:36:18 +05:30
parent a789bdfaa6
commit c0c04a9528
3 changed files with 83 additions and 54 deletions

View File

@ -1546,8 +1546,8 @@ proc tryReconstructingDataColumns* (self: BeaconNode,
# Reconstruct data column sidecars from recovered blobs # Reconstruct data column sidecars from recovered blobs
let reconstructedDataColumns = get_data_column_sidecars(signed_block, recovered_cps.get) let reconstructedDataColumns = get_data_column_sidecars(signed_block, recovered_cps.get)
debugEcho "Reconstructed Data Columns len" debugEcho "Reconstructed Data Columns len"
debugEcho reconstructedDataColumns.get.len debugEcho reconstructedDataColumns.len
for data_column in reconstructedDataColumns.get: for data_column in reconstructedDataColumns:
if data_column.index notin custodiedColumnIndices: if data_column.index notin custodiedColumnIndices:
continue continue

View File

@ -27,6 +27,8 @@ const
# The number of cells in an extended blob | # The number of cells in an extended blob |
# RANDOM_CHALLENGE_KZG_CELL_BATCH_DOMAIN = 'RCKZGCBATCH__V1_' # RANDOM_CHALLENGE_KZG_CELL_BATCH_DOMAIN = 'RCKZGCBATCH__V1_'
KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH* = 4 KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH* = 4
KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH_GINDEX* = 27
type type
BLSFieldElement* = KzgBytes32 BLSFieldElement* = KzgBytes32

View File

@ -247,97 +247,124 @@ proc compute_signed_block_header(signed_block: deneb.SignedBeaconBlock |
signature: signed_block.signature signature: signed_block.signature
) )
# https://github.com/ethereum/consensus-specs/blob/bb8f3caafc92590cdcf2d14974adb602db9b5ca3/specs/_features/eip7594/das-core.md#get_data_column_sidecars # 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_block: deneb.TrustedSignedBeaconBlock | proc get_data_column_sidecars*(signed_beacon_block: deneb.TrustedSignedBeaconBlock |
deneb.SignedBeaconBlock | electra.TrustedSignedBeaconBlock,
electra.TrustedSignedBeaconBlock |
electra.SignedBeaconBlock,
cellsAndProofs: seq[CellsAndProofs]): cellsAndProofs: seq[CellsAndProofs]):
Result[seq[DataColumnSidecar], string] = seq[DataColumnSidecar] =
# Given a signed block and the cells/proofs associated with each blob ## Given a trusted signed beacon block and the cells/proofs associated
# in the block, assemble the sidecars which can be distributed to peers. ## with each data column (thereby blob as well) corresponding to the block,
var ## this function assembles the sidecars which can be distributed to
blck = signed_block.message ## 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 = signed_beacon_block_header =
compute_signed_block_header(signed_block) SignedBeaconBlockHeader(
kzg_incl_proof: array[4, Eth2Digest] message: beacon_block_header,
signature: signed_beacon_block.signature.toValidatorSig)
var sidecars = newSeqOfCap[DataColumnSidecar](CELLS_PER_EXT_BLOB) var
sidecars =
if cellsAndProofs.len == 0: newSeqOfCap[DataColumnSidecar](kzg_abi.CELLS_PER_EXT_BLOB)
return ok(sidecars)
for column_index in 0..<NUMBER_OF_COLUMNS: for column_index in 0..<NUMBER_OF_COLUMNS:
var var
column_cells: DataColumn column_cells: seq[KzgCell]
column_proofs: KzgProofs column_proofs: seq[KzgProof]
for i in 0..<cellsAndProofs.len: for i in 0..<cellsAndProofs.len:
discard column_cells.add(cellsAndProofs[i].cells) column_cells.add(cellsAndProofs[i].cells)
discard column_proofs.add(cellsAndProofs[i].proofs) column_proofs.add(cellsAndProofs[i].proofs)
var sidecar = DataColumnSidecar( var sidecar = DataColumnSidecar(
index: ColumnIndex(column_index), index: ColumnIndex(column_index),
column: column_cells, column: DataColumn.init(column_cells),
kzg_commitments: blck.body.blob_kzg_commitments, kzg_commitments: blck.body.blob_kzg_commitments,
kzg_proofs: column_proofs, kzg_proofs: KzgProofs.init(column_proofs),
signed_block_header: signed_beacon_block_header) signed_block_header: signed_beacon_block_header)
blck.body.build_proof( blck.body.build_proof(
27.GeneralizedIndex, KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH_GINDEX.GeneralizedIndex,
sidecar.kzg_commitments_inclusion_proof).expect("Valid gindex") sidecar.kzg_commitments_inclusion_proof).expect("Valid gindex")
sidecars.add(sidecar) sidecars.add(sidecar)
ok(sidecars) sidecars
# https://github.com/ethereum/consensus-specs/blob/5f48840f4d768bf0e0a8156a3ed06ec333589007/specs/_features/eip7594/das-core.md#get_data_column_sidecars # Alternative approach to `get_data_column_sidecars` by directly computing
proc get_data_column_sidecars*(signed_block: deneb.SignedBeaconBlock | # blobs from blob bundles
proc get_data_column_sidecars*(signed_beacon_block: deneb.SignedBeaconBlock |
electra.SignedBeaconBlock, electra.SignedBeaconBlock,
blobs: seq[KzgBlob]): blobs: seq[KzgBlob]):
Result[seq[DataColumnSidecar], string] = Result[seq[DataColumnSidecar], string] =
var ## Given a signed beacon block and the blobs corresponding to the block,
blck = signed_block.message ## this function assembles the sidecars which can be distributed to
signed_beacon_block_header = compute_signed_block_header(signed_block) ## the peers post data column reconstruction at every slot start.
kzg_incl_proof: array[4, Eth2Digest] ##
## 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))
var sidecars = newSeqOfCap[DataColumnSidecar](CELLS_PER_EXT_BLOB) signed_beacon_block_header =
SignedBeaconBlockHeader(
if blobs.len == 0: message: beacon_block_header,
return ok(sidecars) signature: signed_beacon_block.signature)
var var
sidecars =
newSeqOfCap[DataColumnSidecar](eip7594.CELLS_PER_EXT_BLOB)
cells = newSeq[CellBytes](blobs.len) cells = newSeq[CellBytes](blobs.len)
proofs = newSeq[ProofBytes](blobs.len) proofs = newSeq[ProofBytes](blobs.len)
for i in 0..<blobs.len: for i in 0..<blobs.len:
let let
cell_and_proof = computeCellsAndProofs(blobs[i]) cell_and_proof = computeCellsAndKzgProofs(blobs[i])
if cell_and_proof.isErr(): if cell_and_proof.isErr():
return err("EIP7549: Could not compute cells") return err("EIP7549: Could not compute cells")
cells[i] = cell_and_proof.get.cells cells[i] = cell_and_proof.get.cells
proofs[i] = cell_and_proof.get.proofs proofs[i] = cell_and_proof.get.proofs
let blobCount = blobs.len for columnIndex in 0..<eip7594.CELLS_PER_EXT_BLOB:
for columnIndex in 0..<CELLS_PER_EXT_BLOB:
var var
column: DataColumn column: seq[KzgCell]
kzgProofOfColumn: KzgProofs kzgProofOfColumn: seq[KzgProof]
for rowIndex in 0..<blobCount: for rowIndex in 0..<blobs.len:
discard column.add(cells[rowIndex][columnIndex]) column.add(cells[rowIndex][columnIndex])
kzgProofOfColumn.add(proofs[rowIndex][columnIndex])
for rowIndex in 0..<blobCount:
discard kzgProofOfColumn.add(proofs[rowIndex][columnIndex])
var sidecar = DataColumnSidecar( var sidecar = DataColumnSidecar(
index: ColumnIndex(columnIndex), index: ColumnIndex(columnIndex),
column: DataColumn(column), column: DataColumn.init(column),
kzg_commitments: blck.body.blob_kzg_commitments, kzg_commitments: blck.body.blob_kzg_commitments,
kzg_proofs: KzgProofs(kzgProofOfColumn), kzg_proofs: KzgProofs.init(kzgProofOfColumn),
signed_block_header: signed_beacon_block_header) signed_block_header: signed_beacon_block_header)
blck.body.build_proof( blck.body.build_proof(
27.GeneralizedIndex, KZG_COMMITMENTS_INCLUSION_PROOF_DEPTH_GINDEX.GeneralizedIndex,
sidecar.kzg_commitments_inclusion_proof).expect("Valid gindex") sidecar.kzg_commitments_inclusion_proof).expect("Valid gindex")
sidecars.add(sidecar) sidecars.add(sidecar)
ok(sidecars) ok(sidecars)
# Helper function to `verifyCellKzgProofBatch` at https://github.com/ethereum/c-kzg-4844/blob/das/bindings/nim/kzg_ex.nim#L170 # Helper function to `verifyCellKzgProofBatch` at https://github.com/ethereum/c-kzg-4844/blob/das/bindings/nim/kzg_ex.nim#L170