2018-07-19 16:31:50 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
2018-07-30 06:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/database"
|
2018-07-19 16:31:50 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStartStop(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
|
|
|
tmp := fmt.Sprintf("%s/beacontest", os.TempDir())
|
2018-07-31 04:41:27 +00:00
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2018-07-30 06:14:50 +00:00
|
|
|
config := &database.DBConfig{DataDir: tmp, Name: "beacontestdata", InMemory: false}
|
|
|
|
db, err := database.NewDB(config)
|
2018-07-19 16:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
2018-07-22 16:58:14 +00:00
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
2018-07-22 16:58:14 +00:00
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-07-28 19:53:02 +00:00
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}})
|
2018-07-22 16:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-08-09 22:54:59 +00:00
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
}
|
|
|
|
chainService, err := NewChainService(ctx, cfg, db, web3Service)
|
2018-07-19 16:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chainService.Start()
|
|
|
|
|
|
|
|
if err := chainService.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to stop chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := hook.AllEntries()[0].Message
|
2018-07-28 19:53:02 +00:00
|
|
|
want := "Starting service"
|
2018-07-19 16:31:50 +00:00
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = hook.AllEntries()[1].Message
|
2018-08-13 02:07:37 +00:00
|
|
|
want = "No genesis block found on disk, initializing genesis block"
|
2018-07-19 16:31:50 +00:00
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:14:50 +00:00
|
|
|
msg = hook.AllEntries()[2].Message
|
2018-08-13 02:07:37 +00:00
|
|
|
want = "No chainstate found on disk, initializing beacon from genesis"
|
2018-07-19 16:31:50 +00:00
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
2018-07-31 04:41:27 +00:00
|
|
|
msg = hook.AllEntries()[3].Message
|
2018-08-13 02:07:37 +00:00
|
|
|
want = "Stopping service"
|
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = hook.AllEntries()[4].Message
|
2018-07-31 04:41:27 +00:00
|
|
|
want = "Persisting current active and crystallized states before closing"
|
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
// The context should have been canceled.
|
|
|
|
if chainService.ctx.Err() == nil {
|
|
|
|
t.Error("context was not canceled")
|
|
|
|
}
|
|
|
|
hook.Reset()
|
|
|
|
}
|