add: blob recovery logic

This commit is contained in:
Agnish Ghosh 2024-06-26 16:05:58 +05:30
parent 0b4cf1017b
commit 34a2478113
No known key found for this signature in database
GPG Key ID: 7BDDA05D1B25E9F8
1 changed files with 53 additions and 0 deletions

View File

@ -140,6 +140,59 @@ proc recover_matrix*(partial_matrix: seq[MatrixEntry], blobCount: int): Result[s
cells.add(e.cell)
proofs.add(e.kzg_proof)
proc recover_blobs*(
data_columns: seq[DataColumnSidecar],
columnCount: int,
blck: deneb.SignedBeaconBlock |
electra.SignedBeaconBlock |
ForkySignedBeaconBlock):
Result[seq[KzgBlob], cstring] =
# This helper recovers blobs from the data column sidecars
if not (data_columns.len != 0):
return err("DataColumnSidecar: Length should not be 0")
var blobCount = data_columns[0].len
for data_column in data_columns:
if not (blobCount == data_column.column.len):
return err ("DataColumns do not have the same length")
var recovered_blobs = newSeqOfCap[KzgBlob](blobCount)
for blobIdx in 0 ..< blobCount:
var
cell_ids = newSeqOfCap[CellID](columnCount)
ckzgCells = newSeqOfCap[KzgCell](columnCount)
for data_column in data_columns:
cell_ids.add(data_column.index)
let
column = data_column.column
cell = column[blobIdx]
# Transform the cell as a ckzg cell
var ckzgCell: Cell
for i in 0 ..< int(FIELD_ELEMENTS_PER_CELL):
ckzgCell[i] = cell[32*i ..< 32*(i+1)].toArray()
ckzgCells.add(ckzgCell)
# Recovering the blob
let recovered_cells = recoverAllCells(cell_ids, ckzgCells)
if not recovered_cells.isOk:
return err (fmt"Recovering all cells for blob - {blobIdx} failed: {recovered_cells.error}")
let recovered_blob_res = cellsToBlob(recovered_cells.get)
if not recovered_blob_res.isOk:
return err (fmt"Cells to blob for blob - {blobIdx} failed: {recovered_blob_res.error}")
recovered_blobs.add(recovered_blob_res.get)
ok(recovered_blobs)
# https://github.com/ethereum/consensus-specs/blob/5f48840f4d768bf0e0a8156a3ed06ec333589007/specs/_features/eip7594/das-core.md#get_data_column_sidecars
proc get_data_column_sidecars*(signed_block: deneb.SignedBeaconBlock | electra.SignedBeaconBlock | ForkySignedBeaconBlock, blobs: seq[KzgBlob]): Result[seq[DataColumnSidecar], cstring] =
var