Skip to content

Commit 3b502f7

Browse files
authored
fix: log re-orgs at INFO level (#1819)
1 parent 867ed30 commit 3b502f7

File tree

4 files changed

+42
-118
lines changed

4 files changed

+42
-118
lines changed

src/datastore/common.ts

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -981,43 +981,30 @@ export interface FaucetRequestQueryResult {
981981
occurred_at: string;
982982
}
983983

984+
interface ReOrgEntities {
985+
blocks: number;
986+
microblocks: number;
987+
minerRewards: number;
988+
txs: number;
989+
stxLockEvents: number;
990+
stxEvents: number;
991+
ftEvents: number;
992+
nftEvents: number;
993+
pox2Events: number;
994+
pox3Events: number;
995+
pox4Events: number;
996+
contractLogs: number;
997+
smartContracts: number;
998+
names: number;
999+
namespaces: number;
1000+
subdomains: number;
1001+
}
1002+
9841003
export interface ReOrgUpdatedEntities {
985-
markedCanonical: {
986-
blocks: number;
987-
microblocks: number;
988-
minerRewards: number;
989-
txs: number;
990-
stxLockEvents: number;
991-
stxEvents: number;
992-
ftEvents: number;
993-
nftEvents: number;
994-
pox2Events: number;
995-
pox3Events: number;
996-
pox4Events: number;
997-
contractLogs: number;
998-
smartContracts: number;
999-
names: number;
1000-
namespaces: number;
1001-
subdomains: number;
1002-
};
1003-
markedNonCanonical: {
1004-
blocks: number;
1005-
microblocks: number;
1006-
minerRewards: number;
1007-
txs: number;
1008-
stxLockEvents: number;
1009-
stxEvents: number;
1010-
ftEvents: number;
1011-
nftEvents: number;
1012-
pox2Events: number;
1013-
pox3Events: number;
1014-
pox4Events: number;
1015-
contractLogs: number;
1016-
smartContracts: number;
1017-
names: number;
1018-
namespaces: number;
1019-
subdomains: number;
1020-
};
1004+
markedCanonical: ReOrgEntities;
1005+
markedNonCanonical: ReOrgEntities;
1006+
prunedMempoolTxs: number;
1007+
restoredMempoolTxs: number;
10211008
}
10221009

10231010
export interface TransferQueryResult {

src/datastore/helpers.ts

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,67 +1331,7 @@ export function newReOrgUpdatedEntities(): ReOrgUpdatedEntities {
13311331
namespaces: 0,
13321332
subdomains: 0,
13331333
},
1334+
prunedMempoolTxs: 0,
1335+
restoredMempoolTxs: 0,
13341336
};
13351337
}
1336-
1337-
export function logReorgResultInfo(updatedEntities: ReOrgUpdatedEntities) {
1338-
const updates = [
1339-
['blocks', updatedEntities.markedCanonical.blocks, updatedEntities.markedNonCanonical.blocks],
1340-
[
1341-
'microblocks',
1342-
updatedEntities.markedCanonical.microblocks,
1343-
updatedEntities.markedNonCanonical.microblocks,
1344-
],
1345-
['txs', updatedEntities.markedCanonical.txs, updatedEntities.markedNonCanonical.txs],
1346-
[
1347-
'miner-rewards',
1348-
updatedEntities.markedCanonical.minerRewards,
1349-
updatedEntities.markedNonCanonical.minerRewards,
1350-
],
1351-
[
1352-
'stx-lock events',
1353-
updatedEntities.markedCanonical.stxLockEvents,
1354-
updatedEntities.markedNonCanonical.stxLockEvents,
1355-
],
1356-
[
1357-
'stx-token events',
1358-
updatedEntities.markedCanonical.stxEvents,
1359-
updatedEntities.markedNonCanonical.stxEvents,
1360-
],
1361-
[
1362-
'non-fungible-token events',
1363-
updatedEntities.markedCanonical.nftEvents,
1364-
updatedEntities.markedNonCanonical.nftEvents,
1365-
],
1366-
[
1367-
'fungible-token events',
1368-
updatedEntities.markedCanonical.ftEvents,
1369-
updatedEntities.markedNonCanonical.ftEvents,
1370-
],
1371-
[
1372-
'contract logs',
1373-
updatedEntities.markedCanonical.contractLogs,
1374-
updatedEntities.markedNonCanonical.contractLogs,
1375-
],
1376-
[
1377-
'smart contracts',
1378-
updatedEntities.markedCanonical.smartContracts,
1379-
updatedEntities.markedNonCanonical.smartContracts,
1380-
],
1381-
['names', updatedEntities.markedCanonical.names, updatedEntities.markedNonCanonical.names],
1382-
[
1383-
'namespaces',
1384-
updatedEntities.markedCanonical.namespaces,
1385-
updatedEntities.markedNonCanonical.namespaces,
1386-
],
1387-
[
1388-
'subdomains',
1389-
updatedEntities.markedCanonical.subdomains,
1390-
updatedEntities.markedNonCanonical.subdomains,
1391-
],
1392-
];
1393-
const markedCanonical = updates.map(e => `${e[1]} ${e[0]}`).join(', ');
1394-
logger.debug(`Entities marked as canonical: ${markedCanonical}`);
1395-
const markedNonCanonical = updates.map(e => `${e[2]} ${e[0]}`).join(', ');
1396-
logger.debug(`Entities marked as non-canonical: ${markedNonCanonical}`);
1397-
}

src/datastore/pg-write-store.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import {
3838
BurnchainRewardInsertValues,
3939
TxInsertValues,
4040
MempoolTxInsertValues,
41-
MempoolTxQueryResult,
4241
SmartContractInsertValues,
4342
BnsNameInsertValues,
4443
BnsNamespaceInsertValues,
@@ -66,17 +65,14 @@ import {
6665
setTotalBlockUpdateDataExecutionCost,
6766
convertTxQueryResultToDbMempoolTx,
6867
markBlockUpdateDataAsNonCanonical,
69-
MEMPOOL_TX_COLUMNS,
7068
MICROBLOCK_COLUMNS,
7169
parseBlockQueryResult,
72-
parseMempoolTxQueryResult,
7370
parseMicroblockQueryResult,
7471
parseTxQueryResult,
7572
TX_COLUMNS,
7673
TX_METADATA_TABLES,
7774
validateZonefileHash,
7875
newReOrgUpdatedEntities,
79-
logReorgResultInfo,
8076
} from './helpers';
8177
import { PgNotifier } from './pg-notifier';
8278
import { MIGRATIONS_DIR, PgStore } from './pg-store';
@@ -2684,12 +2680,16 @@ export class PgWriteStore extends PgStore {
26842680
false,
26852681
updatedEntities
26862682
);
2687-
await this.restoreMempoolTxs(sql, markNonCanonicalResult.txsMarkedNonCanonical);
2683+
const restoredMempoolTxs = await this.restoreMempoolTxs(
2684+
sql,
2685+
markNonCanonicalResult.txsMarkedNonCanonical
2686+
);
2687+
updatedEntities.restoredMempoolTxs += restoredMempoolTxs.restoredTxs.length;
26882688
}
26892689

2690-
// The canonical microblock tables _must_ be restored _after_ orphaning all other blocks at a given height,
2691-
// because there is only 1 row per microblock hash, and both the orphaned blocks at this height and the
2692-
// canonical block can be pointed to the same microblocks.
2690+
// The canonical microblock tables _must_ be restored _after_ orphaning all other blocks at a
2691+
// given height, because there is only 1 row per microblock hash, and both the orphaned blocks
2692+
// at this height and the canonical block can be pointed to the same microblocks.
26932693
const restoredBlock = parseBlockQueryResult(restoredBlockResult[0]);
26942694
const microCanonicalUpdateResult = await this.updateMicroCanonical(sql, {
26952695
isCanonical: true,
@@ -2712,24 +2712,17 @@ export class PgWriteStore extends PgStore {
27122712
updatedEntities.markedCanonical.microblocks += microblocksAccepted.size;
27132713
updatedEntities.markedNonCanonical.microblocks += microblocksOrphaned.size;
27142714

2715-
microblocksOrphaned.forEach(mb => logger.debug(`Marked microblock as non-canonical: ${mb}`));
2716-
microblocksAccepted.forEach(mb => logger.debug(`Marked microblock as canonical: ${mb}`));
2717-
27182715
const markCanonicalResult = await this.markEntitiesCanonical(
27192716
sql,
27202717
indexBlockHash,
27212718
true,
27222719
updatedEntities
27232720
);
2724-
const removedTxsResult = await this.pruneMempoolTxs(
2721+
const prunedMempoolTxs = await this.pruneMempoolTxs(
27252722
sql,
27262723
markCanonicalResult.txsMarkedCanonical
27272724
);
2728-
if (removedTxsResult.removedTxs.length > 0) {
2729-
logger.debug(
2730-
`Removed ${removedTxsResult.removedTxs.length} txs from mempool table during reorg handling`
2731-
);
2732-
}
2725+
updatedEntities.prunedMempoolTxs += prunedMempoolTxs.removedTxs.length;
27332726
const parentResult = await sql<{ index_block_hash: string }[]>`
27342727
SELECT index_block_hash
27352728
FROM blocks
@@ -2779,11 +2772,13 @@ export class PgWriteStore extends PgStore {
27792772
block.parent_index_block_hash
27802773
}`
27812774
);
2782-
// This blocks builds off a previously orphaned chain. Restore canonical status for this
2783-
// chain.
2775+
// This block builds off a previously orphaned chain. Restore canonical status for this chain.
27842776
if (!parentResult[0].canonical && block.block_height > chainTipHeight) {
27852777
await this.restoreOrphanedChain(sql, parentResult[0].index_block_hash, updatedEntities);
2786-
logReorgResultInfo(updatedEntities);
2778+
logger.info(
2779+
updatedEntities,
2780+
`Re-org resolved. Block ${block.block_height} builds off a previously orphaned chain.`
2781+
);
27872782
}
27882783
// Reflect updated transaction totals in `chain_tip` table.
27892784
const txCountDelta =

src/tests/datastore-tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3607,6 +3607,8 @@ describe('postgres datastore', () => {
36073607
namespaces: 0,
36083608
subdomains: 0,
36093609
},
3610+
prunedMempoolTxs: 0,
3611+
restoredMempoolTxs: 0,
36103612
});
36113613

36123614
const blockQuery1 = await db.getBlock({ hash: block1.block_hash });

0 commit comments

Comments
 (0)