-
Notifications
You must be signed in to change notification settings - Fork 22
/
node.go
461 lines (366 loc) · 20.7 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package node
import (
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
batch "github.com/rocket-pool/batch-query"
"github.com/rocket-pool/node-manager-core/beacon"
"github.com/rocket-pool/node-manager-core/eth"
"github.com/rocket-pool/rocketpool-go/v2/core"
"github.com/rocket-pool/rocketpool-go/v2/rocketpool"
)
// ===============
// === Structs ===
// ===============
// Binding for a Rocket Pool Node
type Node struct {
// The address of this node
Address common.Address
// True if the node exists (i.e. there is a registered node at this address)
Exists *core.SimpleField[bool]
// The time that the node was registered with the network
RegistrationTime *core.FormattedUint256Field[time.Time]
// The node's timezone location
TimezoneLocation *core.SimpleField[string]
// The network ID for the node's rewards
RewardNetwork *core.FormattedUint256Field[uint64]
// The node's average minipool fee (commission)
AverageFee *core.FormattedUint256Field[float64]
// The node's smoothing pool opt-in status
SmoothingPoolRegistrationState *core.SimpleField[bool]
// The time of the node's last smoothing pool opt-in / opt-out
SmoothingPoolRegistrationChanged *core.FormattedUint256Field[time.Time]
// True if the node's fee distributor has been initialized yet
IsFeeDistributorInitialized *core.SimpleField[bool]
// The node's fee distributor address
DistributorAddress *core.SimpleField[common.Address]
// The amount of ETH in the node's deposit credit bank
Credit *core.SimpleField[*big.Int]
// The amount of ETH in the node's deposit credit bank that is currently usable for new deposits
UsableCredit *core.SimpleField[*big.Int]
// The current donated ETH balance for the node
DonatedEthBalance *core.SimpleField[*big.Int]
// The sum of the node's credit and donated balances
TotalCreditAndDonatedBalance *core.SimpleField[*big.Int]
// How much of the combined credit and donated balances can be used for new deposits
UsableCreditAndDonatedBalance *core.SimpleField[*big.Int]
// The node's RPL stake
RplStake *core.SimpleField[*big.Int]
// The node's effective RPL stake
EffectiveRplStake *core.SimpleField[*big.Int]
// The node's minimum RPL stake to collateralize its minipools
MinimumRplStake *core.SimpleField[*big.Int]
// The node's maximum RPL stake to collateralize its minipools
MaximumRplStake *core.SimpleField[*big.Int]
// The time the node last staked RPL
RplStakedTime *core.FormattedUint256Field[time.Time]
// The amount of ETH the node has borrowed from the deposit pool to create its minipools
EthMatched *core.SimpleField[*big.Int]
// The amount of ETH the node can still borrow from the deposit pool to create any new minipools
EthMatchedLimit *core.SimpleField[*big.Int]
// The number of minipools owned by the node count
MinipoolCount *core.FormattedUint256Field[uint64]
// The number of minipools owned by the node that are not finalised
ActiveMinipoolCount *core.FormattedUint256Field[uint64]
// The number of minipools owned by a node that are finalised
FinalisedMinipoolCount *core.FormattedUint256Field[uint64]
// The number of minipools owned by a node that are validating
ValidatingMinipoolCount *core.FormattedUint256Field[uint64]
// The node's primary withdrawal address
PrimaryWithdrawalAddress *core.SimpleField[common.Address]
// The node's pending primary withdrawal address
PendingPrimaryWithdrawalAddress *core.SimpleField[common.Address]
// The node's RPL withdrawal address
IsRplWithdrawalAddressSet *core.SimpleField[bool]
// The node's RPL withdrawal address
RplWithdrawalAddress *core.SimpleField[common.Address]
// The node's pending RPL withdrawal address
PendingRplWithdrawalAddress *core.SimpleField[common.Address]
// The amount of RPL locked as part of active PDAO proposals or challenges
RplLocked *core.SimpleField[*big.Int]
// True if RPL the node can have its RPL locked via protocol DAO proposals / challenges
IsRplLockingAllowed *core.SimpleField[bool]
// The address that the provided node has currently delegated voting power to
CurrentVotingDelegate *core.SimpleField[common.Address]
// Whether or not on-chain voting has been initialized for the node
IsVotingInitialized *core.SimpleField[bool]
// === Internal fields ===
rp *rocketpool.RocketPool
distFactory *core.Contract
networkVoting *core.Contract
nodeDeposit *core.Contract
nodeMgr *core.Contract
nodeStaking *core.Contract
mpFactory *core.Contract
mpMgr *core.Contract
storage *core.Contract
txMgr *eth.TransactionManager
}
// ====================
// === Constructors ===
// ====================
// Creates a new Node instance
func NewNode(rp *rocketpool.RocketPool, address common.Address) (*Node, error) {
distFactory, err := rp.GetContract(rocketpool.ContractName_RocketNodeDistributorFactory)
if err != nil {
return nil, fmt.Errorf("error getting distributor factory binding: %w", err)
}
networkVoting, err := rp.GetContract(rocketpool.ContractName_RocketNetworkVoting)
if err != nil {
return nil, fmt.Errorf("error getting network voting binding: %w", err)
}
nodeDeposit, err := rp.GetContract(rocketpool.ContractName_RocketNodeDeposit)
if err != nil {
return nil, fmt.Errorf("error getting node deposit binding: %w", err)
}
nodeManager, err := rp.GetContract(rocketpool.ContractName_RocketNodeManager)
if err != nil {
return nil, fmt.Errorf("error getting node staking binding: %w", err)
}
nodeStaking, err := rp.GetContract(rocketpool.ContractName_RocketNodeStaking)
if err != nil {
return nil, fmt.Errorf("error getting node staking binding: %w", err)
}
minipoolFactory, err := rp.GetContract(rocketpool.ContractName_RocketMinipoolFactory)
if err != nil {
return nil, fmt.Errorf("error getting minipool factory binding: %w", err)
}
minipoolManager, err := rp.GetContract(rocketpool.ContractName_RocketMinipoolManager)
if err != nil {
return nil, fmt.Errorf("error getting minipool manager binding: %w", err)
}
return &Node{
Address: address,
// DistributorFactory
DistributorAddress: core.NewSimpleField[common.Address](distFactory, "getProxyAddress", address),
// NetworkVoting
CurrentVotingDelegate: core.NewSimpleField[common.Address](networkVoting, "getCurrentDelegate", address),
IsVotingInitialized: core.NewSimpleField[bool](networkVoting, "getVotingInitialised", address),
// NodeDeposit
Credit: core.NewSimpleField[*big.Int](nodeDeposit, "getNodeDepositCredit", address),
UsableCredit: core.NewSimpleField[*big.Int](nodeDeposit, "getNodeUsableCredit", address),
DonatedEthBalance: core.NewSimpleField[*big.Int](nodeDeposit, "getNodeEthBalance", address),
TotalCreditAndDonatedBalance: core.NewSimpleField[*big.Int](nodeDeposit, "getNodeCreditAndBalance", address),
UsableCreditAndDonatedBalance: core.NewSimpleField[*big.Int](nodeDeposit, "getNodeUsableCreditAndBalance", address),
// NodeManager
Exists: core.NewSimpleField[bool](nodeManager, "getNodeExists", address),
RegistrationTime: core.NewFormattedUint256Field[time.Time](nodeManager, "getNodeRegistrationTime", address),
TimezoneLocation: core.NewSimpleField[string](nodeManager, "getNodeTimezoneLocation", address),
RewardNetwork: core.NewFormattedUint256Field[uint64](nodeManager, "getRewardNetwork", address),
IsFeeDistributorInitialized: core.NewSimpleField[bool](nodeManager, "getFeeDistributorInitialised", address),
AverageFee: core.NewFormattedUint256Field[float64](nodeManager, "getAverageNodeFee", address),
SmoothingPoolRegistrationState: core.NewSimpleField[bool](nodeManager, "getSmoothingPoolRegistrationState", address),
SmoothingPoolRegistrationChanged: core.NewFormattedUint256Field[time.Time](nodeManager, "getSmoothingPoolRegistrationChanged", address),
IsRplWithdrawalAddressSet: core.NewSimpleField[bool](nodeManager, "getNodeRPLWithdrawalAddressIsSet", address),
RplWithdrawalAddress: core.NewSimpleField[common.Address](nodeManager, "getNodeRPLWithdrawalAddress", address),
PendingRplWithdrawalAddress: core.NewSimpleField[common.Address](nodeManager, "getNodePendingRPLWithdrawalAddress", address),
// NodeStaking
RplStake: core.NewSimpleField[*big.Int](nodeStaking, "getNodeRPLStake", address),
EffectiveRplStake: core.NewSimpleField[*big.Int](nodeStaking, "getNodeEffectiveRPLStake", address),
MinimumRplStake: core.NewSimpleField[*big.Int](nodeStaking, "getNodeMinimumRPLStake", address),
MaximumRplStake: core.NewSimpleField[*big.Int](nodeStaking, "getNodeMaximumRPLStake", address),
RplStakedTime: core.NewFormattedUint256Field[time.Time](nodeStaking, "getNodeRPLStakedTime", address),
EthMatched: core.NewSimpleField[*big.Int](nodeStaking, "getNodeETHMatched", address),
EthMatchedLimit: core.NewSimpleField[*big.Int](nodeStaking, "getNodeETHMatchedLimit", address),
RplLocked: core.NewSimpleField[*big.Int](nodeStaking, "getNodeRPLLocked", address),
IsRplLockingAllowed: core.NewSimpleField[bool](nodeStaking, "getRPLLockingAllowed", address),
// MinipoolManager
MinipoolCount: core.NewFormattedUint256Field[uint64](minipoolManager, "getNodeMinipoolCount", address),
ActiveMinipoolCount: core.NewFormattedUint256Field[uint64](minipoolManager, "getNodeActiveMinipoolCount", address),
FinalisedMinipoolCount: core.NewFormattedUint256Field[uint64](minipoolManager, "getNodeFinalisedMinipoolCount", address),
ValidatingMinipoolCount: core.NewFormattedUint256Field[uint64](minipoolManager, "getNodeValidatingMinipoolCount", address),
// Storage
PrimaryWithdrawalAddress: core.NewSimpleField[common.Address](rp.Storage.Contract, "getNodeWithdrawalAddress", address),
PendingPrimaryWithdrawalAddress: core.NewSimpleField[common.Address](rp.Storage.Contract, "getNodePendingWithdrawalAddress", address),
rp: rp,
distFactory: distFactory,
networkVoting: networkVoting,
nodeDeposit: nodeDeposit,
nodeMgr: nodeManager,
nodeStaking: nodeStaking,
mpFactory: minipoolFactory,
mpMgr: minipoolManager,
storage: rp.Storage.Contract,
txMgr: rp.GetTransactionManager(),
}, nil
}
// =============
// === Calls ===
// =============
// Get the address that the node has delegated voting power to at the given block
func (c *Node) GetVotingDelegateAtBlock(mc *batch.MultiCaller, delegate_Out *common.Address, blockNumber uint32) {
core.AddCall(mc, c.networkVoting, delegate_Out, "getDelegate", c.Address, blockNumber)
}
// Get the voting power of the given node at the provided block
func (c *Node) GetVotingPowerAtBlock(mc *batch.MultiCaller, power_Out **big.Int, blockNumber uint32) {
core.AddCall(mc, c.networkVoting, power_Out, "getVotingPower", c.Address, blockNumber)
}
// ====================
// === Transactions ===
// ====================
// === NetworkVoting ===
// Get info for initializing on-chain voting for the node
func (c *Node) InitializeVoting(opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.networkVoting.Contract, "initialiseVoting", opts)
}
// Get info for initializing on-chain voting for the node, specifying an optional delegate along with it
func (c *Node) InitializeVotingWithDelegate(delegate common.Address, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.networkVoting.Contract, "initialiseVotingWithDelegate", opts, delegate)
}
// Get info for setting the voting delegate for the node
func (c *Node) SetVotingDelegate(newDelegate common.Address, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.networkVoting.Contract, "setDelegate", opts, newDelegate)
}
// === NodeDeposit ===
// Get info for making a node deposit and creating a new minipool
func (c *Node) Deposit(bondAmount *big.Int, minimumNodeFee float64, validatorPubkey beacon.ValidatorPubkey, validatorSignature beacon.ValidatorSignature, depositDataRoot common.Hash, salt *big.Int, expectedMinipoolAddress common.Address, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeDeposit.Contract, "deposit", opts, bondAmount, eth.EthToWei(minimumNodeFee), validatorPubkey[:], validatorSignature[:], depositDataRoot, salt, expectedMinipoolAddress)
}
// Get info for making a node deposit and creating a new minipool by using the credit balance
func (c *Node) DepositWithCredit(bondAmount *big.Int, minimumNodeFee float64, validatorPubkey beacon.ValidatorPubkey, validatorSignature beacon.ValidatorSignature, depositDataRoot common.Hash, salt *big.Int, expectedMinipoolAddress common.Address, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeDeposit.Contract, "depositWithCredit", opts, bondAmount, eth.EthToWei(minimumNodeFee), validatorPubkey[:], validatorSignature[:], depositDataRoot, salt, expectedMinipoolAddress)
}
// Get info for making a vacant minipool for solo staker migration
func (c *Node) CreateVacantMinipool(bondAmount *big.Int, minimumNodeFee float64, validatorPubkey beacon.ValidatorPubkey, salt *big.Int, expectedMinipoolAddress common.Address, currentBalance *big.Int, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeDeposit.Contract, "createVacantMinipool", opts, bondAmount, eth.EthToWei(minimumNodeFee), validatorPubkey[:], salt, expectedMinipoolAddress, currentBalance)
}
// Withdraw unused ETH that was donated (staked on behalf of the node)
func (c *Node) WithdrawDonatedEth(amount *big.Int, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeDeposit.Contract, "withdrawEth", opts, c.Address, amount)
}
// === NodeManager ===
// Get info for registering a node
func (c *Node) Register(timezoneLocation string, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
_, err := time.LoadLocation(timezoneLocation)
if err != nil {
return nil, fmt.Errorf("error verifying timezone [%s]: %w", timezoneLocation, err)
}
return c.txMgr.CreateTransactionInfo(c.nodeMgr.Contract, "registerNode", opts, timezoneLocation)
}
// Get info for setting a node's timezone location
func (c *Node) SetTimezoneLocation(timezoneLocation string, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
_, err := time.LoadLocation(timezoneLocation)
if err != nil {
return nil, fmt.Errorf("error verifying timezone [%s]: %w", timezoneLocation, err)
}
return c.txMgr.CreateTransactionInfo(c.nodeMgr.Contract, "setTimezoneLocation", opts, timezoneLocation)
}
// Get info for initializing (creating) the node's fee distributor
func (c *Node) InitializeFeeDistributor(opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeMgr.Contract, "initialiseFeeDistributor", opts)
}
// Get info for opting in or out of the smoothing pool
func (c *Node) SetSmoothingPoolRegistrationState(optIn bool, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeMgr.Contract, "setSmoothingPoolRegistrationState", opts, optIn)
}
// Get info for setting the RPL withdrawal address
func (c *Node) SetRplWithdrawalAddress(withdrawalAddress common.Address, confirm bool, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeMgr.Contract, "setRPLWithdrawalAddress", opts, c.Address, withdrawalAddress, confirm)
}
// Get info for confirming the RPL withdrawal address
func (c *Node) ConfirmRplWithdrawalAddress(opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeMgr.Contract, "confirmRPLWithdrawalAddress", opts, c.Address)
}
// === NodeStaking ===
// Get info for staking RPL
func (c *Node) StakeRpl(rplAmount *big.Int, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeStaking.Contract, "stakeRPL", opts, rplAmount)
}
// Get info for adding or removing an address from the stake-RPL-on-behalf allowlist
func (c *Node) SetStakeRplForAllowed(caller common.Address, allowed bool, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeStaking.Contract, "setStakeRPLForAllowed", opts, caller, allowed)
}
// Get info for withdrawing staked RPL
func (c *Node) WithdrawRpl(rplAmount *big.Int, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeStaking.Contract, "withdrawRPL", opts, c.Address, rplAmount)
}
// Get info for enabling or disabling RPL locking (for Protocol DAO votes or challenges)
func (c *Node) SetRplLockingAllowed(allowed bool, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.nodeStaking.Contract, "setRPLLockingAllowed", opts, c.Address, allowed)
}
// === Storage ===
// Get info for setting the node's primary withdrawal address
func (c *Node) SetPrimaryWithdrawalAddress(withdrawalAddress common.Address, confirm bool, opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.storage.Contract, "setWithdrawalAddress", opts, c.Address, withdrawalAddress, confirm)
}
// Get info for confirming the node's primary withdrawal address
func (c *Node) ConfirmPrimaryWithdrawalAddress(opts *bind.TransactOpts) (*eth.TransactionInfo, error) {
return c.txMgr.CreateTransactionInfo(c.storage.Contract, "confirmWithdrawalAddress", opts, c.Address)
}
// ===================
// === Sub-Getters ===
// ===================
// === DistributorFactory ===
// Get a node's distributor with details
func (c *Node) GetNodeDistributor(distributorAddress common.Address, includeDetails bool, opts *bind.CallOpts) (*NodeDistributor, error) {
// Create the distributor
distributor, err := NewNodeDistributor(c.rp, c.Address, distributorAddress)
if err != nil {
return nil, fmt.Errorf("error creating node distributor binding for node %s at %s: %w", c.Address, distributorAddress.Hex(), err)
}
// Get details via a multicall query
if includeDetails {
err = c.rp.Query(func(mc *batch.MultiCaller) error {
eth.QueryAllFields(distributor, mc)
return nil
}, opts)
if err != nil {
return nil, fmt.Errorf("error getting node distributor for node %s at %s: %w", c.Address, distributorAddress.Hex(), err)
}
}
// Return
return distributor, nil
}
// === MinipoolManager ===
// Get one of the node's minipool addresses by index
func (c *Node) GetMinipoolAddress(mc *batch.MultiCaller, address_Out *common.Address, index uint64) {
core.AddCall(mc, c.mpMgr, address_Out, "getNodeMinipoolAt", c.Address, big.NewInt(int64(index)))
}
// Get one of the node's validating minipool addresses by index
func (c *Node) GetValidatingMinipoolAddress(mc *batch.MultiCaller, address_Out *common.Address, index uint64) {
core.AddCall(mc, c.mpMgr, address_Out, "getNodeValidatingMinipoolAt", c.Address, big.NewInt(int64(index)))
}
// Get all of the node's minipool addresses in a standalone call.
// This will use an internal batched multicall invocation to retrieve all of them.
// Provide the value returned from GetMinipoolCount() in minipoolCount.
func (c *Node) GetMinipoolAddresses(minipoolCount uint64, opts *bind.CallOpts) ([]common.Address, error) {
addresses := make([]common.Address, minipoolCount)
// Run the multicall query for each address
err := c.rp.BatchQuery(int(minipoolCount), c.rp.AddressBatchSize,
func(mc *batch.MultiCaller, index int) error {
c.GetMinipoolAddress(mc, &addresses[index], uint64(index))
return nil
}, opts)
if err != nil {
return nil, fmt.Errorf("error getting minipool addresses: %w", err)
}
// Return
return addresses, nil
}
// Get all of the node's validating minipool addresses in a standalone call.
// This will use an internal batched multicall invocation to retrieve all of them.
// Provide the value returned from GetValidatingMinipoolCount() in minipoolCount.
func (c *Node) GetValidatingMinipoolAddresses(minipoolCount uint64, opts *bind.CallOpts) ([]common.Address, error) {
addresses := make([]common.Address, minipoolCount)
// Run the multicall query for each address
err := c.rp.BatchQuery(int(minipoolCount), c.rp.AddressBatchSize,
func(mc *batch.MultiCaller, index int) error {
c.GetValidatingMinipoolAddress(mc, &addresses[index], uint64(index))
return nil
}, opts)
if err != nil {
return nil, fmt.Errorf("error getting minipool addresses: %w", err)
}
// Return
return addresses, nil
}
// =============
// === Utils ===
// =============
// === MinipoolFactory ===
// Get the address of a minipool based on the node's address and a salt
func (c *Node) GetExpectedMinipoolAddress(mc *batch.MultiCaller, address_Out *common.Address, salt *big.Int) {
core.AddCall(mc, c.mpFactory, address_Out, "getExpectedAddress", c.Address, salt)
}