-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathspv.c
692 lines (610 loc) · 28 KB
/
spv.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
The MIT License (MIT)
Copyright (c) 2016 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 _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <conio.h>
#else
#include <getopt.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <dogecoin/block.h>
#include <dogecoin/blockchain.h>
#include <dogecoin/headersdb.h>
#include <dogecoin/headersdb_file.h>
#include <dogecoin/net.h>
#include <dogecoin/protocol.h>
#include <dogecoin/serialize.h>
#include <dogecoin/spv.h>
#include <dogecoin/tx.h>
#include <dogecoin/utils.h>
static const unsigned int HEADERS_MAX_RESPONSE_TIME = 120;
static const unsigned int MIN_TIME_DELTA_FOR_STATE_CHECK = 5;
static const unsigned int BLOCK_GAP_TO_DEDUCT_TO_START_SCAN_FROM = 5;
static const unsigned int BLOCKS_DELTA_IN_S = 60;
static const unsigned int COMPLETED_WHEN_NUM_NODES_AT_SAME_HEIGHT = 2;
static dogecoin_bool dogecoin_net_spv_node_timer_callback(dogecoin_node *node, uint64_t *now);
void dogecoin_net_spv_post_cmd(dogecoin_node *node, dogecoin_p2p_msg_hdr *hdr, struct const_buffer *buf);
void dogecoin_net_spv_node_handshake_done(dogecoin_node *node);
void dogecoin_node_connection_state_changed_cb(dogecoin_node *node) {
if (node->nodegroup->should_connect_to_more_nodes_cb) {
if (node->nodegroup->should_connect_to_more_nodes_cb(node)) {
dogecoin_spv_client_discover_peers((dogecoin_spv_client*)node->nodegroup->ctx, NULL);
dogecoin_node_group_connect_next_nodes(node->nodegroup);
}
}
}
/**
* @brief This function deterimines if we should connect to more nodes.
*
* @param node the node that we are connected to.
*
* @return dogecoin_bool (uint8_t)
*/
dogecoin_bool dogecoin_node_should_connect_to_more_cb(dogecoin_node* node) {
int connected_amount = dogecoin_node_group_amount_of_connected_nodes(node->nodegroup, NODE_CONNECTED) + dogecoin_node_group_amount_of_connected_nodes(node->nodegroup, NODE_CONNECTING);
node->nodegroup->log_write_cb("check if more nodes are required (connected to already: %d): %s\n", connected_amount, connected_amount < node->nodegroup->desired_amount_connected_nodes ? "true" : "false");
if (connected_amount < node->nodegroup->desired_amount_connected_nodes) {
return true;
}
return false;
}
/**
* The function sets the nodegroup's postcmd_cb to dogecoin_net_spv_post_cmd,
* the nodegroup's handshake_done_cb to dogecoin_net_spv_node_handshake_done,
* the nodegroup's node_connection_state_changed_cb to NULL, and the
* nodegroup's periodic_timer_cb to dogecoin_net_spv_node_timer_callback
*
* @param nodegroup The nodegroup to set the callbacks for.
*/
void dogecoin_net_set_spv(dogecoin_node_group *nodegroup)
{
nodegroup->postcmd_cb = dogecoin_net_spv_post_cmd;
nodegroup->handshake_done_cb = dogecoin_net_spv_node_handshake_done;
nodegroup->should_connect_to_more_nodes_cb = dogecoin_node_should_connect_to_more_cb;
nodegroup->node_connection_state_changed_cb = dogecoin_node_connection_state_changed_cb;
nodegroup->periodic_timer_cb = dogecoin_net_spv_node_timer_callback;
}
/**
* The function creates a new dogecoin_spv_client object and initializes it
*
* @param params The chainparams struct that we created earlier.
* @param debug If true, the node will print out debug messages to stdout.
* @param headers_memonly If true, the headers database will not be loaded from disk.
*
* @return A pointer to a dogecoin_spv_client object.
*/
dogecoin_spv_client* dogecoin_spv_client_new(const dogecoin_chainparams *params, dogecoin_bool debug, dogecoin_bool headers_memonly, dogecoin_bool use_checkpoints, dogecoin_bool full_sync, int maxnodes)
{
dogecoin_spv_client* client;
client = dogecoin_calloc(1, sizeof(*client));
client->last_headersrequest_time = 0; //!< time when we requested the last header package
client->last_statecheck_time = 0;
client->oldest_item_of_interest = time(NULL)-5*60;
client->stateflags = full_sync ? SPV_FULLBLOCK_SYNC_FLAG : SPV_HEADER_SYNC_FLAG;
client->chainparams = params;
client->nodegroup = dogecoin_node_group_new(params);
client->nodegroup->ctx = client;
if (maxnodes > 120) {
maxnodes = 120;
}
client->nodegroup->desired_amount_connected_nodes = maxnodes;
dogecoin_net_set_spv(client->nodegroup);
if (debug) {
client->nodegroup->log_write_cb = net_write_log_printf;
}
if (params == &dogecoin_chainparams_main || params == &dogecoin_chainparams_test) {
client->use_checkpoints = use_checkpoints;
}
client->headers_db = &dogecoin_headers_db_interface_file;
client->headers_db_ctx = client->headers_db->init(params, headers_memonly);
// set callbacks
client->header_connected = NULL;
client->called_sync_completed = false;
client->sync_completed = NULL;
client->header_message_processed = NULL;
client->sync_transaction = NULL;
return client;
}
/**
* It adds peers to the nodegroup.
*
* @param client the dogecoin_spv_client object
* @param ips A comma-separated list of IPs or seeds to connect to.
*/
void dogecoin_spv_client_discover_peers(dogecoin_spv_client* client, const char *ips)
{
#ifndef _WIN32
// set stdin to non-blocking for quit command
int stdin_flags = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
#endif
dogecoin_node_group_add_peers_by_ip_or_seed(client->nodegroup, ips);
}
/**
* The function loops through all the nodes in the node group and connects to the next nodes in the
* node group
*
* @param client The dogecoin_spv_client object.
*/
void dogecoin_spv_client_runloop(dogecoin_spv_client* client)
{
dogecoin_node_group_connect_next_nodes(client->nodegroup);
dogecoin_node_group_event_loop(client->nodegroup);
}
/**
* It frees the memory allocated for the client
*
* @param client The client object to be freed.
*
* @return Nothing.
*/
void dogecoin_spv_client_free(dogecoin_spv_client *client)
{
if (!client)
return;
if (client->headers_db)
{
if (client->headers_db_ctx)
{
client->headers_db->free(client->headers_db_ctx);
}
client->headers_db_ctx = NULL;
client->headers_db = NULL;
}
if (client->nodegroup) {
dogecoin_node_group_free(client->nodegroup);
client->nodegroup = NULL;
}
dogecoin_free(client);
}
/**
* Loads the headers database from a file
*
* @param client the client object
* @param file_path The path to the headers database file.
* @param prompt If true, the user will be prompted to confirm loading the database.
*
* @return A boolean value.
*/
dogecoin_bool dogecoin_spv_client_load(dogecoin_spv_client *client, const char *file_path, dogecoin_bool prompt)
{
if (!client)
return false;
if (!client->headers_db)
return false;
return client->headers_db->load(client->headers_db_ctx, file_path, prompt);
}
/**
* If we are in the header sync state, we request headers from a random node
*
* @param node the node that we are checking
* @param now The current time in seconds.
*/
void dogecoin_net_spv_periodic_statecheck(dogecoin_node *node, uint64_t *now)
{
/* statecheck logic */
/* ================ */
dogecoin_spv_client *client = (dogecoin_spv_client*)node->nodegroup->ctx;
dogecoin_blockindex *pindex = client->headers_db->getchaintip(client->headers_db_ctx);
client->nodegroup->log_write_cb("Statecheck: amount of connected nodes: %d\nchaintip hash: %s\nchaintip height: %d\n", dogecoin_node_group_amount_of_connected_nodes(client->nodegroup, NODE_CONNECTED), hash_to_string(pindex->hash), pindex->height);
if (client->last_headersrequest_time > 0 && *now > client->last_headersrequest_time)
{
int64_t timedetla = *now - client->last_headersrequest_time;
client->nodegroup->log_write_cb("No header response in time (used %d) for node %d\n", timedetla, node->nodeid);
if (timedetla > HEADERS_MAX_RESPONSE_TIME)
{
node->state &= ~NODE_HEADERSYNC;
dogecoin_node_misbehave(node);
}
}
if (node->time_last_request > 0 && *now > node->time_last_request)
{
// we are downloading blocks from this peer
int64_t timedelta = *now - node->time_last_request;
client->nodegroup->log_write_cb("No block response in time (used %d) for node %d\n", timedelta, node->nodeid);
if (timedelta > HEADERS_MAX_RESPONSE_TIME)
{
node->state &= ~NODE_BLOCKSYNC;
dogecoin_node_misbehave(node);
}
}
if ((client->stateflags & SPV_HEADER_SYNC_FLAG) == SPV_HEADER_SYNC_FLAG)
{
client->last_headersrequest_time = 0;
dogecoin_net_spv_request_headers(client);
}
if ((client->stateflags & SPV_FULLBLOCK_SYNC_FLAG) == SPV_FULLBLOCK_SYNC_FLAG)
{
node->time_last_request = 0;
dogecoin_net_spv_request_headers(client);
}
client->last_statecheck_time = *now;
}
/**
* This function is called by the dogecoin_node_timer_callback function.
*
* It checks if the last_statecheck_time is greater than the minimum time delta for state checks.
*
* If it is, it calls the dogecoin_net_spv_periodic_statecheck function.
*
* The dogecoin_net_spv_periodic_statecheck function checks if the node is connected to the network.
*
* @param node The node that the timer is being called on.
* @param now the current time in seconds since the epoch
*
* @return A boolean value.
*/
static dogecoin_bool dogecoin_net_spv_node_timer_callback(dogecoin_node *node, uint64_t *now)
{
dogecoin_spv_client *client = (dogecoin_spv_client*)node->nodegroup->ctx;
if (client->last_statecheck_time + MIN_TIME_DELTA_FOR_STATE_CHECK < *now)
{
dogecoin_net_spv_periodic_statecheck(node, now);
}
return true;
}
/**
* Fill up the blocklocators vector with the blocklocators from the headers database
*
* @param client the spv client
* @param blocklocators a vector of block hashes that we want to scan from
*
* @return The blocklocators are being returned.
*/
void dogecoin_net_spv_fill_block_locator(dogecoin_spv_client *client, vector *blocklocators) {
int64_t min_timestamp = client->oldest_item_of_interest - BLOCK_GAP_TO_DEDUCT_TO_START_SCAN_FROM * BLOCKS_DELTA_IN_S; /* ensure we going back ~300 blocks */
if (client->headers_db->getchaintip(client->headers_db_ctx)->height == 0) {
if (client->use_checkpoints && client->oldest_item_of_interest > BLOCK_GAP_TO_DEDUCT_TO_START_SCAN_FROM * BLOCKS_DELTA_IN_S) {
const dogecoin_checkpoint *checkpoint = memcmp(client->chainparams, &dogecoin_chainparams_main, 4) == 0 ? dogecoin_mainnet_checkpoint_array : dogecoin_testnet_checkpoint_array;
size_t mainnet_checkpoint_size = sizeof(dogecoin_mainnet_checkpoint_array) / sizeof(dogecoin_mainnet_checkpoint_array[0]);
size_t testnet_checkpoint_size = sizeof(dogecoin_testnet_checkpoint_array) / sizeof(dogecoin_testnet_checkpoint_array[0]);
size_t length = memcmp(client->chainparams, &dogecoin_chainparams_main, 8) == 0 ? mainnet_checkpoint_size : testnet_checkpoint_size;
int i;
for (i = length - 1; i >= 0; i--) {
if (checkpoint[i].timestamp < min_timestamp) {
uint256 *hash = dogecoin_calloc(1, sizeof(uint256));
utils_uint256_sethex((char *)checkpoint[i].hash, (uint8_t *)hash);
vector_add(blocklocators, (void *)hash);
if (!client->headers_db->has_checkpoint_start(client->headers_db_ctx)) {
client->headers_db->set_checkpoint_start(client->headers_db_ctx, *hash, checkpoint[i].height);
}
}
}
if (blocklocators->len > 0) return; // return if we could fill up the blocklocator with checkpoints
}
uint256 *hash = dogecoin_calloc(1, sizeof(uint256));
memcpy_safe(hash, &client->chainparams->genesisblockhash, sizeof(uint256));
vector_add(blocklocators, (void *)hash);
client->nodegroup->log_write_cb("Setting blocklocator with genesis block\n");
} else {
client->headers_db->fill_blocklocator_tip(client->headers_db_ctx, blocklocators);
}
}
/**
* This function is called when a node is in headers sync state. It will request the next block headers
* from the node
*
* @param node The node that is requesting headers or blocks.
* @param blocks boolean, true if we want to request blocks, false if we want to request headers
*/
void dogecoin_net_spv_node_request_headers_or_blocks(dogecoin_node *node, dogecoin_bool blocks)
{
// request next headers
vector *blocklocators = vector_new(1, free);
dogecoin_net_spv_fill_block_locator((dogecoin_spv_client *)node->nodegroup->ctx, blocklocators);
cstring *getheader_msg = cstr_new_sz(256);
dogecoin_p2p_msg_getheaders(blocklocators, NULL, getheader_msg);
cstring *p2p_msg = dogecoin_p2p_message_new(node->nodegroup->chainparams->netmagic, (blocks ? DOGECOIN_MSG_GETBLOCKS : DOGECOIN_MSG_GETHEADERS), getheader_msg->str, getheader_msg->len);
cstr_free(getheader_msg, true);
dogecoin_node_send(node, p2p_msg);
node->state |= ( blocks ? NODE_BLOCKSYNC : NODE_HEADERSYNC);
if (blocks) {
node->time_last_request = time(NULL);
} else {
((dogecoin_spv_client*)node->nodegroup->ctx)->last_headersrequest_time = time(NULL);
}
vector_free(blocklocators, true);
cstr_free(p2p_msg, true);
}
/**
* If we have not yet reached the height of the blockchain tip, we request headers from a peer. If we
* have reached the height of the blockchain tip, we request blocks from a peer
*
* @param client the spv client
*
* @return dogecoin_bool
*/
dogecoin_bool dogecoin_net_spv_request_headers(dogecoin_spv_client *client)
{
size_t i;
dogecoin_bool new_headers_available = false;
for(i = 0; i < client->nodegroup->nodes->len; ++i)
{
dogecoin_node *check_node = vector_idx(client->nodegroup->nodes, i);
if (((check_node->state & NODE_HEADERSYNC) == NODE_HEADERSYNC || (check_node->state & NODE_BLOCKSYNC) == NODE_BLOCKSYNC) && (check_node->state & NODE_CONNECTED) == NODE_CONNECTED) { return true; }
}
// If in header or block sync state, request headers or blocks from the node with the longest chain
if ((client->stateflags & SPV_HEADER_SYNC_FLAG) == SPV_HEADER_SYNC_FLAG || (client->stateflags & SPV_FULLBLOCK_SYNC_FLAG) == SPV_FULLBLOCK_SYNC_FLAG)
{
dogecoin_node *node_with_longest_chain = NULL;
unsigned int longest_chain_height = 0;
for(i = 0; i < client->nodegroup->nodes->len; ++i)
{
dogecoin_node *check_node = vector_idx(client->nodegroup->nodes, i);
if (((check_node->state & NODE_CONNECTED) == NODE_CONNECTED) && check_node->version_handshake)
{
if (check_node->bestknownheight > longest_chain_height)
{
longest_chain_height = check_node->bestknownheight;
node_with_longest_chain = check_node;
}
}
}
// Request headers or blocks from the node with the longest chain
if (node_with_longest_chain != NULL) {
dogecoin_net_spv_node_request_headers_or_blocks(node_with_longest_chain, (client->stateflags & SPV_FULLBLOCK_SYNC_FLAG) == SPV_FULLBLOCK_SYNC_FLAG);
new_headers_available = true;
}
}
// Fallback: original logic for handling cases where no suitable node was found
unsigned int nodes_at_same_height = 0;
if (!new_headers_available && client->headers_db->getchaintip(client->headers_db_ctx)->header.timestamp < client->oldest_item_of_interest - (BLOCK_GAP_TO_DEDUCT_TO_START_SCAN_FROM * BLOCKS_DELTA_IN_S) && client->stateflags == SPV_HEADER_SYNC_FLAG)
{
for(i = 0; i < client->nodegroup->nodes->len; i++)
{
dogecoin_node *check_node = vector_idx(client->nodegroup->nodes, i);
if (((check_node->state & NODE_CONNECTED) == NODE_CONNECTED) && check_node->version_handshake)
{
if (check_node->bestknownheight > client->headers_db->getchaintip(client->headers_db_ctx)->height) {
dogecoin_net_spv_node_request_headers_or_blocks(check_node, false);
new_headers_available = true;
break;
} else if (check_node->bestknownheight == client->headers_db->getchaintip(client->headers_db_ctx)->height) {
nodes_at_same_height++;
}
}
}
}
if (!new_headers_available && (dogecoin_node_group_amount_of_connected_nodes(client->nodegroup, NODE_CONNECTED) > 0) && client->stateflags == SPV_FULLBLOCK_SYNC_FLAG) {
// try to fetch blocks if no new headers are available but connected nodes are reachable
for(i = 0; i< client->nodegroup->nodes->len; i++)
{
dogecoin_node *check_node = vector_idx(client->nodegroup->nodes, i);
if (((check_node->state & NODE_CONNECTED) == NODE_CONNECTED) && check_node->version_handshake)
{
if (check_node->bestknownheight == client->headers_db->getchaintip(client->headers_db_ctx)->height) {
nodes_at_same_height++;
}
dogecoin_net_spv_node_request_headers_or_blocks(check_node, true);
new_headers_available = true;
}
}
}
if (nodes_at_same_height >= COMPLETED_WHEN_NUM_NODES_AT_SAME_HEIGHT && !client->called_sync_completed && client->sync_completed)
{
client->sync_completed(client);
client->called_sync_completed = true;
}
return new_headers_available;
}
/**
* When the handshake is done, we request the headers
*
* @param node The node that just completed the handshake.
*/
void dogecoin_net_spv_node_handshake_done(dogecoin_node *node)
{
dogecoin_net_spv_request_headers((dogecoin_spv_client*)node->nodegroup->ctx);
}
/**
* The function is called when a new message is received from a peer
*
* @param node
* @param hdr
* @param buf
*
* @return Nothing.
*/
void dogecoin_net_spv_post_cmd(dogecoin_node *node, dogecoin_p2p_msg_hdr *hdr, struct const_buffer *buf)
{
dogecoin_spv_client *client = (dogecoin_spv_client *)node->nodegroup->ctx;
if (strcmp(hdr->command, DOGECOIN_MSG_INV) == 0 && (node->state & NODE_BLOCKSYNC) == NODE_BLOCKSYNC)
{
struct const_buffer original_inv = { buf->p, buf->len };
uint32_t varlen;
deser_varlen(&varlen, buf);
dogecoin_bool contains_block = false;
client->nodegroup->log_write_cb("Get inv request with %d items\n", varlen);
unsigned int i;
for (i = 0; i < varlen; i++)
{
uint32_t type;
deser_u32(&type, buf);
if (type == DOGECOIN_INV_TYPE_BLOCK && ((varlen >= 500) || (client->headers_db->getchaintip(client->headers_db_ctx)->height > node->bestknownheight - 1440))) {
contains_block = true;
deser_u256(node->last_requested_inv, buf);
} else {
deser_skip(buf, 32);
}
}
if (contains_block) {
node->time_last_request = time(NULL);
client->nodegroup->log_write_cb("Requesting %d blocks\n", varlen);
cstring *p2p_msg = dogecoin_p2p_message_new(node->nodegroup->chainparams->netmagic, DOGECOIN_MSG_GETDATA, original_inv.p, original_inv.len);
dogecoin_node_send(node, p2p_msg);
cstr_free(p2p_msg, true);
}
}
if (strcmp(hdr->command, DOGECOIN_MSG_BLOCK) == 0)
{
dogecoin_bool connected;
dogecoin_blockindex *pindex = client->headers_db->connect_hdr(client->headers_db_ctx, buf, false, &connected);
node->time_last_request = time(NULL);
if (connected) {
if (client->header_connected) { client->header_connected(client); }
// for now, turn of stall checks if we are near the tip
if (pindex->header.timestamp > node->time_last_request - 30*60) {
node->time_last_request = 0;
}
time_t lasttime = pindex->header.timestamp;
char s[1000];
time_t t = lasttime;
struct tm *p = localtime(&t);
strftime(s, sizeof s, "%F %T", p);
char *ctime_no_newline;
ctime_no_newline = strtok(s, "\n");
printf("%s|%d|%s|%d\n", hash_to_string(pindex->hash), pindex->height, ctime_no_newline, hdr->data_len);
uint64_t start = time(NULL);
uint32_t amount_of_txs;
if (!deser_varlen(&amount_of_txs, buf)) {
if (!client->headers_db->disconnect_tip(client->headers_db_ctx)) {
dogecoin_free(pindex);
}
client->nodegroup->log_write_cb("Error deserializing amount of transactions from node %d\n", node->nodeid);
node->state &= ~NODE_BLOCKSYNC;
node->nodegroup->node_connection_state_changed_cb(node);
return;
}
client->nodegroup->log_write_cb("Start parsing %d transactions...\n", (int)amount_of_txs);
size_t consumedlength = 0;
unsigned int i;
for (i = 0; i < amount_of_txs; i++)
{
dogecoin_tx* tx = dogecoin_tx_new();
if (!dogecoin_tx_deserialize(buf->p, buf->len, tx, &consumedlength)) {
client->nodegroup->log_write_cb("Error deserializing transaction\n");
if (!client->headers_db->disconnect_tip(client->headers_db_ctx)) {
dogecoin_free(pindex);
}
dogecoin_tx_free(tx);
node->state &= ~NODE_BLOCKSYNC;
node->nodegroup->node_connection_state_changed_cb(node);
return;
}
deser_skip(buf, consumedlength);
if (client->sync_transaction) { client->sync_transaction(client->sync_transaction_ctx, tx, i, pindex); }
dogecoin_tx_free(tx);
}
client->nodegroup->log_write_cb("done (took %lld secs)\n", (unsigned long long)(time(NULL) - start));
}
else
{
client->nodegroup->log_write_cb("Got invalid block with hash %s and height %d (not in sequence) from node %d\n", hash_to_string(pindex->hash), pindex->height, node->nodeid);
node->state &= ~NODE_BLOCKSYNC;
node->nodegroup->node_connection_state_changed_cb(node);
dogecoin_free(pindex);
return;
}
if (dogecoin_hash_equal((uint8_t *)node->last_requested_inv, (uint8_t *)pindex->hash)) {
// instead of querying whether the last connected header timestamp is greater than the oldest item of interest
// we check if the height is greater than or equal to the node's bestknown height minus 5 minutes
if (client->headers_db->getchaintip(client->headers_db_ctx)->height >= node->bestknownheight - 5) {
// last requested block reached, consider stop syncing
if (!client->called_sync_completed && client->sync_completed) { client->sync_completed(client); client->called_sync_completed = true; }
} else if (client->headers_db->getchaintip(client->headers_db_ctx)->height < node->bestknownheight - 1440) {
node->time_last_request = time(NULL);
dogecoin_net_spv_node_request_headers_or_blocks(node, true);
}
}
}
if (strcmp(hdr->command, DOGECOIN_MSG_HEADERS) == 0)
{
uint32_t amount_of_headers;
if (!deser_varlen(&amount_of_headers, buf)) return;
uint64_t now = time(NULL);
client->nodegroup->log_write_cb("Got %d headers (took %d s) from node %d\n", amount_of_headers, now - client->last_headersrequest_time, node->nodeid);
// flag off the request stall check
client->last_headersrequest_time = 0;
unsigned int connected_headers = 0;
unsigned int i;
for (i = 0; i < amount_of_headers; i++)
{
dogecoin_bool connected;
dogecoin_blockindex *pindex = client->headers_db->connect_hdr(client->headers_db_ctx, buf, false, &connected);
if (!pindex)
{
client->nodegroup->log_write_cb("Header deserialization failed (node %d)\n", node->nodeid);
}
if (!deser_skip(buf, 1)) {
client->nodegroup->log_write_cb("Header deserialization (tx count skip) failed (node %d)\n", node->nodeid);
}
if (!connected)
{
client->nodegroup->log_write_cb("Got invalid headers (not in sequence) from node %d\n", node->nodeid);
node->state &= ~NODE_HEADERSYNC;
break;
} else {
if (client->header_connected) { client->header_connected(client); }
connected_headers++;
if (pindex->height >= node->bestknownheight - 5) {
client->stateflags &= ~SPV_HEADER_SYNC_FLAG;
client->stateflags |= SPV_FULLBLOCK_SYNC_FLAG;
node->state &= ~NODE_HEADERSYNC;
node->state |= NODE_BLOCKSYNC;
client->nodegroup->log_write_cb("start loading block from node %d at height %d at time: %ld\n", node->nodeid, client->headers_db->getchaintip(client->headers_db_ctx)->height, client->headers_db->getchaintip(client->headers_db_ctx)->header.timestamp);
dogecoin_net_spv_node_request_headers_or_blocks(node, true);
break;
}
}
}
dogecoin_blockindex *chaintip = client->headers_db->getchaintip(client->headers_db_ctx);
client->nodegroup->log_write_cb("Connected %d headers\n", connected_headers);
client->nodegroup->log_write_cb("Chaintip at height %d\n", chaintip->height);
if (client->header_message_processed && client->header_message_processed(client, node, chaintip) == false)
return;
if (amount_of_headers == MAX_HEADERS_RESULTS && ((node->state & NODE_BLOCKSYNC) != NODE_BLOCKSYNC))
{
time_t lasttime = chaintip->header.timestamp;
client->nodegroup->log_write_cb("chain size: %d, last time %s", chaintip->height, ctime(&lasttime));
dogecoin_net_spv_node_request_headers_or_blocks(node, false);
}
}
// Check for a 'Q' or 'q' on stdin, to quit.
#ifdef _WIN32
if (_kbhit()) {
char c = fgetc(stdin);
if (c == 'Q' || c == 'q') {
printf("Disconnecting...\n");
dogecoin_node_group_shutdown(client->nodegroup);
}
}
#else
char c = fgetc(stdin);
if (c == 'Q' || c == 'q') {
// Reset standard input back to blocking mode
int stdin_flags = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, stdin_flags & ~O_NONBLOCK);
printf("Disconnecting...\n");
dogecoin_node_group_shutdown(client->nodegroup);
}
#endif
}