2019-08-26 20:06:16 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
2019-11-05 02:37:18 +00:00
|
|
|
"bytes"
|
2019-08-26 20:06:16 +00:00
|
|
|
"context"
|
2019-09-09 21:13:50 +00:00
|
|
|
"time"
|
2019-08-26 20:06:16 +00:00
|
|
|
|
2019-11-05 02:37:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-11-05 02:37:18 +00:00
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
2019-12-07 17:57:26 +00:00
|
|
|
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
|
2019-11-05 02:37:18 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-08-26 20:06:16 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-09-05 16:04:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2019-11-05 02:37:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-08-26 20:06:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ChainService defines the mock interface for testing
|
|
|
|
type ChainService struct {
|
2019-11-23 03:35:47 +00:00
|
|
|
State *pb.BeaconState
|
|
|
|
Root []byte
|
|
|
|
Block *ethpb.BeaconBlock
|
|
|
|
FinalizedCheckPoint *ethpb.Checkpoint
|
|
|
|
BlocksReceived []*ethpb.BeaconBlock
|
|
|
|
Genesis time.Time
|
|
|
|
Fork *pb.Fork
|
|
|
|
DB db.Database
|
|
|
|
stateNotifier statefeed.Notifier
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateNotifier mocks the same method in the chain service.
|
|
|
|
func (ms *ChainService) StateNotifier() statefeed.Notifier {
|
|
|
|
if ms.stateNotifier == nil {
|
|
|
|
ms.stateNotifier = &MockStateNotifier{}
|
|
|
|
}
|
|
|
|
return ms.stateNotifier
|
|
|
|
}
|
|
|
|
|
|
|
|
// MockStateNotifier mocks the state notifier.
|
|
|
|
type MockStateNotifier struct {
|
|
|
|
feed *event.Feed
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateFeed returns a state feed.
|
|
|
|
func (msn *MockStateNotifier) StateFeed() *event.Feed {
|
|
|
|
if msn.feed == nil {
|
|
|
|
msn.feed = new(event.Feed)
|
|
|
|
}
|
|
|
|
return msn.feed
|
2019-08-26 20:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiveBlock mocks ReceiveBlock method in chain service.
|
|
|
|
func (ms *ChainService) ReceiveBlock(ctx context.Context, block *ethpb.BeaconBlock) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 03:32:00 +00:00
|
|
|
// ReceiveBlockNoVerify mocks ReceiveBlockNoVerify method in chain service.
|
|
|
|
func (ms *ChainService) ReceiveBlockNoVerify(ctx context.Context, block *ethpb.BeaconBlock) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:06:16 +00:00
|
|
|
// ReceiveBlockNoPubsub mocks ReceiveBlockNoPubsub method in chain service.
|
|
|
|
func (ms *ChainService) ReceiveBlockNoPubsub(ctx context.Context, block *ethpb.BeaconBlock) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiveBlockNoPubsubForkchoice mocks ReceiveBlockNoPubsubForkchoice method in chain service.
|
|
|
|
func (ms *ChainService) ReceiveBlockNoPubsubForkchoice(ctx context.Context, block *ethpb.BeaconBlock) error {
|
2019-09-25 17:00:04 +00:00
|
|
|
if ms.State == nil {
|
|
|
|
ms.State = &pb.BeaconState{}
|
|
|
|
}
|
2019-11-05 02:37:18 +00:00
|
|
|
if !bytes.Equal(ms.Root, block.ParentRoot) {
|
|
|
|
return errors.Errorf("wanted %#x but got %#x", ms.Root, block.ParentRoot)
|
|
|
|
}
|
2019-09-25 17:00:04 +00:00
|
|
|
ms.State.Slot = block.Slot
|
|
|
|
ms.BlocksReceived = append(ms.BlocksReceived, block)
|
2019-11-05 02:37:18 +00:00
|
|
|
signingRoot, err := ssz.SigningRoot(block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ms.DB != nil {
|
|
|
|
if err := ms.DB.SaveBlock(ctx, block); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Infof("Saved block with root: %#x at slot %d", signingRoot, block.Slot)
|
|
|
|
}
|
|
|
|
ms.Root = signingRoot[:]
|
|
|
|
ms.Block = block
|
2019-08-26 20:06:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadSlot mocks HeadSlot method in chain service.
|
|
|
|
func (ms *ChainService) HeadSlot() uint64 {
|
|
|
|
return ms.State.Slot
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadRoot mocks HeadRoot method in chain service.
|
|
|
|
func (ms *ChainService) HeadRoot() []byte {
|
|
|
|
return ms.Root
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadBlock mocks HeadBlock method in chain service.
|
|
|
|
func (ms *ChainService) HeadBlock() *ethpb.BeaconBlock {
|
2019-09-19 01:14:26 +00:00
|
|
|
return ms.Block
|
2019-08-26 20:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadState mocks HeadState method in chain service.
|
2019-11-19 16:12:50 +00:00
|
|
|
func (ms *ChainService) HeadState(context.Context) (*pb.BeaconState, error) {
|
|
|
|
return ms.State, nil
|
2019-08-26 20:06:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 05:23:55 +00:00
|
|
|
// CurrentFork mocks HeadState method in chain service.
|
|
|
|
func (ms *ChainService) CurrentFork() *pb.Fork {
|
|
|
|
return ms.Fork
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:06:16 +00:00
|
|
|
// FinalizedCheckpt mocks FinalizedCheckpt method in chain service.
|
|
|
|
func (ms *ChainService) FinalizedCheckpt() *ethpb.Checkpoint {
|
|
|
|
return ms.FinalizedCheckPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiveAttestation mocks ReceiveAttestation method in chain service.
|
|
|
|
func (ms *ChainService) ReceiveAttestation(context.Context, *ethpb.Attestation) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceiveAttestationNoPubsub mocks ReceiveAttestationNoPubsub method in chain service.
|
|
|
|
func (ms *ChainService) ReceiveAttestationNoPubsub(context.Context, *ethpb.Attestation) error {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-05 16:04:06 +00:00
|
|
|
|
2019-10-18 03:30:14 +00:00
|
|
|
// GenesisTime mocks the same method in the chain service.
|
|
|
|
func (ms *ChainService) GenesisTime() time.Time {
|
|
|
|
return ms.Genesis
|
|
|
|
}
|