diff --git a/packages/transaction-pool-service/source/query.test.ts b/packages/transaction-pool-service/source/query.test.ts index a9739b294..eb4c03dc5 100644 --- a/packages/transaction-pool-service/source/query.test.ts +++ b/packages/transaction-pool-service/source/query.test.ts @@ -138,7 +138,7 @@ describe<{ ]); }); - it("getFromHighestPriority - should return transactions order by fee", async (context) => { + it("getFromHighestPriority - should return transactions order by nonce/fee", async (context) => { stub(context.mempool, "getSenderMempools").returnValueOnce([ { getFromEarliest: () => [context.sender1Transaction200, context.sender1Transaction100] }, { getFromEarliest: () => [context.sender2Transaction100, context.sender2Transaction200] }, @@ -148,10 +148,10 @@ describe<{ const result = await query.getFromHighestPriority().all(); assert.equal(result, [ - context.sender2Transaction200, - context.sender2Transaction100, - context.sender1Transaction200, context.sender1Transaction100, + context.sender1Transaction200, + context.sender2Transaction100, + context.sender2Transaction200, ]); }); diff --git a/packages/transaction-pool-service/source/query.ts b/packages/transaction-pool-service/source/query.ts index eb22e6a1d..0415a8f66 100644 --- a/packages/transaction-pool-service/source/query.ts +++ b/packages/transaction-pool-service/source/query.ts @@ -88,10 +88,15 @@ export class Query implements Contracts.TransactionPool.Query { return new QueryIterable( [...this.mempool.getSenderMempools()] .flatMap((senderMempool) => [...senderMempool.getFromLatest()]) - .sort( - (a: Contracts.Crypto.Transaction, b: Contracts.Crypto.Transaction) => - a.data.gasPrice - b.data.gasPrice, - ), + .sort((a: Contracts.Crypto.Transaction, b: Contracts.Crypto.Transaction) => { + // Compare by nonce in ascending order + if (!a.data.nonce.isEqualTo(b.data.nonce)) { + return a.data.nonce.comparedTo(b.data.nonce); + } + + // If nonce values are equal, compare by gas price in ascending order + return a.data.gasPrice - b.data.gasPrice; + }), ); }