prysm-pulse/beacon-chain/rpc/aggregator/server_test.go
terence tsao f5cb04012e Aggregator selection from RPC to validator client (#4071)
* Config
* Updated proto
* Updated pool
* Updated RPC
* Updated validator client
* run time works
* Clean ups
* Fix tests
* Visibility
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into aggregator
* Raul's feedback
* Tests for RPC server
* Tests for validator client
* Span
* More tests
* Use go routine for SubmitAggregateAndProof
* Go routines
* Updated comments
* Use array of roles
* Fixed tests
* Build
* Update validator/client/runner.go

Co-Authored-By: Preston Van Loon <preston@prysmaticlabs.com>
* Update validator/client/runner.go

Co-Authored-By: Preston Van Loon <preston@prysmaticlabs.com>
* If
* Merge branch 'refactor-validator-roles' of https://github.com/prysmaticlabs/prysm into refactor-validator-roles
* Empty
* Feedback
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into aggregator
* Removed proto/eth/v1alpha1/shard_chain.pb.go?
* Cleaned up
* Revert
* Comments
* Lint
* Comment
* Merge branch 'master' into aggregator
2019-11-22 05:11:38 +00:00

100 lines
2.8 KiB
Go

package aggregator
import (
"context"
"crypto/rand"
"strings"
"testing"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
dbutil "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/params"
)
func init() {
// Use minimal config to reduce test setup time.
params.OverrideBeaconConfig(params.MinimalSpecConfig())
}
func TestSubmitAggregateAndProof_Syncing(t *testing.T) {
db := dbutil.SetupDB(t)
defer dbutil.TeardownDB(t, db)
ctx := context.Background()
s := &pbp2p.BeaconState{}
aggregatorServer := &Server{
HeadFetcher: &mock.ChainService{State: s},
SyncChecker: &mockSync.Sync{IsSyncing: true},
BeaconDB: db,
}
req := &pb.AggregationRequest{CommitteeIndex: 1}
wanted := "Syncing to latest head, not ready to respond"
if _, err := aggregatorServer.SubmitAggregateAndProof(ctx, req); !strings.Contains(err.Error(), wanted) {
t.Error("Did not receive wanted error")
}
}
func TestSubmitAggregateAndProof_CantFindValidatorIndex(t *testing.T) {
db := dbutil.SetupDB(t)
defer dbutil.TeardownDB(t, db)
ctx := context.Background()
s := &pbp2p.BeaconState{
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
}
aggregatorServer := &Server{
HeadFetcher: &mock.ChainService{State: s},
SyncChecker: &mockSync.Sync{IsSyncing: false},
BeaconDB: db,
}
priv, err := bls.RandKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
sig := priv.Sign([]byte{'A'}, 0)
req := &pb.AggregationRequest{CommitteeIndex: 1, SlotSignature: sig.Marshal()}
wanted := "Could not locate validator index in DB"
if _, err := aggregatorServer.SubmitAggregateAndProof(ctx, req); !strings.Contains(err.Error(), wanted) {
t.Error("Did not receive wanted error")
}
}
func TestSubmitAggregateAndProof_IsAggregator(t *testing.T) {
db := dbutil.SetupDB(t)
defer dbutil.TeardownDB(t, db)
ctx := context.Background()
s := &pbp2p.BeaconState{
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
}
aggregatorServer := &Server{
HeadFetcher: &mock.ChainService{State: s},
SyncChecker: &mockSync.Sync{IsSyncing: false},
BeaconDB: db,
}
priv, err := bls.RandKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
sig := priv.Sign([]byte{'A'}, 0)
pubKey := [48]byte{'A'}
req := &pb.AggregationRequest{CommitteeIndex: 1, SlotSignature: sig.Marshal(), PublicKey: pubKey[:]}
if err := aggregatorServer.BeaconDB.SaveValidatorIndex(ctx, pubKey, 100); err != nil {
t.Fatal(err)
}
if _, err = aggregatorServer.SubmitAggregateAndProof(ctx, req); err != nil {
t.Fatal(err)
}
}