From 56ac0b8ba8882f6e95a24b11272bb0378e88af9d Mon Sep 17 00:00:00 2001 From: zhi Date: Thu, 21 Dec 2023 11:16:32 +0800 Subject: [PATCH] address comments --- actpool/actqueue.go | 4 +--- actpool/queueworker.go | 10 +++++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/actpool/actqueue.go b/actpool/actqueue.go index 4be2f4f2ee..af1fe52006 100644 --- a/actpool/actqueue.go +++ b/actpool/actqueue.go @@ -9,7 +9,6 @@ import ( "container/heap" "context" "math/big" - "sort" "sync" "time" @@ -315,8 +314,6 @@ func (q *actQueue) AllActs() []action.SealedEnvelope { if len(q.items) == 0 { return acts } - // WARNING: after calling this function, the queue should be destroyed - sort.Sort(q.ascQueue) for _, nonce := range q.ascQueue { acts = append(acts, q.items[nonce.nonce]) } @@ -333,6 +330,7 @@ func (q *actQueue) PopActionWithLargestNonce() *action.SealedEnvelope { heap.Remove(&q.ascQueue, itemMeta.ascIdx) item := q.items[itemMeta.nonce] delete(q.items, itemMeta.nonce) + q.updateFromNonce(itemMeta.nonce) return &item } diff --git a/actpool/queueworker.go b/actpool/queueworker.go index 235a0bdd54..ef3367a550 100644 --- a/actpool/queueworker.go +++ b/actpool/queueworker.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "errors" "math/big" + "sort" "strings" "sync" "sync/atomic" @@ -266,7 +267,11 @@ func (worker *queueWorker) AllActions(sender address.Address) ([]action.SealedEn worker.mu.RLock() defer worker.mu.RUnlock() if actQueue := worker.accountActs.Account(sender.String()); actQueue != nil { - return actQueue.AllActs(), true + acts := actQueue.AllActs() + sort.Slice(acts, func(i, j int) bool { + return acts[i].Nonce() < acts[j].Nonce() + }) + return acts, true } return nil, false } @@ -290,9 +295,8 @@ func (worker *queueWorker) ResetAccount(sender address.Address) []action.SealedE if actQueue != nil { pendingActs := actQueue.AllActs() actQueue.Reset() - worker.mu.Lock() + // the following line is thread safe with worker.mu.RLock worker.emptyAccounts.Set(senderStr, struct{}{}) - worker.mu.Unlock() return pendingActs } return nil