Skip to content

Commit

Permalink
feat: include tenure-height in block responses
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Oct 21, 2024
1 parent 68e1f15 commit 1fb128b
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/datastore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface DbBlock {
block_time: number;
signer_bitvec: string | null;
signer_signatures: string[] | null;
tenure_height: number;
}

/** An interface representing the microblock data that can be constructed _only_ from the /new_microblocks payload */
Expand Down Expand Up @@ -863,6 +864,7 @@ export interface BlockQueryResult {
execution_cost_write_length: string;
tx_count: number;
signer_bitvec: string | null;
tenure_height: number | null;
}

export interface MicroblockQueryResult {
Expand Down
3 changes: 2 additions & 1 deletion src/datastore/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ export function parseFaucetRequestQueryResult(result: FaucetRequestQueryResult):
}

export function parseBlockQueryResult(row: BlockQueryResult): DbBlock {
// TODO(mb): is the tx_index preserved between microblocks and committed anchor blocks?
const block: DbBlock = {
block_hash: row.block_hash,
index_block_hash: row.index_block_hash,
Expand All @@ -488,6 +487,8 @@ export function parseBlockQueryResult(row: BlockQueryResult): DbBlock {
tx_count: row.tx_count,
signer_bitvec: row.signer_bitvec,
signer_signatures: null, // this field is not queried from db by default due to size constraints
// If `tenure_height` is not available, but `signer_bitvec` is set we can safely assume it's same as `block_height` (epoch2.x rules)
tenure_height: row.tenure_height ?? (row.signer_bitvec ? -1 : row.block_height),
};
return block;
}
Expand Down
2 changes: 2 additions & 0 deletions src/event-stream/core-node-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ export interface CoreNodeBlockMessage {
pox_v3_unlock_height?: number;
/** Available starting in epoch3, only included in blocks where the pox cycle rewards are first calculated */
cycle_number?: number;
/** AKA `coinbase_height`. In epoch2.x this is the same as `block_height`. In epoch3 this is used to track tenure heights. Only available starting in stacks-core 3.0.0.0.0-rc6 */
tenure_height?: number | null;
/** Available starting in epoch3, only included in blocks where the pox cycle rewards are first calculated */
reward_set?: {
pox_ustx_threshold: string; // "666720000000000"
Expand Down
16 changes: 16 additions & 0 deletions src/event-stream/event-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,21 @@ export function parseNewBlockMessage(
const signerSignatures =
msg.signer_signature?.map(s => (s.startsWith('0x') ? s : '0x' + s)) ?? null;

let tenureHeight: number;
if (typeof msg.tenure_height === 'number') {
tenureHeight = msg.tenure_height;
} else {
if (msg.signer_bitvec) {
logger.warn(
`Nakamoto block ${msg.block_height} has no tenure_height, defaulting to -1. Use stacks-core version 3.0.0.0.0-rc6 or newer!`
);
tenureHeight = -1;
} else {
// `tenure_height` is not available in the block message, but this is an epoch2.x block so we can safely assume it's same as `block_height`
tenureHeight = msg.block_height;
}
}

const dbBlock: DbBlock = {
canonical: true,
block_hash: msg.block_hash,
Expand All @@ -1039,6 +1054,7 @@ export function parseNewBlockMessage(
block_time: blockData.block_time,
signer_bitvec: signerBitvec,
signer_signatures: signerSignatures,
tenure_height: tenureHeight,
};

logger.debug(`Received block ${msg.block_hash} (${msg.block_height}) from node`, dbBlock);
Expand Down
3 changes: 3 additions & 0 deletions tests/api/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('address tests', () => {
parent_microblock_hash: '',
parent_microblock_sequence: 0,
block_height: 1,
tenure_height: 1,
block_time: 39486,
burn_block_time: 39486,
burn_block_hash: '0x1234',
Expand Down Expand Up @@ -1157,6 +1158,7 @@ describe('address tests', () => {
parent_microblock_hash: '',
parent_microblock_sequence: 0,
block_height: 1,
tenure_height: 1,
block_time: 39486,
burn_block_time: 39486,
burn_block_hash: '0x1234',
Expand Down Expand Up @@ -2375,6 +2377,7 @@ describe('address tests', () => {
parent_microblock_hash: '',
parent_microblock_sequence: 0,
block_height: 1,
tenure_height: 1,
block_time: 1594647995,
burn_block_time: 1594647995,
burn_block_hash: '0x1234',
Expand Down
2 changes: 2 additions & 0 deletions tests/api/block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('block tests', () => {
parent_microblock_hash: '',
parent_microblock_sequence: 0,
block_height: 1235,
tenure_height: 1235,
block_time: 1594647996,
burn_block_time: 1594647996,
burn_block_hash: '0x1234',
Expand Down Expand Up @@ -606,6 +607,7 @@ describe('block tests', () => {
parent_microblock_hash: '',
parent_microblock_sequence: 0,
block_height: 1,
tenure_height: 1,
block_time: 39486,
burn_block_time: 39486,
burn_block_hash: '0x1234',
Expand Down
1 change: 1 addition & 0 deletions tests/api/cache-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('cache-control tests', () => {
parent_microblock_hash: '0x00',
parent_microblock_sequence: 0,
block_height: 1,
tenure_height: 1,
block_time: 1594647996,
burn_block_time: 1594647996,
burn_block_hash: '0x1234',
Expand Down
Loading

0 comments on commit 1fb128b

Please sign in to comment.