Page MenuHomePhabricator

D13602.id39351.diff
No OneTemporary

D13602.id39351.diff

diff --git a/apps/alias-server/src/chronik.js b/apps/alias-server/src/chronik.js
--- a/apps/alias-server/src/chronik.js
+++ b/apps/alias-server/src/chronik.js
@@ -43,19 +43,12 @@
hash160,
processedBlockheight,
processedTxCount,
- optionalMocks = false,
) {
let potentiallyUnprocessedTxs = [];
- // optionalMocks is a param used to inject mock chronik data for unit tests
- /*
- optionalMocks = {txHistoryFirstPageResponse, remainingTxHistoryPageResponses}
- */
-
// Get first page of most recent chronik tx history
- const txHistoryFirstPageResponse = optionalMocks
- ? optionalMocks.txHistoryFirstPageResponse
- : await module.exports.getTxHistoryPage(chronik, hash160);
+ const txHistoryFirstPageResponse =
+ await module.exports.getTxHistoryPage(chronik, hash160);
const { txs, numPages } = txHistoryFirstPageResponse;
// This first page of results contains the most recent chronik txs at the address
@@ -103,9 +96,9 @@
// Use Promise.all so that an error is thrown if any single promise fails
let remainingTxHistoryPageResponses;
try {
- remainingTxHistoryPageResponses = optionalMocks
- ? optionalMocks.remainingTxHistoryPageResponses
- : await Promise.all(txHistoryPageResponsePromises);
+ remainingTxHistoryPageResponses = await Promise.all(
+ txHistoryPageResponsePromises,
+ );
} catch (err) {
log(`Error in Promise.all(txHistoryPageResponsePromises)`, err);
// Return false; you won't have all the tx history if this happens
@@ -138,18 +131,8 @@
}
}
}
- if (optionalMocks) {
- // Return spy variables to make sure they check out in unit tests
- return {
- maxTxs,
- maxUnprocessedTxCount,
- numPagesToFetch,
- alreadyHaveAllPotentiallyUnprocessedTxs,
- unprocessedTxs,
- };
- } else {
- return unprocessedTxs;
- }
+
+ return unprocessedTxs;
},
getAllTxHistory: async function (chronik, hash160) {
let allTxHistory = [];
diff --git a/apps/alias-server/test/chronikTests.js b/apps/alias-server/test/chronikTests.js
--- a/apps/alias-server/test/chronikTests.js
+++ b/apps/alias-server/test/chronikTests.js
@@ -5,6 +5,7 @@
'use strict';
const assert = require('assert');
const config = require('../config');
+const cashaddr = require('ecashaddrjs');
const { getUnprocessedTxHistory } = require('../src/chronik');
const {
allTxHistoryFromChronik,
@@ -19,33 +20,27 @@
const processedBlockheight = processedTxs[0].block.height;
const processedTxCount = processedTxs.length;
const unprocessedTxs = [];
- const allTxHistory = unprocessedTxs.concat(processedTxs);
- const txHistoryFirstPageResponse = {
- txs: allTxHistory.slice(0, config.txHistoryPageSize),
- numPages: Math.ceil(allTxHistory.length / config.txHistoryPageSize),
- };
- const optionalMocks = {
- txHistoryFirstPageResponse,
- remainingTxHistoryPageResponses: [],
- };
+ const { MockChronikClient } = require('./mocks/chronikMock');
+ // Initialize chronik mock with full tx history of test alias address
+ const { type, hash } = cashaddr.decode(
+ config.aliasConstants.address,
+ true,
+ );
+ const mockedChronik = new MockChronikClient(
+ type,
+ hash,
+ allTxHistoryFromChronik,
+ );
const result = await getUnprocessedTxHistory(
- null,
+ mockedChronik,
config.aliasConstants.registrationHash160,
processedBlockheight,
processedTxCount,
- optionalMocks,
);
- const expectedResult = {
- maxTxs: undefined,
- maxUnprocessedTxCount: undefined,
- numPagesToFetch: undefined,
- alreadyHaveAllPotentiallyUnprocessedTxs: true,
- unprocessedTxs: unprocessedTxs,
- };
-
- assert.deepEqual(result, expectedResult);
+
+ assert.deepEqual(result, unprocessedTxs);
});
it(`getUnprocessedTxHistory correctly recognizes when 11 unprocessed unconfirmed transactions are available with a txHistoryPageSize=${config.txHistoryPageSize}`, async () => {
const processedTxs = allTxHistoryFromChronik;
@@ -54,35 +49,22 @@
const unprocessedTxs = unconfirmedTxs;
const allTxHistory = unprocessedTxs.concat(processedTxs);
- const numPages = Math.ceil(
- allTxHistory.length / config.txHistoryPageSize,
+ const { MockChronikClient } = require('./mocks/chronikMock');
+ // Initialize chronik mock with full tx history of test alias address
+ const { type, hash } = cashaddr.decode(
+ config.aliasConstants.address,
+ true,
);
-
- const txHistoryFirstPageResponse = {
- txs: allTxHistory.slice(0, config.txHistoryPageSize),
- numPages,
- };
- const optionalMocks = {
- txHistoryFirstPageResponse,
- remainingTxHistoryPageResponses: [],
- };
+ const mockedChronik = new MockChronikClient(type, hash, allTxHistory);
const result = await getUnprocessedTxHistory(
- null,
+ mockedChronik,
config.aliasConstants.registrationHash160,
processedBlockheight,
processedTxCount,
- optionalMocks,
);
- const expectedResult = {
- maxTxs: undefined,
- maxUnprocessedTxCount: undefined,
- numPagesToFetch: undefined,
- alreadyHaveAllPotentiallyUnprocessedTxs: true,
- unprocessedTxs: unprocessedTxs,
- };
-
- assert.deepEqual(result, expectedResult);
+
+ assert.deepEqual(result, unprocessedTxs);
});
it(`getUnprocessedTxHistory correctly recognizes when 11 unprocessed confirmed transactions are available with a txHistoryPageSize=${config.txHistoryPageSize}`, async () => {
const processedTxs = allTxHistoryFromChronik;
@@ -91,35 +73,21 @@
const unprocessedTxs = unconfirmedTxsAfterConfirmation;
const allTxHistory = unprocessedTxs.concat(processedTxs);
- const numPages = Math.ceil(
- allTxHistory.length / config.txHistoryPageSize,
+ const { MockChronikClient } = require('./mocks/chronikMock');
+ // Initialize chronik mock with full tx history of test alias address
+ const { type, hash } = cashaddr.decode(
+ config.aliasConstants.address,
+ true,
);
-
- const txHistoryFirstPageResponse = {
- txs: allTxHistory.slice(0, config.txHistoryPageSize),
- numPages,
- };
- const optionalMocks = {
- txHistoryFirstPageResponse,
- remainingTxHistoryPageResponses: [],
- };
+ const mockedChronik = new MockChronikClient(type, hash, allTxHistory);
const result = await getUnprocessedTxHistory(
- null,
+ mockedChronik,
config.aliasConstants.registrationHash160,
processedBlockheight,
processedTxCount,
- optionalMocks,
);
- const expectedResult = {
- maxTxs: undefined,
- maxUnprocessedTxCount: undefined,
- numPagesToFetch: undefined,
- alreadyHaveAllPotentiallyUnprocessedTxs: true,
- unprocessedTxs: unprocessedTxs,
- };
-
- assert.deepEqual(result, expectedResult);
+ assert.deepEqual(result, unprocessedTxs);
});
it(`getUnprocessedTxHistory correctly fetches and parses required tx history with a txHistoryPageSize=${config.txHistoryPageSize}`, async () => {
// Note: txs are processed by blockheight. So, test will only work if you look at batches of txs from different blocks
@@ -147,6 +115,19 @@
}
}
+ // Mock chronik with allTxHistory
+ const { MockChronikClient } = require('./mocks/chronikMock');
+ // Initialize chronik mock with full tx history of test alias address
+ const { type, hash } = cashaddr.decode(
+ config.aliasConstants.address,
+ true,
+ );
+ const mockedChronik = new MockChronikClient(
+ type,
+ hash,
+ allTxHistoryFromChronik,
+ );
+
// Iterate over all block cutoff amounts of unprocessedTxs to test
for (let i = 0; i < blockheightDeltaTxCounts; i += 1) {
const desiredUnprocessedTxs = blockheightDeltaTxCounts[i];
@@ -193,11 +174,6 @@
alreadyHaveAllPotentiallyUnprocessedTxs = true;
}
}
-
- const txHistoryFirstPageResponse = {
- txs: txHistoryFirstPageTxs,
- numPages,
- };
// Calculate these values as in the function
let maxTxs, maxUnprocessedTxCount, numPagesToFetch;
let remainingTxHistoryPageResponses = [];
@@ -219,27 +195,14 @@
}
}
- const optionalMocks = {
- txHistoryFirstPageResponse,
- remainingTxHistoryPageResponses,
- };
-
const result = await getUnprocessedTxHistory(
- null,
+ mockedChronik,
config.aliasConstants.registrationHash160,
processedBlockheight,
processedTxCount,
- optionalMocks,
);
- const expectedResult = {
- maxTxs,
- maxUnprocessedTxCount,
- numPagesToFetch,
- alreadyHaveAllPotentiallyUnprocessedTxs,
- unprocessedTxs: unprocessedTxs,
- };
-
- assert.deepEqual(result, expectedResult);
+
+ assert.deepEqual(result, unprocessedTxs);
}
});
});
diff --git a/apps/alias-server/test/mocks/chronikMock.js b/apps/alias-server/test/mocks/chronikMock.js
new file mode 100644
--- /dev/null
+++ b/apps/alias-server/test/mocks/chronikMock.js
@@ -0,0 +1,46 @@
+// Copyright (c) 2023 The Bitcoin developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+'use strict';
+
+/* Mock chronik-client instance
+ * Must be initialized with type, hash, and full tx history
+ * Will return expected paginated responses of chronik tx history
+ */
+module.exports = {
+ MockChronikClient: class {
+ constructor(type, hash, txHistory) {
+ this._url = `https://mocked-chronik-instance/not-a-url/`;
+ this._wsUrl = `wss://mocked-chronik-instance/not-a-url/`;
+ this.txHistory = txHistory;
+
+ // Method to get paginated tx history with same variables as chronik
+ function getTxHistory(pageNumber = 0, pageSize) {
+ // Return chronik shaped responses
+ const startSliceOnePage = pageNumber * pageSize;
+ const endSliceOnePage = startSliceOnePage + pageSize;
+ const thisPage = txHistory.slice(
+ startSliceOnePage,
+ endSliceOnePage,
+ );
+ const response = {};
+
+ response.txs = thisPage;
+ response.numPages = Math.ceil(txHistory.length / pageSize);
+ return response;
+ }
+ this.mock = {};
+ this.mock[type] = {};
+ this.mock[type][hash] = {
+ history: function (pageNumber = 0, pageSize) {
+ // return history
+ return getTxHistory(pageNumber, pageSize);
+ },
+ };
+ // Return assigned mocks
+ this.script = function (type, hash) {
+ return this.mock[type][hash];
+ };
+ }
+ },
+};

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 22, 09:55 (5 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4558928
Default Alt Text
D13602.id39351.diff (12 KB)

Event Timeline