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

Mock payloads #9

Merged
merged 7 commits into from
Oct 25, 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
6 changes: 6 additions & 0 deletions chunk-parser/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ export class BufferCursor {
return bytes.toString('utf8');
}

readU8LengthPrefixedString(): string {
const length = this.readU8();
const bytes = this.readBytes(length);
return bytes.toString('utf8');
}

}

export class BufferWriter {
Expand Down
43 changes: 38 additions & 5 deletions chunk-parser/src/signer-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,48 @@ function parseBlockPushed(cursor: BufferCursor) {
}

function parseMockProposal(cursor: BufferCursor) {
console.log('MockProposal ignored');
const peerInfo = parseMockPeerInfo(cursor);
const signature = cursor.readBytes(65).toString('hex');
return { peerInfo, signature };
}

// https://github.com/stacks-network/stacks-core/blob/09d920cd1926422a8e3fb76fe4e1b1ef649546b4/libsigner/src/v0/messages.rs#L283
function parseMockPeerInfo(cursor: BufferCursor) {
const burnBlockHeight = cursor.readU64BE();
const stacksTipConsensusHash = cursor.readBytes(20).toString('hex');
const stacksTip = cursor.readBytes(32).toString('hex');
const stacksTipHeight = cursor.readU64BE();
const serverVersion = cursor.readU8LengthPrefixedString();
const poxConsensusHash = cursor.readBytes(20).toString('hex');
const networkId = cursor.readU32BE();
const indexBlockHash = getIndexBlockHash(stacksTip, stacksTipConsensusHash);
return {
burnBlockHeight,
stacksTipConsensusHash,
stacksTip,
stacksTipHeight,
serverVersion,
poxConsensusHash,
networkId,
indexBlockHash,
}
}

function parseMockSignature(cursor: BufferCursor) {
console.log('MockSignature ignored');
const signature = cursor.readBytes(65).toString('hex');
const mockProposal = parseMockProposal(cursor);
const metadata = parseSignerMessageMetadata(cursor);
return {
signature,
mockProposal,
metadata,
}
}

function parseMockBlock(cursor: BufferCursor) {
console.log('MockBlock ignored');
const mockProposal = parseMockProposal(cursor);
const mockSignatures = cursor.readArray(parseMockSignature);
return { mockProposal, mockSignatures };
}

// https://github.com/stacks-network/stacks-core/blob/cd702e7dfba71456e4983cf530d5b174e34507dc/libsigner/src/events.rs#L74
Expand All @@ -172,7 +205,7 @@ function parseBlockProposal(cursor: BufferCursor) {
function parseNakamotoBlock(cursor: BufferCursor) {
const header = parseNakamotoBlockHeader(cursor);
const blockHash = getNakamotoBlockHash(header);
const indexBlockHash = getNakamotoIndexBlockHash(blockHash, header.consensusHash);
const indexBlockHash = getIndexBlockHash(blockHash, header.consensusHash);
const tx = cursor.readArray(parseStacksTransaction);
return { blockHash, indexBlockHash, header, tx };
}
Expand All @@ -186,7 +219,7 @@ function parseStacksTransaction(cursor: BufferCursor) {
}

// https://github.com/stacks-network/stacks-core/blob/a2dcd4c3ffdb625a6478bb2c0b23836bc9c72f9f/stacks-common/src/types/chainstate.rs#L268-L279
function getNakamotoIndexBlockHash(blockHash: string, consensusHash: string): string {
function getIndexBlockHash(blockHash: string, consensusHash: string): string {
const hasher = crypto.createHash('sha512-256');
hasher.update(Buffer.from(blockHash, 'hex'));
hasher.update(Buffer.from(consensusHash, 'hex'));
Expand Down
4 changes: 4 additions & 0 deletions migrations/1729684505752_blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export function up(pgm: MigrationBuilder): void {
type: 'bytea',
notNull: true,
},
is_nakamoto_block: {
type: 'boolean',
notNull: true,
},
});

pgm.createIndex('blocks', ['block_height']);
Expand Down
61 changes: 61 additions & 0 deletions migrations/1729684505758_mock_proposals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';

export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('mock_proposals', {
id: {
type: 'bigserial',
primaryKey: true,
},
received_at: {
type: 'timestamptz',
notNull: true,
},
miner_key: {
type: 'bytea',
notNull: true,
},
burn_block_height: {
type: 'integer',
notNull: true,
},
stacks_tip_consensus_hash: {
type: 'bytea',
notNull: true,
},
// AKA block_hash
stacks_tip: {
type: 'bytea',
notNull: true,
},
// AKA block_height
stacks_tip_height: {
type: 'integer',
notNull: true,
},
server_version: {
type: 'text',
notNull: true,
},
pox_consensus_hash: {
type: 'bytea',
notNull: true,
},
network_id: {
type: 'bigint',
notNull: true,
},
index_block_hash: {
type: 'bytea',
notNull: true,
},
});

pgm.createIndex('mock_proposals', ['received_at']);
pgm.createIndex('mock_proposals', ['stacks_tip_height']);
pgm.createIndex('mock_proposals', ['stacks_tip']);
pgm.createIndex('mock_proposals', ['index_block_hash']);
pgm.createIndex('mock_proposals', ['burn_block_height']);
}
74 changes: 74 additions & 0 deletions migrations/1729684505759_mock_signature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';

export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('mock_signatures', {
id: {
type: 'bigserial',
primaryKey: true,
},
received_at: {
type: 'timestamptz',
notNull: true,
},
signer_key: {
type: 'bytea',
notNull: true,
},
signature: {
type: 'bytea',
notNull: true,
},

// Mock proposal fields
burn_block_height: {
type: 'integer',
notNull: true,
},
stacks_tip_consensus_hash: {
type: 'bytea',
notNull: true,
},
// AKA block_hash
stacks_tip: {
type: 'bytea',
notNull: true,
},
// AKA block_height
stacks_tip_height: {
type: 'integer',
notNull: true,
},
server_version: {
type: 'text',
notNull: true,
},
pox_consensus_hash: {
type: 'bytea',
notNull: true,
},
network_id: {
type: 'bigint',
notNull: true,
},
index_block_hash: {
type: 'bytea',
notNull: true,
},

// Metadata fields
metadata_server_version: {
type: 'text',
notNull: true,
},
});

pgm.createIndex('mock_signatures', ['received_at']);
pgm.createIndex('mock_signatures', ['signer_key']);
pgm.createIndex('mock_signatures', ['stacks_tip_height']);
pgm.createIndex('mock_signatures', ['stacks_tip']);
pgm.createIndex('mock_signatures', ['index_block_hash']);
pgm.createIndex('mock_signatures', ['burn_block_height']);
}
102 changes: 102 additions & 0 deletions migrations/1729684505760_mock_blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';

export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('mock_blocks', {
id: {
type: 'bigserial',
primaryKey: true,
},
received_at: {
type: 'timestamptz',
notNull: true,
},
miner_key: {
type: 'bytea',
notNull: true,
},
signature: {
type: 'bytea',
notNull: true,
},

// Mock proposal fields
burn_block_height: {
type: 'integer',
notNull: true,
},
stacks_tip_consensus_hash: {
type: 'bytea',
notNull: true,
},
// AKA block_hash
stacks_tip: {
type: 'bytea',
notNull: true,
},
// AKA block_height
stacks_tip_height: {
type: 'integer',
notNull: true,
},
server_version: {
type: 'text',
notNull: true,
},
pox_consensus_hash: {
type: 'bytea',
notNull: true,
},
network_id: {
type: 'bigint',
notNull: true,
},
index_block_hash: {
type: 'bytea',
notNull: true,
},
});

pgm.createIndex('mock_blocks', ['received_at']);
pgm.createIndex('mock_blocks', ['stacks_tip_height']);
pgm.createIndex('mock_blocks', ['stacks_tip']);
pgm.createIndex('mock_blocks', ['index_block_hash']);
pgm.createIndex('mock_blocks', ['burn_block_height']);

// Mock block signer signatures
pgm.createTable('mock_block_signer_signatures', {
id: {
type: 'bigserial',
primaryKey: true,
},
signer_key: {
type: 'bytea',
notNull: true,
},
signer_signature: {
type: 'bytea',
notNull: true,
},
// AKA block_hash
stacks_tip: {
type: 'bytea',
notNull: true,
},
// AKA block_height
stacks_tip_height: {
type: 'integer',
notNull: true,
},
index_block_hash: {
type: 'bytea',
notNull: true,
},
});

pgm.createIndex('mock_block_signer_signatures', ['signer_key']);
pgm.createIndex('mock_block_signer_signatures', ['stacks_tip']);
pgm.createIndex('mock_block_signer_signatures', ['stacks_tip_height']);
pgm.createIndex('mock_block_signer_signatures', ['index_block_hash']);
}
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@fastify/swagger": "^7.6.1",
"@fastify/type-provider-typebox": "^3.2.0",
"@hirosystems/api-toolkit": "^1.7.1",
"@hirosystems/chainhook-client": "^2.1.1",
"@hirosystems/chainhook-client": "^2.3.0",
"@sinclair/typebox": "^0.28.17",
"@stacks/transactions": "^6.1.0",
"@types/node": "^20.16.1",
Expand Down
Loading
Loading