Page MenuHomePhabricator

D13396.id38721.diff
No OneTemporary

D13396.id38721.diff

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
@@ -18,6 +18,16 @@
unique: true,
},
);
+ // in pendingAliases, do not enforce unique aliases. If more than one pending registration is active, notify admin
+ // enforce unique txids
+ db.collection(config.database.collections.pendingAliases).createIndex(
+ {
+ txid: 1,
+ },
+ {
+ unique: true,
+ },
+ );
// Enforce unique txids, i.e. you cannot have the same txid in the db more than once
db.collection(
config.database.collections.confirmedTxHistory,
diff --git a/web/alias-server/index.js b/web/alias-server/index.js
--- a/web/alias-server/index.js
+++ b/web/alias-server/index.js
@@ -43,6 +43,23 @@
}
});
+ app.get('/pending', async function (req, res) {
+ // Get IP address from before cloudflare proxy
+ const ip = req.clientIp;
+ log(`/pending from IP: ${ip}, host ${req.headers.host}`);
+ let pendingAliases;
+ try {
+ pendingAliases = await db
+ .collection(config.database.collections.pendingAliases)
+ .find()
+ .project({ _id: 0 })
+ .toArray();
+ return res.status(200).json(pendingAliases);
+ } catch (error) {
+ return res.status(500).json({ error });
+ }
+ });
+
app.listen(config.express.port);
}
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
@@ -4,6 +4,7 @@
getAliasTxs,
getAliasStringsFromValidAliases,
getUnprocessedValidAliasRegistrations,
+ parseAliasTx,
} = require('./alias');
const { getUnprocessedTxHistory } = require('./chronik');
const {
@@ -429,7 +430,88 @@
}
break;
case 'AddedToMempool':
- log(`New tx: ${wsMsg.txid}`);
+ const unconfirmedTxid = wsMsg.txid;
+ log(`New tx: ${unconfirmedTxid}`);
+ /*
+ Process new unconfirmed txs as potential pending aliases
+
+ The point of collecting these potential alias registrations is to prevent
+ accidental double registration of aliases in Cashtab
+
+ Hence, we do not need to know if any of these pending alias txs
+ are potentially valid or not. Even if this tx will be invalidated
+ on confirmation, we would still like to prevent someone trying to register
+ it in Cashtab
+ */
+
+ // Get tx info from chronik about this tx
+ // Parse it for alias prefix, string, and fee (everything but if it exists earlier or not)
+ let txDetails, potentialAliasDetails;
+ try {
+ txDetails = await chronik.tx(unconfirmedTxid);
+ potentialAliasDetails = parseAliasTx(
+ txDetails,
+ config.aliasConstants,
+ );
+ if (potentialAliasDetails) {
+ log(
+ `${unconfirmedTxid} is a potential alias registration: ${potentialAliasDetails.alias}`,
+ );
+ } else {
+ log(
+ `${unconfirmedTxid} is not a valid alias registration.`,
+ );
+ }
+ } catch (err) {
+ log(`Error in chronik.tx(${unconfirmedTxid})`, err);
+ potentialAliasDetails = false;
+ }
+
+ if (potentialAliasDetails) {
+ // Insert this parsed alias tx into the database (if it does not exist)
+ let pendingAliasUpdateResult;
+ const { address, alias, txid, blockheight } =
+ potentialAliasDetails;
+
+ let pendingAliasUpdate = {
+ $set: {
+ address,
+ alias,
+ txid,
+ blockheight,
+ },
+ };
+ // this option instructs the method to create a document if no documents match the filter
+ const pendingAliasOptions = { upsert: true };
+
+ try {
+ pendingAliasUpdateResult = await db
+ .collection(
+ config.database.collections.pendingAliases,
+ )
+ .updateOne(
+ potentialAliasDetails,
+ pendingAliasUpdate,
+ pendingAliasOptions,
+ );
+ if (
+ pendingAliasUpdateResult &&
+ pendingAliasUpdateResult.upsertedCount > 0
+ ) {
+ log(`Added ${alias}, ${txid} to pendingAliases`);
+ } else {
+ log(
+ `potentialAlias ${alias} not added to pendingAliases collection, already there`,
+ );
+ }
+ } catch (err) {
+ log(
+ `Error in updateOne() for new potentialAlias with txid ${txid}`,
+ err,
+ );
+ // Monitor. If pendingAliases aren't getting added, this is not a critical failure.
+ }
+ }
break;
case 'Confirmed':
log(`New confirmed tx: ${wsMsg.txid}`);

File Metadata

Mime Type
text/plain
Expires
Thu, Feb 6, 16:12 (17 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5081624
Default Alt Text
D13396.id38721.diff (5 KB)

Event Timeline