/
tx_pool.go
executable file
·473 lines (420 loc) · 12.1 KB
/
tx_pool.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2018 The sphinx Authors
// Modified based on go-ethereum, which Copyright (C) 2014 The go-ethereum Authors.
//
// The sphinx is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The sphinx is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the sphinx. If not, see <http://www.gnu.org/licenses/>.
package txpool
import (
"errors"
"fmt"
"github.com/shx-project/sphinx/blockchain"
"github.com/shx-project/sphinx/blockchain/state"
"github.com/shx-project/sphinx/blockchain/types"
"github.com/shx-project/sphinx/common"
"github.com/shx-project/sphinx/common/log"
"github.com/shx-project/sphinx/config"
"github.com/shx-project/sphinx/event"
"github.com/shx-project/sphinx/event/sub"
"gopkg.in/fatih/set.v0"
"math/rand"
"sync"
"sync/atomic"
"time"
)
var (
evictionInterval = time.Minute // Time interval to check for evictable transactions
statsReportInterval = 5 * time.Second // Time interval to report transaction pool stats
chanHeadBuffer = 10
maxTransactionSize = common.StorageSize(32 * 1024)
tmpQEvictionInterval = 3 * time.Minute // Time interval to check for evictable tmpQueue transactions
maxHandleKnownTxs = 2000000
maxThreadNumber = 4
)
var INSTANCE = atomic.Value{}
var STOPPED = atomic.Value{}
var handleKnownTx = set.New()
// blockChain provides the state of blockchain and current gas limit to do
// some pre checks in tx pool.
type blockChain interface {
CurrentBlock() *types.Block
GetBlock(hash common.Hash, number uint64) *types.Block
StateAt(root common.Hash) (*state.StateDB, error)
SubscribeChainHeadEvent(ch chan<- bc.ChainHeadEvent) sub.Subscription
}
type TxPool struct {
wg sync.WaitGroup
dealwg sync.WaitGroup
stopCh chan struct{}
chain blockChain
chainHeadSub sub.Subscription
chainHeadCh chan bc.ChainHeadEvent
threadsNum int
fullCh []chan *types.Transaction
verifyCh []chan *types.Transaction
invalidTxCh chan *types.Transaction
txFeed sub.Feed
scope sub.SubscriptionScope
txPreTrigger *event.Trigger
signer types.Signer
config config.TxPoolConfiguration
queue sync.Map //map[txhash]*types.Transaction
pending sync.Map //map[txhash]*types.Transaction
onChain sync.Map //map[txhash]uint64 tx has been inserted to chain
working sync.Map //map[workid]types.transactions
poolBlockCount uint64 //block count pooled in onChain.
smu sync.RWMutex // mutex for below.
currentState *state.StateDB // Current state in the blockchain head
pendingState *state.ManagedState // Pending state tracking virtual nonces
}
//Create the transaction pool and start main process loop.
func NewTxPool(config config.TxPoolConfiguration, chainConfig *config.ChainConfig, blockChain blockChain) *TxPool {
if INSTANCE.Load() != nil {
return INSTANCE.Load().(*TxPool)
}
//2.Create the transaction pool with its initial settings
pool := &TxPool{
config: config,
chain: blockChain,
signer: types.NewQSSigner(chainConfig.ChainId),
chainHeadCh: make(chan bc.ChainHeadEvent, chanHeadBuffer),
stopCh: make(chan struct{}),
threadsNum: maxThreadNumber,
fullCh: make([]chan *types.Transaction, maxThreadNumber),
verifyCh: make([]chan *types.Transaction, maxThreadNumber),
invalidTxCh: make(chan *types.Transaction, 100000),
poolBlockCount: 100,
}
for i := 0; i < pool.threadsNum; i++ {
pool.fullCh[i] = make(chan *types.Transaction, 2000000)
pool.verifyCh[i] = make(chan *types.Transaction, 2000000)
}
INSTANCE.Store(pool)
return pool
}
func (pool *TxPool) KnownTxAdd(hash common.Hash) {
if handleKnownTx.Size() >= maxHandleKnownTxs {
handleKnownTx.Clear()
}
handleKnownTx.Add(hash)
}
func (pool *TxPool) Signer() types.Signer {
return pool.signer
}
func (pool *TxPool) Start() {
pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh)
// Register Publish TxPre publisher
pool.txPreTrigger = event.RegisterTrigger("tx_pool_tx_pre_publisher")
// start main process loop
go pool.loop()
for i := 0; i < pool.threadsNum; i++ {
go pool.DealTxRoutine(pool.fullCh[i], pool.verifyCh[i])
}
go pool.dealInvalid()
}
func GetTxPool() *TxPool {
if INSTANCE.Load() != nil {
return INSTANCE.Load().(*TxPool)
}
log.Warn("TxPool is nil, please init tx pool first.")
return nil
}
//Stop the transaction pool.
func (pool *TxPool) Stop() {
if STOPPED.Load() == nil {
//1.stop main process loop
pool.stopCh <- struct{}{}
//2.wait quit
pool.wg.Wait()
STOPPED.Store(true)
}
}
//Main process loop.
func (pool *TxPool) loop() {
pool.wg.Add(1)
defer pool.wg.Done()
// Start the stats reporting and transaction eviction tickers
evict := time.NewTicker(evictionInterval)
defer evict.Stop()
// Keep waiting for and reacting to the various events
for {
select {
// Handle ChainHeadEvent
//case ev := <-pool.chainHeadCh:
// log.Info("txpool loop, get new block.")
// if ev.Block != nil {
// pool.JustPending(ev.Block)
// }
// Handle onChain tx over block count.
case <-evict.C:
go pool.FitOnChain()
//stop signal
case <-pool.stopCh:
close(pool.invalidTxCh)
for i := 0; i < pool.threadsNum; i++ {
close(pool.fullCh[i])
close(pool.verifyCh[i])
}
pool.dealwg.Wait()
return
}
}
}
func (pool *TxPool) dealInvalid() {
pool.wg.Add(1)
defer pool.wg.Done()
for {
select {
case tx, ok := <-pool.invalidTxCh:
//go pool.FitOnChain()
if !ok {
return
}
log.Trace("txpool got invalid tx", "hash", tx.Hash())
}
}
}
func (pool *TxPool) FitOnChain() {
head := pool.chain.CurrentBlock()
curHigh := head.Number().Uint64()
pool.onChain.Range(func(k, v interface{}) bool {
number := v.(uint64)
if number > curHigh+pool.poolBlockCount {
pool.onChain.Delete(k)
}
return true
})
}
func (pool *TxPool) validateTx(tx *types.Transaction) error {
// Heuristic limit, reject transactions over 32KB to prevent DOS attacks
if tx.Size() > maxTransactionSize {
log.Trace("ErrOversizedData maxTransactionSize", "ErrOversizedData", ErrOversizedData)
return ErrOversizedData
}
return nil
}
func (pool *TxPool) DupTx(tx *types.Transaction) error {
hash := tx.Hash()
if handleKnownTx.Has(hash) {
log.Trace("Discarding already known transaction", "hash", hash)
return fmt.Errorf("known transaction: %x", hash)
}
if _, ok := pool.queue.Load(hash); ok {
log.Trace("Discarding already known transaction", "hash", hash)
return fmt.Errorf("known transaction: %x", hash)
}
if _, ok := pool.pending.Load(hash); ok {
log.Trace("Discarding already known transaction", "hash", hash)
return fmt.Errorf("known transaction: %x", hash)
}
if _, ok := pool.onChain.Load(hash); ok {
log.Trace("Discarding already known transaction", "hash", hash)
return fmt.Errorf("known transaction: %x", hash)
}
return nil
}
func (pool *TxPool) verifyTx(tx *types.Transaction) bool {
return true
}
func (pool *TxPool) sendToVerify(tx *types.Transaction, verifyCh chan *types.Transaction) error {
select {
case verifyCh <- tx:
log.Trace("tx pool send to verify", "tx.Hash", tx.Hash())
default:
return errors.New("tx pool verifyCh is full")
}
return nil
}
func (pool *TxPool) DealTxRoutine(fullCh, verifyCh chan *types.Transaction) {
pool.dealwg.Add(1)
go func() {
defer pool.dealwg.Done()
for {
select {
case tx, ok := <-fullCh:
if !ok {
//channel closed.
return
}
pool.queue.Store(tx.Hash(), tx)
pool.sendToVerify(tx, verifyCh)
}
}
}()
pool.dealwg.Add(1)
go func() {
// VerifyRoutine
defer pool.dealwg.Done()
for {
select {
case tx, ok := <-verifyCh:
if !ok {
//channel closed.
return
}
if pool.verifyTx(tx) {
pool.pending.Store(tx.Hash(), tx)
pool.queue.Delete(tx.Hash())
} else {
pool.invalidTxCh <- tx
}
}
}
}()
}
func (pool *TxPool) sendToFull(tx *types.Transaction, fullCh chan *types.Transaction) bool {
select {
case fullCh <- tx:
return false
default:
return true
}
return false
}
func (pool *TxPool) AddTx(tx *types.Transaction) error {
if dup := pool.DupTx(tx); dup != nil {
return dup
}
pool.KnownTxAdd(tx.Hash())
//if err := pool.validateTx(tx); err != nil {
// return err
//}
start := rand.Intn(pool.threadsNum)
for try := 0; try < pool.threadsNum; try++ {
index := (start + try) % pool.threadsNum
full := pool.sendToFull(tx, pool.fullCh[index])
if !full {
log.Trace("AddTx", "tx.Hash", tx.Hash())
return nil
}
}
log.Trace("AddTx failed", "fullch is full,tx.Hash", tx.Hash())
return errors.New("fullch is full")
}
func (pool *TxPool) AddTxs(txs []*types.Transaction) error {
for i, tx := range txs {
err := pool.AddTx(tx)
if err != nil {
log.Trace("AddTxs to add tx failed", "index", i, "err", err)
continue
}
}
return nil
}
// reset move package in block transactions from pending to onChain.
func (pool *TxPool) JustPending(newblock *types.Block) {
newChainTxs := newblock.Transactions()
for _, tx := range newChainTxs {
pool.onChain.Store(tx.Hash(), newblock.Number().Uint64())
pool.pending.Delete(tx.Hash())
}
}
//For RPC
// stats retrieves the current pool stats, namely the number of pending and the
// number of queued (non-executable) transactions.
func (pool *TxPool) Stats() (int, int) {
pending := 0
queued := 0
pool.pending.Range(func(k, v interface{}) bool {
pending += 1
return true
})
pool.queue.Range(func(k, v interface{}) bool {
queued += 1
return true
})
return pending, queued
}
// Get returns a transaction if it is contained in the pool
// and nil otherwise.
func (pool *TxPool) GetTxByHash(hash common.Hash) *types.Transaction {
v, ok := pool.pending.Load(hash)
if !ok {
v, ok = pool.queue.Load(hash)
if !ok {
log.Trace("not Finding already known tmptx transaction", "hash", hash)
return nil
}
tmptx := v.(*types.Transaction)
return tmptx
}
tx := v.(*types.Transaction)
return tx
}
// Pending retrieves all currently processable transactions, groupped by origin
// account and sorted by nonce. The returned transaction set is a copy and can be
// freely modified by calling code.
func (pool *TxPool) Pending(workid int64, maxtxs int) (pending types.Transactions) {
var i = 0
pending = make(types.Transactions, 0, maxtxs)
pool.pending.Range(func(k, v interface{}) bool {
if i >= maxtxs {
return false
}
tx := v.(*types.Transaction)
pending = append(pending, tx)
pool.pending.Delete(tx.Hash())
i++
return true
})
if i > 0 {
pool.working.Store(workid, pending)
}
return
}
func (pool *TxPool) WorkEnded(workid int64, blocknumber uint64, succeed bool) {
mining, ok := pool.working.Load(workid)
if ok {
if txs, yes := mining.(types.Transactions); yes {
for _, tx := range txs {
if succeed {
// append to onChain
pool.onChain.Store(tx.Hash(), blocknumber)
} else {
// append to pending
pool.pending.Store(tx.Hash(), tx)
}
}
pool.working.Delete(workid)
}
}
}
func (pool *TxPool) Pended() (pending types.Transactions, err error) {
pool.pending.Range(func(k, v interface{}) bool {
tx := v.(*types.Transaction)
pending = append(pending, tx)
return true
})
return
}
// State returns the virtual managed state of the transaction pool.
func (pool *TxPool) State() *state.ManagedState {
pool.smu.RLock()
defer pool.smu.RUnlock()
return pool.pendingState
}
func (pool *TxPool) Content() (pending, queue types.Transactions) {
pool.pending.Range(func(k, v interface{}) bool {
tx := v.(*types.Transaction)
pending = append(pending, tx)
return true
})
pool.queue.Range(func(k, v interface{}) bool {
tx := v.(*types.Transaction)
queue = append(queue, tx)
return true
})
return
}
func (pool *TxPool) SubscribeTxPreEvent(ch chan<- bc.TxPreEvent) sub.Subscription {
return pool.scope.Track(pool.txFeed.Subscribe(ch))
}