2020-07-27 00:56:55 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2020-10-16 07:05:40 +00:00
|
|
|
"time"
|
2020-07-27 00:56:55 +00:00
|
|
|
|
2020-10-24 03:38:05 +00:00
|
|
|
"github.com/golang/snappy"
|
2020-09-06 18:05:16 +00:00
|
|
|
pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
|
2020-10-16 07:05:40 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2020-10-23 01:53:50 +00:00
|
|
|
testp2p "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
2020-09-06 18:05:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2020-10-24 03:38:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-07-27 00:56:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_PublishToTopicConcurrentMapWrite(t *testing.T) {
|
2021-02-12 17:45:22 +00:00
|
|
|
s, err := NewService(context.Background(), &Config{
|
2020-10-16 07:05:40 +00:00
|
|
|
StateNotifier: &mock.MockStateNotifier{},
|
|
|
|
})
|
2020-07-27 00:56:55 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-16 07:05:40 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
2020-07-27 00:56:55 +00:00
|
|
|
defer cancel()
|
2020-10-16 07:05:40 +00:00
|
|
|
|
|
|
|
go s.awaitStateInitialized()
|
|
|
|
fd := initializeStateWithForkDigest(ctx, t, s.stateNotifier.StateFeed())
|
|
|
|
|
|
|
|
if !s.isInitialized() {
|
|
|
|
t.Fatal("service was not initialized")
|
|
|
|
}
|
|
|
|
|
2020-10-23 01:53:50 +00:00
|
|
|
// Set up two connected test hosts.
|
|
|
|
p0 := testp2p.NewTestP2P(t)
|
|
|
|
p1 := testp2p.NewTestP2P(t)
|
|
|
|
p0.Connect(p1)
|
|
|
|
s.host = p0.BHost
|
|
|
|
s.pubsub = p0.PubSub()
|
|
|
|
|
|
|
|
topic := fmt.Sprintf(BlockSubnetTopicFormat, fd) + "/" + encoder.ProtocolSuffixSSZSnappy
|
|
|
|
|
|
|
|
// Establish the remote peer to be subscribed to the outgoing topic.
|
|
|
|
_, err = p1.SubscribeToTopic(topic)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-07-27 00:56:55 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(10)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
go func(i int) {
|
2020-10-16 07:05:40 +00:00
|
|
|
assert.NoError(t, s.PublishToTopic(ctx, topic, []byte{}))
|
2020-07-27 00:56:55 +00:00
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2020-09-06 18:05:16 +00:00
|
|
|
|
|
|
|
func TestMessageIDFunction_HashesCorrectly(t *testing.T) {
|
2020-10-24 03:38:05 +00:00
|
|
|
invalidSnappy := [32]byte{'J', 'U', 'N', 'K'}
|
|
|
|
pMsg := &pubsubpb.Message{Data: invalidSnappy[:]}
|
|
|
|
hashedData := hashutil.Hash(append(params.BeaconNetworkConfig().MessageDomainInvalidSnappy[:], pMsg.Data...))
|
|
|
|
msgID := string(hashedData[:20])
|
2020-09-06 18:05:16 +00:00
|
|
|
assert.Equal(t, msgID, msgIDFunction(pMsg), "Got incorrect msg id")
|
2020-10-24 03:38:05 +00:00
|
|
|
|
|
|
|
validObj := [32]byte{'v', 'a', 'l', 'i', 'd'}
|
|
|
|
enc := snappy.Encode(nil, validObj[:])
|
|
|
|
nMsg := &pubsubpb.Message{Data: enc}
|
|
|
|
hashedData = hashutil.Hash(append(params.BeaconNetworkConfig().MessageDomainValidSnappy[:], validObj[:]...))
|
|
|
|
msgID = string(hashedData[:20])
|
|
|
|
assert.Equal(t, msgID, msgIDFunction(nMsg), "Got incorrect msg id")
|
2020-09-06 18:05:16 +00:00
|
|
|
}
|