From 0ec51ddc70d4120d168a51e50f06798ae641807e Mon Sep 17 00:00:00 2001 From: Dakoda Greaves Date: Thu, 29 Jun 2023 13:08:09 -0700 Subject: [PATCH] qa: fix remaining test cases on arith_uint256_tests -rename remove_all_hash_uintes to remove_all_hash_uints --- include/dogecoin/arith_uint256.h | 92 +--------------- src/arith_uint256.c | 178 ++----------------------------- src/block.c | 12 +-- src/headersdb_file.c | 2 +- src/wallet.c | 2 +- test/arith_uint256_tests.c | 52 +-------- test/unittester.c | 6 +- test/wallet_tests.c | 2 - 8 files changed, 23 insertions(+), 323 deletions(-) diff --git a/include/dogecoin/arith_uint256.h b/include/dogecoin/arith_uint256.h index 32ff77db1..088766d4a 100644 --- a/include/dogecoin/arith_uint256.h +++ b/include/dogecoin/arith_uint256.h @@ -80,31 +80,7 @@ char* get_hash_uint_by_index(int index); LIBDOGECOIN_API void remove_hash_uint(hash_uint *hash_uint); -LIBDOGECOIN_API void remove_all_hash_uintes(); - -/* hash_uintmap functions */ -typedef struct hash_uintmap { - int index; - int count; - hash_uint *hash_uints; - UT_hash_handle hh; -} hash_uintmap; - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-variable" -static hash_uintmap *hash_uintmaps = NULL; -#pragma GCC diagnostic pop - -// instantiates a new hash_uintmap -LIBDOGECOIN_API hash_uintmap* new_hash_uintmap(); -LIBDOGECOIN_API int start_hash_uintmap(); - -LIBDOGECOIN_API void add_hash_uintmap(hash_uintmap *map); -LIBDOGECOIN_API hash_uintmap* find_hash_uintmap(int idx); - -LIBDOGECOIN_API void remove_hash_uintmap(hash_uintmap *map); - -LIBDOGECOIN_API void remove_all_hash_uintmaps(); +LIBDOGECOIN_API void remove_all_hash_uints(); DISABLE_WARNING_PUSH DISABLE_WARNING(-Wunused-function) @@ -209,62 +185,6 @@ long long int: "long long int", unsigned long long int: "unsigned long long int" uint8_t *: "uint8_t *", default: "other") #define fmt "%20s is '%s'\n" -// int main() { - -// size_t s; ptrdiff_t p; intmax_t i; int ai[3] = {0}; return printf( fmt fmt fmt fmt fmt fmt fmt fmt, - -// "size_t", typename(s), "ptrdiff_t", typename(p), -// "intmax_t", typename(i), "character constant", typename('0'), -// "0x7FFFFFFF", typename(0x7FFFFFFF), "0xFFFFFFFF", typename(0xFFFFFFFF), -// "0x7FFFFFFFU", typename(0x7FFFFFFFU), "array of int", typename(ai)); -// } - -static const base_uint* self(base_uint* a, ...) { - // char* tostring() { - // return utils_uint8_to_hex(b->x.u8, sizeof(b->x.u8)); - // } - va_list parameters; - va_start (parameters, a); - printf("parameters length: %lu\n", ARRAYLEN(parameters)); - printf("parameters length: %s\n", parameters); - printf("parameters length: %lu\n", strlen(parameters)); - printf("%s\n", typename(parameters)); - printf("%s\n", typename(parameters)); - for (const char* p = parameters; *p != '\0'; ++p) - { - printf("bleh\n"); - } - // char* arg = va_arg(parameters, char*); - // // printf(fmt, typename(arg)); - // // printf("%d\n", sizeof(arg)); - // // printf("%d\n", sizeof(*arg)); - // // printf("%p\n", arg); - // // printf("%p\n", arg); - - // char* argc = arg; - // printf("%s\n", arg); - va_end(parameters); - // if (strlen(argc) > 0) { - // printf("parameters: %s\n", argc); - // printf("%zu\n", strlen(argc)); - // printf("%zu\n", sizeof(&argc[0])); - // printf("%zu\n", sizeof(*a)); - // printf("%zu\n", sizeof(*arg)); - - // } - // for (int j = 0; j < sizeof(parameters); j++) { - // printf("parameters: %s\n", &argc[j]); - // printf("parameters: %02x\n", argu[j]); - // printf("%d\n", sizeof(&argc[j])); - // printf("%d\n", sizeof(argu[j])); - // } - - // int i = 0; - // b->WIDTH = WIDTH; - // for (; i < 32; i++) - // b->x.u8[i] = ? va_arg(parameters, uint8_t*) : 0; - return a; -} static const base_uint* _base_uint(base_uint* b, ...) { va_list parameters; @@ -278,10 +198,6 @@ static const base_uint* _base_uint(base_uint* b, ...) { return b; } -static const base_uint* _b(char* (*tostring)()) { - -} - static void copy_base_uint(base_uint* a, base_uint* b) { int i = 0; for (; i < b->WIDTH; i++) @@ -370,12 +286,6 @@ static base_uint* base_uint_and(base_uint* a, base_uint* b) { for (; i < 8; i++) { result->x.u32[i] = a->x.u32[i] & b->x.u32[i]; } - printf("a: %s\n", utils_uint8_to_hex(a->x.u32, 32)); - printf("a: %s\n", utils_uint8_to_hex(a->x.u8, 32)); - printf("b: %s\n", utils_uint8_to_hex(b->x.u32, 32)); - printf("b: %s\n", utils_uint8_to_hex(b->x.u8, 32)); - printf("result: %s\n", utils_uint8_to_hex(result->x.u32, 32)); - printf("result: %s\n", utils_uint8_to_hex(result->x.u8, 32)); return result; } diff --git a/src/arith_uint256.c b/src/arith_uint256.c index a5e14174a..c7ecfe295 100644 --- a/src/arith_uint256.c +++ b/src/arith_uint256.c @@ -29,8 +29,6 @@ #include #include - - /** * @brief This function instantiates a new working hash_uint, * but does not add it to the hash_uint table. @@ -99,81 +97,13 @@ hash_uint* zero_hash_uint(int index) { } void set_hash_uint(int index, uint8_t* x, char* typename) { - printnl(typename); hash_uint* hash_uint_local = zero_hash_uint(index); - dogecoin_bool set = true; - size_t i = sizeof(x); - if (typename=="unsigned char") { - printf("%s\n", typename); - goto sethash_uint; - } else if (typename=="unsigned long int") { - printf("%s\n", typename); - set = false; - goto sethash_uint; - } else if (typename=="int") { - printf("%s\n", typename); - set = false; - goto sethash_uint; - } else if (typename=="unsigned int") { - printf("%s\n", typename); - set = false; - goto sethash_uint; - } sethash_uint: - printf("i: %d\n", i); - printf("u32: %s\n", utils_uint8_to_hex(hash_uint_local->x.u8, 32)); - printf("u32: %s\n", utils_uint8_to_hex(hash_uint_local->x.u32, 32)); - printf("u32: %s\n", utils_uint8_to_hex(hash_uint_local->data, 32)); set_compact(&hash_uint_local->x, x, &hash_uint_local->checks.negative, &hash_uint_local->checks.overflow); memcpy_safe(hash_uint_local->data, hash_uint_local->x.u8, 32); - printf("u32: %s\n", utils_uint8_to_hex(hash_uint_local->x.u8, 32)); - printf("u32: %s\n", utils_uint8_to_hex(hash_uint_local->x.u32, 32)); - printf("u32: %s\n", utils_uint8_to_hex(hash_uint_local->data, 32)); - if (i < 32) { - if (((uint32_t)x >> 24) < 32) swap_bytes(hash_uint_local->data, 32); - else swap_bytes(hash_uint_local->data, 4); - } else { - x = utils_uint8_to_hex((uint8_t*)x, 32); - utils_reverse_hex(x, 64); - swap_bytes(hash_uint_local->x.u8, 32); - } - printf("u32: %s\n", utils_uint8_to_hex(hash_uint_local->x.u8, 32)); - if (set) utils_uint256_sethex(x, hash_uint_local->data); -} - -void set_hash_uint_compact(int index, uint8_t* x, char* typename) { - - printnl(typename); - hash_uint* hash_uint_local = zero_hash_uint(index); - dogecoin_bool set = true; - if (typename=="unsigned char") { - printf("%s\n", typename); - x = utils_uint8_to_hex((uint8_t*)x, 32); - utils_reverse_hex(x, 64); - } else if (typename=="unsigned long int") { - printf("%s\n", typename); - size_t i = sizeof(x); - if (i < 32) { - memcpy_safe(hash_uint_local->data, (uint8_t*)&x, i); - } - set = false; - } else if (typename=="int") { - printf("%s\n", typename); - size_t i = sizeof(x); - if (i < 32) { - base_uint* b = init_base_uint(); - dogecoin_bool overflow, negative; - set_compact(b, x, negative, overflow); - memcpy_safe(hash_uint_local->data, b->x.u8, 32); - printf("b: %s\n", to_string(b->x.u8)); - printf("b: %s\n", to_string(b->x.u32)); - swap_bytes(hash_uint_local->data, 32); - dogecoin_free(b); - } - set = false; - } - if (set) utils_uint256_sethex(x, hash_uint_local->data); + if (((uint32_t)x >> 24) < 32) swap_bytes(hash_uint_local->data, 32); + else swap_bytes(hash_uint_local->data, 4); } void showbits( unsigned int x ) @@ -217,7 +147,7 @@ void remove_hash_uint(hash_uint *hash_uint) { * * @return Nothing. */ -void remove_all_hash_uintes() { +void remove_all_hash_uints() { struct hash_uint *hash_uint; struct hash_uint *tmp; @@ -226,95 +156,6 @@ void remove_all_hash_uintes() { } } -/** - * @brief This function instantiates a new working hash_uintmap, - * but does not add it to the hash_uint table. - * - * @return A pointer to the new working hash_uintmap. - */ -hash_uintmap* new_hash_uintmap() { - hash_uintmap* map = (struct hash_uintmap*)dogecoin_calloc(1, sizeof *map); - map->count = 1; - map->hash_uints = hash_uints; - map->index = HASH_COUNT(hash_uintmaps) + 1; - return map; -} - -/** - * @brief This function creates a new map, places it in - * the hash_uint table, and returns the index of the new map, - * starting from 1 and incrementing each subsequent call. - * - * @return The index of the new map. - */ -int start_hash_uintmap() { - hash_uintmap* map = new_hash_uintmap(); - add_hash_uintmap(map); - return map->index; -} - -/** - * @brief This function takes a pointer to an existing working - * hash_uintmap object and adds it to the hash_uint table. - * - * @param map The pointer to the working hash_uintmap. - * - * @return Nothing. - */ -void add_hash_uintmap(hash_uintmap *map) { - hash_uintmap *hash_uint_local; - HASH_FIND_INT(hash_uintmaps, &map->index, hash_uint_local); - if (hash_uint_local == NULL) { - HASH_ADD_INT(hash_uintmaps, index, map); - } else { - HASH_REPLACE_INT(hash_uintmaps, index, map, hash_uint_local); - } - dogecoin_free(hash_uint_local); -} - -/** - * @brief This function takes an index and returns the working - * hash_uintmap associated with that index in the hash_uint table. - * - * @param index The index of the target working hash_uintmap. - * - * @return The pointer to the working hash_uintmap associated with - * the provided index. - */ -hash_uintmap* find_hash_uintmap(int index) { - hash_uintmap *map; - HASH_FIND_INT(hash_uintmaps, &index, map); - return map; -} - -/** - * @brief This function removes the specified working hash_uintmap - * from the hash_uint table and frees the hash_uintmaps in memory. - * - * @param map The pointer to the hash_uintmap to remove. - * - * @return Nothing. - */ -void remove_hash_uintmap(hash_uintmap *map) { - HASH_DEL(hash_uintmaps, map); /* delete it (hash_uintmaps advances to next) */ - dogecoin_free(map); -} - -/** - * @brief This function removes all working hash_uintmaps from - * the hash_uint table. - * - * @return Nothing. - */ -void remove_all_hash_uintmaps() { - struct hash_uintmap *map; - struct hash_uintmap *tmp; - - HASH_ITER(hh, hash_uintmaps, map, tmp) { - remove_hash_uintmap(map); - } -} - /* base_uint functions */ void shift_left(int index, uint256 b, unsigned int shift) { @@ -492,7 +333,6 @@ void base_uint_set_hex(base_uint* a, const char* psz) { p1++; } } - printf("psz: %s\n", a->to_string(a)); } char* base_uint_to_string(const struct base_uint* a) { @@ -590,7 +430,7 @@ uint32_t get_compact(swap* a, dogecoin_bool f_negative) uint32_t get_compact_hash_uint(int index) { - hash_uint* a = find_hash_uint(a); + hash_uint* a = find_hash_uint(index); print_hash_uint(index); int nSize = (hash_uint_bits(a) + 7) / 8; uint32_t nCompact = 0; @@ -598,7 +438,7 @@ uint32_t get_compact_hash_uint(int index) nCompact = get_hash_uint_low64(a) << 8 * (3 - nSize); } else { swap* bn = dogecoin_calloc(1, sizeof(*bn)); - bn->u32[0] = (uint32_t)&a->x.u32[0] >> 8 * (nSize - 3); + bn->u32[0] = (uint32_t)a->x.u32[0] >> 8 * (nSize - 3); nCompact = get_low64(bn); dogecoin_free(bn); } @@ -611,18 +451,13 @@ uint32_t get_compact_hash_uint(int index) assert((nCompact & ~0x007fffff) == 0); assert(nSize < 256); nCompact |= nSize << 24; - nCompact |= (&a->checks.negative && (nCompact & 0x007fffff) ? 0x00800000 : 0); + nCompact |= (a->checks.negative && (nCompact & 0x007fffff) ? 0x00800000 : 0); return nCompact; } swap* set_compact(swap* hash_uint, uint32_t compact, dogecoin_bool *pf_negative, dogecoin_bool *pf_overflow) { int size = compact >> 24; uint32_t word = compact & 0x007fffff; - printf("word: %lu\n", word); - printf("size: %d\n", size); - printf("size: %d\n", pf_negative); - printf("size: %d\n", &pf_negative); - printf("size: %d\n", *pf_negative); if (size <= 3) { word >>= 8 * (3 - size); hash_uint->u32[0] = word; @@ -638,7 +473,6 @@ swap* set_compact(swap* hash_uint, uint32_t compact, dogecoin_bool *pf_negative, (word > 0xff && size > 33) || (word > 0xffff && size > 32)); } - printf("size: %d\n", *pf_negative); return hash_uint; } diff --git a/src/block.c b/src/block.c index 8d62bdebb..75be746c7 100644 --- a/src/block.c +++ b/src/block.c @@ -197,6 +197,10 @@ void dogecoin_auxpow_block_free(dogecoin_auxpow_block* block) { if (!block) return; dogecoin_block_header_free(block->header); dogecoin_tx_free(block->parent_coinbase); + int i = 0; + for (; i < block->parent_merkle_count; i++) { + dogecoin_free(block->parent_coinbase_merkle[i]); + } block->parent_merkle_count = 0; block->aux_merkle_count = 0; remove_all_hashes(); @@ -299,12 +303,8 @@ int dogecoin_block_header_deserialize(dogecoin_block_header* header, struct cons if (!deser_u32(&block->header->nonce, buf)) return false; dogecoin_block_header_copy(header, block->header); - if ((block->header->version & BLOCK_VERSION_AUXPOW_BIT) != 0) { - if (!deserialize_dogecoin_auxpow_block(block, buf)) { - printf("deserialize_dogecoin_auxpow_block failed!\n"); - // dogecoin_auxpow_block_free(block); - // return false; - } + if ((block->header->version & BLOCK_VERSION_AUXPOW_BIT && buf->len) != 0) { + deserialize_dogecoin_auxpow_block(block, buf); } dogecoin_auxpow_block_free(block); return true; diff --git a/src/headersdb_file.c b/src/headersdb_file.c index 67b32b73d..69a9b0917 100644 --- a/src/headersdb_file.c +++ b/src/headersdb_file.c @@ -260,7 +260,7 @@ dogecoin_blockindex * dogecoin_headers_db_connect_hdr(dogecoin_headers_db* db, s *connected = false; dogecoin_blockindex *blockindex = dogecoin_calloc(1, sizeof(dogecoin_blockindex)); - if (!dogecoin_block_header_deserialize(&blockindex->header, buf)) return NULL; + dogecoin_block_header_deserialize(&blockindex->header, buf); dogecoin_block_header_hash(&blockindex->header, (uint8_t *)&blockindex->hash); diff --git a/src/wallet.c b/src/wallet.c index db86f0127..fca6d073c 100644 --- a/src/wallet.c +++ b/src/wallet.c @@ -337,7 +337,7 @@ void dogecoin_wallet_free(dogecoin_wallet* wallet) dogecoin_btree_tdestroy(wallet->wtxes_rbtree, dogecoin_free); if (wallet->waddr_vector) { - vector_free(wallet->waddr_vector, true); + vector_free(wallet->waddr_vector, false); wallet->waddr_vector = NULL; } diff --git a/test/arith_uint256_tests.c b/test/arith_uint256_tests.c index 1e36cb1c1..2d674a622 100644 --- a/test/arith_uint256_tests.c +++ b/test/arith_uint256_tests.c @@ -59,39 +59,7 @@ void shiftArrayLeft(unsigned char* to, const unsigned char* from, unsigned int a } } -bool almostEqual(double d1, double d2) -{ - return fabs(d1-d2) <= 4*fabs(d1)*__DBL_EPSILON__; -} - -void foo (int i) -{ - printf ("foo %d!\n", i); -} - -void bar (int i) -{ - printf ("%d bar!\n", i); -} - -void message (void (*func)(int), int times) -{ - int j; - for (j=0; jchecks.overflow, false); // Make sure that we don't generate compacts with the 0x00800000 bit set - // set_hash_uint(num_index, 0x80, typename(0x80)); - // num = 0x80; - // u_assert_uint32_eq(get_compact(&num->x, num->checks.negative), 0x02008000U); + set_hash_uint(num_index, 0x80, typename(0x80)); + num->x.u8[0] = 0x80; + u_assert_uint32_eq(get_compact(&num->x, num->checks.negative), 0x02008000U); set_hash_uint(num_index, 0x01fe0000, typename(0x01fe0000)); print_hash_uint(num_index); @@ -293,8 +261,7 @@ void test_arith_uint256() { set_hash_uint(num_index, 0x20123456, typename(0x20123456)); print_hash_uint(num_index); u_assert_str_eq(get_hash_uint_by_index(num_index), "1234560000000000000000000000000000000000000000000000000000000000"); - // u_assert_uint32_eq(get_compact(&num->x, num->checks.negative), 0x20123456U); - // u_assert_uint32_eq(get_compact_hash_uint(num_index), 0x20123456U); + u_assert_uint32_eq(get_compact_hash_uint(num_index), 0x20123456U); u_assert_int_eq(num->checks.negative, false); u_assert_int_eq(num->checks.overflow, false); @@ -303,17 +270,8 @@ void test_arith_uint256() { u_assert_int_eq(num->checks.negative, false); u_assert_int_eq(num->checks.overflow, true); - - remove_all_hash_uintes(); + remove_all_hash_uints(); // constructors, equality, inequality u_assert_int_eq(1, 0 + 1); - - - // dogecoin_free(R1L); - // dogecoin_free(R2L); - // dogecoin_free(OneL); - // dogecoin_free(MaxL); - // dogecoin_free(ZeroL); - // vector_free(tmp, false); } diff --git a/test/unittester.c b/test/unittester.c index 779915419..e27cd5caf 100644 --- a/test/unittester.c +++ b/test/unittester.c @@ -165,8 +165,8 @@ int main() #endif #ifdef WITH_WALLET - // u_run_test(test_wallet_basics); - // u_run_test(test_wallet); + u_run_test(test_wallet_basics); + u_run_test(test_wallet); #endif #ifdef WITH_TOOLS @@ -177,7 +177,7 @@ int main() u_run_test(test_net_flag_defined); u_run_test(test_net_basics_plus_download_block); u_run_test(test_protocol); -// u_run_test(test_spv); + // u_run_test(test_spv); #else u_run_test(test_net_flag_not_defined); #endif diff --git a/test/wallet_tests.c b/test/wallet_tests.c index ac85b1247..476763caa 100644 --- a/test/wallet_tests.c +++ b/test/wallet_tests.c @@ -205,7 +205,6 @@ void test_wallet_basics() dogecoin_wallet_addr *wallet_addr = dogecoin_wallet_next_addr(wallet); u_assert_int_eq(wallet_addr->childindex, 0); - dogecoin_wallet_free(wallet); wallet = dogecoin_wallet_new(&dogecoin_chainparams_main); u_assert_int_eq(dogecoin_wallet_load(wallet, wallettmpfile, &error, &created), true); @@ -235,7 +234,6 @@ void test_wallet_basics() u_assert_is_null(waddr_search); dogecoin_wallet_flush(wallet); - dogecoin_wallet_free(wallet); wallet = dogecoin_wallet_new(&dogecoin_chainparams_main); u_assert_int_eq(dogecoin_wallet_load(wallet, wallettmpfile, &error, &created), true);