Skip to content

Commit b6775e5

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 d6d6ffe commit b6775e5

File tree

2 files changed

+544
-34
lines changed

2 files changed

+544
-34
lines changed

docs/handler-spec.md

Lines changed: 178 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Many operations return objects (contexts, blocks, chains, etc.) that must persis
8282

8383
**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.
8484

85-
## Test Suites and Expected Responses
85+
## Test Suites Overview
8686

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

@@ -91,44 +91,188 @@ The conformance tests are organized into suites, each testing a specific aspect
9191

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

94-
**Method:** `btck_script_pubkey_verify`
95-
96-
**Expected Response Format:**
97-
```json
98-
{
99-
"result": true
100-
}
101-
```
102-
or
103-
```json
104-
{
105-
"result": false
106-
}
107-
```
108-
10994
### Script Verification Error Cases
11095
**File:** [`script_verify_errors.json`](../testdata/script_verify_errors.json)
11196

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

114-
**Method:** `btck_script_pubkey_verify`
99+
### Chain Operations
100+
**File:** [`chain.json`](../testdata/chain.json)
115101

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

129-
**Error Members:**
274+
**Result:** Boolean - true if script is valid, false if invalid
130275

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

0 commit comments

Comments
 (0)