/
exchain_pending_pool.go
185 lines (166 loc) · 4.46 KB
/
exchain_pending_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
package mempool
import (
"sync"
"github.com/okx/okbchain/libs/tendermint/types"
)
const (
FlagEnablePendingPool = "mempool.enable_pending_pool"
)
type PendingPool struct {
maxSize int
addressTxsMap map[string]map[uint64]*mempoolTx
txsMap map[string]*mempoolTx
mtx sync.RWMutex
period int
reserveBlocks int
periodCounter map[string]int // address with period count
maxTxPerAddress int
}
func newPendingPool(maxSize int, period int, reserveBlocks int, maxTxPerAddress int) *PendingPool {
return &PendingPool{
maxSize: maxSize,
addressTxsMap: make(map[string]map[uint64]*mempoolTx),
txsMap: make(map[string]*mempoolTx),
period: period,
reserveBlocks: reserveBlocks,
periodCounter: make(map[string]int),
maxTxPerAddress: maxTxPerAddress,
}
}
func (p *PendingPool) Size() int {
p.mtx.RLock()
defer p.mtx.RUnlock()
return len(p.txsMap)
}
func (p *PendingPool) txCount(address string) int {
p.mtx.RLock()
defer p.mtx.RUnlock()
if _, ok := p.addressTxsMap[address]; !ok {
return 0
}
return len(p.addressTxsMap[address])
}
func (p *PendingPool) getTx(address string, nonce uint64) *mempoolTx {
p.mtx.RLock()
defer p.mtx.RUnlock()
if _, ok := p.addressTxsMap[address]; ok {
return p.addressTxsMap[address][nonce]
}
return nil
}
func (p *PendingPool) hasTx(tx types.Tx) bool {
p.mtx.RLock()
defer p.mtx.RUnlock()
_, exist := p.txsMap[txID(tx)]
return exist
}
func (p *PendingPool) addTx(pendingTx *mempoolTx) {
p.mtx.Lock()
defer p.mtx.Unlock()
if _, ok := p.addressTxsMap[pendingTx.from]; !ok {
p.addressTxsMap[pendingTx.from] = make(map[uint64]*mempoolTx)
}
p.addressTxsMap[pendingTx.from][pendingTx.realTx.GetNonce()] = pendingTx
p.txsMap[txID(pendingTx.tx)] = pendingTx
}
func (p *PendingPool) removeTx(address string, nonce uint64) {
p.mtx.Lock()
defer p.mtx.Unlock()
if _, ok := p.addressTxsMap[address]; ok {
if pendingTx, ok := p.addressTxsMap[address][nonce]; ok {
delete(p.addressTxsMap[address], nonce)
delete(p.txsMap, txID(pendingTx.tx))
}
if len(p.addressTxsMap[address]) == 0 {
delete(p.addressTxsMap, address)
delete(p.periodCounter, address)
}
// update period counter
if count, ok := p.periodCounter[address]; ok && count > 0 {
p.periodCounter[address] = count - 1
}
}
}
func (p *PendingPool) removeTxByHash(txHash string) {
p.mtx.Lock()
defer p.mtx.Unlock()
if pendingTx, ok := p.txsMap[txHash]; ok {
delete(p.txsMap, txHash)
if _, ok := p.addressTxsMap[pendingTx.from]; ok {
delete(p.addressTxsMap[pendingTx.from], pendingTx.realTx.GetNonce())
if len(p.addressTxsMap[pendingTx.from]) == 0 {
delete(p.addressTxsMap, pendingTx.from)
delete(p.periodCounter, pendingTx.from)
}
// update period counter
if count, ok := p.periodCounter[pendingTx.from]; ok && count > 0 {
p.periodCounter[pendingTx.from] = count - 1
}
}
}
}
func (p *PendingPool) handlePendingTx(addressNonce map[string]uint64) map[string]uint64 {
p.mtx.Lock()
defer p.mtx.Unlock()
addrMap := make(map[string]uint64)
for addr, accountNonce := range addressNonce {
if txsMap, ok := p.addressTxsMap[addr]; ok {
for nonce, pendingTx := range txsMap {
// remove invalid pending tx
if nonce <= accountNonce {
delete(p.addressTxsMap[addr], nonce)
delete(p.txsMap, txID(pendingTx.tx))
} else if nonce == accountNonce+1 {
addrMap[addr] = nonce
}
}
if len(p.addressTxsMap[addr]) == 0 {
delete(p.addressTxsMap, addr)
}
}
}
return addrMap
}
func (p *PendingPool) handlePeriodCounter() {
p.mtx.Lock()
defer p.mtx.Unlock()
for addr, txMap := range p.addressTxsMap {
count := p.periodCounter[addr]
if count >= p.reserveBlocks {
delete(p.addressTxsMap, addr)
for _, pendingTx := range txMap {
delete(p.txsMap, txID(pendingTx.tx))
}
delete(p.periodCounter, addr)
} else {
p.periodCounter[addr] = count + 1
}
}
}
func (p *PendingPool) validate(address string, tx types.Tx) error {
// tx already in pending pool
if p.hasTx(tx) {
return ErrTxAlreadyInPendingPool{
txHash: txID(tx),
}
}
poolSize := p.Size()
if poolSize >= p.maxSize {
return ErrPendingPoolIsFull{
size: poolSize,
maxSize: p.maxSize,
}
}
txCount := p.txCount(address)
if txCount >= p.maxTxPerAddress {
return ErrPendingPoolAddressLimit{
address: address,
size: txCount,
maxSize: p.maxTxPerAddress,
}
}
return nil
}
type AccountRetriever interface {
GetAccountNonce(address string) uint64
}