/
balance_monitor.go
214 lines (181 loc) · 5.65 KB
/
balance_monitor.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
package services
import (
"context"
"fmt"
"math"
"math/big"
"sync"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/smartcontractkit/chainlink/core/assets"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/service"
"github.com/smartcontractkit/chainlink/core/services/eth"
httypes "github.com/smartcontractkit/chainlink/core/services/headtracker/types"
"github.com/smartcontractkit/chainlink/core/services/keystore"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/utils"
"gorm.io/gorm"
"github.com/ethereum/go-ethereum/common"
gethCommon "github.com/ethereum/go-ethereum/common"
)
type (
// BalanceMonitor checks the balance for each key on every new head
BalanceMonitor interface {
httypes.HeadTrackable
GetEthBalance(gethCommon.Address) *assets.Eth
service.Service
}
balanceMonitor struct {
db *gorm.DB
ethClient eth.Client
ethKeyStore *keystore.Eth
ethBalances map[gethCommon.Address]*assets.Eth
ethBalancesMtx *sync.RWMutex
sleeperTask utils.SleeperTask
}
NullBalanceMonitor struct{}
)
// NewBalanceMonitor returns a new balanceMonitor
func NewBalanceMonitor(db *gorm.DB, ethClient eth.Client, ethKeyStore *keystore.Eth) BalanceMonitor {
bm := &balanceMonitor{
db,
ethClient,
ethKeyStore,
make(map[gethCommon.Address]*assets.Eth),
new(sync.RWMutex),
nil,
}
bm.sleeperTask = utils.NewSleeperTask(&worker{bm: bm})
return bm
}
// Connect complies with HeadTrackable
func (bm *balanceMonitor) Connect(_ *models.Head) error {
return nil
}
func (bm *balanceMonitor) Start() error {
// Always query latest balance on start
bm.checkBalance(nil)
return nil
}
// Close shuts down the BalanceMonitor, should not be used after this
func (bm *balanceMonitor) Close() error {
return bm.sleeperTask.Stop()
}
func (bm *balanceMonitor) Ready() error {
return nil
}
func (bm *balanceMonitor) Healthy() error {
return nil
}
// OnNewLongestChain checks the balance for each key
func (bm *balanceMonitor) OnNewLongestChain(_ context.Context, head models.Head) {
bm.checkBalance(&head)
}
func (bm *balanceMonitor) checkBalance(head *models.Head) {
logger.Debugw("BalanceMonitor: signalling balance worker")
bm.sleeperTask.WakeUp()
}
func (bm *balanceMonitor) updateBalance(ethBal assets.Eth, address gethCommon.Address) {
promUpdateEthBalance(ðBal, address)
bm.ethBalancesMtx.Lock()
oldBal := bm.ethBalances[address]
bm.ethBalances[address] = ðBal
bm.ethBalancesMtx.Unlock()
loggerFields := []interface{}{
"address", address.Hex(),
"ethBalance", ethBal.String(),
"weiBalance", ethBal.ToInt(),
"id", "balance_log",
}
if oldBal == nil {
logger.Infow(fmt.Sprintf("ETH balance for %s: %s", address.Hex(), ethBal.String()), loggerFields...)
return
}
if ethBal.Cmp(oldBal) != 0 {
logger.Infow(fmt.Sprintf("New ETH balance for %s: %s", address.Hex(), ethBal.String()), loggerFields...)
}
}
func (bm *balanceMonitor) GetEthBalance(address gethCommon.Address) *assets.Eth {
bm.ethBalancesMtx.RLock()
defer bm.ethBalancesMtx.RUnlock()
return bm.ethBalances[address]
}
type worker struct {
bm *balanceMonitor
}
func (w *worker) Work() {
keys, err := w.bm.ethKeyStore.SendingKeys()
if err != nil {
logger.Error("BalanceMonitor: error getting keys", err)
}
var wg sync.WaitGroup
wg.Add(len(keys))
for _, key := range keys {
go func(k ethkey.Key) {
w.checkAccountBalance(k)
wg.Done()
}(key)
}
wg.Wait()
}
// Approximately ETH block time
const ethFetchTimeout = 15 * time.Second
func (w *worker) checkAccountBalance(k ethkey.Key) {
ctx, cancel := context.WithTimeout(context.Background(), ethFetchTimeout)
defer cancel()
bal, err := w.bm.ethClient.BalanceAt(ctx, k.Address.Address(), nil)
if err != nil {
logger.Errorw(fmt.Sprintf("BalanceMonitor: error getting balance for key %s", k.Address.Hex()),
"error", err,
"address", k.Address,
)
} else if bal == nil {
logger.Errorw(fmt.Sprintf("BalanceMonitor: error getting balance for key %s: invariant violation, bal may not be nil", k.Address.Hex()),
"error", err,
"address", k.Address,
)
} else {
ethBal := assets.Eth(*bal)
w.bm.updateBalance(ethBal, k.Address.Address())
}
}
func (*NullBalanceMonitor) GetEthBalance(gethCommon.Address) *assets.Eth {
return nil
}
func (*NullBalanceMonitor) Start() error { return nil }
func (*NullBalanceMonitor) Close() error { return nil }
func (*NullBalanceMonitor) Ready() error { return nil }
func (*NullBalanceMonitor) Healthy() error { return nil }
func (*NullBalanceMonitor) Connect(head *models.Head) error {
return nil
}
func (*NullBalanceMonitor) OnNewLongestChain(ctx context.Context, head models.Head) {}
var promETHBalance = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "eth_balance",
Help: "Each Ethereum account's balance",
},
[]string{"account"},
)
func promUpdateEthBalance(balance *assets.Eth, from common.Address) {
balanceFloat, err := ApproximateFloat64(balance)
if err != nil {
logger.Error(fmt.Errorf("updatePrometheusEthBalance: %v", err))
return
}
promETHBalance.WithLabelValues(from.Hex()).Set(balanceFloat)
}
func ApproximateFloat64(e *assets.Eth) (float64, error) {
ef := new(big.Float).SetInt(e.ToInt())
weif := new(big.Float).SetInt(models.WeiPerEth)
bf := new(big.Float).Quo(ef, weif)
f64, _ := bf.Float64()
if f64 == math.Inf(1) || f64 == math.Inf(-1) {
return math.Inf(1), errors.New("assets.Eth.Float64: Could not approximate Eth value into float")
}
return f64, nil
}