@@ -116,24 +116,10 @@ void dogecoin_headers_db_free(dogecoin_headers_db* db) {
116
116
}
117
117
118
118
if (db -> tree_root ) {
119
- dogecoin_btree_tdestroy (db -> tree_root , NULL );
119
+ dogecoin_btree_tdestroy (db -> tree_root , dogecoin_free );
120
120
db -> tree_root = NULL ;
121
121
}
122
122
123
- // Free all blockindex structures starting from chaintip to chainbottom
124
- if (db -> chaintip ) {
125
- dogecoin_blockindex * scan_tip = db -> chaintip ;
126
- while (scan_tip -> prev ) {
127
- dogecoin_blockindex * prev = scan_tip -> prev ;
128
- dogecoin_free (scan_tip );
129
- scan_tip = prev ;
130
- }
131
- // If scan_tip is not the genesis block, free it
132
- if (scan_tip != & db -> genesis ) {
133
- dogecoin_free (scan_tip );
134
- }
135
- }
136
-
137
123
db -> chaintip = NULL ;
138
124
db -> chainbottom = NULL ;
139
125
@@ -311,13 +297,19 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
311
297
dogecoin_blockindex * blockindex = dogecoin_calloc (1 , sizeof (dogecoin_blockindex ));
312
298
if (!dogecoin_block_header_deserialize (& blockindex -> header , buf , db -> params ))
313
299
{
314
- dogecoin_free (blockindex );
315
300
fprintf (stderr , "Error deserializing block header\n" );
316
- return NULL ;
301
+ return blockindex ;
317
302
}
318
303
319
304
dogecoin_block_header_hash (& blockindex -> header , (uint8_t * )& blockindex -> hash );
320
305
306
+ // Check if the block header is already in the database
307
+ dogecoin_blockindex * block = dogecoin_headersdb_find (db , blockindex -> hash );
308
+ if (block ) {
309
+ // Block header already in database, return blockindex
310
+ return blockindex ;
311
+ }
312
+
321
313
dogecoin_blockindex * connect_at = NULL ;
322
314
dogecoin_blockindex * fork_from_block = NULL ;
323
315
@@ -343,8 +335,7 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
343
335
cstr_free (s , true);
344
336
if (!check_pow (& hash , blockindex -> header .bits , db -> params , & blockindex -> header .chainwork )) {
345
337
printf ("%s:%d:%s : non-AUX proof of work failed : %s\n" , __FILE__ , __LINE__ , __func__ , strerror (errno ));
346
- dogecoin_free (blockindex );
347
- return false;
338
+ return blockindex ;
348
339
}
349
340
}
350
341
@@ -383,10 +374,9 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
383
374
// Break the loop if either reaches the start of the chain
384
375
if (!common_ancestor || !fork_chain ) {
385
376
fprintf (stderr , "Unable to find common ancestor.\n" );
386
- dogecoin_free (blockindex );
387
377
dogecoin_free (chaintip_chainwork );
388
378
dogecoin_free (added_chainwork );
389
- return NULL ;
379
+ return blockindex ;
390
380
}
391
381
}
392
382
@@ -413,7 +403,7 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
413
403
// Free the dynamically allocated memory
414
404
dogecoin_free (chaintip_chainwork );
415
405
dogecoin_free (added_chainwork );
416
- return NULL ;
406
+ return blockindex ;
417
407
}
418
408
419
409
// Update the chain tip to the previous block
0 commit comments