-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
360 lines (285 loc) · 17.7 KB
/
Copy pathProgram.cs
File metadata and controls
360 lines (285 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System.Text.Json;
using BitcoinKernel.TestHandler.Handlers;
using BitcoinKernel.TestHandler.Protocol;
namespace BitcoinKernel.TestHandler;
/// <summary>
/// Test handler for Bitcoin Kernel conformance tests.
/// Implements the JSON-based protocol for testing bindings.
/// Reads JSON requests line-by-line from stdin, writes JSON responses to stdout.
/// </summary>
class Program
{
static async Task<int> Main(string[] args)
{
var jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
WriteIndented = false
};
using var dispatcher = new MethodDispatcher();
try
{
string? line;
while ((line = await Console.In.ReadLineAsync()) != null)
{
if (string.IsNullOrWhiteSpace(line))
continue;
Response response;
try
{
var request = JsonSerializer.Deserialize<Request>(line, jsonOptions);
if (request == null)
{
response = new Response { Id = "unknown", Result = null, Error = new ErrorResponse() };
}
else
{
response = Dispatch(request, dispatcher, jsonOptions);
}
}
catch (JsonException)
{
response = new Response { Id = "unknown", Result = null, Error = new ErrorResponse() };
}
var responseJson = JsonSerializer.Serialize(response, jsonOptions);
await Console.Out.WriteLineAsync(responseJson);
await Console.Out.FlushAsync();
}
return 0;
}
catch (Exception ex)
{
await Console.Error.WriteLineAsync($"Fatal error: {ex.Message}");
return 1;
}
}
private static Response Dispatch(Request request, MethodDispatcher dispatcher, JsonSerializerOptions opts)
{
var id = request.Id;
try
{
return request.Method switch
{
// ── Context ──────────────────────────────────────────────────
"btck_context_create" =>
dispatcher.ContextCreate(id, request.Ref,
Deserialize<BtckContextCreateParams>(request.Params, opts)),
"btck_context_destroy" =>
dispatcher.ContextDestroy(id,
Deserialize<BtckContextDestroyParams>(request.Params, opts)),
// ── Chainstate Manager ────────────────────────────────────────
"btck_chainstate_manager_create" =>
dispatcher.ChainstateManagerCreate(id, request.Ref,
Deserialize<BtckChainstateManagerCreateParams>(request.Params, opts)),
"btck_chainstate_manager_get_active_chain" =>
dispatcher.ChainstateManagerGetActiveChain(id, request.Ref,
Deserialize<BtckChainstateManagerGetActiveChainParams>(request.Params, opts)),
"btck_chainstate_manager_process_block" =>
dispatcher.ChainstateManagerProcessBlock(id,
Deserialize<BtckChainstateManagerProcessBlockParams>(request.Params, opts)),
"btck_chainstate_manager_destroy" =>
dispatcher.ChainstateManagerDestroy(id,
Deserialize<BtckChainstateManagerDestroyParams>(request.Params, opts)),
// ── Chain ─────────────────────────────────────────────────────
"btck_chain_get_height" =>
dispatcher.ChainGetHeight(id,
Deserialize<BtckChainGetHeightParams>(request.Params, opts)),
"btck_chain_get_by_height" =>
dispatcher.ChainGetByHeight(id, request.Ref,
Deserialize<BtckChainGetByHeightParams>(request.Params, opts)),
"btck_chain_contains" =>
dispatcher.ChainContains(id,
Deserialize<BtckChainContainsParams>(request.Params, opts)),
// ── Block ─────────────────────────────────────────────────────
"btck_block_create" =>
dispatcher.BlockCreate(id, request.Ref,
Deserialize<BtckBlockCreateParams>(request.Params, opts)),
"btck_block_get_hash" =>
dispatcher.BlockGetHash(id, request.Ref,
Deserialize<BtckBlockRefParams>(request.Params, opts)),
"btck_block_get_header" =>
dispatcher.BlockGetHeader(id, request.Ref,
Deserialize<BtckBlockRefParams>(request.Params, opts)),
"btck_block_copy" =>
dispatcher.BlockCopy(id, request.Ref,
Deserialize<BtckBlockRefParams>(request.Params, opts)),
"btck_block_count_transactions" =>
dispatcher.BlockCountTransactions(id,
Deserialize<BtckBlockRefParams>(request.Params, opts)),
"btck_block_get_transaction_at" =>
dispatcher.BlockGetTransactionAt(id, request.Ref,
Deserialize<BtckBlockGetTransactionAtParams>(request.Params, opts)),
"btck_block_to_bytes" =>
dispatcher.BlockToBytes(id,
Deserialize<BtckBlockRefParams>(request.Params, opts)),
"btck_block_destroy" =>
dispatcher.BlockDestroy(id,
Deserialize<BtckBlockRefParams>(request.Params, opts)),
"btck_block_tree_entry_get_block_hash" =>
dispatcher.BlockTreeEntryGetBlockHash(id, request.Ref,
Deserialize<BtckBlockTreeEntryGetBlockHashParams>(request.Params, opts)),
// ── Block Hash ────────────────────────────────────────────────
"btck_block_hash_create" =>
dispatcher.BlockHashCreate(id, request.Ref,
Deserialize<BtckBlockHashCreateParams>(request.Params, opts)),
"btck_block_hash_to_bytes" =>
dispatcher.BlockHashToBytes(id,
Deserialize<BtckBlockHashRefParams>(request.Params, opts)),
"btck_block_hash_equals" =>
dispatcher.BlockHashEquals(id,
Deserialize<BtckBlockHashEqualsParams>(request.Params, opts)),
"btck_block_hash_copy" =>
dispatcher.BlockHashCopy(id, request.Ref,
Deserialize<BtckBlockHashRefParams>(request.Params, opts)),
"btck_block_hash_destroy" =>
dispatcher.BlockHashDestroy(id,
Deserialize<BtckBlockHashRefParams>(request.Params, opts)),
// ── Block Header ──────────────────────────────────────────────
"btck_block_header_create" =>
dispatcher.BlockHeaderCreate(id, request.Ref,
Deserialize<BtckBlockHeaderCreateParams>(request.Params, opts)),
"btck_block_header_to_bytes" =>
dispatcher.BlockHeaderToBytes(id,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
"btck_block_header_get_hash" =>
dispatcher.BlockHeaderGetHash(id, request.Ref,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
"btck_block_header_get_prev_hash" =>
dispatcher.BlockHeaderGetPrevHash(id, request.Ref,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
"btck_block_header_get_version" =>
dispatcher.BlockHeaderGetVersion(id,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
"btck_block_header_get_timestamp" =>
dispatcher.BlockHeaderGetTimestamp(id,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
"btck_block_header_get_bits" =>
dispatcher.BlockHeaderGetBits(id,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
"btck_block_header_get_nonce" =>
dispatcher.BlockHeaderGetNonce(id,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
"btck_block_header_copy" =>
dispatcher.BlockHeaderCopy(id, request.Ref,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
"btck_block_header_destroy" =>
dispatcher.BlockHeaderDestroy(id,
Deserialize<BtckBlockHeaderRefParams>(request.Params, opts)),
// ── Script Pubkey ─────────────────────────────────────────────
"btck_script_pubkey_create" =>
dispatcher.ScriptPubkeyCreate(id, request.Ref,
Deserialize<BtckScriptPubkeyCreateParams>(request.Params, opts)),
"btck_script_pubkey_destroy" =>
dispatcher.ScriptPubkeyDestroy(id,
Deserialize<BtckScriptPubkeyDestroyParams>(request.Params, opts)),
"btck_script_pubkey_copy" =>
dispatcher.ScriptPubkeyCopy(id, request.Ref,
Deserialize<BtckScriptPubkeyRefParams>(request.Params, opts)),
"btck_script_pubkey_to_bytes" =>
dispatcher.ScriptPubkeyToBytes(id,
Deserialize<BtckScriptPubkeyRefParams>(request.Params, opts)),
"btck_script_pubkey_verify" =>
dispatcher.ScriptPubkeyVerify(id,
Deserialize<BtckScriptPubkeyVerifyParams>(request.Params, opts)),
// ── Transaction ───────────────────────────────────────────────
"btck_transaction_create" =>
dispatcher.TransactionCreate(id, request.Ref,
Deserialize<BtckTransactionCreateParams>(request.Params, opts)),
"btck_transaction_destroy" =>
dispatcher.TransactionDestroy(id,
Deserialize<BtckTransactionDestroyParams>(request.Params, opts)),
"btck_transaction_copy" =>
dispatcher.TransactionCopy(id, request.Ref,
Deserialize<BtckTransactionRefParams>(request.Params, opts)),
"btck_transaction_count_inputs" =>
dispatcher.TransactionCountInputs(id,
Deserialize<BtckTransactionRefParams>(request.Params, opts)),
"btck_transaction_count_outputs" =>
dispatcher.TransactionCountOutputs(id,
Deserialize<BtckTransactionRefParams>(request.Params, opts)),
"btck_transaction_get_txid" =>
dispatcher.TransactionGetTxid(id, request.Ref,
Deserialize<BtckTransactionRefParams>(request.Params, opts)),
"btck_transaction_to_bytes" =>
dispatcher.TransactionToBytes(id,
Deserialize<BtckTransactionRefParams>(request.Params, opts)),
"btck_transaction_get_input_at" =>
dispatcher.TransactionGetInputAt(id, request.Ref,
Deserialize<BtckTransactionGetInputAtParams>(request.Params, opts)),
"btck_transaction_get_output_at" =>
dispatcher.TransactionGetOutputAt(id, request.Ref,
Deserialize<BtckTransactionGetOutputAtParams>(request.Params, opts)),
// ── Transaction Input ─────────────────────────────────────────
"btck_transaction_input_get_out_point" =>
dispatcher.TransactionInputGetOutPoint(id, request.Ref,
Deserialize<BtckTransactionInputRefParams>(request.Params, opts)),
"btck_transaction_input_copy" =>
dispatcher.TransactionInputCopy(id, request.Ref,
Deserialize<BtckTransactionInputRefParams>(request.Params, opts)),
"btck_transaction_input_destroy" =>
dispatcher.TransactionInputDestroy(id,
Deserialize<BtckTransactionInputRefParams>(request.Params, opts)),
// ── Transaction Out Point ─────────────────────────────────────
"btck_transaction_out_point_get_index" =>
dispatcher.TransactionOutPointGetIndex(id,
Deserialize<BtckTransactionOutPointRefParams>(request.Params, opts)),
"btck_transaction_out_point_get_txid" =>
dispatcher.TransactionOutPointGetTxid(id, request.Ref,
Deserialize<BtckTransactionOutPointRefParams>(request.Params, opts)),
"btck_transaction_out_point_copy" =>
dispatcher.TransactionOutPointCopy(id, request.Ref,
Deserialize<BtckTransactionOutPointRefParams>(request.Params, opts)),
"btck_transaction_out_point_destroy" =>
dispatcher.TransactionOutPointDestroy(id,
Deserialize<BtckTransactionOutPointRefParams>(request.Params, opts)),
// ── Txid ──────────────────────────────────────────────────────
"btck_txid_to_bytes" =>
dispatcher.TxidToBytes(id,
Deserialize<BtckTxidRefParams>(request.Params, opts)),
"btck_txid_equals" =>
dispatcher.TxidEquals(id,
Deserialize<BtckTxidEqualsParams>(request.Params, opts)),
"btck_txid_copy" =>
dispatcher.TxidCopy(id, request.Ref,
Deserialize<BtckTxidRefParams>(request.Params, opts)),
"btck_txid_destroy" =>
dispatcher.TxidDestroy(id,
Deserialize<BtckTxidRefParams>(request.Params, opts)),
// ── Transaction Output ────────────────────────────────────────
"btck_transaction_output_create" =>
dispatcher.TransactionOutputCreate(id, request.Ref,
Deserialize<BtckTransactionOutputCreateParams>(request.Params, opts)),
"btck_transaction_output_destroy" =>
dispatcher.TransactionOutputDestroy(id,
Deserialize<BtckTransactionOutputDestroyParams>(request.Params, opts)),
"btck_transaction_output_copy" =>
dispatcher.TransactionOutputCopy(id, request.Ref,
Deserialize<BtckTransactionOutputRefParams>(request.Params, opts)),
"btck_transaction_output_get_amount" =>
dispatcher.TransactionOutputGetAmount(id,
Deserialize<BtckTransactionOutputRefParams>(request.Params, opts)),
"btck_transaction_output_get_script_pubkey" =>
dispatcher.TransactionOutputGetScriptPubkey(id, request.Ref,
Deserialize<BtckTransactionOutputRefParams>(request.Params, opts)),
// ── Precomputed Transaction Data ──────────────────────────────
"btck_precomputed_transaction_data_create" =>
dispatcher.PrecomputedTransactionDataCreate(id, request.Ref,
Deserialize<BtckPrecomputedTransactionDataCreateParams>(request.Params, opts)),
"btck_precomputed_transaction_data_destroy" =>
dispatcher.PrecomputedTransactionDataDestroy(id,
Deserialize<BtckPrecomputedTransactionDataDestroyParams>(request.Params, opts)),
// ── Unknown ───────────────────────────────────────────────────
_ => new Response { Id = id, Result = null, Error = new ErrorResponse() }
};
}
catch (Exception)
{
return new Response { Id = id, Result = null, Error = new ErrorResponse() };
}
}
private static T Deserialize<T>(JsonElement? element, JsonSerializerOptions opts) where T : new()
{
if (element == null) return new T();
return JsonSerializer.Deserialize<T>(element.Value, opts) ?? new T();
}
}