Timeline of replacements for RBF-d transactions

This commit is contained in:
Mononaut
2022-12-13 17:11:37 -06:00
parent 8db7326a5a
commit 1b843da785
11 changed files with 295 additions and 21 deletions

View File

@@ -1,8 +1,15 @@
import { TransactionExtended } from "../mempool.interfaces";
import { TransactionExtended, TransactionStripped } from "../mempool.interfaces";
import { Common } from "./common";
interface RbfTransaction extends TransactionStripped {
rbf?: boolean;
}
class RbfCache {
private replacedBy: { [txid: string]: string; } = {};
private replaces: { [txid: string]: string[] } = {};
private rbfChains: { [root: string]: { tx: TransactionStripped, time: number, mined?: boolean }[] } = {}; // sequences of consecutive replacements
private chainMap: { [txid: string]: string } = {}; // map of txids to sequence ids
private txs: { [txid: string]: TransactionExtended } = {};
private expiring: { [txid: string]: Date } = {};
@@ -10,13 +17,34 @@ class RbfCache {
setInterval(this.cleanup.bind(this), 1000 * 60 * 60);
}
public add(replacedTx: TransactionExtended, newTxId: string): void {
this.replacedBy[replacedTx.txid] = newTxId;
this.txs[replacedTx.txid] = replacedTx;
if (!this.replaces[newTxId]) {
this.replaces[newTxId] = [];
public add(replacedTxExtended: TransactionExtended, newTxExtended: TransactionExtended): void {
const replacedTx = Common.stripTransaction(replacedTxExtended) as RbfTransaction;
replacedTx.rbf = replacedTxExtended.vin.some((v) => v.sequence < 0xfffffffe);
const newTx = Common.stripTransaction(newTxExtended) as RbfTransaction;
newTx.rbf = newTxExtended.vin.some((v) => v.sequence < 0xfffffffe);
this.replacedBy[replacedTx.txid] = newTx.txid;
this.txs[replacedTx.txid] = replacedTxExtended;
if (!this.replaces[newTx.txid]) {
this.replaces[newTx.txid] = [];
}
this.replaces[newTx.txid].push(replacedTx.txid);
// maintain rbf chains
if (this.chainMap[replacedTx.txid]) {
// add to an existing chain
const chainRoot = this.chainMap[replacedTx.txid];
this.rbfChains[chainRoot].push({ tx: newTx, time: newTxExtended.firstSeen || Date.now() });
this.chainMap[newTx.txid] = chainRoot;
} else {
// start a new chain
this.rbfChains[replacedTx.txid] = [
{ tx: replacedTx, time: replacedTxExtended.firstSeen || Date.now() },
{ tx: newTx, time: newTxExtended.firstSeen || Date.now() },
];
this.chainMap[replacedTx.txid] = replacedTx.txid;
this.chainMap[newTx.txid] = replacedTx.txid;
}
this.replaces[newTxId].push(replacedTx.txid);
}
public getReplacedBy(txId: string): string | undefined {
@@ -31,6 +59,10 @@ class RbfCache {
return this.txs[txId];
}
public getRbfChain(txId: string): { tx: TransactionStripped, time: number }[] {
return this.rbfChains[this.chainMap[txId]] || [];
}
// flag a transaction as removed from the mempool
public evict(txid): void {
this.expiring[txid] = new Date(Date.now() + 1000 * 86400); // 24 hours
@@ -48,14 +80,20 @@ class RbfCache {
// remove a transaction & all previous versions from the cache
private remove(txid): void {
// don't remove a transaction while a newer version remains in the mempool
if (this.replaces[txid] && !this.replacedBy[txid]) {
// don't remove a transaction if a newer version remains in the mempool
if (!this.replacedBy[txid]) {
const replaces = this.replaces[txid];
delete this.replaces[txid];
delete this.chainMap[txid];
delete this.txs[txid];
delete this.expiring[txid];
for (const tx of replaces) {
// recursively remove prior versions from the cache
delete this.replacedBy[tx];
delete this.txs[tx];
// if this is the root of a chain, remove that too
if (this.chainMap[tx] === tx) {
delete this.rbfChains[tx];
}
this.remove(tx);
}
}