From 58092f746c7a572d2802a8dbdf595f6356c3fc37 Mon Sep 17 00:00:00 2001 From: steveluscher Date: Mon, 30 May 2022 11:52:33 -0700 Subject: [PATCH] fix: serializing transactions; sort that takes less time and memory --- web3.js/src/transaction.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/web3.js/src/transaction.ts b/web3.js/src/transaction.ts index 7d6e0eb4cd..5c189b4983 100644 --- a/web3.js/src/transaction.ts +++ b/web3.js/src/transaction.ts @@ -395,13 +395,16 @@ export class Transaction { // Sort. Prioritizing first by signer, then by writable uniqueMetas.sort(function (x, y) { - const pubkeySorting = x.pubkey - .toBase58() - .localeCompare(y.pubkey.toBase58()); - const checkSigner = x.isSigner === y.isSigner ? 0 : x.isSigner ? -1 : 1; - const checkWritable = - x.isWritable === y.isWritable ? pubkeySorting : x.isWritable ? -1 : 1; - return checkSigner || checkWritable; + if (x.isSigner !== y.isSigner) { + // Signers always come before non-signers + return x.isSigner ? -1 : 1; + } + if (x.isWritable !== y.isWritable) { + // Writable accounts always come before read-only accounts + return x.isWritable ? -1 : 1; + } + // Otherwise, sort by pubkey. + return x.pubkey._bn.cmp(y.pubkey._bn); }); // Move fee payer to the front