2019-08-16 20:03:11 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
2019-08-27 13:27:04 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
2019-08-16 20:03:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
|
|
|
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
|
2019-09-20 17:54:32 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/sync/peerstatus"
|
2019-08-16 20:03:11 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2019-09-18 20:02:34 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-08-16 20:03:11 +00:00
|
|
|
)
|
|
|
|
|
2019-09-18 20:02:34 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
func TestHelloRPCHandler_Disconnects_OnForkVersionMismatch(t *testing.T) {
|
|
|
|
peerstatus.Clear()
|
|
|
|
|
2019-09-11 18:38:35 +00:00
|
|
|
// TODO(3441): Fix ssz string length issue.
|
|
|
|
t.Skip("3441: SSZ is decoding a string with an unexpected length")
|
2019-08-16 20:03:11 +00:00
|
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
|
|
p1.Connect(p2)
|
|
|
|
if len(p1.Host.Network().Peers()) != 1 {
|
|
|
|
t.Error("Expected peers to be connected")
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
r := &RegularSync{p2p: p1}
|
2019-08-16 20:03:11 +00:00
|
|
|
pcl := protocol.ID("/testing")
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
|
|
defer wg.Done()
|
2019-08-30 20:15:40 +00:00
|
|
|
code, errMsg, err := ReadStatusCode(stream, p1.Encoding())
|
2019-08-16 20:03:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if code == 0 {
|
|
|
|
t.Error("Expected a non-zero code")
|
|
|
|
}
|
2019-09-11 18:38:35 +00:00
|
|
|
if errMsg != errWrongForkVersion.Error() {
|
|
|
|
t.Logf("Received error string len %d, wanted error string len %d", len(errMsg), len(errWrongForkVersion.Error()))
|
|
|
|
t.Errorf("Received unexpected message response in the stream: %s. Wanted %s.", errMsg, errWrongForkVersion.Error())
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
stream1, err := p1.Host.NewStream(context.Background(), p2.Host.ID(), pcl)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
err = r.statusRPCHandler(context.Background(), &pb.Status{HeadForkVersion: []byte("fake")}, stream1)
|
2019-08-16 20:03:11 +00:00
|
|
|
if err != errWrongForkVersion {
|
|
|
|
t.Errorf("Expected error %v, got %v", errWrongForkVersion, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
|
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p1.Host.Network().Peers()) != 0 {
|
|
|
|
t.Error("handler did not disconnect peer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
func TestHelloRPCHandler_ReturnsHelloMessage(t *testing.T) {
|
|
|
|
peerstatus.Clear()
|
|
|
|
|
2019-08-16 20:03:11 +00:00
|
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
|
|
p1.Connect(p2)
|
|
|
|
if len(p1.Host.Network().Peers()) != 1 {
|
|
|
|
t.Error("Expected peers to be connected")
|
|
|
|
}
|
|
|
|
|
2019-08-23 17:48:40 +00:00
|
|
|
// Set up a head state with data we expect.
|
2019-08-16 20:03:11 +00:00
|
|
|
headRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 111})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
finalizedRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 40})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
genesisState.Slot = 111
|
|
|
|
genesisState.BlockRoots[111%params.BeaconConfig().SlotsPerHistoricalRoot] = headRoot[:]
|
2019-08-23 17:48:40 +00:00
|
|
|
finalizedCheckpt := ðpb.Checkpoint{
|
2019-08-16 20:03:11 +00:00
|
|
|
Epoch: 5,
|
|
|
|
Root: finalizedRoot[:],
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &RegularSync{
|
|
|
|
p2p: p1,
|
2019-08-26 20:06:16 +00:00
|
|
|
chain: &mock.ChainService{
|
|
|
|
State: genesisState,
|
|
|
|
FinalizedCheckPoint: finalizedCheckpt,
|
|
|
|
Root: headRoot[:],
|
2019-08-23 17:48:40 +00:00
|
|
|
},
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup streams
|
|
|
|
pcl := protocol.ID("/testing")
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
|
|
defer wg.Done()
|
|
|
|
expectSuccess(t, r, stream)
|
2019-09-20 06:27:28 +00:00
|
|
|
out := &pb.Status{}
|
2019-09-09 02:34:52 +00:00
|
|
|
if err := r.p2p.Encoding().DecodeWithLength(stream, out); err != nil {
|
2019-08-16 20:03:11 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-20 06:27:28 +00:00
|
|
|
expected := &pb.Status{
|
|
|
|
HeadForkVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
HeadSlot: genesisState.Slot,
|
|
|
|
HeadRoot: headRoot[:],
|
|
|
|
FinalizedEpoch: 5,
|
|
|
|
FinalizedRoot: finalizedRoot[:],
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
|
|
|
if !proto.Equal(out, expected) {
|
|
|
|
t.Errorf("Did not receive expected message. Got %+v wanted %+v", out, expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
stream1, err := p1.Host.NewStream(context.Background(), p2.Host.ID(), pcl)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
err = r.statusRPCHandler(context.Background(), &pb.Status{HeadForkVersion: params.BeaconConfig().GenesisForkVersion}, stream1)
|
2019-08-16 20:03:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unxpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
|
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 20:02:34 +00:00
|
|
|
func TestHandshakeHandlers_Roundtrip(t *testing.T) {
|
2019-09-20 17:54:32 +00:00
|
|
|
peerstatus.Clear()
|
|
|
|
|
2019-09-18 20:02:34 +00:00
|
|
|
// Scenario is that p1 and p2 connect, exchange handshakes.
|
|
|
|
// p2 disconnects and p1 should forget the handshake status.
|
|
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
|
|
|
|
|
|
r := &RegularSync{
|
|
|
|
p2p: p1,
|
|
|
|
chain: &mock.ChainService{
|
2019-09-19 08:00:47 +00:00
|
|
|
State: &pb.BeaconState{Slot: 5},
|
2019-09-18 20:02:34 +00:00
|
|
|
FinalizedCheckPoint: ðpb.Checkpoint{},
|
|
|
|
},
|
2019-09-20 17:54:32 +00:00
|
|
|
ctx: context.Background(),
|
2019-09-18 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.Start()
|
|
|
|
|
|
|
|
// Setup streams
|
2019-09-20 06:27:28 +00:00
|
|
|
pcl := protocol.ID("/eth2/beacon_chain/req/status/1/ssz")
|
2019-09-18 20:02:34 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
|
|
defer wg.Done()
|
2019-09-20 06:27:28 +00:00
|
|
|
out := &pb.Status{}
|
2019-09-18 20:02:34 +00:00
|
|
|
if err := r.p2p.Encoding().DecodeWithLength(stream, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
resp := &pb.Status{HeadSlot: 100, HeadForkVersion: params.BeaconConfig().GenesisForkVersion}
|
2019-09-18 20:02:34 +00:00
|
|
|
|
|
|
|
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err := r.p2p.Encoding().EncodeWithLength(stream, resp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
stream.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
p1.Connect(p2)
|
|
|
|
|
|
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
|
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for stream buffer to be read.
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
if peerstatus.Count() != 1 {
|
|
|
|
t.Errorf("Expected 1 status in the cache, got %d", peerstatus.Count())
|
2019-09-18 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p2.Disconnect(p1.PeerID()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for disconnect event to trigger.
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
if peerstatus.Count() != 0 {
|
|
|
|
t.Errorf("Expected 0 status in the tracker, got %d", peerstatus.Count())
|
2019-09-18 20:02:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|
2019-08-29 16:32:52 +00:00
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
func TestStatusRPCRequest_RequestSent(t *testing.T) {
|
2019-08-29 16:32:52 +00:00
|
|
|
p1 := p2ptest.NewTestP2P(t)
|
|
|
|
p2 := p2ptest.NewTestP2P(t)
|
|
|
|
|
|
|
|
// Set up a head state with data we expect.
|
|
|
|
headRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 111})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
finalizedRoot, err := ssz.HashTreeRoot(ðpb.BeaconBlock{Slot: 40})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
genesisState, err := state.GenesisBeaconState(nil, 0, ðpb.Eth1Data{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
genesisState.Slot = 111
|
|
|
|
genesisState.BlockRoots[111%params.BeaconConfig().SlotsPerHistoricalRoot] = headRoot[:]
|
|
|
|
finalizedCheckpt := ðpb.Checkpoint{
|
|
|
|
Epoch: 5,
|
|
|
|
Root: finalizedRoot[:],
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &RegularSync{
|
|
|
|
p2p: p1,
|
|
|
|
chain: &mock.ChainService{
|
|
|
|
State: genesisState,
|
|
|
|
FinalizedCheckPoint: finalizedCheckpt,
|
|
|
|
Root: headRoot[:],
|
|
|
|
},
|
2019-09-20 17:54:32 +00:00
|
|
|
ctx: context.Background(),
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup streams
|
2019-09-20 06:27:28 +00:00
|
|
|
pcl := protocol.ID("/eth2/beacon_chain/req/status/1/ssz")
|
2019-08-29 16:32:52 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
|
|
|
|
defer wg.Done()
|
2019-09-20 06:27:28 +00:00
|
|
|
out := &pb.Status{}
|
2019-09-09 02:34:52 +00:00
|
|
|
if err := r.p2p.Encoding().DecodeWithLength(stream, out); err != nil {
|
2019-08-29 16:32:52 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-20 06:27:28 +00:00
|
|
|
expected := &pb.Status{
|
|
|
|
HeadForkVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
HeadSlot: genesisState.Slot,
|
|
|
|
HeadRoot: headRoot[:],
|
|
|
|
FinalizedEpoch: 5,
|
|
|
|
FinalizedRoot: finalizedRoot[:],
|
2019-08-29 16:32:52 +00:00
|
|
|
}
|
|
|
|
if !proto.Equal(out, expected) {
|
|
|
|
t.Errorf("Did not receive expected message. Got %+v wanted %+v", out, expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-20 06:27:28 +00:00
|
|
|
p1.AddConnectionHandler(r.sendRPCStatusRequest)
|
2019-08-29 16:32:52 +00:00
|
|
|
p1.Connect(p2)
|
|
|
|
|
2019-09-16 17:54:46 +00:00
|
|
|
if testutil.WaitTimeout(&wg, 1*time.Second) {
|
2019-08-29 16:32:52 +00:00
|
|
|
t.Fatal("Did not receive stream within 1 sec")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p1.Host.Network().Peers()) != 1 {
|
|
|
|
t.Error("Expected peers to continue being connected")
|
|
|
|
}
|
|
|
|
}
|