Added Check on POWChain Service Initialization (#1502)

* added check on web3Service initialization

* fix unit test
This commit is contained in:
skillful-alex 2019-02-07 19:11:18 +03:00 committed by Raul Jordan
parent a865b84217
commit 1861cf2d15
2 changed files with 52 additions and 27 deletions

View File

@ -81,6 +81,10 @@ func (c *ChainService) Start() {
go c.blockProcessing()
} else {
log.Info("Waiting for ChainStart log from the Validator Deposit Contract to start the beacon chain...")
if c.web3Service == nil {
log.Fatal("Not configured web3Service for POW chain")
return // return need for TestStartUninitializedChainWithoutConfigPOWChain
}
subChainStart := c.web3Service.ChainStartFeed().Subscribe(c.genesisTimeChan)
go func() {
genesisTime := <-c.genesisTimeChan

View File

@ -120,29 +120,31 @@ func setupInitialDeposits(t *testing.T) []*pb.Deposit {
return deposits
}
func setupBeaconChain(t *testing.T, faultyPoWClient bool, beaconDB *db.BeaconDB) *ChainService {
func setupBeaconChain(t *testing.T, faultyPoWClient bool, beaconDB *db.BeaconDB, enablePOWChain bool) *ChainService {
endpoint := "ws://127.0.0.1"
ctx := context.Background()
var web3Service *powchain.Web3Service
var err error
if faultyPoWClient {
client := &faultyClient{}
web3Service, err = powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{
Endpoint: endpoint,
DepositContract: common.Address{},
Reader: client,
Client: client,
Logger: client,
})
} else {
client := &mockClient{}
web3Service, err = powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{
Endpoint: endpoint,
DepositContract: common.Address{},
Reader: client,
Client: client,
Logger: client,
})
if enablePOWChain {
if faultyPoWClient {
client := &faultyClient{}
web3Service, err = powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{
Endpoint: endpoint,
DepositContract: common.Address{},
Reader: client,
Client: client,
Logger: client,
})
} else {
client := &mockClient{}
web3Service, err = powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{
Endpoint: endpoint,
DepositContract: common.Address{},
Reader: client,
Client: client,
Logger: client,
})
}
}
if err != nil {
t.Fatalf("unable to set up web3 service: %v", err)
@ -152,7 +154,7 @@ func setupBeaconChain(t *testing.T, faultyPoWClient bool, beaconDB *db.BeaconDB)
BeaconBlockBuf: 0,
BeaconDB: beaconDB,
Web3Service: web3Service,
EnablePOWChain: true,
EnablePOWChain: enablePOWChain,
}
if err != nil {
t.Fatalf("could not register blockchain service: %v", err)
@ -179,7 +181,7 @@ func TestStartStopUninitializedChain(t *testing.T) {
hook := logTest.NewGlobal()
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
chainService := setupBeaconChain(t, false, db)
chainService := setupBeaconChain(t, false, db, true)
chainService.IncomingBlockFeed()
@ -209,11 +211,30 @@ func TestStartStopUninitializedChain(t *testing.T) {
}
}
func TestStartUninitializedChainWithoutConfigPOWChain(t *testing.T) {
hook := logTest.NewGlobal()
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
chainService := setupBeaconChain(t, false, db, false)
origExitFunc := logrus.StandardLogger().ExitFunc
defer func() { logrus.StandardLogger().ExitFunc = origExitFunc }()
fatal := false
logrus.StandardLogger().ExitFunc = func(int) { fatal = true }
// Test the start function.
chainService.Start()
if !fatal {
t.Fatalf("Not exists fatal for init BeaconChain without POW chain")
}
testutil.AssertLogsContain(t, hook, "Not configured web3Service for POW chain")
}
func TestStartStopInitializedChain(t *testing.T) {
hook := logTest.NewGlobal()
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
chainService := setupBeaconChain(t, false, db)
chainService := setupBeaconChain(t, false, db, true)
unixTime := uint64(time.Now().Unix())
deposits := setupInitialDeposits(t)
@ -250,7 +271,7 @@ func TestRunningChainServiceFaultyPOWChain(t *testing.T) {
hook := logTest.NewGlobal()
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
chainService := setupBeaconChain(t, true, db)
chainService := setupBeaconChain(t, true, db, true)
unixTime := uint64(time.Now().Unix())
deposits := setupInitialDeposits(t)
if err := db.InitializeState(unixTime, deposits); err != nil {
@ -304,7 +325,7 @@ func TestRunningChainService(t *testing.T) {
hook := logTest.NewGlobal()
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
chainService := setupBeaconChain(t, false, db)
chainService := setupBeaconChain(t, false, db, true)
unixTime := uint64(time.Now().Unix())
deposits := setupInitialDeposits(t)
if err := db.InitializeState(unixTime, deposits); err != nil {
@ -400,7 +421,7 @@ func TestDoesPOWBlockExist(t *testing.T) {
hook := logTest.NewGlobal()
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
chainService := setupBeaconChain(t, true, db)
chainService := setupBeaconChain(t, true, db, true)
unixTime := uint64(time.Now().Unix())
deposits := setupInitialDeposits(t)
if err := db.InitializeState(unixTime, deposits); err != nil {
@ -467,7 +488,7 @@ func TestUpdateHead(t *testing.T) {
hook := logTest.NewGlobal()
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
chainService := setupBeaconChain(t, false, db)
chainService := setupBeaconChain(t, false, db, true)
unixTime := uint64(time.Now().Unix())
deposits := setupInitialDeposits(t)
if err := db.InitializeState(unixTime, deposits); err != nil {
@ -503,7 +524,7 @@ func TestUpdateHead(t *testing.T) {
func TestIsBlockReadyForProcessing(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
chainService := setupBeaconChain(t, false, db)
chainService := setupBeaconChain(t, false, db, true)
unixTime := uint64(time.Now().Unix())
deposits := setupInitialDeposits(t)
if err := db.InitializeState(unixTime, deposits); err != nil {