Skip to content

Commit

Permalink
chore!: Refactor RecoverBlob to RecoverCellsAndProofs (#14160)
Browse files Browse the repository at this point in the history
* change recoverBlobs to recoverCellsAndProofs

* modify code to take in the cells and proofs for a particular blob instead of the blob itself

* add CellsAndProofs structure

* modify recoverCellsAndProofs to return `cellsAndProofs` structure

* modify `DataColumnSidecarsForReconstruct` to accept the `cellsAndKZGProofs` structure

* bazel run //:gazelle -- fix

* use kzg abstraction for kzg method

* move CellsAndProofs to kzg.go
  • Loading branch information
kevaundray committed Jul 3, 2024
1 parent b8f43c0 commit 6387b51
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
7 changes: 7 additions & 0 deletions beacon-chain/blockchain/kzg/kzg.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ const CellsPerExtBlob = ckzg4844.CellsPerExtBlob
// TODO: Note that callers of this package rely on `BytesPerCell`
type Cell ckzg4844.Cell

// CellsAndProofs represents the Cells and Proofs corresponding to
// a single blob.
type CellsAndProofs struct {
Cells [ckzg4844.CellsPerExtBlob]Cell
Proofs [ckzg4844.CellsPerExtBlob]Proof
}

func BlobToKZGCommitment(blob *Blob) (Commitment, error) {
comm, err := kzg4844.BlobToCommitment(kzg4844.Blob(*blob))
if err != nil {
Expand Down
31 changes: 11 additions & 20 deletions beacon-chain/core/peerdas/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/holiman/uint256"
errors "github.com/pkg/errors"

"github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/kzg"
kzg "github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/kzg"

"github.com/prysmaticlabs/prysm/v5/cmd/beacon-chain/flags"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
Expand Down Expand Up @@ -205,39 +206,29 @@ func DataColumnSidecarsForReconstruct(
blobKzgCommitments [][]byte,
signedBlockHeader *ethpb.SignedBeaconBlockHeader,
kzgCommitmentsInclusionProof [][]byte,
blobs []kzg.Blob,
cellsAndProofs []kzg.CellsAndProofs,
) ([]*ethpb.DataColumnSidecar, error) {
blobsCount := len(blobs)
// Each CellsAndProofs corresponds to a Blob
// So we can get the BlobCount by checking the length of CellsAndProofs
blobsCount := len(cellsAndProofs)
if blobsCount == 0 {
return nil, nil
}

// Compute cells and proofs.
cells := make([][kzg.CellsPerExtBlob]kzg.Cell, 0, blobsCount)
proofs := make([][kzg.CellsPerExtBlob]kzg.Proof, 0, blobsCount)

for i := range blobs {
blob := &blobs[i]
blobCells, blobProofs, err := kzg.ComputeCellsAndKZGProofs(blob)
if err != nil {
return nil, errors.Wrap(err, "compute cells and KZG proofs")
}

cells = append(cells, blobCells)
proofs = append(proofs, blobProofs)
}

// Get the column sidecars.
sidecars := make([]*ethpb.DataColumnSidecar, 0, kzg.CellsPerExtBlob)
for columnIndex := uint64(0); columnIndex < kzg.CellsPerExtBlob; columnIndex++ {
column := make([]kzg.Cell, 0, blobsCount)
kzgProofOfColumn := make([]kzg.Proof, 0, blobsCount)

for rowIndex := 0; rowIndex < blobsCount; rowIndex++ {
cell := cells[rowIndex][columnIndex]
cellsForRow := cellsAndProofs[rowIndex].Cells
proofsForRow := cellsAndProofs[rowIndex].Proofs

cell := cellsForRow[columnIndex]
column = append(column, cell)

kzgProof := proofs[rowIndex][columnIndex]
kzgProof := proofsForRow[columnIndex]
kzgProofOfColumn = append(kzgProofOfColumn, kzgProof)
}

Expand Down
33 changes: 21 additions & 12 deletions beacon-chain/sync/data_columns_reconstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/kzg"
kzg "github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/kzg"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/peerdas"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/config/params"
Expand All @@ -20,12 +20,12 @@ import (

const broadCastMissingDataColumnsTimeIntoSlot = 3 * time.Second

// recoverBlobs recovers the blobs from the data column sidecars.
func recoverBlobs(
// recoverCellsAndProofs recovers the cells and proofs from the data column sidecars.
func recoverCellsAndProofs(
dataColumnSideCars []*ethpb.DataColumnSidecar,
columnsCount int,
blockRoot [fieldparams.RootLength]byte,
) ([]kzg.Blob, error) {
) ([]kzg.CellsAndProofs, error) {
if len(dataColumnSideCars) == 0 {
return nil, errors.New("no data column sidecars")
}
Expand All @@ -40,7 +40,8 @@ func recoverBlobs(
}
}

recoveredBlobs := make([]kzg.Blob, 0, blobCount)
// Recover cells and compute proofs.
recoveredCellsAndProofs := make([]kzg.CellsAndProofs, 0, blobCount)

for blobIndex := 0; blobIndex < blobCount; blobIndex++ {
start := time.Now()
Expand Down Expand Up @@ -76,15 +77,23 @@ func recoverBlobs(
return nil, errors.Wrapf(err, "cells to blob for blob %d", blobIndex)
}

recoveredBlobs = append(recoveredBlobs, recoveredBlob)
blobCells, blobProofs, err := kzg.ComputeCellsAndKZGProofs(&recoveredBlob)
if err != nil {
return nil, errors.Wrapf(err, "compute cells and KZG proofs for blob %d", blobIndex)
}
recoveredCellsAndProofs = append(recoveredCellsAndProofs, kzg.CellsAndProofs{
Cells: blobCells,
Proofs: blobProofs,
})

log.WithFields(logrus.Fields{
"elapsed": time.Since(start),
"index": blobIndex,
"root": fmt.Sprintf("%x", blockRoot),
}).Debug("Recovered blob")
}).Debug("Recovered cells and proofs")
}

return recoveredBlobs, nil
return recoveredCellsAndProofs, nil
}

func (s *Service) reconstructDataColumns(ctx context.Context, verifiedRODataColumn blocks.VerifiedRODataColumn) error {
Expand Down Expand Up @@ -127,18 +136,18 @@ func (s *Service) reconstructDataColumns(ctx context.Context, verifiedRODataColu
dataColumnSideCars = append(dataColumnSideCars, dataColumnSidecar)
}

// Recover blobs.
recoveredBlobs, err := recoverBlobs(dataColumnSideCars, storedColumnsCount, blockRoot)
// Recover cells and proofs
recoveredCellsAndProofs, err := recoverCellsAndProofs(dataColumnSideCars, storedColumnsCount, blockRoot)
if err != nil {
return errors.Wrap(err, "recover blobs")
return errors.Wrap(err, "recover cells and proofs")
}

// Reconstruct the data columns sidecars.
dataColumnSidecars, err := peerdas.DataColumnSidecarsForReconstruct(
verifiedRODataColumn.KzgCommitments,
verifiedRODataColumn.SignedBlockHeader,
verifiedRODataColumn.KzgCommitmentsInclusionProof,
recoveredBlobs,
recoveredCellsAndProofs,
)
if err != nil {
return errors.Wrap(err, "data column sidecars")
Expand Down

0 comments on commit 6387b51

Please sign in to comment.