Skip to content

Commit 9c39743

Browse files
fix: log block event counts after processing (#1820)
* fix: log re-orgs at `INFO` level (#1819) * chore(release): 7.7.1 [skip ci] ## [7.7.1](v7.7.0...v7.7.1) (2024-01-11) ### Bug Fixes * log re-orgs at `INFO` level ([#1819](#1819)) ([3b502f7](3b502f7)) * fix: log block event counts after processing * fix: switch breaks --------- Co-authored-by: semantic-release-bot <[email protected]>
1 parent 86dfdb5 commit 9c39743

File tree

7 files changed

+155
-125
lines changed

7 files changed

+155
-125
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## [7.7.1](https://github.com/hirosystems/stacks-blockchain-api/compare/v7.7.0...v7.7.1) (2024-01-11)
2+
3+
4+
### Bug Fixes
5+
6+
* log re-orgs at `INFO` level ([#1819](https://github.com/hirosystems/stacks-blockchain-api/issues/1819)) ([3b502f7](https://github.com/hirosystems/stacks-blockchain-api/commit/3b502f73149c185265fc8948e75ba064892ce6d2))
7+
18
## [7.7.0](https://github.com/hirosystems/stacks-blockchain-api/compare/v7.6.0...v7.7.0) (2024-01-10)
29

310

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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ import {
7575
TX_METADATA_TABLES,
7676
validateZonefileHash,
7777
newReOrgUpdatedEntities,
78-
logReorgResultInfo,
7978
} from './helpers';
8079
import { PgNotifier } from './pg-notifier';
8180
import { MIGRATIONS_DIR, PgStore } from './pg-store';
@@ -2762,12 +2761,16 @@ export class PgWriteStore extends PgStore {
27622761
false,
27632762
updatedEntities
27642763
);
2765-
await this.restoreMempoolTxs(sql, markNonCanonicalResult.txsMarkedNonCanonical);
2764+
const restoredMempoolTxs = await this.restoreMempoolTxs(
2765+
sql,
2766+
markNonCanonicalResult.txsMarkedNonCanonical
2767+
);
2768+
updatedEntities.restoredMempoolTxs += restoredMempoolTxs.restoredTxs.length;
27662769
}
27672770

2768-
// The canonical microblock tables _must_ be restored _after_ orphaning all other blocks at a given height,
2769-
// because there is only 1 row per microblock hash, and both the orphaned blocks at this height and the
2770-
// canonical block can be pointed to the same microblocks.
2771+
// The canonical microblock tables _must_ be restored _after_ orphaning all other blocks at a
2772+
// given height, because there is only 1 row per microblock hash, and both the orphaned blocks
2773+
// at this height and the canonical block can be pointed to the same microblocks.
27712774
const restoredBlock = parseBlockQueryResult(restoredBlockResult[0]);
27722775
const microCanonicalUpdateResult = await this.updateMicroCanonical(sql, {
27732776
isCanonical: true,
@@ -2790,24 +2793,17 @@ export class PgWriteStore extends PgStore {
27902793
updatedEntities.markedCanonical.microblocks += microblocksAccepted.size;
27912794
updatedEntities.markedNonCanonical.microblocks += microblocksOrphaned.size;
27922795

2793-
microblocksOrphaned.forEach(mb => logger.debug(`Marked microblock as non-canonical: ${mb}`));
2794-
microblocksAccepted.forEach(mb => logger.debug(`Marked microblock as canonical: ${mb}`));
2795-
27962796
const markCanonicalResult = await this.markEntitiesCanonical(
27972797
sql,
27982798
indexBlockHash,
27992799
true,
28002800
updatedEntities
28012801
);
2802-
const removedTxsResult = await this.pruneMempoolTxs(
2802+
const prunedMempoolTxs = await this.pruneMempoolTxs(
28032803
sql,
28042804
markCanonicalResult.txsMarkedCanonical
28052805
);
2806-
if (removedTxsResult.removedTxs.length > 0) {
2807-
logger.debug(
2808-
`Removed ${removedTxsResult.removedTxs.length} txs from mempool table during reorg handling`
2809-
);
2810-
}
2806+
updatedEntities.prunedMempoolTxs += prunedMempoolTxs.removedTxs.length;
28112807
const parentResult = await sql<{ index_block_hash: string }[]>`
28122808
SELECT index_block_hash
28132809
FROM blocks
@@ -2857,11 +2853,13 @@ export class PgWriteStore extends PgStore {
28572853
block.parent_index_block_hash
28582854
}`
28592855
);
2860-
// This blocks builds off a previously orphaned chain. Restore canonical status for this
2861-
// chain.
2856+
// This block builds off a previously orphaned chain. Restore canonical status for this chain.
28622857
if (!parentResult[0].canonical && block.block_height > chainTipHeight) {
28632858
await this.restoreOrphanedChain(sql, parentResult[0].index_block_hash, updatedEntities);
2864-
logReorgResultInfo(updatedEntities);
2859+
logger.info(
2860+
updatedEntities,
2861+
`Re-org resolved. Block ${block.block_height} builds off a previously orphaned chain.`
2862+
);
28652863
}
28662864
// Reflect updated transaction totals in `chain_tip` table.
28672865
const txCountDelta =

src/event-stream/event-server.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
CoreNodeMsgBlockData,
4747
parseMicroblocksFromTxs,
4848
isPoxPrintEvent,
49+
newCoreNoreBlockEventCounts,
4950
} from './reader';
5051
import {
5152
decodeTransaction,
@@ -230,6 +231,7 @@ async function handleBlockMessage(
230231
db: PgWriteStore
231232
): Promise<void> {
232233
const ingestionTimer = stopwatch();
234+
const counts = newCoreNoreBlockEventCounts();
233235
const parsedTxs: CoreNodeParsedTxMessage[] = [];
234236
const blockData: CoreNodeMsgBlockData = {
235237
...msg,
@@ -238,8 +240,42 @@ async function handleBlockMessage(
238240
const parsedTx = parseMessageTransaction(chainId, item, blockData, msg.events);
239241
if (parsedTx) {
240242
parsedTxs.push(parsedTx);
243+
counts.tx_total += 1;
244+
switch (parsedTx.parsed_tx.payload.type_id) {
245+
case TxPayloadTypeID.Coinbase:
246+
counts.txs.coinbase += 1;
247+
break;
248+
case TxPayloadTypeID.CoinbaseToAltRecipient:
249+
counts.txs.coinbase_to_alt_recipient += 1;
250+
break;
251+
case TxPayloadTypeID.ContractCall:
252+
counts.txs.contract_call += 1;
253+
break;
254+
case TxPayloadTypeID.NakamotoCoinbase:
255+
counts.txs.nakamoto_coinbase += 1;
256+
break;
257+
case TxPayloadTypeID.PoisonMicroblock:
258+
counts.txs.poison_microblock += 1;
259+
break;
260+
case TxPayloadTypeID.SmartContract:
261+
counts.txs.smart_contract += 1;
262+
break;
263+
case TxPayloadTypeID.TenureChange:
264+
counts.txs.tenure_change += 1;
265+
break;
266+
case TxPayloadTypeID.TokenTransfer:
267+
counts.txs.token_transfer += 1;
268+
break;
269+
case TxPayloadTypeID.VersionedSmartContract:
270+
counts.txs.versioned_smart_contract += 1;
271+
break;
272+
}
241273
}
242274
});
275+
for (const event of msg.events) {
276+
counts.event_total += 1;
277+
counts.events[event.type] += 1;
278+
}
243279

244280
const dbBlock: DbBlock = {
245281
canonical: true,
@@ -281,6 +317,7 @@ async function handleBlockMessage(
281317
tx_fees_streamed_produced: BigInt(minerReward.tx_fees_streamed_produced),
282318
};
283319
dbMinerRewards.push(dbMinerReward);
320+
counts.miner_rewards += 1;
284321
}
285322

286323
logger.debug(`Received ${dbMinerRewards.length} matured miner rewards`);
@@ -304,18 +341,10 @@ async function handleBlockMessage(
304341
index_block_hash: msg.index_block_hash,
305342
block_hash: msg.block_hash,
306343
};
344+
counts.microblocks += 1;
307345
return microblock;
308346
});
309347

310-
parsedTxs.forEach(tx => {
311-
logger.debug(`Received anchor block mined tx: ${tx.core_tx.txid}`);
312-
logger.info('Transaction confirmed', {
313-
txid: tx.core_tx.txid,
314-
in_microblock: tx.microblock_hash != '',
315-
stacks_height: dbBlock.block_height,
316-
});
317-
});
318-
319348
const dbData: DataStoreBlockUpdateData = {
320349
block: dbBlock,
321350
microblocks: dbMicroblocks,
@@ -328,7 +357,10 @@ async function handleBlockMessage(
328357

329358
await db.update(dbData);
330359
const ingestionTime = ingestionTimer.getElapsed();
331-
logger.info(`Ingested block ${msg.block_height} (${msg.block_hash}) in ${ingestionTime}ms`);
360+
logger.info(
361+
counts,
362+
`Ingested block ${msg.block_height} (${msg.block_hash}) in ${ingestionTime}ms`
363+
);
332364
}
333365

334366
function parseDataStoreTxEventData(

src/event-stream/reader.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,67 @@ export function isPoxPrintEvent(event: SmartContractEvent): boolean {
847847
if (event.contract_event.topic !== 'print') return false;
848848
return PoxContractIdentifiers.includes(event.contract_event.contract_identifier);
849849
}
850+
851+
interface CoreNodeBlockEventCounts {
852+
microblocks: number;
853+
tx_total: number;
854+
txs: {
855+
token_transfer: number;
856+
smart_contract: number;
857+
contract_call: number;
858+
poison_microblock: number;
859+
coinbase: number;
860+
coinbase_to_alt_recipient: number;
861+
versioned_smart_contract: number;
862+
tenure_change: number;
863+
nakamoto_coinbase: number;
864+
};
865+
event_total: number;
866+
events: {
867+
contract_event: number;
868+
stx_transfer_event: number;
869+
stx_mint_event: number;
870+
stx_burn_event: number;
871+
stx_lock_event: number;
872+
nft_transfer_event: number;
873+
nft_mint_event: number;
874+
nft_burn_event: number;
875+
ft_transfer_event: number;
876+
ft_mint_event: number;
877+
ft_burn_event: number;
878+
};
879+
miner_rewards: number;
880+
}
881+
882+
export function newCoreNoreBlockEventCounts(): CoreNodeBlockEventCounts {
883+
return {
884+
microblocks: 0,
885+
tx_total: 0,
886+
txs: {
887+
token_transfer: 0,
888+
smart_contract: 0,
889+
contract_call: 0,
890+
poison_microblock: 0,
891+
coinbase: 0,
892+
coinbase_to_alt_recipient: 0,
893+
versioned_smart_contract: 0,
894+
tenure_change: 0,
895+
nakamoto_coinbase: 0,
896+
},
897+
event_total: 0,
898+
events: {
899+
contract_event: 0,
900+
stx_transfer_event: 0,
901+
stx_mint_event: 0,
902+
stx_burn_event: 0,
903+
stx_lock_event: 0,
904+
nft_transfer_event: 0,
905+
nft_mint_event: 0,
906+
nft_burn_event: 0,
907+
ft_transfer_event: 0,
908+
ft_mint_event: 0,
909+
ft_burn_event: 0,
910+
},
911+
miner_rewards: 0,
912+
};
913+
}

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)