Skip to content

Commit 54a8e75

Browse files
committed
Add chain test suite and adapt handler-spec.md
Adds chain.json test suite demonstrating stateful chain operations including block processing, height tracking, and verifying state after a reorg scenario. Documents the ref field and registry pattern in handler-spec.md. Restructures handler-spec.md with a method reference documenting supported operations (context management, chainstate manager, chain operations, block operations, and script verification) with their parameters, return values, and error conditions.
1 parent fe6fb4b commit 54a8e75

File tree

2 files changed

+659
-36
lines changed

2 files changed

+659
-36
lines changed

docs/handler-spec.md

Lines changed: 221 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ Handlers communicate with the test runner via **stdin/stdout**:
1717
{
1818
"id": "unique-request-id",
1919
"method": "method_name",
20-
"params": { /* method-specific parameters */ }
20+
"params": { /* method-specific parameters */ },
21+
"ref": "reference-name"
2122
}
2223
```
2324

2425
**Fields:**
2526
- `id` (string, required): Unique identifier for this request
2627
- `method` (string, required): The operation to perform. Each unique method must be implemented by the handler to exercise the corresponding binding API operation.
2728
- `params` (object, optional): Method-specific parameters
29+
- `ref` (string, optional): Reference name for storing the returned object. Required for methods that return object references (see [Object References and Registry](#object-references-and-registry))
2830

2931
### Response
3032

@@ -41,12 +43,25 @@ Handlers communicate with the test runner via **stdin/stdout**:
4143
```
4244

4345
**Fields:**
44-
- `result` (any, optional): The return value, or `null` for void/nullptr operations. Must be `null` on error
46+
- `result` (any, optional): The return value, or `null` for void/nullptr operations. Must be `null` on error. For methods that return object references, the result is a reference type object (see [Reference Type](#reference-type))
4547
- `error` (object, optional): Error details. Must be `null` on success. An empty object `{}` is used to indicate an error is raised without further details, it is NOT equivalent to `null`
4648
- `code` (object, optional): Error code details
4749
- `type` (string, required): Error type (e.g., "btck_ScriptVerifyStatus")
4850
- `member` (string, required): Specific error member (e.g., "ERROR_INVALID_FLAGS_COMBINATION")
4951

52+
### Reference Type
53+
54+
For methods that return object references, the result is an object containing the reference name:
55+
56+
```json
57+
{
58+
"ref": "reference-name"
59+
}
60+
```
61+
62+
**Fields:**
63+
- `ref` (string, required): The reference name from the request's `ref` field
64+
5065
**Note:** Throughout this protocol, an omitted field is semantically equivalent to `null`.
5166

5267
## Handler Requirements
@@ -56,53 +71,223 @@ Handlers communicate with the test runner via **stdin/stdout**:
5671
3. **Error Handling**: Return error responses for invalid requests or failed operations
5772
4. **Exit Behavior**: Exit cleanly when stdin closes
5873

59-
## Test Suites and Expected Responses
60-
61-
The conformance tests are organized into suites, each testing a specific aspect of the Bitcoin Kernel bindings. Test files are located in [`../testdata/`](../testdata/).
62-
63-
### Script Verification Success Cases
64-
**File:** [`script_verify_success.json`](../testdata/script_verify_success.json)
74+
## Object References and Registry
6575

66-
Test cases where the script verification operation executes successfully and returns a boolean result (true for valid scripts, false for invalid scripts).
76+
Many operations return objects (contexts, blocks, chains, etc.) that must persist across requests. The protocol uses named references and a registry pattern:
6777

68-
**Method:** `btck_script_pubkey_verify`
78+
**Creating Objects**: Methods that return objects require a `ref` field in the request. The handler stores the object in a registry under that name and returns a reference type object containing the reference name.
6979

70-
**Expected Response Format:**
7180
```json
72-
{
73-
"result": true
74-
}
81+
// Request
82+
{"id": "1", "method": "btck_context_create", "params": {...}, "ref": "$ctx1"}
83+
// Response
84+
{"id": "1", "result": {"ref": "$ctx1"}, "error": null}
85+
// Handler action: registry["$ctx1"] = created_context_ptr
7586
```
76-
or
87+
88+
**Using Objects**: When a parameter is marked as `(reference, required)`, the runner passes a reference type object and the handler extracts the reference name to look it up:
89+
7790
```json
78-
{
79-
"result": false
80-
}
91+
// Request
92+
{"id": "2", "method": "btck_chainstate_manager_create", "params": {"context": {"ref": "$ctx1"}}, "ref": "$csm1"}
93+
// Response
94+
{"id": "2", "result": {"ref": "$csm1"}, "error": null}
95+
// Handler action: Extract ref from params.context, look up registry["$ctx1"], create manager, store as registry["$csm1"]
8196
```
8297

98+
**Implementation**: Handlers must maintain a registry (map of reference names to object pointers) throughout their lifetime. Objects remain alive until explicitly destroyed or handler exit.
99+
100+
## Test Suites Overview
101+
102+
The conformance tests are organized into suites, each testing a specific aspect of the Bitcoin Kernel bindings. Test files are located in [`../testdata/`](../testdata/).
103+
104+
### Script Verification Success Cases
105+
**File:** [`script_verify_success.json`](../testdata/script_verify_success.json)
106+
107+
Test cases where the script verification operation executes successfully and returns a boolean result (true for valid scripts, false for invalid scripts).
108+
83109
### Script Verification Error Cases
84110
**File:** [`script_verify_errors.json`](../testdata/script_verify_errors.json)
85111

86112
Test cases where the verification operation fails to determine validity of the script due to bad user input.
87113

88-
**Method:** `btck_script_pubkey_verify`
114+
### Chain Operations
115+
**File:** [`chain.json`](../testdata/chain.json)
89116

90-
**Expected Response Format:**
91-
```json
92-
{
93-
"result": null,
94-
"error": {
95-
"code": {
96-
"type": "btck_ScriptVerifyStatus",
97-
"member": "ERROR_MEMBER_NAME"
98-
}
99-
}
100-
}
101-
```
117+
Sets up blocks, checks chain state, and verifies that the chain tip changes as expected after a reorg scenario.
118+
119+
## Method Reference
120+
121+
Methods are grouped by functional area. Each method documents its parameters, return values, and possible errors.
122+
123+
### Context Management
124+
125+
#### `btck_context_create`
126+
127+
Creates a context with specified chain parameters.
128+
129+
**Parameters:**
130+
- `chain_parameters` (object, required):
131+
- `chain_type` (string, required): Chain type ("btck_ChainType_MAINNET", "btck_ChainType_TESTNET", "btck_ChainType_TESTNET_4", "btck_ChainType_SIGNET", "btck_ChainType_REGTEST")
132+
133+
**Result:** Reference type - Object containing the reference name from the request `ref` field (e.g., `{"ref": "$context"}`)
134+
135+
**Error:** `{}` when operation fails (C API returned null)
136+
137+
---
138+
139+
### Chainstate Manager Operations
140+
141+
#### `btck_chainstate_manager_create`
142+
143+
Creates a chainstate manager from a context.
144+
145+
**Parameters:**
146+
- `context` (reference, required): Context reference from `btck_context_create`
147+
148+
**Result:** Reference type - Object containing the reference name from the request `ref` field (e.g., `{"ref": "$chainstate_manager"}`)
149+
150+
**Error:** `{}` when operation fails (C API returned null)
151+
152+
---
153+
154+
#### `btck_chainstate_manager_get_active_chain`
155+
156+
Retrieves the currently active chain from the chainstate manager.
157+
158+
**Parameters:**
159+
- `chainstate_manager` (reference, required): Chainstate manager reference
160+
161+
**Result:** Reference type - Object containing the reference name from the request `ref` field (e.g., `{"ref": "$chain"}`)
162+
163+
**Error:** `null` (cannot return error)
164+
165+
---
166+
167+
#### `btck_chainstate_manager_process_block`
168+
169+
Processes a block through validation checks, disk storage, and UTXO set validation; successful processing does not indicate block validity.
170+
171+
**Parameters:**
172+
- `chainstate_manager` (reference, required): Chainstate manager reference
173+
- `block` (reference, required): Block reference from `btck_block_create`
174+
175+
**Result:** Object containing:
176+
- `new_block` (boolean): `true` if this block was not processed before, `false` otherwise
177+
178+
**Error:** `{}` when processing fails
179+
180+
---
181+
182+
#### `btck_chainstate_manager_destroy`
183+
184+
Destroys a chainstate manager and frees associated resources.
185+
186+
**Parameters:**
187+
- `chainstate_manager` (reference, required): Chainstate manager reference to destroy
188+
189+
**Result:** `null` (void operation)
190+
191+
**Error:** `null` (cannot return error)
192+
193+
---
194+
195+
### Chain Operations
196+
197+
#### `btck_chain_get_height`
198+
199+
Gets the current height of the active chain.
200+
201+
**Parameters:**
202+
- `chain` (reference, required): Chain reference from `btck_chainstate_manager_get_active_chain`
203+
204+
**Result:** Integer - The chain height (0 = genesis)
205+
206+
**Error:** `null` (cannot return error)
207+
208+
---
209+
210+
#### `btck_chain_get_by_height`
211+
212+
Retrieves a block tree entry at a specific height in the chain.
213+
214+
**Parameters:**
215+
- `chain` (reference, required): Chain reference
216+
- `block_height` (integer, required): Height to query
217+
218+
**Result:** Reference type - Object containing the reference name from the request `ref` field (e.g., `{"ref": "$block_tree_entry"}`)
219+
220+
**Error:** `{}` when height is out of bounds (C API returned null)
221+
222+
---
223+
224+
#### `btck_chain_contains`
225+
226+
Checks whether a block tree entry is part of the active chain.
227+
228+
**Parameters:**
229+
- `chain` (reference, required): Chain reference
230+
- `block_tree_entry` (reference, required): Block tree entry reference to check
231+
232+
**Result:** Boolean - true if block is in the active chain, false otherwise
233+
234+
**Error:** `null` (cannot return error)
235+
236+
---
237+
238+
### Block Operations
239+
240+
#### `btck_block_create`
241+
242+
Creates a block object from raw block data.
243+
244+
**Parameters:**
245+
- `raw_block` (string, required): Hex-encoded raw block data
246+
247+
**Result:** Reference type - Object containing the reference name from the request `ref` field (e.g., `{"ref": "$block"}`)
248+
249+
**Error:** `{}` when operation fails (C API returned null)
250+
251+
---
252+
253+
#### `btck_block_tree_entry_get_block_hash`
254+
255+
Gets the block hash from a block tree entry.
256+
257+
**Parameters:**
258+
- `block_tree_entry` (reference, required): Block tree entry reference from `btck_chain_get_by_height`
259+
260+
**Result:** String - The block hash (hex-encoded, 64 characters)
261+
262+
**Error:** `null` (cannot return error)
263+
264+
---
265+
266+
### Script Verification
267+
268+
#### `btck_script_pubkey_verify`
269+
270+
Verifies a script pubkey against spending conditions.
271+
272+
**Parameters:**
273+
- `script_pubkey` (string): Hex-encoded script pubkey to be spent
274+
- `amount` (number): Amount of the script pubkey's associated output. May be zero if the witness flag is not set
275+
- `tx_to` (string): Hex-encoded transaction spending the script_pubkey
276+
- `input_index` (number): Index of the input in tx_to spending the script_pubkey
277+
- `flags` (array of strings): Script verification flags controlling validation constraints. Valid flags include:
278+
- `btck_ScriptVerificationFlags_P2SH`
279+
- `btck_ScriptVerificationFlags_DERSIG`
280+
- `btck_ScriptVerificationFlags_NULLDUMMY`
281+
- `btck_ScriptVerificationFlags_CHECKLOCKTIMEVERIFY`
282+
- `btck_ScriptVerificationFlags_CHECKSEQUENCEVERIFY`
283+
- `btck_ScriptVerificationFlags_WITNESS`
284+
- `btck_ScriptVerificationFlags_TAPROOT`
285+
- `spent_outputs` (array of objects): Array of outputs spent by the transaction. May be empty if the taproot flag is not set. Each object contains:
286+
- `script_pubkey` (string): Hex-encoded script pubkey of the spent output
287+
- `amount` (number): Amount in satoshis of the spent output
102288

103-
**Error Members:**
289+
**Result:** Boolean - true if script is valid, false if invalid
104290

105-
| Member | Description |
106-
|--------|-------------|
107-
| `ERROR_INVALID_FLAGS_COMBINATION` | Invalid or inconsistent verification flags were provided. This occurs when the supplied `script_verify_flags` combination violates internal consistency rules. |
108-
| `ERROR_SPENT_OUTPUTS_REQUIRED` | Spent outputs are required but were not provided (e.g., for Taproot verification). |
291+
**Error:** On error, returns error code with type `btck_ScriptVerifyStatus` and member can be one of:
292+
- `ERROR_INVALID_FLAGS_COMBINATION` - Invalid or inconsistent verification flags were provided. This occurs when the supplied `script_verify_flags` combination violates internal consistency rules.
293+
- `ERROR_SPENT_OUTPUTS_REQUIRED` - Spent outputs are required but were not provided (e.g., for Taproot verification).

0 commit comments

Comments
 (0)