mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-31 23:41:22 +00:00
f5c88e1bcb
* clean up test * adding test setup * add chainstart check to sync querier * goimports * goimports * backend cleanup * adding getters * lint * moving to db * gazelle * adding more services setup * adding more changes * adding different test setups * other merge issues * imports * fixing keys * get intial sync able to be set up * new changes and gazelle * use simulated p2p * everything finally works * unexport fields * revert changes from merge * remove mock server * add documentation * gazelle * add another node * review comments * fix tests
101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package sync
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
func NotSyncQuerierConfig() *QuerierConfig {
|
|
return &QuerierConfig{
|
|
ResponseBufferSize: 100,
|
|
CurrentHeadSlot: 10,
|
|
}
|
|
}
|
|
|
|
func initializeTestSyncService(ctx context.Context, cfg *Config, synced bool) *Service {
|
|
var sqCfg *QuerierConfig
|
|
if synced {
|
|
sqCfg = DefaultQuerierConfig()
|
|
} else {
|
|
sqCfg = NotSyncQuerierConfig()
|
|
}
|
|
|
|
services := NewSyncService(ctx, cfg)
|
|
|
|
sqCfg.BeaconDB = cfg.BeaconDB
|
|
sqCfg.P2P = cfg.P2P
|
|
sq := NewQuerierService(ctx, sqCfg)
|
|
|
|
services.Querier = sq
|
|
|
|
return services
|
|
}
|
|
|
|
func setupInitialDeposits(t *testing.T, numDeposits int) ([]*pb.Deposit, []*bls.SecretKey) {
|
|
privKeys := make([]*bls.SecretKey, numDeposits)
|
|
deposits := make([]*pb.Deposit, numDeposits)
|
|
for i := 0; i < len(deposits); i++ {
|
|
priv, err := bls.RandKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
depositInput := &pb.DepositInput{
|
|
Pubkey: priv.PublicKey().Marshal(),
|
|
}
|
|
balance := params.BeaconConfig().MaxDepositAmount
|
|
depositData, err := helpers.EncodeDepositData(depositInput, balance, time.Now().Unix())
|
|
if err != nil {
|
|
t.Fatalf("Cannot encode data: %v", err)
|
|
}
|
|
deposits[i] = &pb.Deposit{DepositData: depositData}
|
|
privKeys[i] = priv
|
|
}
|
|
return deposits, privKeys
|
|
}
|
|
|
|
func setupTestSyncService(t *testing.T, synced bool) (*Service, *db.BeaconDB) {
|
|
db := internal.SetupDB(t)
|
|
|
|
unixTime := uint64(time.Now().Unix())
|
|
deposits, _ := setupInitialDeposits(t, 10)
|
|
if err := db.InitializeState(unixTime, deposits); err != nil {
|
|
t.Fatalf("Failed to initialize state: %v", err)
|
|
}
|
|
|
|
cfg := &Config{
|
|
ChainService: &mockChainService{},
|
|
P2P: &mockP2P{},
|
|
BeaconDB: db,
|
|
OperationService: &mockOperationService{},
|
|
}
|
|
service := initializeTestSyncService(context.Background(), cfg, synced)
|
|
return service, db
|
|
|
|
}
|
|
|
|
func TestStatus_Synced(t *testing.T) {
|
|
serviceSynced, db := setupTestSyncService(t, true)
|
|
defer internal.TeardownDB(t, db)
|
|
if serviceSynced.Status() != nil {
|
|
t.Errorf("Wanted nil, but got %v", serviceSynced.Status())
|
|
}
|
|
}
|
|
|
|
func TestStatus_NotSynced(t *testing.T) {
|
|
serviceNotSynced, db := setupTestSyncService(t, false)
|
|
defer internal.TeardownDB(t, db)
|
|
_, querierErr := serviceNotSynced.Querier.IsSynced()
|
|
if serviceNotSynced.Status() != querierErr {
|
|
t.Errorf("Wanted %v, but got %v", querierErr, serviceNotSynced.Status())
|
|
}
|
|
}
|