2022-12-09 10:32:58 -06:00
|
|
|
import { TransactionExtended } from "../mempool.interfaces";
|
|
|
|
|
2022-03-08 14:49:25 +01:00
|
|
|
export interface CachedRbf {
|
|
|
|
txid: string;
|
|
|
|
expires: Date;
|
|
|
|
}
|
|
|
|
|
2022-12-09 10:32:58 -06:00
|
|
|
export interface CachedRbfs {
|
|
|
|
txids: string[];
|
|
|
|
expires: Date;
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:49:25 +01:00
|
|
|
class RbfCache {
|
2022-12-09 10:32:58 -06:00
|
|
|
private replacedby: { [txid: string]: CachedRbf; } = {};
|
|
|
|
private replaces: { [txid: string]: CachedRbfs } = {};
|
|
|
|
private txs: { [txid: string]: TransactionExtended } = {};
|
2022-03-08 14:49:25 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
setInterval(this.cleanup.bind(this), 1000 * 60 * 60);
|
|
|
|
}
|
|
|
|
|
2022-12-09 10:32:58 -06:00
|
|
|
public add(replacedTx: TransactionExtended, newTxId: string): void {
|
|
|
|
const expiry = new Date(Date.now() + 1000 * 604800); // 1 week
|
|
|
|
this.replacedby[replacedTx.txid] = {
|
|
|
|
expires: expiry,
|
2022-03-08 14:49:25 +01:00
|
|
|
txid: newTxId,
|
|
|
|
};
|
2022-12-09 10:32:58 -06:00
|
|
|
this.txs[replacedTx.txid] = replacedTx;
|
|
|
|
if (!this.replaces[newTxId]) {
|
|
|
|
this.replaces[newTxId] = {
|
|
|
|
txids: [],
|
|
|
|
expires: expiry,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
this.replaces[newTxId].txids.push(replacedTx.txid);
|
|
|
|
this.replaces[newTxId].expires = expiry;
|
2022-03-08 14:49:25 +01:00
|
|
|
}
|
|
|
|
|
2022-12-09 10:32:58 -06:00
|
|
|
public getReplacedBy(txId: string): CachedRbf | undefined {
|
|
|
|
return this.replacedby[txId];
|
|
|
|
}
|
|
|
|
|
|
|
|
public getReplaces(txId: string): CachedRbfs | undefined {
|
|
|
|
return this.replaces[txId];
|
|
|
|
}
|
|
|
|
|
|
|
|
public getTx(txId: string): TransactionExtended | undefined {
|
|
|
|
return this.txs[txId];
|
2022-03-08 14:49:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private cleanup(): void {
|
|
|
|
const currentDate = new Date();
|
2022-12-09 10:32:58 -06:00
|
|
|
for (const c in this.replacedby) {
|
|
|
|
if (this.replacedby[c].expires < currentDate) {
|
|
|
|
delete this.replacedby[c];
|
|
|
|
delete this.txs[c];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const c in this.replaces) {
|
|
|
|
if (this.replaces[c].expires < currentDate) {
|
|
|
|
delete this.replaces[c];
|
2022-03-08 14:49:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new RbfCache();
|