/
node.go
326 lines (282 loc) · 12.2 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
package state
import (
"context"
"fmt"
"math/big"
"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/rocketpool-go/v2/core"
"github.com/rocket-pool/rocketpool-go/v2/node"
"github.com/rocket-pool/rocketpool-go/v2/rocketpool"
"github.com/rocket-pool/rocketpool-go/v2/types"
"golang.org/x/sync/errgroup"
)
const (
legacyNodeBatchSize int = 100
nodeAddressBatchSize int = 1000
)
// Complete details for a node
type NativeNodeDetails struct {
Exists bool
RegistrationTime *big.Int
TimezoneLocation string
FeeDistributorInitialised bool
FeeDistributorAddress common.Address
RewardNetwork *big.Int
RplStake *big.Int
EffectiveRPLStake *big.Int
MinimumRPLStake *big.Int
MaximumRPLStake *big.Int
EthMatched *big.Int
EthMatchedLimit *big.Int
MinipoolCount *big.Int
BalanceETH *big.Int
BalanceRETH *big.Int
BalanceRPL *big.Int
BalanceOldRPL *big.Int
DepositCreditBalance *big.Int
DistributorBalanceUserETH *big.Int // Must call CalculateAverageFeeAndDistributorShares to get this
DistributorBalanceNodeETH *big.Int // Must call CalculateAverageFeeAndDistributorShares to get this
PrimaryWithdrawalAddress common.Address
PendingPrimaryWithdrawalAddress common.Address
SmoothingPoolRegistrationState bool
SmoothingPoolRegistrationChanged *big.Int
NodeAddress common.Address
AverageNodeFee *big.Int // Must call CalculateAverageFeeAndDistributorShares to get this
CollateralisationRatio *big.Int
DistributorBalance *big.Int
IsRplWithdrawalAddressSet bool
RplWithdrawalAddress common.Address
PendingRplWithdrawalAddress common.Address
}
// Gets the details for a node using the efficient multicall contract
func GetNativeNodeDetails(rp *rocketpool.RocketPool, contracts *NetworkContracts, nodeAddress common.Address) (NativeNodeDetails, error) {
opts := &bind.CallOpts{
BlockNumber: contracts.ElBlockNumber,
}
details := NativeNodeDetails{
NodeAddress: nodeAddress,
AverageNodeFee: big.NewInt(0),
CollateralisationRatio: big.NewInt(0),
DistributorBalanceUserETH: big.NewInt(0),
DistributorBalanceNodeETH: big.NewInt(0),
}
mc, err := batch.NewMultiCaller(rp.Client, contracts.MulticallerAddress)
if err != nil {
return NativeNodeDetails{}, fmt.Errorf("error creating multicaller: %w", err)
}
addNodeDetailsCalls(contracts, mc, &details, nodeAddress)
_, err = mc.FlexibleCall(true, opts)
if err != nil {
return NativeNodeDetails{}, fmt.Errorf("error executing multicall: %w", err)
}
// Get the node's ETH balance
details.BalanceETH, err = rp.Client.BalanceAt(context.Background(), nodeAddress, opts.BlockNumber)
if err != nil {
return NativeNodeDetails{}, err
}
// Get the distributor balance
distributorBalance, err := rp.Client.BalanceAt(context.Background(), details.FeeDistributorAddress, opts.BlockNumber)
if err != nil {
return NativeNodeDetails{}, err
}
// Do some postprocessing on the node data
details.DistributorBalance = distributorBalance
// Fix the effective stake
if details.EffectiveRPLStake.Cmp(details.MinimumRPLStake) == -1 {
details.EffectiveRPLStake.SetUint64(0)
}
return details, nil
}
// Gets the details for all nodes using the efficient multicall contract
func GetAllNativeNodeDetails(rp *rocketpool.RocketPool, contracts *NetworkContracts) ([]NativeNodeDetails, error) {
opts := &bind.CallOpts{
BlockNumber: contracts.ElBlockNumber,
}
// Get the list of node addresses
addresses, err := getNodeAddressesFast(rp, contracts, opts)
if err != nil {
return nil, fmt.Errorf("error getting node addresses: %w", err)
}
count := len(addresses)
nodeDetails := make([]NativeNodeDetails, count)
// Sync
var wg errgroup.Group
wg.SetLimit(threadLimit)
// Run the getters in batches
for i := 0; i < count; i += legacyNodeBatchSize {
i := i
max := i + legacyNodeBatchSize
if max > count {
max = count
}
wg.Go(func() error {
var err error
mc, err := batch.NewMultiCaller(rp.Client, contracts.MulticallerAddress)
if err != nil {
return err
}
for j := i; j < max; j++ {
address := addresses[j]
details := &nodeDetails[j]
details.NodeAddress = address
details.AverageNodeFee = big.NewInt(0)
details.DistributorBalanceUserETH = big.NewInt(0)
details.DistributorBalanceNodeETH = big.NewInt(0)
details.CollateralisationRatio = big.NewInt(0)
addNodeDetailsCalls(contracts, mc, details, address)
}
_, err = mc.FlexibleCall(true, opts)
if err != nil {
return fmt.Errorf("error executing multicall: %w", err)
}
return nil
})
}
if err := wg.Wait(); err != nil {
return nil, fmt.Errorf("error getting node details: %w", err)
}
// Get the balances of the nodes
distributorAddresses := make([]common.Address, count)
balances, err := contracts.BalanceBatcher.GetEthBalances(addresses, opts)
if err != nil {
return nil, fmt.Errorf("error getting node balances: %w", err)
}
for i, details := range nodeDetails {
nodeDetails[i].BalanceETH = balances[i]
distributorAddresses[i] = details.FeeDistributorAddress
}
// Get the balances of the distributors
balances, err = contracts.BalanceBatcher.GetEthBalances(distributorAddresses, opts)
if err != nil {
return nil, fmt.Errorf("error getting distributor balances: %w", err)
}
// Do some postprocessing on the node data
for i := range nodeDetails {
details := &nodeDetails[i]
details.DistributorBalance = balances[i]
// Fix the effective stake
if details.EffectiveRPLStake.Cmp(details.MinimumRPLStake) == -1 {
details.EffectiveRPLStake.SetUint64(0)
}
}
return nodeDetails, nil
}
// Calculate the average node fee and user/node shares of the distributor's balance
func CalculateAverageFeeAndDistributorShares(rp *rocketpool.RocketPool, contracts *NetworkContracts, node NativeNodeDetails, minipoolDetails []*NativeMinipoolDetails) {
// Calculate the total of all fees for staking minipools that aren't finalized
totalFee := big.NewInt(0)
eligibleMinipools := int64(0)
for _, mpd := range minipoolDetails {
if mpd.Status == types.MinipoolStatus_Staking && !mpd.Finalised {
totalFee.Add(totalFee, mpd.NodeFee)
eligibleMinipools++
}
}
// Get the average fee (0 if there aren't any minipools)
if eligibleMinipools > 0 {
node.AverageNodeFee.Div(totalFee, big.NewInt(eligibleMinipools))
}
// Get the user and node portions of the distributor balance
distributorBalance := big.NewInt(0).Set(node.DistributorBalance)
if distributorBalance.Cmp(big.NewInt(0)) > 0 {
nodeBalance := big.NewInt(0)
nodeBalance.Mul(distributorBalance, big.NewInt(1e18))
nodeBalance.Div(nodeBalance, node.CollateralisationRatio)
userBalance := big.NewInt(0)
userBalance.Sub(distributorBalance, nodeBalance)
if eligibleMinipools == 0 {
// Split it based solely on the collateralisation ratio if there are no minipools (and hence no average fee)
node.DistributorBalanceNodeETH = big.NewInt(0).Set(nodeBalance)
node.DistributorBalanceUserETH = big.NewInt(0).Sub(distributorBalance, nodeBalance)
} else {
// Amount of ETH given to the NO as a commission
commissionEth := big.NewInt(0)
commissionEth.Mul(userBalance, node.AverageNodeFee)
commissionEth.Div(commissionEth, big.NewInt(1e18))
node.DistributorBalanceNodeETH.Add(nodeBalance, commissionEth) // Node gets their portion + commission on user portion
node.DistributorBalanceUserETH.Sub(distributorBalance, node.DistributorBalanceNodeETH) // User gets balance - node share
}
} else {
// No distributor balance
node.DistributorBalanceNodeETH = big.NewInt(0)
node.DistributorBalanceUserETH = big.NewInt(0)
}
return
}
// Get all node addresses using the multicaller
func getNodeAddressesFast(rp *rocketpool.RocketPool, contracts *NetworkContracts, opts *bind.CallOpts) ([]common.Address, error) {
nodeMgr, err := node.NewNodeManager(rp)
if err != nil {
return nil, fmt.Errorf("error getting node manager: %w", err)
}
// Get node count
err = rp.Query(nil, opts, nodeMgr.NodeCount)
if err != nil {
return []common.Address{}, err
}
nodeCount := nodeMgr.NodeCount.Formatted()
// Sync
var wg errgroup.Group
wg.SetLimit(threadLimit)
addresses := make([]common.Address, nodeCount)
// Run the getters in batches
count := int(nodeCount)
for i := 0; i < count; i += nodeAddressBatchSize {
i := i
max := i + nodeAddressBatchSize
if max > count {
max = count
}
wg.Go(func() error {
var err error
mc, err := batch.NewMultiCaller(rp.Client, contracts.MulticallerAddress)
if err != nil {
return err
}
for j := i; j < max; j++ {
core.AddCall(mc, contracts.RocketNodeManager, &addresses[j], "getNodeAt", big.NewInt(int64(j)))
}
_, err = mc.FlexibleCall(true, opts)
if err != nil {
return fmt.Errorf("error executing multicall: %w", err)
}
return nil
})
}
if err := wg.Wait(); err != nil {
return nil, fmt.Errorf("error getting node addresses: %w", err)
}
return addresses, nil
}
// Add all of the calls for the node details to the multicaller
func addNodeDetailsCalls(contracts *NetworkContracts, mc *batch.MultiCaller, details *NativeNodeDetails, address common.Address) {
core.AddCall(mc, contracts.RocketNodeManager, &details.Exists, "getNodeExists", address)
core.AddCall(mc, contracts.RocketNodeManager, &details.RegistrationTime, "getNodeRegistrationTime", address)
core.AddCall(mc, contracts.RocketNodeManager, &details.TimezoneLocation, "getNodeTimezoneLocation", address)
core.AddCall(mc, contracts.RocketNodeManager, &details.FeeDistributorInitialised, "getFeeDistributorInitialised", address)
core.AddCall(mc, contracts.RocketNodeDistributorFactory, &details.FeeDistributorAddress, "getProxyAddress", address)
core.AddCall(mc, contracts.RocketNodeManager, &details.RewardNetwork, "getRewardNetwork", address)
core.AddCall(mc, contracts.RocketNodeStaking, &details.RplStake, "getNodeRPLStake", address)
core.AddCall(mc, contracts.RocketNodeStaking, &details.EffectiveRPLStake, "getNodeEffectiveRPLStake", address)
core.AddCall(mc, contracts.RocketNodeStaking, &details.MinimumRPLStake, "getNodeMinimumRPLStake", address)
core.AddCall(mc, contracts.RocketNodeStaking, &details.MaximumRPLStake, "getNodeMaximumRPLStake", address)
core.AddCall(mc, contracts.RocketNodeStaking, &details.EthMatched, "getNodeETHMatched", address)
core.AddCall(mc, contracts.RocketNodeStaking, &details.EthMatchedLimit, "getNodeETHMatchedLimit", address)
core.AddCall(mc, contracts.RocketMinipoolManager, &details.MinipoolCount, "getNodeMinipoolCount", address)
core.AddCall(mc, contracts.RocketTokenRETH, &details.BalanceRETH, "balanceOf", address)
core.AddCall(mc, contracts.RocketTokenRPL, &details.BalanceRPL, "balanceOf", address)
core.AddCall(mc, contracts.RocketTokenRPLFixedSupply, &details.BalanceOldRPL, "balanceOf", address)
core.AddCall(mc, contracts.RocketStorage, &details.PrimaryWithdrawalAddress, "getNodeWithdrawalAddress", address)
core.AddCall(mc, contracts.RocketStorage, &details.PendingPrimaryWithdrawalAddress, "getNodePendingWithdrawalAddress", address)
core.AddCall(mc, contracts.RocketNodeManager, &details.SmoothingPoolRegistrationState, "getSmoothingPoolRegistrationState", address)
core.AddCall(mc, contracts.RocketNodeManager, &details.SmoothingPoolRegistrationChanged, "getSmoothingPoolRegistrationChanged", address)
// Atlas
core.AddCall(mc, contracts.RocketNodeDeposit, &details.DepositCreditBalance, "getNodeDepositCredit", address)
core.AddCall(mc, contracts.RocketNodeStaking, &details.CollateralisationRatio, "getNodeETHCollateralisationRatio", address)
// Houston
core.AddCall(mc, contracts.RocketNodeManager, &details.IsRplWithdrawalAddressSet, "getNodeRPLWithdrawalAddressIsSet", address)
core.AddCall(mc, contracts.RocketNodeManager, &details.RplWithdrawalAddress, "getNodeRPLWithdrawalAddress", address)
core.AddCall(mc, contracts.RocketNodeManager, &details.PendingRplWithdrawalAddress, "getNodePendingRPLWithdrawalAddress", address)
}