/
create-vacant-minipool.go
277 lines (240 loc) · 8.21 KB
/
create-vacant-minipool.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
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
package node
import (
"context"
"fmt"
"math/big"
"time"
"github.com/Seb369888/poolsea-go/minipool"
"github.com/Seb369888/poolsea-go/node"
"github.com/Seb369888/poolsea-go/settings/protocol"
"github.com/Seb369888/poolsea-go/settings/trustednode"
rptypes "github.com/Seb369888/poolsea-go/types"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/Seb369888/smartnode/shared/services"
"github.com/Seb369888/smartnode/shared/services/beacon"
"github.com/Seb369888/smartnode/shared/types/api"
cfgtypes "github.com/Seb369888/smartnode/shared/types/config"
"github.com/Seb369888/smartnode/shared/utils/eth1"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
)
func canCreateVacantMinipool(c *cli.Context, amountWei *big.Int, minNodeFee float64, salt *big.Int, pubkey rptypes.ValidatorPubkey) (*api.CanCreateVacantMinipoolResponse, error) {
// Get services
if err := services.RequireNodeRegistered(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
ec, err := services.GetEthClient(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
bc, err := services.GetBeaconClient(c)
if err != nil {
return nil, err
}
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
// Response
response := api.CanCreateVacantMinipoolResponse{}
// Get node account
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return nil, err
}
// Adjust the salt
if salt.Cmp(big.NewInt(0)) == 0 {
nonce, err := ec.NonceAt(context.Background(), nodeAccount.Address, nil)
if err != nil {
return nil, err
}
salt.SetUint64(nonce)
}
// Data
var wg1 errgroup.Group
var ethMatched *big.Int
var ethMatchedLimit *big.Int
// Check node deposits are enabled
wg1.Go(func() error {
depositEnabled, err := protocol.GetVacantMinipoolsEnabled(rp, nil)
if err == nil {
response.DepositDisabled = !depositEnabled
}
return err
})
// Get node staking information
wg1.Go(func() error {
var err error
ethMatched, err = node.GetNodeEthMatched(rp, nodeAccount.Address, nil)
return err
})
wg1.Go(func() error {
var err error
ethMatchedLimit, err = node.GetNodeEthMatchedLimit(rp, nodeAccount.Address, nil)
return err
})
// Wait for data
if err := wg1.Wait(); err != nil {
return nil, err
}
// Get the next minipool address and withdrawal credentials
minipoolAddress, err := minipool.GetExpectedAddress(rp, nodeAccount.Address, salt, nil)
if err != nil {
return nil, err
}
// Check data
validatorEthWei := eth.EthToWei(ValidatorEth)
matchRequest := big.NewInt(0).Sub(validatorEthWei, amountWei)
availableToMatch := big.NewInt(0).Sub(ethMatchedLimit, ethMatched)
response.InsufficientRplStake = (availableToMatch.Cmp(matchRequest) == -1)
response.MinipoolAddress = minipoolAddress
// Update response
response.CanDeposit = !(response.InsufficientRplStake || response.InvalidAmount || response.DepositDisabled)
if !response.CanDeposit {
return &response, nil
}
// Get gas estimate
opts, err := w.GetNodeAccountTransactor()
if err != nil {
return nil, err
}
// Check if the pubkey is for an existing active_ongoing validator
validatorStatus, err := bc.GetValidatorStatus(pubkey, nil)
if err != nil {
return nil, fmt.Errorf("error checking status of existing validator: %w", err)
}
if !validatorStatus.Exists {
return nil, fmt.Errorf("validator %s does not exist on the Beacon chain. If you recently created it, please wait until the Consensus layer has processed your deposits.", pubkey.Hex())
}
if validatorStatus.Status != beacon.ValidatorState_ActiveOngoing {
return nil, fmt.Errorf("validator %s must be in the active_ongoing state to be migrated, but it is currently in %s.", pubkey.Hex(), string(validatorStatus.Status))
}
if cfg.Smartnode.Network.Value.(cfgtypes.Network) != cfgtypes.Network_Devnet && validatorStatus.WithdrawalCredentials[0] != 0x00 {
return nil, fmt.Errorf("validator %s already has withdrawal credentials [%s], which are not BLS credentials.", pubkey.Hex(), validatorStatus.WithdrawalCredentials.Hex())
}
// Convert the existing balance from gwei to wei
balanceWei := big.NewInt(0).SetUint64(validatorStatus.Balance)
balanceWei.Mul(balanceWei, big.NewInt(1e9))
// Run the deposit gas estimator
gasInfo, err := node.EstimateCreateVacantMinipoolGas(rp, amountWei, minNodeFee, pubkey, salt, minipoolAddress, balanceWei, opts)
if err != nil {
return nil, err
}
response.GasInfo = gasInfo
return &response, nil
}
func createVacantMinipool(c *cli.Context, amountWei *big.Int, minNodeFee float64, salt *big.Int, pubkey rptypes.ValidatorPubkey) (*api.CreateVacantMinipoolResponse, error) {
// Get services
if err := services.RequireNodeRegistered(c); err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
ec, err := services.GetEthClient(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
bc, err := services.GetBeaconClient(c)
if err != nil {
return nil, err
}
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
// Get node account
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return nil, err
}
// Response
response := api.CreateVacantMinipoolResponse{}
// Adjust the salt
if salt.Cmp(big.NewInt(0)) == 0 {
nonce, err := ec.NonceAt(context.Background(), nodeAccount.Address, nil)
if err != nil {
return nil, err
}
salt.SetUint64(nonce)
}
// Make sure ETH2 is on the correct chain
depositContractInfo, err := getDepositContractInfo(c)
if err != nil {
return nil, err
}
if depositContractInfo.RPNetwork != depositContractInfo.BeaconNetwork ||
depositContractInfo.RPDepositContract != depositContractInfo.BeaconDepositContract {
return nil, fmt.Errorf("Beacon network mismatch! Expected %s on chain %d, but beacon is using %s on chain %d.",
depositContractInfo.RPDepositContract.Hex(),
depositContractInfo.RPNetwork,
depositContractInfo.BeaconDepositContract.Hex(),
depositContractInfo.BeaconNetwork)
}
// Get the scrub period
scrubPeriodUnix, err := trustednode.GetPromotionScrubPeriod(rp, nil)
if err != nil {
return nil, err
}
scrubPeriod := time.Duration(scrubPeriodUnix) * time.Second
response.ScrubPeriod = scrubPeriod
// Get transactor
opts, err := w.GetNodeAccountTransactor()
if err != nil {
return nil, err
}
// Get the next minipool address and withdrawal credentials
minipoolAddress, err := minipool.GetExpectedAddress(rp, nodeAccount.Address, salt, nil)
if err != nil {
return nil, err
}
withdrawalCredentials, err := minipool.GetMinipoolWithdrawalCredentials(rp, minipoolAddress, nil)
if err != nil {
return nil, err
}
response.WithdrawalCredentials = withdrawalCredentials
// Check if the pubkey is for an existing active_ongoing validator
validatorStatus, err := bc.GetValidatorStatus(pubkey, nil)
if err != nil {
return nil, fmt.Errorf("error checking status of existing validator: %w", err)
}
if !validatorStatus.Exists {
return nil, fmt.Errorf("validator %s does not exist.", pubkey.Hex())
}
if validatorStatus.Status != beacon.ValidatorState_ActiveOngoing {
return nil, fmt.Errorf("validator %s must be in the active_ongoing state to be migrated, but it is currently in %s.", pubkey.Hex(), string(validatorStatus.Status))
}
if cfg.Smartnode.Network.Value.(cfgtypes.Network) != cfgtypes.Network_Devnet && validatorStatus.WithdrawalCredentials[0] != 0x00 {
return nil, fmt.Errorf("validator %s already has withdrawal credentials [%s], which are not BLS credentials.", pubkey.Hex(), validatorStatus.WithdrawalCredentials.Hex())
}
// Convert the existing balance from gwei to wei
balanceWei := big.NewInt(0).SetUint64(validatorStatus.Balance)
balanceWei.Mul(balanceWei, big.NewInt(1e9))
// Override the provided pending TX if requested
err = eth1.CheckForNonceOverride(c, opts)
if err != nil {
return nil, fmt.Errorf("Error checking for nonce override: %w", err)
}
// Create the minipool
tx, err := node.CreateVacantMinipool(rp, amountWei, minNodeFee, pubkey, salt, minipoolAddress, balanceWei, opts)
if err != nil {
return nil, err
}
response.TxHash = tx.Hash()
response.MinipoolAddress = minipoolAddress
// Return response
return &response, nil
}