Skip to content

Commit

Permalink
remove commitments from consensusSidecars and do comparison early
Browse files Browse the repository at this point in the history
  • Loading branch information
shotes committed Dec 18, 2024
1 parent 7cfaea4 commit 0b44fab
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 40 deletions.
26 changes: 17 additions & 9 deletions beacon/blockchain/process_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package blockchain

import (
"context"
"fmt"
"time"

payloadtime "github.com/berachain/beacon-kit/beacon/payload-time"
Expand Down Expand Up @@ -83,21 +84,28 @@ func (s *Service[
return createProcessProposalResponse(errors.WrapNonFatal(ErrNilBlob))
}

// Process the blob sidecars
//
// In theory, swapping the order of verification between the sidecars
// and the incoming block should not introduce any inconsistencies
// in the state on which the sidecar verification depends on (notably
// the currently active fork). ProcessProposal should only
// keep the state changes as candidates (which is what we do in
// VerifyIncomingBlock).
// Make sure we have the right number of BlobSidecars
kzgCommitments := blk.GetBody().GetBlobKzgCommitments()
if len(kzgCommitments) != sidecars.Len() {
err = fmt.Errorf("expected %d sidecars, got %d",
len(kzgCommitments), sidecars.Len(),
)
return createProcessProposalResponse(errors.WrapNonFatal(err))
}

if len(kzgCommitments) > 0 {
// Process the blob sidecars
//
// In theory, swapping the order of verification between the sidecars
// and the incoming block should not introduce any inconsistencies
// in the state on which the sidecar verification depends on (notably
// the currently active fork). ProcessProposal should only
// keep the state changes as candidates (which is what we do in
// VerifyIncomingBlock).
var consensusSidecars *types.ConsensusSidecars[BlobSidecarsT]
consensusSidecars = consensusSidecars.New(
sidecars,
blk.GetHeader(),
kzgCommitments,
)
err = s.VerifyIncomingBlobSidecars(
ctx,
Expand Down
16 changes: 4 additions & 12 deletions consensus/types/consensus_sidecars.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,21 @@ package types

import (
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/primitives/eip4844"
)

type ConsensusSidecars[SidecarsT any] struct {
sidecars SidecarsT
blkHeader *ctypes.BeaconBlockHeader
kzgCommitments []eip4844.KZGCommitment
sidecars SidecarsT
blkHeader *ctypes.BeaconBlockHeader
}

// New creates a new ConsensusSidecars instance.
func (s *ConsensusSidecars[SidecarsT]) New(
sidecars SidecarsT,
blkHeader *ctypes.BeaconBlockHeader,
kzgCommitments []eip4844.KZGCommitment,
) *ConsensusSidecars[SidecarsT] {
return &ConsensusSidecars[SidecarsT]{
sidecars: sidecars,
blkHeader: blkHeader,
kzgCommitments: kzgCommitments,
sidecars: sidecars,
blkHeader: blkHeader,
}
}

Expand All @@ -51,7 +47,3 @@ func (s *ConsensusSidecars[SidecarsT]) GetSidecars() SidecarsT {
func (s *ConsensusSidecars[SidecarsT]) GetHeader() *ctypes.BeaconBlockHeader {
return s.blkHeader
}

func (s *ConsensusSidecars[SidecarsT]) GetKzgCommitments() []eip4844.KZGCommitment {
return s.kzgCommitments
}
6 changes: 2 additions & 4 deletions da/blob/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ func (sp *Processor[
) error,
) error {
var (
sidecars = cs.GetSidecars()
blkHeader = cs.GetHeader()
kzgCommitments = cs.GetKzgCommitments()
sidecars = cs.GetSidecars()
blkHeader = cs.GetHeader()
)
defer sp.metrics.measureVerifySidecarsDuration(
time.Now(), math.U64(sidecars.Len()),
Expand All @@ -111,7 +110,6 @@ func (sp *Processor[
sidecars,
blkHeader,
verifierFn,
kzgCommitments,
)
}

Expand Down
1 change: 0 additions & 1 deletion da/blob/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type BeaconBlock interface {
type ConsensusSidecars[BlobSidecarsT any] interface {
GetSidecars() BlobSidecarsT
GetHeader() *ctypes.BeaconBlockHeader
GetKzgCommitments() []eip4844.KZGCommitment
}

type Sidecar interface {
Expand Down
12 changes: 1 addition & 11 deletions da/blob/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func (bv *verifier[_, BlobSidecarsT]) verifySidecars(
blkHeader *ctypes.BeaconBlockHeader,
signature crypto.BLSSignature,
) error,
kzgCommitments []eip4844.KZGCommitment,
) error {
defer bv.metrics.measureVerifySidecarsDuration(
time.Now(), math.U64(sidecars.Len()),
Expand All @@ -83,17 +82,8 @@ func (bv *verifier[_, BlobSidecarsT]) verifySidecars(

g, _ := errgroup.WithContext(context.Background())

// Ensure we have the same number of blobSidecars as we do KzgCommitments
// in the BeaconBlock.
numCommitments := len(kzgCommitments)
if numCommitments != sidecars.Len() {
return fmt.Errorf("expected %d sidecars, got %d",
numCommitments, sidecars.Len(),
)
}

// create lookup table to check for duplicate commitments
duplicateCommitment := make(map[eip4844.KZGCommitment]struct{}, numCommitments)
duplicateCommitment := make(map[eip4844.KZGCommitment]struct{})

// Validate sidecar fields against data from the BeaconBlock.
for i, s := range sidecars.GetSidecars() {
Expand Down
2 changes: 0 additions & 2 deletions da/da/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package da
import (
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/primitives/crypto"
"github.com/berachain/beacon-kit/primitives/eip4844"
)

// BlobProcessor is the interface for the blobs processor.
Expand Down Expand Up @@ -52,7 +51,6 @@ type BlobProcessor[
type ConsensusSidecars[BlobSidecarsT any] interface {
GetSidecars() BlobSidecarsT
GetHeader() *ctypes.BeaconBlockHeader
GetKzgCommitments() []eip4844.KZGCommitment
}

// BlobSidecar is the interface for the blob sidecar.
Expand Down
1 change: 0 additions & 1 deletion node-core/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ type (
] interface {
GetSidecars() BlobSidecarsT
GetHeader() *ctypes.BeaconBlockHeader
GetKzgCommitments() []eip4844.KZGCommitment
}

// BlobSidecars is the interface for blobs sidecars.
Expand Down

0 comments on commit 0b44fab

Please sign in to comment.