2018-07-14 02:15:37 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-07-14 19:48:42 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-07-14 02:15:37 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
2018-07-31 04:41:27 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/simulator"
|
2018-07-28 19:53:02 +00:00
|
|
|
rbcsync "github.com/prysmaticlabs/prysm/beacon-chain/sync"
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
2018-07-30 06:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/database"
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/debug"
|
2018-07-28 19:53:02 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/p2p"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-07-14 02:15:37 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
var log = logrus.WithField("prefix", "node")
|
2018-07-19 16:31:50 +00:00
|
|
|
var beaconChainDBName = "beaconchaindata"
|
|
|
|
|
2018-07-14 02:15:37 +00:00
|
|
|
// BeaconNode defines a struct that handles the services running a random beacon chain
|
|
|
|
// full PoS node. It handles the lifecycle of the entire system and registers
|
|
|
|
// services to a service registry.
|
|
|
|
type BeaconNode struct {
|
2018-07-14 19:48:42 +00:00
|
|
|
ctx *cli.Context
|
2018-07-14 02:15:37 +00:00
|
|
|
services *shared.ServiceRegistry
|
|
|
|
lock sync.RWMutex
|
|
|
|
stop chan struct{} // Channel to wait for termination notifications.
|
2018-07-30 06:14:50 +00:00
|
|
|
db *database.DB
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new node instance, sets up configuration options, and registers
|
|
|
|
// every required service to the node.
|
|
|
|
func New(ctx *cli.Context) (*BeaconNode, error) {
|
|
|
|
registry := shared.NewServiceRegistry()
|
2018-07-19 16:31:50 +00:00
|
|
|
|
2018-07-14 02:15:37 +00:00
|
|
|
beacon := &BeaconNode{
|
2018-07-14 19:48:42 +00:00
|
|
|
ctx: ctx,
|
2018-07-14 02:15:37 +00:00
|
|
|
services: registry,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:14:50 +00:00
|
|
|
if err := beacon.startDB(ctx); err != nil {
|
2018-07-19 16:31:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-28 19:53:02 +00:00
|
|
|
if err := beacon.registerP2P(); err != nil {
|
2018-07-19 16:31:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-28 19:53:02 +00:00
|
|
|
if err := beacon.registerPOWChainService(); err != nil {
|
2018-07-14 19:48:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-28 19:53:02 +00:00
|
|
|
if err := beacon.registerBlockchainService(); err != nil {
|
2018-07-25 16:57:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-31 04:41:27 +00:00
|
|
|
if ctx.GlobalBool(utils.SimulatorFlag.Name) {
|
|
|
|
if err := beacon.registerSimulatorService(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:57:44 +00:00
|
|
|
if err := beacon.registerSyncService(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:15:37 +00:00
|
|
|
return beacon, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the BeaconNode and kicks off every registered service.
|
|
|
|
func (b *BeaconNode) Start() {
|
|
|
|
b.lock.Lock()
|
|
|
|
|
|
|
|
log.Info("Starting beacon node")
|
|
|
|
|
|
|
|
b.services.StartAll()
|
|
|
|
|
|
|
|
stop := b.stop
|
|
|
|
b.lock.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sigc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
defer signal.Stop(sigc)
|
|
|
|
<-sigc
|
|
|
|
log.Info("Got interrupt, shutting down...")
|
|
|
|
go b.Close()
|
|
|
|
for i := 10; i > 0; i-- {
|
|
|
|
<-sigc
|
|
|
|
if i > 1 {
|
2018-07-21 17:51:18 +00:00
|
|
|
log.Info("Already shutting down, interrupt more to panic", "times", i-1)
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 03:35:15 +00:00
|
|
|
debug.Exit() // Ensure trace and CPU profile data are flushed.
|
2018-07-14 02:15:37 +00:00
|
|
|
panic("Panic closing the beacon node")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for stop channel to be closed.
|
|
|
|
<-stop
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close handles graceful shutdown of the system.
|
|
|
|
func (b *BeaconNode) Close() {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
log.Info("Stopping beacon node")
|
2018-07-31 04:41:27 +00:00
|
|
|
b.services.StopAll()
|
|
|
|
b.db.Close()
|
2018-07-14 02:15:37 +00:00
|
|
|
close(b.stop)
|
|
|
|
}
|
2018-07-14 19:48:42 +00:00
|
|
|
|
2018-07-30 06:14:50 +00:00
|
|
|
func (b *BeaconNode) startDB(ctx *cli.Context) error {
|
|
|
|
path := ctx.GlobalString(cmd.DataDirFlag.Name)
|
|
|
|
config := &database.DBConfig{DataDir: path, Name: beaconChainDBName, InMemory: false}
|
|
|
|
db, err := database.NewDB(config)
|
2018-07-19 16:31:50 +00:00
|
|
|
if err != nil {
|
2018-07-30 06:14:50 +00:00
|
|
|
return err
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
2018-07-30 06:14:50 +00:00
|
|
|
|
|
|
|
b.db = db
|
|
|
|
return nil
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 19:53:02 +00:00
|
|
|
func (b *BeaconNode) registerP2P() error {
|
|
|
|
beaconp2p, err := p2p.NewServer()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not register p2p service: %v", err)
|
|
|
|
}
|
|
|
|
return b.services.RegisterService(beaconp2p)
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
func (b *BeaconNode) registerBlockchainService() error {
|
2018-07-22 16:58:14 +00:00
|
|
|
var web3Service *powchain.Web3Service
|
|
|
|
if err := b.services.FetchService(&web3Service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:14:50 +00:00
|
|
|
blockchainService, err := blockchain.NewChainService(context.TODO(), b.db, web3Service)
|
2018-07-19 16:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not register blockchain service: %v", err)
|
|
|
|
}
|
|
|
|
return b.services.RegisterService(blockchainService)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BeaconNode) registerPOWChainService() error {
|
|
|
|
web3Service, err := powchain.NewWeb3Service(context.TODO(), &powchain.Web3ServiceConfig{
|
|
|
|
Endpoint: b.ctx.GlobalString(utils.Web3ProviderFlag.Name),
|
|
|
|
Pubkey: b.ctx.GlobalString(utils.PubKeyFlag.Name),
|
|
|
|
VrcAddr: common.HexToAddress(b.ctx.GlobalString(utils.VrcContractFlag.Name)),
|
|
|
|
})
|
2018-07-14 19:48:42 +00:00
|
|
|
if err != nil {
|
2018-07-19 16:31:50 +00:00
|
|
|
return fmt.Errorf("could not register proof-of-work chain web3Service: %v", err)
|
2018-07-14 19:48:42 +00:00
|
|
|
}
|
|
|
|
return b.services.RegisterService(web3Service)
|
|
|
|
}
|
2018-07-25 16:57:44 +00:00
|
|
|
|
|
|
|
func (b *BeaconNode) registerSyncService() error {
|
|
|
|
var chainService *blockchain.ChainService
|
2018-07-29 00:44:24 +00:00
|
|
|
if err := b.services.FetchService(&chainService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-25 16:57:44 +00:00
|
|
|
|
2018-07-28 19:53:02 +00:00
|
|
|
var p2pService *p2p.Server
|
2018-07-29 00:44:24 +00:00
|
|
|
if err := b.services.FetchService(&p2pService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-25 16:57:44 +00:00
|
|
|
|
2018-07-28 19:53:02 +00:00
|
|
|
syncService := rbcsync.NewSyncService(context.Background(), rbcsync.DefaultConfig(), p2pService, chainService)
|
2018-07-25 16:57:44 +00:00
|
|
|
return b.services.RegisterService(syncService)
|
|
|
|
}
|
2018-07-31 04:41:27 +00:00
|
|
|
|
|
|
|
func (b *BeaconNode) registerSimulatorService() error {
|
|
|
|
var p2pService *p2p.Server
|
|
|
|
if err := b.services.FetchService(&p2pService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var web3Service *powchain.Web3Service
|
|
|
|
if err := b.services.FetchService(&web3Service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chainService *blockchain.ChainService
|
|
|
|
if err := b.services.FetchService(&chainService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := simulator.DefaultConfig()
|
|
|
|
simulatorService := simulator.NewSimulator(context.TODO(), cfg, p2pService, web3Service, chainService)
|
|
|
|
return b.services.RegisterService(simulatorService)
|
|
|
|
}
|