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/database"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/debug"
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-19 16:31:50 +00:00
|
|
|
path := ctx.GlobalString(cmd.DataDirFlag.Name)
|
|
|
|
if err := beacon.registerBeaconDB(path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
if err := beacon.registerPOWChainService(); err != nil {
|
2018-07-19 16:31:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
if err := beacon.registerBlockchainService(); err != nil {
|
2018-07-14 19:48:42 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
b.services.StopAll()
|
|
|
|
log.Info("Stopping beacon node")
|
|
|
|
close(b.stop)
|
|
|
|
}
|
2018-07-14 19:48:42 +00:00
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
func (b *BeaconNode) registerBeaconDB(path string) error {
|
|
|
|
config := &database.BeaconDBConfig{DataDir: path, Name: beaconChainDBName, InMemory: false}
|
|
|
|
beaconDB, err := database.NewBeaconDB(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not register beaconDB service: %v", err)
|
|
|
|
}
|
|
|
|
return b.services.RegisterService(beaconDB)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BeaconNode) registerBlockchainService() error {
|
|
|
|
var beaconDB *database.BeaconDB
|
|
|
|
if err := b.services.FetchService(&beaconDB); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
var web3Service *powchain.Web3Service
|
|
|
|
if err := b.services.FetchService(&web3Service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blockchainService, err := blockchain.NewChainService(context.TODO(), beaconDB, 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)
|
|
|
|
}
|