/
monitor.go
282 lines (236 loc) · 8.17 KB
/
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
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
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package transaction
import (
"context"
"errors"
"io"
"math/big"
"sync"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/v2/pkg/log"
)
var ErrTransactionCancelled = errors.New("transaction cancelled")
var ErrMonitorClosed = errors.New("monitor closed")
// Monitor is a nonce-based watcher for transaction confirmations.
// Instead of watching transactions individually, the senders nonce is monitored and transactions are checked based on this.
// The idea is that if the nonce is still lower than that of a pending transaction, there is no point in actually checking the transaction for a receipt.
// At the same time if the nonce was already used and this was a few blocks ago we can reasonably assume that it will never confirm.
type Monitor interface {
io.Closer
// WatchTransaction watches the transaction until either there is 1 confirmation or a competing transaction with cancellationDepth confirmations.
WatchTransaction(txHash common.Hash, nonce uint64) (<-chan types.Receipt, <-chan error, error)
}
type transactionMonitor struct {
lock sync.Mutex
ctx context.Context // context which is used for all backend calls
cancelFunc context.CancelFunc // function to cancel the above context
wg sync.WaitGroup
logger log.Logger
backend Backend
sender common.Address // sender of transactions which this instance can monitor
pollingInterval time.Duration // time between checking for new blocks
cancellationDepth uint64 // number of blocks until considering a tx cancellation final
watchesByNonce map[uint64]map[common.Hash][]transactionWatch // active watches grouped by nonce and tx hash
watchAdded chan struct{} // channel to trigger instant pending check
}
type transactionWatch struct {
receiptC chan types.Receipt // channel to which the receipt will be written once available
errC chan error // error channel (primarily for cancelled transactions)
}
func NewMonitor(logger log.Logger, backend Backend, sender common.Address, pollingInterval time.Duration, cancellationDepth uint64) Monitor {
ctx, cancelFunc := context.WithCancel(context.Background())
t := &transactionMonitor{
ctx: ctx,
cancelFunc: cancelFunc,
logger: logger.WithName(loggerName).Register(),
backend: backend,
sender: sender,
pollingInterval: pollingInterval,
cancellationDepth: cancellationDepth,
watchesByNonce: make(map[uint64]map[common.Hash][]transactionWatch),
watchAdded: make(chan struct{}, 1),
}
t.wg.Add(1)
go t.watchPending()
return t
}
func (tm *transactionMonitor) WatchTransaction(txHash common.Hash, nonce uint64) (<-chan types.Receipt, <-chan error, error) {
loggerV1 := tm.logger.V(1).Register()
tm.lock.Lock()
defer tm.lock.Unlock()
// these channels will be written to at most once
// buffer size is 1 to avoid blocking in the watch loop
receiptC := make(chan types.Receipt, 1)
errC := make(chan error, 1)
if _, ok := tm.watchesByNonce[nonce]; !ok {
tm.watchesByNonce[nonce] = make(map[common.Hash][]transactionWatch)
}
tm.watchesByNonce[nonce][txHash] = append(tm.watchesByNonce[nonce][txHash], transactionWatch{
receiptC: receiptC,
errC: errC,
})
select {
case tm.watchAdded <- struct{}{}:
default:
}
loggerV1.Debug("starting to watch transaction", "tx", txHash, "nonce", nonce)
return receiptC, errC, nil
}
// main watch loop
func (tm *transactionMonitor) watchPending() {
loggerV1 := tm.logger.V(1).Register()
defer tm.wg.Done()
defer func() {
tm.lock.Lock()
defer tm.lock.Unlock()
for _, watches := range tm.watchesByNonce {
for _, txMap := range watches {
for _, watch := range txMap {
select {
case watch.errC <- ErrMonitorClosed:
default:
}
}
}
}
}()
var (
lastBlock uint64 = 0
added bool // flag if this iteration was triggered by the watchAdded channel
)
for {
added = false
select {
// if a new watch has been added check again without waiting
case <-tm.watchAdded:
added = true
// otherwise wait
case <-time.After(tm.pollingInterval):
// if the main context is cancelled terminate
case <-tm.ctx.Done():
return
}
// if there are no watched transactions there is nothing to do
if !tm.hasWatches() {
continue
}
// switch to new head subscriptions once websockets are the norm
block, err := tm.backend.BlockNumber(tm.ctx)
if err != nil {
tm.logger.Error(err, "could not get block number")
continue
} else if block <= lastBlock && !added {
// if the block number is not higher than before there is nothing todo
// unless a watch was added in which case we will do the check anyway
// in the rare case where a block was reorged and the new one is the first to contain our tx we wait an extra block
continue
}
if err := tm.checkPending(block); err != nil {
loggerV1.Debug("error while checking pending transactions", "error", err)
continue
}
lastBlock = block
}
}
// potentiallyConfirmedTxWatches returns all watches with nonce less than what was specified
func (tm *transactionMonitor) potentiallyConfirmedTxWatches(nonce uint64) (watches map[uint64]map[common.Hash][]transactionWatch) {
tm.lock.Lock()
defer tm.lock.Unlock()
potentiallyConfirmedTxWatches := make(map[uint64]map[common.Hash][]transactionWatch)
for n, watches := range tm.watchesByNonce {
if n < nonce {
potentiallyConfirmedTxWatches[n] = watches
}
}
return potentiallyConfirmedTxWatches
}
func (tm *transactionMonitor) hasWatches() bool {
tm.lock.Lock()
defer tm.lock.Unlock()
return len(tm.watchesByNonce) > 0
}
// check pending checks the given block (number) for confirmed or cancelled transactions
func (tm *transactionMonitor) checkPending(block uint64) error {
nonce, err := tm.backend.NonceAt(tm.ctx, tm.sender, new(big.Int).SetUint64(block))
if err != nil {
return err
}
// transactions with a nonce lower or equal to what is found on-chain are either confirmed or (at least temporarily) cancelled
potentiallyConfirmedTxWatches := tm.potentiallyConfirmedTxWatches(nonce)
confirmedNonces := make(map[uint64]*types.Receipt)
var cancelledNonces []uint64
for nonceGroup, watchMap := range potentiallyConfirmedTxWatches {
for txHash := range watchMap {
receipt, err := tm.backend.TransactionReceipt(tm.ctx, txHash)
if err != nil {
if errors.Is(err, ethereum.NotFound) {
// if both err and receipt are nil, there is no receipt
// the reason why we consider this only potentially cancelled is to catch cases where after a reorg the original transaction wins
continue
}
return err
}
if receipt != nil {
// if we have a receipt we have a confirmation
confirmedNonces[nonceGroup] = receipt
}
}
}
for nonceGroup := range potentiallyConfirmedTxWatches {
if _, ok := confirmedNonces[nonceGroup]; ok {
continue
}
oldNonce, err := tm.backend.NonceAt(tm.ctx, tm.sender, new(big.Int).SetUint64(block-tm.cancellationDepth))
if err != nil {
return err
}
if nonceGroup < oldNonce {
cancelledNonces = append(cancelledNonces, nonceGroup)
}
}
// notify the subscribers and remove watches for confirmed or cancelled transactions
tm.lock.Lock()
defer tm.lock.Unlock()
for nonce, receipt := range confirmedNonces {
for txHash, watches := range potentiallyConfirmedTxWatches[nonce] {
if receipt.TxHash == txHash {
for _, watch := range watches {
select {
case watch.receiptC <- *receipt:
default:
}
}
} else {
for _, watch := range watches {
select {
case watch.errC <- ErrTransactionCancelled:
default:
}
}
}
}
delete(tm.watchesByNonce, nonce)
}
for _, nonce := range cancelledNonces {
for _, watches := range tm.watchesByNonce[nonce] {
for _, watch := range watches {
select {
case watch.errC <- ErrTransactionCancelled:
default:
}
}
}
delete(tm.watchesByNonce, nonce)
}
return nil
}
func (tm *transactionMonitor) Close() error {
tm.cancelFunc()
tm.wg.Wait()
return nil
}