diff --git a/backend/src/api/common.ts b/backend/src/api/common.ts index 751bab5a3..358a98c98 100644 --- a/backend/src/api/common.ts +++ b/backend/src/api/common.ts @@ -222,7 +222,25 @@ export class Common { } static getTransactionFlags(tx: TransactionExtended): number { - let flags = 0n; + let flags = tx.flags ? BigInt(tx.flags) : 0n; + + // Update variable flags (CPFP, RBF) + if (tx.ancestors?.length) { + flags |= TransactionFlags.cpfp_child; + } + if (tx.descendants?.length) { + flags |= TransactionFlags.cpfp_parent; + } + if (tx.replacement) { + flags |= TransactionFlags.replacement; + } + + // Already processed static flags, no need to do it again + if (tx.flags) { + return Number(flags); + } + + // Process static flags if (tx.version === 1) { flags |= TransactionFlags.v1; } else if (tx.version === 2) { @@ -306,15 +324,7 @@ export class Common { if (hasFakePubkey) { flags |= TransactionFlags.fake_pubkey; } - if (tx.ancestors?.length) { - flags |= TransactionFlags.cpfp_child; - } - if (tx.descendants?.length) { - flags |= TransactionFlags.cpfp_parent; - } - if (rbfCache.getRbfTree(tx.txid)) { - flags |= TransactionFlags.replacement; - } + // fast but bad heuristic to detect possible coinjoins // (at least 5 inputs and 5 outputs, less than half of which are unique amounts, with no address reuse) const addressReuse = Object.values(reusedAddresses).reduce((acc, count) => Math.max(acc, count), 0) > 1; @@ -335,6 +345,7 @@ export class Common { static classifyTransaction(tx: TransactionExtended): TransactionClassified { const flags = this.getTransactionFlags(tx); + tx.flags = flags; return { ...this.stripTransaction(tx), flags, diff --git a/backend/src/api/disk-cache.ts b/backend/src/api/disk-cache.ts index 093f07f0d..202f8f4cb 100644 --- a/backend/src/api/disk-cache.ts +++ b/backend/src/api/disk-cache.ts @@ -256,6 +256,7 @@ class DiskCache { txs: rbfData.rbf.txs.map(([txid, entry]) => ({ value: entry })), trees: rbfData.rbf.trees, expiring: rbfData.rbf.expiring.map(([txid, value]) => ({ key: txid, value })), + mempool: memPool.getMempool(), }); } } catch (e) { diff --git a/backend/src/api/rbf-cache.ts b/backend/src/api/rbf-cache.ts index ad1762485..a087abbe0 100644 --- a/backend/src/api/rbf-cache.ts +++ b/backend/src/api/rbf-cache.ts @@ -97,6 +97,8 @@ class RbfCache { return; } + newTxExtended.replacement = true; + const newTx = Common.stripTransaction(newTxExtended) as RbfTransaction; const newTime = newTxExtended.firstSeen || (Date.now() / 1000); newTx.rbf = newTxExtended.vin.some((v) => v.sequence < 0xfffffffe); @@ -368,14 +370,14 @@ class RbfCache { }; } - public async load({ txs, trees, expiring }): Promise { + public async load({ txs, trees, expiring, mempool }): Promise { try { txs.forEach(txEntry => { this.txs.set(txEntry.value.txid, txEntry.value); }); this.staleCount = 0; for (const deflatedTree of trees) { - await this.importTree(deflatedTree.root, deflatedTree.root, deflatedTree, this.txs); + await this.importTree(mempool, deflatedTree.root, deflatedTree.root, deflatedTree, this.txs); } expiring.forEach(expiringEntry => { if (this.txs.has(expiringEntry.key)) { @@ -413,7 +415,7 @@ class RbfCache { return deflated; } - async importTree(root, txid, deflated, txs: Map, mined: boolean = false): Promise { + async importTree(mempool, root, txid, deflated, txs: Map, mined: boolean = false): Promise { const treeInfo = deflated[txid]; const replaces: RbfTree[] = []; @@ -426,9 +428,12 @@ class RbfCache { // recursively reconstruct child trees for (const childId of treeInfo.replaces) { - const replaced = await this.importTree(root, childId, deflated, txs, mined); + const replaced = await this.importTree(mempool, root, childId, deflated, txs, mined); if (replaced) { this.replacedBy.set(replaced.tx.txid, txid); + if (mempool[replaced.tx.txid]) { + mempool[replaced.tx.txid].replacement = true; + } replaces.push(replaced); if (replaced.mined) { mined = true; diff --git a/backend/src/api/redis-cache.ts b/backend/src/api/redis-cache.ts index 82ce34ad1..edfd2142b 100644 --- a/backend/src/api/redis-cache.ts +++ b/backend/src/api/redis-cache.ts @@ -222,6 +222,7 @@ class RedisCache { txs: rbfTxs, trees: rbfTrees.map(loadedTree => { loadedTree.value.key = loadedTree.key; return loadedTree.value; }), expiring: rbfExpirations, + mempool: memPool.getMempool(), }); } diff --git a/backend/src/mempool.interfaces.ts b/backend/src/mempool.interfaces.ts index 4a630f1e4..c93372ded 100644 --- a/backend/src/mempool.interfaces.ts +++ b/backend/src/mempool.interfaces.ts @@ -94,7 +94,9 @@ export interface TransactionExtended extends IEsploraApi.Transaction { vsize: number, }; acceleration?: boolean; + replacement?: boolean; uid?: number; + flags?: number; } export interface MempoolTransactionExtended extends TransactionExtended {