-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
head_tracker.go
300 lines (259 loc) · 7.24 KB
/
head_tracker.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
package services
import (
"fmt"
"sync"
"time"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/logger"
strpkg "github.com/smartcontractkit/chainlink/core/store"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/presenters"
"github.com/smartcontractkit/chainlink/core/utils"
)
// HeadTracker holds and stores the latest block number experienced by this particular node
// in a thread safe manner. Reconstitutes the last block number from the data
// store on reboot.
type HeadTracker struct {
callbacks []strpkg.HeadTrackable
headers chan models.BlockHeader
headSubscription models.EthSubscription
store *strpkg.Store
head *models.Head
headMutex sync.RWMutex
connected bool
sleeper utils.Sleeper
done chan struct{}
started bool
listenForNewHeadsWg sync.WaitGroup
subscriptionSucceeded chan struct{}
}
// NewHeadTracker instantiates a new HeadTracker using the orm to persist new block numbers.
// Can be passed in an optional sleeper object that will dictate how often
// it tries to reconnect.
func NewHeadTracker(store *strpkg.Store, callbacks []strpkg.HeadTrackable, sleepers ...utils.Sleeper) *HeadTracker {
var sleeper utils.Sleeper
if len(sleepers) > 0 {
sleeper = sleepers[0]
} else {
sleeper = utils.NewBackoffSleeper()
}
return &HeadTracker{
store: store,
callbacks: callbacks,
sleeper: sleeper,
}
}
// Start retrieves the last persisted block number from the HeadTracker,
// subscribes to new heads, and if successful fires Connect on the
// HeadTrackable argument.
func (ht *HeadTracker) Start() error {
ht.headMutex.Lock()
defer ht.headMutex.Unlock()
if ht.started {
return nil
}
if err := ht.updateHeadFromDb(); err != nil {
return err
}
number := ht.head
if number != nil {
logger.Debug("Tracking logs from last block ", presenters.FriendlyBigInt(number.ToInt()), " with hash ", number.Hash.Hex())
}
ht.done = make(chan struct{})
ht.subscriptionSucceeded = make(chan struct{})
ht.listenForNewHeadsWg.Add(1)
go ht.listenForNewHeads()
ht.started = true
return nil
}
// Stop unsubscribes all connections and fires Disconnect.
func (ht *HeadTracker) Stop() error {
ht.headMutex.Lock()
if !ht.started {
ht.headMutex.Unlock()
return nil
}
if ht.connected {
ht.connected = false
ht.disconnect()
}
close(ht.done)
close(ht.subscriptionSucceeded)
ht.started = false
ht.headMutex.Unlock()
ht.listenForNewHeadsWg.Wait()
return nil
}
// Save updates the latest block number, if indeed the latest, and persists
// this number in case of reboot. Thread safe.
func (ht *HeadTracker) Save(n *models.Head) error {
if n == nil {
return errors.New("Cannot save a nil block header")
}
ht.headMutex.Lock()
if n.GreaterThan(ht.head) {
copy := *n
ht.head = ©
ht.headMutex.Unlock()
} else {
ht.headMutex.Unlock()
msg := fmt.Sprintf("Cannot save new head confirmation %v because it's equal to or less than current head %v with hash %s", n, ht.head, n.Hash.Hex())
return errBlockNotLater{msg}
}
return ht.store.CreateHead(n)
}
// Head returns the latest block header being tracked, or nil.
func (ht *HeadTracker) Head() *models.Head {
ht.headMutex.RLock()
defer ht.headMutex.RUnlock()
return ht.head
}
// Connected returns whether or not this HeadTracker is connected.
func (ht *HeadTracker) Connected() bool {
ht.headMutex.RLock()
defer ht.headMutex.RUnlock()
return ht.connected
}
func (ht *HeadTracker) connect(bn *models.Head) {
for _, trackable := range ht.callbacks {
logger.WarnIf(trackable.Connect(bn))
}
}
func (ht *HeadTracker) disconnect() {
for _, trackable := range ht.callbacks {
trackable.Disconnect()
}
}
func (ht *HeadTracker) onNewHead(head *models.Head) {
ht.headMutex.Lock()
defer ht.headMutex.Unlock()
for _, trackable := range ht.callbacks {
trackable.OnNewHead(head)
}
}
func (ht *HeadTracker) listenForNewHeads() {
defer ht.listenForNewHeadsWg.Done()
defer ht.unsubscribeFromHead()
for {
if !ht.subscribe() {
return
}
if err := ht.receiveHeaders(); err != nil {
logger.Errorw(fmt.Sprintf("Error in new head subscription, unsubscribed: %s", err.Error()), "err", err)
continue
} else {
return
}
}
}
// subscribe periodically attempts to connect to the ethereum node via websocket.
// It returns true on success, and false if cut short by a done request and did not connect.
func (ht *HeadTracker) subscribe() bool {
ht.sleeper.Reset()
for {
ht.unsubscribeFromHead()
logger.Info("Connecting to node ", ht.store.Config.EthereumURL(), " in ", ht.sleeper.Duration())
select {
case <-ht.done:
return false
case <-time.After(ht.sleeper.After()):
err := ht.subscribeToHead()
if err != nil {
logger.Warnw(fmt.Sprintf("Failed to connect to ethereum node %v", ht.store.Config.EthereumURL()), "err", err)
} else {
logger.Info("Connected to ethereum node ", ht.store.Config.EthereumURL())
return true
}
}
}
}
func (ht *HeadTracker) receiveHeaders() error {
for {
select {
case <-ht.done:
return nil
case block, open := <-ht.headers:
if !open {
return errors.New("HeadTracker headers prematurely closed")
}
head := block.ToHead()
logger.Debugw(
fmt.Sprintf("Received new head %v", presenters.FriendlyBigInt(head.ToInt())),
"blockHeight", head.ToInt(),
"blockHash", block.Hash(),
"hash", head.Hash)
if err := ht.Save(head); err != nil {
switch err.(type) {
case errBlockNotLater:
logger.Warn(err)
default:
logger.Error(err)
}
} else {
ht.onNewHead(head)
}
case err, open := <-ht.headSubscription.Err():
if open && err != nil {
return err
}
}
}
}
func (ht *HeadTracker) subscribeToHead() error {
ht.headMutex.Lock()
defer ht.headMutex.Unlock()
ht.headers = make(chan models.BlockHeader)
sub, err := ht.store.TxManager.SubscribeToNewHeads(ht.headers)
if err != nil {
return errors.Wrap(err, "TxManager#SubscribeToNewHeads")
}
if err := verifyEthereumChainID(ht); err != nil {
return errors.Wrap(err, "verifyEthereumChainID")
}
ht.headSubscription = sub
ht.connected = true
ht.connect(ht.head)
return nil
}
func (ht *HeadTracker) unsubscribeFromHead() error {
ht.headMutex.Lock()
defer ht.headMutex.Unlock()
if !ht.connected {
return nil
}
timedUnsubscribe(ht.headSubscription)
ht.connected = false
ht.disconnect()
close(ht.headers)
return nil
}
func (ht *HeadTracker) updateHeadFromDb() error {
number, err := ht.store.LastHead()
if err != nil {
return err
}
ht.head = number
return nil
}
type errBlockNotLater struct {
message string
}
func (e errBlockNotLater) Error() string {
return e.message
}
// chainIDVerify checks whether or not the ChainID from the Chainlink config
// matches the ChainID reported by the ETH node connected to this Chainlink node.
func verifyEthereumChainID(ht *HeadTracker) error {
ethereumChainID, err := ht.store.TxManager.GetChainID()
if err != nil {
return err
}
if ethereumChainID.Cmp(ht.store.Config.ChainID()) != 0 {
return fmt.Errorf(
"Ethereum ChainID doesn't match chainlink config.ChainID: config ID=%d, eth RPC ID=%d",
ht.store.Config.ChainID(),
ethereumChainID,
)
}
return nil
}