Skip to content

Commit 8078267

Browse files
authored
fix: parsing new enums in the BlockResponse(ValidationFailed) chunks (#83)
1 parent 1b8d784 commit 8078267

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,27 @@
9797
],
9898
"program": "${workspaceFolder}/testing/csv-to-json.js"
9999
},
100+
{
101+
"type": "node",
102+
"request": "launch",
103+
"name": "Debug Current js File",
104+
"program": "${file}",
105+
"cwd": "${fileDirname}",
106+
"skipFiles": ["<node_internals>/**"],
107+
"console": "integratedTerminal"
108+
},
109+
{
110+
"type": "node",
111+
"request": "launch",
112+
"name": "Debug Current TS File (ts-node)",
113+
"runtimeExecutable": "ts-node",
114+
"args": ["--transpile-only", "${file}"],
115+
"cwd": "${fileDirname}",
116+
"skipFiles": ["<node_internals>/**"],
117+
"console": "integratedTerminal",
118+
"env": {
119+
"TS_NODE_PROJECT": "${workspaceFolder}/tsconfig.json"
120+
}
121+
}
100122
]
101123
}

src/event-stream/msg-parsing.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ function parseSignerMessageMetadata(cursor: BufferCursor) {
351351
return { server_version: cursor.readVecString() };
352352
}
353353

354+
// https://github.com/stacks-network/stacks-core/blob/e45867c1781a7c45541e765cd42b6a084996fec8/libsigner/src/v0/messages.rs#L934
354355
const RejectCodeTypePrefix = {
355356
// The block was rejected due to validation issues
356357
ValidationFailed: 0,
@@ -367,13 +368,41 @@ const RejectCodeTypePrefix = {
367368
};
368369

369370
enum ValidateRejectCode {
370-
BadBlockHash = 0,
371-
BadTransaction = 1,
372-
InvalidBlock = 2,
373-
ChainstateError = 3,
374-
UnknownParent = 4,
375-
NonCanonicalTenure = 5,
376-
NoSuchTenure = 6,
371+
/// The block was rejected due to validation issues
372+
ValidationFailed = 0,
373+
/// The block was rejected due to connectivity issues with the signer
374+
ConnectivityIssues = 1,
375+
/// The block was rejected in a prior round
376+
RejectedInPriorRound = 2,
377+
/// The block was rejected due to no sortition view
378+
NoSortitionView = 3,
379+
/// The block was rejected due to a mismatch with expected sortition view
380+
SortitionViewMismatch = 4,
381+
/// The block was rejected due to a testing directive
382+
TestingDirective = 5,
383+
/// The block attempted to reorg the previous tenure but was not allowed
384+
ReorgNotAllowed = 6,
385+
/// The bitvec field does not match what is expected
386+
InvalidBitvec = 7,
387+
/// The miner's pubkey hash does not match the winning pubkey hash
388+
PubkeyHashMismatch = 8,
389+
/// The miner has been marked as invalid
390+
InvalidMiner = 9,
391+
/// Miner is last sortition winner, when the current sortition winner is
392+
/// still valid
393+
NotLatestSortitionWinner = 10,
394+
/// The block does not confirm the expected parent block
395+
InvalidParentBlock = 11,
396+
/// The block contains a block found tenure change, but we've already seen
397+
/// a block found
398+
DuplicateBlockFound = 12,
399+
/// The block attempted a tenure extend but the burn view has not changed
400+
/// and not enough time has passed for a time-based tenure extend
401+
InvalidTenureExtend = 13,
402+
/// Unknown reject code, for forward compatibility
403+
Unknown = 254,
404+
/// The block was approved, no rejection details needed
405+
NotRejected = 255,
377406
}
378407

379408
// https://github.com/stacks-network/stacks-core/blob/cd702e7dfba71456e4983cf530d5b174e34507dc/libsigner/src/v0/messages.rs#L812

tests/unit/chunk-parsing.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { StackerDbChunk } from '../../src/event-stream/core-node-message';
2+
3+
describe('Chunk parsing tests', () => {
4+
// Jest has an issue with loading the `@noble/secp256k1` library related to esm limitations in jest
5+
test.skip('parse block response with ValidationFailed(PubkeyHashMismatch)', async () => {
6+
const { parseStackerDbChunk } = await import('../../src/event-stream/msg-parsing');
7+
const payload = `{"contract_id":{"issuer":[22,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]],"name":"signers-1-1"},"modified_slots":[{"data":"010100000038426c6f636b206973206e6f7420686967686572207468616e20746865206869676865737420626c6f636b20696e206974732074656e7572650008257441ee3785f859ed3a302f2ee19b4ecb3fc3e494c9f982011d6a49a29b36df0000000100f82c5e8637fe8eaf0cee174700841e921c7c16b6163b3804a780f7cbfe35fa8b79a3a2f22878dd66caff6f2ba891b647991147326bbb10de079759edcda3142a00000055737461636b732d7369676e657220332e312e302e302e392e30202872656c656173652f332e312e302e302e393a636262323264362b2c2072656c65617365206275696c642c206c696e7578205b7838365f36345d29030000000a00000000681bbe160008","sig":"01031fe2a0528a2cea71b5cc23e660b5ccddadb730caee4cc0a8388d6df8c5c8fa52d75d243c840c2f34bbc68878b63e7bbb2a6fb0711a843d37f390d366f6d0b4","slot_id":10,"slot_version":176385}]}`;
8+
const payloadJson: StackerDbChunk = JSON.parse(payload);
9+
const parsed = parseStackerDbChunk(payloadJson);
10+
expect(parsed[0]).toMatchObject({
11+
contract: 'signers-1-1',
12+
pubkey: '025588e24e2bf387fe8cc7bccba1aac7fe599b96724892431e992a40d06e8fe220',
13+
sig: '01031fe2a0528a2cea71b5cc23e660b5ccddadb730caee4cc0a8388d6df8c5c8fa52d75d243c840c2f34bbc68878b63e7bbb2a6fb0711a843d37f390d366f6d0b4',
14+
messageType: 'BlockResponse',
15+
blockResponse: {
16+
type: 'rejected',
17+
reason: 'Block is not higher than the highest block in its tenure',
18+
reasonCode: { rejectCode: 'ValidationFailed', validateRejectCode: 'PubkeyHashMismatch' },
19+
signerSignatureHash: '257441ee3785f859ed3a302f2ee19b4ecb3fc3e494c9f982011d6a49a29b36df',
20+
chainId: 1,
21+
signature:
22+
'00f82c5e8637fe8eaf0cee174700841e921c7c16b6163b3804a780f7cbfe35fa8b79a3a2f22878dd66caff6f2ba891b647991147326bbb10de079759edcda3142a',
23+
metadata: {
24+
server_version:
25+
'stacks-signer 3.1.0.0.9.0 (release/3.1.0.0.9:cbb22d6+, release build, linux [x86_64])',
26+
},
27+
},
28+
});
29+
});
30+
});

0 commit comments

Comments
 (0)