Skip to content

Commit

Permalink
qa: fix remaining test cases on arith_uint256_tests
Browse files Browse the repository at this point in the history
-rename remove_all_hash_uintes to remove_all_hash_uints
  • Loading branch information
xanimo committed Jun 30, 2023
1 parent e9bca2b commit 0ec51dd
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 323 deletions.
92 changes: 1 addition & 91 deletions include/dogecoin/arith_uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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++)
Expand Down Expand Up @@ -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;
}

Expand Down
178 changes: 6 additions & 172 deletions src/arith_uint256.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#include <dogecoin/dogecoin.h>
#include <dogecoin/common.h>



/**
* @brief This function instantiates a new working hash_uint,
* but does not add it to the hash_uint table.
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -590,15 +430,15 @@ 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;
if (nSize <= 3) {
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);
}
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
12 changes: 6 additions & 6 deletions src/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 0ec51dd

Please sign in to comment.