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

feat: ingest signer_bitvec #1900

Merged
merged 2 commits into from
Mar 20, 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
12 changes: 12 additions & 0 deletions migrations/1710856121027_stacks_block_signer-bitvec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable camelcase */

/** @param { import("node-pg-migrate").MigrationBuilder } pgm */
exports.up = pgm => {

pgm.addColumn('blocks', {
signer_bitvec: {
type: 'bit varying',
}
});

};
3 changes: 3 additions & 0 deletions src/datastore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface DbBlock {
execution_cost_write_length: number;
tx_count: number;
block_time: number;
signer_bitvec: string | null;
}

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

export interface MicroblockQueryResult {
Expand Down Expand Up @@ -1227,6 +1229,7 @@ export interface BlockInsertValues {
execution_cost_write_count: number;
execution_cost_write_length: number;
tx_count: number;
signer_bitvec: string | null;
}

export interface MicroblockInsertValues {
Expand Down
2 changes: 2 additions & 0 deletions src/datastore/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export const BLOCK_COLUMNS = [
'execution_cost_write_count',
'execution_cost_write_length',
'tx_count',
'signer_bitvec',
];

export const MICROBLOCK_COLUMNS = [
Expand Down Expand Up @@ -473,6 +474,7 @@ export function parseBlockQueryResult(row: BlockQueryResult): DbBlock {
execution_cost_write_count: Number.parseInt(row.execution_cost_write_count),
execution_cost_write_length: Number.parseInt(row.execution_cost_write_length),
tx_count: row.tx_count,
signer_bitvec: row.signer_bitvec,
};
return block;
}
Expand Down
2 changes: 2 additions & 0 deletions src/datastore/pg-write-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ export class PgWriteStore extends PgStore {
execution_cost_write_count: block.execution_cost_write_count,
execution_cost_write_length: block.execution_cost_write_length,
tx_count: block.tx_count,
signer_bitvec: block.signer_bitvec,
};
const result = await sql`
INSERT INTO blocks ${sql(values)}
Expand Down Expand Up @@ -3059,6 +3060,7 @@ export class PgWriteStore extends PgStore {
execution_cost_write_count: block.execution_cost_write_count,
execution_cost_write_length: block.execution_cost_write_length,
tx_count: block.tx_count,
signer_bitvec: block.signer_bitvec,
}));
await sql`
INSERT INTO blocks ${sql(values)}
Expand Down
1 change: 1 addition & 0 deletions src/event-stream/core-node-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ export interface CoreNodeBlockMessage {
};
};
block_time: number;
signer_bitvec?: string | null;
}

export interface CoreNodeParsedTxMessage {
Expand Down
14 changes: 13 additions & 1 deletion src/event-stream/event-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import * as bodyParser from 'body-parser';
import { asyncHandler } from '../api/async-handler';
import PQueue from 'p-queue';
import * as prom from 'prom-client';
import { ChainID, assertNotNullish, getChainIDNetwork, getIbdBlockHeight } from '../helpers';
import {
BitVec,
ChainID,
assertNotNullish,
getChainIDNetwork,
getIbdBlockHeight,
} from '../helpers';
import {
CoreNodeBlockMessage,
CoreNodeEventType,
Expand Down Expand Up @@ -291,6 +297,10 @@ async function handleBlockMessage(
counts.events[event.type] += 1;
}

const signerBitvec = msg.signer_bitvec
? BitVec.consensusDeserializeToString(msg.signer_bitvec)
: null;

const dbBlock: DbBlock = {
canonical: true,
block_hash: msg.block_hash,
Expand All @@ -311,6 +321,7 @@ async function handleBlockMessage(
execution_cost_write_length: 0,
tx_count: msg.transactions.length,
block_time: msg.block_time,
signer_bitvec: signerBitvec,
};

logger.debug(`Received block ${msg.block_hash} (${msg.block_height}) from node`, dbBlock);
Expand Down Expand Up @@ -1148,6 +1159,7 @@ export function parseNewBlockMessage(chainId: ChainID, msg: CoreNodeBlockMessage
execution_cost_write_count: totalCost.execution_cost_write_count,
execution_cost_write_length: totalCost.execution_cost_write_length,
tx_count: msg.transactions.length,
signer_bitvec: msg.signer_bitvec ?? null,
};

const dbMinerRewards: DbMinerReward[] = [];
Expand Down
42 changes: 42 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,45 @@ export function getUintEnvOrDefault(envName: string, defaultValue = 0) {
}
return Number(v);
}

export class BitVec {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this deserializer be moved to your Rust lib? Maybe in another PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, probably another PR if needed. I don't expect this to be a bottleneck though.

bits: boolean[];
constructor(bits: boolean[]) {
this.bits = bits;
}

/**
* Deserialize a bit vector from a bytes in the consensus format:
* - 2 bytes (u16): bit length (how many bits to read from the byte data)
* - 4 bytes (u32): data length (how many remaining bytes to read)
*/
static consensusDeserialize(serializedData: Uint8Array) {
const dataView = new DataView(serializedData.buffer, serializedData.byteOffset);
const bitLen = dataView.getUint16(0);
const dataLen = dataView.getUint32(2);
const bitVecBytes = serializedData.subarray(6, 6 + dataLen);
const bits = Array.from(
{ length: bitLen },
(_, i) => !!(bitVecBytes[i >>> 3] & (128 >> i % 8))
);
return new BitVec(bits);
}

/** Return a base-2 string */
toString() {
return this.bits.map(b => (b ? '1' : '0')).join('');
}

/**
* Deserialize a bit vector from a bytes in the consensus format, and return as a base-2 string
*/
static consensusDeserializeToString(serializedData: Uint8Array | string): string {
const data =
typeof serializedData === 'string'
? Buffer.from(serializedData.replace(/^0x/, ''), 'hex')
: serializedData;
const bitVec = BitVec.consensusDeserialize(data);
const bitVecStr = bitVec.toString();
return bitVecStr;
}
}
1 change: 1 addition & 0 deletions src/test-utils/test-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function testBlock(args?: TestBlockArgs): DbBlock {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
signer_bitvec: null,
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/tests/address-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('address tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
signer_bitvec: null,
};
let indexIdIndex = 0;
const createStxTx = (
Expand Down Expand Up @@ -884,6 +885,7 @@ describe('address tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
signer_bitvec: null,
};

let indexIdIndex = 0;
Expand Down Expand Up @@ -2078,6 +2080,7 @@ describe('address tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
signer_bitvec: null,
};
const txBuilder = await makeContractCall({
contractAddress: 'ST11NJTTKGVT6D1HY4NJRVQWMQM7TVAR091EJ8P2Y',
Expand Down
2 changes: 2 additions & 0 deletions src/tests/block-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('block tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
signer_bitvec: null,
};
await db.updateBlock(client, block);
const tx: DbTxRaw = {
Expand Down Expand Up @@ -532,6 +533,7 @@ describe('block tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
signer_bitvec: null,
};
const dbTx1: DbTxRaw = {
...dbBlock,
Expand Down
1 change: 1 addition & 0 deletions src/tests/cache-control-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe('cache-control tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
tx_count: 1,
signer_bitvec: null,
};
const tx: DbTxRaw = {
tx_id: '0x1234',
Expand Down
Loading
Loading