forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspv_tests.c
585 lines (477 loc) · 23.2 KB
/
spv_tests.c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
The MIT License (MIT)
Copyright (c) 2015 Jonas Schnelli
Copyright (c) 2023 bluezr
Copyright (c) 2023-2024 The Dogecoin Foundation
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#ifdef _MSC_VER
#include <stdio.h>
#else
#include <unistd.h>
#endif
#include <test/utest.h>
#include <dogecoin/arith_uint256.h>
#include <dogecoin/block.h>
#include <dogecoin/headersdb_file.h>
#include <dogecoin/net.h>
#include <dogecoin/spv.h>
#include <dogecoin/utils.h>
#include <dogecoin/validation.h>
void test_spv_sync_completed(dogecoin_spv_client* client) {
printf("Sync completed, at height %d\n", client->headers_db->getchaintip(client->headers_db_ctx)->height);
dogecoin_node_group_shutdown(client->nodegroup);
}
dogecoin_bool test_spv_header_message_processed(struct dogecoin_spv_client_ *client, dogecoin_node *node, dogecoin_blockindex *newtip) {
UNUSED(node);
if (newtip) {
printf("New headers tip height %d\n", newtip->height);
if (newtip->height >= 4008284) {
test_spv_sync_completed(client);
}
}
return true;
}
void test_spv()
{
// set chain:
const dogecoin_chainparams* chain = &dogecoin_chainparams_test;
// concatenate chain to prefix of headers database:
char* header_suffix = "_headers.db";
char* header_prefix = (char*)chain->chainname;
char* headersfile = concat(header_prefix, header_suffix);
// unlink newly prefixed headers database:
unlink(headersfile);
// init new spv client with debugging off and syncing to memory:
#ifndef __APPLE__
// due to TBD anomaly in ci environment, enable http server if not running Apple
dogecoin_spv_client* client = dogecoin_spv_client_new(chain, false, true, true, false, 8, "localhost:8888");
#else
dogecoin_spv_client* client = dogecoin_spv_client_new(chain, false, true, true, false, 8, NULL);
#endif
client->header_message_processed = test_spv_header_message_processed;
client->sync_completed = test_spv_sync_completed;
dogecoin_spv_client_load(client, headersfile, false);
dogecoin_free(headersfile);
printf("Discover peers...");
dogecoin_spv_client_discover_peers(client, NULL);
printf("done\n");
printf("Start interacting with the p2p network...\n");
dogecoin_spv_client_runloop(client);
dogecoin_spv_client_free(client);
remove_all_hashes();
remove_all_maps();
}
void test_reorg() {
// Initialize the chain parameters for mainnet
const dogecoin_chainparams* chain = &dogecoin_chainparams_main;
// Setup headers database file path for testing
char* headersfile = "test_headers.db";
// Unlink the headers database file
unlink(headersfile);
// Initialize SPV client
dogecoin_spv_client* client = dogecoin_spv_client_new(chain, false, false, false, false, 8, NULL);
client->header_message_processed = test_spv_header_message_processed;
client->sync_completed = test_spv_sync_completed;
dogecoin_spv_client_load(client, headersfile, false);
// Create headers for the main chain and new chain
dogecoin_block_header* header1 = dogecoin_block_header_new();
dogecoin_block_header* header2 = dogecoin_block_header_new();
dogecoin_block_header* header3 = dogecoin_block_header_new();
dogecoin_block_header* header4 = dogecoin_block_header_new();
dogecoin_block_header* header2_stale = dogecoin_block_header_new();
dogecoin_block_header* header2_fork = dogecoin_block_header_new();
dogecoin_block_header* header3_fork = dogecoin_block_header_new();
dogecoin_block_header* header4_fork = dogecoin_block_header_new();
dogecoin_block_header* header5_fork = dogecoin_block_header_new();
size_t outlen;
// Initialize header1
header1->version = 1; // 1
header1->timestamp = 1386474927; // 1
header1->nonce = 1417875456; // 1
header1->bits = 0x1e0ffff0; // 1
char prevblock_hex1[65] = "1a91e3dace36e2be3bf030a65679fe821aa1d6ef92e7c9902eb318182c355691";
utils_reverse_hex(prevblock_hex1, 64);
utils_hex_to_bin(prevblock_hex1, (uint8_t*) header1->prev_block, 64, &outlen);
char merkleroot_hex1[65] = "5f7e779f7600f54e528686e91d5891f3ae226ee907f461692519e549105f521c";
utils_reverse_hex(merkleroot_hex1, 64);
utils_hex_to_bin(merkleroot_hex1, (uint8_t*) header1->merkle_root, 64, &outlen);
// Initialize header2
header2->version = 1; // 2
header2->timestamp = 1386474933; // 2
header2->nonce = 3404207872; // 2
header2->bits = 0x1e0ffff0; // 2
char prevblock_hex2[65] = "82bc68038f6034c0596b6e313729793a887fded6e92a31fbdf70863f89d9bea2";
utils_reverse_hex(prevblock_hex2, 64);
utils_hex_to_bin(prevblock_hex2, (uint8_t*) header2->prev_block, 64, &outlen);
char merkleroot_hex2[65] = "3b14b76d22a3f2859d73316002bc1b9bfc7f37e2c3393be9b722b62bbd786983";
utils_reverse_hex(merkleroot_hex2, 64);
utils_hex_to_bin(merkleroot_hex2, (uint8_t*) header2->merkle_root, 64, &outlen);
// Initialize header3
header3->version = 1; // 3
header3->timestamp = 1386474940; // 3
header3->nonce = 3785361152; // 3
header3->bits = 0x1e0ffff0; // 2
char prevblock_hex3[65] = "ea5380659e02a68c073369e502125c634b2fb0aaf351b9360c673368c4f20c96";
utils_reverse_hex(prevblock_hex3, 64);
utils_hex_to_bin(prevblock_hex3, (uint8_t*) header3->prev_block, 64, &outlen);
char merkleroot_hex3[65] = "1e10c28574e3b9d7032329b624ce4ac8064d0e91324aa14634aa2da61146ddfd";
utils_reverse_hex(merkleroot_hex3, 64);
utils_hex_to_bin(merkleroot_hex3, (uint8_t*) header3->merkle_root, 64, &outlen);
// Initialize header4
header4->version = 1; // 4
header4->timestamp = 1386474943; // 4
header4->nonce = 151130624; // 4
header4->bits = 0x1e0ffff0; // 2
char prevblock_hex4[65] = "76f80a8a81e6f6669d340651723b874f97395c4dbda200f8b024df4c6566a92c";
utils_reverse_hex(prevblock_hex4, 64);
utils_hex_to_bin(prevblock_hex4, (uint8_t*) header4->prev_block, 64, &outlen);
char merkleroot_hex4[65] = "9f69a09b940fc7645b0a261e81a1f777e3e6514989eaf15bbc66759fa49b70c2";
utils_reverse_hex(merkleroot_hex4, 64);
utils_hex_to_bin(merkleroot_hex4, (uint8_t*) header4->merkle_root, 64, &outlen);
// Initialize header2_stale
header2_stale->version = 1; // 2
header2_stale->timestamp = 1386474933; // 2
header2_stale->nonce = 3404481231; // 2
header2_stale->bits = 0x1e0ffff0; // 2
utils_hex_to_bin(prevblock_hex2, (uint8_t*) header2_stale->prev_block, 64, &outlen);
utils_hex_to_bin(merkleroot_hex2, (uint8_t*) header2_stale->merkle_root, 64, &outlen);
// Initialize header2_fork
header2_fork->version = 1; // 2
header2_fork->timestamp = 1386474933; // 2
header2_fork->nonce = 3406419112; // 2 header2_fork->nonce = 3404481231; // 2
header2_fork->bits = 0x1e0ffef0; // 2
utils_hex_to_bin(prevblock_hex2, (uint8_t*) header2_fork->prev_block, 64, &outlen);
utils_hex_to_bin(merkleroot_hex2, (uint8_t*) header2_fork->merkle_root, 64, &outlen);
// Initialize header3_fork
header3_fork->version = 1;
header3_fork->timestamp = 1386474934; // 2 + 1
header3_fork->nonce = 3407274091; //
header3_fork->bits = 0x1e0ffef0; //
utils_hex_to_bin(merkleroot_hex2, (uint8_t*) header3_fork->merkle_root, 64, &outlen); // merkle is a don't care
// Initialize header4_fork
header4_fork->version = 1;
header4_fork->timestamp = 1386474935; // 2 + 2
header4_fork->nonce = 3414880011; //
header4_fork->bits = 0x1e0ffef0; //
utils_hex_to_bin(merkleroot_hex2, (uint8_t*) header4_fork->merkle_root, 64, &outlen); // merkle is a don't care
// Initialize header5_fork
header5_fork->version = 1;
header5_fork->timestamp = 1386474936; // 2 + 3
header5_fork->nonce = 3420420582; //
header5_fork->bits = 0x1e0ffef0; //
utils_hex_to_bin(merkleroot_hex2, (uint8_t*) header5_fork->merkle_root, 64, &outlen); // merkle is a don't care
// Calculate the chainwork for each header
uint256 chainwork1, chainwork2, chainwork3, chainwork4, chainwork2_stale, chainwork2_fork, chainwork3_fork, chainwork4_fork, chainwork5_fork;
arith_uint256* target1 = init_arith_uint256();
arith_uint256* target2 = init_arith_uint256();
arith_uint256* target3 = init_arith_uint256();
arith_uint256* target4 = init_arith_uint256();
arith_uint256* target2_stale = init_arith_uint256();
arith_uint256* target2_fork = init_arith_uint256();
arith_uint256* target3_fork = init_arith_uint256();
arith_uint256* target4_fork = init_arith_uint256();
arith_uint256* target5_fork = init_arith_uint256();
cstring* s = cstr_new_sz(64);
dogecoin_bool f_negative, f_overflow;
uint256* hash = dogecoin_uint256_vla(1);
// Compute the hash of the block header 1
dogecoin_block_header_serialize(s, header1);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 1
target1 = set_compact(target1, header1->bits, &f_negative, &f_overflow);
check_pow(hash, header1->bits, chain, &chainwork1);
dogecoin_free(target1);
// Compute the hash of the block header 2
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header2);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 2
target2 = set_compact(target2, header2->bits, &f_negative, &f_overflow);
check_pow(hash, header2->bits, chain, &chainwork2);
dogecoin_free(target2);
// Compute the hash of the block header 3
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header3);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 3
target3 = set_compact(target3, header3->bits, &f_negative, &f_overflow);
check_pow(hash, header3->bits, chain, &chainwork3);
dogecoin_free(target3);
// Compute the hash of the block header 4
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header4);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 4
target4 = set_compact(target4, header4->bits, &f_negative, &f_overflow);
check_pow(hash, header4->bits, chain, &chainwork4);
dogecoin_free(target4);
arith_uint256* arith_chainwork2 = init_arith_uint256();
memcpy(arith_chainwork2, &chainwork2, sizeof(arith_uint256));
arith_uint256* arith_chainwork2_stale = init_arith_uint256();
// Mine the stale block header 2
// loop until the chainwork of the stale is equal and the hash passes PoW
while (true) {
// Compute the hash of the block header 2
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header2_stale);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 2
target2_stale = set_compact(target2_stale, header2_stale->bits, &f_negative, &f_overflow);
bool pow_passed = check_pow(hash, header2_stale->bits, chain, &chainwork2_stale);
// Update the arith_uint256 chainwork of the stale
memcpy(arith_chainwork2_stale, &chainwork2_stale, sizeof(uint256));
// Check if the chainwork of the stale is equal and the hash passes PoW
if (arith_uint256_equal(arith_chainwork2_stale, arith_chainwork2) && pow_passed) {
debug_print("Nonce: %u\n", header2_stale->nonce);
debug_print("Hash: %s\n", hash_to_string((uint8_t*) hash));
debug_print("Chainwork: %s\n", hash_to_string((uint8_t*) arith_chainwork2_stale));
break;
}
// Increment the nonce
header2_stale->nonce++;
}
// Free the arith_uint256 chainwork of the stale
dogecoin_free(arith_chainwork2_stale);
// Free the target and hash
dogecoin_free(target2_stale);
arith_uint256* arith_chainwork2_fork = init_arith_uint256();
// Mine the forked block header 2
// loop until the chainwork of the fork is greater and the hash passes PoW
while (true) {
// Compute the hash of the block header 2
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header2_fork);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 2
target2_fork = set_compact(target2_fork, header2_fork->bits, &f_negative, &f_overflow);
bool pow_passed = check_pow(hash, header2_fork->bits, chain, &chainwork2_fork);
// Update the arith_uint256 chainwork of the fork
memcpy(arith_chainwork2_fork, &chainwork2_fork, sizeof(uint256));
// Check if the chainwork of the fork is greater and the hash passes PoW
if (arith_uint256_greater_than(arith_chainwork2_fork, arith_chainwork2) && pow_passed) {
debug_print("Nonce: %u\n", header2_fork->nonce);
debug_print("Hash: %s\n", hash_to_string((uint8_t*) hash));
debug_print("Chainwork: %s\n", hash_to_string((uint8_t*) arith_chainwork2_fork));
break;
}
// Increment the nonce
header2_fork->nonce++;
}
// Free the arith_uint256 chainwork of the fork
dogecoin_free(arith_chainwork2);
dogecoin_free(arith_chainwork2_fork);
// Free the target and hash
dogecoin_free(target2_fork);
// Compute the sha256d hash of the header2_fork
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header2_fork);
dogecoin_block_header_hash(header2_fork, (uint8_t*) hash);
cstr_free(s, true);
// Set header3_fork's previous block to header2_fork's hash
memcpy(&header3_fork->prev_block, hash, DOGECOIN_HASH_LENGTH);
arith_uint256* arith_chainwork3_fork = init_arith_uint256();
// Mine the forked block header 3
// loop until the chainwork of the fork is greater and the hash passes PoW
while (true) {
// Compute the hash of the block header 3
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header3_fork);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 3
target3_fork = set_compact(target3_fork, header3_fork->bits, &f_negative, &f_overflow);
bool pow_passed = check_pow(hash, header3_fork->bits, chain, &chainwork3_fork);
// Update the arith_uint256 chainwork of the fork
memcpy(arith_chainwork3_fork, &chainwork3_fork, sizeof(uint256));
// Check if the hash passes PoW
if (pow_passed) {
debug_print("Nonce: %u\n", header3_fork->nonce);
debug_print("Hash: %s\n", hash_to_string((uint8_t*) hash));
debug_print("Chainwork: %s\n", hash_to_string((uint8_t*) arith_chainwork3_fork));
break;
}
// Increment the nonce
header3_fork->nonce++;
}
// Free the arith_uint256 chainwork of the fork
dogecoin_free(arith_chainwork3_fork);
// Free the target
dogecoin_free(target3_fork);
// Compute the sha256d hash of the header3_fork
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header3_fork);
dogecoin_block_header_hash(header3_fork, (uint8_t*) hash);
cstr_free(s, true);
// Set header4_fork's previous block to header3_fork's hash
memcpy(&header4_fork->prev_block, hash, DOGECOIN_HASH_LENGTH);
arith_uint256* arith_chainwork4_fork = init_arith_uint256();
// Mine the forked block header 4
// loop until the chainwork of the fork is greater and the hash passes PoW
while (true) {
// Compute the hash of the block header 4
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header4_fork);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 4
target4_fork = set_compact(target4_fork, header4_fork->bits, &f_negative, &f_overflow);
bool pow_passed = check_pow(hash, header4_fork->bits, chain, &chainwork4_fork);
// Update the arith_uint256 chainwork of the fork
memcpy(arith_chainwork4_fork, &chainwork4_fork, sizeof(uint256));
// Check if the hash passes PoW
if (pow_passed) {
debug_print("Nonce: %u\n", header4_fork->nonce);
debug_print("Hash: %s\n", hash_to_string((uint8_t*) hash));
debug_print("Chainwork: %s\n", hash_to_string((uint8_t*) arith_chainwork4_fork));
break;
}
// Increment the nonce
header4_fork->nonce++;
}
// Free the arith_uint256 chainwork of the fork
dogecoin_free(arith_chainwork4_fork);
// Free the target
dogecoin_free(target4_fork);
// Compute the sha256d hash of the header4_fork
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header4_fork);
dogecoin_block_header_hash(header4_fork, (uint8_t*) hash);
cstr_free(s, true);
// Set header5_fork's previous block to header4_fork's hash
memcpy(&header5_fork->prev_block, hash, DOGECOIN_HASH_LENGTH);
arith_uint256* arith_chainwork5_fork = init_arith_uint256();
// Mine the forked block header 5
// loop until the chainwork of the fork is greater and the hash passes PoW
while (true) {
// Compute the hash of the block header 5
s = cstr_new_sz(64);
dogecoin_block_header_serialize(s, header5_fork);
dogecoin_block_header_scrypt_hash(s, hash);
cstr_free(s, true);
// Compute the chainwork 5
target5_fork = set_compact(target5_fork, header5_fork->bits, &f_negative, &f_overflow);
bool pow_passed = check_pow(hash, header5_fork->bits, chain, &chainwork5_fork);
// Update the arith_uint256 chainwork of the fork
memcpy(arith_chainwork5_fork, &chainwork5_fork, sizeof(uint256));
// Check if the hash passes PoW
if (pow_passed) {
debug_print("Nonce: %u\n", header5_fork->nonce);
debug_print("Hash: %s\n", hash_to_string((uint8_t*) hash));
debug_print("Chainwork: %s\n", hash_to_string((uint8_t*) arith_chainwork5_fork));
break;
}
// Increment the nonce
header5_fork->nonce++;
}
// Free the arith_uint256 chainwork of the fork
dogecoin_free(arith_chainwork5_fork);
// Free the target
dogecoin_free(target5_fork);
// Free the hash
dogecoin_free(hash);
// Create a cstring for the new block headers
cstring* cbuf_all = cstr_new_sz(80 * 9);
// Serialize header1 into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header1);
// Serialize header2 into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header2);
// Serialize header3 into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header3);
// Serialize header4 into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header4);
// Serialize header5_fork into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header5_fork);
// Serialize header2_stale into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header2_stale);
// Serialize header2_fork into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header2_fork);
// Serialize header3_fork into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header3_fork);
// Serialize header4_fork into cbuf_all
dogecoin_block_header_serialize(cbuf_all, header4_fork);
// Serialize header5_fork into cbuf_all again
dogecoin_block_header_serialize(cbuf_all, header5_fork);
// Serialize header5_fork into cbuf_all yet again (duplicate)
dogecoin_block_header_serialize(cbuf_all, header5_fork);
// Define a constant buffer for each header
struct const_buffer cbuf_header1 = {cbuf_all->str, 80};
struct const_buffer cbuf_header2 = {cbuf_all->str + 80, 80};
struct const_buffer cbuf_header3 = {cbuf_all->str + 160, 80};
struct const_buffer cbuf_header4 = {cbuf_all->str + 240, 80};
struct const_buffer cbuf_header5_fork = {cbuf_all->str + 320, 80};
struct const_buffer cbuf_header2_stale = {cbuf_all->str + 400, 80};
struct const_buffer cbuf_header2_fork = {cbuf_all->str + 480, 80};
struct const_buffer cbuf_header3_fork = {cbuf_all->str + 560, 80};
struct const_buffer cbuf_header4_fork = {cbuf_all->str + 640, 80};
struct const_buffer cbuf_header5_fork_again = {cbuf_all->str + 720, 80};
struct const_buffer cbuf_header5_fork_duplicate = {cbuf_all->str + 800, 80};
// Connect the headers to the database
dogecoin_bool connected;
dogecoin_headers_db *db = client->headers_db_ctx;
dogecoin_headers_db_connect_hdr(db, &cbuf_header1, false, &connected);
u_assert_true (connected);
dogecoin_headers_db_connect_hdr(db, &cbuf_header2, false, &connected);
u_assert_true (connected);
dogecoin_headers_db_connect_hdr(db, &cbuf_header3, false, &connected);
u_assert_true (connected);
dogecoin_headers_db_connect_hdr(db, &cbuf_header4, false, &connected);
u_assert_true (connected);
dogecoin_free(dogecoin_headers_db_connect_hdr(db, &cbuf_header5_fork, false, &connected));
u_assert_true (!connected);
dogecoin_headers_db_connect_hdr(db, &cbuf_header2_stale, false, &connected);
u_assert_true (connected);
dogecoin_headers_db_connect_hdr(db, &cbuf_header2_fork, false, &connected);
u_assert_true (connected);
dogecoin_headers_db_connect_hdr(db, &cbuf_header3_fork, false, &connected);
u_assert_true (connected);
dogecoin_headers_db_connect_hdr(db, &cbuf_header4_fork, false, &connected);
u_assert_true (connected);
dogecoin_headers_db_connect_hdr(db, &cbuf_header5_fork_again, false, &connected);
u_assert_true (connected);
dogecoin_free(dogecoin_headers_db_connect_hdr(db, &cbuf_header5_fork_duplicate, false, &connected));
u_assert_true (!connected);
// Cleanup
cstr_free(cbuf_all, true);
dogecoin_block_header_free(header1);
dogecoin_block_header_free(header2);
dogecoin_block_header_free(header3);
dogecoin_block_header_free(header4);
dogecoin_block_header_free(header2_stale);
dogecoin_block_header_free(header2_fork);
dogecoin_block_header_free(header3_fork);
dogecoin_block_header_free(header4_fork);
dogecoin_block_header_free(header5_fork);
dogecoin_spv_client_free(client);
remove_all_hashes();
remove_all_maps();
// Re-initialize SPV client and load the headers database
client = dogecoin_spv_client_new(chain, false, false, false, false, 8, NULL);
client->header_message_processed = test_spv_header_message_processed;
client->sync_completed = test_spv_sync_completed;
dogecoin_spv_client_load(client, headersfile, false);
// Cleanup
dogecoin_spv_client_free(client);
remove_all_hashes();
remove_all_maps();
}