Skip to content

Commit 9c6e048

Browse files
authored
Merge pull request #9 from hirosystems/feat/mock-payloads
Mock payloads
2 parents f223740 + bde4666 commit 9c6e048

File tree

10 files changed

+501
-48
lines changed

10 files changed

+501
-48
lines changed

chunk-parser/src/common.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ export class BufferCursor {
110110
return bytes.toString('utf8');
111111
}
112112

113+
readU8LengthPrefixedString(): string {
114+
const length = this.readU8();
115+
const bytes = this.readBytes(length);
116+
return bytes.toString('utf8');
117+
}
118+
113119
}
114120

115121
export class BufferWriter {

chunk-parser/src/signer-message.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,48 @@ function parseBlockPushed(cursor: BufferCursor) {
149149
}
150150

151151
function parseMockProposal(cursor: BufferCursor) {
152-
console.log('MockProposal ignored');
152+
const peerInfo = parseMockPeerInfo(cursor);
153+
const signature = cursor.readBytes(65).toString('hex');
154+
return { peerInfo, signature };
155+
}
156+
157+
// https://github.com/stacks-network/stacks-core/blob/09d920cd1926422a8e3fb76fe4e1b1ef649546b4/libsigner/src/v0/messages.rs#L283
158+
function parseMockPeerInfo(cursor: BufferCursor) {
159+
const burnBlockHeight = cursor.readU64BE();
160+
const stacksTipConsensusHash = cursor.readBytes(20).toString('hex');
161+
const stacksTip = cursor.readBytes(32).toString('hex');
162+
const stacksTipHeight = cursor.readU64BE();
163+
const serverVersion = cursor.readU8LengthPrefixedString();
164+
const poxConsensusHash = cursor.readBytes(20).toString('hex');
165+
const networkId = cursor.readU32BE();
166+
const indexBlockHash = getIndexBlockHash(stacksTip, stacksTipConsensusHash);
167+
return {
168+
burnBlockHeight,
169+
stacksTipConsensusHash,
170+
stacksTip,
171+
stacksTipHeight,
172+
serverVersion,
173+
poxConsensusHash,
174+
networkId,
175+
indexBlockHash,
176+
}
153177
}
154178

155179
function parseMockSignature(cursor: BufferCursor) {
156-
console.log('MockSignature ignored');
180+
const signature = cursor.readBytes(65).toString('hex');
181+
const mockProposal = parseMockProposal(cursor);
182+
const metadata = parseSignerMessageMetadata(cursor);
183+
return {
184+
signature,
185+
mockProposal,
186+
metadata,
187+
}
157188
}
158189

159190
function parseMockBlock(cursor: BufferCursor) {
160-
console.log('MockBlock ignored');
191+
const mockProposal = parseMockProposal(cursor);
192+
const mockSignatures = cursor.readArray(parseMockSignature);
193+
return { mockProposal, mockSignatures };
161194
}
162195

163196
// https://github.com/stacks-network/stacks-core/blob/cd702e7dfba71456e4983cf530d5b174e34507dc/libsigner/src/events.rs#L74
@@ -172,7 +205,7 @@ function parseBlockProposal(cursor: BufferCursor) {
172205
function parseNakamotoBlock(cursor: BufferCursor) {
173206
const header = parseNakamotoBlockHeader(cursor);
174207
const blockHash = getNakamotoBlockHash(header);
175-
const indexBlockHash = getNakamotoIndexBlockHash(blockHash, header.consensusHash);
208+
const indexBlockHash = getIndexBlockHash(blockHash, header.consensusHash);
176209
const tx = cursor.readArray(parseStacksTransaction);
177210
return { blockHash, indexBlockHash, header, tx };
178211
}
@@ -186,7 +219,7 @@ function parseStacksTransaction(cursor: BufferCursor) {
186219
}
187220

188221
// https://github.com/stacks-network/stacks-core/blob/a2dcd4c3ffdb625a6478bb2c0b23836bc9c72f9f/stacks-common/src/types/chainstate.rs#L268-L279
189-
function getNakamotoIndexBlockHash(blockHash: string, consensusHash: string): string {
222+
function getIndexBlockHash(blockHash: string, consensusHash: string): string {
190223
const hasher = crypto.createHash('sha512-256');
191224
hasher.update(Buffer.from(blockHash, 'hex'));
192225
hasher.update(Buffer.from(consensusHash, 'hex'));

migrations/1729684505752_blocks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export function up(pgm: MigrationBuilder): void {
3838
type: 'bytea',
3939
notNull: true,
4040
},
41+
is_nakamoto_block: {
42+
type: 'boolean',
43+
notNull: true,
44+
},
4145
});
4246

4347
pgm.createIndex('blocks', ['block_height']);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
3+
4+
export const shorthands: ColumnDefinitions | undefined = undefined;
5+
6+
export function up(pgm: MigrationBuilder): void {
7+
pgm.createTable('mock_proposals', {
8+
id: {
9+
type: 'bigserial',
10+
primaryKey: true,
11+
},
12+
received_at: {
13+
type: 'timestamptz',
14+
notNull: true,
15+
},
16+
miner_key: {
17+
type: 'bytea',
18+
notNull: true,
19+
},
20+
burn_block_height: {
21+
type: 'integer',
22+
notNull: true,
23+
},
24+
stacks_tip_consensus_hash: {
25+
type: 'bytea',
26+
notNull: true,
27+
},
28+
// AKA block_hash
29+
stacks_tip: {
30+
type: 'bytea',
31+
notNull: true,
32+
},
33+
// AKA block_height
34+
stacks_tip_height: {
35+
type: 'integer',
36+
notNull: true,
37+
},
38+
server_version: {
39+
type: 'text',
40+
notNull: true,
41+
},
42+
pox_consensus_hash: {
43+
type: 'bytea',
44+
notNull: true,
45+
},
46+
network_id: {
47+
type: 'bigint',
48+
notNull: true,
49+
},
50+
index_block_hash: {
51+
type: 'bytea',
52+
notNull: true,
53+
},
54+
});
55+
56+
pgm.createIndex('mock_proposals', ['received_at']);
57+
pgm.createIndex('mock_proposals', ['stacks_tip_height']);
58+
pgm.createIndex('mock_proposals', ['stacks_tip']);
59+
pgm.createIndex('mock_proposals', ['index_block_hash']);
60+
pgm.createIndex('mock_proposals', ['burn_block_height']);
61+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
3+
4+
export const shorthands: ColumnDefinitions | undefined = undefined;
5+
6+
export function up(pgm: MigrationBuilder): void {
7+
pgm.createTable('mock_signatures', {
8+
id: {
9+
type: 'bigserial',
10+
primaryKey: true,
11+
},
12+
received_at: {
13+
type: 'timestamptz',
14+
notNull: true,
15+
},
16+
signer_key: {
17+
type: 'bytea',
18+
notNull: true,
19+
},
20+
signature: {
21+
type: 'bytea',
22+
notNull: true,
23+
},
24+
25+
// Mock proposal fields
26+
burn_block_height: {
27+
type: 'integer',
28+
notNull: true,
29+
},
30+
stacks_tip_consensus_hash: {
31+
type: 'bytea',
32+
notNull: true,
33+
},
34+
// AKA block_hash
35+
stacks_tip: {
36+
type: 'bytea',
37+
notNull: true,
38+
},
39+
// AKA block_height
40+
stacks_tip_height: {
41+
type: 'integer',
42+
notNull: true,
43+
},
44+
server_version: {
45+
type: 'text',
46+
notNull: true,
47+
},
48+
pox_consensus_hash: {
49+
type: 'bytea',
50+
notNull: true,
51+
},
52+
network_id: {
53+
type: 'bigint',
54+
notNull: true,
55+
},
56+
index_block_hash: {
57+
type: 'bytea',
58+
notNull: true,
59+
},
60+
61+
// Metadata fields
62+
metadata_server_version: {
63+
type: 'text',
64+
notNull: true,
65+
},
66+
});
67+
68+
pgm.createIndex('mock_signatures', ['received_at']);
69+
pgm.createIndex('mock_signatures', ['signer_key']);
70+
pgm.createIndex('mock_signatures', ['stacks_tip_height']);
71+
pgm.createIndex('mock_signatures', ['stacks_tip']);
72+
pgm.createIndex('mock_signatures', ['index_block_hash']);
73+
pgm.createIndex('mock_signatures', ['burn_block_height']);
74+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
3+
4+
export const shorthands: ColumnDefinitions | undefined = undefined;
5+
6+
export function up(pgm: MigrationBuilder): void {
7+
pgm.createTable('mock_blocks', {
8+
id: {
9+
type: 'bigserial',
10+
primaryKey: true,
11+
},
12+
received_at: {
13+
type: 'timestamptz',
14+
notNull: true,
15+
},
16+
miner_key: {
17+
type: 'bytea',
18+
notNull: true,
19+
},
20+
signature: {
21+
type: 'bytea',
22+
notNull: true,
23+
},
24+
25+
// Mock proposal fields
26+
burn_block_height: {
27+
type: 'integer',
28+
notNull: true,
29+
},
30+
stacks_tip_consensus_hash: {
31+
type: 'bytea',
32+
notNull: true,
33+
},
34+
// AKA block_hash
35+
stacks_tip: {
36+
type: 'bytea',
37+
notNull: true,
38+
},
39+
// AKA block_height
40+
stacks_tip_height: {
41+
type: 'integer',
42+
notNull: true,
43+
},
44+
server_version: {
45+
type: 'text',
46+
notNull: true,
47+
},
48+
pox_consensus_hash: {
49+
type: 'bytea',
50+
notNull: true,
51+
},
52+
network_id: {
53+
type: 'bigint',
54+
notNull: true,
55+
},
56+
index_block_hash: {
57+
type: 'bytea',
58+
notNull: true,
59+
},
60+
});
61+
62+
pgm.createIndex('mock_blocks', ['received_at']);
63+
pgm.createIndex('mock_blocks', ['stacks_tip_height']);
64+
pgm.createIndex('mock_blocks', ['stacks_tip']);
65+
pgm.createIndex('mock_blocks', ['index_block_hash']);
66+
pgm.createIndex('mock_blocks', ['burn_block_height']);
67+
68+
// Mock block signer signatures
69+
pgm.createTable('mock_block_signer_signatures', {
70+
id: {
71+
type: 'bigserial',
72+
primaryKey: true,
73+
},
74+
signer_key: {
75+
type: 'bytea',
76+
notNull: true,
77+
},
78+
signer_signature: {
79+
type: 'bytea',
80+
notNull: true,
81+
},
82+
// AKA block_hash
83+
stacks_tip: {
84+
type: 'bytea',
85+
notNull: true,
86+
},
87+
// AKA block_height
88+
stacks_tip_height: {
89+
type: 'integer',
90+
notNull: true,
91+
},
92+
index_block_hash: {
93+
type: 'bytea',
94+
notNull: true,
95+
},
96+
});
97+
98+
pgm.createIndex('mock_block_signer_signatures', ['signer_key']);
99+
pgm.createIndex('mock_block_signer_signatures', ['stacks_tip']);
100+
pgm.createIndex('mock_block_signer_signatures', ['stacks_tip_height']);
101+
pgm.createIndex('mock_block_signer_signatures', ['index_block_hash']);
102+
}

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@fastify/swagger": "^7.6.1",
5050
"@fastify/type-provider-typebox": "^3.2.0",
5151
"@hirosystems/api-toolkit": "^1.7.1",
52-
"@hirosystems/chainhook-client": "^2.1.1",
52+
"@hirosystems/chainhook-client": "^2.3.0",
5353
"@sinclair/typebox": "^0.28.17",
5454
"@stacks/transactions": "^6.1.0",
5555
"@types/node": "^20.16.1",

0 commit comments

Comments
 (0)