Skip to content

Commit 031cac6

Browse files
add cpp
1 parent 16bee26 commit 031cac6

File tree

8 files changed

+71
-14
lines changed

8 files changed

+71
-14
lines changed

Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ AUTOMAKE_OPTIONS = foreign
22

33
SUBDIRS = include
44

5+
#lib_LTLIBRARIES = libutreexo_cpp.la
6+
#libutreexo_cpp_la_SOURCES = include/cpp/utreexo.cpp
7+
#libutreexo_cpp_la_LDFLAGS = -version-info 0:1:0
8+
59
check_PROGRAMS = test_flat_file test_forest
610
test_flat_file_SOURCES = src/test_flat_file.c
711

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,7 @@ AC_CONFIG_HEADERS([src/config.h])
7979
AC_CONFIG_FILES([Makefile])
8080
AC_CONFIG_FILES([include/Makefile])
8181

82+
AC_LANG([C++])
83+
AC_PROG_CXX
84+
8285
AC_OUTPUT

include/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
headers = utreexo.h
1+
headers = utreexo.h utreexo.hpp
22

33
include_HEADERS = $(headers)

include/utreexo.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#ifndef UTREEXO_H
5353
#define UTREEXO_H
5454

55+
#ifdef __cplusplus
56+
extern "C" {
57+
#endif
58+
5559
#include <stdint.h>
5660

5761
/**
@@ -81,11 +85,23 @@ typedef struct utreexo_forest_ *utreexo_forest;
8185
* forest can have up to 64 trees (which is more than enough for most use
8286
* cases).
8387
*
88+
* This method returns 0 if everything goes Ok, 1 otherwise.
89+
*
8490
* Out: p: The newly created forest
8591
* In: filename: File name of the forest backend. If the file doesn't exist,
8692
* it'll be created.
8793
*/
88-
extern void utreexo_forest_init(utreexo_forest *p, const char *filename);
94+
extern int utreexo_forest_init(utreexo_forest *p, const char *filename);
95+
96+
/**
97+
* Frees-up a forest. This method should be called when you're done with
98+
* the forest, otherwise may cause resource leak.
99+
*
100+
* This method doesn't fail.
101+
*
102+
* In: p: A valid forest that have been innitialized using utreexo_forest_init
103+
*/
104+
extern int utreexo_forest_free(utreexo_forest p);
89105

90106
/**
91107
* Modify is the main interface for a utreexo forest. Because order matters
@@ -94,13 +110,15 @@ extern void utreexo_forest_init(utreexo_forest *p, const char *filename);
94110
* takes as arguments the leaf that should be added/removed, and the number of
95111
* leaves for each operation.
96112
*
113+
* This method returns 0 if everything goes Ok, 1 otherwise.
114+
*
97115
* Out: p: The newly created forest
98116
* In: leaf: The leaf that should be added
99117
* leaf_count: The number of leaves that should be added/removed
100118
*/
101-
extern void utreexo_forest_modify(utreexo_forest forest,
102-
utreexo_node_hash *utxos, int utxo_count,
103-
utreexo_node_hash *stxos, int stxo_count);
119+
extern int utreexo_forest_modify(utreexo_forest forest,
120+
utreexo_node_hash *utxos, int utxo_count,
121+
utreexo_node_hash *stxos, int stxo_count);
104122

105123
/**
106124
* Prove that some elements are in the forest. This function takes as input
@@ -110,6 +128,10 @@ extern void utreexo_forest_modify(utreexo_forest forest,
110128
* In: leaf: The leaf that should be added
111129
* leaf_count: The number of leaves that should be added/removed
112130
*/
113-
static void utreexo_forest_prove(utreexo_forest forest, utreexo_node_hash *leaf,
114-
int leaf_count, utreexo_node_hash *proof);
131+
static int utreexo_forest_prove(utreexo_forest forest, utreexo_node_hash *leaf,
132+
int leaf_count, utreexo_node_hash *proof);
133+
#ifdef __cplusplus
134+
}
135+
#endif // __cplusplus
136+
115137
#endif

include/utreexo.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <utreexo.h>
2+
#include <vector>
3+
4+
class UtreexoForest {
5+
private:
6+
utreexo_forest forest;
7+
8+
public:
9+
UtreexoForest(char *path) { utreexo_forest_init(&this->forest, path); };
10+
UtreexoForest() { utreexo_forest_init(&this->forest, "forest.bin"); };
11+
~UtreexoForest() { utreexo_forest_free(this->forest); };
12+
void Modify(std::vector<utreexo_node_hash> utxos,
13+
std::vector<utreexo_node_hash> stxos) {
14+
utreexo_forest_modify(this->forest, utxos.data(), utxos.size(),
15+
stxos.data(), stxos.size());
16+
};
17+
};

src/map_forest_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static inline void grab_node(struct utreexo_forest *f,
9595
*parent = pparent;
9696
}
9797

98-
static inline void utreexo_forest_free(struct utreexo_forest *forest) {
98+
static inline void _utreexo_forest_free(struct utreexo_forest *forest) {
9999
utreexo_forest_file_close(forest->data);
100100
free(forest);
101101
}

src/mmap_forest.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,37 @@ static const int utreexo_forest_version_major = 0;
1313

1414
#define CHECK_PTR(x) \
1515
if (x == NULL) { \
16-
errno = -1; \
17-
return; \
16+
return 1; \
1817
}
1918

20-
void utreexo_forest_modify(struct utreexo_forest *forest,
21-
utreexo_node_hash *leaf, int leaf_count) {
19+
int utreexo_forest_modify(struct utreexo_forest *forest,
20+
utreexo_node_hash *leaf, int leaf_count) {
21+
2222
for (int i = 0; i < leaf_count; i++) {
2323
utreexo_forest_add(forest, leaf[i]);
2424
}
25+
26+
return 1;
27+
}
28+
int utreexo_forest_free(struct utreexo_forest *p) {
29+
_utreexo_forest_free(p);
30+
return 0;
2531
}
2632

27-
void utreexo_forest_init(struct utreexo_forest **p, const char *filename) {
33+
int utreexo_forest_init(struct utreexo_forest **p, const char *filename) {
34+
CHECK_PTR(p);
35+
CHECK_PTR(filename);
2836

2937
struct utreexo_forest *forest = malloc(sizeof(struct utreexo_forest));
3038
struct utreexo_forest_file *file = NULL;
3139
char *heap;
40+
3241
utreexo_forest_file_init(&file, (void **)&heap, filename);
3342

3443
forest->data = file;
3544
forest->nLeaf = (uint64_t *)heap;
3645
forest->roots = (utreexo_forest_node **)(heap + sizeof(uint64_t));
46+
3747
*p = forest;
48+
return 0;
3849
}

src/mmap_forest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct utreexo_forest {
2727
static inline void utreexo_forest_add(struct utreexo_forest *p,
2828
utreexo_node_hash leaf);
2929
/* Free up a forest. */
30-
static inline void utreexo_forest_free(struct utreexo_forest *p);
30+
static inline void _utreexo_forest_free(struct utreexo_forest *p);
3131

3232
/* Deletes a single node from a forest.
3333
*

0 commit comments

Comments
 (0)