2019-01-19 02:57:51 +00:00
|
|
|
// Package blockchain defines the life-cycle and status of the beacon chain
|
|
|
|
// as well as the Ethereum Serenity beacon chain fork-choice rule based on
|
|
|
|
// Casper Proof of Stake finality.
|
2018-07-19 16:31:50 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
2019-04-16 18:19:31 +00:00
|
|
|
"bytes"
|
2018-07-19 16:31:50 +00:00
|
|
|
"context"
|
2018-07-31 04:41:27 +00:00
|
|
|
"fmt"
|
2019-04-16 18:19:31 +00:00
|
|
|
"sync"
|
2018-09-18 13:06:28 +00:00
|
|
|
"time"
|
2018-07-19 16:31:50 +00:00
|
|
|
|
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"
|
2018-11-16 17:01:41 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-03-19 23:07:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/operations"
|
2018-07-22 16:58:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
2018-12-20 22:00:38 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2018-10-03 01:49:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2019-02-26 15:38:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2019-03-17 02:56:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/p2p"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-04-02 08:49:45 +00:00
|
|
|
"go.opencensus.io/trace"
|
2018-07-19 16:31:50 +00:00
|
|
|
)
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
var log = logrus.WithField("prefix", "blockchain")
|
|
|
|
|
2019-03-19 23:07:49 +00:00
|
|
|
// ChainFeeds interface defines the methods of the ChainService which provide
|
|
|
|
// information feeds.
|
|
|
|
type ChainFeeds interface {
|
|
|
|
StateInitializedFeed() *event.Feed
|
2019-02-18 23:34:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
// ChainService represents a service that handles the internal
|
|
|
|
// logic of managing the full PoS beacon chain.
|
|
|
|
type ChainService struct {
|
2019-02-13 17:51:57 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
beaconDB *db.BeaconDB
|
|
|
|
web3Service *powchain.Web3Service
|
2019-03-06 21:03:52 +00:00
|
|
|
attsService *attestation.Service
|
2019-03-19 23:07:49 +00:00
|
|
|
opsPoolService operations.OperationFeeds
|
2019-02-13 17:51:57 +00:00
|
|
|
chainStartChan chan time.Time
|
|
|
|
canonicalBlockFeed *event.Feed
|
|
|
|
genesisTime time.Time
|
2019-03-12 20:44:21 +00:00
|
|
|
finalizedEpoch uint64
|
2019-02-13 17:51:57 +00:00
|
|
|
stateInitializedFeed *event.Feed
|
2019-03-17 02:56:05 +00:00
|
|
|
p2p p2p.Broadcaster
|
2019-04-16 18:19:31 +00:00
|
|
|
canonicalBlocks map[uint64][]byte
|
|
|
|
canonicalBlocksLock sync.RWMutex
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 22:54:59 +00:00
|
|
|
// Config options for the service.
|
|
|
|
type Config struct {
|
2019-03-13 21:17:32 +00:00
|
|
|
BeaconBlockBuf int
|
|
|
|
Web3Service *powchain.Web3Service
|
|
|
|
AttsService *attestation.Service
|
|
|
|
BeaconDB *db.BeaconDB
|
2019-03-19 23:07:49 +00:00
|
|
|
OpsPoolService operations.OperationFeeds
|
2019-03-13 21:17:32 +00:00
|
|
|
DevMode bool
|
2019-03-17 02:56:05 +00:00
|
|
|
P2p p2p.Broadcaster
|
2018-08-09 22:54:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
// NewChainService instantiates a new service instance that will
|
|
|
|
// be registered into a running beacon node.
|
2018-08-24 04:09:59 +00:00
|
|
|
func NewChainService(ctx context.Context, cfg *Config) (*ChainService, error) {
|
2018-07-19 16:31:50 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-07-31 04:41:27 +00:00
|
|
|
return &ChainService{
|
2019-02-13 17:51:57 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
beaconDB: cfg.BeaconDB,
|
|
|
|
web3Service: cfg.Web3Service,
|
2019-02-18 23:34:49 +00:00
|
|
|
opsPoolService: cfg.OpsPoolService,
|
2019-03-06 21:03:52 +00:00
|
|
|
attsService: cfg.AttsService,
|
2019-02-13 17:51:57 +00:00
|
|
|
canonicalBlockFeed: new(event.Feed),
|
2019-03-13 21:17:32 +00:00
|
|
|
chainStartChan: make(chan time.Time),
|
2019-02-13 17:51:57 +00:00
|
|
|
stateInitializedFeed: new(event.Feed),
|
2019-03-17 02:56:05 +00:00
|
|
|
p2p: cfg.P2p,
|
2019-04-16 18:19:31 +00:00
|
|
|
canonicalBlocks: make(map[uint64][]byte),
|
2018-07-31 04:41:27 +00:00
|
|
|
}, nil
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start a blockchain service's main event loop.
|
|
|
|
func (c *ChainService) Start() {
|
2019-04-05 14:41:49 +00:00
|
|
|
beaconState, err := c.beaconDB.HeadState(c.ctx)
|
2018-10-02 20:07:33 +00:00
|
|
|
if err != nil {
|
2019-01-28 11:59:37 +00:00
|
|
|
log.Fatalf("Could not fetch beacon state: %v", err)
|
2018-10-02 20:07:33 +00:00
|
|
|
}
|
2019-01-28 11:59:37 +00:00
|
|
|
// If the chain has already been initialized, simply start the block processing routine.
|
|
|
|
if beaconState != nil {
|
|
|
|
log.Info("Beacon chain data already exists, starting service")
|
|
|
|
c.genesisTime = time.Unix(int64(beaconState.GenesisTime), 0)
|
2019-03-12 20:44:21 +00:00
|
|
|
c.finalizedEpoch = beaconState.FinalizedEpoch
|
2019-01-28 11:59:37 +00:00
|
|
|
} else {
|
|
|
|
log.Info("Waiting for ChainStart log from the Validator Deposit Contract to start the beacon chain...")
|
2019-02-07 16:11:18 +00:00
|
|
|
if c.web3Service == nil {
|
|
|
|
log.Fatal("Not configured web3Service for POW chain")
|
2019-02-14 20:04:47 +00:00
|
|
|
return // return need for TestStartUninitializedChainWithoutConfigPOWChain.
|
2019-02-07 16:11:18 +00:00
|
|
|
}
|
2019-02-13 17:51:57 +00:00
|
|
|
subChainStart := c.web3Service.ChainStartFeed().Subscribe(c.chainStartChan)
|
2019-01-28 11:59:37 +00:00
|
|
|
go func() {
|
2019-02-13 17:51:57 +00:00
|
|
|
genesisTime := <-c.chainStartChan
|
2019-03-13 21:17:32 +00:00
|
|
|
c.processChainStartTime(genesisTime, subChainStart)
|
|
|
|
return
|
2019-01-28 11:59:37 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 21:17:32 +00:00
|
|
|
// processChainStartTime initializes a series of deposits from the ChainStart deposits in the eth1
|
|
|
|
// deposit contract, initializes the beacon chain's state, and kicks off the beacon chain.
|
|
|
|
func (c *ChainService) processChainStartTime(genesisTime time.Time, chainStartSub event.Subscription) {
|
|
|
|
initialDepositsData := c.web3Service.ChainStartDeposits()
|
|
|
|
initialDeposits := make([]*pb.Deposit, len(initialDepositsData))
|
|
|
|
for i := range initialDepositsData {
|
|
|
|
initialDeposits[i] = &pb.Deposit{DepositData: initialDepositsData[i]}
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:43:29 +00:00
|
|
|
beaconState, err := c.initializeBeaconChain(genesisTime, initialDeposits, c.web3Service.ChainStartETH1Data())
|
2019-03-13 21:17:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not initialize beacon chain: %v", err)
|
|
|
|
}
|
|
|
|
c.finalizedEpoch = beaconState.FinalizedEpoch
|
|
|
|
c.stateInitializedFeed.Send(genesisTime)
|
|
|
|
chainStartSub.Unsubscribe()
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:59:37 +00:00
|
|
|
// initializes the state and genesis block of the beacon chain to persistent storage
|
|
|
|
// based on a genesis timestamp value obtained from the ChainStart event emitted
|
|
|
|
// by the ETH1.0 Deposit Contract and the POWChain service of the node.
|
2019-03-02 23:38:22 +00:00
|
|
|
func (c *ChainService) initializeBeaconChain(genesisTime time.Time, deposits []*pb.Deposit,
|
2019-03-06 20:13:09 +00:00
|
|
|
eth1data *pb.Eth1Data) (*pb.BeaconState, error) {
|
2019-04-02 08:49:45 +00:00
|
|
|
ctx, span := trace.StartSpan(context.Background(), "beacon-chain.ChainService.initializeBeaconChain")
|
|
|
|
defer span.End()
|
2019-01-28 11:59:37 +00:00
|
|
|
log.Info("ChainStart time reached, starting the beacon chain!")
|
|
|
|
c.genesisTime = genesisTime
|
|
|
|
unixTime := uint64(genesisTime.Unix())
|
2019-04-09 19:06:23 +00:00
|
|
|
if err := c.beaconDB.InitializeState(c.ctx, unixTime, deposits, eth1data); err != nil {
|
2019-03-06 20:13:09 +00:00
|
|
|
return nil, fmt.Errorf("could not initialize beacon state to disk: %v", err)
|
2019-01-28 11:59:37 +00:00
|
|
|
}
|
2019-04-05 14:41:49 +00:00
|
|
|
beaconState, err := c.beaconDB.HeadState(c.ctx)
|
2019-01-28 11:59:37 +00:00
|
|
|
if err != nil {
|
2019-03-06 20:13:09 +00:00
|
|
|
return nil, fmt.Errorf("could not attempt fetch beacon state: %v", err)
|
2019-01-28 11:59:37 +00:00
|
|
|
}
|
2019-03-17 02:56:05 +00:00
|
|
|
|
2019-02-26 03:42:31 +00:00
|
|
|
stateRoot, err := hashutil.HashProto(beaconState)
|
2019-01-28 11:59:37 +00:00
|
|
|
if err != nil {
|
2019-03-06 20:13:09 +00:00
|
|
|
return nil, fmt.Errorf("could not hash beacon state: %v", err)
|
2019-01-28 11:59:37 +00:00
|
|
|
}
|
2019-04-10 06:52:06 +00:00
|
|
|
genBlock := b.NewGenesisBlock(stateRoot[:])
|
2019-04-24 17:21:00 +00:00
|
|
|
genBlockRoot, err := hashutil.HashBeaconBlock(genBlock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not hash beacon block: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-25 18:23:57 +00:00
|
|
|
// TODO(#2011): Remove this in state caching.
|
|
|
|
beaconState.LatestBlock = genBlock
|
|
|
|
|
2019-02-26 05:37:28 +00:00
|
|
|
if err := c.beaconDB.SaveBlock(genBlock); err != nil {
|
2019-03-06 20:13:09 +00:00
|
|
|
return nil, fmt.Errorf("could not save genesis block to disk: %v", err)
|
2019-01-28 11:59:37 +00:00
|
|
|
}
|
2019-04-24 17:21:00 +00:00
|
|
|
if err := c.beaconDB.SaveAttestationTarget(ctx, &pb.AttestationTarget{
|
|
|
|
Slot: genBlock.Slot,
|
|
|
|
BlockRoot: genBlockRoot[:],
|
|
|
|
ParentRoot: genBlock.ParentRootHash32,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to save attestation target: %v", err)
|
|
|
|
}
|
2019-04-02 08:49:45 +00:00
|
|
|
if err := c.beaconDB.UpdateChainHead(ctx, genBlock, beaconState); err != nil {
|
2019-03-06 20:13:09 +00:00
|
|
|
return nil, fmt.Errorf("could not set chain head, %v", err)
|
2019-02-26 05:37:28 +00:00
|
|
|
}
|
2019-03-19 17:16:17 +00:00
|
|
|
if err := c.beaconDB.SaveJustifiedBlock(genBlock); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not save gensis block as justified block: %v", err)
|
|
|
|
}
|
|
|
|
if err := c.beaconDB.SaveFinalizedBlock(genBlock); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not save gensis block as finalized block: %v", err)
|
|
|
|
}
|
|
|
|
if err := c.beaconDB.SaveJustifiedState(beaconState); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not save gensis state as justified state: %v", err)
|
|
|
|
}
|
|
|
|
if err := c.beaconDB.SaveFinalizedState(beaconState); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not save gensis state as finalized state: %v", err)
|
|
|
|
}
|
2019-03-06 20:13:09 +00:00
|
|
|
return beaconState, nil
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the blockchain service's main event loop and associated goroutines.
|
|
|
|
func (c *ChainService) Stop() error {
|
|
|
|
defer c.cancel()
|
2018-10-10 16:17:48 +00:00
|
|
|
|
2018-07-25 16:57:44 +00:00
|
|
|
log.Info("Stopping service")
|
2018-07-19 16:31:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-22 16:58:14 +00:00
|
|
|
|
2018-12-30 21:20:43 +00:00
|
|
|
// Status always returns nil.
|
|
|
|
// TODO(1202): Add service health checks.
|
|
|
|
func (c *ChainService) Status() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-24 04:09:59 +00:00
|
|
|
// CanonicalBlockFeed returns a channel that is written to
|
2018-08-18 03:34:56 +00:00
|
|
|
// whenever a new block is determined to be canonical in the chain.
|
2018-08-24 04:09:59 +00:00
|
|
|
func (c *ChainService) CanonicalBlockFeed() *event.Feed {
|
|
|
|
return c.canonicalBlockFeed
|
2018-08-18 03:34:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 17:51:57 +00:00
|
|
|
// StateInitializedFeed returns a feed that is written to
|
|
|
|
// when the beacon state is first initialized.
|
|
|
|
func (c *ChainService) StateInitializedFeed() *event.Feed {
|
|
|
|
return c.stateInitializedFeed
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:08:05 +00:00
|
|
|
// ChainHeadRoot returns the hash root of the last beacon block processed by the
|
|
|
|
// block chain service.
|
|
|
|
func (c *ChainService) ChainHeadRoot() ([32]byte, error) {
|
|
|
|
head, err := c.beaconDB.ChainHead()
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, fmt.Errorf("could not retrieve chain head: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-26 03:42:31 +00:00
|
|
|
root, err := hashutil.HashBeaconBlock(head)
|
2019-02-20 20:08:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, fmt.Errorf("could not tree hash parent block: %v", err)
|
|
|
|
}
|
|
|
|
return root, nil
|
|
|
|
}
|
2019-04-16 18:19:31 +00:00
|
|
|
|
|
|
|
// IsCanonical returns true if the input block hash of the corresponding slot
|
|
|
|
// is part of the canonical chain. False otherwise.
|
|
|
|
func (c *ChainService) IsCanonical(slot uint64, hash []byte) bool {
|
|
|
|
c.canonicalBlocksLock.RLock()
|
|
|
|
defer c.canonicalBlocksLock.RUnlock()
|
|
|
|
if canonicalHash, ok := c.canonicalBlocks[slot]; ok {
|
|
|
|
return bytes.Equal(canonicalHash, hash)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// InsertsCanonical inserts a canonical block hash to its corresponding slot.
|
|
|
|
// This is used for testing purpose.
|
|
|
|
func (c *ChainService) InsertsCanonical(slot uint64, hash []byte) {
|
|
|
|
c.canonicalBlocksLock.Lock()
|
|
|
|
defer c.canonicalBlocksLock.Unlock()
|
|
|
|
c.canonicalBlocks[slot] = hash
|
|
|
|
}
|