Compare commits

...

1 Commits

Author SHA1 Message Date
terence tsao
9ca6b9b30d EIP4844: crypto stubs 2023-03-22 13:23:03 -07:00
2 changed files with 41 additions and 1 deletions

View File

@@ -715,3 +715,39 @@ func (s *Service) fillMissingBlockPayloadId(ctx context.Context) error {
})
return err
}
// validateBlobs validates the blobs with the kzg commitments in block.
// Spec code:
// def validate_blobs(expected_kzg_commitments: Sequence[KZGCommitment],
//
// blobs: Sequence[Blob],
// proofs: Sequence[KZGProof]) -> None:
// assert len(expected_kzg_commitments) == len(blobs)
// assert len(blobs) == len(proofs)
//
// assert verify_blob_kzg_proof_batch(blobs, expected_kzg_commitments, proofs)
func (s *Service) validateBlobs(ctx context.Context, blk interfaces.ReadOnlyBeaconBlock) error {
if blk.Version() < version.Deneb {
return nil
}
comms, err := blk.Body().BlobKzgCommitments()
if err != nil {
return err
}
if len(comms) == 0 {
return nil
}
scs, err := s.cfg.BeaconDB.BlobSidecarsBySlot(ctx, blk.Slot()) // Also can use ByRoot here.
if err != nil {
return err
}
if len(scs.Sidecars) != len(comms) {
return fmt.Errorf("number of sidecars %d does not match number of commitments %d", len(scs.Sidecars), len(comms))
}
proofs := make([][]byte, len(comms))
for i, sc := range scs.Sidecars {
proofs[i] = sc.KzgProof
}
// TODO(Potuz): assert verify_blob_kzg_proof_batch(blobs, expected_kzg_commitments, proofs)
return nil
}

View File

@@ -154,7 +154,6 @@ func (vs *Server) GetBeaconBlock(ctx context.Context, req *ethpb.BlockRequest) (
return nil, status.Errorf(codes.Internal, "Could not get block root: %v", err)
}
// TODO: Better error handling. If something is wrong with the blob, we don't want to fail block production. Also should check if the kzg commitment matches.
validatorBlobs := make([]*ethpb.BlobSidecar, len(blk.Body.BlobKzgCommitments))
var gethBlobs types.Blobs
for _, b := range blobs {
@@ -162,6 +161,11 @@ func (vs *Server) GetBeaconBlock(ctx context.Context, req *ethpb.BlockRequest) (
copy(gethBlob[:], b.Data)
gethBlobs = append(gethBlobs, gethBlob)
}
// TODO(Potuz):
// - Currently using `ComputeCommitmentsAndProofs` from Geth. Is this ok?
// - Add a defensive check if block's kzg commitments align with blobs. If fails, we continue and skip blob.
commitments, _, proofs, err := gethBlobs.ComputeCommitmentsAndProofs()
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not compute commitments and proofs: %v", err)