Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidson-Souza committed Sep 3, 2024
1 parent 886979f commit 0a4046d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 59 deletions.
8 changes: 8 additions & 0 deletions contrib/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
make check &> /dev/null

./test_flat_file
./test_forest
./test_leaf_map

# clean up
rm *.bin
23 changes: 14 additions & 9 deletions src/forest_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
static inline struct utreexo_forest get_test_forest(const char *filename) {
void *heap = NULL;
struct utreexo_forest_file *file = NULL;

char forest_name[100];
sprintf(forest_name, "forest_%s", filename);
utreexo_forest_file_init(&file, &heap, forest_name);

utreexo_forest_file_init(&file, &heap, filename);
uint8_t *roots = ((uint8_t *)heap) + sizeof(uint64_t);

char map_name[100];
sprintf(map_name, "map_%s", filename);
sprintf(map_name, "forest_map_%s", filename);

utreexo_leaf_map map;
utreexo_leaf_map_new(&map, map_name, O_CREAT | O_RDWR, NULL);
Expand Down Expand Up @@ -158,10 +161,10 @@ void test_add_single() {

void *heap = NULL;
struct utreexo_forest_file *file = NULL;
utreexo_forest_file_init(&file, &heap, "add_single.bin");
utreexo_forest_file_init(&file, &heap, "forest_add_single.bin");

utreexo_leaf_map leaf_map;
utreexo_leaf_map_new(&leaf_map, "leaves_single.bin", O_CREAT | O_RDWR, NULL);
utreexo_leaf_map_new(&leaf_map, "forest_leaves_single.bin", O_CREAT | O_RDWR, NULL);

struct utreexo_forest p = {
.data = file,
Expand All @@ -178,15 +181,17 @@ void test_add_single() {

void test_add_two() {
TEST_BEGIN("add_two");
utreexo_node_hash leaf = {.hash = {0}};
hash_from_u8(leaf.hash, 0);
utreexo_node_hash leaf1 = {.hash = {0}};
utreexo_node_hash leaf2 = {.hash = {0}};
hash_from_u8(leaf1.hash, 0);
hash_from_u8(leaf2.hash, 1);
struct utreexo_forest p = get_test_forest("add_two.bin");
utreexo_forest_add(&p, leaf);
utreexo_forest_add(&p, leaf);
utreexo_forest_add(&p, leaf1);
utreexo_forest_add(&p, leaf2);
utreexo_forest_node *root = p.roots[1];

unsigned char expected[32] = {0};
parent_hash(expected, leaf.hash, leaf.hash);
parent_hash(expected, leaf1.hash, leaf2.hash);

ASSERT_ARRAY_EQ(root->hash.hash, expected, 32);
TEST_END;
Expand Down
44 changes: 19 additions & 25 deletions src/leaf_map_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ static inline void utreexo_leaf_map_get(utreexo_leaf_map *map,
position = utreexo_leaf_map_get_pos(hash);
++hash;

lseek(map->fd, position, SEEK_SET);
read(map->fd, &pnode, sizeof(utreexo_forest_node *));
if (pnode == utreexo_thumbstone)
continue; // we found a deleted node, keep looking
pread(map->fd, &pnode, sizeof(utreexo_forest_node *), position);

// this is a deleted node, keep looking
if (pnode == utreexo_thumbstone)
continue;
// we've reached an empty node and didn't find the leaf
// give up
if (pnode == NULL)
break;
// we found the leaf
if (memcmp(pnode->hash.hash, leaf.hash, 32) == 0)
break;
} while (1);
Expand All @@ -88,30 +89,25 @@ static inline void utreexo_leaf_map_set(utreexo_leaf_map *map,

unsigned int hash = map->hash(key);
leaf_offset position = 0;

do {
position = utreexo_leaf_map_get_pos(hash);
++hash;

lseek(map->fd, position, SEEK_SET);
read(map->fd, &pnode, sizeof(utreexo_forest_node **));

if (pnode == utreexo_thumbstone) {
continue;
}


pread(map->fd, &pnode, sizeof(utreexo_forest_node **), position);

if (pnode == NULL)
break;
} while (1);
// this node is already here
if (pnode != NULL && pnode != utreexo_thumbstone)
return;

lseek(map->fd, position, SEEK_SET);
write(map->fd, &node, sizeof(utreexo_forest_node *));
pwrite(map->fd, &node, sizeof(utreexo_forest_node *), position);
}

static inline void utreexo_leaf_delete(utreexo_leaf_map *map,
utreexo_node_hash leaf) {
static inline void utreexo_leaf_map_delete(utreexo_leaf_map *map,
utreexo_node_hash leaf) {
utreexo_forest_node *pnode = NULL;
unsigned char key[36] = {0};

Expand All @@ -124,21 +120,20 @@ static inline void utreexo_leaf_delete(utreexo_leaf_map *map,
position = utreexo_leaf_map_get_pos(hash);
++hash;

lseek(map->fd, position, SEEK_SET);
read(map->fd, &pnode, sizeof(utreexo_forest_node *));

if (pnode == utreexo_thumbstone) {
pread(map->fd, &pnode, sizeof(utreexo_forest_node *), position);

if (pnode == utreexo_thumbstone)
continue;
}

// this node doesn't exist
if (pnode == NULL) {
if (pnode == NULL)
break;
}

// we found the node
if (memcmp(pnode->hash.hash, leaf.hash, 32) == 0)
break;
} while (1);

// node not found, return early
if (pnode == NULL)
return;
Expand All @@ -147,6 +142,5 @@ static inline void utreexo_leaf_delete(utreexo_leaf_map *map,
// our open hashing alogritm wouldn't see the colliding elements added
// afterwards.
pnode = utreexo_thumbstone;
lseek(map->fd, position, SEEK_SET);
write(map->fd, &pnode, sizeof(utreexo_forest_node **));
pwrite(map->fd, &pnode, sizeof(utreexo_forest_node **), position);
}
23 changes: 9 additions & 14 deletions src/mmap_forest.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,19 @@ extern int utreexo_forest_modify(struct utreexo_forest *forest,
CHECK_PTR_VAR(utxos, utxo_count);
CHECK_PTR_VAR(stxos, stxo_count);

/* for (size_t utxo = 0; utxo < utxo_count; ++utxo) {
utreexo_forest_node *pnode = NULL;
utreexo_leaf_map_get(&forest->leaf_map, &pnode, utxos[utxo]);
if (pnode == NULL)
return -3;
if (delete_single(forest, pnode)) {
return -2;
}
}
for (size_t utxo = 0; utxo < utxo_count; ++utxo) {
utreexo_forest_node *pnode = NULL;
utreexo_leaf_map_get(&forest->leaf_map, &pnode, utxos[utxo]);
if (pnode == NULL)
return -3;
if (delete_single(forest, pnode)) {
return -2;
}
}

for (size_t i = 0; i < utxo_count; i++) {
utreexo_forest_add(forest, utxos[i]);
}*/
VALGRIND_MAKE_MEM_UNDEFINED(forest->data->header->wrt_page, sizeof(void *));
for (size_t i = 0; i < utxo_count; i++) {
utreexo_forest_add(forest, utxos[i]);
}

return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions src/test_flat_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void test_free_page_list();
int main() {
struct utreexo_forest_file *file;
void *heap = NULL;
utreexo_forest_file_init(&file, &heap, "test.bin");
utreexo_forest_file_init(&file, &heap, "flat_file_test.bin");

const utreexo_forest_node *parent = test_create_nodes(file);
test_retrieve_nodes(parent);
Expand Down Expand Up @@ -114,7 +114,7 @@ void test_add_many(int n_adds) {

void *heap = NULL;
struct utreexo_forest_file *file;
utreexo_forest_file_init(&file, &heap, "add_many.bin");
utreexo_forest_file_init(&file, &heap, "flat_fileadd_many.bin");

for (int i = 0; i < n_adds; i++) {
utreexo_forest_file_node_alloc(file);
Expand All @@ -128,7 +128,7 @@ void test_free_page_list() {
TEST_BEGIN("test free page reallocation");
struct utreexo_forest_file *file;
void *heap = NULL;
utreexo_forest_file_init(&file, &heap, "reallocation.bin");
utreexo_forest_file_init(&file, &heap, "flat_file_reallocation.bin");
utreexo_forest_node *nodes[NODES_PER_PAGE] = {0};
const utreexo_forest_node node = {
.hash = {{0}},
Expand Down
43 changes: 35 additions & 8 deletions src/test_leaf_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ int main() {
void *_ptr;
utreexo_leaf_map map;

utreexo_leaf_map_new(&map, "leaves1.bin", O_CREAT | O_RDWR, chash);
utreexo_forest_file_init(&file, &_ptr, "test_map1.bin");
utreexo_leaf_map_new(&map, "leaf_map_leaves1.bin", O_CREAT | O_RDWR, chash);
utreexo_forest_file_init(&file, &_ptr, "leaf_map_test_map1.bin");

utreexo_forest_node *n = utreexo_forest_file_node_alloc(file);
utreexo_leaf_map_set(&map, n, (utreexo_leaf_hash){.hash = {1}});
Expand All @@ -33,8 +33,8 @@ int main() {
void *_ptr;
utreexo_leaf_map map;

utreexo_leaf_map_new(&map, "leaves2.bin", O_CREAT | O_RDWR, chash);
utreexo_forest_file_init(&file, &_ptr, "test_map2.bin");
utreexo_leaf_map_new(&map, "leaf_map_leaves2.bin", O_CREAT | O_RDWR, chash);
utreexo_forest_file_init(&file, &_ptr, "leaf_map_test_map2.bin");

// alloc a new node
utreexo_forest_node *n = utreexo_forest_file_node_alloc(file);
Expand All @@ -55,8 +55,8 @@ int main() {
ASSERT_EQ(n, n1);

// delete both nodes
utreexo_leaf_delete(&map, (utreexo_leaf_hash){.hash = {1}});
utreexo_leaf_delete(&map, (utreexo_leaf_hash){.hash = {2}});
utreexo_leaf_map_delete(&map, (utreexo_leaf_hash){.hash = {1}});
utreexo_leaf_map_delete(&map, (utreexo_leaf_hash){.hash = {2}});

TEST_END;
}
Expand All @@ -67,8 +67,8 @@ int main() {
void *_ptr;
utreexo_leaf_map map;

utreexo_leaf_map_new(&map, "leaves3.bin", O_CREAT | O_RDWR, NULL);
utreexo_forest_file_init(&file, &_ptr, "test_map3.bin");
utreexo_leaf_map_new(&map, "leaf_map_leaves3.bin", O_CREAT | O_RDWR, NULL);
utreexo_forest_file_init(&file, &_ptr, "leaf_map_test_map3.bin");

for (size_t i = 0; i < 20000; ++i) {
utreexo_forest_node *n = utreexo_forest_file_node_alloc(file);
Expand All @@ -88,4 +88,31 @@ int main() {

TEST_END;
}
{
TEST_BEGIN("add one thousand, delete one and try to get it back");
struct utreexo_forest_file *file = NULL;
void *_ptr;
utreexo_leaf_map map;
utreexo_leaf_map_new(&map, "leaf_map_leaves4.bin", O_CREAT | O_RDWR, NULL);
utreexo_forest_file_init(&file, &_ptr, "leaf_map_test_map4.bin");

for (size_t i = 0; i < 1000; ++i) {
utreexo_forest_node *n = utreexo_forest_file_node_alloc(file);
memmove(&n->hash.hash, &i, sizeof(size_t));
utreexo_leaf_map_set(&map, n, n->hash);
}

utreexo_leaf_hash del_hash;
size_t i = 1;
memmove(del_hash.hash, &i, sizeof(size_t));

utreexo_forest_node *n = NULL;
utreexo_leaf_map_get(&map, &n, del_hash);
assert(n != NULL);
utreexo_leaf_map_delete(&map, del_hash);

utreexo_leaf_map_get(&map, &n, del_hash);
assert(n == NULL);
TEST_END;
}
}

0 comments on commit 0a4046d

Please sign in to comment.