2020-10-03 01:51:58 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/d4l3k/messagediff"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
pb "github.com/libp2p/go-libp2p-pubsub/pb"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
|
|
p2ptesting "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-10-03 01:51:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-10-03 01:51:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_decodePubsubMessage(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2020-10-16 07:05:40 +00:00
|
|
|
topic string
|
2020-10-03 01:51:58 +00:00
|
|
|
input *pubsub.Message
|
|
|
|
want proto.Message
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Nil message",
|
|
|
|
input: nil,
|
|
|
|
wantErr: errNilPubsubMessage,
|
|
|
|
},
|
|
|
|
{
|
2020-10-16 07:05:40 +00:00
|
|
|
name: "nil topic",
|
2020-10-03 01:51:58 +00:00
|
|
|
input: &pubsub.Message{
|
|
|
|
Message: &pb.Message{
|
2020-10-16 07:05:40 +00:00
|
|
|
Topic: nil,
|
2020-10-03 01:51:58 +00:00
|
|
|
},
|
|
|
|
},
|
2020-10-16 07:05:40 +00:00
|
|
|
wantErr: errNilPubsubMessage,
|
2020-10-03 01:51:58 +00:00
|
|
|
},
|
|
|
|
{
|
2020-10-16 07:05:40 +00:00
|
|
|
name: "invalid topic format",
|
|
|
|
topic: "foo",
|
2020-10-03 01:51:58 +00:00
|
|
|
wantErr: errInvalidTopic,
|
|
|
|
},
|
|
|
|
{
|
2020-10-16 07:05:40 +00:00
|
|
|
name: "topic not mapped to any message type",
|
|
|
|
topic: "/eth2/abcdef/foo",
|
2020-10-03 01:51:58 +00:00
|
|
|
wantErr: p2p.ErrMessageNotMapped,
|
|
|
|
},
|
|
|
|
{
|
2020-10-16 07:05:40 +00:00
|
|
|
name: "valid message -- beacon block",
|
|
|
|
topic: p2p.GossipTypeMapping[reflect.TypeOf(ðpb.SignedBeaconBlock{})],
|
2020-10-03 01:51:58 +00:00
|
|
|
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{
|
2021-03-21 19:07:42 +00:00
|
|
|
cfg: &Config{P2P: p2ptesting.NewTestP2P(t)},
|
2020-10-03 01:51:58 +00:00
|
|
|
}
|
2020-10-16 07:05:40 +00:00
|
|
|
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
|
|
|
|
}
|
2020-10-03 01:51:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|