/
address_record.go
215 lines (190 loc) · 4.58 KB
/
address_record.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
package mempool
import (
"sync"
"github.com/okx/okbchain/libs/tendermint/libs/clist"
"github.com/okx/okbchain/libs/tendermint/types"
)
type elementManager interface {
removeElement(*clist.CElement)
removeElementByKey(key [32]byte) *clist.CElement
reorganizeElements([]*clist.CElement)
}
type AddressRecord struct {
addrTxs sync.Map // address -> *addrMap
elementManager
}
type addrMap struct {
sync.RWMutex
items map[uint64]*clist.CElement // nonce -> *mempoolTx
maxNonce uint64
}
func newAddressRecord(em elementManager) *AddressRecord {
return &AddressRecord{elementManager: em}
}
func (ar *AddressRecord) AddItem(address string, cElement *clist.CElement) {
v, ok := ar.addrTxs.Load(address)
if !ok {
// LoadOrStore to prevent double storing
v, ok = ar.addrTxs.LoadOrStore(address, &addrMap{items: make(map[uint64]*clist.CElement)})
}
am := v.(*addrMap)
am.Lock()
defer am.Unlock()
am.items[cElement.Nonce] = cElement
if cElement.Nonce > am.maxNonce {
am.maxNonce = cElement.Nonce
}
}
func (ar *AddressRecord) checkRepeatedAndAddItem(memTx *mempoolTx, txPriceBump int64, cb func(*clist.CElement) *clist.CElement) *clist.CElement {
gasPrice := memTx.realTx.GetGasPrice()
nonce := memTx.realTx.GetNonce()
newElement := clist.NewCElement(memTx, memTx.from, gasPrice, nonce)
v, ok := ar.addrTxs.Load(memTx.from)
if !ok {
v, ok = ar.addrTxs.LoadOrStore(memTx.from, &addrMap{items: make(map[uint64]*clist.CElement)})
}
am := v.(*addrMap)
am.Lock()
defer am.Unlock()
// do not need to check element nonce
if newElement.Nonce > am.maxNonce {
cb(newElement)
am.maxNonce = newElement.Nonce
am.items[newElement.Nonce] = newElement
return newElement
}
for _, e := range am.items {
if e.Nonce == nonce {
// only replace tx for bigger gas price
expectedGasPrice := MultiPriceBump(e.GasPrice, txPriceBump)
if gasPrice.Cmp(expectedGasPrice) <= 0 {
return nil
}
// delete the old element and reorganize the elements whose nonce is greater the the new element
ar.removeElement(e)
items := []*clist.CElement{newElement}
for _, item := range am.items {
if item.Nonce > nonce {
items = append(items, item)
}
}
ar.reorganizeElements(items)
am.items[newElement.Nonce] = newElement
return newElement
}
}
cb(newElement)
am.items[newElement.Nonce] = newElement
return newElement
}
func (ar *AddressRecord) CleanItems(address string, nonce uint64, cb func(element *clist.CElement)) {
v, ok := ar.addrTxs.Load(address)
if !ok {
return
}
am := v.(*addrMap)
am.Lock()
defer am.Unlock()
for k, v := range am.items {
if v.Nonce <= nonce {
cb(v)
delete(am.items, k)
}
}
if len(am.items) == 0 {
ar.addrTxs.Delete(address)
}
}
func (ar *AddressRecord) GetItems(address string) []*clist.CElement {
v, ok := ar.addrTxs.Load(address)
if !ok {
return nil
}
am := v.(*addrMap)
var l []*clist.CElement
am.RLock()
defer am.RUnlock()
for _, v := range am.items {
l = append(l, v)
}
return l
}
func (ar *AddressRecord) DeleteItem(e *clist.CElement) {
if v, ok := ar.addrTxs.Load(e.Address); ok {
am := v.(*addrMap)
am.Lock()
defer am.Unlock()
delete(am.items, e.Nonce)
//calculate max Nonce
am.maxNonce = calculateMaxNonce(am)
if len(am.items) == 0 {
ar.addrTxs.Delete(e.Address)
}
}
}
func (ar *AddressRecord) GetAddressList() []string {
var addrList []string
ar.addrTxs.Range(func(k, v interface{}) bool {
addrList = append(addrList, k.(string))
return true
})
return addrList
}
func (ar *AddressRecord) GetAddressTxsCnt(address string) int {
v, ok := ar.addrTxs.Load(address)
if !ok {
return 0
}
am := v.(*addrMap)
am.RLock()
defer am.RUnlock()
return len(am.items)
}
func (ar *AddressRecord) GetAddressNonce(address string) (uint64, bool) {
v, ok := ar.addrTxs.Load(address)
if !ok {
return 0, false
}
am := v.(*addrMap)
am.RLock()
defer am.RUnlock()
return am.maxNonce, true
}
func (ar *AddressRecord) GetAddressTxs(address string, max int) types.Txs {
v, ok := ar.addrTxs.Load(address)
if !ok {
return nil
}
am := v.(*addrMap)
am.RLock()
defer am.RUnlock()
if max <= 0 || max > len(am.items) {
max = len(am.items)
}
txs := make([]types.Tx, 0, max)
for _, e := range am.items {
if len(txs) == max {
break
}
txs = append(txs, e.Value.(*mempoolTx).tx)
}
return txs
}
func calculateMaxNonce(data *addrMap) uint64 {
maxNonce := uint64(0)
for k, _ := range data.items {
if k > maxNonce {
maxNonce = k
}
}
return maxNonce
}
type AddressNonce struct {
addr string
nonce uint64
}
var addressNoncePool = sync.Pool{
New: func() interface{} {
return &AddressNonce{}
},
}