Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replacing txid when mempool transaction is dropped by fee and r… #2141

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions migrations/1715844926068_add-replacing-txid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable camelcase */
/** @param { import("node-pg-migrate").MigrationBuilder } pgm */

exports.up = pgm => {
pgm.addColumn('mempool_txs', {
replacing_txid: {
type: 'string',
notNull: false,
}
});
};


1 change: 1 addition & 0 deletions src/api/controllers/db-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ function parseDbAbstractMempoolTx(
const abstractMempoolTx: AbstractMempoolTransaction = {
...baseTx,
tx_status: getTxStatusString(dbMempoolTx.status) as MempoolTransactionStatus,
replacing_txid: dbMempoolTx.replacing_txid,
receipt_time: dbMempoolTx.receipt_time,
receipt_time_iso: unixEpochToIso(dbMempoolTx.receipt_time),
};
Expand Down
3 changes: 3 additions & 0 deletions src/api/schemas/entities/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ export const AbstractMempoolTransactionProperties = {
description: 'Status of the transaction',
}
),
replacing_txid: Type.Optional(Type.String({
description: 'The transaction ID used which is used to replace the previous',
})),
receipt_time: Type.Integer({
description:
'A unix timestamp (in seconds) indicating when the transaction broadcast was received by the node.',
Expand Down
4 changes: 4 additions & 0 deletions src/datastore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ export interface DbMempoolFeePriority {
export interface DbMempoolTx extends BaseTx {
pruned: boolean;

replacing_txid?: string

receipt_time: number;

post_conditions: string;
Expand Down Expand Up @@ -893,6 +895,7 @@ export interface MempoolTxQueryResult {
type_id: number;
anchor_mode: number;
status: number;
replacing_txid?: string;
receipt_time: number;
receipt_block_height: number;

Expand Down Expand Up @@ -1235,6 +1238,7 @@ export interface MempoolTxInsertValues {
type_id: DbTxTypeId;
anchor_mode: DbTxAnchorMode;
status: DbTxStatus;
replacing_txid: string | null,
receipt_time: number;
receipt_block_height: number;
post_conditions: PgBytea;
Expand Down
12 changes: 12 additions & 0 deletions src/datastore/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const MEMPOOL_TX_COLUMNS = [
'type_id',
'anchor_mode',
'status',
'replacing_txid',
'receipt_time',
'receipt_block_height',
'post_conditions',
Expand Down Expand Up @@ -300,6 +301,7 @@ export function parseMempoolTxQueryResult(result: MempoolTxQueryResult): DbMempo
type_id: result.type_id as DbTxTypeId,
anchor_mode: result.anchor_mode as DbTxAnchorMode,
status: result.status,
replacing_txid: result.replacing_txid,
receipt_time: result.receipt_time,
post_conditions: result.post_conditions,
fee_rate: BigInt(result.fee_rate),
Expand Down Expand Up @@ -1045,6 +1047,16 @@ export function getTxDbStatus(
}
}

export function getReplacingTx(
new_txid : string | null
): string {
if (new_txid === null){
return '';
} else{
return new_txid;
}
}

/**
* Extract tx-type specific data from a Transaction and into a tx db model.
* @param txData - Transaction data to extract from.
Expand Down
8 changes: 5 additions & 3 deletions src/datastore/pg-write-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,7 @@ export class PgWriteStore extends PgStore {
type_id: tx.type_id,
anchor_mode: tx.anchor_mode,
status: tx.status,
replacing_txid: tx.replacing_txid ?? null,
receipt_time: tx.receipt_time,
receipt_block_height: chainTip.block_height,
post_conditions: tx.post_conditions,
Expand Down Expand Up @@ -1943,6 +1944,7 @@ export class PgWriteStore extends PgStore {
UPDATE mempool_txs
SET pruned = false,
status = ${DbTxStatus.Pending},
replacing_txid = NULL,
receipt_block_height = ${values[0].receipt_block_height},
receipt_time = ${values[0].receipt_time}
WHERE tx_id IN ${sql(values.map(v => v.tx_id))}
Expand Down Expand Up @@ -2069,12 +2071,12 @@ export class PgWriteStore extends PgStore {
}
}

async dropMempoolTxs({ status, txIds }: { status: DbTxStatus; txIds: string[] }): Promise<void> {
async dropMempoolTxs({ status, txIds, replacing_txid }: { status: DbTxStatus; txIds: string[]; replacing_txid: string | null }): Promise<void> {
for (const batch of batchIterate(txIds, INSERT_BATCH_SIZE)) {
const updateResults = await this.sql<{ tx_id: string }[]>`
WITH pruned AS (
UPDATE mempool_txs
SET pruned = TRUE, status = ${status}
SET pruned = TRUE, status = ${status}, replacing_txid = ${replacing_txid}
WHERE tx_id IN ${this.sql(batch)} AND pruned = FALSE
RETURNING tx_id
),
Expand Down Expand Up @@ -2683,7 +2685,7 @@ export class PgWriteStore extends PgStore {
),
restored AS (
UPDATE mempool_txs
SET pruned = false, status = ${DbTxStatus.Pending}
SET pruned = false, status = ${DbTxStatus.Pending}, replacing_txid = NULL
WHERE pruned = true AND tx_id IN (SELECT DISTINCT tx_id FROM affected_mempool_tx_ids)
RETURNING tx_id
),
Expand Down
1 change: 1 addition & 0 deletions src/event-stream/core-node-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ export type CoreNodeDropMempoolTxReasonType =
export interface CoreNodeDropMempoolTxMessage {
dropped_txids: string[];
reason: CoreNodeDropMempoolTxReasonType;
new_txid: string | null;
}

export interface CoreNodeAttachmentMessage {
Expand Down
4 changes: 3 additions & 1 deletion src/event-stream/event-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { PgWriteStore } from '../datastore/pg-write-store';
import {
createDbMempoolTxFromCoreMsg,
createDbTxFromCoreMsg,
getReplacingTx,
getTxDbStatus,
} from '../datastore/helpers';
import { handleBnsImport } from '../import-v1';
Expand Down Expand Up @@ -169,7 +170,8 @@ async function handleDroppedMempoolTxsMessage(
): Promise<void> {
logger.debug(`Received ${msg.dropped_txids.length} dropped mempool txs`);
const dbTxStatus = getTxDbStatus(msg.reason);
await db.dropMempoolTxs({ status: dbTxStatus, txIds: msg.dropped_txids });
const replacing_txid = msg.new_txid;
await db.dropMempoolTxs({ status: dbTxStatus, txIds: msg.dropped_txids, replacing_txid });
}

async function handleMicroblockMessage(
Expand Down
2 changes: 2 additions & 0 deletions tests/api/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,7 @@ describe('address tests', () => {
raw_tx: bufferToHex(Buffer.from('test-raw-mempool-tx')),
type_id: DbTxTypeId.Coinbase,
status: 1,
replacing_txid: '',
post_conditions: '0x01f5',
fee_rate: 1234n,
sponsored: true,
Expand Down Expand Up @@ -2588,6 +2589,7 @@ describe('address tests', () => {
raw_tx: bufferToHex(Buffer.from('test-raw-mempool-tx')),
type_id: DbTxTypeId.Coinbase,
status: 1,
replacing_txid: '',
post_conditions: '0x01f5',
fee_rate: 1234n,
sponsored: true,
Expand Down
2 changes: 1 addition & 1 deletion tests/api/cache-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ describe('cache-control tests', () => {
expect(request3.text).toBe('');

// Drop one tx.
await db.dropMempoolTxs({ status: DbTxStatus.DroppedReplaceByFee, txIds: ['0x1101'] });
await db.dropMempoolTxs({ status: DbTxStatus.DroppedReplaceByFee, txIds: ['0x1101'], replacing_txid: '0x1109' });

// Cache is now a miss.
const request4 = await supertest(api.server)
Expand Down
1 change: 1 addition & 0 deletions tests/api/datastore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3323,6 +3323,7 @@ describe('postgres datastore', () => {
token_transfer_memo: bufferToHex(Buffer.from('hi')),
token_transfer_recipient_address: 'stx-recipient-addr',
status: DbTxStatus.Pending,
replacing_txid: '',
post_conditions: '0x',
fee_rate: 1234n,
sponsored: false,
Expand Down
Loading