Skip to content

Commit

Permalink
unit test coverage on verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
kasey committed Feb 14, 2024
1 parent b1980b6 commit bbf2391
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
1 change: 1 addition & 0 deletions beacon-chain/sync/backfill/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_test(
srcs = [
"batch_test.go",
"batcher_test.go",
"blobs_test.go",
"pool_test.go",
"service_test.go",
"status_test.go",
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/sync/backfill/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ type blobSyncConfig struct {
store *filesystem.BlobStorage
}

func newBlobSync(cs primitives.Slot, vbs verifiedROBlocks, cfg *blobSyncConfig) (*blobSync, error) {
func newBlobSync(current primitives.Slot, vbs verifiedROBlocks, cfg *blobSyncConfig) (*blobSync, error) {
expected, err := vbs.blobIdents(cfg.retentionStart)
if err != nil {
return nil, err
}
bbv := newBlobBatchVerifier(cfg.nbv)
as := das.NewLazilyPersistentStore(cfg.store, bbv)
return &blobSync{cs: cs, expected: expected, bbv: bbv, store: as}, nil
return &blobSync{current: current, expected: expected, bbv: bbv, store: as}, nil
}

type blobVerifierMap map[[32]byte][fieldparams.MaxBlobsPerBlock]verification.BlobVerifier
Expand All @@ -51,7 +51,7 @@ type blobSync struct {
expected []blobSummary
next int
bbv *blobBatchVerifier
cs primitives.Slot
current primitives.Slot
}

func (bs *blobSync) blobsNeeded() int {
Expand Down Expand Up @@ -89,7 +89,7 @@ func (bs *blobSync) validateNext(rb blocks.ROBlob) error {
if err := v.SidecarKzgProofVerified(); err != nil {
return err
}
if err := bs.store.Persist(bs.cs, rb); err != nil {
if err := bs.store.Persist(bs.current, rb); err != nil {
return err
}

Expand Down
128 changes: 128 additions & 0 deletions beacon-chain/sync/backfill/blobs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package backfill

import (
"testing"

"github.com/prysmaticlabs/prysm/v4/beacon-chain/db/filesystem"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/verification"
"github.com/prysmaticlabs/prysm/v4/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v4/testing/require"
"github.com/prysmaticlabs/prysm/v4/testing/util"
)

func testBlobGen(t *testing.T, start primitives.Slot, n int) ([]blocks.ROBlock, [][]blocks.ROBlob) {
blks := make([]blocks.ROBlock, n)
blobs := make([][]blocks.ROBlob, n)
for i := 0; i < n; i++ {
bk, bl := util.GenerateTestDenebBlockWithSidecar(t, [32]byte{}, start+primitives.Slot(i), 3)
blks[i] = bk
blobs[i] = bl
}
return blks, blobs
}

func TestValidateNext_happy(t *testing.T) {
current := primitives.Slot(128)
blks, blobs := testBlobGen(t, 63, 4)
cfg := &blobSyncConfig{
retentionStart: 0,
nbv: testNewBlobVerifier(),
store: filesystem.NewEphemeralBlobStorage(t),
}
bsync, err := newBlobSync(current, blks, cfg)
require.NoError(t, err)
nb := 0
for i := range blobs {
bs := blobs[i]
for ib := range bs {
require.NoError(t, bsync.validateNext(bs[ib]))
nb += 1
}
}
require.Equal(t, nb, bsync.next)
// we should get an error if we read another blob.
require.ErrorIs(t, bsync.validateNext(blobs[0][0]), errUnexpectedResponseSize)
}

func TestValidateNext_cheapErrors(t *testing.T) {
current := primitives.Slot(128)
blks, blobs := testBlobGen(t, 63, 2)
cfg := &blobSyncConfig{
retentionStart: 0,
nbv: testNewBlobVerifier(),
store: filesystem.NewEphemeralBlobStorage(t),
}
bsync, err := newBlobSync(current, blks, cfg)
require.NoError(t, err)
require.ErrorIs(t, bsync.validateNext(blobs[len(blobs)-1][0]), errUnexpectedResponseContent)
}

func TestValidateNext_sigMatch(t *testing.T) {
current := primitives.Slot(128)
blks, blobs := testBlobGen(t, 63, 1)
cfg := &blobSyncConfig{
retentionStart: 0,
nbv: testNewBlobVerifier(),
store: filesystem.NewEphemeralBlobStorage(t),
}
bsync, err := newBlobSync(current, blks, cfg)
require.NoError(t, err)
blobs[0][0].SignedBlockHeader.Signature = bytesutil.PadTo([]byte("derp"), 48)
require.ErrorIs(t, bsync.validateNext(blobs[0][0]), verification.ErrInvalidProposerSignature)
}

func TestValidateNext_errorsFromVerifier(t *testing.T) {
current := primitives.Slot(128)
blks, blobs := testBlobGen(t, 63, 1)
cases := []struct {
name string
err error
cb func(*verification.MockBlobVerifier)
}{
{
name: "index oob",
err: verification.ErrBlobIndexInvalid,
cb: func(v *verification.MockBlobVerifier) {
v.ErrBlobIndexInBounds = verification.ErrBlobIndexInvalid
},
},
{
name: "not inclusion proven",
err: verification.ErrSidecarInclusionProofInvalid,
cb: func(v *verification.MockBlobVerifier) {
v.ErrSidecarInclusionProven = verification.ErrSidecarInclusionProofInvalid
},
},
{
name: "not kzg proof valid",
err: verification.ErrSidecarKzgProofInvalid,
cb: func(v *verification.MockBlobVerifier) {
v.ErrSidecarKzgProofVerified = verification.ErrSidecarKzgProofInvalid
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
cfg := &blobSyncConfig{
retentionStart: 0,
nbv: testNewBlobVerifier(c.cb),
store: filesystem.NewEphemeralBlobStorage(t),
}
bsync, err := newBlobSync(current, blks, cfg)
require.NoError(t, err)
require.ErrorIs(t, bsync.validateNext(blobs[0][0]), c.err)
})
}
}

func testNewBlobVerifier(opts ...func(*verification.MockBlobVerifier)) verification.NewBlobVerifier {
return func(b blocks.ROBlob, reqs []verification.Requirement) verification.BlobVerifier {
v := &verification.MockBlobVerifier{}
for i := range opts {
opts[i](v)
}
return v
}
}

0 comments on commit bbf2391

Please sign in to comment.