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"
|
2018-07-19 16:31:50 +00:00
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/types"
|
2018-11-21 06:44:04 +00:00
|
|
|
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
|
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"
|
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-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{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Pubkey: "",
|
|
|
|
VrcAddr: common.Address{},
|
|
|
|
Reader: client,
|
|
|
|
Client: client,
|
|
|
|
Logger: client,
|
|
|
|
})
|
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{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Pubkey: "",
|
|
|
|
VrcAddr: common.Address{},
|
|
|
|
Reader: client,
|
|
|
|
Client: client,
|
|
|
|
Logger: client,
|
|
|
|
})
|
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)
|
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
if err := beaconDB.InitializeState(nil); 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
2018-12-03 16:10:47 +00:00
|
|
|
Slot: 1,
|
|
|
|
CandidatePowReceiptRootHash32: []byte("a"),
|
2018-09-18 13:06:28 +00:00
|
|
|
})
|
|
|
|
|
2018-10-14 15:29:57 +00:00
|
|
|
blockChan := make(chan *types.Block)
|
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-11-18 16:33:05 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "proof-of-Work chain reference in block does not exist")
|
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)
|
2018-12-01 22:09:12 +00:00
|
|
|
beaconState, err := types.NewGenesisBeaconState(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-01 22:09:12 +00:00
|
|
|
stateRoot, _ := beaconState.Hash()
|
2018-08-15 04:49:59 +00:00
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
genesis := types.NewGenesisBlock([32]byte{})
|
2018-10-05 17:14:50 +00:00
|
|
|
chainService.beaconDB.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-10-16 00:02:00 +00:00
|
|
|
currentSlot := uint64(1)
|
2018-10-22 21:04:17 +00:00
|
|
|
attestationSlot := uint64(0)
|
2018-12-01 22:09:12 +00:00
|
|
|
shard := beaconState.ShardAndCommitteesForSlots()[attestationSlot].ArrayShardAndCommittee[0].Shard
|
2018-09-21 19:33:53 +00:00
|
|
|
|
2018-09-11 05:09:41 +00:00
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
2018-12-03 16:10:47 +00:00
|
|
|
Slot: currentSlot,
|
|
|
|
StateRootHash32: stateRoot[:],
|
2018-12-13 19:35:11 +00:00
|
|
|
ParentRootHash32: parentHash[:],
|
2018-12-03 16:10:47 +00:00
|
|
|
CandidatePowReceiptRootHash32: []byte("a"),
|
2018-09-15 14:51:17 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{{
|
2018-11-21 01:41:20 +00:00
|
|
|
Slot: attestationSlot,
|
|
|
|
AttesterBitfield: []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},
|
2018-10-22 21:04:17 +00:00
|
|
|
Shard: shard,
|
2018-09-21 19:33:53 +00:00
|
|
|
JustifiedBlockHash: parentHash[:],
|
2018-09-04 23:18:55 +00:00
|
|
|
}},
|
2018-08-15 04:49:59 +00:00
|
|
|
})
|
|
|
|
|
2018-10-14 15:29:57 +00:00
|
|
|
blockChan := make(chan *types.Block)
|
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-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
|
|
|
|
2018-09-11 05:09:41 +00:00
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
2018-10-10 02:34:50 +00:00
|
|
|
Slot: 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-12-01 22:09:12 +00:00
|
|
|
func getShardForSlot(t *testing.T, beaconState *types.BeaconState, slot uint64) uint64 {
|
2018-11-21 06:44:04 +00:00
|
|
|
shardAndCommittee, err := v.GetShardAndCommitteesForSlot(
|
2018-12-01 22:09:12 +00:00
|
|
|
beaconState.ShardAndCommitteesForSlots(),
|
|
|
|
beaconState.LastStateRecalculationSlot(),
|
|
|
|
slot,
|
|
|
|
)
|
2018-10-22 21:04:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to get shard for slot: %d", slot)
|
|
|
|
}
|
|
|
|
return shardAndCommittee.ArrayShardAndCommittee[0].Shard
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
func TestProcessBlocksWithCorrectAttestations(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-12-01 22:09:12 +00:00
|
|
|
beaconState, err := types.NewGenesisBeaconState(nil)
|
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-12-01 22:09:12 +00:00
|
|
|
stateRoot, _ := beaconState.Hash()
|
2018-09-18 13:06:28 +00:00
|
|
|
|
2018-09-14 05:07:30 +00:00
|
|
|
block0 := types.NewBlock(&pb.BeaconBlock{
|
2018-10-10 02:34:50 +00:00
|
|
|
Slot: 0,
|
2018-09-14 05:07:30 +00:00
|
|
|
})
|
2018-10-05 17:14:50 +00:00
|
|
|
if saveErr := chainService.beaconDB.SaveBlock(block0); saveErr != nil {
|
2018-10-31 22:05:55 +00:00
|
|
|
t.Fatalf("Could not save block: %v", saveErr)
|
2018-09-14 05:07:30 +00:00
|
|
|
}
|
|
|
|
block0Hash, err := block0.Hash()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to compute block's hash: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-22 21:04:17 +00:00
|
|
|
currentSlot := uint64(1)
|
|
|
|
attestationSlot := currentSlot - 1
|
2018-09-21 19:33:53 +00:00
|
|
|
|
2018-09-12 03:17:20 +00:00
|
|
|
block1 := types.NewBlock(&pb.BeaconBlock{
|
2018-12-13 19:35:11 +00:00
|
|
|
ParentRootHash32: block0Hash[:],
|
|
|
|
Slot: currentSlot,
|
|
|
|
StateRootHash32: stateRoot[:],
|
2018-09-15 14:51:17 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{{
|
2018-11-21 01:41:20 +00:00
|
|
|
Slot: attestationSlot,
|
|
|
|
AttesterBitfield: []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},
|
2018-12-01 22:09:12 +00:00
|
|
|
Shard: getShardForSlot(t, beaconState, attestationSlot),
|
2018-11-05 17:35:50 +00:00
|
|
|
JustifiedBlockHash: block0Hash[:],
|
2018-09-12 03:17:20 +00:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2018-10-14 15:29:57 +00:00
|
|
|
blockChan := make(chan *types.Block)
|
2018-10-05 17:14:50 +00:00
|
|
|
exitRoutine := make(chan bool)
|
2018-09-12 03:17:20 +00:00
|
|
|
go func() {
|
2018-10-14 15:29:57 +00:00
|
|
|
chainService.blockProcessing(blockChan)
|
2018-09-12 03:17:20 +00:00
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
|
|
|
|
chainService.incomingBlockChan <- block1
|
2018-10-14 15:29:57 +00:00
|
|
|
block1Returned := <-blockChan
|
|
|
|
|
|
|
|
if block1 != block1Returned {
|
|
|
|
t.Fatalf("expected %v and %v to be the same", block1, block1Returned)
|
|
|
|
}
|
2018-09-12 03:17:20 +00:00
|
|
|
|
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{
|
2018-12-13 19:35:11 +00:00
|
|
|
ParentRootHash32: block1Hash[:],
|
|
|
|
Slot: currentSlot,
|
2018-09-18 13:06:28 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{
|
2018-10-22 21:04:17 +00:00
|
|
|
{
|
2018-11-05 17:35:50 +00:00
|
|
|
Slot: currentSlot - 1,
|
|
|
|
AttesterBitfield: []byte{64, 0},
|
2018-12-01 22:09:12 +00:00
|
|
|
Shard: getShardForSlot(t, beaconState, currentSlot-1),
|
2018-11-05 17:35:50 +00:00
|
|
|
JustifiedBlockHash: block0Hash[:],
|
2018-10-22 21:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-05 17:35:50 +00:00
|
|
|
Slot: currentSlot - 2,
|
|
|
|
AttesterBitfield: []byte{128, 0},
|
2018-12-01 22:09:12 +00:00
|
|
|
Shard: getShardForSlot(t, beaconState, currentSlot-2),
|
2018-11-05 17:35:50 +00:00
|
|
|
JustifiedBlockHash: block0Hash[:],
|
2018-10-22 21:04:17 +00:00
|
|
|
},
|
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{
|
2018-12-13 19:35:11 +00:00
|
|
|
ParentRootHash32: block2Hash[:],
|
|
|
|
Slot: currentSlot,
|
2018-09-18 13:06:28 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{
|
2018-10-22 21:04:17 +00:00
|
|
|
{
|
2018-11-05 17:35:50 +00:00
|
|
|
Slot: currentSlot - 1,
|
|
|
|
AttesterBitfield: []byte{32, 0},
|
2018-12-01 22:09:12 +00:00
|
|
|
Shard: getShardForSlot(t, beaconState, currentSlot-1),
|
2018-11-05 17:35:50 +00:00
|
|
|
JustifiedBlockHash: block0Hash[:],
|
2018-10-22 21:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-05 17:35:50 +00:00
|
|
|
Slot: currentSlot - 2,
|
|
|
|
AttesterBitfield: []byte{64, 0},
|
2018-12-01 22:09:12 +00:00
|
|
|
Shard: getShardForSlot(t, beaconState, currentSlot-2),
|
2018-11-05 17:35:50 +00:00
|
|
|
JustifiedBlockHash: block0Hash[:],
|
2018-10-22 21:04:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-05 17:35:50 +00:00
|
|
|
Slot: currentSlot - 3,
|
|
|
|
AttesterBitfield: []byte{128, 0},
|
2018-12-01 22:09:12 +00:00
|
|
|
Shard: getShardForSlot(t, beaconState, currentSlot-3),
|
2018-11-05 17:35:50 +00:00
|
|
|
JustifiedBlockHash: block0Hash[:],
|
2018-10-22 21:04:17 +00:00
|
|
|
},
|
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
|
2018-10-14 15:29:57 +00:00
|
|
|
<-blockChan
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.incomingBlockChan <- block2
|
2018-10-14 15:29:57 +00:00
|
|
|
<-blockChan
|
2018-09-18 13:06:28 +00:00
|
|
|
chainService.incomingBlockChan <- block3
|
2018-10-14 15:29:57 +00:00
|
|
|
<-blockChan
|
2018-09-15 14:51:17 +00:00
|
|
|
|
|
|
|
chainService.cancel()
|
|
|
|
exitRoutine <- true
|
|
|
|
}
|
2018-10-18 04:23:18 +00:00
|
|
|
|
|
|
|
func TestUpdateHead(t *testing.T) {
|
2018-12-01 22:09:12 +00:00
|
|
|
beaconState, err := types.NewGenesisBeaconState(nil)
|
2018-10-18 04:23:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not generate genesis state: %v", err)
|
|
|
|
}
|
2018-12-01 22:09:12 +00:00
|
|
|
stateRoot, _ := beaconState.Hash()
|
2018-10-18 04:23:18 +00:00
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
genesis := types.NewGenesisBlock(stateRoot)
|
2018-10-18 04:23:18 +00:00
|
|
|
genesisHash, err := genesis.Hash()
|
|
|
|
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-01 22:09:12 +00:00
|
|
|
state *types.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-11 15:46:01 +00:00
|
|
|
state: types.NewBeaconState(&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-01 22:09:12 +00:00
|
|
|
state: types.NewBeaconState(&pb.BeaconState{
|
2018-12-11 15:46:01 +00:00
|
|
|
FinalizedSlot: 0,
|
|
|
|
JustifiedSlot: 10,
|
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-01 22:09:12 +00:00
|
|
|
stateRoot, _ := tt.state.Hash()
|
2018-10-18 04:23:18 +00:00
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
2018-12-03 16:10:47 +00:00
|
|
|
Slot: tt.blockSlot,
|
|
|
|
StateRootHash32: stateRoot[:],
|
2018-12-13 19:35:11 +00:00
|
|
|
ParentRootHash32: genesisHash[:],
|
2018-12-03 16:10:47 +00:00
|
|
|
CandidatePowReceiptRootHash32: []byte("a"),
|
2018-10-18 04:23:18 +00:00
|
|
|
})
|
|
|
|
h, err := block.Hash()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
blockChan := make(chan *types.Block)
|
|
|
|
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
|
|
|
|
|
|
|
func TestUpdateBlockVoteCache(t *testing.T) {
|
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
|
|
|
chainService := setupBeaconChain(t, true, db)
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
beaconState, err := types.NewGenesisBeaconState(nil)
|
2018-11-11 16:54:17 +00:00
|
|
|
if err != nil {
|
2018-12-01 22:09:12 +00:00
|
|
|
t.Fatalf("failed to initialize genesis state: %v", err)
|
2018-11-11 16:54:17 +00:00
|
|
|
}
|
|
|
|
block := types.NewBlock(&pb.BeaconBlock{
|
2018-12-13 19:35:11 +00:00
|
|
|
Slot: 1,
|
|
|
|
ParentRootHash32: []byte{},
|
2018-11-11 16:54:17 +00:00
|
|
|
Attestations: []*pb.AggregatedAttestation{
|
|
|
|
{
|
|
|
|
Slot: 0,
|
|
|
|
Shard: 1,
|
|
|
|
AttesterBitfield: []byte{'F', 'F'},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
err = chainService.calculateNewBlockVotes(block, beaconState)
|
2018-11-11 16:54:17 +00:00
|
|
|
if err != nil {
|
2018-12-01 22:09:12 +00:00
|
|
|
t.Errorf("failed to update the block vote cache: %v", err)
|
2018-11-11 16:54:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateBlockVoteCacheNoAttestations(t *testing.T) {
|
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
|
|
|
chainService := setupBeaconChain(t, true, db)
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
beaconState, err := types.NewGenesisBeaconState(nil)
|
2018-11-11 16:54:17 +00:00
|
|
|
if err != nil {
|
2018-12-01 22:09:12 +00:00
|
|
|
t.Fatalf("failed to initialize genesis state: %v", err)
|
2018-11-11 16:54:17 +00:00
|
|
|
}
|
|
|
|
block := types.NewBlock(nil)
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
err = chainService.calculateNewBlockVotes(block, beaconState)
|
2018-11-11 16:54:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to update the block vote cache: %v", err)
|
|
|
|
}
|
|
|
|
}
|