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-11 05:09:41 +00:00
|
|
|
"math/big"
|
2019-01-01 17:17:44 +00:00
|
|
|
"strconv"
|
2018-07-19 16:31:50 +00:00
|
|
|
"testing"
|
2019-01-05 05:39:34 +00:00
|
|
|
"time"
|
2018-07-19 16:31:50 +00:00
|
|
|
|
2018-12-01 22:09:12 +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-12-01 22:09:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2018-12-23 22:51:04 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-12-20 22:00:38 +00:00
|
|
|
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2018-12-23 22:51:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2018-10-05 17:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2018-11-07 19:07:41 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
|
2018-07-22 16:58:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
2018-08-15 04:49:59 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-01-13 20:52:31 +00:00
|
|
|
bytesutil "github.com/prysmaticlabs/prysm/shared/bytes"
|
2018-12-23 22:51:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2018-12-19 05:18:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
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{}
|
|
|
|
|
2019-01-16 14:01:21 +00:00
|
|
|
func (m *mockClient) SubscribeNewHead(ctx context.Context, ch chan<- *gethTypes.Header) (ethereum.Subscription, error) {
|
2018-08-15 04:49:59 +00:00
|
|
|
return new(event.Feed).Subscribe(ch), nil
|
|
|
|
}
|
|
|
|
|
2019-01-16 14:01:21 +00:00
|
|
|
func (m *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
|
|
|
}
|
|
|
|
|
2019-01-16 14:01:21 +00:00
|
|
|
func (m *mockClient) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- gethTypes.Log) (ethereum.Subscription, error) {
|
2018-08-15 04:49:59 +00:00
|
|
|
return new(event.Feed).Subscribe(ch), nil
|
|
|
|
}
|
|
|
|
|
2019-01-16 14:01:21 +00:00
|
|
|
func (m *mockClient) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return []byte{'t', 'e', 's', 't'}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockClient) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return []byte{'t', 'e', 's', 't'}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockClient) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]gethTypes.Log, error) {
|
|
|
|
logs := make([]gethTypes.Log, 3)
|
|
|
|
for i := 0; i < len(logs); i++ {
|
|
|
|
logs[i].Address = common.Address{}
|
|
|
|
logs[i].Topics = make([]common.Hash, 5)
|
|
|
|
logs[i].Topics[0] = common.Hash{'a'}
|
|
|
|
logs[i].Topics[1] = common.Hash{'b'}
|
|
|
|
logs[i].Topics[2] = common.Hash{'c'}
|
|
|
|
|
|
|
|
}
|
|
|
|
return logs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockClient) LatestBlockHash() common.Hash {
|
2018-08-15 04:49:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-16 14:01:21 +00:00
|
|
|
func (f *faultyClient) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]gethTypes.Log, error) {
|
|
|
|
return nil, errors.New("unable to retrieve logs")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *faultyClient) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return []byte{}, errors.New("unable to retrieve contract code")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *faultyClient) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
|
|
|
|
return []byte{}, errors.New("unable to retrieve contract code")
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
func (f *faultyClient) LatestBlockHash() common.Hash {
|
|
|
|
return common.BytesToHash([]byte{'A'})
|
|
|
|
}
|
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
func setupBeaconChain(t *testing.T, faultyPoWClient bool, beaconDB *db.BeaconDB) *ChainService {
|
2018-07-22 16:58:14 +00:00
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-10-05 17:14:50 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
var web3Service *powchain.Web3Service
|
2018-10-17 06:11:24 +00:00
|
|
|
var err error
|
2018-10-05 17:14:50 +00:00
|
|
|
if faultyPoWClient {
|
|
|
|
client := &faultyClient{}
|
2018-11-16 17:01:41 +00:00
|
|
|
web3Service, err = powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{
|
2019-01-17 15:14:32 +00:00
|
|
|
Endpoint: endpoint,
|
|
|
|
DepositContract: common.Address{},
|
|
|
|
Reader: client,
|
|
|
|
Client: client,
|
|
|
|
Logger: client,
|
2018-11-16 17:01:41 +00:00
|
|
|
})
|
2018-10-05 17:14:50 +00:00
|
|
|
} else {
|
|
|
|
client := &mockClient{}
|
2018-11-16 17:01:41 +00:00
|
|
|
web3Service, err = powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{
|
2019-01-17 15:14:32 +00:00
|
|
|
Endpoint: endpoint,
|
|
|
|
DepositContract: common.Address{},
|
|
|
|
Reader: client,
|
|
|
|
Client: client,
|
|
|
|
Logger: client,
|
2018-11-16 17:01:41 +00:00
|
|
|
})
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-07-22 16:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
2019-01-09 03:03:57 +00:00
|
|
|
if err := beaconDB.InitializeState(); err != nil {
|
2018-12-01 22:09:12 +00:00
|
|
|
t.Fatalf("failed to initialize state: %v", err)
|
2018-10-17 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 22:54:59 +00:00
|
|
|
cfg := &Config{
|
|
|
|
BeaconBlockBuf: 0,
|
2018-10-17 06:11:24 +00:00
|
|
|
BeaconDB: beaconDB,
|
2018-10-05 17:14:50 +00:00
|
|
|
Web3Service: web3Service,
|
|
|
|
EnablePOWChain: true,
|
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-10-05 17:14:50 +00:00
|
|
|
return chainService
|
|
|
|
}
|
|
|
|
|
2018-12-19 05:18:42 +00:00
|
|
|
func SetSlotInState(service *ChainService, slot uint64) error {
|
2019-01-21 09:34:11 +00:00
|
|
|
bState, err := service.beaconDB.State()
|
2018-12-19 05:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-23 22:51:04 +00:00
|
|
|
bState.Slot = slot
|
2018-12-19 05:18:42 +00:00
|
|
|
return service.beaconDB.SaveState(bState)
|
|
|
|
}
|
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
func TestStartStop(t *testing.T) {
|
2018-11-07 19:07:41 +00:00
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2018-10-17 06:11:24 +00:00
|
|
|
chainService := setupBeaconChain(t, false, db)
|
2018-09-27 02:34:35 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.IncomingBlockFeed()
|
|
|
|
|
|
|
|
// Test the start function.
|
2018-08-20 15:50:11 +00:00
|
|
|
chainService.Start()
|
2018-07-19 16:31:50 +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
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
func TestRunningChainServiceFaultyPOWChain(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2018-11-07 19:07:41 +00:00
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2018-10-17 06:11:24 +00:00
|
|
|
chainService := setupBeaconChain(t, true, db)
|
2018-09-18 13:06:28 +00:00
|
|
|
|
2018-12-19 05:18:42 +00:00
|
|
|
if err := SetSlotInState(chainService, 1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
parentBlock := &pb.BeaconBlock{
|
2018-12-19 05:18:42 +00:00
|
|
|
Slot: 1,
|
2018-12-20 22:00:38 +00:00
|
|
|
}
|
2018-12-19 05:18:42 +00:00
|
|
|
|
2019-01-14 16:41:20 +00:00
|
|
|
parentHash, err := hashutil.HashBeaconBlock(parentBlock)
|
2018-12-19 05:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to hash block %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := chainService.beaconDB.SaveBlock(parentBlock); err != nil {
|
|
|
|
t.Fatalf("Unable to save block %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
block := &pb.BeaconBlock{
|
2019-01-09 04:42:42 +00:00
|
|
|
Slot: 2,
|
|
|
|
ParentRootHash32: parentHash[:],
|
|
|
|
DepositRootHash32: []byte("a"),
|
2018-12-20 22:00:38 +00:00
|
|
|
}
|
2018-09-18 13:06:28 +00:00
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
blockChan := make(chan *pb.BeaconBlock)
|
2018-09-18 13:06:28 +00:00
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
2018-10-14 15:29:57 +00:00
|
|
|
chainService.blockProcessing(blockChan)
|
2018-09-18 13:06:28 +00:00
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
if err := chainService.beaconDB.SaveBlock(block); err != nil {
|
2018-09-18 13:06:28 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chainService.incomingBlockChan <- block
|
2018-11-06 20:48:11 +00:00
|
|
|
<-blockChan
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
|
|
|
|
2018-12-19 05:18:42 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "unable to retrieve POW chain reference block failed")
|
2018-09-18 13:06:28 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-11-07 19:07:41 +00:00
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2018-10-17 06:11:24 +00:00
|
|
|
chainService := setupBeaconChain(t, false, db)
|
2019-01-01 17:17:44 +00:00
|
|
|
deposits := make([]*pb.Deposit, params.BeaconConfig().DepositsForChainStart)
|
|
|
|
for i := 0; i < len(deposits); i++ {
|
2019-01-05 05:39:34 +00:00
|
|
|
depositInput := &pb.DepositInput{
|
|
|
|
Pubkey: []byte(strconv.Itoa(i)),
|
|
|
|
RandaoCommitmentHash32: []byte{41, 13, 236, 217, 84, 139, 98, 168, 214, 3, 69,
|
|
|
|
169, 136, 56, 111, 200, 75, 166, 188, 149, 72, 64, 8, 246, 54, 47, 147, 22, 14, 243, 229, 99},
|
|
|
|
}
|
|
|
|
depositData, err := b.EncodeDepositData(
|
|
|
|
depositInput,
|
|
|
|
params.BeaconConfig().MaxDepositInGwei,
|
|
|
|
time.Now().Unix(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not encode deposit input: %v", err)
|
|
|
|
}
|
|
|
|
deposits[i] = &pb.Deposit{DepositData: depositData}
|
2019-01-01 17:17:44 +00:00
|
|
|
}
|
|
|
|
beaconState, err := state.InitialBeaconState(deposits, 0, nil)
|
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-12-27 03:15:12 +00:00
|
|
|
beaconState.Slot = 5
|
2018-08-18 03:34:56 +00:00
|
|
|
|
2018-12-23 22:51:04 +00:00
|
|
|
enc, _ := proto.Marshal(beaconState)
|
|
|
|
stateRoot := hashutil.Hash(enc)
|
2018-08-15 04:49:59 +00:00
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
genesis := b.NewGenesisBlock([]byte{})
|
2018-12-23 22:51:04 +00:00
|
|
|
if err := chainService.beaconDB.SaveBlock(genesis); err != nil {
|
|
|
|
t.Fatalf("could not save block to db: %v", err)
|
|
|
|
}
|
2019-01-14 16:41:20 +00:00
|
|
|
parentHash, err := hashutil.HashBeaconBlock(genesis)
|
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
|
|
|
}
|
2019-01-01 17:17:44 +00:00
|
|
|
if err := chainService.beaconDB.SaveState(beaconState); err != nil {
|
|
|
|
t.Fatalf("Can't save state to db %v", err)
|
|
|
|
}
|
2019-01-21 09:34:11 +00:00
|
|
|
beaconState, err = chainService.beaconDB.State()
|
2018-12-19 05:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't get state from db %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-09 09:49:17 +00:00
|
|
|
var ShardCommittees []*pb.ShardCommitteeArray
|
2018-12-19 05:18:42 +00:00
|
|
|
for i := uint64(0); i < params.BeaconConfig().EpochLength*2; i++ {
|
2019-01-09 09:49:17 +00:00
|
|
|
ShardCommittees = append(ShardCommittees, &pb.ShardCommitteeArray{
|
|
|
|
ArrayShardCommittee: []*pb.ShardCommittee{
|
2018-12-19 05:18:42 +00:00
|
|
|
{Committee: []uint32{9, 8, 311, 12, 92, 1, 23, 17}},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-09 09:49:17 +00:00
|
|
|
beaconState.ShardCommitteesAtSlots = ShardCommittees
|
2018-12-19 05:18:42 +00:00
|
|
|
if err := chainService.beaconDB.SaveState(beaconState); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-27 03:15:12 +00:00
|
|
|
currentSlot := uint64(5)
|
2018-10-22 21:04:17 +00:00
|
|
|
attestationSlot := uint64(0)
|
2019-01-09 09:49:17 +00:00
|
|
|
shard := beaconState.ShardCommitteesAtSlots[attestationSlot].ArrayShardCommittee[0].Shard
|
2018-09-21 19:33:53 +00:00
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
block := &pb.BeaconBlock{
|
2019-01-09 04:42:42 +00:00
|
|
|
Slot: currentSlot + 1,
|
|
|
|
StateRootHash32: stateRoot[:],
|
|
|
|
ParentRootHash32: parentHash[:],
|
|
|
|
DepositRootHash32: []byte("a"),
|
2018-12-22 20:30:59 +00:00
|
|
|
Body: &pb.BeaconBlockBody{
|
|
|
|
Attestations: []*pb.Attestation{{
|
|
|
|
ParticipationBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
Data: &pb.AttestationData{
|
2019-01-01 17:17:44 +00:00
|
|
|
Slot: attestationSlot,
|
|
|
|
Shard: shard,
|
|
|
|
JustifiedBlockRootHash32: params.BeaconConfig().ZeroHash[:],
|
|
|
|
LatestCrosslinkRootHash32: params.BeaconConfig().ZeroHash[:],
|
2018-12-22 20:30:59 +00:00
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
2018-12-20 22:00:38 +00:00
|
|
|
}
|
2018-08-15 04:49:59 +00:00
|
|
|
|
2018-12-19 05:18:42 +00:00
|
|
|
if err := SetSlotInState(chainService, currentSlot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
blockChan := make(chan *pb.BeaconBlock)
|
2018-08-25 18:59:46 +00:00
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
2018-10-14 15:29:57 +00:00
|
|
|
chainService.blockProcessing(blockChan)
|
2018-08-25 18:59:46 +00:00
|
|
|
<-exitRoutine
|
|
|
|
}()
|
2018-09-18 13:06:28 +00:00
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
if err := chainService.beaconDB.SaveBlock(block); err != nil {
|
2018-08-24 04:09:59 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chainService.incomingBlockChan <- block
|
2018-10-14 15:29:57 +00:00
|
|
|
<-blockChan
|
2018-08-15 04:49:59 +00:00
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
2018-12-19 05:18:42 +00:00
|
|
|
|
2018-10-14 15:29:57 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Chain service context closed, exiting goroutine")
|
2018-12-01 22:09:12 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Processed beacon block")
|
2018-09-18 13:06:28 +00:00
|
|
|
}
|
2018-08-25 18:59:46 +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()
|
2018-11-07 19:07:41 +00:00
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2018-10-17 06:11:24 +00:00
|
|
|
chainService := setupBeaconChain(t, true, db)
|
2018-08-24 04:09:59 +00:00
|
|
|
|
2019-01-21 09:34:11 +00:00
|
|
|
beaconState, err := chainService.beaconDB.State()
|
2018-12-19 05:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to retrieve beacon state %v", err)
|
|
|
|
}
|
2018-08-24 04:09:59 +00:00
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
// Using a faulty client should throw error.
|
2019-01-13 14:04:14 +00:00
|
|
|
powHash := bytesutil.ToBytes32(beaconState.LatestDepositRootHash32)
|
2018-12-23 22:51:04 +00:00
|
|
|
exists := chainService.doesPoWBlockExist(powHash)
|
2018-09-18 13:06:28 +00:00
|
|
|
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-10-18 04:23:18 +00:00
|
|
|
func TestUpdateHead(t *testing.T) {
|
2019-01-01 17:17:44 +00:00
|
|
|
beaconState, err := state.InitialBeaconState(nil, 0, nil)
|
2018-10-18 04:23:18 +00:00
|
|
|
if err != nil {
|
2018-12-23 22:51:04 +00:00
|
|
|
t.Fatalf("Cannot create genesis beacon state: %v", err)
|
2018-10-18 04:23:18 +00:00
|
|
|
}
|
2018-12-23 22:51:04 +00:00
|
|
|
enc, _ := proto.Marshal(beaconState)
|
|
|
|
stateRoot := hashutil.Hash(enc)
|
2018-10-18 04:23:18 +00:00
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
genesis := b.NewGenesisBlock(stateRoot[:])
|
2019-01-14 16:41:20 +00:00
|
|
|
genesisHash, err := hashutil.HashBeaconBlock(genesis)
|
2018-10-18 04:23:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not get genesis block hash: %v", err)
|
|
|
|
}
|
|
|
|
// Table driven tests for various fork choice scenarios.
|
|
|
|
tests := []struct {
|
|
|
|
blockSlot uint64
|
2018-12-23 22:51:04 +00:00
|
|
|
state *pb.BeaconState
|
2018-10-18 04:23:18 +00:00
|
|
|
logAssert string
|
|
|
|
}{
|
|
|
|
// Higher slot but same crystallized state should trigger chain update.
|
|
|
|
{
|
|
|
|
blockSlot: 64,
|
2018-12-01 22:09:12 +00:00
|
|
|
state: beaconState,
|
2018-10-18 04:23:18 +00:00
|
|
|
logAssert: "Chain head block and state updated",
|
|
|
|
},
|
|
|
|
// Higher slot, different crystallized state, but higher last finalized slot.
|
|
|
|
{
|
|
|
|
blockSlot: 64,
|
2018-12-23 22:51:04 +00:00
|
|
|
state: &pb.BeaconState{FinalizedSlot: 10},
|
2018-10-18 04:23:18 +00:00
|
|
|
logAssert: "Chain head block and state updated",
|
|
|
|
},
|
|
|
|
// Higher slot, different crystallized state, same last finalized slot,
|
|
|
|
// but last justified slot.
|
|
|
|
{
|
|
|
|
blockSlot: 64,
|
2018-12-23 22:51:04 +00:00
|
|
|
state: &pb.BeaconState{
|
2018-12-11 15:46:01 +00:00
|
|
|
FinalizedSlot: 0,
|
|
|
|
JustifiedSlot: 10,
|
2018-12-23 22:51:04 +00:00
|
|
|
},
|
2018-10-18 04:23:18 +00:00
|
|
|
logAssert: "Chain head block and state updated",
|
|
|
|
},
|
|
|
|
// Same slot should not trigger a head update.
|
|
|
|
{
|
|
|
|
blockSlot: 0,
|
2018-12-01 22:09:12 +00:00
|
|
|
state: beaconState,
|
2018-10-18 04:23:18 +00:00
|
|
|
logAssert: "Chain head not updated",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
hook := logTest.NewGlobal()
|
2018-11-07 19:07:41 +00:00
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2018-10-18 04:23:18 +00:00
|
|
|
chainService := setupBeaconChain(t, false, db)
|
|
|
|
|
2018-12-23 22:51:04 +00:00
|
|
|
enc, _ := proto.Marshal(tt.state)
|
|
|
|
stateRoot := hashutil.Hash(enc)
|
2018-12-20 22:00:38 +00:00
|
|
|
block := &pb.BeaconBlock{
|
2019-01-09 04:42:42 +00:00
|
|
|
Slot: tt.blockSlot,
|
|
|
|
StateRootHash32: stateRoot[:],
|
|
|
|
ParentRootHash32: genesisHash[:],
|
|
|
|
DepositRootHash32: []byte("a"),
|
2018-12-20 22:00:38 +00:00
|
|
|
}
|
2019-01-14 16:41:20 +00:00
|
|
|
h, err := hashutil.HashBeaconBlock(block)
|
2018-10-18 04:23:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
2018-12-20 22:00:38 +00:00
|
|
|
blockChan := make(chan *pb.BeaconBlock)
|
2018-10-18 04:23:18 +00:00
|
|
|
go func() {
|
|
|
|
chainService.updateHead(blockChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := chainService.beaconDB.SaveBlock(block); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-01 22:09:12 +00:00
|
|
|
chainService.unfinalizedBlocks[h] = tt.state
|
2018-10-18 04:23:18 +00:00
|
|
|
|
|
|
|
// If blocks pending processing is empty, the updateHead routine does nothing.
|
|
|
|
blockChan <- block
|
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
|
|
|
|
|
|
|
testutil.AssertLogsContain(t, hook, tt.logAssert)
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 16:54:17 +00:00
|
|
|
|
2018-12-19 05:18:42 +00:00
|
|
|
func TestIsBlockReadyForProcessing(t *testing.T) {
|
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
|
|
|
chainService := setupBeaconChain(t, false, db)
|
2019-01-09 23:49:50 +00:00
|
|
|
err := db.InitializeState()
|
2018-12-19 05:18:42 +00:00
|
|
|
if err != nil {
|
2019-01-09 23:49:50 +00:00
|
|
|
t.Fatalf("Can't initialze genesis state: %v", err)
|
|
|
|
}
|
2019-01-21 09:34:11 +00:00
|
|
|
beaconState, err := db.State()
|
2019-01-09 23:49:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't get genesis state: %v", err)
|
2018-12-19 05:18:42 +00:00
|
|
|
}
|
2018-12-20 22:00:38 +00:00
|
|
|
block := &pb.BeaconBlock{
|
2018-12-19 05:18:42 +00:00
|
|
|
ParentRootHash32: []byte{'a'},
|
2018-12-20 22:00:38 +00:00
|
|
|
}
|
2018-12-19 05:18:42 +00:00
|
|
|
|
2019-01-06 05:05:16 +00:00
|
|
|
if err := chainService.isBlockReadyForProcessing(block, beaconState); err == nil {
|
2018-12-19 05:18:42 +00:00
|
|
|
t.Fatal("block processing succeeded despite block having no parent saved")
|
|
|
|
}
|
|
|
|
|
2018-12-23 22:51:04 +00:00
|
|
|
beaconState.Slot = 10
|
2018-12-19 05:18:42 +00:00
|
|
|
|
2018-12-23 22:51:04 +00:00
|
|
|
enc, _ := proto.Marshal(beaconState)
|
|
|
|
stateRoot := hashutil.Hash(enc)
|
2018-12-20 22:00:38 +00:00
|
|
|
genesis := b.NewGenesisBlock([]byte{})
|
2018-12-23 22:51:04 +00:00
|
|
|
if err := chainService.beaconDB.SaveBlock(genesis); err != nil {
|
|
|
|
t.Fatalf("cannot save block: %v", err)
|
|
|
|
}
|
2019-01-14 16:41:20 +00:00
|
|
|
parentHash, err := hashutil.HashBeaconBlock(genesis)
|
2018-12-19 05:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get hash of canonical head: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
block2 := &pb.BeaconBlock{
|
2018-12-19 05:18:42 +00:00
|
|
|
ParentRootHash32: parentHash[:],
|
|
|
|
Slot: 10,
|
2018-12-20 22:00:38 +00:00
|
|
|
}
|
2018-12-19 05:18:42 +00:00
|
|
|
|
2019-01-06 05:05:16 +00:00
|
|
|
if err := chainService.isBlockReadyForProcessing(block2, beaconState); err == nil {
|
2018-12-19 05:18:42 +00:00
|
|
|
t.Fatal("block processing succeeded despite block slot being invalid")
|
|
|
|
}
|
|
|
|
|
2019-01-13 14:04:14 +00:00
|
|
|
h := bytesutil.ToBytes32([]byte("a"))
|
2019-01-09 04:42:42 +00:00
|
|
|
beaconState.LatestDepositRootHash32 = h[:]
|
2018-12-23 22:51:04 +00:00
|
|
|
beaconState.Slot = 0
|
2018-12-19 05:18:42 +00:00
|
|
|
|
2018-12-23 22:51:04 +00:00
|
|
|
currentSlot := uint64(1)
|
2018-12-19 05:18:42 +00:00
|
|
|
attestationSlot := uint64(0)
|
2019-01-09 09:49:17 +00:00
|
|
|
shard := beaconState.ShardCommitteesAtSlots[attestationSlot].ArrayShardCommittee[0].Shard
|
2018-12-19 05:18:42 +00:00
|
|
|
|
2018-12-20 22:00:38 +00:00
|
|
|
block3 := &pb.BeaconBlock{
|
2019-01-09 04:42:42 +00:00
|
|
|
Slot: currentSlot,
|
|
|
|
StateRootHash32: stateRoot[:],
|
|
|
|
ParentRootHash32: parentHash[:],
|
|
|
|
DepositRootHash32: []byte("a"),
|
2018-12-22 20:30:59 +00:00
|
|
|
Body: &pb.BeaconBlockBody{
|
|
|
|
Attestations: []*pb.Attestation{{
|
|
|
|
ParticipationBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
Data: &pb.AttestationData{
|
|
|
|
Slot: attestationSlot,
|
|
|
|
Shard: shard,
|
|
|
|
JustifiedBlockRootHash32: parentHash[:],
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
2018-12-20 22:00:38 +00:00
|
|
|
}
|
2018-12-19 05:18:42 +00:00
|
|
|
|
|
|
|
chainService.enablePOWChain = true
|
|
|
|
|
2019-01-06 05:05:16 +00:00
|
|
|
if err := chainService.isBlockReadyForProcessing(block3, beaconState); err != nil {
|
2018-12-23 22:51:04 +00:00
|
|
|
t.Fatalf("block processing failed despite being a valid block: %v", err)
|
2018-12-19 05:18:42 +00:00
|
|
|
}
|
|
|
|
}
|