2018-08-09 18:25:48 +00:00
|
|
|
// Package node defines the services that a beacon chain node would perform.
|
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"
|
2018-10-17 06:11:24 +00:00
|
|
|
"path"
|
2018-07-14 02:15:37 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-08-15 04:49:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
gethRPC "github.com/ethereum/go-ethereum/rpc"
|
2019-03-08 07:30:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/attestation"
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
2018-10-05 17:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-01-31 18:54:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/operations"
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
2018-08-01 22:08:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/rpc"
|
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"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/debug"
|
2018-07-28 19:53:02 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/p2p"
|
2018-11-18 16:39:35 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2018-11-15 12:54:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/prometheus"
|
2019-02-28 03:55:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/tracing"
|
2019-01-10 04:19:33 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/version"
|
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")
|
2019-03-04 20:10:03 +00:00
|
|
|
|
|
|
|
const beaconChainDBName = "beaconchaindata"
|
|
|
|
const testSkipPowFlag = "test-skip-pow"
|
2018-07-19 16:31:50 +00:00
|
|
|
|
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-10-05 17:14:50 +00:00
|
|
|
db *db.BeaconDB
|
2018-07-14 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 18:25:48 +00:00
|
|
|
// NewBeaconNode creates a new node instance, sets up configuration options, and registers
|
2018-07-14 02:15:37 +00:00
|
|
|
// every required service to the node.
|
2018-08-09 18:25:48 +00:00
|
|
|
func NewBeaconNode(ctx *cli.Context) (*BeaconNode, error) {
|
2019-02-28 03:55:47 +00:00
|
|
|
if err := tracing.Setup(
|
|
|
|
"beacon-chain", // service name
|
|
|
|
ctx.GlobalString(cmd.TracingEndpointFlag.Name),
|
|
|
|
ctx.GlobalFloat64(cmd.TraceSampleFractionFlag.Name),
|
|
|
|
ctx.GlobalBool(cmd.EnableTracingFlag.Name),
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-14 02:15:37 +00:00
|
|
|
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-10-02 02:04:37 +00:00
|
|
|
// Use demo config values if demo config flag is set.
|
|
|
|
if ctx.GlobalBool(utils.DemoConfigFlag.Name) {
|
2019-02-25 02:09:45 +00:00
|
|
|
log.Info("Using custom parameter configuration")
|
2018-11-18 16:39:35 +00:00
|
|
|
params.UseDemoBeaconConfig()
|
2018-09-28 06:48:39 +00:00
|
|
|
}
|
|
|
|
|
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-09-20 11:46:35 +00:00
|
|
|
if err := beacon.registerP2P(ctx); err != nil {
|
2018-07-19 16:31:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-20 15:50:11 +00:00
|
|
|
if err := beacon.registerPOWChainService(ctx); err != nil {
|
2018-07-14 19:48:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-25 02:09:45 +00:00
|
|
|
if err := beacon.registerOperationService(); err != nil {
|
2018-07-25 16:57:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-06 21:03:52 +00:00
|
|
|
if err := beacon.registerAttestationService(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-25 02:09:45 +00:00
|
|
|
if err := beacon.registerBlockchainService(ctx); err != nil {
|
2018-09-24 01:22:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 06:57:04 +00:00
|
|
|
if err := beacon.registerSyncService(ctx); err != nil {
|
2018-11-19 01:59:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
if err := beacon.registerRPCService(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:54:45 +00:00
|
|
|
if !ctx.GlobalBool(cmd.DisableMonitoringFlag.Name) {
|
|
|
|
if err := beacon.registerPrometheusService(ctx); 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()
|
|
|
|
|
2019-01-11 02:19:58 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"version": version.GetVersion(),
|
|
|
|
}).Info("Starting beacon node")
|
2018-07-14 02:15:37 +00:00
|
|
|
|
|
|
|
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-09-21 18:35:48 +00:00
|
|
|
debug.Exit(b.ctx) // 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()
|
2018-10-17 06:11:24 +00:00
|
|
|
if err := b.db.Close(); err != nil {
|
|
|
|
log.Errorf("Failed to close database: %v", err)
|
|
|
|
}
|
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 {
|
2018-10-17 06:11:24 +00:00
|
|
|
baseDir := ctx.GlobalString(cmd.DataDirFlag.Name)
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-10-17 06:11:24 +00:00
|
|
|
db, err := db.NewDB(path.Join(baseDir, beaconChainDBName))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-19 01:59:11 +00:00
|
|
|
log.Info("checking db")
|
2018-07-30 06:14:50 +00:00
|
|
|
b.db = db
|
|
|
|
return nil
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 11:46:35 +00:00
|
|
|
func (b *BeaconNode) registerP2P(ctx *cli.Context) error {
|
|
|
|
beaconp2p, err := configureP2P(ctx)
|
2018-07-28 19:53:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not register p2p service: %v", err)
|
|
|
|
}
|
2018-08-29 16:32:54 +00:00
|
|
|
|
2018-07-28 19:53:02 +00:00
|
|
|
return b.services.RegisterService(beaconp2p)
|
|
|
|
}
|
|
|
|
|
2019-03-04 20:10:03 +00:00
|
|
|
func (b *BeaconNode) registerBlockchainService(_ *cli.Context) error {
|
2018-07-22 16:58:14 +00:00
|
|
|
var web3Service *powchain.Web3Service
|
2019-03-04 20:10:03 +00:00
|
|
|
if err := b.services.FetchService(&web3Service); err != nil {
|
|
|
|
return err
|
2018-07-22 16:58:14 +00:00
|
|
|
}
|
2019-02-25 02:09:45 +00:00
|
|
|
var opsService *operations.Service
|
|
|
|
if err := b.services.FetchService(&opsService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-06 21:03:52 +00:00
|
|
|
var attsService *attestation.Service
|
|
|
|
if err := b.services.FetchService(&attsService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-22 16:58:14 +00:00
|
|
|
|
2019-03-04 20:57:59 +00:00
|
|
|
blockchainService, err := blockchain.NewChainService(context.Background(), &blockchain.Config{
|
2018-11-05 17:35:50 +00:00
|
|
|
BeaconDB: b.db,
|
|
|
|
Web3Service: web3Service,
|
2019-02-25 02:09:45 +00:00
|
|
|
OpsPoolService: opsService,
|
2019-03-06 21:03:52 +00:00
|
|
|
AttsService: attsService,
|
2018-11-05 17:35:50 +00:00
|
|
|
BeaconBlockBuf: 10,
|
|
|
|
IncomingBlockBuf: 100, // Big buffer to accommodate other feed subscribers.
|
2018-08-24 04:09:59 +00:00
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:54:24 +00:00
|
|
|
func (b *BeaconNode) registerOperationService() error {
|
2019-03-04 20:57:59 +00:00
|
|
|
operationService := operations.NewOpsPoolService(context.Background(), &operations.Config{
|
2018-10-05 17:14:50 +00:00
|
|
|
BeaconDB: b.db,
|
2018-09-24 01:22:09 +00:00
|
|
|
})
|
|
|
|
|
2019-01-31 18:54:24 +00:00
|
|
|
return b.services.RegisterService(operationService)
|
2018-09-24 01:22:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 18:40:17 +00:00
|
|
|
func (b *BeaconNode) registerPOWChainService(cliCtx *cli.Context) error {
|
2019-03-04 20:10:03 +00:00
|
|
|
if b.ctx.GlobalBool(testSkipPowFlag) {
|
|
|
|
return b.services.RegisterService(&powchain.Web3Service{})
|
2018-08-20 15:50:11 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 04:49:57 +00:00
|
|
|
depAddress := b.ctx.GlobalString(utils.DepositContractFlag.Name)
|
|
|
|
|
2019-03-04 20:10:03 +00:00
|
|
|
if depAddress == "" {
|
|
|
|
log.Fatal("No deposit contract specified. Add --deposit-contract with a valud deposit contract address to start.")
|
|
|
|
}
|
|
|
|
|
2019-02-28 04:49:57 +00:00
|
|
|
if !common.IsHexAddress(depAddress) {
|
|
|
|
log.Fatalf("Invalid deposit contract address given: %s", depAddress)
|
|
|
|
}
|
|
|
|
|
2018-08-15 04:49:59 +00:00
|
|
|
rpcClient, err := gethRPC.Dial(b.ctx.GlobalString(utils.Web3ProviderFlag.Name))
|
|
|
|
if err != nil {
|
2018-08-20 15:50:11 +00:00
|
|
|
log.Fatalf("Access to PoW chain is required for validator. Unable to connect to Geth node: %v", err)
|
2018-08-15 04:49:59 +00:00
|
|
|
}
|
|
|
|
powClient := ethclient.NewClient(rpcClient)
|
|
|
|
|
2019-02-26 18:40:17 +00:00
|
|
|
delay := cliCtx.GlobalUint64(utils.ChainStartDelay.Name)
|
2019-02-14 17:11:03 +00:00
|
|
|
|
2019-02-26 18:40:17 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
cfg := &powchain.Web3ServiceConfig{
|
2019-01-16 14:01:21 +00:00
|
|
|
Endpoint: b.ctx.GlobalString(utils.Web3ProviderFlag.Name),
|
2019-02-28 04:49:57 +00:00
|
|
|
DepositContract: common.HexToAddress(depAddress),
|
2019-01-16 14:01:21 +00:00
|
|
|
Client: powClient,
|
|
|
|
Reader: powClient,
|
|
|
|
Logger: powClient,
|
2019-02-16 21:00:52 +00:00
|
|
|
BlockFetcher: powClient,
|
2019-01-16 14:01:21 +00:00
|
|
|
ContractBackend: powClient,
|
2019-02-05 17:25:09 +00:00
|
|
|
BeaconDB: b.db,
|
2019-02-14 17:11:03 +00:00
|
|
|
ChainStartDelay: delay,
|
2019-02-26 18:40:17 +00:00
|
|
|
}
|
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, cfg)
|
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
|
|
|
}
|
2019-02-26 18:40:17 +00:00
|
|
|
|
|
|
|
if err := b.db.VerifyContractAddress(ctx, cfg.DepositContract); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-14 19:48:42 +00:00
|
|
|
return b.services.RegisterService(web3Service)
|
|
|
|
}
|
2018-07-25 16:57:44 +00:00
|
|
|
|
2019-03-04 20:10:03 +00:00
|
|
|
func (b *BeaconNode) registerSyncService(_ *cli.Context) error {
|
2018-07-25 16:57:44 +00:00
|
|
|
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
|
|
|
|
2019-01-31 18:54:24 +00:00
|
|
|
var operationService *operations.Service
|
|
|
|
if err := b.services.FetchService(&operationService); err != nil {
|
2018-09-24 01:22:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-21 06:57:04 +00:00
|
|
|
var web3Service *powchain.Web3Service
|
2019-03-04 20:10:03 +00:00
|
|
|
if err := b.services.FetchService(&web3Service); err != nil {
|
|
|
|
return err
|
2019-02-21 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:26:44 +00:00
|
|
|
cfg := &rbcsync.Config{
|
2019-01-31 18:54:24 +00:00
|
|
|
ChainService: chainService,
|
|
|
|
P2P: p2pService,
|
|
|
|
BeaconDB: b.db,
|
|
|
|
OperationService: operationService,
|
2019-02-21 06:57:04 +00:00
|
|
|
PowChainService: web3Service,
|
2018-11-19 01:59:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:14:50 +00:00
|
|
|
syncService := rbcsync.NewSyncService(context.Background(), cfg)
|
2018-07-25 16:57:44 +00:00
|
|
|
return b.services.RegisterService(syncService)
|
|
|
|
}
|
2018-07-31 04:41:27 +00:00
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
func (b *BeaconNode) registerRPCService(ctx *cli.Context) error {
|
2018-08-18 03:34:56 +00:00
|
|
|
var chainService *blockchain.ChainService
|
|
|
|
if err := b.services.FetchService(&chainService); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:54:24 +00:00
|
|
|
var operationService *operations.Service
|
|
|
|
if err := b.services.FetchService(&operationService); err != nil {
|
2018-09-24 01:22:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:32:20 +00:00
|
|
|
var web3Service *powchain.Web3Service
|
2019-03-04 20:10:03 +00:00
|
|
|
if err := b.services.FetchService(&web3Service); err != nil {
|
|
|
|
return err
|
2018-09-21 14:32:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
port := ctx.GlobalString(utils.RPCPort.Name)
|
2018-08-08 22:43:25 +00:00
|
|
|
cert := ctx.GlobalString(utils.CertFlag.Name)
|
|
|
|
key := ctx.GlobalString(utils.KeyFlag.Name)
|
2019-02-20 21:58:23 +00:00
|
|
|
chainStartDelayFlag := ctx.GlobalUint64(utils.ChainStartDelay.Name)
|
2019-03-04 20:57:59 +00:00
|
|
|
rpcService := rpc.NewRPCService(context.Background(), &rpc.Config{
|
2019-02-20 21:58:23 +00:00
|
|
|
Port: port,
|
|
|
|
CertFlag: cert,
|
|
|
|
KeyFlag: key,
|
|
|
|
ChainStartDelayFlag: chainStartDelayFlag,
|
|
|
|
SubscriptionBuf: 100,
|
|
|
|
BeaconDB: b.db,
|
|
|
|
ChainService: chainService,
|
|
|
|
OperationService: operationService,
|
|
|
|
POWChainService: web3Service,
|
2018-09-05 03:35:32 +00:00
|
|
|
})
|
2018-08-24 16:07:23 +00:00
|
|
|
|
2018-08-01 22:08:44 +00:00
|
|
|
return b.services.RegisterService(rpcService)
|
|
|
|
}
|
2018-11-15 12:54:45 +00:00
|
|
|
|
|
|
|
func (b *BeaconNode) registerPrometheusService(ctx *cli.Context) error {
|
|
|
|
service := prometheus.NewPrometheusService(
|
|
|
|
fmt.Sprintf(":%d", ctx.GlobalInt64(cmd.MonitoringPortFlag.Name)),
|
2018-12-30 21:20:43 +00:00
|
|
|
b.services,
|
2018-11-15 12:54:45 +00:00
|
|
|
)
|
|
|
|
hook := prometheus.NewLogrusCollector()
|
|
|
|
logrus.AddHook(hook)
|
|
|
|
return b.services.RegisterService(service)
|
|
|
|
}
|
2019-03-06 21:03:52 +00:00
|
|
|
|
|
|
|
func (b *BeaconNode) registerAttestationService() error {
|
|
|
|
attsService := attestation.NewAttestationService(context.Background(),
|
|
|
|
&attestation.Config{
|
|
|
|
BeaconDB: b.db,
|
|
|
|
})
|
|
|
|
|
|
|
|
return b.services.RegisterService(attsService)
|
|
|
|
}
|