-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathservices.go
More file actions
322 lines (287 loc) · 9.34 KB
/
services.go
File metadata and controls
322 lines (287 loc) · 9.34 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package services
import (
"fmt"
"math/big"
"os"
"sync"
"github.com/docker/docker/client"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/rocket-pool/rocketpool-go/rocketpool"
"github.com/rocket-pool/rocketpool-go/utils/eth"
"github.com/urfave/cli"
"github.com/rocket-pool/smartnode/shared/services/beacon"
"github.com/rocket-pool/smartnode/shared/services/config"
"github.com/rocket-pool/smartnode/shared/services/contracts"
"github.com/rocket-pool/smartnode/shared/services/passwords"
"github.com/rocket-pool/smartnode/shared/services/wallet"
lhkeystore "github.com/rocket-pool/smartnode/shared/services/wallet/keystore/lighthouse"
lokeystore "github.com/rocket-pool/smartnode/shared/services/wallet/keystore/lodestar"
nmkeystore "github.com/rocket-pool/smartnode/shared/services/wallet/keystore/nimbus"
prkeystore "github.com/rocket-pool/smartnode/shared/services/wallet/keystore/prysm"
tkkeystore "github.com/rocket-pool/smartnode/shared/services/wallet/keystore/teku"
"github.com/rocket-pool/smartnode/shared/utils/rp"
)
// Config
const (
DockerAPIVersion string = "1.40"
EcContainerName string = "eth1"
FallbackEcContainerName string = "eth1-fallback"
BnContainerName string = "eth2"
)
// Service instances & initializers
var (
cfg *config.RocketPoolConfig
passwordManager *passwords.PasswordManager
nodeWallet *wallet.Wallet
ecManager *ExecutionClientManager
bcManager *BeaconClientManager
rocketPool *rocketpool.RocketPool
oneInchOracle *contracts.OneInchOracle
rplFaucet *contracts.RPLFaucet
snapshotDelegation *contracts.SnapshotDelegation
beaconClient beacon.Client
docker *client.Client
initCfg sync.Once
initPasswordManager sync.Once
initNodeWallet sync.Once
initECManager sync.Once
initBCManager sync.Once
initRocketPool sync.Once
initOneInchOracle sync.Once
initRplFaucet sync.Once
initSnapshotDelegation sync.Once
initBeaconClient sync.Once
initDocker sync.Once
)
//
// Service providers
//
func GetConfig(c *cli.Context) (*config.RocketPoolConfig, error) {
return getConfig(c)
}
func GetPasswordManager(c *cli.Context) (*passwords.PasswordManager, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
return getPasswordManager(cfg), nil
}
func GetWallet(c *cli.Context) (*wallet.Wallet, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
pm := getPasswordManager(cfg)
return getWallet(c, cfg, pm)
}
func GetEthClient(c *cli.Context) (*ExecutionClientManager, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
ec, err := getEthClient(c, cfg)
if err != nil {
return nil, err
}
return ec, nil
}
func GetRocketPool(c *cli.Context) (*rocketpool.RocketPool, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
var ec rocketpool.ExecutionClient
if c.GlobalBool("use-protected-api") {
url := cfg.Smartnode.GetFlashbotsProtectUrl()
ec, err = ethclient.Dial(url)
} else {
ec, err = getEthClient(c, cfg)
}
if err != nil {
return nil, err
}
return getRocketPool(cfg, ec)
}
func GetOneInchOracle(c *cli.Context) (*contracts.OneInchOracle, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
ec, err := getEthClient(c, cfg)
if err != nil {
return nil, err
}
return getOneInchOracle(cfg, ec)
}
func GetRplFaucet(c *cli.Context) (*contracts.RPLFaucet, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
ec, err := getEthClient(c, cfg)
if err != nil {
return nil, err
}
return getRplFaucet(cfg, ec)
}
func GetSnapshotDelegation(c *cli.Context) (*contracts.SnapshotDelegation, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
ec, err := getEthClient(c, cfg)
if err != nil {
return nil, err
}
return getSnapshotDelegation(cfg, ec)
}
func GetBeaconClient(c *cli.Context) (*BeaconClientManager, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
return getBeaconClient(c, cfg)
}
func GetDocker(c *cli.Context) (*client.Client, error) {
return getDocker()
}
//
// Service instance getters
//
func getConfig(c *cli.Context) (*config.RocketPoolConfig, error) {
var err error
initCfg.Do(func() {
settingsFile := os.ExpandEnv(c.GlobalString("settings"))
cfg, err = rp.LoadConfigFromFile(settingsFile)
if cfg == nil && err == nil {
err = fmt.Errorf("Settings file [%s] not found.", settingsFile)
}
})
return cfg, err
}
func getPasswordManager(cfg *config.RocketPoolConfig) *passwords.PasswordManager {
initPasswordManager.Do(func() {
passwordManager = passwords.NewPasswordManager(os.ExpandEnv(cfg.Smartnode.GetPasswordPath()))
})
return passwordManager
}
func getWallet(c *cli.Context, cfg *config.RocketPoolConfig, pm *passwords.PasswordManager) (*wallet.Wallet, error) {
var err error
initNodeWallet.Do(func() {
var maxFee *big.Int
maxFeeFloat := c.GlobalFloat64("maxFee")
if maxFeeFloat == 0 {
maxFeeFloat = cfg.Smartnode.ManualMaxFee.Value.(float64)
}
if maxFeeFloat != 0 {
maxFee = eth.GweiToWei(maxFeeFloat)
}
var maxPriorityFee *big.Int
maxPriorityFeeFloat := c.GlobalFloat64("maxPrioFee")
if maxPriorityFeeFloat == 0 {
maxPriorityFeeFloat = cfg.Smartnode.PriorityFee.Value.(float64)
}
if maxPriorityFeeFloat != 0 {
maxPriorityFee = eth.GweiToWei(maxPriorityFeeFloat)
}
chainId := cfg.Smartnode.GetChainID()
nodeWallet, err = wallet.NewWallet(os.ExpandEnv(cfg.Smartnode.GetWalletPath()), chainId, maxFee, maxPriorityFee, 0, pm)
if err != nil {
return
}
// Offline mode
if cfg.Offline.Value == true {
operatorAddress := cfg.NodeAddress.Value.(string)
if operatorAddress == "" {
fmt.Println("Offline mode enabled, but no operator address specified in config file, skipping offline setup...")
return
}
nodeWallet.SetOffline(operatorAddress)
}
// Keystores
lighthouseKeystore := lhkeystore.NewKeystore(os.ExpandEnv(cfg.Smartnode.GetValidatorKeychainPath()), pm)
lodestarKeystore := lokeystore.NewKeystore(os.ExpandEnv(cfg.Smartnode.GetValidatorKeychainPath()), pm)
nimbusKeystore := nmkeystore.NewKeystore(os.ExpandEnv(cfg.Smartnode.GetValidatorKeychainPath()), pm)
prysmKeystore := prkeystore.NewKeystore(os.ExpandEnv(cfg.Smartnode.GetValidatorKeychainPath()), pm)
tekuKeystore := tkkeystore.NewKeystore(os.ExpandEnv(cfg.Smartnode.GetValidatorKeychainPath()), pm)
nodeWallet.AddKeystore("lighthouse", lighthouseKeystore)
nodeWallet.AddKeystore("lodestar", lodestarKeystore)
nodeWallet.AddKeystore("nimbus", nimbusKeystore)
nodeWallet.AddKeystore("prysm", prysmKeystore)
nodeWallet.AddKeystore("teku", tekuKeystore)
})
return nodeWallet, err
}
func getEthClient(c *cli.Context, cfg *config.RocketPoolConfig) (*ExecutionClientManager, error) {
var err error
initECManager.Do(func() {
// Create a new client manager
ecManager, err = NewExecutionClientManager(cfg)
if err == nil {
// Check if the manager should ignore sync checks and/or default to using the fallback (used by the API container when driven by the CLI)
if c.GlobalBool("ignore-sync-check") {
ecManager.ignoreSyncCheck = true
}
if c.GlobalBool("force-fallbacks") {
ecManager.primaryReady = false
}
}
})
return ecManager, err
}
func getRocketPool(cfg *config.RocketPoolConfig, client rocketpool.ExecutionClient) (*rocketpool.RocketPool, error) {
var err error
initRocketPool.Do(func() {
rocketPool, err = rocketpool.NewRocketPool(client, common.HexToAddress(cfg.Smartnode.GetStorageAddress()))
})
return rocketPool, err
}
func getOneInchOracle(cfg *config.RocketPoolConfig, client rocketpool.ExecutionClient) (*contracts.OneInchOracle, error) {
var err error
initOneInchOracle.Do(func() {
oneInchOracle, err = contracts.NewOneInchOracle(common.HexToAddress(cfg.Smartnode.GetOneInchOracleAddress()), client)
})
return oneInchOracle, err
}
func getRplFaucet(cfg *config.RocketPoolConfig, client rocketpool.ExecutionClient) (*contracts.RPLFaucet, error) {
var err error
initRplFaucet.Do(func() {
rplFaucet, err = contracts.NewRPLFaucet(common.HexToAddress(cfg.Smartnode.GetRplFaucetAddress()), client)
})
return rplFaucet, err
}
func getSnapshotDelegation(cfg *config.RocketPoolConfig, client rocketpool.ExecutionClient) (*contracts.SnapshotDelegation, error) {
var err error
initSnapshotDelegation.Do(func() {
address := cfg.Smartnode.GetSnapshotDelegationAddress()
if address != "" {
snapshotDelegation, err = contracts.NewSnapshotDelegation(common.HexToAddress(address), client)
}
})
return snapshotDelegation, err
}
func getBeaconClient(c *cli.Context, cfg *config.RocketPoolConfig) (*BeaconClientManager, error) {
var err error
initBCManager.Do(func() {
// Create a new client manager
bcManager, err = NewBeaconClientManager(cfg)
if err == nil {
// Check if the manager should ignore sync checks and/or default to using the fallback (used by the API container when driven by the CLI)
if c.GlobalBool("ignore-sync-check") {
bcManager.ignoreSyncCheck = true
}
if c.GlobalBool("force-fallbacks") {
bcManager.primaryReady = false
}
}
})
return bcManager, err
}
func getDocker() (*client.Client, error) {
var err error
initDocker.Do(func() {
docker, err = client.NewClientWithOpts(client.WithVersion(DockerAPIVersion))
})
return docker, err
}