/
account.go
282 lines (224 loc) · 6.31 KB
/
account.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
package txpool
import (
"sync"
"sync/atomic"
"github.com/JResearchLabs/Flutechain/types"
)
// Thread safe map of all accounts registered by the pool.
// Each account (value) is bound to one address (key).
type accountsMap struct {
sync.Map
count uint64
}
// Intializes an account for the given address.
func (m *accountsMap) initOnce(addr types.Address, nonce uint64) *account {
a, _ := m.LoadOrStore(addr, &account{})
newAccount := a.(*account) //nolint:forcetypeassert
// run only once
newAccount.init.Do(func() {
// create queues
newAccount.enqueued = newAccountQueue()
newAccount.promoted = newAccountQueue()
// set the nonce
newAccount.setNonce(nonce)
// update global count
atomic.AddUint64(&m.count, 1)
})
return newAccount
}
// exists checks if an account exists within the map.
func (m *accountsMap) exists(addr types.Address) bool {
_, ok := m.Load(addr)
return ok
}
// getPrimaries collects the heads (first-in-line transaction)
// from each of the promoted queues.
func (m *accountsMap) getPrimaries() (primaries []*types.Transaction) {
m.Range(func(key, value interface{}) bool {
addressKey, ok := key.(types.Address)
if !ok {
return false
}
account := m.get(addressKey)
account.promoted.lock(false)
defer account.promoted.unlock()
// add head of the queue
if tx := account.promoted.peek(); tx != nil {
primaries = append(primaries, tx)
}
return true
})
return primaries
}
// get returns the account associated with the given address.
func (m *accountsMap) get(addr types.Address) *account {
a, ok := m.Load(addr)
if !ok {
return nil
}
fetchedAccount, ok := a.(*account)
if !ok {
return nil
}
return fetchedAccount
}
// promoted returns the number of all promoted transactons.
func (m *accountsMap) promoted() (total uint64) {
m.Range(func(key, value interface{}) bool {
accountKey, ok := key.(types.Address)
if !ok {
return false
}
account := m.get(accountKey)
account.promoted.lock(false)
defer account.promoted.unlock()
total += account.promoted.length()
return true
})
return
}
// allTxs returns all promoted and all enqueued transactions, depending on the flag.
func (m *accountsMap) allTxs(includeEnqueued bool) (
allPromoted, allEnqueued map[types.Address][]*types.Transaction,
) {
allPromoted = make(map[types.Address][]*types.Transaction)
allEnqueued = make(map[types.Address][]*types.Transaction)
m.Range(func(key, value interface{}) bool {
addr, _ := key.(types.Address)
account := m.get(addr)
account.promoted.lock(false)
defer account.promoted.unlock()
if account.promoted.length() != 0 {
allPromoted[addr] = account.promoted.queue
}
if includeEnqueued {
account.enqueued.lock(false)
defer account.enqueued.unlock()
if account.enqueued.length() != 0 {
allEnqueued[addr] = account.enqueued.queue
}
}
return true
})
return
}
// An account is the core structure for processing
// transactions from a specific address. The nextNonce
// field is what separates the enqueued from promoted transactions:
//
// 1. enqueued - transactions higher than the nextNonce
// 2. promoted - transactions lower than the nextNonce
//
// If an enqueued transaction matches the nextNonce,
// a promoteRequest is signaled for this account
// indicating the account's enqueued transaction(s)
// are ready to be moved to the promoted queue.
type account struct {
init sync.Once
enqueued, promoted *accountQueue
nextNonce uint64
demotions uint
}
// getNonce returns the next expected nonce for this account.
func (a *account) getNonce() uint64 {
return atomic.LoadUint64(&a.nextNonce)
}
// setNonce sets the next expected nonce for this account.
func (a *account) setNonce(nonce uint64) {
atomic.StoreUint64(&a.nextNonce, nonce)
}
// reset aligns the account with the new nonce
// by pruning all transactions with nonce lesser than new.
// After pruning, a promotion may be signaled if the first
// enqueued transaction matches the new nonce.
func (a *account) reset(nonce uint64, promoteCh chan<- promoteRequest) (
prunedPromoted,
prunedEnqueued []*types.Transaction,
) {
a.promoted.lock(true)
defer a.promoted.unlock()
// prune the promoted txs
prunedPromoted = append(
prunedPromoted,
a.promoted.prune(nonce)...,
)
if nonce <= a.getNonce() {
// only the promoted queue needed pruning
return
}
a.enqueued.lock(true)
defer a.enqueued.unlock()
// prune the enqueued txs
prunedEnqueued = append(
prunedEnqueued,
a.enqueued.prune(nonce)...,
)
// update nonce expected for this account
a.setNonce(nonce)
// it is important to signal promotion while
// the locks are held to ensure no other
// handler will mutate the account
if first := a.enqueued.peek(); first != nil &&
first.Nonce == nonce {
// first enqueued tx is expected -> signal promotion
promoteCh <- promoteRequest{account: first.From}
}
return
}
// enqueue attempts tp push the transaction onto the enqueued queue.
func (a *account) enqueue(tx *types.Transaction) error {
a.enqueued.lock(true)
defer a.enqueued.unlock()
// reject low nonce tx
if tx.Nonce < a.getNonce() {
return ErrNonceTooLow
}
// enqueue tx
a.enqueued.push(tx)
return nil
}
// Promote moves eligible transactions from enqueued to promoted.
//
// Eligible transactions are all sequential in order of nonce
// and the first one has to have nonce less (or equal) to the account's
// nextNonce.
func (a *account) promote() []*types.Transaction {
a.promoted.lock(true)
a.enqueued.lock(true)
defer func() {
a.enqueued.unlock()
a.promoted.unlock()
}()
// sanity check
currentNonce := a.getNonce()
if a.enqueued.length() == 0 ||
a.enqueued.peek().Nonce > currentNonce {
// nothing to promote
return nil
}
promoted := make([]*types.Transaction, 0)
nextNonce := a.enqueued.peek().Nonce
// move all promotable txs (enqueued txs that are sequential in nonce)
// to the account's promoted queue
for {
tx := a.enqueued.peek()
if tx == nil ||
tx.Nonce != nextNonce {
break
}
// pop from enqueued
tx = a.enqueued.pop()
// push to promoted
a.promoted.push(tx)
// update counters
nextNonce += 1
// update return result
promoted = append(promoted, tx)
}
// only update the nonce map if the new nonce
// is higher than the one previously stored.
if nextNonce > currentNonce {
a.setNonce(nextNonce)
}
return promoted
}