-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
142 lines (119 loc) · 3.83 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package node
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/ovcharovvladimir/Prysm/beacon-chain/blockchain"
"github.com/ovcharovvladimir/Prysm/beacon-chain/database"
"github.com/ovcharovvladimir/Prysm/beacon-chain/powchain"
"github.com/ovcharovvladimir/Prysm/beacon-chain/utils"
"github.com/ovcharovvladimir/Prysm/shared"
"github.com/ovcharovvladimir/Prysm/shared/cmd"
"github.com/ovcharovvladimir/Prysm/shared/debug"
"github.com/ovcharovvladimir/essentiaHybrid/common"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var log = logrus.WithField("prefix", "node")
var beaconChainDBName = "beaconchaindata"
// 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 {
ctx *cli.Context
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()
beacon := &BeaconNode{
ctx: ctx,
services: registry,
stop: make(chan struct{}),
}
path := ctx.GlobalString(cmd.DataDirFlag.Name)
if err := beacon.registerBeaconDB(path); err != nil {
return nil, err
}
if err := beacon.registerPOWChainService(); err != nil {
return nil, err
}
if err := beacon.registerBlockchainService(); err != nil {
return nil, err
}
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 {
log.Info("Already shutting down, interrupt more to panic", "times", i-1)
}
}
debug.Exit() // Ensure trace and CPU profile data are flushed.
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)
}
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
}
var web3Service *powchain.Web3Service
if err := b.services.FetchService(&web3Service); err != nil {
return err
}
blockchainService, err := blockchain.NewChainService(context.TODO(), beaconDB, web3Service)
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)),
})
if err != nil {
return fmt.Errorf("could not register proof-of-work chain web3Service: %v", err)
}
return b.services.RegisterService(web3Service)
}