Estimate accelerated positions in partner mempools
This commit is contained in:
parent
9b9aaed757
commit
f31b28251c
@ -1,12 +1,13 @@
|
||||
import { GbtGenerator, GbtResult, ThreadTransaction as RustThreadTransaction, ThreadAcceleration as RustThreadAcceleration } from 'rust-gbt';
|
||||
import logger from '../logger';
|
||||
import { MempoolBlock, MempoolTransactionExtended, MempoolBlockWithTransactions, MempoolBlockDelta, Ancestor, CompactThreadTransaction, EffectiveFeeStats, TransactionClassified, TransactionCompressed, MempoolDeltaChange, GbtCandidates } from '../mempool.interfaces';
|
||||
import { MempoolBlock, MempoolTransactionExtended, MempoolBlockWithTransactions, MempoolBlockDelta, Ancestor, CompactThreadTransaction, EffectiveFeeStats, TransactionClassified, TransactionCompressed, MempoolDeltaChange, GbtCandidates, PoolTag } from '../mempool.interfaces';
|
||||
import { Common, OnlineFeeStatsCalculator } from './common';
|
||||
import config from '../config';
|
||||
import { Worker } from 'worker_threads';
|
||||
import path from 'path';
|
||||
import mempool from './mempool';
|
||||
import { Acceleration } from './services/acceleration';
|
||||
import PoolsRepository from '../repositories/PoolsRepository';
|
||||
|
||||
const MAX_UINT32 = Math.pow(2, 32) - 1;
|
||||
|
||||
@ -21,6 +22,17 @@ class MempoolBlocks {
|
||||
private uidMap: Map<number, string> = new Map(); // map short numerical uids to full txids
|
||||
private txidMap: Map<string, number> = new Map(); // map full txids back to short numerical uids
|
||||
|
||||
private pools: { [id: number]: PoolTag } = {};
|
||||
|
||||
constructor() {
|
||||
PoolsRepository.$getPools().then(allPools => {
|
||||
this.pools = {};
|
||||
for (const pool of allPools) {
|
||||
this.pools[pool.uniqueId] = pool;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public getMempoolBlocks(): MempoolBlock[] {
|
||||
return this.mempoolBlocks.map((block) => {
|
||||
return {
|
||||
@ -478,7 +490,7 @@ class MempoolBlocks {
|
||||
const deltas = this.calculateMempoolDeltas(this.mempoolBlocks, mempoolBlocks);
|
||||
this.mempoolBlocks = mempoolBlocks;
|
||||
this.mempoolBlockDeltas = deltas;
|
||||
|
||||
this.updateAccelerationPositions(mempool, accelerations, mempoolBlocks);
|
||||
}
|
||||
|
||||
return mempoolBlocks;
|
||||
@ -625,6 +637,124 @@ class MempoolBlocks {
|
||||
tx.acc ? 1 : 0,
|
||||
];
|
||||
}
|
||||
|
||||
// estimates and saves positions of accelerations in mining partner mempools
|
||||
private updateAccelerationPositions(mempoolCache: { [txid: string]: MempoolTransactionExtended }, accelerations: { [txid: string]: Acceleration }, mempoolBlocks: MempoolBlockWithTransactions[]): void {
|
||||
const accelerationPositions: { [txid: string]: { poolId: number, pool: string, block: number, vsize: number }[] } = {};
|
||||
// keep track of simulated mempool blocks for each active pool
|
||||
const pools: {
|
||||
[pool: string]: { name: string, block: number, vsize: number, accelerations: string[], complete: boolean };
|
||||
} = {};
|
||||
// prepare a list of accelerations in ascending order (we'll pop items off the end of the list)
|
||||
const accQueue: { acceleration: Acceleration, rate: number, vsize: number }[] = Object.values(accelerations).map(acc => {
|
||||
let vsize = mempoolCache[acc.txid].vsize;
|
||||
for (const ancestor of mempoolCache[acc.txid].ancestors || []) {
|
||||
vsize += (ancestor.weight / 4);
|
||||
}
|
||||
return {
|
||||
acceleration: acc,
|
||||
rate: mempoolCache[acc.txid].effectiveFeePerVsize,
|
||||
vsize
|
||||
};
|
||||
}).sort((a, b) => a.rate - b.rate);
|
||||
// initialize the pool tracker
|
||||
for (const { acceleration } of accQueue) {
|
||||
accelerationPositions[acceleration.txid] = [];
|
||||
for (const pool of acceleration.pools) {
|
||||
if (!pools[pool]) {
|
||||
pools[pool] = {
|
||||
name: this.pools[pool]?.name || 'unknown',
|
||||
block: 0,
|
||||
vsize: 0,
|
||||
accelerations: [],
|
||||
complete: false,
|
||||
};
|
||||
}
|
||||
pools[pool].accelerations.push(acceleration.txid);
|
||||
}
|
||||
for (const ancestor of mempoolCache[acceleration.txid].ancestors || []) {
|
||||
accelerationPositions[ancestor.txid] = [];
|
||||
}
|
||||
}
|
||||
|
||||
for (const pool of Object.keys(pools)) {
|
||||
// if any pools accepted *every* acceleration, we can just use the GBT result positions directly
|
||||
if (pools[pool].accelerations.length === Object.keys(accelerations).length) {
|
||||
pools[pool].complete = true;
|
||||
}
|
||||
}
|
||||
|
||||
let block = 0;
|
||||
let index = 0;
|
||||
let next = accQueue.pop();
|
||||
// build simulated blocks for each pool by taking the best option from
|
||||
// either the mempool or the list of accelerations.
|
||||
while (next && block < mempoolBlocks.length) {
|
||||
while (next && index < mempoolBlocks[block].transactions.length) {
|
||||
const nextTx = mempoolBlocks[block].transactions[index];
|
||||
if (next.rate >= (nextTx.rate || (nextTx.fee / nextTx.vsize))) {
|
||||
for (const pool of next.acceleration.pools) {
|
||||
if (pools[pool].vsize + next.vsize <= 999_000) {
|
||||
pools[pool].vsize += next.vsize;
|
||||
} else {
|
||||
pools[pool].block++;
|
||||
pools[pool].vsize = next.vsize;
|
||||
}
|
||||
// insert the acceleration into matching pool's blocks
|
||||
if (pools[pool].complete && mempoolCache[next.acceleration.txid]?.position !== undefined) {
|
||||
accelerationPositions[next.acceleration.txid].push({
|
||||
...mempoolCache[next.acceleration.txid].position as { block: number, vsize: number },
|
||||
poolId: pool,
|
||||
pool: pools[pool].name
|
||||
});
|
||||
} else {
|
||||
accelerationPositions[next.acceleration.txid].push({
|
||||
poolId: pool,
|
||||
pool: pools[pool].name,
|
||||
block: pools[pool].block,
|
||||
vsize: pools[pool].vsize - (next.vsize / 2),
|
||||
});
|
||||
}
|
||||
// and any accelerated ancestors
|
||||
for (const ancestor of mempoolCache[next.acceleration.txid].ancestors || []) {
|
||||
if (pools[pool].complete && mempoolCache[ancestor.txid]?.position !== undefined) {
|
||||
accelerationPositions[ancestor.txid].push({
|
||||
...mempoolCache[ancestor.txid].position as { block: number, vsize: number },
|
||||
poolId: pool,
|
||||
pool: pools[pool].name,
|
||||
});
|
||||
} else {
|
||||
accelerationPositions[ancestor.txid].push({
|
||||
poolId: pool,
|
||||
pool: pools[pool].name,
|
||||
block: pools[pool].block,
|
||||
vsize: pools[pool].vsize - (next.vsize / 2),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
next = accQueue.pop();
|
||||
} else {
|
||||
// skip accelerated transactions and their CPFP ancestors
|
||||
if (accelerationPositions[nextTx.txid] == null) {
|
||||
// insert into all pools' blocks
|
||||
for (const pool of Object.keys(pools)) {
|
||||
if (pools[pool].vsize + nextTx.vsize <= 999_000) {
|
||||
pools[pool].vsize += nextTx.vsize;
|
||||
} else {
|
||||
pools[pool].block++;
|
||||
pools[pool].vsize = nextTx.vsize;
|
||||
}
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
block++;
|
||||
index = 0;
|
||||
}
|
||||
mempool.setAccelerationPositions(accelerationPositions);
|
||||
}
|
||||
}
|
||||
|
||||
export default new MempoolBlocks();
|
||||
|
@ -27,6 +27,7 @@ class Mempool {
|
||||
deletedTransactions: MempoolTransactionExtended[], accelerationDelta: string[], candidates?: GbtCandidates) => Promise<void>) | undefined;
|
||||
|
||||
private accelerations: { [txId: string]: Acceleration } = {};
|
||||
private accelerationPositions: { [txid: string]: { poolId: number, pool: string, block: number, vsize: number }[] } = {};
|
||||
|
||||
private txPerSecondArray: number[] = [];
|
||||
private txPerSecond: number = 0;
|
||||
@ -514,6 +515,14 @@ class Mempool {
|
||||
}
|
||||
}
|
||||
|
||||
setAccelerationPositions(positions: { [txid: string]: { poolId: number, pool: string, block: number, vsize: number }[] }): void {
|
||||
this.accelerationPositions = positions;
|
||||
}
|
||||
|
||||
getAccelerationPositions(txid: string): { [pool: number]: { poolId: number, pool: string, block: number, vsize: number } } | undefined {
|
||||
return this.accelerationPositions[txid];
|
||||
}
|
||||
|
||||
private startTimer() {
|
||||
const state: any = {
|
||||
start: Date.now(),
|
||||
|
@ -10,6 +10,12 @@ export interface Acceleration {
|
||||
effectiveFee: number,
|
||||
feeDelta: number,
|
||||
pools: number[],
|
||||
positions?: {
|
||||
[pool: number]: {
|
||||
block: number,
|
||||
vbytes: number,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export interface AccelerationHistory {
|
||||
|
@ -206,7 +206,8 @@ class WebsocketHandler {
|
||||
}
|
||||
response['txPosition'] = JSON.stringify({
|
||||
txid: trackTxid,
|
||||
position
|
||||
position,
|
||||
accelerationPositions: memPool.getAccelerationPositions(tx.txid),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@ -821,7 +822,8 @@ class WebsocketHandler {
|
||||
...mempoolTx.position,
|
||||
accelerated: mempoolTx.acceleration || undefined,
|
||||
acceleratedBy: mempoolTx.acceleratedBy || undefined,
|
||||
}
|
||||
},
|
||||
accelerationPositions: memPool.getAccelerationPositions(mempoolTx.txid),
|
||||
};
|
||||
if (!mempoolTx.cpfpChecked && !mempoolTx.acceleration) {
|
||||
calculateCpfp(mempoolTx, newMempool);
|
||||
@ -834,7 +836,7 @@ class WebsocketHandler {
|
||||
effectiveFeePerVsize: mempoolTx.effectiveFeePerVsize || null,
|
||||
sigops: mempoolTx.sigops,
|
||||
adjustedVsize: mempoolTx.adjustedVsize,
|
||||
acceleration: mempoolTx.acceleration
|
||||
acceleration: mempoolTx.acceleration,
|
||||
};
|
||||
}
|
||||
response['txPosition'] = JSON.stringify(positionData);
|
||||
@ -1137,7 +1139,8 @@ class WebsocketHandler {
|
||||
...mempoolTx.position,
|
||||
accelerated: mempoolTx.acceleration || undefined,
|
||||
acceleratedBy: mempoolTx.acceleratedBy || undefined,
|
||||
}
|
||||
},
|
||||
accelerationPositions: memPool.getAccelerationPositions(mempoolTx.txid),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user