Skip to content

Commit

Permalink
added block header verification in sidecars
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Oct 28, 2024
1 parent 16540c0 commit b0a59d9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
6 changes: 4 additions & 2 deletions mod/da/pkg/blob/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Processor[
// chainSpec defines the specifications of the blockchain.
chainSpec common.ChainSpec
// verifier is responsible for verifying the blobs.
verifier BlobVerifier[BlobSidecarsT]
verifier BlobVerifier[BlobSidecarsT, BeaconBlockHeaderT]
// blockBodyOffsetFn is a function that calculates the block body offset
// based on the slot and chain specifications.
blockBodyOffsetFn func(math.Slot, common.ChainSpec) uint64
Expand All @@ -66,7 +66,7 @@ func NewProcessor[
](
logger log.Logger,
chainSpec common.ChainSpec,
verifier BlobVerifier[BlobSidecarsT],
verifier BlobVerifier[BlobSidecarsT, BeaconBlockHeaderT],
blockBodyOffsetFn func(math.Slot, common.ChainSpec) uint64,
telemetrySink TelemetrySink,
) *Processor[
Expand Down Expand Up @@ -94,6 +94,7 @@ func (sp *Processor[
var (
startTime = time.Now()
sidecars = cs.GetSidecars()
blkHeader = cs.GetHeader()
)
defer sp.metrics.measureVerifySidecarsDuration(
startTime, math.U64(sidecars.Len()),
Expand All @@ -111,6 +112,7 @@ func (sp *Processor[
sidecars.Get(0).GetBeaconBlockHeader().GetSlot(),
sp.chainSpec,
),
blkHeader,
)
}

Expand Down
8 changes: 6 additions & 2 deletions mod/da/pkg/blob/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ type BeaconBlockHeader interface {
}

//nolint:revive // name conflict
type BlobVerifier[BlobSidecarsT any] interface {
type BlobVerifier[BlobSidecarsT, BeaconBlockHeaderT any] interface {
VerifyInclusionProofs(scs BlobSidecarsT, kzgOffset uint64) error
VerifyKZGProofs(scs BlobSidecarsT) error
VerifySidecars(sidecars BlobSidecarsT, kzgOffset uint64) error
VerifySidecars(
sidecars BlobSidecarsT,
kzgOffset uint64,
blkHeader BeaconBlockHeaderT,
) error
}

type ConsensusSidecars[BlobSidecarsT any, BeaconBlockHeaderT any] interface {
Expand Down
12 changes: 11 additions & 1 deletion mod/da/pkg/blob/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package blob

import (
"context"
"fmt"
"reflect"
"time"

"github.com/berachain/beacon-kit/mod/da/pkg/kzg"
Expand Down Expand Up @@ -60,7 +62,9 @@ func NewVerifier[
// VerifySidecars verifies the blobs for both inclusion as well
// as the KZG proofs.
func (bv *Verifier[_, _, BlobSidecarsT]) VerifySidecars(
sidecars BlobSidecarsT, kzgOffset uint64,
sidecars BlobSidecarsT,
kzgOffset uint64,
blkHeader BeaconBlockHeader,
) error {
var (
g, _ = errgroup.WithContext(context.Background())
Expand All @@ -72,6 +76,12 @@ func (bv *Verifier[_, _, BlobSidecarsT]) VerifySidecars(
bv.proofVerifier.GetImplementation(),
)

for i, s := range sidecars.GetSidecars() {
if !reflect.DeepEqual(s, blkHeader) {
return fmt.Errorf("unequal block header: idx %d", i)
}
}

// Verify the inclusion proofs on the blobs concurrently.
g.Go(func() error {
// TODO: KZGOffset needs to be configurable and not
Expand Down
5 changes: 3 additions & 2 deletions mod/node-core/pkg/components/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ func ProvideBlobVerifier[
// BlobProcessorIn is the input for the BlobProcessor.
type BlobProcessorIn[
BlobSidecarsT any,
BeaconBlockHeaderT any,
LoggerT any,
] struct {
depinject.In

BlobVerifier BlobVerifier[BlobSidecarsT]
BlobVerifier BlobVerifier[BlobSidecarsT, BeaconBlockHeaderT]
ChainSpec common.ChainSpec
Logger LoggerT
TelemetrySink *metrics.TelemetrySink
Expand All @@ -101,7 +102,7 @@ func ProvideBlobProcessor[
BlobSidecarsT BlobSidecars[BlobSidecarsT, BlobSidecarT],
LoggerT log.AdvancedLogger[LoggerT],
](
in BlobProcessorIn[BlobSidecarsT, LoggerT],
in BlobProcessorIn[BlobSidecarsT, BeaconBlockHeaderT, LoggerT],
) *dablob.Processor[
AvailabilityStoreT, BeaconBlockBodyT, BeaconBlockHeaderT,
ConsensusSidecarsT, BlobSidecarT, BlobSidecarsT,
Expand Down
10 changes: 7 additions & 3 deletions mod/node-core/pkg/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ type (
// BeaconStateMarshallable represents an interface for a beacon state
// with generic types.
BeaconStateMarshallable[
T any,
T,
BeaconBlockHeaderT,
Eth1DataT,
ExecutionPayloadHeaderT,
Expand Down Expand Up @@ -270,10 +270,14 @@ type (
VerifyInclusionProofs(kzgOffset uint64) error
}

BlobVerifier[BlobSidecarsT any] interface {
BlobVerifier[BlobSidecarsT, BeaconBlockHeaderT any] interface {
VerifyInclusionProofs(scs BlobSidecarsT, kzgOffset uint64) error
VerifyKZGProofs(scs BlobSidecarsT) error
VerifySidecars(sidecars BlobSidecarsT, kzgOffset uint64) error
VerifySidecars(
sidecars BlobSidecarsT,
kzgOffset uint64,
blkHeader BeaconBlockHeaderT,
) error
}

// // BlockchainService defines the interface for interacting with the
Expand Down

0 comments on commit b0a59d9

Please sign in to comment.