From 7576033134ee744e2d7739d05cedcd98c0bce1e8 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 2 Mar 2020 12:01:03 +0100 Subject: [PATCH] Fix order when merging results --- packages/bcp/src/cosmwasmconnection.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index a7054e82..648ea7a0 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -60,7 +60,7 @@ function isDefined(value: X | undefined): value is X { return value !== undefined; } -function deduplicate(input: ReadonlyArray, comparator: (a: T, b: T) => number): ReadonlyArray { +function deduplicate(input: ReadonlyArray, comparator: (a: T, b: T) => number): Array { const out = new Array(); for (const element of input) { if (!out.find(o => comparator(o, element) === 0)) { @@ -70,6 +70,15 @@ function deduplicate(input: ReadonlyArray, comparator: (a: T, b: T) => num return out; } +/** Compares transaxtion by height. If the height is equal, compare by hash to ensure deterministic order */ +function compareByHeightAndHash(a: TxsResponse, b: TxsResponse): number { + if (a.height === b.height) { + return a.txhash.localeCompare(b.txhash); + } else { + return parseInt(a.height, 10) - parseInt(b.height, 10); + } +} + /** Account and undefined are valid events. The third option means no event fired yet */ type LastWatchAccountEvent = Account | undefined | "no_event_fired_yet"; @@ -362,7 +371,7 @@ export class CosmWasmConnection implements BlockchainConnection { } const responses = await Promise.all(pendingRequests); const allResults = responses.reduce((accumulator, results) => accumulator.concat(results), []); - txs = deduplicate(allResults, (a, b) => a.txhash.localeCompare(b.txhash)); + txs = deduplicate(allResults, (a, b) => a.txhash.localeCompare(b.txhash)).sort(compareByHeightAndHash); } else { throw new Error("Unsupported query"); }