prysm-pulse/beacon-chain/rpc/service_test.go
Raul Jordan 053038446c
Allow 8 Validator Multinode Cluster to Run Indefinitely (#2050)
* plug forkchoice to blockchain service's block processing

* fixed tests

* more fixes...

* clean ups

* fixed test

* Update beacon-chain/blockchain/block_processing.go

* merged with 2006 and started fixing tests

* remove prints

* fixed tests

* lint

* include ops service

* if there's a skip slot, slot--

* fixed typo

* started working on test

* no fork choice in propose

* bleh, need to fix state generator first

* state gen takes input slot

* feedback

* fixed tests

* preston's feedback

* fmt

* removed extra logging

* add more logs

* fixed validator attest

* builds

* fixed save block

* children fix

* removed verbose logs

* fix fork choice

* right logs

* Add Prometheus Counter for Reorg (#2051)

* fetch every slot (#2052)

* test Fixes

* lint

* only regenerate state if there was a reorg

* better logging

* fixed seed

* better logging

* process skip slots in assignment requests

* fix lint

* disable state root computation

* filter attestations in regular sync

* log important items

* better info logs

* added spans to stategen

* span in stategen

* set validator deadline

* randao stuff

* disable sig verify

* lint

* lint

* save only using historical states

* use new goroutine for handling sync messages

* change default buffer sizes

* better p2p

* rem some useless logs

* lint

* sync tests complete

* complete tests

* tests fixed

* lint

* fix flakey att service

* PR feedback

* undo k8s changes

* Update beacon-chain/blockchain/block_processing.go

* Update beacon-chain/sync/regular_sync.go

* Add feature flag to enable compute state root

* add comment

* gazelle lint fix
2019-03-25 10:21:21 -05:00

181 lines
4.5 KiB
Go

package rpc
import (
"context"
"errors"
"fmt"
"io/ioutil"
"testing"
"github.com/gogo/protobuf/proto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func init() {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(ioutil.Discard)
}
type TestLogger struct {
logrus.FieldLogger
testMap map[string]interface{}
}
func (t *TestLogger) Errorf(format string, args ...interface{}) {
t.testMap["error"] = true
}
type mockOperationService struct {
pendingAttestations []*pb.Attestation
}
func (ms *mockOperationService) IncomingAttFeed() *event.Feed {
return new(event.Feed)
}
func (ms *mockOperationService) IncomingExitFeed() *event.Feed {
return new(event.Feed)
}
func (ms *mockOperationService) HandleAttestations(_ context.Context, _ proto.Message) error {
return nil
}
func (ms *mockOperationService) PendingAttestations() ([]*pb.Attestation, error) {
if ms.pendingAttestations != nil {
return ms.pendingAttestations, nil
}
return []*pb.Attestation{
{
AggregationBitfield: []byte("A"),
Data: &pb.AttestationData{
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().SlotsPerEpoch,
},
},
{
AggregationBitfield: []byte("B"),
Data: &pb.AttestationData{
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().SlotsPerEpoch,
},
},
{
AggregationBitfield: []byte("C"),
Data: &pb.AttestationData{
Slot: params.BeaconConfig().GenesisSlot + params.BeaconConfig().SlotsPerEpoch,
},
},
}, nil
}
type mockChainService struct {
blockFeed *event.Feed
stateFeed *event.Feed
attestationFeed *event.Feed
stateInitializedFeed *event.Feed
}
func (m *mockChainService) StateInitializedFeed() *event.Feed {
return m.stateInitializedFeed
}
func (m *mockChainService) ReceiveBlock(ctx context.Context, block *pb.BeaconBlock) (*pb.BeaconState, error) {
return &pb.BeaconState{}, nil
}
func (m *mockChainService) ApplyForkChoiceRule(ctx context.Context, block *pb.BeaconBlock, computedState *pb.BeaconState) error {
return nil
}
func (m *mockChainService) CanonicalBlockFeed() *event.Feed {
return new(event.Feed)
}
func (m mockChainService) SaveHistoricalState(beaconState *pb.BeaconState) error {
return nil
}
func newMockChainService() *mockChainService {
return &mockChainService{
blockFeed: new(event.Feed),
stateFeed: new(event.Feed),
attestationFeed: new(event.Feed),
stateInitializedFeed: new(event.Feed),
}
}
func TestLifecycle_OK(t *testing.T) {
hook := logTest.NewGlobal()
rpcService := NewRPCService(context.Background(), &Config{
Port: "7348",
CertFlag: "alice.crt",
KeyFlag: "alice.key",
})
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")
}
func TestRPC_BadEndpoint(t *testing.T) {
fl := logrus.WithField("prefix", "rpc")
log = &TestLogger{
FieldLogger: fl,
testMap: make(map[string]interface{}),
}
hook := logTest.NewLocal(fl.Logger)
rpcService := NewRPCService(context.Background(), &Config{
Port: "ralph merkle!!!",
})
if val, ok := log.(*TestLogger).testMap["error"]; ok {
t.Fatalf("Error in Start() occurred before expected: %v", val)
}
rpcService.Start()
if _, ok := log.(*TestLogger).testMap["error"]; !ok {
t.Fatal("No error occurred. Expected Start() to output an error")
}
testutil.AssertLogsContain(t, hook, "Starting service")
}
func TestStatus_CredentialError(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())
}
}
func TestRPC_InsecureEndpoint(t *testing.T) {
hook := logTest.NewGlobal()
rpcService := NewRPCService(context.Background(), &Config{
Port: "7777",
})
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")
}