aboutsummaryrefslogtreecommitdiffstats
path: root/shared/librelational_engine/library.cpp
diff options
context:
space:
mode:
authorMarcos Magueta <maguetamarcos@gmail.com>2024-10-27 01:53:18 -0300
committerGitHub <noreply@github.com>2024-10-27 01:53:18 -0300
commitfcb5613755b08fcb7ae73a3826b1cdce40e2747f (patch)
treee9f1df3a4e0d9b2bb38fdc432edeae21424dbe99 /shared/librelational_engine/library.cpp
parent2eab114a120c7818861c0640ecdc300cf7366e53 (diff)
downloadSakura-fcb5613755b08fcb7ae73a3826b1cdce40e2747f.tar.gz
Sakura-fcb5613755b08fcb7ae73a3826b1cdce40e2747f.zip
State of the world with Merkle Tree composition of files (#2)
* file state hash calculation with history on replace and insert * add state transitions with linear hashing * add test * format dune files * nix format * delete shared flake * disable fmt check on flake * add nix formatter to flake shell
Diffstat (limited to '')
-rw-r--r--shared/librelational_engine/library.cpp47
1 files changed, 45 insertions, 2 deletions
diff --git a/shared/librelational_engine/library.cpp b/shared/librelational_engine/library.cpp
index 8048486..cab8f93 100644
--- a/shared/librelational_engine/library.cpp
+++ b/shared/librelational_engine/library.cpp
@@ -2,8 +2,51 @@
#include <merklecpp.h>
#include <openssl/sha.h>
#include <iomanip>
+#include <fstream>
+#include <iterator>
+
+#include <sys/stat.h>
+
+std::vector<uint8_t> read_tree(const char* tree_path) {
+ std::ifstream file(tree_path, std::ios::binary);
+ file.unsetf(std::ios::skipws);
+ file.seekg(0, std::ios::end);
+ const std::streampos file_size = file.tellg();
+ file.seekg(0, std::ios::beg);
+ std::vector<uint8_t> v;
+ v.reserve(file_size);
+ v.insert(v.begin(), std::istream_iterator<uint8_t>(file), std::istream_iterator<uint8_t>());
+ return v;
+}
+
+void write_tree(const char* tree_path, const std::vector<uint8_t>& serialized_tree) {
+ std::ofstream out(tree_path, std::ios::out | std::ios::binary);
+ out.write(reinterpret_cast<const char*>(serialized_tree.data()), serialized_tree.size());
+ out.close();
+}
+
extern "C" {
#include "library.h"
+ extern void register_to_merkle(const char* tree_path, const char* computed_hash) {
+ struct stat st;
+ if (stat(tree_path, &st) == 0) {
+ const std::vector<uint8_t> tree_bytes = read_tree(tree_path);
+ merkle::Tree tree;
+ tree.deserialise(tree_bytes);
+ const merkle::Tree::Hash hash(computed_hash);
+ tree.insert(hash);
+ std::vector<uint8_t> target;
+ tree.serialise(target);
+ write_tree(tree_path, target);
+ } else {
+ merkle::Tree::Hash hash(computed_hash);
+ merkle::Tree tree;
+ tree.insert(hash);
+ std::vector<uint8_t> target;
+ tree.serialise(target);
+ write_tree(tree_path, target);
+ }
+ }
extern void merkle_generate_root(char** hashes, const int n, char* root) {
merkle::Tree tree;
for (int i = 0; i < n; i++){
@@ -23,8 +66,8 @@ extern "C" {
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
std::memcpy(computed_hash, ss.str().c_str(), HASH_SIZE);
- std::cout << "⚙️ COMPUTED HASH: " << computed_hash << std::endl;
+ // std::cout << "⚙️ COMPUTED HASH: " << computed_hash << std::endl;
}
extern int trash() { return 100; }
-} \ No newline at end of file
+}