Skip to content

Commit ad723e8

Browse files
committed
Add chain test suite and method reference
Adds chain.json test suite demonstrating stateful chain operations including block processing, height tracking, and verifying state after a reorg scenario. 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 f8b236c commit ad723e8

File tree

2 files changed

+454
-37
lines changed

2 files changed

+454
-37
lines changed

docs/handler-spec.md

Lines changed: 177 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Many operations return objects (contexts, blocks, chains, etc.) that must persis
8585

8686
**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.
8787

88-
## Test Suites and Expected Responses
88+
## Test Suites Overview
8989

9090
The conformance tests are organized into suites, each testing a specific aspect of the Bitcoin Kernel bindings. Test files are located in [`../testdata/`](../testdata/).
9191

@@ -94,47 +94,187 @@ The conformance tests are organized into suites, each testing a specific aspect
9494

9595
Test cases where the script verification operation executes successfully and returns a boolean result (true for valid scripts, false for invalid scripts).
9696

97-
**Method:** `btck_script_pubkey_verify`
98-
99-
**Expected Response Format:**
100-
```json
101-
{
102-
"id": "test-id",
103-
"result": true
104-
}
105-
```
106-
or
107-
```json
108-
{
109-
"id": "test-id",
110-
"result": false
111-
}
112-
```
113-
11497
### Script Verification Error Cases
11598
**File:** [`script_verify_errors.json`](../testdata/script_verify_errors.json)
11699

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

119-
**Method:** `btck_script_pubkey_verify`
102+
### Chain Operations
103+
**File:** [`chain.json`](../testdata/chain.json)
120104

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

135-
**Error Members:**
276+
**Result:** Boolean - true if script is valid, false if invalid
136277

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

0 commit comments

Comments
 (0)