Skip to content

Commit

Permalink
headersdb_file: added chainwork to database record
Browse files Browse the repository at this point in the history
headersdb_file: added chainwork to load and write
headersdb_file: added chainwork check on write
headersdb_file: updated current version for chainwork
  • Loading branch information
edtubbs committed May 16, 2024
1 parent 8b0ffdf commit bfe0fd4
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/headersdb_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <dogecoin/validation.h>

static const unsigned char file_hdr_magic[4] = {0xA8, 0xF0, 0x11, 0xC5}; /* header magic */
static const uint32_t current_version = 2;
static const uint32_t current_version = 3; /* 3: added chainwork */

/**
* "Compare two block headers by their hashes."
Expand Down Expand Up @@ -217,16 +217,18 @@ dogecoin_bool dogecoin_headers_db_load(dogecoin_headers_db* db, const char *file
fflush(stdout);
}

uint8_t buf_all[32+4+80];
uint8_t buf_all[32+4+32+80];
if (fread(buf_all, sizeof(buf_all), 1, db->headers_tree_file) == 1) {
struct const_buffer cbuf_all = {buf_all, sizeof(buf_all)};

//load all

uint256 hash;
uint32_t height;
uint256 chainwork;
deser_u256(hash, &cbuf_all);
deser_u32(&height, &cbuf_all);
deser_u256(chainwork, &cbuf_all);
dogecoin_bool connected;
if (firstblock)
{
Expand All @@ -253,6 +255,7 @@ dogecoin_bool dogecoin_headers_db_load(dogecoin_headers_db* db, const char *file
connected_headers_count++;
}
}
memcpy(db->chaintip->chainwork, chainwork, sizeof(uint256));
}
}
}
Expand All @@ -269,9 +272,10 @@ dogecoin_bool dogecoin_headers_db_load(dogecoin_headers_db* db, const char *file
* @return Nothing.
*/
dogecoin_bool dogecoin_headers_db_write(dogecoin_headers_db* db, dogecoin_blockindex *blockindex) {
cstring *rec = cstr_new_sz(100);
cstring *rec = cstr_new_sz(148); // hash + height + chainwork + header
ser_u256(rec, blockindex->hash);
ser_u32(rec, blockindex->height);
ser_u256(rec, blockindex->chainwork);
dogecoin_block_header_serialize(rec, &blockindex->header);
size_t res = fwrite(rec->str, rec->len, 1, db->headers_tree_file);
dogecoin_file_commit(db->headers_tree_file);
Expand Down Expand Up @@ -420,11 +424,7 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
db->chaintip = blockindex;
}

// Free the dynamically allocated memory
dogecoin_free(chaintip_chainwork);
dogecoin_free(added_chainwork);

if (!load_process && db->read_write_file)
if (!load_process && db->read_write_file && arith_uint256_greater_than(added_chainwork, chaintip_chainwork))
{
if (!dogecoin_headers_db_write(db, blockindex)) {
fprintf(stderr, "Error writing blockheader to database\n");
Expand All @@ -434,6 +434,10 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s
dogecoin_btree_tsearch(blockindex, &db->tree_root, dogecoin_header_compare);
}

// Free the dynamically allocated memory
dogecoin_free(chaintip_chainwork);
dogecoin_free(added_chainwork);

if (db->max_hdr_in_mem > 0) {
// de-allocate no longer required headers
// keep them only on-disk
Expand Down

0 comments on commit bfe0fd4

Please sign in to comment.