diff --git a/src/hashdb64/smt_64.cpp b/src/hashdb64/smt_64.cpp index 43ad1b1ff..3bab50217 100644 --- a/src/hashdb64/smt_64.cpp +++ b/src/hashdb64/smt_64.cpp @@ -10,6 +10,8 @@ #include "key_utils.hpp" #include "tree_chunk.hpp" +//#define SMT64_PRINT_TREE_CHUNKS + zkresult Smt64::writeTree (Database64 &db, const Goldilocks::Element (&oldRoot)[4], const vector &keyValues, Goldilocks::Element (&newRoot)[4], uint64_t &flushId, uint64_t &lastSentFlushId) { zkresult zkr; @@ -260,12 +262,14 @@ zkresult Smt64::writeTree (Database64 &db, const Goldilocks::Element (&oldRoot)[ return zkr; } +#ifdef SMT64_PRINT_TREE_CHUNKS // Print chunks for (uint c = 0; c < chunks.size(); c++) { zklog.info("Smt64::writeTree() chunk " + to_string(c)); chunks[c]->print(); } +#endif // Free memory for (uint c = 0; c < chunks.size(); c++) delete chunks[c]; @@ -491,12 +495,14 @@ zkresult Smt64::readTree (Database64 &db, const Goldilocks::Element (&root)[4], dbQueries.clear(); +#ifdef SMT64_PRINT_TREE_CHUNKS // Print chunks for (uint c = 0; c < chunks.size(); c++) { zklog.info("Smt64::readTree() chunk " + to_string(c)); chunks[c]->print(); } +#endif // Free memory for (uint c = 0; c < chunks.size(); c++) delete chunks[c]; diff --git a/src/hashdb64/tree_chunk.cpp b/src/hashdb64/tree_chunk.cpp index fc3fc8e1b..57bcb0150 100644 --- a/src/hashdb64/tree_chunk.cpp +++ b/src/hashdb64/tree_chunk.cpp @@ -270,7 +270,7 @@ zkresult TreeChunk::calculateHash (void) } bChildrenRestValid = false; - TimerStart(TREE_CHUNK_CALCULATE_HASH); + //TimerStart(TREE_CHUNK_CALCULATE_HASH); if (level%6 != 0) { @@ -335,7 +335,7 @@ zkresult TreeChunk::calculateHash (void) bHashValid = true; bChildrenRestValid = true; - TimerStopAndLog(TREE_CHUNK_CALCULATE_HASH); + //TimerStopAndLog(TREE_CHUNK_CALCULATE_HASH); return ZKR_SUCCESS; } @@ -351,7 +351,7 @@ zkresult TreeChunk::calculateHash (void) bHashValid = true; bChildrenRestValid = true; - TimerStopAndLog(TREE_CHUNK_CALCULATE_HASH); + //TimerStopAndLog(TREE_CHUNK_CALCULATE_HASH); return ZKR_SUCCESS; } @@ -367,14 +367,14 @@ zkresult TreeChunk::calculateHash (void) bHashValid = true; bChildrenRestValid = true; - TimerStopAndLog(TREE_CHUNK_CALCULATE_HASH); + //TimerStopAndLog(TREE_CHUNK_CALCULATE_HASH); return ZKR_SUCCESS; } default: { zklog.error("TreeChunk::calculateHash() found unexpected child1.type=" + to_string(child1.type)); - TimerStopAndLog(TREE_CHUNK_CALCULATE_HASH); + //TimerStopAndLog(TREE_CHUNK_CALCULATE_HASH); return ZKR_UNSPECIFIED; } } diff --git a/src/service/hashdb/hashdb.cpp b/src/service/hashdb/hashdb.cpp index b71d6802f..348e23410 100644 --- a/src/service/hashdb/hashdb.cpp +++ b/src/service/hashdb/hashdb.cpp @@ -353,6 +353,32 @@ void HashDB::clearCache(void) } } +zkresult HashDB::readTree (const Goldilocks::Element (&root)[4], vector &keyValues) +{ + if (config.hashDB64) + { + return smt64.readTree(db64, root, keyValues); + } + else + { + zklog.error("HashDB::readTree() called with config.hashDB64=false"); + return ZKR_UNSPECIFIED; + } +} + +zkresult HashDB::writeTree (const Goldilocks::Element (&oldRoot)[4], const vector &keyValues, Goldilocks::Element (&newRoot)[4], uint64_t &flushId, uint64_t &lastSentFlushId) +{ + if (config.hashDB64) + { + return smt64.writeTree(db64, oldRoot, keyValues, newRoot, flushId, lastSentFlushId); + } + else + { + zklog.error("HashDB::writeTree() called with config.hashDB64=false"); + return ZKR_UNSPECIFIED; + } +} + void HashDB::setAutoCommit(const bool autoCommit) { #ifdef LOG_TIME_STATISTICS_HASHDB diff --git a/src/service/hashdb/hashdb.hpp b/src/service/hashdb/hashdb.hpp index 5f2f23a7a..b867709db 100644 --- a/src/service/hashdb/hashdb.hpp +++ b/src/service/hashdb/hashdb.hpp @@ -48,6 +48,8 @@ class HashDB : public HashDBInterface zkresult getFlushStatus (uint64_t &storedFlushId, uint64_t &storingFlushId, uint64_t &lastFlushId, uint64_t &pendingToFlushNodes, uint64_t &pendingToFlushProgram, uint64_t &storingNodes, uint64_t &storingProgram, string &proverId); zkresult getFlushData (uint64_t flushId, uint64_t &storedFlushId, unordered_map (&nodes), unordered_map (&program), string &nodesStateRoot); void clearCache (void); + zkresult readTree (const Goldilocks::Element (&root)[4], vector &keyValues); + zkresult writeTree (const Goldilocks::Element (&oldRoot)[4], const vector &keyValues, Goldilocks::Element (&newRoot)[4], uint64_t &flushId, uint64_t &lastSentFlushId); // Methods added for testing purposes void setAutoCommit(const bool autoCommit); diff --git a/src/service/hashdb/hashdb_interface.hpp b/src/service/hashdb/hashdb_interface.hpp index 2372d8500..ebb505e96 100644 --- a/src/service/hashdb/hashdb_interface.hpp +++ b/src/service/hashdb/hashdb_interface.hpp @@ -9,6 +9,8 @@ #include #include "zkresult.hpp" #include "persistence.hpp" +#include "database_64.hpp" +#include "key_value.hpp" class HashDBInterface { @@ -27,6 +29,9 @@ class HashDBInterface virtual zkresult getFlushStatus (uint64_t &storedFlushId, uint64_t &storingFlushId, uint64_t &lastFlushId, uint64_t &pendingToFlushNodes, uint64_t &pendingToFlushProgram, uint64_t &storingNodes, uint64_t &storingProgram, string &proverId) = 0; virtual zkresult getFlushData (uint64_t flushId, uint64_t &storedFlushId, unordered_map (&nodes), unordered_map (&program), string &nodesStateRoot) = 0; virtual void clearCache (void) = 0; + virtual zkresult readTree (const Goldilocks::Element (&root)[4], vector &keyValues) = 0; + virtual zkresult writeTree (const Goldilocks::Element (&oldRoot)[4], const vector &keyValues, Goldilocks::Element (&newRoot)[4], uint64_t &flushId, uint64_t &lastSentFlushId) = 0; + }; #endif \ No newline at end of file diff --git a/src/service/hashdb/hashdb_remote.cpp b/src/service/hashdb/hashdb_remote.cpp index 243de5fd8..1c9677c08 100644 --- a/src/service/hashdb/hashdb_remote.cpp +++ b/src/service/hashdb/hashdb_remote.cpp @@ -406,3 +406,15 @@ zkresult HashDBRemote::getFlushData(uint64_t flushId, uint64_t &storedFlushId, u return ZKR_SUCCESS; } + +zkresult HashDBRemote::readTree (const Goldilocks::Element (&root)[4], vector &keyValues) +{ + zklog.error("HashDBRemote::readTree() called, but this method is only allowed locally"); + return ZKR_UNSPECIFIED; +} + +zkresult HashDBRemote::writeTree (const Goldilocks::Element (&oldRoot)[4], const vector &keyValues, Goldilocks::Element (&newRoot)[4], uint64_t &flushId, uint64_t &lastSentFlushId) +{ + zklog.error("HashDBRemote::writeTree() called, but this method is only allowed locally"); + return ZKR_UNSPECIFIED; +} diff --git a/src/service/hashdb/hashdb_remote.hpp b/src/service/hashdb/hashdb_remote.hpp index 3e0065df7..94dc83cb7 100644 --- a/src/service/hashdb/hashdb_remote.hpp +++ b/src/service/hashdb/hashdb_remote.hpp @@ -14,6 +14,7 @@ #include "zkresult.hpp" #include "utils/time_metric.hpp" #include "timer.hpp" +#include "database_64.hpp" class HashDBRemote : public HashDBInterface { @@ -41,6 +42,8 @@ class HashDBRemote : public HashDBInterface zkresult getFlushStatus (uint64_t &storedFlushId, uint64_t &storingFlushId, uint64_t &lastFlushId, uint64_t &pendingToFlushNodes, uint64_t &pendingToFlushProgram, uint64_t &storingNodes, uint64_t &storingProgram, string &proverId); zkresult getFlushData (uint64_t flushId, uint64_t &storedFlushId, unordered_map (&nodes), unordered_map (&program), string &nodesStateRoot); void clearCache (void) {}; + zkresult readTree (const Goldilocks::Element (&root)[4], vector &keyValues); + zkresult writeTree (const Goldilocks::Element (&oldRoot)[4], const vector &keyValues, Goldilocks::Element (&newRoot)[4], uint64_t &flushId, uint64_t &lastSentFlushId); }; #endif \ No newline at end of file diff --git a/test/hashdb/smt_64_test.cpp b/test/hashdb/smt_64_test.cpp index d181af50f..e3824931b 100644 --- a/test/hashdb/smt_64_test.cpp +++ b/test/hashdb/smt_64_test.cpp @@ -3,17 +3,27 @@ #include "smt.hpp" #include "hashdb_singleton.hpp" #include "unistd.h" +#include "hashdb_factory.hpp" #define SMT64_TEST_NUMBER_OF_KEYS 10 uint64_t Smt64Test (const Config &config) { + TimerStart(SMT64_TEST); + uint64_t numberOfFailedTests = 0; Goldilocks::Element root[4] = {0, 0, 0, 0}; Goldilocks fr; zkresult zkr; vector keyValues; + HashDBInterface *pHashDB = HashDBClientFactory::createHashDBClient(fr, config); + if (pHashDB == NULL) + { + zklog.error("Smt64Test() failed calling HashDBClientFactory::createHashDBClient()"); + return 1; + } + // Init the keyValues for (uint64_t i=0; iwriteTree(root, keyValues, root, flushId, lastSentFlushId); if (zkr != ZKR_SUCCESS) { zklog.error("Smt64Test() failed calling smt64.writeTree() result=" + zkresult2string(zkr)); return 1; } - zklog.info("Smt64Test() smt64.writeTree() returned root=" + fea2string(fr, root) + " flushId=" + to_string(flushId)); + //zklog.info("Smt64Test() smt64.writeTree() returned root=" + fea2string(fr, root) + " flushId=" + to_string(flushId)); + // Wait for the returned flush ID to be sent do { uint64_t storedFlushId, storingFlushId, lastFlushId, pendingToFlushNodes, pendingToFlushProgram, storingNodes, storingProgram; - zkr = db64.getFlushStatus(storedFlushId, storingFlushId, lastFlushId, pendingToFlushNodes, pendingToFlushProgram, storingNodes, storingProgram); + string proverId; + zkr = pHashDB->getFlushStatus(storedFlushId, storingFlushId, lastFlushId, pendingToFlushNodes, pendingToFlushProgram, storingNodes, storingProgram, proverId); if (zkr != ZKR_SUCCESS) { zklog.error("Smt64Test() failed calling db64.getFlushStatus() result=" + zkresult2string(zkr)); @@ -54,24 +63,28 @@ uint64_t Smt64Test (const Config &config) } if (storedFlushId >= flushId) { - zklog.info("Smt64Test() called db64.getFlushStatus() and got storedFlushId=" + to_string(storedFlushId) + " >= flushId=" + to_string(flushId)); + //zklog.info("Smt64Test() called db64.getFlushStatus() and got storedFlushId=" + to_string(storedFlushId) + " >= flushId=" + to_string(flushId)); break; } sleep(1); } while (true); - + // Make a copy of the key values, overwriting the value with an invalid value vector readKeyValues = keyValues; for (uint64_t i=0; ireadTree(root, readKeyValues); if (zkr != ZKR_SUCCESS) { zklog.error("Smt64Test() failed calling smt64.readTree() result=" + zkresult2string(zkr)); return 1; } + + // Check that both keys and values match the ones we wrote for (uint64_t i=0; iset("", 0, root, keyValues[i].key.fe, keyValues[i].value, PERSISTENCE_DATABASE, root, NULL, NULL); if (zkr != ZKR_SUCCESS) { - zklog.error("Smt64Test() failed calling smt.set() result=" + zkresult2string(zkr)); + zklog.error("Smt64Test() failed calling pHashDB->set() result=" + zkresult2string(zkr)); return 1; } - root[0] = smtSetResult.newRoot[0]; - root[1] = smtSetResult.newRoot[1]; - root[2] = smtSetResult.newRoot[2]; - root[3] = smtSetResult.newRoot[3]; - zklog.info("Smt64Test() smt.set() i=" + to_string(i) + " returned root=" + fea2string(fr, root)); + zklog.info("Smt64Test() pHashDB->set() i=" + to_string(i) + " returned root=" + fea2string(fr, root)); } } - //sleep(2); + TimerStopAndLog(SMT64_TEST); return numberOfFailedTests; } \ No newline at end of file