-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathservices.go
More file actions
294 lines (264 loc) · 8.82 KB
/
services.go
File metadata and controls
294 lines (264 loc) · 8.82 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
package services
import (
"fmt"
"math/big"
"os"
"path/filepath"
"sync"
"github.com/docker/docker/client"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/rocket-pool/smartnode/bindings/rocketpool"
"github.com/rocket-pool/smartnode/bindings/utils/eth"
rpSettings "github.com/rocket-pool/smartnode/shared/services/rocketpool"
"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"
)
// Service instances & initializers
var (
cfg *config.RocketPoolConfig
passwordManager *passwords.PasswordManager
addressManager *wallet.AddressManager
nodeWallet wallet.Wallet
ecManager *ExecutionClientManager
bcManager *BeaconClientManager
rocketPool *rocketpool.RocketPool
rocketSignerRegistry *contracts.RocketSignerRegistry
beaconClient beacon.Client
docker *client.Client
initCfg sync.Once
initPasswordManager sync.Once
initAddressManager sync.Once
initNodeWallet sync.Once
initECManager sync.Once
initBCManager sync.Once
initRocketPool sync.Once
initOneInchOracle sync.Once
initRocketSignerRegistry 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)
am := getAddressManager(cfg)
return getWallet(c, cfg, pm, am, false)
}
func GetHdWallet(c *cli.Context) (wallet.Wallet, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
pm := getPasswordManager(cfg)
am := getAddressManager(cfg)
return getWallet(c, cfg, pm, am, true)
}
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 GetRocketSignerRegistry(c *cli.Context) (*contracts.RocketSignerRegistry, error) {
cfg, err := getConfig(c)
if err != nil {
return nil, err
}
ec, err := getEthClient(c, cfg)
if err != nil {
return nil, err
}
return getRocketSignerRegistry(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) {
var err error
initDocker.Do(func() {
docker, err = client.NewClientWithOpts(client.WithVersion(dockerAPIVersion))
})
return docker, err
}
//
// Service instance getters
//
func getConfig(c *cli.Context) (*config.RocketPoolConfig, error) {
var err error
initCfg.Do(func() {
settingsFile := c.GlobalString("settings")
if settingsFile == "" {
configDir := c.GlobalString("config-path")
if configDir != "" {
settingsFile = filepath.Join(configDir, rpSettings.SettingsFile)
}
}
expanded := os.ExpandEnv(settingsFile)
cfg, err = rp.LoadConfigFromFile(expanded)
if cfg == nil && err == nil {
err = fmt.Errorf("settings file [%s] not found", expanded)
}
})
return cfg, err
}
func getPasswordManager(cfg *config.RocketPoolConfig) *passwords.PasswordManager {
initPasswordManager.Do(func() {
passwordManager = passwords.NewPasswordManager(os.ExpandEnv(cfg.Smartnode.GetPasswordPath()))
})
return passwordManager
}
func getAddressManager(cfg *config.RocketPoolConfig) *wallet.AddressManager {
initAddressManager.Do(func() {
addressManager = wallet.NewAddressManager(os.ExpandEnv(cfg.Smartnode.GetNodeAddressPath()))
})
return addressManager
}
func getWallet(c *cli.Context, cfg *config.RocketPoolConfig, pm *passwords.PasswordManager, am *wallet.AddressManager, ignoreMasquerade bool) (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()
if ignoreMasquerade {
nodeWallet, err = wallet.NewHdWallet(os.ExpandEnv(cfg.Smartnode.GetWalletPath()), chainId, maxFee, maxPriorityFee, 0, pm, am)
} else {
nodeWallet, err = wallet.NewWallet(os.ExpandEnv(cfg.Smartnode.GetNodeAddressPath()), os.ExpandEnv(cfg.Smartnode.GetWalletPath()), chainId, maxFee, maxPriorityFee, 0, pm, am)
}
if err != nil {
return
}
// 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 getRocketSignerRegistry(cfg *config.RocketPoolConfig, client rocketpool.ExecutionClient) (*contracts.RocketSignerRegistry, error) {
var err error
initRocketSignerRegistry.Do(func() {
address := cfg.Smartnode.GetRocketSignerRegistryAddress()
if address != "" {
rocketSignerRegistry, err = contracts.NewRocketSignerRegistry(common.HexToAddress(address), client)
}
})
return rocketSignerRegistry, 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
}