diff --git a/beacon-chain/sync/BUILD.bazel b/beacon-chain/sync/BUILD.bazel index 3c4e99075..7118016bf 100644 --- a/beacon-chain/sync/BUILD.bazel +++ b/beacon-chain/sync/BUILD.bazel @@ -149,6 +149,7 @@ go_test( "subscriber_beacon_blocks_test.go", "subscriber_test.go", "subscription_topic_handler_test.go", + "sync_fuzz_test.go", "sync_test.go", "utils_test.go", "validate_aggregate_proof_test.go", diff --git a/beacon-chain/sync/sync_fuzz_test.go b/beacon-chain/sync/sync_fuzz_test.go new file mode 100644 index 000000000..144e74092 --- /dev/null +++ b/beacon-chain/sync/sync_fuzz_test.go @@ -0,0 +1,56 @@ +//go:build go1.18 +// +build go1.18 + +package sync + +import ( + "context" + "fmt" + "testing" + "time" + + "github.com/libp2p/go-libp2p-core/peer" + pubsub "github.com/libp2p/go-libp2p-pubsub" + pb "github.com/libp2p/go-libp2p-pubsub/pb" + gcache "github.com/patrickmn/go-cache" + chainMock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" + "github.com/prysmaticlabs/prysm/beacon-chain/p2p" + "github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder" + p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" + mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing" + ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" +) + +func FuzzValidateBeaconBlockPubSub(f *testing.F) { + ctx, cancel := context.WithCancel(context.Background()) + r := &Service{ + cfg: &config{ + initialSync: &mockSync.Sync{IsSyncing: false}, + chain: &chainMock.ChainService{}, + }, + ctx: ctx, + cancel: cancel, + slotToPendingBlocks: gcache.New(time.Second, 2*time.Second), + seenPendingBlocks: make(map[[32]byte]bool), + blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof), + } + validTopic := fmt.Sprintf(p2p.BlockSubnetTopicFormat, []byte{0xb5, 0x30, 0x3f, 0x2a}) + "/" + encoder.ProtocolSuffixSSZSnappy + f.Add("junk", []byte("junk"), []byte("junk"), []byte("junk"), []byte(validTopic), []byte("junk"), []byte("junk")) + f.Fuzz(func(t *testing.T, pid string, from, data, seqno, topic, signature, key []byte) { + r.cfg.p2p = p2ptest.NewFuzzTestP2P() + r.rateLimiter = newRateLimiter(r.cfg.p2p) + strTop := string(topic) + msg := &pubsub.Message{ + Message: &pb.Message{ + From: from, + Data: data, + Seqno: seqno, + Topic: &strTop, + Signature: signature, + Key: key, + }, + } + _, err := r.validateBeaconBlockPubSub(ctx, peer.ID(pid), msg) + _ = err + }) +}