/
tracker.go
336 lines (295 loc) · 10.1 KB
/
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
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
package txmgr
import (
"context"
"fmt"
"sync"
"time"
"github.com/O1MaGnUmO1/chainlink-common/pkg/logger"
"github.com/O1MaGnUmO1/chainlink-common/pkg/services"
"github.com/O1MaGnUmO1/chainlink-common/pkg/utils/mailbox"
feetypes "github.com/O1MaGnUmO1/erinaceus-vrf/common/fee/types"
txmgrtypes "github.com/O1MaGnUmO1/erinaceus-vrf/common/txmgr/types"
"github.com/O1MaGnUmO1/erinaceus-vrf/common/types"
)
const (
// defaultTTL is the default time to live for abandoned transactions
// After this TTL, the TXM stops tracking abandoned Txs.
defaultTTL = 6 * time.Hour
// handleTxesTimeout represents a sanity limit on how long handleTxesByState
// should take to complete
handleTxesTimeout = 10 * time.Minute
)
// AbandonedTx is a transaction who's 'FromAddress' was removed from the KeyStore(by the Node Operator).
// Thus, any new attempts for this Tx can't be signed/created. This means no fee bumping can be done.
// However, the Tx may still have live attempts in the chain's mempool, and could get confirmed on the
// chain as-is. Thus, the TXM should not directly discard this Tx.
type AbandonedTx[ADDR types.Hashable] struct {
id int64
fromAddress ADDR
}
// Tracker tracks all transactions which have abandoned fromAddresses.
// The fromAddresses can be deleted by Node Operators from the KeyStore. In such cases,
// existing in-flight transactions for these fromAddresses are considered abandoned too.
// Since such Txs can still have attempts on chain's mempool, these could still be confirmed.
// This tracker just tracks such Txs for some time, in case they get confirmed as-is.
type Tracker[
CHAIN_ID types.ID,
ADDR types.Hashable,
TX_HASH types.Hashable,
BLOCK_HASH types.Hashable,
R txmgrtypes.ChainReceipt[TX_HASH, BLOCK_HASH],
SEQ types.Sequence,
FEE feetypes.Fee,
] struct {
services.StateMachine
txStore txmgrtypes.TxStore[ADDR, CHAIN_ID, TX_HASH, BLOCK_HASH, R, SEQ, FEE]
keyStore txmgrtypes.KeyStore[ADDR, CHAIN_ID, SEQ]
chainID CHAIN_ID
lggr logger.Logger
enabledAddrs map[ADDR]bool
txCache map[int64]AbandonedTx[ADDR]
ttl time.Duration
lock sync.Mutex
mb *mailbox.Mailbox[int64]
wg sync.WaitGroup
isStarted bool
ctx context.Context
ctxCancel context.CancelFunc
}
func NewTracker[
CHAIN_ID types.ID,
ADDR types.Hashable,
TX_HASH types.Hashable,
BLOCK_HASH types.Hashable,
R txmgrtypes.ChainReceipt[TX_HASH, BLOCK_HASH],
SEQ types.Sequence,
FEE feetypes.Fee,
](
txStore txmgrtypes.TxStore[ADDR, CHAIN_ID, TX_HASH, BLOCK_HASH, R, SEQ, FEE],
keyStore txmgrtypes.KeyStore[ADDR, CHAIN_ID, SEQ],
chainID CHAIN_ID,
lggr logger.Logger,
) *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE] {
return &Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]{
txStore: txStore,
keyStore: keyStore,
chainID: chainID,
lggr: logger.Named(lggr, "TxMgrTracker"),
enabledAddrs: map[ADDR]bool{},
txCache: map[int64]AbandonedTx[ADDR]{},
ttl: defaultTTL,
mb: mailbox.NewSingle[int64](),
lock: sync.Mutex{},
wg: sync.WaitGroup{},
}
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) Start(_ context.Context) (err error) {
tr.lock.Lock()
defer tr.lock.Unlock()
return tr.StartOnce("Tracker", func() error {
return tr.startInternal()
})
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) startInternal() (err error) {
tr.ctx, tr.ctxCancel = context.WithCancel(context.Background())
if err := tr.setEnabledAddresses(); err != nil {
return fmt.Errorf("failed to set enabled addresses: %w", err)
}
if err := tr.trackAbandonedTxes(tr.ctx); err != nil {
return fmt.Errorf("failed to track abandoned txes: %w", err)
}
tr.isStarted = true
if len(tr.txCache) == 0 {
tr.lggr.Infow("no abandoned txes found, skipping runLoop")
return nil
}
tr.wg.Add(1)
go tr.runLoop()
return nil
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) Close() error {
tr.lock.Lock()
defer tr.lock.Unlock()
return tr.StopOnce("Tracker", func() error {
return tr.closeInternal()
})
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) closeInternal() error {
tr.lggr.Infow("stopping tracker")
if !tr.isStarted {
return fmt.Errorf("tracker not started")
}
tr.ctxCancel()
tr.wg.Wait()
tr.isStarted = false
return nil
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) runLoop() {
defer tr.wg.Done()
ttlExceeded := time.NewTicker(tr.ttl)
defer ttlExceeded.Stop()
for {
select {
case <-tr.mb.Notify():
for {
if tr.ctx.Err() != nil {
return
}
blockHeight, exists := tr.mb.Retrieve()
if !exists {
break
}
if err := tr.HandleTxesByState(tr.ctx, blockHeight); err != nil {
tr.lggr.Errorw(fmt.Errorf("failed to handle txes by state: %w", err).Error())
}
}
case <-ttlExceeded.C:
tr.lggr.Infow("ttl exceeded")
tr.MarkAllTxesFatal(tr.ctx)
return
case <-tr.ctx.Done():
return
}
}
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) GetAbandonedAddresses() []ADDR {
tr.lock.Lock()
defer tr.lock.Unlock()
if !tr.isStarted {
return []ADDR{}
}
abandonedAddrs := make([]ADDR, len(tr.txCache))
for _, atx := range tr.txCache {
abandonedAddrs = append(abandonedAddrs, atx.fromAddress)
}
return abandonedAddrs
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) IsStarted() bool {
tr.lock.Lock()
defer tr.lock.Unlock()
return tr.isStarted
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) setEnabledAddresses() error {
enabledAddrs, err := tr.keyStore.EnabledAddressesForChain(tr.chainID)
if err != nil {
return fmt.Errorf("failed to get enabled addresses for chain: %w", err)
}
if len(enabledAddrs) == 0 {
tr.lggr.Warnf("enabled address list is empty")
}
for _, addr := range enabledAddrs {
tr.enabledAddrs[addr] = true
}
return nil
}
// trackAbandonedTxes called once to find and insert all abandoned txes into the tracker.
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) trackAbandonedTxes(ctx context.Context) (err error) {
if tr.isStarted {
return fmt.Errorf("tracker already started")
}
nonFatalTxes, err := tr.txStore.GetNonFatalTransactions(ctx, tr.chainID)
if err != nil {
return fmt.Errorf("failed to get non fatal txes from txStore: %w", err)
}
// insert abandoned txes
for _, tx := range nonFatalTxes {
if !tr.enabledAddrs[tx.FromAddress] {
tr.insertTx(tx)
}
}
if err := tr.handleTxesByState(ctx, 0); err != nil {
return fmt.Errorf("failed to handle txes by state: %w", err)
}
return nil
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) HandleTxesByState(ctx context.Context, blockHeight int64) error {
tr.lock.Lock()
defer tr.lock.Unlock()
tr.ctx, tr.ctxCancel = context.WithTimeout(ctx, handleTxesTimeout)
defer tr.ctxCancel()
return tr.handleTxesByState(ctx, blockHeight)
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) handleTxesByState(ctx context.Context, blockHeight int64) error {
for id, atx := range tr.txCache {
tx, err := tr.txStore.GetTxByID(ctx, atx.id)
if err != nil {
return fmt.Errorf("failed to get tx by ID: %w", err)
}
switch tx.State {
case TxConfirmed:
if err := tr.handleConfirmedTx(tx, blockHeight); err != nil {
return fmt.Errorf("failed to handle confirmed txes: %w", err)
}
case TxConfirmedMissingReceipt, TxUnconfirmed:
// Keep tracking tx
case TxInProgress, TxUnstarted:
// Tx could never be sent on chain even once. That means that we need to sign
// an attempt to even broadcast this Tx to the chain. Since the fromAddress
// is deleted, we can't sign it.
errMsg := "The FromAddress for this Tx was deleted before this Tx could be broadcast to the chain."
if err := tr.markTxFatal(ctx, tx, errMsg); err != nil {
return fmt.Errorf("failed to mark tx as fatal: %w", err)
}
delete(tr.txCache, id)
case TxFatalError:
delete(tr.txCache, id)
default:
tr.lggr.Errorw(fmt.Sprintf("unhandled transaction state: %v", tx.State))
}
}
return nil
}
// handleConfirmedTx removes a transaction from the tracker if it's been finalized on chain
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) handleConfirmedTx(
tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE],
blockHeight int64,
) error {
finalized, err := tr.txStore.IsTxFinalized(tr.ctx, blockHeight, tx.ID, tr.chainID)
if err != nil {
return fmt.Errorf("failed to check if tx is finalized: %w", err)
}
if finalized {
delete(tr.txCache, tx.ID)
}
return nil
}
// insertTx inserts a transaction into the tracker as an AbandonedTx
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) insertTx(
tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) {
if _, contains := tr.txCache[tx.ID]; contains {
return
}
tr.txCache[tx.ID] = AbandonedTx[ADDR]{
id: tx.ID,
fromAddress: tx.FromAddress,
}
tr.lggr.Debugw(fmt.Sprintf("inserted tx %v", tx.ID))
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) markTxFatal(ctx context.Context,
tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE],
errMsg string) error {
tx.Error.SetValid(errMsg)
// Set state to TxInProgress so the tracker can attempt to mark it as fatal
tx.State = TxInProgress
if err := tr.txStore.UpdateTxFatalError(ctx, tx); err != nil {
return fmt.Errorf("failed to mark tx %v as abandoned: %w", tx.ID, err)
}
return nil
}
func (tr *Tracker[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) MarkAllTxesFatal(ctx context.Context) {
tr.lock.Lock()
defer tr.lock.Unlock()
errMsg := fmt.Sprintf(
"fromAddress for this Tx was deleted, and existing attempts onchain didn't finalize within %d hours, thus this Tx was abandoned.",
int(tr.ttl.Hours()))
for _, atx := range tr.txCache {
tx, err := tr.txStore.GetTxByID(ctx, atx.id)
if err != nil {
tr.lggr.Errorw(fmt.Errorf("failed to get tx by ID: %w", err).Error())
continue
}
if err := tr.markTxFatal(ctx, tx, errMsg); err != nil {
tr.lggr.Errorw(fmt.Errorf("failed to mark tx as abandoned: %w", err).Error())
}
}
}