2018-07-19 16:31:50 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-20 05:07:28 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
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-05-01 15:14:46 +00:00
|
|
|
"reflect"
|
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
|
|
|
|
2019-04-18 17:40:46 +00:00
|
|
|
"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"
|
2019-03-17 02:56:05 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-03-06 21:03:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/attestation"
|
2019-04-10 06:52:06 +00:00
|
|
|
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2019-02-20 05:07:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
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-05-01 15:14:46 +00:00
|
|
|
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
|
2019-02-20 05:07:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2019-02-18 23:34:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2019-03-26 15:40:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2019-03-12 23:39:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/forkutil"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2019-03-17 02:56:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/p2p"
|
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"
|
|
|
|
)
|
|
|
|
|
2019-03-19 23:07:49 +00:00
|
|
|
// Ensure ChainService implements interfaces.
|
|
|
|
var _ = ChainFeeds(&ChainService{})
|
|
|
|
|
2018-08-18 03:34:56 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
2019-03-26 15:40:55 +00:00
|
|
|
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
|
2019-04-10 06:30:17 +00:00
|
|
|
EnableCrosslinks: true,
|
|
|
|
EnableCheckBlockStateRoot: true,
|
2019-03-26 15:40:55 +00:00
|
|
|
})
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 23:34:49 +00:00
|
|
|
type mockOperationService struct{}
|
|
|
|
|
|
|
|
func (ms *mockOperationService) IncomingProcessedBlockFeed() *event.Feed {
|
|
|
|
return new(event.Feed)
|
|
|
|
}
|
|
|
|
|
2019-03-19 23:07:49 +00:00
|
|
|
func (ms *mockOperationService) IncomingAttFeed() *event.Feed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *mockOperationService) IncomingExitFeed() *event.Feed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-02-24 02:58:13 +00:00
|
|
|
func (m *mockClient) BlockByNumber(ctx context.Context, number *big.Int) (*gethTypes.Block, error) {
|
|
|
|
head := &gethTypes.Header{Number: big.NewInt(0), Difficulty: big.NewInt(100)}
|
|
|
|
return gethTypes.NewBlockWithHeader(head), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethTypes.Header, error) {
|
|
|
|
return &gethTypes.Header{Number: big.NewInt(0), Difficulty: big.NewInt(100)}, nil
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-02-24 02:58:13 +00:00
|
|
|
func (f *faultyClient) BlockByNumber(ctx context.Context, number *big.Int) (*gethTypes.Block, error) {
|
|
|
|
return nil, errors.New("failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *faultyClient) HeaderByNumber(ctx context.Context, number *big.Int) (*gethTypes.Header, error) {
|
|
|
|
return nil, errors.New("failed")
|
|
|
|
}
|
|
|
|
|
2018-09-18 13:06:28 +00:00
|
|
|
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'})
|
|
|
|
}
|
2019-03-17 02:56:05 +00:00
|
|
|
|
|
|
|
type mockBroadcaster struct {
|
|
|
|
broadcastCalled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mb *mockBroadcaster) Broadcast(_ context.Context, _ proto.Message) {
|
|
|
|
mb.broadcastCalled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = p2p.Broadcaster(&mockBroadcaster{})
|
|
|
|
|
2019-02-20 05:07:28 +00:00
|
|
|
func setupInitialDeposits(t *testing.T, numDeposits int) ([]*pb.Deposit, []*bls.SecretKey) {
|
|
|
|
privKeys := make([]*bls.SecretKey, numDeposits)
|
|
|
|
deposits := make([]*pb.Deposit, numDeposits)
|
2019-02-03 22:44:48 +00:00
|
|
|
for i := 0; i < len(deposits); i++ {
|
2019-02-20 05:07:28 +00:00
|
|
|
priv, err := bls.RandKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
depositInput := &pb.DepositInput{
|
|
|
|
Pubkey: priv.PublicKey().Marshal(),
|
|
|
|
}
|
|
|
|
balance := params.BeaconConfig().MaxDepositAmount
|
|
|
|
depositData, err := helpers.EncodeDepositData(depositInput, balance, time.Now().Unix())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot encode data: %v", err)
|
|
|
|
}
|
2019-04-14 08:17:39 +00:00
|
|
|
deposits[i] = &pb.Deposit{
|
|
|
|
DepositData: depositData,
|
|
|
|
MerkleTreeIndex: uint64(i),
|
|
|
|
}
|
2019-02-20 05:07:28 +00:00
|
|
|
privKeys[i] = priv
|
2019-02-03 22:44:48 +00:00
|
|
|
}
|
2019-02-20 05:07:28 +00:00
|
|
|
return deposits, privKeys
|
2019-02-03 22:44:48 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 08:17:39 +00:00
|
|
|
func createPreChainStartDeposit(t *testing.T, pk []byte, index uint64) *pb.Deposit {
|
2019-02-09 14:07:30 +00:00
|
|
|
depositInput := &pb.DepositInput{Pubkey: pk}
|
|
|
|
balance := params.BeaconConfig().MaxDepositAmount
|
2019-02-19 18:05:34 +00:00
|
|
|
depositData, err := helpers.EncodeDepositData(depositInput, balance, time.Now().Unix())
|
2019-02-09 14:07:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot encode data: %v", err)
|
|
|
|
}
|
2019-04-14 08:17:39 +00:00
|
|
|
return &pb.Deposit{DepositData: depositData, MerkleTreeIndex: index}
|
2019-02-09 14:07:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 05:07:28 +00:00
|
|
|
func createRandaoReveal(t *testing.T, beaconState *pb.BeaconState, privKeys []*bls.SecretKey) []byte {
|
|
|
|
// We fetch the proposer's index as that is whom the RANDAO will be verified against.
|
|
|
|
proposerIdx, err := helpers.BeaconProposerIndex(beaconState, beaconState.Slot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
epoch := helpers.SlotToEpoch(beaconState.Slot)
|
|
|
|
buf := make([]byte, 32)
|
|
|
|
binary.LittleEndian.PutUint64(buf, epoch)
|
2019-03-12 23:39:13 +00:00
|
|
|
domain := forkutil.DomainVersion(beaconState.Fork, epoch, params.BeaconConfig().DomainRandao)
|
2019-02-20 05:07:28 +00:00
|
|
|
// We make the previous validator's index sign the message instead of the proposer.
|
|
|
|
epochSignature := privKeys[proposerIdx].Sign(buf, domain)
|
|
|
|
return epochSignature.Marshal()
|
|
|
|
}
|
|
|
|
|
2019-04-26 06:24:01 +00:00
|
|
|
func setupGenesisBlock(t *testing.T, cs *ChainService) ([32]byte, *pb.BeaconBlock) {
|
2019-04-10 06:52:06 +00:00
|
|
|
genesis := b.NewGenesisBlock([]byte{})
|
2019-02-20 05:07:28 +00:00
|
|
|
if err := cs.beaconDB.SaveBlock(genesis); err != nil {
|
|
|
|
t.Fatalf("could not save block to db: %v", err)
|
|
|
|
}
|
2019-02-26 03:42:31 +00:00
|
|
|
parentHash, err := hashutil.HashBeaconBlock(genesis)
|
2019-02-20 05:07:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get tree hash root of canonical head: %v", err)
|
|
|
|
}
|
|
|
|
return parentHash, genesis
|
|
|
|
}
|
|
|
|
|
2019-04-01 01:44:16 +00:00
|
|
|
func setupBeaconChain(t *testing.T, beaconDB *db.BeaconDB, attsService *attestation.Service) *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
|
2019-04-01 01:44:16 +00:00
|
|
|
client := &mockClient{}
|
|
|
|
web3Service, err = powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
DepositContract: common.Address{},
|
|
|
|
Reader: client,
|
|
|
|
Client: client,
|
|
|
|
Logger: client,
|
|
|
|
})
|
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
|
|
|
|
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,
|
2019-02-18 23:34:49 +00:00
|
|
|
OpsPoolService: &mockOperationService{},
|
2019-03-06 21:03:52 +00:00
|
|
|
AttsService: attsService,
|
2019-03-17 02:56:05 +00:00
|
|
|
P2p: &mockBroadcaster{},
|
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-04-05 14:41:49 +00:00
|
|
|
bState, err := service.beaconDB.HeadState(context.Background())
|
2018-12-19 05:18:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-23 22:51:04 +00:00
|
|
|
bState.Slot = slot
|
2019-04-02 08:49:45 +00:00
|
|
|
return service.beaconDB.SaveState(context.Background(), bState)
|
2018-12-19 05:18:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestChainStartStop_Uninitialized(t *testing.T) {
|
2019-01-28 11:59:37 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2018-11-07 19:07:41 +00:00
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2019-04-01 01:44:16 +00:00
|
|
|
chainService := setupBeaconChain(t, db, nil)
|
2018-09-27 02:34:35 +00:00
|
|
|
|
2019-01-28 11:59:37 +00:00
|
|
|
// Test the start function.
|
2019-02-13 17:51:57 +00:00
|
|
|
genesisChan := make(chan time.Time, 0)
|
|
|
|
sub := chainService.stateInitializedFeed.Subscribe(genesisChan)
|
|
|
|
defer sub.Unsubscribe()
|
2019-01-28 11:59:37 +00:00
|
|
|
chainService.Start()
|
2019-02-13 17:51:57 +00:00
|
|
|
chainService.chainStartChan <- time.Unix(0, 0)
|
|
|
|
genesisTime := <-genesisChan
|
|
|
|
if genesisTime != time.Unix(0, 0) {
|
|
|
|
t.Errorf(
|
|
|
|
"Expected genesis time to equal chainstart time (%v), received %v",
|
|
|
|
time.Unix(0, 0),
|
|
|
|
genesisTime,
|
|
|
|
)
|
2019-01-28 11:59:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 14:41:49 +00:00
|
|
|
beaconState, err := db.HeadState(context.Background())
|
2019-03-13 21:17:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-06 20:13:09 +00:00
|
|
|
if beaconState == nil || beaconState.Slot != params.BeaconConfig().GenesisSlot {
|
|
|
|
t.Error("Expected canonical state feed to send a state with genesis block")
|
|
|
|
}
|
2019-01-28 11:59:37 +00:00
|
|
|
if err := chainService.Stop(); err != nil {
|
|
|
|
t.Fatalf("Unable to stop chain service: %v", err)
|
|
|
|
}
|
|
|
|
// The context should have been canceled.
|
|
|
|
if chainService.ctx.Err() != context.Canceled {
|
|
|
|
t.Error("Context was not canceled")
|
|
|
|
}
|
|
|
|
testutil.AssertLogsContain(t, hook, "Waiting for ChainStart log from the Validator Deposit Contract to start the beacon chain...")
|
|
|
|
testutil.AssertLogsContain(t, hook, "ChainStart time reached, starting the beacon chain!")
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestChainStartStop_Initialized(t *testing.T) {
|
2019-01-28 11:59:37 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
2019-02-28 03:55:47 +00:00
|
|
|
|
2019-04-01 01:44:16 +00:00
|
|
|
chainService := setupBeaconChain(t, db, nil)
|
2019-01-28 11:59:37 +00:00
|
|
|
|
|
|
|
unixTime := uint64(time.Now().Unix())
|
2019-02-20 05:07:28 +00:00
|
|
|
deposits, _ := setupInitialDeposits(t, 100)
|
2019-04-09 19:06:23 +00:00
|
|
|
if err := db.InitializeState(context.Background(), unixTime, deposits, &pb.Eth1Data{}); err != nil {
|
2019-01-28 11:59:37 +00:00
|
|
|
t.Fatalf("Could not initialize beacon state to disk: %v", err)
|
|
|
|
}
|
2019-04-26 06:24:01 +00:00
|
|
|
setupGenesisBlock(t, chainService)
|
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-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.
|
2019-01-28 11:59:37 +00:00
|
|
|
if chainService.ctx.Err() != context.Canceled {
|
2018-07-19 16:31:50 +00:00
|
|
|
t.Error("context was not canceled")
|
|
|
|
}
|
2019-01-28 11:59:37 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Beacon chain data already exists, starting service")
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
2019-05-01 15:14:46 +00:00
|
|
|
|
|
|
|
func TestRecentCanonicalRoots_CanFilter(t *testing.T) {
|
|
|
|
service := setupBeaconChain(t, nil, nil)
|
|
|
|
blks := map[uint64][]byte{
|
|
|
|
1: {'A'},
|
|
|
|
50: {'E'},
|
|
|
|
2: {'B'},
|
|
|
|
99: {'F'},
|
|
|
|
30: {'D'},
|
|
|
|
3: {'C'},
|
|
|
|
}
|
|
|
|
service.canonicalBlocks = blks
|
|
|
|
|
|
|
|
want := []*pbrpc.BlockRoot{{Slot: 99, Root: []byte{'F'}}}
|
|
|
|
roots := service.RecentCanonicalRoots(1)
|
|
|
|
if !reflect.DeepEqual(want, roots) {
|
|
|
|
t.Log("Incorrect block roots received")
|
|
|
|
}
|
|
|
|
|
|
|
|
want = []*pbrpc.BlockRoot{
|
|
|
|
{Slot: 99, Root: []byte{'F'}},
|
|
|
|
{Slot: 50, Root: []byte{'E'}},
|
|
|
|
{Slot: 30, Root: []byte{'D'}},
|
|
|
|
}
|
|
|
|
roots = service.RecentCanonicalRoots(3)
|
|
|
|
if !reflect.DeepEqual(want, roots) {
|
|
|
|
t.Log("Incorrect block roots received")
|
|
|
|
}
|
|
|
|
|
|
|
|
want = []*pbrpc.BlockRoot{
|
|
|
|
{Slot: 99, Root: []byte{'F'}},
|
|
|
|
{Slot: 50, Root: []byte{'E'}},
|
|
|
|
{Slot: 30, Root: []byte{'D'}},
|
|
|
|
{Slot: 3, Root: []byte{'C'}},
|
|
|
|
{Slot: 2, Root: []byte{'B'}},
|
|
|
|
{Slot: 1, Root: []byte{'A'}},
|
|
|
|
}
|
|
|
|
roots = service.RecentCanonicalRoots(100)
|
|
|
|
if !reflect.DeepEqual(want, roots) {
|
|
|
|
t.Log("Incorrect block roots received")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-06 18:02:04 +00:00
|
|
|
|
|
|
|
func TestCanonicalBlock_CanGet(t *testing.T) {
|
|
|
|
db := internal.SetupDB(t)
|
|
|
|
defer internal.TeardownDB(t, db)
|
|
|
|
service := setupBeaconChain(t, db, nil)
|
|
|
|
|
|
|
|
blk1 := &pb.BeaconBlock{Slot: 500}
|
|
|
|
blk1Root, err := hashutil.HashBeaconBlock(blk1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := db.SaveBlock(blk1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
blk2 := &pb.BeaconBlock{Slot: 600}
|
|
|
|
blk2Root, _ := hashutil.HashBeaconBlock(blk2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := db.SaveBlock(blk2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cMap := map[uint64][]byte{
|
|
|
|
blk1.Slot: blk1Root[:],
|
|
|
|
blk2.Slot: blk2Root[:],
|
|
|
|
700: {'A'},
|
|
|
|
}
|
|
|
|
service.canonicalBlocks = cMap
|
|
|
|
blk1Db, err := service.CanonicalBlock(blk1.Slot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(blk1, blk1Db) {
|
|
|
|
t.Error("block 1 don't match")
|
|
|
|
}
|
|
|
|
blk2Db, err := service.CanonicalBlock(blk2.Slot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(blk2, blk2Db) {
|
|
|
|
t.Error("block 2 don't match")
|
|
|
|
}
|
|
|
|
blk3Db, err := service.CanonicalBlock(999)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if blk3Db != nil {
|
|
|
|
t.Error("block 3 is suppose to be nil")
|
|
|
|
}
|
|
|
|
}
|