This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
/
mempool.go
447 lines (420 loc) · 11.6 KB
/
mempool.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package core
import (
"fmt"
"sync"
"time"
"github.com/pkg/errors"
abci "github.com/tendermint/classic/abci/types"
cmn "github.com/tendermint/classic/libs/common"
"github.com/tendermint/classic/libs/events"
ctypes "github.com/tendermint/classic/rpc/core/types"
rpctypes "github.com/tendermint/classic/rpc/lib/types"
"github.com/tendermint/classic/types"
)
//-----------------------------------------------------------------------------
// NOTE: tx should be signed, but this is only checked at the app level (not by Tendermint!)
// Returns right away, with no response. Does not wait for CheckTx nor
// DeliverTx results.
//
// If you want to be sure that the transaction is included in a block, you can
// subscribe for the result using JSONRPC via a websocket. See
// https://tendermint.com/docs/app-dev/subscribing-to-events-via-websocket.html
// If you haven't received anything after a couple of blocks, resend it. If the
// same happens again, send it to some other node. A few reasons why it could
// happen:
//
// 1. malicious node can drop or pretend it had committed your tx
// 2. malicious proposer (not necessary the one you're communicating with) can
// drop transactions, which might become valid in the future
// (https://github.com/tendermint/classic/issues/3322)
// 3. node can be offline
//
// Please refer to
// https://tendermint.com/docs/tendermint-core/using-tendermint.html#formatting
// for formatting/encoding rules.
//
//
// ```shell
// curl 'localhost:26657/broadcast_tx_async?tx="123"'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// result, err := client.BroadcastTxAsync("123")
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "error": "",
// "result": {
// "hash": "E39AAB7A537ABAA237831742DCE1117F187C3C52",
// "log": "",
// "data": "",
// "code": "0"
// },
// "id": "",
// "jsonrpc": "2.0"
// }
// ```
//
// ### Query Parameters
//
// | Parameter | Type | Default | Required | Description |
// |-----------+------+---------+----------+-----------------|
// | tx | Tx | nil | true | The transaction |
func BroadcastTxAsync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
err := mempool.CheckTx(tx, nil)
if err != nil {
return nil, err
}
return &ctypes.ResultBroadcastTx{Hash: tx.Hash()}, nil
}
// Returns with the response from CheckTx. Does not wait for DeliverTx result.
//
// If you want to be sure that the transaction is included in a block, you can
// subscribe for the result using JSONRPC via a websocket. See
// https://tendermint.com/docs/app-dev/subscribing-to-events-via-websocket.html
// If you haven't received anything after a couple of blocks, resend it. If the
// same happens again, send it to some other node. A few reasons why it could
// happen:
//
// 1. malicious node can drop or pretend it had committed your tx
// 2. malicious proposer (not necessary the one you're communicating with) can
// drop transactions, which might become valid in the future
// (https://github.com/tendermint/classic/issues/3322)
//
// Please refer to
// https://tendermint.com/docs/tendermint-core/using-tendermint.html#formatting
// for formatting/encoding rules.
//
// ```shell
// curl 'localhost:26657/broadcast_tx_sync?tx="456"'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// result, err := client.BroadcastTxSync("456")
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "jsonrpc": "2.0",
// "id": "",
// "result": {
// "code": "0",
// "data": "",
// "log": "",
// "hash": "0D33F2F03A5234F38706E43004489E061AC40A2E"
// },
// "error": ""
// }
// ```
//
// ### Query Parameters
//
// | Parameter | Type | Default | Required | Description |
// |-----------+------+---------+----------+-----------------|
// | tx | Tx | nil | true | The transaction |
func BroadcastTxSync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
resCh := make(chan abci.Response, 1)
err := mempool.CheckTx(tx, func(res abci.Response) {
resCh <- res
})
if err != nil {
return nil, err
}
res := <-resCh
r := res.(abci.ResponseCheckTx)
return &ctypes.ResultBroadcastTx{
Error: r.Error,
Data: r.Data,
Log: r.Log,
Hash: tx.Hash(),
}, nil
}
// Returns with the responses from CheckTx and DeliverTx.
//
// IMPORTANT: use only for testing and development. In production, use
// BroadcastTxSync or BroadcastTxAsync. You can subscribe for the transaction
// result using JSONRPC via a websocket. See
// https://tendermint.com/docs/app-dev/subscribing-to-events-via-websocket.html
//
// CONTRACT: only returns error if mempool.CheckTx() errs or if we timeout
// waiting for tx to commit.
//
// If CheckTx or DeliverTx fail, no error will be returned, but the returned result
// will contain a non-OK ABCI code.
//
// Please refer to
// https://tendermint.com/docs/tendermint-core/using-tendermint.html#formatting
// for formatting/encoding rules.
//
//
// ```shell
// curl 'localhost:26657/broadcast_tx_commit?tx="789"'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// result, err := client.BroadcastTxCommit("789")
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "error": "",
// "result": {
// "height": "26682",
// "hash": "75CA0F856A4DA078FC4911580360E70CEFB2EBEE",
// "deliver_tx": {
// "log": "",
// "data": "",
// "code": "0"
// },
// "check_tx": {
// "log": "",
// "data": "",
// "code": "0"
// }
// },
// "id": "",
// "jsonrpc": "2.0"
// }
// ```
//
// ### Query Parameters
//
// | Parameter | Type | Default | Required | Description |
// |-----------+------+---------+----------+-----------------|
// | tx | Tx | nil | true | The transaction |
func BroadcastTxCommit(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
// Broadcast tx and wait for CheckTx result
checkTxResCh := make(chan abci.Response, 1)
err := mempool.CheckTx(tx, func(res abci.Response) {
checkTxResCh <- res
})
if err != nil {
logger.Error("Error on broadcastTxCommit", "err", err)
return nil, fmt.Errorf("Error on broadcastTxCommit: %v", err)
}
checkTxResMsg := <-checkTxResCh
checkTxRes := checkTxResMsg.(abci.ResponseCheckTx)
if checkTxRes.Error != nil {
return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxRes,
DeliverTx: abci.ResponseDeliverTx{},
Hash: tx.Hash(),
}, nil
}
// Wait for the tx to be included in a block or timeout.
txRes, err := gTxDispatcher.getTxResult(tx, nil)
if err != nil {
return nil, err
}
return &ctypes.ResultBroadcastTxCommit{
CheckTx: checkTxRes,
DeliverTx: txRes.Response,
Hash: tx.Hash(),
Height: txRes.Height,
}, nil
}
// Get unconfirmed transactions (maximum ?limit entries) including their number.
//
// ```shell
// curl 'localhost:26657/unconfirmed_txs'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// result, err := client.UnconfirmedTxs()
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "result" : {
// "txs" : [],
// "total_bytes" : "0",
// "n_txs" : "0",
// "total" : "0"
// },
// "jsonrpc" : "2.0",
// "id" : ""
// }
// ```
//
// ### Query Parameters
//
// | Parameter | Type | Default | Required | Description |
// |-----------+------+---------+----------+--------------------------------------|
// | limit | int | 30 | false | Maximum number of entries (max: 100) |
// ```
func UnconfirmedTxs(ctx *rpctypes.Context, limit int) (*ctypes.ResultUnconfirmedTxs, error) {
// reuse per_page validator
limit = validatePerPage(limit)
txs := mempool.ReapMaxTxs(limit)
return &ctypes.ResultUnconfirmedTxs{
Count: len(txs),
Total: mempool.Size(),
TotalBytes: mempool.TxsBytes(),
Txs: txs}, nil
}
// Get number of unconfirmed transactions.
//
// ```shell
// curl 'localhost:26657/num_unconfirmed_txs'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
// err := client.Start()
// if err != nil {
// // handle error
// }
// defer client.Stop()
// result, err := client.UnconfirmedTxs()
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "jsonrpc" : "2.0",
// "id" : "",
// "result" : {
// "n_txs" : "0",
// "total_bytes" : "0",
// "total" : "0"
// "txs" : null,
// }
// }
// ```
func NumUnconfirmedTxs(ctx *rpctypes.Context) (*ctypes.ResultUnconfirmedTxs, error) {
return &ctypes.ResultUnconfirmedTxs{
Count: mempool.Size(),
Total: mempool.Size(),
TotalBytes: mempool.TxsBytes()}, nil
}
//----------------------------------------
// txListener
// NOTE: txDispatcher doesn't handle any throttling or resource management.
// The RPC websockets system is expected to throttle requests.
type txDispatcher struct {
cmn.BaseService
evsw events.EventSwitch
listenerID string
sub <-chan events.Event
mtx sync.Mutex
waiters map[string]*txWaiter // string(types.Tx) -> *txWaiter
}
func newTxDispatcher(evsw events.EventSwitch) *txDispatcher {
listenerID := fmt.Sprintf("txDispatcher#%v", cmn.RandStr(6))
sub := events.SubscribeToEvent(evsw, listenerID, types.EventTx{})
td := &txDispatcher{
evsw: evsw,
listenerID: listenerID,
sub: sub,
waiters: make(map[string]*txWaiter),
}
td.BaseService = *cmn.NewBaseService(nil, "txDispatcher", td)
err := td.Start()
if err != nil {
panic(err)
}
return td
}
func (td *txDispatcher) OnStart() error {
go td.listenRoutine()
return nil
}
func (td *txDispatcher) OnStop() {
td.evsw.RemoveListener(td.listenerID)
}
func (td *txDispatcher) listenRoutine() {
for {
select {
case event, ok := <-td.sub:
if !ok {
td.Stop()
panic("txDispatcher subscription unexpectedly closed")
}
txEvent := event.(types.EventTx)
td.notifyTxEvent(txEvent)
case <-td.Quit():
return
}
}
}
func (td *txDispatcher) notifyTxEvent(txEvent types.EventTx) {
td.mtx.Lock()
defer td.mtx.Unlock()
tx := txEvent.Result.Tx
waiter, ok := td.waiters[string(tx)]
if !ok {
return // nothing to do
} else {
waiter.txRes = txEvent.Result
close(waiter.waitCh)
}
}
// blocking
// If the tx is already being waited on, returns the result from the original request.
// Upon result or timeout, the tx is forgotten from txDispatcher, and can be re-requested.
// If the tx times out, an error is returned.
// Quit can optionally be provided to terminate early (e.g. if the caller disconnects).
func (td *txDispatcher) getTxResult(tx types.Tx, quit chan struct{}) (types.TxResult, error) {
// Get or create waiter.
td.mtx.Lock()
waiter, ok := td.waiters[string(tx)]
if !ok {
waiter = newTxWaiter(tx)
}
td.mtx.Unlock()
select {
case <-waiter.waitCh:
return waiter.txRes, nil
case <-waiter.timeCh:
return types.TxResult{}, errors.New("request timeout")
case <-quit:
return types.TxResult{}, errors.New("caller quit")
}
}
type txWaiter struct {
tx types.Tx
waitCh chan struct{}
timeCh <-chan time.Time
txRes types.TxResult
}
func newTxWaiter(tx types.Tx) *txWaiter {
return &txWaiter{
tx: tx,
waitCh: make(chan struct{}),
timeCh: time.After(config.TimeoutBroadcastTxCommit),
}
}