prysm-pulse/beacon-chain/sync/decode_pubsub_test.go
Preston Van Loon 07e7e030d9
Update PubSub and include topic filter (#7496)
* Update pubsub and fix topicIDs

* WIP filter

* Add suggested code from @bidlocode

* add tests and fix bugs

* more tests

* Wait until state initialized to accept pubsub filtering

* rename for clarity and clarify comment

* fix test builds

* Autofix issues in 2 files

Resolved issues in the following files via DeepSource Autofix:
1. beacon-chain/p2p/pubsub_filter.go
2. beacon-chain/p2p/pubsub_filter_test.go

* @nisdas pr feedback

* pr feedback and fuzz fix

* Update beacon-chain/p2p/pubsub_filter.go

* Must have protocol suffix

* Must have protocol suffix

* gofmt

* rm test, fix panic

* Fix tests

* Add isInitialized check

* Add a few more tests for better coverage

* cache fork digest, make pubsub filter part of the p2p service

* rename service

* gofmt

* Add comment

* fix

Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-10-16 07:05:40 +00:00

94 lines
2.2 KiB
Go

package sync
import (
"bytes"
"reflect"
"testing"
"github.com/d4l3k/messagediff"
"github.com/gogo/protobuf/proto"
pubsub "github.com/libp2p/go-libp2p-pubsub"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
p2ptesting "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func TestService_decodePubsubMessage(t *testing.T) {
tests := []struct {
name string
topic string
input *pubsub.Message
want proto.Message
wantErr error
}{
{
name: "Nil message",
input: nil,
wantErr: errNilPubsubMessage,
},
{
name: "nil topic",
input: &pubsub.Message{
Message: &pb.Message{
Topic: nil,
},
},
wantErr: errNilPubsubMessage,
},
{
name: "invalid topic format",
topic: "foo",
wantErr: errInvalidTopic,
},
{
name: "topic not mapped to any message type",
topic: "/eth2/abcdef/foo",
wantErr: p2p.ErrMessageNotMapped,
},
{
name: "valid message -- beacon block",
topic: p2p.GossipTypeMapping[reflect.TypeOf(&ethpb.SignedBeaconBlock{})],
input: &pubsub.Message{
Message: &pb.Message{
Data: func() []byte {
buf := new(bytes.Buffer)
if _, err := p2ptesting.NewTestP2P(t).Encoding().EncodeGossip(buf, testutil.NewBeaconBlock()); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}(),
},
},
wantErr: nil,
want: testutil.NewBeaconBlock(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Service{
p2p: p2ptesting.NewTestP2P(t),
}
if tt.topic != "" {
if tt.input == nil {
tt.input = &pubsub.Message{Message: &pb.Message{}}
} else if tt.input.Message == nil {
tt.input.Message = &pb.Message{}
}
tt.input.Message.Topic = &tt.topic
}
got, err := s.decodePubsubMessage(tt.input)
if err != tt.wantErr {
t.Errorf("decodePubsubMessage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
diff, _ := messagediff.PrettyDiff(got, tt.want)
t.Log(diff)
t.Errorf("decodePubsubMessage() got = %v, want %v", got, tt.want)
}
})
}
}