From d16d961cb22ece2f0512fa37f827098aee9fedec Mon Sep 17 00:00:00 2001 From: mononaut <83316221+mononaut@users.noreply.github.com> Date: Mon, 3 Jul 2023 11:55:43 -0400 Subject: [PATCH] Apply suggestions from code review avoid regex in partial txid ordering conversion Co-authored-by: Jonathan Underwood --- backend/src/__tests__/gbt/gbt-tests.ts | 8 +++++++- backend/src/api/transaction-utils.ts | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/src/__tests__/gbt/gbt-tests.ts b/backend/src/__tests__/gbt/gbt-tests.ts index 8619457ea..8927b4096 100644 --- a/backend/src/__tests__/gbt/gbt-tests.ts +++ b/backend/src/__tests__/gbt/gbt-tests.ts @@ -56,5 +56,11 @@ function mempoolFromArrayBuffer(buf: ArrayBuffer): ThreadTransaction[] { } function txidToOrdering(txid: string): number { - return parseInt(txid.slice(56).match(/../g)?.reverse().join('') as string, 16); + return ( + ((parseInt(txid.substring(62, 64), 16) << 24) | + (parseInt(txid.substring(60, 62), 16) << 16) | + (parseInt(txid.substring(58, 60), 16) << 8) | + parseInt(txid.substring(56, 58), 16)) >>> + 0 + ); } diff --git a/backend/src/api/transaction-utils.ts b/backend/src/api/transaction-utils.ts index 28861a997..92f5b71c5 100644 --- a/backend/src/api/transaction-utils.ts +++ b/backend/src/api/transaction-utils.ts @@ -158,7 +158,13 @@ class TransactionUtils { // returns the most significant 4 bytes of the txid as an integer public txidToOrdering(txid: string): number { - return parseInt(txid.slice(56).match(/../g)?.reverse().join('') as string, 16); + return ( + ((parseInt(txid.substring(62, 64), 16) << 24) | + (parseInt(txid.substring(60, 62), 16) << 16) | + (parseInt(txid.substring(58, 60), 16) << 8) | + parseInt(txid.substring(56, 58), 16)) >>> + 0 + ); } }