2018-08-01 22:08:44 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-04-16 18:19:31 +00:00
|
|
|
"bytes"
|
2018-08-01 22:08:44 +00:00
|
|
|
"context"
|
2019-02-04 21:54:30 +00:00
|
|
|
"errors"
|
2018-08-01 22:08:44 +00:00
|
|
|
"fmt"
|
2018-08-18 03:34:56 +00:00
|
|
|
"io/ioutil"
|
2018-08-01 22:08:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-03-17 02:56:05 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-05-09 17:38:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-02-16 00:36:40 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-05-09 17:38:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2018-10-03 01:49:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2019-05-09 17:38:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2018-08-01 22:08:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2018-08-18 03:34:56 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-08-01 22:08:44 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2018-08-18 03:34:56 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:54:30 +00:00
|
|
|
type TestLogger struct {
|
|
|
|
logrus.FieldLogger
|
|
|
|
testMap map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestLogger) Errorf(format string, args ...interface{}) {
|
|
|
|
t.testMap["error"] = true
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:54:02 +00:00
|
|
|
type mockOperationService struct {
|
|
|
|
pendingAttestations []*pb.Attestation
|
|
|
|
}
|
2018-09-21 14:32:20 +00:00
|
|
|
|
2019-01-31 18:54:24 +00:00
|
|
|
func (ms *mockOperationService) IncomingAttFeed() *event.Feed {
|
|
|
|
return new(event.Feed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *mockOperationService) IncomingExitFeed() *event.Feed {
|
2018-10-05 17:14:50 +00:00
|
|
|
return new(event.Feed)
|
2018-10-02 20:07:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 02:56:05 +00:00
|
|
|
func (ms *mockOperationService) HandleAttestations(_ context.Context, _ proto.Message) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-09 23:53:19 +00:00
|
|
|
func (ms *mockOperationService) IsAttCanonical(_ context.Context, att *pb.Attestation) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 20:03:28 +00:00
|
|
|
func (ms *mockOperationService) PendingAttestations(_ context.Context) ([]*pb.Attestation, error) {
|
2019-03-06 16:54:02 +00:00
|
|
|
if ms.pendingAttestations != nil {
|
|
|
|
return ms.pendingAttestations, nil
|
|
|
|
}
|
2019-02-16 00:36:40 +00:00
|
|
|
return []*pb.Attestation{
|
|
|
|
{
|
|
|
|
AggregationBitfield: []byte("A"),
|
2019-02-25 02:09:45 +00:00
|
|
|
Data: &pb.AttestationData{
|
2019-04-17 23:10:55 +00:00
|
|
|
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().SlotsPerEpoch,
|
|
|
|
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:],
|
2019-02-25 02:09:45 +00:00
|
|
|
},
|
2019-02-16 00:36:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
AggregationBitfield: []byte("B"),
|
2019-02-25 02:09:45 +00:00
|
|
|
Data: &pb.AttestationData{
|
2019-04-17 23:10:55 +00:00
|
|
|
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().SlotsPerEpoch,
|
|
|
|
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:],
|
2019-02-25 02:09:45 +00:00
|
|
|
},
|
2019-02-16 00:36:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
AggregationBitfield: []byte("C"),
|
2019-02-25 02:09:45 +00:00
|
|
|
Data: &pb.AttestationData{
|
2019-04-17 23:10:55 +00:00
|
|
|
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().SlotsPerEpoch,
|
|
|
|
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:],
|
2019-02-25 02:09:45 +00:00
|
|
|
},
|
2019-02-16 00:36:40 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
type mockChainService struct {
|
2019-02-13 17:51:57 +00:00
|
|
|
blockFeed *event.Feed
|
|
|
|
stateFeed *event.Feed
|
|
|
|
attestationFeed *event.Feed
|
|
|
|
stateInitializedFeed *event.Feed
|
2019-04-16 18:19:31 +00:00
|
|
|
canonicalBlocks map[uint64][]byte
|
2019-05-09 17:38:05 +00:00
|
|
|
targets map[uint64]*pb.AttestationTarget
|
|
|
|
beaconDB *db.BeaconDB
|
2018-09-05 03:35:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 21:17:32 +00:00
|
|
|
func (m *mockChainService) StateInitializedFeed() *event.Feed {
|
|
|
|
return m.stateInitializedFeed
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 21:17:32 +00:00
|
|
|
func (m *mockChainService) ReceiveBlock(ctx context.Context, block *pb.BeaconBlock) (*pb.BeaconState, error) {
|
|
|
|
return &pb.BeaconState{}, nil
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 21:17:32 +00:00
|
|
|
func (m *mockChainService) ApplyForkChoiceRule(ctx context.Context, block *pb.BeaconBlock, computedState *pb.BeaconState) error {
|
|
|
|
return nil
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 21:17:32 +00:00
|
|
|
func (m *mockChainService) CanonicalBlockFeed() *event.Feed {
|
|
|
|
return new(event.Feed)
|
2019-02-13 17:51:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 06:02:52 +00:00
|
|
|
func (m *mockChainService) UpdateCanonicalRoots(block *pb.BeaconBlock, root [32]byte) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-25 15:21:21 +00:00
|
|
|
func (m mockChainService) SaveHistoricalState(beaconState *pb.BeaconState) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 18:19:31 +00:00
|
|
|
func (m mockChainService) IsCanonical(slot uint64, hash []byte) bool {
|
|
|
|
return bytes.Equal(m.canonicalBlocks[slot], hash)
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:38:05 +00:00
|
|
|
func (m *mockChainService) BlockChildren(ctx context.Context, block *pb.BeaconBlock, highestSlot uint64) ([]*pb.BeaconBlock, error) {
|
|
|
|
blockRoot, err := hashutil.HashBeaconBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var children []*pb.BeaconBlock
|
|
|
|
startSlot := block.Slot + 1
|
|
|
|
for i := startSlot; i <= highestSlot; i++ {
|
|
|
|
kids, err := m.beaconDB.BlocksBySlot(ctx, i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get block by slot: %v", err)
|
|
|
|
}
|
|
|
|
children = append(children, kids...)
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredChildren := []*pb.BeaconBlock{}
|
|
|
|
for _, kid := range children {
|
|
|
|
parentRoot := bytesutil.ToBytes32(kid.ParentRootHash32)
|
|
|
|
if blockRoot == parentRoot {
|
|
|
|
filteredChildren = append(filteredChildren, kid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredChildren, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockChainService) AttestationTargets(justifiedState *pb.BeaconState) (map[uint64]*pb.AttestationTarget, error) {
|
|
|
|
return m.targets, nil
|
2019-05-01 15:14:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
func newMockChainService() *mockChainService {
|
|
|
|
return &mockChainService{
|
2019-02-13 17:51:57 +00:00
|
|
|
blockFeed: new(event.Feed),
|
|
|
|
stateFeed: new(event.Feed),
|
|
|
|
attestationFeed: new(event.Feed),
|
|
|
|
stateInitializedFeed: new(event.Feed),
|
2018-09-21 14:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 13:17:45 +00:00
|
|
|
type mockSyncService struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *mockSyncService) Status() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestLifecycle_OK(t *testing.T) {
|
2018-08-01 22:08:44 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2018-09-05 03:35:32 +00:00
|
|
|
rpcService := NewRPCService(context.Background(), &Config{
|
2019-05-02 13:17:45 +00:00
|
|
|
Port: "7348",
|
|
|
|
CertFlag: "alice.crt",
|
|
|
|
KeyFlag: "alice.key",
|
|
|
|
SyncService: &mockSyncService{},
|
2018-09-05 03:35:32 +00:00
|
|
|
})
|
2018-08-01 22:08:44 +00:00
|
|
|
|
|
|
|
rpcService.Start()
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
2019-04-20 04:09:01 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Listening on port")
|
2018-08-01 22:08:44 +00:00
|
|
|
|
|
|
|
rpcService.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
2019-02-04 21:54:30 +00:00
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestRPC_BadEndpoint(t *testing.T) {
|
2019-02-04 21:54:30 +00:00
|
|
|
fl := logrus.WithField("prefix", "rpc")
|
|
|
|
|
|
|
|
log = &TestLogger{
|
|
|
|
FieldLogger: fl,
|
|
|
|
testMap: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
hook := logTest.NewLocal(fl.Logger)
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
rpcService := NewRPCService(context.Background(), &Config{
|
2019-05-02 13:17:45 +00:00
|
|
|
Port: "ralph merkle!!!",
|
|
|
|
SyncService: &mockSyncService{},
|
2018-09-21 14:32:20 +00:00
|
|
|
})
|
2018-08-01 22:08:44 +00:00
|
|
|
|
2019-03-25 15:21:21 +00:00
|
|
|
if val, ok := log.(*TestLogger).testMap["error"]; ok {
|
|
|
|
t.Fatalf("Error in Start() occurred before expected: %v", val)
|
2019-02-04 21:54:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
rpcService.Start()
|
|
|
|
|
2019-02-04 21:54:30 +00:00
|
|
|
if _, ok := log.(*TestLogger).testMap["error"]; !ok {
|
|
|
|
t.Fatal("No error occurred. Expected Start() to output an error")
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
|
2019-02-04 21:54:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestStatus_CredentialError(t *testing.T) {
|
2019-02-04 21:54:30 +00:00
|
|
|
credentialErr := errors.New("credentialError")
|
|
|
|
s := &Service{credentialError: credentialErr}
|
|
|
|
|
|
|
|
if err := s.Status(); err != s.credentialError {
|
|
|
|
t.Errorf("Wanted: %v, got: %v", s.credentialError, s.Status())
|
|
|
|
}
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
2018-08-09 22:54:59 +00:00
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestRPC_InsecureEndpoint(t *testing.T) {
|
2018-08-09 22:54:59 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2018-09-21 14:32:20 +00:00
|
|
|
rpcService := NewRPCService(context.Background(), &Config{
|
2019-05-02 13:17:45 +00:00
|
|
|
Port: "7777",
|
|
|
|
SyncService: &mockSyncService{},
|
2018-09-21 14:32:20 +00:00
|
|
|
})
|
2018-08-09 22:54:59 +00:00
|
|
|
|
|
|
|
rpcService.Start()
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
2019-04-20 04:09:01 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, fmt.Sprint("Listening on port"))
|
2018-08-09 22:54:59 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "You are using an insecure gRPC connection")
|
|
|
|
|
|
|
|
rpcService.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
}
|