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

fix: log re-orgs at INFO level #1819

Merged
merged 2 commits into from
Jan 11, 2024
Merged
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
59 changes: 23 additions & 36 deletions src/datastore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
64 changes: 2 additions & 62 deletions src/datastore/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
35 changes: 15 additions & 20 deletions src/datastore/pg-write-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
BurnchainRewardInsertValues,
TxInsertValues,
MempoolTxInsertValues,
MempoolTxQueryResult,
SmartContractInsertValues,
BnsNameInsertValues,
BnsNamespaceInsertValues,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
2 changes: 2 additions & 0 deletions src/tests/datastore-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3607,6 +3607,8 @@ describe('postgres datastore', () => {
namespaces: 0,
subdomains: 0,
},
prunedMempoolTxs: 0,
restoredMempoolTxs: 0,
});

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