Skip to content

Commit 3141592

Browse files
steveluscherpull[bot]
authored andcommitted
fix: serializing transactions; sort that takes less time and memory
1 parent 9223efd commit 3141592

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

web3.js/src/transaction.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,16 @@ export class Transaction {
395395

396396
// Sort. Prioritizing first by signer, then by writable
397397
uniqueMetas.sort(function (x, y) {
398-
const pubkeySorting = x.pubkey
399-
.toBase58()
400-
.localeCompare(y.pubkey.toBase58());
401-
const checkSigner = x.isSigner === y.isSigner ? 0 : x.isSigner ? -1 : 1;
402-
const checkWritable =
403-
x.isWritable === y.isWritable ? pubkeySorting : x.isWritable ? -1 : 1;
404-
return checkSigner || checkWritable;
398+
if (x.isSigner !== y.isSigner) {
399+
// Signers always come before non-signers
400+
return x.isSigner ? -1 : 1;
401+
}
402+
if (x.isWritable !== y.isWritable) {
403+
// Writable accounts always come before read-only accounts
404+
return x.isWritable ? -1 : 1;
405+
}
406+
// Otherwise, sort by pubkey.
407+
return x.pubkey._bn.cmp(y.pubkey._bn);
405408
});
406409

407410
// Move fee payer to the front

0 commit comments

Comments
 (0)