Skip to content

Commit 0a4046d

Browse files
fix tests
1 parent 886979f commit 0a4046d

File tree

6 files changed

+88
-59
lines changed

6 files changed

+88
-59
lines changed

contrib/run_tests.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
make check &> /dev/null
2+
3+
./test_flat_file
4+
./test_forest
5+
./test_leaf_map
6+
7+
# clean up
8+
rm *.bin

src/forest_test.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
static inline struct utreexo_forest get_test_forest(const char *filename) {
1515
void *heap = NULL;
1616
struct utreexo_forest_file *file = NULL;
17+
18+
char forest_name[100];
19+
sprintf(forest_name, "forest_%s", filename);
20+
utreexo_forest_file_init(&file, &heap, forest_name);
1721

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

2124
char map_name[100];
22-
sprintf(map_name, "map_%s", filename);
25+
sprintf(map_name, "forest_map_%s", filename);
2326

2427
utreexo_leaf_map map;
2528
utreexo_leaf_map_new(&map, map_name, O_CREAT | O_RDWR, NULL);
@@ -158,10 +161,10 @@ void test_add_single() {
158161

159162
void *heap = NULL;
160163
struct utreexo_forest_file *file = NULL;
161-
utreexo_forest_file_init(&file, &heap, "add_single.bin");
164+
utreexo_forest_file_init(&file, &heap, "forest_add_single.bin");
162165

163166
utreexo_leaf_map leaf_map;
164-
utreexo_leaf_map_new(&leaf_map, "leaves_single.bin", O_CREAT | O_RDWR, NULL);
167+
utreexo_leaf_map_new(&leaf_map, "forest_leaves_single.bin", O_CREAT | O_RDWR, NULL);
165168

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

179182
void test_add_two() {
180183
TEST_BEGIN("add_two");
181-
utreexo_node_hash leaf = {.hash = {0}};
182-
hash_from_u8(leaf.hash, 0);
184+
utreexo_node_hash leaf1 = {.hash = {0}};
185+
utreexo_node_hash leaf2 = {.hash = {0}};
186+
hash_from_u8(leaf1.hash, 0);
187+
hash_from_u8(leaf2.hash, 1);
183188
struct utreexo_forest p = get_test_forest("add_two.bin");
184-
utreexo_forest_add(&p, leaf);
185-
utreexo_forest_add(&p, leaf);
189+
utreexo_forest_add(&p, leaf1);
190+
utreexo_forest_add(&p, leaf2);
186191
utreexo_forest_node *root = p.roots[1];
187192

188193
unsigned char expected[32] = {0};
189-
parent_hash(expected, leaf.hash, leaf.hash);
194+
parent_hash(expected, leaf1.hash, leaf2.hash);
190195

191196
ASSERT_ARRAY_EQ(root->hash.hash, expected, 32);
192197
TEST_END;

src/leaf_map_impl.h

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ static inline void utreexo_leaf_map_get(utreexo_leaf_map *map,
6363
position = utreexo_leaf_map_get_pos(hash);
6464
++hash;
6565

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

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

8990
unsigned int hash = map->hash(key);
9091
leaf_offset position = 0;
92+
9193
do {
9294
position = utreexo_leaf_map_get_pos(hash);
9395
++hash;
94-
95-
lseek(map->fd, position, SEEK_SET);
96-
read(map->fd, &pnode, sizeof(utreexo_forest_node **));
97-
98-
if (pnode == utreexo_thumbstone) {
99-
continue;
100-
}
101-
96+
97+
pread(map->fd, &pnode, sizeof(utreexo_forest_node **), position);
98+
10299
if (pnode == NULL)
103100
break;
104101
} while (1);
105102
// this node is already here
106103
if (pnode != NULL && pnode != utreexo_thumbstone)
107104
return;
108105

109-
lseek(map->fd, position, SEEK_SET);
110-
write(map->fd, &node, sizeof(utreexo_forest_node *));
106+
pwrite(map->fd, &node, sizeof(utreexo_forest_node *), position);
111107
}
112108

113-
static inline void utreexo_leaf_delete(utreexo_leaf_map *map,
114-
utreexo_node_hash leaf) {
109+
static inline void utreexo_leaf_map_delete(utreexo_leaf_map *map,
110+
utreexo_node_hash leaf) {
115111
utreexo_forest_node *pnode = NULL;
116112
unsigned char key[36] = {0};
117113

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

127-
lseek(map->fd, position, SEEK_SET);
128-
read(map->fd, &pnode, sizeof(utreexo_forest_node *));
129-
130-
if (pnode == utreexo_thumbstone) {
123+
pread(map->fd, &pnode, sizeof(utreexo_forest_node *), position);
124+
125+
if (pnode == utreexo_thumbstone)
131126
continue;
132-
}
133127

134128
// this node doesn't exist
135-
if (pnode == NULL) {
129+
if (pnode == NULL)
136130
break;
137-
}
131+
138132
// we found the node
139133
if (memcmp(pnode->hash.hash, leaf.hash, 32) == 0)
140134
break;
141135
} while (1);
136+
142137
// node not found, return early
143138
if (pnode == NULL)
144139
return;
@@ -147,6 +142,5 @@ static inline void utreexo_leaf_delete(utreexo_leaf_map *map,
147142
// our open hashing alogritm wouldn't see the colliding elements added
148143
// afterwards.
149144
pnode = utreexo_thumbstone;
150-
lseek(map->fd, position, SEEK_SET);
151-
write(map->fd, &pnode, sizeof(utreexo_forest_node **));
145+
pwrite(map->fd, &pnode, sizeof(utreexo_forest_node **), position);
152146
}

src/mmap_forest.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,19 @@ extern int utreexo_forest_modify(struct utreexo_forest *forest,
2626
CHECK_PTR_VAR(utxos, utxo_count);
2727
CHECK_PTR_VAR(stxos, stxo_count);
2828

29-
/* for (size_t utxo = 0; utxo < utxo_count; ++utxo) {
30-
utreexo_forest_node *pnode = NULL;
31-
utreexo_leaf_map_get(&forest->leaf_map, &pnode, utxos[utxo]);
32-
if (pnode == NULL)
33-
return -3;
34-
if (delete_single(forest, pnode)) {
35-
return -2;
36-
}
37-
}
29+
for (size_t utxo = 0; utxo < utxo_count; ++utxo) {
30+
utreexo_forest_node *pnode = NULL;
31+
utreexo_leaf_map_get(&forest->leaf_map, &pnode, utxos[utxo]);
32+
if (pnode == NULL)
33+
return -3;
34+
if (delete_single(forest, pnode)) {
35+
return -2;
36+
}
37+
}
3838

39-
for (size_t i = 0; i < utxo_count; i++) {
40-
utreexo_forest_add(forest, utxos[i]);
41-
}*/
42-
VALGRIND_MAKE_MEM_UNDEFINED(forest->data->header->wrt_page, sizeof(void *));
4339
for (size_t i = 0; i < utxo_count; i++) {
4440
utreexo_forest_add(forest, utxos[i]);
4541
}
46-
4742
return 0;
4843
}
4944

src/test_flat_file.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void test_free_page_list();
3131
int main() {
3232
struct utreexo_forest_file *file;
3333
void *heap = NULL;
34-
utreexo_forest_file_init(&file, &heap, "test.bin");
34+
utreexo_forest_file_init(&file, &heap, "flat_file_test.bin");
3535

3636
const utreexo_forest_node *parent = test_create_nodes(file);
3737
test_retrieve_nodes(parent);
@@ -114,7 +114,7 @@ void test_add_many(int n_adds) {
114114

115115
void *heap = NULL;
116116
struct utreexo_forest_file *file;
117-
utreexo_forest_file_init(&file, &heap, "add_many.bin");
117+
utreexo_forest_file_init(&file, &heap, "flat_fileadd_many.bin");
118118

119119
for (int i = 0; i < n_adds; i++) {
120120
utreexo_forest_file_node_alloc(file);
@@ -128,7 +128,7 @@ void test_free_page_list() {
128128
TEST_BEGIN("test free page reallocation");
129129
struct utreexo_forest_file *file;
130130
void *heap = NULL;
131-
utreexo_forest_file_init(&file, &heap, "reallocation.bin");
131+
utreexo_forest_file_init(&file, &heap, "flat_file_reallocation.bin");
132132
utreexo_forest_node *nodes[NODES_PER_PAGE] = {0};
133133
const utreexo_forest_node node = {
134134
.hash = {{0}},

src/test_leaf_map.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ int main() {
1414
void *_ptr;
1515
utreexo_leaf_map map;
1616

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

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

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

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

5757
// delete both nodes
58-
utreexo_leaf_delete(&map, (utreexo_leaf_hash){.hash = {1}});
59-
utreexo_leaf_delete(&map, (utreexo_leaf_hash){.hash = {2}});
58+
utreexo_leaf_map_delete(&map, (utreexo_leaf_hash){.hash = {1}});
59+
utreexo_leaf_map_delete(&map, (utreexo_leaf_hash){.hash = {2}});
6060

6161
TEST_END;
6262
}
@@ -67,8 +67,8 @@ int main() {
6767
void *_ptr;
6868
utreexo_leaf_map map;
6969

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

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

8989
TEST_END;
9090
}
91+
{
92+
TEST_BEGIN("add one thousand, delete one and try to get it back");
93+
struct utreexo_forest_file *file = NULL;
94+
void *_ptr;
95+
utreexo_leaf_map map;
96+
utreexo_leaf_map_new(&map, "leaf_map_leaves4.bin", O_CREAT | O_RDWR, NULL);
97+
utreexo_forest_file_init(&file, &_ptr, "leaf_map_test_map4.bin");
98+
99+
for (size_t i = 0; i < 1000; ++i) {
100+
utreexo_forest_node *n = utreexo_forest_file_node_alloc(file);
101+
memmove(&n->hash.hash, &i, sizeof(size_t));
102+
utreexo_leaf_map_set(&map, n, n->hash);
103+
}
104+
105+
utreexo_leaf_hash del_hash;
106+
size_t i = 1;
107+
memmove(del_hash.hash, &i, sizeof(size_t));
108+
109+
utreexo_forest_node *n = NULL;
110+
utreexo_leaf_map_get(&map, &n, del_hash);
111+
assert(n != NULL);
112+
utreexo_leaf_map_delete(&map, del_hash);
113+
114+
utreexo_leaf_map_get(&map, &n, del_hash);
115+
assert(n == NULL);
116+
TEST_END;
117+
}
91118
}

0 commit comments

Comments
 (0)