2018-08-01 22:08:44 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-02-16 00:36:40 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2018-10-03 01:49:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
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-01-31 18:54:24 +00:00
|
|
|
type mockOperationService struct{}
|
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-02-16 00:36:40 +00:00
|
|
|
func (ms *mockOperationService) PendingAttestations() ([]*pb.Attestation, error) {
|
|
|
|
return []*pb.Attestation{
|
|
|
|
{
|
|
|
|
AggregationBitfield: []byte("A"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AggregationBitfield: []byte("B"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AggregationBitfield: []byte("C"),
|
|
|
|
},
|
|
|
|
}, 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
|
2018-09-05 03:35:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
func (m *mockChainService) IncomingBlockFeed() *event.Feed {
|
|
|
|
return new(event.Feed)
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
func (m *mockChainService) CanonicalBlockFeed() *event.Feed {
|
2018-08-24 04:09:59 +00:00
|
|
|
return m.blockFeed
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
func (m *mockChainService) CanonicalStateFeed() *event.Feed {
|
2018-08-24 04:09:59 +00:00
|
|
|
return m.stateFeed
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 17:51:57 +00:00
|
|
|
func (m *mockChainService) StateInitializedFeed() *event.Feed {
|
|
|
|
return m.stateInitializedFeed
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
func TestLifecycle(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2018-09-05 03:35:32 +00:00
|
|
|
rpcService := NewRPCService(context.Background(), &Config{
|
2018-10-05 17:14:50 +00:00
|
|
|
Port: "7348",
|
|
|
|
CertFlag: "alice.crt",
|
|
|
|
KeyFlag: "alice.key",
|
2018-09-05 03:35:32 +00:00
|
|
|
})
|
2018-08-01 22:08:44 +00:00
|
|
|
|
|
|
|
rpcService.Start()
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
testutil.AssertLogsContain(t, hook, fmt.Sprintf("RPC server listening on port :%s", rpcService.port))
|
|
|
|
|
|
|
|
rpcService.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
2019-02-04 21:54:30 +00:00
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadEndpoint(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{
|
2018-10-05 17:14:50 +00:00
|
|
|
Port: "ralph merkle!!!",
|
2018-09-21 14:32:20 +00:00
|
|
|
})
|
2018-08-01 22:08:44 +00:00
|
|
|
|
2019-02-04 21:54:30 +00:00
|
|
|
if _, ok := log.(*TestLogger).testMap["error"]; ok {
|
|
|
|
t.Fatal("Error in Start() occurred before expected")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatus(t *testing.T) {
|
|
|
|
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
|
|
|
|
|
|
|
func TestInsecureEndpoint(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2018-09-21 14:32:20 +00:00
|
|
|
rpcService := NewRPCService(context.Background(), &Config{
|
2018-10-05 17:14:50 +00:00
|
|
|
Port: "7777",
|
2018-09-21 14:32:20 +00:00
|
|
|
})
|
2018-08-09 22:54:59 +00:00
|
|
|
|
|
|
|
rpcService.Start()
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
testutil.AssertLogsContain(t, hook, fmt.Sprintf("RPC server listening on port :%s", rpcService.port))
|
|
|
|
testutil.AssertLogsContain(t, hook, "You are using an insecure gRPC connection")
|
|
|
|
|
|
|
|
rpcService.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
|
|
|
}
|