diff --git a/apps/alias-server/src/events.js b/apps/alias-server/src/events.js --- a/apps/alias-server/src/events.js +++ b/apps/alias-server/src/events.js @@ -5,7 +5,7 @@ 'use strict'; const config = require('../config'); const aliasConstants = require('../constants/alias'); -const { wait, removeUnconfirmedTxsFromTxHistory } = require('./utils'); +const { wait, splitTxsByConfirmed } = require('./utils'); const { isFinalBlock } = require('./rpc'); const { getServerState, updateServerState } = require('./db'); const { getUnprocessedTxHistory } = require('./chronik'); @@ -151,17 +151,13 @@ ); // Remove unconfirmed txs as these are not eligible for valid alias registrations - const confirmedUnprocessedTxs = - removeUnconfirmedTxsFromTxHistory(allUnprocessedTxs); + const { confirmedTxs } = splitTxsByConfirmed(allUnprocessedTxs); // Get all potentially valid alias registrations // i.e. correct fee is paid, prefix is good, everything good but not yet checked against // conflicting aliases that registered earlier or have alphabetically earlier txid in // same block - const unprocessedAliasTxs = getAliasTxs( - confirmedUnprocessedTxs, - aliasConstants, - ); + const unprocessedAliasTxs = getAliasTxs(confirmedTxs, aliasConstants); // Add new valid alias txs to the database and get a list of what was added const newAliasRegistrations = await registerAliases( @@ -175,8 +171,7 @@ // New processedConfirmedTxs is determined by adding the count of now-processed txs const newServerState = { processedBlockheight: tipHeight, - processedConfirmedTxs: - processedConfirmedTxs + confirmedUnprocessedTxs.length, + processedConfirmedTxs: processedConfirmedTxs + confirmedTxs.length, }; // Update serverState const serverStateUpdated = await updateServerState(db, newServerState); diff --git a/apps/alias-server/src/utils.js b/apps/alias-server/src/utils.js --- a/apps/alias-server/src/utils.js +++ b/apps/alias-server/src/utils.js @@ -87,16 +87,24 @@ */ return /^[a-z0-9]+$/.test(alias); }, - removeUnconfirmedTxsFromTxHistory: function (txHistory) { + /** + * Take a txHistory object from chronik and return confirmedTxs and unconfirmedTxs + * @param {array} txHistory chronik tx history response + * @returns {object} {confirmedTxs, unconfirmedTxs} + */ + splitTxsByConfirmed: function (txHistory) { // Remove unconfirmed txs from an array of chronik tx objects - const confirmedTxHistory = []; + const confirmedTxs = []; + const unconfirmedTxs = []; for (let i = 0; i < txHistory.length; i += 1) { const thisTx = txHistory[i]; if (typeof thisTx.block !== 'undefined') { - confirmedTxHistory.push(thisTx); + confirmedTxs.push(thisTx); + } else { + unconfirmedTxs.push(thisTx); } } - return confirmedTxHistory; + return { confirmedTxs, unconfirmedTxs }; }, wait: async function (msecs) { await new Promise(resolve => setTimeout(resolve, msecs)); diff --git a/apps/alias-server/test/aliasTests.js b/apps/alias-server/test/aliasTests.js --- a/apps/alias-server/test/aliasTests.js +++ b/apps/alias-server/test/aliasTests.js @@ -13,7 +13,7 @@ sortAliasTxsByTxidAndBlockheight, registerAliases, } = require('../src/alias'); -const { removeUnconfirmedTxsFromTxHistory } = require('../src/utils'); +const { splitTxsByConfirmed } = require('../src/utils'); const { generated, templates } = require('./mocks/aliasMocks'); // Mock mongodb const { MongoClient } = require('mongodb'); @@ -541,12 +541,12 @@ ].block; // App logic is to remove unconfirmed txs before calling registeredAliases. Follow this. - const onlyConfirmedAliasTxs = removeUnconfirmedTxsFromTxHistory( + const { confirmedTxs } = splitTxsByConfirmed( txHistoryWithSomeUnconfirmedTxs, ); // Now getAllAlias txs from this set - const allAliasTxs = getAliasTxs(onlyConfirmedAliasTxs, aliasConstants); + const allAliasTxs = getAliasTxs(confirmedTxs, aliasConstants); // registerAliases won't see the unconfirmed tx, so remove this from your expected result const validAliasRegistrationsClone = JSON.parse( diff --git a/apps/alias-server/test/utilsTests.js b/apps/alias-server/test/utilsTests.js --- a/apps/alias-server/test/utilsTests.js +++ b/apps/alias-server/test/utilsTests.js @@ -10,7 +10,7 @@ getHexFromAlias, getAliasBytecount, isValidAliasString, - removeUnconfirmedTxsFromTxHistory, + splitTxsByConfirmed, satsToFormattedValue, getAliasPrice, } = require('../src/utils'); @@ -60,7 +60,7 @@ assert.deepEqual(isValidAliasString(invalidAliasString), false); } }); - it('removeUnconfirmedTxsFromTxHistory removes unconfirmed txs from an array of chronik tx history', function () { + it('splitTxsByConfirmed returns confirmed and unconfirmed txs from an array of chronik tx history', function () { // First, clone the mock so that you are not modifying it in place const txHistoryWithSomeUnconfirmedTxs = JSON.parse( JSON.stringify(generated.txHistory), @@ -73,12 +73,16 @@ delete txHistoryWithSomeUnconfirmedTxs[2].block; // 828201e4680e6617636193d3f2a319daab80a8cc5772b9a5b6e068de639f2d9c // Manually delete these txs from your expected result - let expectedResult = JSON.parse(JSON.stringify(generated.txHistory)); - expectedResult.splice(0, 3); - assert.deepEqual( - removeUnconfirmedTxsFromTxHistory(txHistoryWithSomeUnconfirmedTxs), - expectedResult, - ); + let confirmedTxs = JSON.parse(JSON.stringify(generated.txHistory)); + delete confirmedTxs[0].block; // db09c578d38f37bd9f2bb69eeb8ecb2e24c5be01aa2914f17d94759aadf71386 + delete confirmedTxs[1].block; // c040ccdc46df2951b2ab0cd6d48cf9db7c518068d1f871e60379ee8ccd1caa0e + delete confirmedTxs[2].block; // 828201e4680e6617636193d3f2a319daab80a8cc5772b9a5b6e068de639f2d9c + + let unconfirmedTxs = confirmedTxs.splice(0, 3); + assert.deepEqual(splitTxsByConfirmed(txHistoryWithSomeUnconfirmedTxs), { + confirmedTxs, + unconfirmedTxs, + }); }); it('satsToFormattedValue returns a 6-decimal formatted fiat amount if total fiat value is less than $0.00001', function () { assert.strictEqual(satsToFormattedValue(10, mockXecPrice), `$0.000003`);