2018-07-19 16:31:50 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-18 13:06:28 +00:00
|
|
|
"errors"
|
2018-08-18 03:34:56 +00:00
|
|
|
"io/ioutil"
|
2018-09-21 19:33:53 +00:00
|
|
|
"math"
|
2018-09-11 05:09:41 +00:00
|
|
|
"math/big"
|
2018-09-18 13:06:28 +00:00
|
|
|
"strings"
|
2018-07-19 16:31:50 +00:00
|
|
|
"testing"
|
2018-09-18 13:06:28 +00:00
|
|
|
"time"
|
2018-07-19 16:31:50 +00:00
|
|
|
|
2018-10-03 02:04:36 +00:00
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
2018-07-22 16:58:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-08-15 04:49:59 +00:00
|
|
|
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
2018-08-18 03:34:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
2018-07-22 16:58:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
2018-08-15 04:49:59 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/types"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2018-07-30 06:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/database"
|
2018-10-03 01:49:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2018-08-18 03:34:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
"github.com/sirupsen/logrus"
|
2018-07-19 16:31:50 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2018-08-18 03:34:56 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
type mockClient struct{}
|
|
|
|
|
|
|
|
func (f *mockClient) SubscribeNewHead(ctx context.Context, ch chan<- *gethTypes.Header) (ethereum.Subscription, error) {
|
|
|
|
return new(event.Feed).Subscribe(ch), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockClient) BlockByHash(ctx context.Context, hash common.Hash) (*gethTypes.Block, error) {
|
2018-09-11 05:09:41 +00:00
|
|
|
head := &gethTypes.Header{Number: big.NewInt(0), Difficulty: big.NewInt(100)}
|
|
|
|
return gethTypes.NewBlockWithHeader(head), nil
|
2018-08-15 04:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockClient) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- gethTypes.Log) (ethereum.Subscription, error) {
|
|
|
|
return new(event.Feed).Subscribe(ch), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *mockClient) LatestBlockHash() common.Hash {
|
|
|
|
return common.BytesToHash([]byte{'A'})
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
type faultyClient struct{}
|
|
|
|
|
|
|
|
func (f *faultyClient) SubscribeNewHead(ctx context.Context, ch chan<- *gethTypes.Header) (ethereum.Subscription, error) {
|
|
|
|
return new(event.Feed).Subscribe(ch), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *faultyClient) BlockByHash(ctx context.Context, hash common.Hash) (*gethTypes.Block, error) {
|
|
|
|
return nil, errors.New("failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *faultyClient) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- gethTypes.Log) (ethereum.Subscription, error) {
|
|
|
|
return new(event.Feed).Subscribe(ch), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *faultyClient) LatestBlockHash() common.Hash {
|
|
|
|
return common.BytesToHash([]byte{'A'})
|
|
|
|
}
|
|
|
|
|
2018-09-11 05:09:41 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
func TestStartStop(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-07-31 04:41:27 +00:00
|
|
|
|
2018-08-24 04:09:59 +00:00
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
2018-07-30 06:14:50 +00:00
|
|
|
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-08-18 03:34:56 +00:00
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-08-15 04:49:59 +00:00
|
|
|
client := &mockClient{}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}}, client, client, client)
|
2018-07-22 16:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-08-09 22:54:59 +00:00
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
2018-08-24 04:09:59 +00:00
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
2018-08-09 22:54:59 +00:00
|
|
|
}
|
2018-08-15 04:49:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
2018-08-24 04:09:59 +00:00
|
|
|
chainService, err := NewChainService(ctx, cfg)
|
2018-07-19 16:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup chain service: %v", err)
|
|
|
|
}
|
2018-09-18 13:06:28 +00:00
|
|
|
|
2018-09-27 02:34:35 +00:00
|
|
|
chainService.slotAlignmentDuration = 0
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.IncomingBlockFeed()
|
|
|
|
chainService.CanonicalBlockBySlotNumber(0)
|
|
|
|
chainService.CheckForCanonicalBlockBySlot(0)
|
2018-09-21 14:32:20 +00:00
|
|
|
chainService.CanonicalHead()
|
|
|
|
chainService.CanonicalCrystallizedState()
|
2018-09-18 13:06:28 +00:00
|
|
|
|
|
|
|
// Test the start function.
|
2018-08-20 15:50:11 +00:00
|
|
|
chainService.Start()
|
2018-07-19 16:31:50 +00:00
|
|
|
|
2018-08-24 04:09:59 +00:00
|
|
|
cfg = &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
|
|
|
}
|
|
|
|
chainService, err = NewChainService(ctx, cfg)
|
2018-08-20 15:50:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup chain service: %v", err)
|
|
|
|
}
|
2018-09-27 02:34:35 +00:00
|
|
|
|
|
|
|
chainService.slotAlignmentDuration = 0
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
chainService.Start()
|
|
|
|
|
2018-08-29 00:55:56 +00:00
|
|
|
if len(chainService.CurrentActiveState().RecentBlockHashes()) != 128 {
|
2018-08-15 04:49:59 +00:00
|
|
|
t.Errorf("incorrect recent block hashes")
|
|
|
|
}
|
2018-09-02 16:44:03 +00:00
|
|
|
|
2018-09-28 06:48:39 +00:00
|
|
|
if len(chainService.CurrentCrystallizedState().Validators()) != params.GetConfig().BootstrappedValidatorsCount {
|
2018-08-15 04:49:59 +00:00
|
|
|
t.Errorf("incorrect default validator size")
|
|
|
|
}
|
2018-09-13 15:24:18 +00:00
|
|
|
blockExists, err := chainService.ContainsBlock([32]byte{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to check if block exists: %v", err)
|
|
|
|
}
|
|
|
|
if blockExists {
|
2018-08-15 04:49:59 +00:00
|
|
|
t.Errorf("chain is not empty")
|
|
|
|
}
|
|
|
|
hasState, err := chainService.HasStoredState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("calling HasStoredState failed")
|
|
|
|
}
|
|
|
|
if hasState {
|
|
|
|
t.Errorf("has stored state should return false")
|
|
|
|
}
|
2018-08-24 04:09:59 +00:00
|
|
|
chainService.CanonicalBlockFeed()
|
|
|
|
chainService.CanonicalCrystallizedStateFeed()
|
2018-08-20 15:50:11 +00:00
|
|
|
|
2018-08-24 04:09:59 +00:00
|
|
|
chainService, _ = NewChainService(ctx, cfg)
|
2018-07-19 16:31:50 +00:00
|
|
|
|
2018-08-29 23:21:15 +00:00
|
|
|
active := types.NewActiveState(&pb.ActiveState{RecentBlockHashes: [][]byte{{'A'}}}, make(map[[32]byte]*types.VoteCache))
|
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
activeStateHash, err := active.Hash()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot hash active state: %v", err)
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
2018-08-15 04:49:59 +00:00
|
|
|
chainService.chain.SetActiveState(active)
|
2018-07-19 16:31:50 +00:00
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
crystallized := types.NewCrystallizedState(&pb.CrystallizedState{LastStateRecalc: 10000})
|
|
|
|
crystallizedStateHash, err := crystallized.Hash()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot hash crystallized state: %v", err)
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
2018-08-15 04:49:59 +00:00
|
|
|
chainService.chain.SetCrystallizedState(crystallized)
|
2018-07-19 16:31:50 +00:00
|
|
|
|
2018-09-11 05:09:41 +00:00
|
|
|
parentBlock := types.NewBlock(nil)
|
2018-08-15 04:49:59 +00:00
|
|
|
parentHash, _ := parentBlock.Hash()
|
|
|
|
|
2018-09-11 05:09:41 +00:00
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
2018-08-15 04:49:59 +00:00
|
|
|
SlotNumber: 2,
|
|
|
|
ActiveStateHash: activeStateHash[:],
|
|
|
|
CrystallizedStateHash: crystallizedStateHash[:],
|
|
|
|
ParentHash: parentHash[:],
|
|
|
|
PowChainRef: []byte("a"),
|
|
|
|
})
|
|
|
|
if err := chainService.SaveBlock(block); err != nil {
|
|
|
|
t.Errorf("save block should have failed")
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
// Save states so HasStoredState state should return true.
|
2018-08-29 23:21:15 +00:00
|
|
|
chainService.chain.SetActiveState(types.NewActiveState(&pb.ActiveState{}, make(map[[32]byte]*types.VoteCache)))
|
2018-08-15 04:49:59 +00:00
|
|
|
chainService.chain.SetCrystallizedState(types.NewCrystallizedState(&pb.CrystallizedState{}))
|
|
|
|
hasState, _ = chainService.HasStoredState()
|
|
|
|
if !hasState {
|
|
|
|
t.Errorf("has stored state should return false")
|
2018-08-13 02:07:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
if err := chainService.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to stop chain service: %v", err)
|
2018-07-31 04:41:27 +00:00
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 04:49:59 +00:00
|
|
|
|
|
|
|
func TestFaultyStop(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-08-24 04:09:59 +00:00
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
2018-08-15 04:49:59 +00:00
|
|
|
db, err := database.NewDB(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
client := &mockClient{}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}}, client, client, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-08-15 04:49:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
2018-08-24 04:09:59 +00:00
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
|
|
|
}
|
2018-08-15 04:49:59 +00:00
|
|
|
|
2018-08-24 04:09:59 +00:00
|
|
|
chainService, err := NewChainService(ctx, cfg)
|
2018-08-15 04:49:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-27 02:34:35 +00:00
|
|
|
chainService.slotAlignmentDuration = 0
|
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
chainService.Start()
|
|
|
|
|
2018-08-29 23:21:15 +00:00
|
|
|
chainService.chain.SetActiveState(types.NewActiveState(nil, make(map[[32]byte]*types.VoteCache)))
|
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
err = chainService.Stop()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("chain stop should have failed with persist active state")
|
|
|
|
}
|
|
|
|
|
2018-08-29 23:21:15 +00:00
|
|
|
chainService.chain.SetActiveState(types.NewActiveState(&pb.ActiveState{}, make(map[[32]byte]*types.VoteCache)))
|
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
chainService.chain.SetCrystallizedState(types.NewCrystallizedState(nil))
|
|
|
|
err = chainService.Stop()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("chain stop should have failed with persist crystallized state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
func TestCurrentBeaconSlot(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
|
|
|
db, err := database.NewDB(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
client := &faultyClient{}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(
|
|
|
|
ctx,
|
|
|
|
&powchain.Web3ServiceConfig{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Pubkey: "",
|
|
|
|
VrcAddr: common.Address{},
|
|
|
|
},
|
|
|
|
client,
|
|
|
|
client,
|
|
|
|
client,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-09-21 14:32:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
|
|
|
}
|
|
|
|
chainService, _ := NewChainService(ctx, cfg)
|
|
|
|
chainService.genesisTimestamp = time.Now()
|
2018-10-02 00:54:45 +00:00
|
|
|
if chainService.CurrentBeaconSlot() != 0 {
|
2018-09-21 14:32:20 +00:00
|
|
|
t.Errorf("Expected us to be in the 0th slot, received %v", chainService.CurrentBeaconSlot())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
func TestRunningChainServiceFaultyPOWChain(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
|
|
|
db, err := database.NewDB(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
client := &faultyClient{}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}}, client, client, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-09-18 13:06:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
2018-10-02 02:04:37 +00:00
|
|
|
EnablePOWChain: true,
|
2018-09-18 13:06:28 +00:00
|
|
|
}
|
|
|
|
chainService, _ := NewChainService(ctx, cfg)
|
|
|
|
|
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
|
|
|
SlotNumber: 1,
|
|
|
|
PowChainRef: []byte("a"),
|
|
|
|
})
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
chainService.blockProcessing()
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := chainService.SaveBlock(block); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chainService.incomingBlockChan <- block
|
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, "Proof-of-Work chain reference in block does not exist")
|
|
|
|
}
|
|
|
|
|
2018-08-29 00:55:56 +00:00
|
|
|
func TestRunningChainService(t *testing.T) {
|
2018-08-18 03:34:56 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2018-08-15 04:49:59 +00:00
|
|
|
ctx := context.Background()
|
2018-08-24 04:09:59 +00:00
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
2018-08-15 04:49:59 +00:00
|
|
|
db, err := database.NewDB(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
client := &mockClient{}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}}, client, client, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-08-15 04:49:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
2018-08-18 03:34:56 +00:00
|
|
|
|
2018-09-11 05:09:41 +00:00
|
|
|
active := types.NewGenesisActiveState()
|
2018-09-27 06:58:00 +00:00
|
|
|
crystallized, err := types.NewGenesisCrystallizedState("")
|
2018-08-18 03:34:56 +00:00
|
|
|
if err != nil {
|
2018-09-04 23:18:55 +00:00
|
|
|
t.Fatalf("Can't generate genesis state: %v", err)
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 23:18:55 +00:00
|
|
|
activeStateHash, _ := active.Hash()
|
|
|
|
crystallizedStateHash, _ := crystallized.Hash()
|
2018-08-15 04:49:59 +00:00
|
|
|
|
2018-08-24 04:09:59 +00:00
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
|
|
|
}
|
|
|
|
chainService, _ := NewChainService(ctx, cfg)
|
2018-08-15 04:49:59 +00:00
|
|
|
|
2018-10-02 18:34:26 +00:00
|
|
|
genesis, err := beaconChain.genesisBlock()
|
2018-09-04 23:18:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get canonical head: %v", err)
|
2018-08-24 04:09:59 +00:00
|
|
|
}
|
2018-09-21 19:33:53 +00:00
|
|
|
beaconChain.saveBlock(genesis)
|
2018-09-04 23:18:55 +00:00
|
|
|
parentHash, err := genesis.Hash()
|
2018-08-15 04:49:59 +00:00
|
|
|
if err != nil {
|
2018-09-04 23:18:55 +00:00
|
|
|
t.Fatalf("unable to get hash of canonical head: %v", err)
|
2018-08-15 04:49:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 06:48:39 +00:00
|
|
|
secondsSinceGenesis := time.Since(params.GetConfig().GenesisTime).Seconds()
|
|
|
|
currentSlot := uint64(math.Floor(secondsSinceGenesis / float64(params.GetConfig().SlotDuration)))
|
2018-09-21 19:33:53 +00:00
|
|
|
|
2018-09-28 06:48:39 +00:00
|
|
|
slotsStart := crystallized.LastStateRecalc() - params.GetConfig().CycleLength
|
|
|
|
slotIndex := (currentSlot - slotsStart) % params.GetConfig().CycleLength
|
2018-09-21 19:33:53 +00:00
|
|
|
shardID := crystallized.ShardAndCommitteesForSlots()[slotIndex].ArrayShardAndCommittee[0].ShardId
|
|
|
|
|
2018-09-11 05:09:41 +00:00
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
2018-09-21 19:33:53 +00:00
|
|
|
SlotNumber: currentSlot,
|
2018-08-15 04:49:59 +00:00
|
|
|
ActiveStateHash: activeStateHash[:],
|
|
|
|
CrystallizedStateHash: crystallizedStateHash[:],
|
|
|
|
ParentHash: parentHash[:],
|
|
|
|
PowChainRef: []byte("a"),
|
2018-09-15 14:51:17 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{{
|
2018-09-21 19:33:53 +00:00
|
|
|
Slot: currentSlot,
|
|
|
|
AttesterBitfield: []byte{128, 0},
|
|
|
|
ShardId: shardID,
|
|
|
|
JustifiedBlockHash: parentHash[:],
|
2018-09-04 23:18:55 +00:00
|
|
|
}},
|
2018-08-15 04:49:59 +00:00
|
|
|
})
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
blockNoParent := types.NewBlock(&pb.BeaconBlock{
|
2018-09-21 19:33:53 +00:00
|
|
|
SlotNumber: currentSlot,
|
2018-09-18 13:06:28 +00:00
|
|
|
PowChainRef: []byte("a"),
|
|
|
|
})
|
|
|
|
|
2018-08-25 18:59:46 +00:00
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.blockProcessing()
|
2018-08-25 18:59:46 +00:00
|
|
|
<-exitRoutine
|
|
|
|
}()
|
2018-09-18 13:06:28 +00:00
|
|
|
|
2018-08-24 04:09:59 +00:00
|
|
|
if err := chainService.SaveBlock(block); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.incomingBlockChan <- blockNoParent
|
2018-08-24 04:09:59 +00:00
|
|
|
chainService.incomingBlockChan <- block
|
2018-08-15 04:49:59 +00:00
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
2018-09-21 19:33:53 +00:00
|
|
|
testutil.WaitForLog(t, hook, "Chain service context closed, exiting goroutine")
|
2018-09-18 13:06:28 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Block points to nil parent")
|
|
|
|
testutil.AssertLogsContain(t, hook, "Finished processing received block")
|
|
|
|
}
|
2018-08-25 18:59:46 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
func TestBlockSlotNumberByHash(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
|
|
|
db, err := database.NewDB(config)
|
2018-09-15 14:51:17 +00:00
|
|
|
if err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
|
|
|
|
2018-09-15 14:51:17 +00:00
|
|
|
}
|
2018-09-18 13:06:28 +00:00
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
client := &mockClient{}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}}, client, client, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-09-15 14:51:17 +00:00
|
|
|
if err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
2018-09-15 14:51:17 +00:00
|
|
|
}
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService, _ := NewChainService(ctx, cfg)
|
|
|
|
|
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
|
|
|
SlotNumber: 1,
|
|
|
|
})
|
|
|
|
hash, err := block.Hash()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-09-15 14:51:17 +00:00
|
|
|
}
|
2018-09-18 13:06:28 +00:00
|
|
|
if err := chainService.SaveBlock(block); err != nil {
|
|
|
|
t.Fatal(err)
|
2018-09-15 14:51:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
slot, err := chainService.BlockSlotNumberByHash(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if slot != 1 {
|
|
|
|
t.Errorf("Expected slot 1, received %d", slot)
|
|
|
|
}
|
|
|
|
_, err = chainService.BlockSlotNumberByHash([32]byte{})
|
|
|
|
if !strings.Contains(err.Error(), "could not get block from DB") {
|
|
|
|
t.Errorf("Received incorrect error, expected could not get block from DB, received %v", err)
|
|
|
|
}
|
2018-08-24 04:09:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
func TestDoesPOWBlockExist(t *testing.T) {
|
2018-08-24 04:09:59 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
|
|
|
db, err := database.NewDB(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-09-18 13:06:28 +00:00
|
|
|
client := &faultyClient{}
|
2018-08-24 04:09:59 +00:00
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}}, client, client, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-08-24 04:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
IncomingBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
|
|
|
}
|
|
|
|
chainService, _ := NewChainService(ctx, cfg)
|
2018-09-11 05:09:41 +00:00
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
2018-09-18 13:06:28 +00:00
|
|
|
SlotNumber: 10,
|
2018-08-24 04:09:59 +00:00
|
|
|
})
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
// Using a faulty client should throw error.
|
|
|
|
exists := chainService.doesPoWBlockExist(block)
|
|
|
|
if exists {
|
|
|
|
t.Error("Block corresponding to nil powchain reference should not exist")
|
2018-09-02 16:44:03 +00:00
|
|
|
}
|
2018-09-18 13:06:28 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "fetching PoW block corresponding to mainchain reference failed")
|
2018-08-15 04:49:59 +00:00
|
|
|
}
|
2018-08-24 16:07:23 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
func TestUpdateHead(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2018-08-29 00:55:56 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
|
|
|
db, err := database.NewDB(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
}
|
2018-08-29 00:55:56 +00:00
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
client := &mockClient{}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}}, client, client, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-08-29 00:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
|
|
|
cfg := &Config{
|
2018-09-18 13:06:28 +00:00
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
IncomingBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
2018-08-29 00:55:56 +00:00
|
|
|
}
|
|
|
|
chainService, _ := NewChainService(ctx, cfg)
|
|
|
|
|
2018-09-11 05:09:41 +00:00
|
|
|
active := types.NewGenesisActiveState()
|
2018-09-27 06:58:00 +00:00
|
|
|
crystallized, err := types.NewGenesisCrystallizedState("")
|
2018-08-29 00:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't generate genesis state: %v", err)
|
|
|
|
}
|
|
|
|
activeStateHash, _ := active.Hash()
|
|
|
|
crystallizedStateHash, _ := crystallized.Hash()
|
|
|
|
|
2018-09-26 15:34:23 +00:00
|
|
|
genesis := types.NewGenesisBlock(activeStateHash, crystallizedStateHash)
|
2018-09-18 13:06:28 +00:00
|
|
|
genesisHash, err := genesis.Hash()
|
2018-08-29 00:55:56 +00:00
|
|
|
if err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatalf("Could not get genesis block hash: %v", err)
|
2018-08-29 00:55:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
|
|
|
SlotNumber: 64,
|
2018-08-29 00:55:56 +00:00
|
|
|
ActiveStateHash: activeStateHash[:],
|
|
|
|
CrystallizedStateHash: crystallizedStateHash[:],
|
2018-09-18 13:06:28 +00:00
|
|
|
ParentHash: genesisHash[:],
|
|
|
|
PowChainRef: []byte("a"),
|
2018-08-29 00:55:56 +00:00
|
|
|
})
|
2018-09-18 13:06:28 +00:00
|
|
|
hash, err := block.Hash()
|
2018-08-29 00:55:56 +00:00
|
|
|
if err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatalf("Could not hash block: %v", err)
|
2018-08-29 00:55:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
timeChan := make(chan time.Time)
|
|
|
|
go func() {
|
|
|
|
chainService.updateHead(timeChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
2018-08-29 00:55:56 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
if err := chainService.SaveBlock(block); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-08-29 00:55:56 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
// If blocks pending processing is empty, the updateHead routine does nothing.
|
|
|
|
chainService.blocksPendingProcessing = [][32]byte{}
|
|
|
|
timeChan <- time.Now()
|
2018-08-29 00:55:56 +00:00
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService, _ = NewChainService(ctx, cfg)
|
|
|
|
go func() {
|
|
|
|
chainService.updateHead(timeChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
2018-09-04 23:18:55 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
// If blocks pending processing contains a hash of a block that does not exist
|
|
|
|
// in persistent storage, we expect an error log to be thrown as
|
|
|
|
// that is unexpected behavior given the block should have been saved during
|
|
|
|
// processing.
|
|
|
|
fakeBlock := types.NewBlock(&pb.BeaconBlock{SlotNumber: 100})
|
|
|
|
fakeBlockHash, err := fakeBlock.Hash()
|
2018-09-04 23:18:55 +00:00
|
|
|
if err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatal(err)
|
2018-09-04 23:18:55 +00:00
|
|
|
}
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.blocksPendingProcessing = [][32]byte{}
|
|
|
|
chainService.blocksPendingProcessing = append(chainService.blocksPendingProcessing, fakeBlockHash)
|
|
|
|
timeChan <- time.Now()
|
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
2018-09-04 23:18:55 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
// Inexistent parent hash should log an error in updateHead.
|
|
|
|
noParentBlock := types.NewBlock(&pb.BeaconBlock{
|
|
|
|
SlotNumber: 64,
|
|
|
|
})
|
|
|
|
noParentBlockHash, err := noParentBlock.Hash()
|
2018-09-04 23:18:55 +00:00
|
|
|
if err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatalf("Could not hash block: %v", err)
|
2018-09-04 23:18:55 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService, _ = NewChainService(ctx, cfg)
|
|
|
|
go func() {
|
|
|
|
chainService.updateHead(timeChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
2018-09-04 23:18:55 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
if err := chainService.SaveBlock(noParentBlock); err != nil {
|
|
|
|
t.Fatal(err)
|
2018-09-04 23:18:55 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.blocksPendingProcessing = [][32]byte{}
|
|
|
|
chainService.blocksPendingProcessing = append(chainService.blocksPendingProcessing, noParentBlockHash)
|
2018-09-04 23:18:55 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
timeChan <- time.Now()
|
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
2018-09-04 23:18:55 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
// Now we test the correct, end-to-end updateHead functionality.
|
|
|
|
chainService, _ = NewChainService(ctx, cfg)
|
2018-09-04 23:18:55 +00:00
|
|
|
go func() {
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.updateHead(timeChan)
|
2018-09-04 23:18:55 +00:00
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.blocksPendingProcessing = [][32]byte{}
|
|
|
|
chainService.blocksPendingProcessing = append(chainService.blocksPendingProcessing, hash)
|
2018-09-04 23:18:55 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
timeChan <- time.Now()
|
2018-09-04 23:18:55 +00:00
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
2018-09-18 13:06:28 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Could not get block")
|
|
|
|
testutil.AssertLogsContain(t, hook, "Failed to get parent of block")
|
|
|
|
testutil.AssertLogsContain(t, hook, "Canonical block determined")
|
2018-09-04 23:18:55 +00:00
|
|
|
}
|
2018-09-09 01:52:18 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
func TestProcessBlocksWithCorrectAttestations(t *testing.T) {
|
2018-09-12 03:17:20 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
config := &database.DBConfig{DataDir: "", Name: "", InMemory: true}
|
|
|
|
db, err := database.NewDB(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint := "ws://127.0.0.1"
|
|
|
|
client := &mockClient{}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}}, client, client, client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2018-09-27 06:58:00 +00:00
|
|
|
beaconChain, err := NewBeaconChain("", db.DB())
|
2018-09-12 03:17:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not register blockchain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
|
|
|
BeaconDB: db.DB(),
|
|
|
|
Chain: beaconChain,
|
|
|
|
Web3Service: web3Service,
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService, _ := NewChainService(ctx, cfg)
|
|
|
|
|
|
|
|
active := types.NewGenesisActiveState()
|
2018-09-27 06:58:00 +00:00
|
|
|
crystallized, err := types.NewGenesisCrystallizedState("")
|
2018-09-18 13:06:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't generate genesis state: %v", err)
|
2018-09-12 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
activeStateHash, _ := active.Hash()
|
|
|
|
crystallizedStateHash, _ := crystallized.Hash()
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
chainService.blockProcessing()
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
2018-09-12 03:17:20 +00:00
|
|
|
|
2018-09-14 05:07:30 +00:00
|
|
|
block0 := types.NewBlock(&pb.BeaconBlock{
|
2018-09-21 19:33:53 +00:00
|
|
|
SlotNumber: 0,
|
2018-09-14 05:07:30 +00:00
|
|
|
})
|
|
|
|
if saveErr := beaconChain.saveBlock(block0); saveErr != nil {
|
|
|
|
t.Fatalf("Cannot save block: %v", saveErr)
|
|
|
|
}
|
|
|
|
block0Hash, err := block0.Hash()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to compute block's hash: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-28 06:48:39 +00:00
|
|
|
secondsSinceGenesis := time.Since(params.GetConfig().GenesisTime).Seconds()
|
|
|
|
currentSlot := uint64(math.Floor(secondsSinceGenesis / float64(params.GetConfig().SlotDuration)))
|
2018-09-21 19:33:53 +00:00
|
|
|
|
2018-09-12 03:17:20 +00:00
|
|
|
block1 := types.NewBlock(&pb.BeaconBlock{
|
2018-09-14 05:07:30 +00:00
|
|
|
ParentHash: block0Hash[:],
|
2018-09-21 19:33:53 +00:00
|
|
|
SlotNumber: currentSlot,
|
2018-09-12 03:17:20 +00:00
|
|
|
ActiveStateHash: activeStateHash[:],
|
|
|
|
CrystallizedStateHash: crystallizedStateHash[:],
|
2018-09-15 14:51:17 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{{
|
2018-09-21 19:33:53 +00:00
|
|
|
Slot: currentSlot,
|
2018-09-18 13:06:28 +00:00
|
|
|
AttesterBitfield: []byte{16, 0},
|
2018-09-12 03:17:20 +00:00
|
|
|
ShardId: 0,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
exitRoutine = make(chan bool)
|
2018-09-12 03:17:20 +00:00
|
|
|
go func() {
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.blockProcessing()
|
2018-09-12 03:17:20 +00:00
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
|
|
|
chainService.incomingBlockChan <- block1
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
block1Hash, err := block1.Hash()
|
2018-09-15 14:51:17 +00:00
|
|
|
if err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatalf("unable to get hash of block 1: %v", err)
|
2018-09-15 14:51:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:33:53 +00:00
|
|
|
currentSlot++
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
// Add 1 more attestation field for slot2
|
|
|
|
block2 := types.NewBlock(&pb.BeaconBlock{
|
|
|
|
ParentHash: block1Hash[:],
|
2018-09-21 19:33:53 +00:00
|
|
|
SlotNumber: currentSlot,
|
2018-09-18 13:06:28 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{
|
2018-09-21 19:33:53 +00:00
|
|
|
{Slot: currentSlot - 1, AttesterBitfield: []byte{8, 0}, ShardId: 0},
|
|
|
|
{Slot: currentSlot, AttesterBitfield: []byte{8, 0}, ShardId: 0},
|
2018-09-18 13:06:28 +00:00
|
|
|
}})
|
|
|
|
block2Hash, err := block2.Hash()
|
2018-09-15 14:51:17 +00:00
|
|
|
if err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatalf("unable to get hash of block 1: %v", err)
|
2018-09-15 14:51:17 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:33:53 +00:00
|
|
|
currentSlot++
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
// Add 1 more attestation field for slot3
|
|
|
|
block3 := types.NewBlock(&pb.BeaconBlock{
|
|
|
|
ParentHash: block2Hash[:],
|
2018-09-21 19:33:53 +00:00
|
|
|
SlotNumber: currentSlot,
|
2018-09-18 13:06:28 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{
|
2018-09-21 19:33:53 +00:00
|
|
|
{Slot: currentSlot - 2, AttesterBitfield: []byte{4, 0}, ShardId: 0},
|
|
|
|
{Slot: currentSlot - 1, AttesterBitfield: []byte{4, 0}, ShardId: 0},
|
|
|
|
{Slot: currentSlot, AttesterBitfield: []byte{4, 0}, ShardId: 0},
|
2018-09-18 13:06:28 +00:00
|
|
|
}})
|
2018-09-15 14:51:17 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.incomingBlockChan <- block1
|
|
|
|
chainService.incomingBlockChan <- block2
|
|
|
|
chainService.incomingBlockChan <- block3
|
2018-09-15 14:51:17 +00:00
|
|
|
|
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
|
|
|
}
|