Skip to content

fix: use burn_block_time in 2.x blocks and block_time after 3.x #2314

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

Merged
merged 7 commits into from
Jul 7, 2025
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
2 changes: 1 addition & 1 deletion src/event-stream/core-node-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export interface CoreNodeBlockMessage {
missed_reward_slots: [];
};
};
block_time: number;
block_time: number | null;
signer_bitvec?: string | null;
signer_signature?: string[];
}
Expand Down
18 changes: 9 additions & 9 deletions src/event-stream/event-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,20 +997,20 @@ export function parseNewBlockMessage(
) {
const counts = newCoreNoreBlockEventCounts();

// Nakamoto blocks now include their own `block_time`, but this will be empty for pre-Nakamoto
// blocks. We'll use the parent burn block timestamp as the receipt date for those. If both are
// blank, there's something wrong with Stacks core.
const block_time = msg.block_time ?? msg.burn_block_time;
if (block_time === undefined || block_time === null) {
throw new Error('Block message has no block_time or burn_block_time');
}

const parsedTxs: CoreNodeParsedTxMessage[] = [];
const blockData: CoreNodeMsgBlockData = {
...msg,
block_time,
};

if (!blockData.block_time) {
// If running in IBD mode, we use the parent burn block timestamp as the receipt date,
// otherwise, use the current timestamp.
const stacksBlockReceiptDate = isEventReplay
? msg.burn_block_time
: Math.round(Date.now() / 1000);
blockData.block_time = stacksBlockReceiptDate;
}

msg.transactions.forEach(item => {
const parsedTx = parseMessageTransaction(chainId, item, blockData, msg.events);
if (parsedTx) {
Expand Down
82 changes: 82 additions & 0 deletions tests/api/block-time.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { ChainID } from '@stacks/transactions';
import { CoreNodeBlockMessage } from '../../src/event-stream/core-node-message';
import { parseNewBlockMessage } from '../../src/event-stream/event-server';

describe('block time tests', () => {
test('takes block_time from block header', () => {
const block: CoreNodeBlockMessage = {
block_time: 1716238792,
block_height: 1,
block_hash: '0x1234',
index_block_hash: '0x5678',
parent_index_block_hash: '0x9abc',
parent_block_hash: '0x1234',
parent_microblock: '0x1234',
parent_microblock_sequence: 0,
parent_burn_block_hash: '0x1234',
parent_burn_block_height: 0,
parent_burn_block_timestamp: 0,
burn_block_time: 1234567890,
burn_block_hash: '0x1234',
burn_block_height: 1,
miner_txid: '0x1234',
events: [],
transactions: [],
matured_miner_rewards: [],
};
const { dbData: parsed } = parseNewBlockMessage(ChainID.Mainnet, block, false);
expect(parsed.block.block_time).toEqual(1716238792); // Takes block_time from block header
});

test('takes burn_block_time from block header when block_time is not present', () => {
const block: CoreNodeBlockMessage = {
block_time: null,
block_height: 1,
block_hash: '0x1234',
index_block_hash: '0x5678',
parent_index_block_hash: '0x9abc',
parent_block_hash: '0x1234',
parent_microblock: '0x1234',
parent_microblock_sequence: 0,
parent_burn_block_hash: '0x1234',
parent_burn_block_height: 0,
parent_burn_block_timestamp: 0,
burn_block_time: 1234567890,
burn_block_hash: '0x1234',
burn_block_height: 1,
miner_txid: '0x1234',
events: [],
transactions: [],
matured_miner_rewards: [],
};
const { dbData: parsed } = parseNewBlockMessage(ChainID.Mainnet, block, false);
expect(parsed.block.block_time).toEqual(1234567890); // Takes burn_block_time from block header
});

test('throws error if block_time and burn_block_time are not present', () => {
// Use `any` to avoid type errors when setting `block_time` and `burn_block_time` to `null`.
const block: any = {
block_time: null,
burn_block_time: null,
block_height: 1,
block_hash: '0x1234',
index_block_hash: '0x5678',
parent_index_block_hash: '0x9abc',
parent_block_hash: '0x1234',
parent_microblock: '0x1234',
parent_microblock_sequence: 0,
parent_burn_block_hash: '0x1234',
parent_burn_block_height: 0,
parent_burn_block_timestamp: 0,
burn_block_hash: '0x1234',
burn_block_height: 1,
miner_txid: '0x1234',
events: [],
transactions: [],
matured_miner_rewards: [],
};
expect(() => parseNewBlockMessage(ChainID.Mainnet, block, false)).toThrow(
'Block message has no block_time or burn_block_time'
);
});
});
32 changes: 26 additions & 6 deletions tests/api/synthetic-stx-txs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { DecodedTxResult, TxPayloadTypeID } from 'stacks-encoding-native-js';
import { CoreNodeBlockMessage } from '../../src/event-stream/core-node-message';
import { parseMessageTransaction } from '../../src/event-stream/reader';
import { CoreNodeMsgBlockData, parseMessageTransaction } from '../../src/event-stream/reader';
import { parseNewBlockMessage } from '../../src/event-stream/event-server';

// Test processing of the psuedo-Stacks transactions, i.e. the ones that
Expand All @@ -20,7 +20,12 @@ describe('synthetic stx txs', () => {
if (!txMsg) {
throw new Error(`Cound not find tx ${txid}`);
}
const parsed = parseMessageTransaction(ChainID.Mainnet, txMsg, blockMsg, blockMsg.events);
const parsed = parseMessageTransaction(
ChainID.Mainnet,
txMsg,
blockMsg as unknown as CoreNodeMsgBlockData,
blockMsg.events
);
if (!parsed) {
throw new Error(`Failed to parse ${txid}`);
}
Expand Down Expand Up @@ -75,7 +80,12 @@ describe('synthetic stx txs', () => {
if (!txMsg) {
throw new Error(`Cound not find tx ${txid}`);
}
const parsed = parseMessageTransaction(ChainID.Mainnet, txMsg, blockMsg, blockMsg.events);
const parsed = parseMessageTransaction(
ChainID.Mainnet,
txMsg,
blockMsg as unknown as CoreNodeMsgBlockData,
blockMsg.events
);
if (!parsed) {
throw new Error(`Failed to parse ${txid}`);
}
Expand Down Expand Up @@ -130,7 +140,12 @@ describe('synthetic stx txs', () => {
if (!txMsg) {
throw new Error(`Cound not find tx ${txid}`);
}
const parsed = parseMessageTransaction(ChainID.Mainnet, txMsg, blockMsg, blockMsg.events);
const parsed = parseMessageTransaction(
ChainID.Mainnet,
txMsg,
blockMsg as unknown as CoreNodeMsgBlockData,
blockMsg.events
);
if (!parsed) {
throw new Error(`Failed to parse ${txid}`);
}
Expand Down Expand Up @@ -234,7 +249,12 @@ describe('synthetic stx txs', () => {
if (!txMsg) {
throw new Error(`Cound not find tx ${txid}`);
}
const parsed = parseMessageTransaction(ChainID.Mainnet, txMsg, blockMsg, blockMsg.events);
const parsed = parseMessageTransaction(
ChainID.Mainnet,
txMsg,
blockMsg as unknown as CoreNodeMsgBlockData,
blockMsg.events
);
if (!parsed) {
throw new Error(`Failed to parse ${txid}`);
}
Expand Down Expand Up @@ -333,7 +353,7 @@ describe('synthetic stx txs', () => {
const parsed = parseMessageTransaction(
ChainID.Mainnet,
payload.txMsg,
payload.blockMsg,
payload.blockMsg as unknown as CoreNodeMsgBlockData,
payload.blockMsg.events
);
let txType: 'contract_call' | 'token_transfer' | null;
Expand Down