2019-08-16 17:13:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
2019-08-16 20:03:11 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2020-04-02 02:09:54 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2020-07-13 01:20:53 +00:00
|
|
|
prysmP2P "github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2019-08-30 20:15:40 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2019-08-16 17:13:04 +00:00
|
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
2020-10-14 07:55:28 +00:00
|
|
|
p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-08-16 17:13:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-25 16:53:41 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-08-16 17:13:04 +00:00
|
|
|
)
|
|
|
|
|
2020-04-02 02:09:54 +00:00
|
|
|
func init() {
|
|
|
|
state.SkipSlotCache.Disable()
|
|
|
|
}
|
|
|
|
|
2019-08-16 20:03:11 +00:00
|
|
|
// expectSuccess status code from a stream in regular sync.
|
2020-10-12 08:11:05 +00:00
|
|
|
func expectSuccess(t *testing.T, stream network.Stream) {
|
2019-08-30 20:15:40 +00:00
|
|
|
code, errMsg, err := ReadStatusCode(stream, &encoder.SszNetworkEncoder{})
|
2020-07-25 16:53:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint8(0), code, "Received non-zero response code")
|
|
|
|
require.Equal(t, "", errMsg, "Received error message from stream")
|
|
|
|
}
|
|
|
|
|
|
|
|
// expectSuccess status code from a stream in regular sync.
|
|
|
|
func expectFailure(t *testing.T, expectedCode uint8, expectedErrorMsg string, stream network.Stream) {
|
|
|
|
code, errMsg, err := ReadStatusCode(stream, &encoder.SszNetworkEncoder{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, uint8(0), code, "Expected request to fail but got a 0 response code")
|
2020-10-12 08:11:05 +00:00
|
|
|
require.Equal(t, expectedCode, code, "Received incorrect response code")
|
2020-07-25 16:53:41 +00:00
|
|
|
require.Equal(t, expectedErrorMsg, errMsg)
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 16:53:38 +00:00
|
|
|
// expectResetStream status code from a stream in regular sync.
|
2020-10-12 08:11:05 +00:00
|
|
|
func expectResetStream(t *testing.T, stream network.Stream) {
|
2019-08-23 16:53:38 +00:00
|
|
|
expectedErr := "stream reset"
|
2019-08-30 20:15:40 +00:00
|
|
|
_, _, err := ReadStatusCode(stream, &encoder.SszNetworkEncoder{})
|
2020-07-25 16:53:41 +00:00
|
|
|
require.ErrorContains(t, expectedErr, err)
|
2019-08-23 16:53:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 17:13:04 +00:00
|
|
|
func TestRegisterRPC_ReceivesValidMessage(t *testing.T) {
|
|
|
|
p2p := p2ptest.NewTestP2P(t)
|
2019-12-17 01:53:55 +00:00
|
|
|
r := &Service{
|
2019-08-16 17:13:04 +00:00
|
|
|
ctx: context.Background(),
|
|
|
|
p2p: p2p,
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
topic := "/testing/foobar/1"
|
2019-09-16 17:54:46 +00:00
|
|
|
handler := func(ctx context.Context, msg interface{}, stream libp2pcore.Stream) error {
|
2020-10-14 07:55:28 +00:00
|
|
|
m, ok := msg.(*p2ppb.Fork)
|
2020-04-14 16:41:09 +00:00
|
|
|
if !ok {
|
|
|
|
t.Error("Object is not of type *pb.TestSimpleMessage")
|
|
|
|
}
|
2020-10-14 07:55:28 +00:00
|
|
|
if !bytes.Equal(m.CurrentVersion, []byte("fooo")) {
|
2019-08-16 17:13:04 +00:00
|
|
|
t.Errorf("Unexpected incoming message: %+v", m)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 07:55:28 +00:00
|
|
|
prysmP2P.RPCTopicMappings[topic] = new(p2ppb.Fork)
|
2020-07-13 01:20:53 +00:00
|
|
|
// Cleanup Topic mappings
|
|
|
|
defer func() {
|
|
|
|
delete(prysmP2P.RPCTopicMappings, topic)
|
|
|
|
}()
|
|
|
|
r.registerRPC(topic, handler)
|
2019-08-16 17:13:04 +00:00
|
|
|
|
2020-10-14 07:55:28 +00:00
|
|
|
p2p.ReceiveRPC(topic, &p2ppb.Fork{CurrentVersion: []byte("fooo"), PreviousVersion: []byte("barr")})
|
2019-08-16 17:13:04 +00:00
|
|
|
|
|
|
|
if testutil.WaitTimeout(&wg, time.Second) {
|
|
|
|
t.Fatal("Did not receive RPC in 1 second")
|
|
|
|
}
|
|
|
|
}
|