Page MenuHomePhabricator

D13361.id38955.diff
No OneTemporary

D13361.id38955.diff

diff --git a/web/alias-server/config.js b/web/alias-server/config.js
--- a/web/alias-server/config.js
+++ b/web/alias-server/config.js
@@ -6,6 +6,7 @@
validAliases: 'validAliasTxs',
pendingAliases: 'pendingAliasTxs',
confirmedTxHistory: 'confirmedTxHistory',
+ serverState: 'serverState',
},
connectionUrl: 'mongodb://localhost:27017',
},
diff --git a/web/alias-server/db.js b/web/alias-server/db.js
--- a/web/alias-server/db.js
+++ b/web/alias-server/db.js
@@ -27,6 +27,9 @@
unique: true,
},
);
+ db.collection(config.database.collections.serverState).createIndex({
+ processedConfirmedTxs: 1,
+ });
log(`Configured connection to database ${config.database.name}`);
return db;
},
diff --git a/web/alias-server/websocket.js b/web/alias-server/websocket.js
--- a/web/alias-server/websocket.js
+++ b/web/alias-server/websocket.js
@@ -73,28 +73,67 @@
*/
}
- let mostRecentAlias;
- let processedBlockheight;
- // If you have aliases in the db, determine the most recently processed block
- if (validAliasesInDb && validAliasesInDb.length > 0) {
+ // Get the valid aliases already in the db
+ let serverState, serverStateArray;
+ try {
+ serverStateArray = await db
+ .collection(config.database.collections.serverState)
+ .find()
+ .toArray();
+ if (serverStateArray.length === 0) {
+ // Special case where you are just starting the app
+
+ log(
+ `App has no serverState. processedConfirmedTxs will default to 0`,
+ );
+ }
+ serverState = serverStateArray[0];
+ } catch (error) {
+ return log(
+ `Error in determining serverState, notifying admin and exiting parseWebsocketMessage()`,
+ error,
+ );
+ /*
+ If this happens,
+ - Notify Admin (TODO)
+ - Do not finish this function. Leave alias info unchanged
+ (will be in error state or not displaying the most recent aliases)
+ - Not displaying the most recent aliases is acceptable if the API does display
+ the blockheight at which info is valid
+ */
+ }
+
+ let mostRecentAlias,
+ processedBlockheight,
+ processedConfirmedTxs;
+ // If you have aliases in the db and processedConfirmedTxs in serverState,
+ // determine the most recently processed block and processedConfirmedTxs
+ if (
+ validAliasesInDb &&
+ validAliasesInDb.length > 0 &&
+ serverStateArray.length > 0 &&
+ serverStateArray[0] &&
+ typeof serverStateArray[0].processedConfirmedTxs !==
+ 'undefined'
+ ) {
// The alias with the highest blockheight will be the last element
mostRecentAlias =
validAliasesInDb[validAliasesInDb.length - 1];
processedBlockheight = mostRecentAlias.blockheight;
+ processedConfirmedTxs = serverState.processedConfirmedTxs;
} else {
log(
`Server startup. There are no valid aliases in the database.`,
);
// If nothing is in cache, get the full tx history
processedBlockheight = 0;
+ processedConfirmedTxs = 0;
// If validAliasesInDb is empty, set it to ABC whitelist
// This will be built on with getUnprocessedValidAliasRegistrations()
validAliasesInDb = generateReservedAliasTxArray();
}
- log(`processedBlockheight: ${processedBlockheight}`);
-
// Get confirmedTxHistory already in db
let confirmedTxHistoryInDb;
try {
@@ -117,13 +156,24 @@
}
// Determine the number of transactions you have seen
+ // NB confirmedTxHistoryInDb to be deprecated
const processedTxCount =
confirmedTxHistoryInDb && confirmedTxHistoryInDb.length
? confirmedTxHistoryInDb.length
: 0;
- log(`processedTxCount`, processedTxCount);
+ if (processedTxCount !== processedConfirmedTxs) {
+ // Could indicate a problem. Log it in case you see it.
+ // processedTxCount is legacy approach, to be deprecated
+ // processedConfirmedTxs is new serverState approach
+ log(`processedTxCount !== processedConfirmedTxs`);
+ log(`processedTxCount: ${processedTxCount}`);
+ log(`processedConfirmedTxs: ${processedConfirmedTxs}`);
+ } else {
+ log(`processedConfirmedTxs: ${processedConfirmedTxs}`);
+ }
+ // Default to legacy method for now until you do some testing
const unprocessedTxs = await getUnprocessedTxHistory(
config.aliasConstants.registrationHash160,
processedBlockheight,
@@ -192,6 +242,10 @@
const confirmedTxsToBeAddedToDb =
removeUnconfirmedTxsFromTxHistory(unprocessedTxs);
+ // Calculate new processedConfirmedTxs
+ const updatedProcessedConfirmedTxs =
+ processedConfirmedTxs + confirmedTxsToBeAddedToDb.length;
+
if (confirmedTxsToBeAddedToDb.length > 0) {
log(
`Adding ${confirmedTxsToBeAddedToDb.length} confirmed txs to the db`,
@@ -211,18 +265,72 @@
);
} catch (err) {
log(
- `A MongoBulkWriteException occurred adding confirmedTxsToBeAddedToDb to the db, but there are successfully processed documents.`,
+ `Error in db.collection(${config.database.collections.confirmedTxHistory}).insertMany(confirmedTxsToBeAddedToDb)`,
+ err,
);
/*
let ids = err.result.result.insertedIds;
for (let id of Object.values(ids)) {
log(`Processed a document with id ${id._id}`);
}
- */
+
log(
`Number of documents inserted: ${err.result.result.nInserted}`,
);
- log(`Error:`, err);
+ */
+ }
+
+ // Update serverState.processedConfirmedTxs
+ log(
+ `Updating serverState.processedConfirmedTxs from ${processedConfirmedTxs} to ${updatedProcessedConfirmedTxs}.`,
+ );
+
+ // else update by id
+ if (serverStateArray.length > 0 && serverState._id) {
+ const serverStateQuery = { _id: serverState._id };
+ const serverStateUpdate = {
+ $set: {
+ processedConfirmedTxs:
+ updatedProcessedConfirmedTxs,
+ },
+ };
+ let serverStateUpdateResult;
+ try {
+ serverStateUpdateResult = await db
+ .collection(
+ config.database.collections.serverState,
+ )
+ .updateOne(serverStateQuery, serverStateUpdate);
+ log(
+ `Updated serverState.processedConfirmedTxs from ${processedConfirmedTxs} to ${updatedProcessedConfirmedTxs}.`,
+ );
+ } catch (err) {
+ log(
+ `Error in db.collection(${config.database.collections.serverState}).update(${serverStateQuery}, ${serverStateUpdate})`,
+ err,
+ );
+ // If this isn't updated, the server will process too many txs next time
+ // Acceptable, no need for failure or admin notification
+ }
+ } else {
+ // If you are writing serverState to the db for the first time
+ let serverStateInsertOneResult;
+ try {
+ serverStateInsertOneResult = await db
+ .collection(
+ config.database.collections.serverState,
+ )
+ .insertOne({
+ processedConfirmedTxs:
+ updatedProcessedConfirmedTxs,
+ });
+ } catch (err) {
+ log(
+ `Error in db.collection(${config.database.collections.serverState}).insertOne({processedConfirmedTxs: ${updatedProcessedConfirmedTxs}}), err`,
+ );
+ // If this isn't updated, the server will process too many txs next time
+ // Acceptable, no need for failure or admin notification
+ }
}
} else {
log(`No new confirmed alias txs since last block`);

File Metadata

Mime Type
text/plain
Expires
Thu, Feb 6, 16:21 (17 h, 25 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5082681
Default Alt Text
D13361.id38955.diff (10 KB)

Event Timeline