/
node.go
181 lines (156 loc) · 5.38 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
package rp
import (
"bytes"
"context"
"fmt"
"math/big"
"time"
"github.com/Seb369888/poolsea-go/minipool"
"github.com/Seb369888/poolsea-go/node"
"github.com/Seb369888/poolsea-go/rocketpool"
tnsettings "github.com/Seb369888/poolsea-go/settings/trustednode"
"github.com/Seb369888/poolsea-go/types"
"github.com/Seb369888/smartnode/shared/services/beacon"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/sync/errgroup"
)
func GetNodeValidatorIndices(rp *rocketpool.RocketPool, ec rocketpool.ExecutionClient, bc beacon.Client, nodeAddress common.Address) ([]uint64, error) {
// Get current block number so all subsequent queries are done at same point in time
blockNumber, err := ec.BlockNumber(context.Background())
if err != nil {
return nil, fmt.Errorf("Error getting block number: %w", err)
}
// Setup call opts
blockNumberBig := big.NewInt(0).SetUint64(blockNumber)
callOpts := bind.CallOpts{BlockNumber: blockNumberBig}
// Get list of pubkeys for this given node
pubkeys, err := minipool.GetNodeValidatingMinipoolPubkeys(rp, nodeAddress, &callOpts)
if err != nil {
return nil, err
}
// Remove zero pubkeys
zeroPubkey := types.ValidatorPubkey{}
filteredPubkeys := []types.ValidatorPubkey{}
for _, pubkey := range pubkeys {
if !bytes.Equal(pubkey[:], zeroPubkey[:]) {
filteredPubkeys = append(filteredPubkeys, pubkey)
}
}
pubkeys = filteredPubkeys
// Get validator statuses by pubkeys
statuses, err := bc.GetValidatorStatuses(pubkeys, nil)
if err != nil {
return nil, fmt.Errorf("Error getting validator statuses: %w", err)
}
// Enumerate validators statuses and fill indices array
validatorIndices := make([]uint64, len(statuses)+1)
i := 0
for _, status := range statuses {
validatorIndices[i] = status.Index
i++
}
return validatorIndices, nil
}
// Checks the given node's current matched ETH, its limit on matched ETH, and how much ETH is preparing to be matched by pending bond reductions
func CheckCollateral(rp *rocketpool.RocketPool, nodeAddress common.Address, opts *bind.CallOpts) (ethMatched *big.Int, ethMatchedLimit *big.Int, pendingMatchAmount *big.Int, err error) {
// Get the node's minipool addresses
addresses, err := minipool.GetNodeMinipoolAddresses(rp, nodeAddress, opts)
if err != nil {
err = fmt.Errorf("error getting minipool addresses for node %s: %w", nodeAddress.Hex(), err)
return
}
latestBlockHeader, err := rp.Client.HeaderByNumber(context.Background(), nil)
if err != nil {
return nil, nil, nil, fmt.Errorf("error getting latest block header: %w", err)
}
blockTime := time.Unix(int64(latestBlockHeader.Time), 0)
var reductionWindowStart uint64
var reductionWindowLength uint64
// Data
var wg1 errgroup.Group
wg1.Go(func() error {
var err error
reductionWindowStart, err = tnsettings.GetBondReductionWindowStart(rp, nil)
return err
})
wg1.Go(func() error {
var err error
reductionWindowLength, err = tnsettings.GetBondReductionWindowLength(rp, nil)
return err
})
// Wait for data
if err = wg1.Wait(); err != nil {
return nil, nil, nil, err
}
reductionWindowEnd := time.Duration(reductionWindowStart+reductionWindowLength) * time.Second
// Data
var wg errgroup.Group
deltas := make([]*big.Int, len(addresses))
zeroTime := time.Unix(0, 0)
wg.Go(func() error {
var err error
ethMatched, err = node.GetNodeEthMatched(rp, nodeAddress, opts)
if err != nil {
return fmt.Errorf("error getting node's matched ETH amount: %w", err)
}
return nil
})
wg.Go(func() error {
var err error
ethMatchedLimit, err = node.GetNodeEthMatchedLimit(rp, nodeAddress, opts)
if err != nil {
return fmt.Errorf("error getting how much ETH the node is able to borrow: %w", err)
}
return nil
})
for i, address := range addresses {
i := i
address := address
wg.Go(func() error {
reduceBondTime, err := minipool.GetReduceBondTime(rp, address, opts)
if err != nil {
return fmt.Errorf("error getting bond reduction time for minipool %s: %w", address.Hex(), err)
}
reduceBondCancelled, err := minipool.GetReduceBondCancelled(rp, address, nil)
if err != nil {
return fmt.Errorf("error getting bond reduction cancel status for minipool %s: %w", address.Hex(), err)
}
// Ignore minipools that don't have a bond reduction pending
timeSinceReductionStart := blockTime.Sub(reduceBondTime)
if reduceBondTime == zeroTime ||
reduceBondCancelled ||
timeSinceReductionStart > reductionWindowEnd {
deltas[i] = big.NewInt(0)
return nil
}
// Get the old and new (pending) bonds
mp, err := minipool.NewMinipool(rp, address, opts)
if err != nil {
return fmt.Errorf("error creating binding for minipool %s: %w", address.Hex(), err)
}
oldBond, err := mp.GetNodeDepositBalance(opts)
if err != nil {
return fmt.Errorf("error getting node deposit balance for minipool %s: %w", address.Hex(), err)
}
newBond, err := minipool.GetReduceBondValue(rp, address, opts)
if err != nil {
return fmt.Errorf("error getting pending bond reduced balance for minipool %s: %w", address.Hex(), err)
}
// Delta = old - new
deltas[i] = big.NewInt(0).Sub(oldBond, newBond)
return nil
})
}
// Wait for data
if err = wg.Wait(); err != nil {
return
}
// Get the total pending match
totalDelta := big.NewInt(0)
for _, delta := range deltas {
totalDelta.Add(totalDelta, delta)
}
pendingMatchAmount = totalDelta
return
}