From 3c72ce74bcb65d6087465247316e07cbe0a28919 Mon Sep 17 00:00:00 2001 From: Rafael Cardenas Date: Wed, 10 Jan 2024 21:28:10 -0600 Subject: [PATCH 1/2] feat: log re-orgs --- src/datastore/common.ts | 59 ++++++++++++------------------ src/datastore/helpers.ts | 64 ++------------------------------- src/datastore/pg-write-store.ts | 35 ++++++++---------- 3 files changed, 40 insertions(+), 118 deletions(-) diff --git a/src/datastore/common.ts b/src/datastore/common.ts index c28dd08623..af08f0b4dc 100644 --- a/src/datastore/common.ts +++ b/src/datastore/common.ts @@ -981,43 +981,30 @@ export interface FaucetRequestQueryResult { occurred_at: string; } +interface ReOrgEntities { + blocks: number; + microblocks: number; + minerRewards: number; + txs: number; + stxLockEvents: number; + stxEvents: number; + ftEvents: number; + nftEvents: number; + pox2Events: number; + pox3Events: number; + pox4Events: number; + contractLogs: number; + smartContracts: number; + names: number; + namespaces: number; + subdomains: number; +} + export interface ReOrgUpdatedEntities { - markedCanonical: { - blocks: number; - microblocks: number; - minerRewards: number; - txs: number; - stxLockEvents: number; - stxEvents: number; - ftEvents: number; - nftEvents: number; - pox2Events: number; - pox3Events: number; - pox4Events: number; - contractLogs: number; - smartContracts: number; - names: number; - namespaces: number; - subdomains: number; - }; - markedNonCanonical: { - blocks: number; - microblocks: number; - minerRewards: number; - txs: number; - stxLockEvents: number; - stxEvents: number; - ftEvents: number; - nftEvents: number; - pox2Events: number; - pox3Events: number; - pox4Events: number; - contractLogs: number; - smartContracts: number; - names: number; - namespaces: number; - subdomains: number; - }; + markedCanonical: ReOrgEntities; + markedNonCanonical: ReOrgEntities; + prunedMempoolTxs: number; + restoredMempoolTxs: number; } export interface TransferQueryResult { diff --git a/src/datastore/helpers.ts b/src/datastore/helpers.ts index db41e4865d..a5aabbaa1e 100644 --- a/src/datastore/helpers.ts +++ b/src/datastore/helpers.ts @@ -1331,67 +1331,7 @@ export function newReOrgUpdatedEntities(): ReOrgUpdatedEntities { namespaces: 0, subdomains: 0, }, + prunedMempoolTxs: 0, + restoredMempoolTxs: 0, }; } - -export function logReorgResultInfo(updatedEntities: ReOrgUpdatedEntities) { - const updates = [ - ['blocks', updatedEntities.markedCanonical.blocks, updatedEntities.markedNonCanonical.blocks], - [ - 'microblocks', - updatedEntities.markedCanonical.microblocks, - updatedEntities.markedNonCanonical.microblocks, - ], - ['txs', updatedEntities.markedCanonical.txs, updatedEntities.markedNonCanonical.txs], - [ - 'miner-rewards', - updatedEntities.markedCanonical.minerRewards, - updatedEntities.markedNonCanonical.minerRewards, - ], - [ - 'stx-lock events', - updatedEntities.markedCanonical.stxLockEvents, - updatedEntities.markedNonCanonical.stxLockEvents, - ], - [ - 'stx-token events', - updatedEntities.markedCanonical.stxEvents, - updatedEntities.markedNonCanonical.stxEvents, - ], - [ - 'non-fungible-token events', - updatedEntities.markedCanonical.nftEvents, - updatedEntities.markedNonCanonical.nftEvents, - ], - [ - 'fungible-token events', - updatedEntities.markedCanonical.ftEvents, - updatedEntities.markedNonCanonical.ftEvents, - ], - [ - 'contract logs', - updatedEntities.markedCanonical.contractLogs, - updatedEntities.markedNonCanonical.contractLogs, - ], - [ - 'smart contracts', - updatedEntities.markedCanonical.smartContracts, - updatedEntities.markedNonCanonical.smartContracts, - ], - ['names', updatedEntities.markedCanonical.names, updatedEntities.markedNonCanonical.names], - [ - 'namespaces', - updatedEntities.markedCanonical.namespaces, - updatedEntities.markedNonCanonical.namespaces, - ], - [ - 'subdomains', - updatedEntities.markedCanonical.subdomains, - updatedEntities.markedNonCanonical.subdomains, - ], - ]; - const markedCanonical = updates.map(e => `${e[1]} ${e[0]}`).join(', '); - logger.debug(`Entities marked as canonical: ${markedCanonical}`); - const markedNonCanonical = updates.map(e => `${e[2]} ${e[0]}`).join(', '); - logger.debug(`Entities marked as non-canonical: ${markedNonCanonical}`); -} diff --git a/src/datastore/pg-write-store.ts b/src/datastore/pg-write-store.ts index 397d6c5da7..29a76ded70 100644 --- a/src/datastore/pg-write-store.ts +++ b/src/datastore/pg-write-store.ts @@ -38,7 +38,6 @@ import { BurnchainRewardInsertValues, TxInsertValues, MempoolTxInsertValues, - MempoolTxQueryResult, SmartContractInsertValues, BnsNameInsertValues, BnsNamespaceInsertValues, @@ -66,17 +65,14 @@ import { setTotalBlockUpdateDataExecutionCost, convertTxQueryResultToDbMempoolTx, markBlockUpdateDataAsNonCanonical, - MEMPOOL_TX_COLUMNS, MICROBLOCK_COLUMNS, parseBlockQueryResult, - parseMempoolTxQueryResult, parseMicroblockQueryResult, parseTxQueryResult, TX_COLUMNS, TX_METADATA_TABLES, validateZonefileHash, newReOrgUpdatedEntities, - logReorgResultInfo, } from './helpers'; import { PgNotifier } from './pg-notifier'; import { MIGRATIONS_DIR, PgStore } from './pg-store'; @@ -2684,12 +2680,16 @@ export class PgWriteStore extends PgStore { false, updatedEntities ); - await this.restoreMempoolTxs(sql, markNonCanonicalResult.txsMarkedNonCanonical); + const restoredMempoolTxs = await this.restoreMempoolTxs( + sql, + markNonCanonicalResult.txsMarkedNonCanonical + ); + updatedEntities.restoredMempoolTxs += restoredMempoolTxs.restoredTxs.length; } - // The canonical microblock tables _must_ be restored _after_ orphaning all other blocks at a given height, - // because there is only 1 row per microblock hash, and both the orphaned blocks at this height and the - // canonical block can be pointed to the same microblocks. + // The canonical microblock tables _must_ be restored _after_ orphaning all other blocks at a + // given height, because there is only 1 row per microblock hash, and both the orphaned blocks + // at this height and the canonical block can be pointed to the same microblocks. const restoredBlock = parseBlockQueryResult(restoredBlockResult[0]); const microCanonicalUpdateResult = await this.updateMicroCanonical(sql, { isCanonical: true, @@ -2712,24 +2712,17 @@ export class PgWriteStore extends PgStore { updatedEntities.markedCanonical.microblocks += microblocksAccepted.size; updatedEntities.markedNonCanonical.microblocks += microblocksOrphaned.size; - microblocksOrphaned.forEach(mb => logger.debug(`Marked microblock as non-canonical: ${mb}`)); - microblocksAccepted.forEach(mb => logger.debug(`Marked microblock as canonical: ${mb}`)); - const markCanonicalResult = await this.markEntitiesCanonical( sql, indexBlockHash, true, updatedEntities ); - const removedTxsResult = await this.pruneMempoolTxs( + const prunedMempoolTxs = await this.pruneMempoolTxs( sql, markCanonicalResult.txsMarkedCanonical ); - if (removedTxsResult.removedTxs.length > 0) { - logger.debug( - `Removed ${removedTxsResult.removedTxs.length} txs from mempool table during reorg handling` - ); - } + updatedEntities.prunedMempoolTxs += prunedMempoolTxs.removedTxs.length; const parentResult = await sql<{ index_block_hash: string }[]>` SELECT index_block_hash FROM blocks @@ -2779,11 +2772,13 @@ export class PgWriteStore extends PgStore { block.parent_index_block_hash }` ); - // This blocks builds off a previously orphaned chain. Restore canonical status for this - // chain. + // This block builds off a previously orphaned chain. Restore canonical status for this chain. if (!parentResult[0].canonical && block.block_height > chainTipHeight) { await this.restoreOrphanedChain(sql, parentResult[0].index_block_hash, updatedEntities); - logReorgResultInfo(updatedEntities); + logger.info( + updatedEntities, + `Re-org resolved. Block ${block.block_height} builds off a previously orphaned chain.` + ); } // Reflect updated transaction totals in `chain_tip` table. const txCountDelta = From fc078d373239f30dffde8ad12dfbdafb4b20ceff Mon Sep 17 00:00:00 2001 From: Rafael Cardenas Date: Wed, 10 Jan 2024 22:20:19 -0600 Subject: [PATCH 2/2] fix: test --- src/tests/datastore-tests.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tests/datastore-tests.ts b/src/tests/datastore-tests.ts index ce2617c73e..7300089606 100644 --- a/src/tests/datastore-tests.ts +++ b/src/tests/datastore-tests.ts @@ -3607,6 +3607,8 @@ describe('postgres datastore', () => { namespaces: 0, subdomains: 0, }, + prunedMempoolTxs: 0, + restoredMempoolTxs: 0, }); const blockQuery1 = await db.getBlock({ hash: block1.block_hash });