-
Notifications
You must be signed in to change notification settings - Fork 105
/
reduce-bonds.go
424 lines (360 loc) · 11.8 KB
/
reduce-bonds.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package node
import (
"context"
"fmt"
"math/big"
"time"
"github.com/docker/docker/client"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/rocket-pool/rocketpool-go/minipool"
"github.com/rocket-pool/rocketpool-go/node"
"github.com/rocket-pool/rocketpool-go/rocketpool"
"github.com/rocket-pool/rocketpool-go/types"
"github.com/rocket-pool/rocketpool-go/utils/eth"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
rpstate "github.com/rocket-pool/rocketpool-go/utils/state"
"github.com/rocket-pool/smartnode/shared/services"
"github.com/rocket-pool/smartnode/shared/services/alerting"
"github.com/rocket-pool/smartnode/shared/services/config"
rpgas "github.com/rocket-pool/smartnode/shared/services/gas"
"github.com/rocket-pool/smartnode/shared/services/state"
"github.com/rocket-pool/smartnode/shared/services/wallet"
"github.com/rocket-pool/smartnode/shared/utils/api"
"github.com/rocket-pool/smartnode/shared/utils/log"
)
// Reduce bonds task
type reduceBonds struct {
c *cli.Context
log log.ColorLogger
cfg *config.RocketPoolConfig
w *wallet.Wallet
rp *rocketpool.RocketPool
d *client.Client
gasThreshold float64
disabled bool
maxFee *big.Int
maxPriorityFee *big.Int
gasLimit uint64
}
// Details required to check for bond reduction eligibility
type minipoolBondReductionDetails struct {
Address common.Address
DepositBalance *big.Int
ReduceBondTime time.Time
ReduceBondCancelled bool
Status types.MinipoolStatus
}
// Create reduce bonds task
func newReduceBonds(c *cli.Context, logger log.ColorLogger) (*reduceBonds, error) {
// Get services
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
d, err := services.GetDocker(c)
if err != nil {
return nil, err
}
// Check if auto-bond-reduction is disabled
gasThreshold := cfg.Smartnode.AutoTxGasThreshold.Value.(float64)
disabled := false
if gasThreshold == 0 {
logger.Println("Automatic tx gas threshold is 0, disabling auto-reduce.")
disabled = true
}
// Get the user-requested max fee
maxFeeGwei := cfg.Smartnode.ManualMaxFee.Value.(float64)
var maxFee *big.Int
if maxFeeGwei == 0 {
maxFee = nil
} else {
maxFee = eth.GweiToWei(maxFeeGwei)
}
// Get the user-requested max fee
priorityFeeGwei := cfg.Smartnode.PriorityFee.Value.(float64)
var priorityFee *big.Int
if priorityFeeGwei == 0 {
logger.Println("WARNING: priority fee was missing or 0, setting a default of 2.")
priorityFee = eth.GweiToWei(2)
} else {
priorityFee = eth.GweiToWei(priorityFeeGwei)
}
// Return task
return &reduceBonds{
c: c,
log: logger,
cfg: cfg,
w: w,
rp: rp,
d: d,
gasThreshold: gasThreshold,
disabled: disabled,
maxFee: maxFee,
maxPriorityFee: priorityFee,
gasLimit: 0,
}, nil
}
// Reduce bonds
func (t *reduceBonds) run(state *state.NetworkState) error {
// Check if auto-reduce is disabled
if t.disabled {
return nil
}
// Log
t.log.Println("Checking for minipool bonds to reduce...")
// Get the latest state
opts := &bind.CallOpts{
BlockNumber: big.NewInt(0).SetUint64(state.ElBlockNumber),
}
// Get node account
nodeAccount, err := t.w.GetNodeAccount()
if err != nil {
return err
}
// Get the bond reduction details
windowStart := state.NetworkDetails.BondReductionWindowStart
windowLength := state.NetworkDetails.BondReductionWindowLength
// Get the time of the latest block
latestEth1Block, err := t.rp.Client.HeaderByNumber(context.Background(), opts.BlockNumber)
if err != nil {
return fmt.Errorf("can't get the latest block time: %w", err)
}
latestBlockTime := time.Unix(int64(latestEth1Block.Time), 0)
// Get reduceable minipools
minipools, err := t.getReduceableMinipools(nodeAccount.Address, windowStart, windowLength, latestBlockTime, state, opts)
if err != nil {
return err
}
if len(minipools) == 0 {
return nil
}
// Log
t.log.Printlnf("%d minipool(s) are ready for bond reduction...", len(minipools))
// Workaround for the fee distribution issue
success, err := t.forceFeeDistribution()
if err != nil {
return err
}
if !success {
return nil
}
// Reduce bonds
successCount := 0
for _, mp := range minipools {
success, err := t.reduceBond(mp, windowStart, windowLength, latestBlockTime, opts)
alerting.AlertMinipoolBondReduced(t.cfg, mp.MinipoolAddress, err == nil)
if err != nil {
t.log.Println(fmt.Errorf("could not reduce bond for minipool %s: %w", mp.MinipoolAddress.Hex(), err))
return err
}
if success {
successCount++
}
}
// Return
return nil
}
// Temp mitigation for the
func (t *reduceBonds) forceFeeDistribution() (bool, error) {
// Get node account
nodeAccount, err := t.w.GetNodeAccount()
if err != nil {
return false, err
}
// Get fee distributor
distributorAddress, err := node.GetDistributorAddress(t.rp, nodeAccount.Address, nil)
if err != nil {
return false, err
}
distributor, err := node.NewDistributor(t.rp, distributorAddress, nil)
if err != nil {
return false, err
}
// Sync
var wg errgroup.Group
var balanceRaw *big.Int
var nodeShare float64
// Get the contract's balance
wg.Go(func() error {
var err error
balanceRaw, err = t.rp.Client.BalanceAt(context.Background(), distributorAddress, nil)
return err
})
// Get the node share of the balance
wg.Go(func() error {
nodeShareRaw, err := distributor.GetNodeShare(nil)
if err != nil {
return fmt.Errorf("error getting node share for distributor %s: %w", distributorAddress.Hex(), err)
}
nodeShare = eth.WeiToEth(nodeShareRaw)
return nil
})
// Wait for data
if err := wg.Wait(); err != nil {
return false, err
}
balance := eth.WeiToEth(balanceRaw)
if balance == 0 {
t.log.Println("Your fee distributor does not have any ETH and does not need to be distributed.")
return true, nil
}
t.log.Println("NOTE: prior to bond reduction, you must distribute the funds in your fee distributor.")
// Print info
rEthShare := balance - nodeShare
t.log.Printlnf("Your fee distributor's balance of %.6f ETH will be distributed as follows:\n", balance)
t.log.Printlnf("\tYour withdrawal address will receive %.6f ETH.", nodeShare)
t.log.Printlnf("\trETH pool stakers will receive %.6f ETH.\n", rEthShare)
opts, err := t.w.GetNodeAccountTransactor()
if err != nil {
return false, err
}
// Get the gas limit
gasInfo, err := distributor.EstimateDistributeGas(opts)
if err != nil {
return false, fmt.Errorf("could not estimate the gas required to distribute node fees: %w", err)
}
var gas *big.Int
if t.gasLimit != 0 {
gas = new(big.Int).SetUint64(t.gasLimit)
} else {
gas = new(big.Int).SetUint64(gasInfo.SafeGasLimit)
}
// Get the max fee
maxFee := t.maxFee
if maxFee == nil || maxFee.Uint64() == 0 {
maxFee, err = rpgas.GetHeadlessMaxFeeWei()
if err != nil {
return false, err
}
}
// Print the gas info
if !api.PrintAndCheckGasInfo(gasInfo, true, t.gasThreshold, &t.log, maxFee, t.gasLimit) {
return false, nil
}
opts.GasFeeCap = maxFee
opts.GasTipCap = t.maxPriorityFee
opts.GasLimit = gas.Uint64()
// Distribute
fmt.Printf("Distributing rewards...\n")
hash, err := distributor.Distribute(opts)
if err != nil {
return false, err
}
// Print TX info and wait for it to be included in a block
err = api.PrintAndWaitForTransaction(t.cfg, hash, t.rp.Client, &t.log)
if err != nil {
return false, err
}
// Log & return
fmt.Println("Successfully distributed your fee distributor's balance. Your rewards should arrive in your withdrawal address shortly.")
return true, nil
}
// Get reduceable minipools
func (t *reduceBonds) getReduceableMinipools(nodeAddress common.Address, windowStart time.Duration, windowLength time.Duration, latestBlockTime time.Time, state *state.NetworkState, opts *bind.CallOpts) ([]*rpstate.NativeMinipoolDetails, error) {
// Filter minipools
reduceableMinipools := []*rpstate.NativeMinipoolDetails{}
for _, mpd := range state.MinipoolDetailsByNode[nodeAddress] {
// TEMP
reduceBondTime, err := minipool.GetReduceBondTime(t.rp, mpd.MinipoolAddress, opts)
if err != nil {
return nil, fmt.Errorf("error getting reduce bond time for minipool %s: %w", mpd.MinipoolAddress.Hex(), err)
}
reduceBondCancelled, err := minipool.GetReduceBondCancelled(t.rp, mpd.MinipoolAddress, opts)
if err != nil {
return nil, fmt.Errorf("error getting reduce bond cancelled for minipool %s: %w", mpd.MinipoolAddress.Hex(), err)
}
depositBalance := eth.WeiToEth(mpd.NodeDepositBalance)
timeSinceReductionStart := latestBlockTime.Sub(reduceBondTime)
if depositBalance == 16 &&
timeSinceReductionStart < (windowStart+windowLength) &&
!reduceBondCancelled &&
mpd.Status == types.Staking {
if timeSinceReductionStart > windowStart {
reduceableMinipools = append(reduceableMinipools, mpd)
} else {
remainingTime := windowStart - timeSinceReductionStart
t.log.Printlnf("Minipool %s has %s left until it can have its bond reduced.", mpd.MinipoolAddress.Hex(), remainingTime)
}
}
}
// Return
return reduceableMinipools, nil
}
// Reduce a minipool's bond
func (t *reduceBonds) reduceBond(mpd *rpstate.NativeMinipoolDetails, windowStart time.Duration, windowLength time.Duration, latestBlockTime time.Time, callOpts *bind.CallOpts) (bool, error) {
// Log
t.log.Printlnf("Reducing bond for minipool %s...", mpd.MinipoolAddress.Hex())
// Get transactor
opts, err := t.w.GetNodeAccountTransactor()
if err != nil {
return false, err
}
// Make the minipool binding
mpBinding, err := minipool.NewMinipoolFromVersion(t.rp, mpd.MinipoolAddress, mpd.Version, callOpts)
if err != nil {
return false, fmt.Errorf("error creating minipool binding for %s: %w", mpd.MinipoolAddress.Hex(), err)
}
// Get the updated minipool interface
mpv3, success := minipool.GetMinipoolAsV3(mpBinding)
if !success {
return false, fmt.Errorf("cannot reduce bond for minipool %s because its delegate version is too low (v%d); please update the delegate", mpBinding.GetAddress().Hex(), mpBinding.GetVersion())
}
// Get the gas limit
gasInfo, err := mpv3.EstimateReduceBondAmountGas(opts)
if err != nil {
return false, fmt.Errorf("could not estimate the gas required to reduce bond: %w", err)
}
var gas *big.Int
if t.gasLimit != 0 {
gas = new(big.Int).SetUint64(t.gasLimit)
} else {
gas = new(big.Int).SetUint64(gasInfo.SafeGasLimit)
}
// Get the max fee
maxFee := t.maxFee
if maxFee == nil || maxFee.Uint64() == 0 {
maxFee, err = rpgas.GetHeadlessMaxFeeWei()
if err != nil {
return false, err
}
}
// TEMP
reduceBondTime, err := minipool.GetReduceBondTime(t.rp, mpd.MinipoolAddress, callOpts)
if err != nil {
return false, fmt.Errorf("error getting reduce bond time for minipool %s: %w", mpd.MinipoolAddress.Hex(), err)
}
// Print the gas info
if !api.PrintAndCheckGasInfo(gasInfo, true, t.gasThreshold, &t.log, maxFee, t.gasLimit) {
timeSinceReductionStart := latestBlockTime.Sub(reduceBondTime)
remainingTime := (windowStart + windowLength) - timeSinceReductionStart
t.log.Printlnf("Time until bond reduction times out: %s", remainingTime)
return false, nil
}
opts.GasFeeCap = maxFee
opts.GasTipCap = t.maxPriorityFee
opts.GasLimit = gas.Uint64()
// Reduce bond
hash, err := mpv3.ReduceBondAmount(opts)
if err != nil {
return false, err
}
// Print TX info and wait for it to be included in a block
err = api.PrintAndWaitForTransaction(t.cfg, hash, t.rp.Client, &t.log)
if err != nil {
return false, err
}
// Log
t.log.Printlnf("Successfully reduced bond for minipool %s.", mpd.MinipoolAddress.Hex())
// Return
return true, nil
}