Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockchain: moved chainwork to blockindex #213

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ IF(USE_TESTS)
TARGET_SOURCES(tests ${visibility}
test/address_tests.c
test/aes_tests.c
test/arith_uint256_tests.c
test/base58_tests.c
test/bip32_tests.c
test/bip39_tests.c
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ if USE_TESTS
noinst_PROGRAMS += tests
tests_LDADD = libdogecoin.la $(ASM_LIB_FILES)
tests_SOURCES = \
test/arith_uint256_tests.c \
test/address_tests.c \
test/aes_tests.c \
test/base58_tests.c \
Expand Down
2 changes: 2 additions & 0 deletions include/dogecoin/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ typedef base_uint_ arith_uint256;

void arith_negate(arith_uint256* input);
arith_uint256* init_arith_uint256();
void arith_shift_left(arith_uint256* input, unsigned int shift);
void arith_shift_right(arith_uint256* input, unsigned int shift);
arith_uint256* set_compact(arith_uint256* hash, uint32_t compact, dogecoin_bool *pf_negative, dogecoin_bool *pf_overflow);
uint8_t* arith_to_uint256(const arith_uint256* a);
arith_uint256* uint_to_arith(const uint256* a);
Expand Down
5 changes: 2 additions & 3 deletions include/dogecoin/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ typedef struct dogecoin_block_header_ {
uint32_t bits;
uint32_t nonce;
auxpow auxpow[1];
uint256 chainwork;
} dogecoin_block_header;

typedef struct dogecoin_auxpow_block_ {
Expand All @@ -75,8 +74,8 @@ LIBDOGECOIN_API dogecoin_block_header* dogecoin_block_header_new();
LIBDOGECOIN_API void dogecoin_block_header_free(dogecoin_block_header* header);
LIBDOGECOIN_API dogecoin_auxpow_block* dogecoin_auxpow_block_new();
LIBDOGECOIN_API void dogecoin_auxpow_block_free(dogecoin_auxpow_block* block);
LIBDOGECOIN_API int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct const_buffer* buf, const dogecoin_chainparams *params);
LIBDOGECOIN_API int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const_buffer* buffer, const dogecoin_chainparams *params);
LIBDOGECOIN_API int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct const_buffer* buf, const dogecoin_chainparams *params, uint256* chainwork);
LIBDOGECOIN_API int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const_buffer* buffer, const dogecoin_chainparams *params, uint256* chainwork);
LIBDOGECOIN_API void dogecoin_block_header_serialize(cstring* s, const dogecoin_block_header* header);
LIBDOGECOIN_API void dogecoin_block_header_copy(dogecoin_block_header* dest, const dogecoin_block_header* src);
LIBDOGECOIN_API dogecoin_bool dogecoin_block_header_hash(dogecoin_block_header* header, uint256 hash);
Expand Down
1 change: 1 addition & 0 deletions include/dogecoin/blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ LIBDOGECOIN_BEGIN_DECL
typedef struct dogecoin_blockindex {
uint32_t height;
uint256 hash;
uint256 chainwork;
dogecoin_block_header header;
struct dogecoin_blockindex* prev;
} dogecoin_blockindex;
Expand Down
6 changes: 4 additions & 2 deletions include/dogecoin/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ typedef struct dogecoin_chainparams_ {
uint32_t b58prefix_bip32_pubkey;
const unsigned char netmagic[4];
uint256 genesisblockhash;
uint256 genesisblockchainwork;
int default_port;
dogecoin_dns_seed dnsseeds[8];
dogecoin_bool strict_id;
dogecoin_bool auxpow_id;
uint256 pow_limit;
uint256 minimumchainwork;
} dogecoin_chainparams;

typedef struct dogecoin_checkpoint_ {
Expand All @@ -66,8 +68,8 @@ extern const dogecoin_chainparams dogecoin_chainparams_test;
extern const dogecoin_chainparams dogecoin_chainparams_regtest;

// the mainnet checkpoints, needs a fix size
extern const dogecoin_checkpoint dogecoin_mainnet_checkpoint_array[22];
extern const dogecoin_checkpoint dogecoin_testnet_checkpoint_array[18];
extern const dogecoin_checkpoint dogecoin_mainnet_checkpoint_array[23];
extern const dogecoin_checkpoint dogecoin_testnet_checkpoint_array[19];

LIBDOGECOIN_API const dogecoin_chainparams* chain_from_b58_prefix(const char* address);
LIBDOGECOIN_API int chain_from_b58_prefix_bool(char* address);
Expand Down
2 changes: 1 addition & 1 deletion include/dogecoin/headersdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ typedef struct dogecoin_headers_db_interface_
dogecoin_blockindex* (*getchaintip)(void *db);
dogecoin_bool (*disconnect_tip)(void *db);
dogecoin_bool (*has_checkpoint_start)(void *db);
void (*set_checkpoint_start)(void *db, uint256 hash, uint32_t height);
void (*set_checkpoint_start)(void *db, uint256 hash, uint32_t height, uint256 chainwork);
} dogecoin_headers_db_interface;

LIBDOGECOIN_END_DECL
Expand Down
4 changes: 2 additions & 2 deletions include/dogecoin/headersdb_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ dogecoin_blockindex * dogecoin_headersdb_find(dogecoin_headers_db* db, uint256 h
dogecoin_blockindex * dogecoin_headersdb_getchaintip(dogecoin_headers_db* db);
dogecoin_bool dogecoin_headersdb_disconnect_tip(dogecoin_headers_db* db);
dogecoin_bool dogecoin_headersdb_has_checkpoint_start(dogecoin_headers_db* db);
void dogecoin_headersdb_set_checkpoint_start(dogecoin_headers_db* db, uint256 hash, uint32_t height);
void dogecoin_headersdb_set_checkpoint_start(dogecoin_headers_db* db, uint256 hash, uint32_t height, uint256 chainwork);

static const dogecoin_headers_db_interface dogecoin_headers_db_interface_file = {
(void* (*)(const dogecoin_chainparams*, dogecoin_bool))dogecoin_headers_db_new,
Expand All @@ -74,7 +74,7 @@ static const dogecoin_headers_db_interface dogecoin_headers_db_interface_file =
(dogecoin_blockindex* (*)(void *))dogecoin_headersdb_getchaintip,
(dogecoin_bool (*)(void *))dogecoin_headersdb_disconnect_tip,
(dogecoin_bool (*)(void *))dogecoin_headersdb_has_checkpoint_start,
(void (*)(void *, uint256, uint32_t))dogecoin_headersdb_set_checkpoint_start
(void (*)(void *, uint256, uint32_t, uint256))dogecoin_headersdb_set_checkpoint_start
};

LIBDOGECOIN_END_DECL
Expand Down
2 changes: 1 addition & 1 deletion include/dogecoin/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ LIBDOGECOIN_BEGIN_DECL
LIBDOGECOIN_API uint32_t get_chainid(uint32_t version);
LIBDOGECOIN_API dogecoin_bool is_auxpow(uint32_t version);
LIBDOGECOIN_API dogecoin_bool is_legacy(uint32_t version);
LIBDOGECOIN_API dogecoin_bool check_auxpow(dogecoin_auxpow_block* block, dogecoin_chainparams* params);
LIBDOGECOIN_API dogecoin_bool check_auxpow(dogecoin_auxpow_block* block, dogecoin_chainparams* params, uint256* chainwork);
LIBDOGECOIN_API dogecoin_bool dogecoin_block_header_scrypt_hash(cstring* s, uint256* hash);

LIBDOGECOIN_END_DECL
Expand Down
26 changes: 16 additions & 10 deletions src/arith_uint256.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ arith_uint256* set_compact(arith_uint256* hash, uint32_t compact, dogecoin_bool
uint32_t word = compact & 0x007fffff;
if (size <= 3) {
word >>= 8 * (3 - size);
memcpy_safe(&hash->pn[0], &word, sizeof word);
hash->pn[0] = word;
} else {
memcpy_safe(&hash->pn[0], &word, sizeof word);
hash->pn[0] = word;
arith_shift_left(hash, 8 * (size - 3));
}
if (pf_negative) *pf_negative = word != 0 && (compact & 0x00800000) != 0;
Expand Down Expand Up @@ -152,21 +152,27 @@ arith_uint256* div_arith_uint256(arith_uint256* a, arith_uint256* b) {
arith_uint256* add_arith_uint256(arith_uint256* a, arith_uint256* b) {
arith_uint256* result = init_arith_uint256();
uint64_t carry = 0;
for (int i = WIDTH - 1; i >= 0; i--) {
for (int i = 0; i < WIDTH; i++) {
uint64_t sum = (uint64_t)a->pn[i] + b->pn[i] + carry;
result->pn[i] = sum; // This will only keep the lower 32 bits
carry = sum >> 32; // This will keep the upper 32 bits (carry)
result->pn[i] = (uint32_t)sum; // This will only keep the lower 32 bits
carry = sum >> 32; // Carry is the upper 32 bits
}
return result;
}

arith_uint256* sub_arith_uint256(arith_uint256* a, arith_uint256* b) {
if (arith_uint256_less_than(a, b)) {
// Handle underflow if necessary
return NULL;
}
arith_uint256* result = init_arith_uint256();
int64_t carry = 0;
for (int i = WIDTH - 1; i >= 0; i--) {
int64_t diff = (uint64_t)a->pn[i] - b->pn[i] - carry;
result->pn[i] = diff; // This will only keep the lower 32 bits
carry = (diff < 0) ? 1 : 0; // If diff is negative, there's a borrow
uint64_t borrow = 0;
for (int i = 0; i < WIDTH; i++) {
uint64_t diff = (uint64_t)a->pn[i] - b->pn[i] - borrow;
result->pn[i] = (uint32_t)diff; // Keep only lower 32 bits
// If diff is less than zero when interpreted as signed, there's a borrow.
// Note: This checks the high bit of the 64-bit diff calculation for borrow.
borrow = (diff > (uint64_t)UINT32_MAX) ? 1 : 0;
}
return result;
}
Expand Down
11 changes: 5 additions & 6 deletions src/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ dogecoin_block_header* dogecoin_block_header_new() {
header->auxpow->check = check;
header->auxpow->ctx = header;
header->auxpow->is = false;
dogecoin_mem_zero(&header->chainwork, DOGECOIN_HASH_LENGTH);
return header;
}

Expand Down Expand Up @@ -310,10 +309,11 @@ void print_block(dogecoin_auxpow_block* block) {
* @param header The header object to be constructed.
* @param buf The buffer to deserialize from.
* @param params The chain parameters.
* @param chainwork The computed chainwork.
*
* @return 1 if deserialization was successful, 0 otherwise.
*/
int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct const_buffer* buf, const dogecoin_chainparams *params) {
int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct const_buffer* buf, const dogecoin_chainparams *params, uint256* chainwork) {
dogecoin_auxpow_block* block = dogecoin_auxpow_block_new();
if (!deser_s32(&block->header->version, buf))
return false;
Expand All @@ -329,7 +329,7 @@ int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct cons
return false;
dogecoin_block_header_copy(header, block->header);
if ((block->header->version & 0x100) != 0 && buf->len) {
if (!deserialize_dogecoin_auxpow_block(block, buf, params)) {
if (!deserialize_dogecoin_auxpow_block(block, buf, params, chainwork)) {
printf("%s:%d:%s:%s\n", __FILE__, __LINE__, __func__, strerror(errno));
return false;
}
Expand All @@ -339,7 +339,7 @@ int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct cons
return true;
}

int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const_buffer* buffer, const dogecoin_chainparams *params) {
int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const_buffer* buffer, const dogecoin_chainparams *params, uint256* chainwork) {
if (buffer->len > DOGECOIN_MAX_P2P_MSG_SIZE) {
return printf("\ntransaction is invalid or to large.\n\n");
}
Expand Down Expand Up @@ -428,7 +428,7 @@ int deserialize_dogecoin_auxpow_block(dogecoin_auxpow_block* block, struct const
return false;
}

if (!check_auxpow(block, (dogecoin_chainparams*)params)) {
if (!check_auxpow(block, (dogecoin_chainparams*)params, chainwork)) {
printf("check_auxpow failed!\n");
return false;
}
Expand Down Expand Up @@ -473,7 +473,6 @@ void dogecoin_block_header_copy(dogecoin_block_header* dest, const dogecoin_bloc
dest->auxpow->check = src->auxpow->check;
dest->auxpow->ctx = src->auxpow->ctx;
dest->auxpow->is = src->auxpow->is;
memcpy_safe(&dest->chainwork, &src->chainwork, sizeof(src->chainwork));
}

/**
Expand Down
18 changes: 13 additions & 5 deletions src/chainparams.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ const dogecoin_chainparams dogecoin_chainparams_main = {
0x02facafd, // starts with dgub
{0xc0, 0xc0, 0xc0, 0xc0}, // pch msg prefixes (magic bytes)
{0x91, 0x56, 0x35, 0x2c, 0x18, 0x18, 0xb3, 0x2e, 0x90, 0xc9, 0xe7, 0x92, 0xef, 0xd6, 0xa1, 0x1a, 0x82, 0xfe, 0x79, 0x56, 0xa6, 0x30, 0xf0, 0x3b, 0xbe, 0xe2, 0x36, 0xce, 0xda, 0xe3, 0x91, 0x1a},
{0x10, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
22556,
{{"seed.multidoge.org"}, {{1}}},
true,
0x0062,
{0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} // pow limit
{0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // pow limit
{0x9b, 0xa4, 0x46, 0xf2, 0x6c, 0xa8, 0x2a, 0x3d, 0x99, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
};

const dogecoin_chainparams dogecoin_chainparams_test = {
Expand All @@ -56,11 +58,13 @@ const dogecoin_chainparams dogecoin_chainparams_test = {
0x043587cf, // starts with tpub
{0xfc, 0xc1, 0xb7, 0xdc}, // pch msg prefixes (magic bytes)
{0x9e, 0x55, 0x50, 0x73, 0xd0, 0xc4, 0xf3, 0x64, 0x56, 0xdb, 0x89, 0x51, 0xf4, 0x49, 0x70, 0x4d, 0x54, 0x4d, 0x28, 0x26, 0xd9, 0xaa, 0x60, 0x63, 0x6b, 0x40, 0x37, 0x46, 0x26, 0x78, 0x0a, 0xbb},
{0x10, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
44556,
{{"testseed.jrn.me.uk"}, {{0}}},
false,
0x0062,
{0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} // pow limit
{0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // pow limit
{0x26, 0x9a, 0xff, 0x62, 0x2f, 0x0f, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
};

const dogecoin_chainparams dogecoin_chainparams_regtest = {
Expand All @@ -73,11 +77,13 @@ const dogecoin_chainparams dogecoin_chainparams_regtest = {
0x043587cf, // starts with tpub
{0xfa, 0xbf, 0xb5, 0xda}, // pch msg prefixes (magic bytes)
{0xa5, 0x73, 0xe9, 0x1c, 0x17, 0x72, 0x07, 0x6c, 0x0d, 0x40, 0xf7, 0x0e, 0x44, 0x08, 0xc8, 0x3a, 0x31, 0x70, 0x5f, 0x29, 0x6a, 0xe6, 0xe7, 0x62, 0x9d, 0x4a, 0xdc, 0xb5, 0xa3, 0x60, 0x21, 0x3d},
{0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
18332,
{{"testseed.jrn.me.uk"}, {{0}}},
true,
0x0062,
{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} // pow limit
{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // pow limit
{0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
};

const dogecoin_checkpoint dogecoin_mainnet_checkpoint_array[] = {
Expand All @@ -102,7 +108,8 @@ const dogecoin_checkpoint dogecoin_mainnet_checkpoint_array[] = {
{3606083, "954c7c66dee51f0a3fb1edb26200b735f5275fe54d9505c76ebd2bcabac36f1e", 1613218169, 0x1a03d764},
{3854173, "e4b4ecda4c022406c502a247c0525480268ce7abbbef632796e8ca1646425e75", 1628934997, 0x1a03ca36},
{3963597, "2b6927cfaa5e82353d45f02be8aadd3bfd165ece5ce24b9bfa4db20432befb5d", 1635884460, 0x1a037bc9},
{4303965, "ed7d266dcbd8bb8af80f9ccb8deb3e18f9cc3f6972912680feeb37b090f8cee0", 1657646310, 0x1a0344f5}};
{4303965, "ed7d266dcbd8bb8af80f9ccb8deb3e18f9cc3f6972912680feeb37b090f8cee0", 1657646310, 0x1a0344f5},
{5050000, "e7d4577405223918491477db725a393bcfc349d8ee63b0a4fde23cbfbfd81dea", 1705383360, 0x1a019541}};

const dogecoin_checkpoint dogecoin_testnet_checkpoint_array[] = {
{0, "bb0a78264637406b6360aad926284d544d7049f45189db5664f3c4d07350559e", 1391503289, 0x1e0ffff0},
Expand All @@ -122,7 +129,8 @@ const dogecoin_checkpoint dogecoin_testnet_checkpoint_array[] = {
{3062910, "113c41c00934f940a41f99d18b2ad9aefd183a4b7fe80527e1e6c12779bd0246", 1613218844, 0x1e0e2221},
{3286675, "07fef07a255d510297c9189dc96da5f4e41a8184bc979df8294487f07fee1cf3", 1628932841, 0x1e0fffff},
{3445426, "70574db7856bd685abe7b0a8a3e79b29882620645bd763b01459176bceb58cd1", 1635884611, 0x1e0fffff},
{3976284, "af23c3e750bb4f2ce091235f006e7e4e2af453d4c866282e7870471dcfeb4382", 1657646588, 0x1e0fffff}};
{3976284, "af23c3e750bb4f2ce091235f006e7e4e2af453d4c866282e7870471dcfeb4382", 1657646588, 0x1e0fffff},
{5900000, "199bea6a442310589cbb50a193a30b097c228bd5a0f21af21e4e53dd57c382d3", 1703511130, 0x1e0fffff}};

const dogecoin_chainparams* chain_from_b58_prefix(const char* address) {
/* determine address prefix for network chainparams */
Expand Down
Loading
Loading