-
Notifications
You must be signed in to change notification settings - Fork 320
/
queueworker.go
321 lines (286 loc) · 8.49 KB
/
queueworker.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
package actpool
import (
"context"
"encoding/hex"
"errors"
"math/big"
"sort"
"strings"
"sync"
"sync/atomic"
"github.com/iotexproject/go-pkgs/cache/ttl"
"github.com/iotexproject/iotex-address/address"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/tracer"
)
type (
queueWorker struct {
queue chan workerJob
ap *actPool
mu sync.RWMutex
accountActs *accountPool
emptyAccounts *ttl.Cache
}
workerJob struct {
ctx context.Context
act *action.SealedEnvelope
rep bool
err chan error
}
pendingActions struct {
sender string
acts []*action.SealedEnvelope
}
)
func newQueueWorker(ap *actPool, jobQueue chan workerJob) *queueWorker {
acc, _ := ttl.NewCache()
return &queueWorker{
queue: jobQueue,
ap: ap,
accountActs: newAccountPool(),
emptyAccounts: acc,
}
}
func (worker *queueWorker) Start() error {
if worker.queue == nil || worker.ap == nil {
return errors.New("worker is invalid")
}
go func() {
for {
job, more := <-worker.queue
if !more { // worker chan is closed
return
}
job.err <- worker.Handle(job)
}
}()
return nil
}
func (worker *queueWorker) Stop() error {
close(worker.queue)
return nil
}
// Hanlde is called sequentially by worker
func (worker *queueWorker) Handle(job workerJob) error {
ctx := job.ctx
// ctx is canceled or timeout
if ctx.Err() != nil {
return ctx.Err()
}
var (
span = tracer.SpanFromContext(ctx)
act = job.act
sender = act.SenderAddress().String()
actHash, _ = act.Hash()
intrinsicGas, _ = act.IntrinsicGas()
replace = job.rep
)
defer span.End()
nonce, balance, err := worker.getConfirmedState(ctx, act.SenderAddress())
if err != nil {
return err
}
if err := worker.checkSelpWithState(act, nonce, balance); err != nil {
return err
}
if err := worker.putAction(sender, act, nonce, balance); err != nil {
return err
}
worker.ap.allActions.Set(actHash, act)
if desAddress, ok := act.Destination(); ok && !strings.EqualFold(sender, desAddress) {
if err := worker.ap.accountDesActs.addAction(act); err != nil {
log.L().Debug("fail to add destionation map", zap.Error(err))
}
}
atomic.AddUint64(&worker.ap.gasInPool, intrinsicGas)
worker.mu.Lock()
defer worker.mu.Unlock()
if replace {
// TODO: early return if sender is the account to pop and nonce is larger than largest in the queue
actToReplace := worker.accountActs.PopPeek()
if actToReplace == nil {
log.L().Warn("UNEXPECTED ERROR: action pool is full, but no action to drop")
return nil
}
worker.ap.removeInvalidActs([]*action.SealedEnvelope{actToReplace})
if actToReplace.SenderAddress().String() == sender && actToReplace.Nonce() == nonce {
err = action.ErrTxPoolOverflow
_actpoolMtc.WithLabelValues("overMaxNumActsPerPool").Inc()
}
}
worker.removeEmptyAccounts()
return err
}
func (worker *queueWorker) getConfirmedState(ctx context.Context, sender address.Address) (uint64, *big.Int, error) {
worker.mu.RLock()
queue := worker.accountActs.Account(sender.String())
worker.mu.RUnlock()
// account state isn't cached in the actpool
if queue == nil {
confirmedState, err := accountutil.AccountState(ctx, worker.ap.sf, sender)
if err != nil {
return 0, nil, err
}
var nonce uint64
if protocol.MustGetFeatureCtx(ctx).UseZeroNonceForFreshAccount {
nonce = confirmedState.PendingNonceConsideringFreshAccount()
} else {
nonce = confirmedState.PendingNonce()
}
return nonce, confirmedState.Balance, nil
}
nonce, balance := queue.AccountState()
return nonce, balance, nil
}
func (worker *queueWorker) checkSelpWithState(act *action.SealedEnvelope, pendingNonce uint64, balance *big.Int) error {
if act.Nonce() < pendingNonce {
_actpoolMtc.WithLabelValues("nonceTooSmall").Inc()
return action.ErrNonceTooLow
}
// Nonce exceeds current range
if act.Nonce()-pendingNonce >= worker.ap.cfg.MaxNumActsPerAcct {
hash, _ := act.Hash()
log.L().Debug("Rejecting action because nonce is too large.",
log.Hex("hash", hash[:]),
zap.Uint64("startNonce", pendingNonce),
zap.Uint64("actNonce", act.Nonce()))
_actpoolMtc.WithLabelValues("nonceTooLarge").Inc()
return action.ErrNonceTooHigh
}
if cost, _ := act.Cost(); balance.Cmp(cost) < 0 {
_actpoolMtc.WithLabelValues("insufficientBalance").Inc()
sender := act.SenderAddress().String()
actHash, _ := act.Hash()
log.L().Debug("insufficient balance for action",
zap.String("actionHash", hex.EncodeToString(actHash[:])),
zap.String("cost", cost.String()),
zap.String("balance", balance.String()),
zap.String("sender", sender),
)
return action.ErrInsufficientFunds
}
return nil
}
func (worker *queueWorker) putAction(sender string, act *action.SealedEnvelope, pendingNonce uint64, confirmedBalance *big.Int) error {
worker.mu.Lock()
err := worker.accountActs.PutAction(
sender,
worker.ap,
pendingNonce,
confirmedBalance,
worker.ap.cfg.ActionExpiry,
act,
)
worker.mu.Unlock()
if err != nil {
actHash, _ := act.Hash()
_actpoolMtc.WithLabelValues("failedPutActQueue").Inc()
log.L().Debug("failed put action into ActQueue",
zap.String("actionHash", hex.EncodeToString(actHash[:])),
zap.Error(err))
return err
}
return nil
}
func (worker *queueWorker) removeEmptyAccounts() {
if worker.emptyAccounts.Count() == 0 {
return
}
worker.emptyAccounts.Range(func(key, _ interface{}) error {
worker.accountActs.DeleteIfEmpty(key.(string))
return nil
})
worker.emptyAccounts.Reset()
}
func (worker *queueWorker) Reset(ctx context.Context) {
worker.mu.RLock()
defer worker.mu.RUnlock()
worker.accountActs.Range(func(from string, queue ActQueue) {
addr, _ := address.FromString(from)
confirmedState, err := accountutil.AccountState(ctx, worker.ap.sf, addr)
if err != nil {
log.L().Error("Error when removing confirmed actions", zap.Error(err))
queue.Reset()
worker.emptyAccounts.Set(from, struct{}{})
return
}
var pendingNonce uint64
if protocol.MustGetFeatureCtx(ctx).UseZeroNonceForFreshAccount {
pendingNonce = confirmedState.PendingNonceConsideringFreshAccount()
} else {
pendingNonce = confirmedState.PendingNonce()
}
// Remove all actions that are committed to new block
acts := queue.UpdateAccountState(pendingNonce, confirmedState.Balance)
acts2 := queue.UpdateQueue()
worker.ap.removeInvalidActs(append(acts, acts2...))
// Delete the queue entry if it becomes empty
if queue.Empty() {
worker.emptyAccounts.Set(from, struct{}{})
}
})
}
// PendingActions returns all accepted actions
func (worker *queueWorker) PendingActions(ctx context.Context) []*pendingActions {
actionArr := make([]*pendingActions, 0)
worker.mu.RLock()
defer worker.mu.RUnlock()
worker.accountActs.Range(func(from string, queue ActQueue) {
if queue.Empty() {
return
}
// Remove the actions that are already timeout
acts := queue.UpdateQueue()
worker.ap.removeInvalidActs(acts)
pd := queue.PendingActs(ctx)
if len(pd) == 0 {
return
}
actionArr = append(actionArr, &pendingActions{
sender: from,
acts: pd,
})
})
return actionArr
}
// AllActions returns the all actions of sender
func (worker *queueWorker) AllActions(sender address.Address) ([]*action.SealedEnvelope, bool) {
worker.mu.RLock()
defer worker.mu.RUnlock()
if actQueue := worker.accountActs.Account(sender.String()); actQueue != nil {
acts := actQueue.AllActs()
sort.Slice(acts, func(i, j int) bool {
return acts[i].Nonce() < acts[j].Nonce()
})
return acts, true
}
return nil, false
}
// PendingNonce returns the pending nonce of sender
func (worker *queueWorker) PendingNonce(sender address.Address) (uint64, bool) {
worker.mu.RLock()
defer worker.mu.RUnlock()
if actQueue := worker.accountActs.Account(sender.String()); actQueue != nil {
return actQueue.PendingNonce(), true
}
return 0, false
}
// ResetAccount resets account in the accountActs of worker
func (worker *queueWorker) ResetAccount(sender address.Address) []*action.SealedEnvelope {
senderStr := sender.String()
worker.mu.RLock()
actQueue := worker.accountActs.PopAccount(senderStr)
worker.mu.RUnlock()
if actQueue != nil {
pendingActs := actQueue.AllActs()
actQueue.Reset()
// the following line is thread safe with worker.mu.RLock
worker.emptyAccounts.Set(senderStr, struct{}{})
return pendingActs
}
return nil
}