diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..23cc499 --- /dev/null +++ b/.clang-format @@ -0,0 +1,62 @@ +# Generated from CLion C/C++ Code Style settings +--- +Language: Cpp +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignOperands: true +AlignTrailingComments: false +AlwaysBreakTemplateDeclarations: Yes +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBraces: Allman +BreakConstructorInitializers: AfterColon +BreakConstructorInitializersBeforeComma: false +ColumnLimit: 0 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ContinuationIndentWidth: 0 +IncludeCategories: + - Regex: '^<.*' + Priority: 1 + - Regex: '^".*' + Priority: 2 + - Regex: '.*' + Priority: 3 +IncludeIsMainRegex: '([-_](test|unittest))?$' +IndentCaseLabels: true +IndentWidth: 4 +InsertNewlineAtEOF: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: All +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: false +SpaceBeforeRangeBasedForLoopColon: false +SpaceInEmptyParentheses: false +SpacesInAngles: false +SpacesInConditionalStatement: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +TabWidth: 4 +PointerAlignment: Right +AlignAfterOpenBracket: DontAlign +AllowShortLambdasOnASingleLine: None +BinPackArguments: false +... diff --git a/CMakeLists.txt b/CMakeLists.txt index 12ab065..8c5ca09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,51 +125,38 @@ if (NOT COMPILER_SUPPORTS_AVX2) message(WARNING "AVX2 not supported by the compiler") endif () -# --- Project Sources --- -SET(FILES - ${CMAKE_SOURCE_DIR}/connection/connection.cpp - ${CMAKE_SOURCE_DIR}/connection/connectionPool.cpp - ${CMAKE_SOURCE_DIR}/connection/NodeIntroducer.cpp - ${CMAKE_SOURCE_DIR}/database/db.cpp - ${CMAKE_SOURCE_DIR}/database/garbageCleaner.cpp - ${CMAKE_SOURCE_DIR}/Logger.cpp - ${CMAKE_SOURCE_DIR}/DataProcessors.cpp - ${CMAKE_SOURCE_DIR}/IOProcessor.cpp - ${CMAKE_SOURCE_DIR}/LoggingEventProcessor.cpp - ${CMAKE_SOURCE_DIR}/QubicServer.cpp - ${CMAKE_SOURCE_DIR}/QubicIndexer.cpp - ${CMAKE_SOURCE_DIR}/Config.cpp - ${CMAKE_SOURCE_DIR}/bob.cpp - ${CMAKE_SOURCE_DIR}/GlobalVar.cpp - ${CMAKE_SOURCE_DIR}/LogEvent.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/bobAPI.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/ApiHelpers.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/querySmartContract.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/RESTServer.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/LogSubscriptionManager.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/LogWebSocket.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/QubicRpcMapper.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/QubicRpcWebSocket.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/QubicRpcMethods.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/QubicSubscriptionManager.cpp - ${CMAKE_SOURCE_DIR}/RESTAPI/QubicRpcHandler.cpp -) +# --- Collect sources from src/ and exclude executables/tools --- +file(GLOB_RECURSE ALL_SRC CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/src/*.c ${CMAKE_SOURCE_DIR}/src/*.cpp) + +set(LIB_SOURCES) +foreach(_f IN LISTS ALL_SRC) + # exclude main executables and optional single-file tools + if(_f MATCHES "/main\\.cpp$" OR _f MATCHES "/pop10\\.cpp$" OR _f MATCHES "/migrator\\.cpp$") + continue() + endif() + list(APPEND LIB_SOURCES ${_f}) +endforeach() + +# Fallback: if explicit sources are required, ensure at least some files exist +if(NOT LIB_SOURCES) + message(FATAL_ERROR "No library sources found under src/.") +endif() -add_library(XKCP_avx2 STATIC XKCP/KangarooTwelve.c - XKCP/KeccakP-1600-AVX2.s - XKCP/KeccakP-1600-times4-AVX2.c - XKCP/TurboSHAKE.c +add_library(XKCP_avx2 STATIC external/XKCP/KangarooTwelve.c + external/XKCP/KeccakP-1600-AVX2.s + external/XKCP/KeccakP-1600-times4-AVX2.c + external/XKCP/TurboSHAKE.c ) # Create a library from the source files to be shared between the main executable and tests -add_library(bob_lib STATIC ${FILES}) +add_library(bob_lib STATIC ${LIB_SOURCES}) target_link_libraries(bob_lib PRIVATE redis++_static hiredis spdlog::spdlog cxxopts::cxxopts ${ZSTD_TARGET} XKCP_avx2 drogon ${JSONCPP_TARGET}) # --- Executable and Linking --- -ADD_EXECUTABLE(bob main.cpp) -ADD_EXECUTABLE(pop10 pop10.cpp) +ADD_EXECUTABLE(bob src/main.cpp) +ADD_EXECUTABLE(pop10 src/pop10.cpp) target_link_libraries(bob PRIVATE bob_lib) target_link_libraries(pop10 PRIVATE bob_lib) @@ -196,6 +183,6 @@ add_custom_command( add_custom_command( TARGET bob POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${CMAKE_SOURCE_DIR}/RESTAPI/openapi.json + ${CMAKE_SOURCE_DIR}/src/rest_api/openapi.json ${CMAKE_BINARY_DIR}/openapi.json ) diff --git a/README.md b/README.md index a19120d..7bd7466 100644 --- a/README.md +++ b/README.md @@ -91,9 +91,9 @@ sudo apt install -y vim net-tools tmux cmake git libjsoncpp-dev \ #### 2. Install KeyDB -KeyDB is a Redis-compatible database required by Bob. Follow the [KeyDB installation guide](KEYDB_INSTALL.md). +KeyDB is a Redis-compatible database required by Bob. Follow the [KeyDB installation guide](doc/KEYDB_INSTALL.md). -Optionally, install KVRocks for additional disk-based persistence: [KVRocks installation guide](KVROCKS_INSTALL.MD). +Optionally, install KVRocks for additional disk-based persistence: [KVRocks installation guide](doc/KVROCKS_INSTALL.MD). #### 3. Build Bob @@ -181,13 +181,13 @@ curl -s -X POST http://localhost:40420/qubic \ ## Useful Resources ### Using Bob -- [Ethereum-like JSON-RPC (HTTP/WS)](QUBIC_JSON_RPC.md) -- [REST API Endpoints](REST_API.md) -- [What is a Logging Event in Qubic?](LOGGING_IN_QUBIC.MD) -- [Mastering the findlog Method](FINDLOG.MD) -- [Dealing with Transactions and Logging](DEAL_WITH_TX.MD) -- [Improve Stability via Kernel Buffer Size](KERN_BUF_SIZE.MD) +- [Ethereum-like JSON-RPC (HTTP/WS)](doc/QUBIC_JSON_RPC.md) +- [REST API Endpoints](doc/REST_API.md) +- [What is a Logging Event in Qubic?](doc/LOGGING_IN_QUBIC.MD) +- [Mastering the findlog Method](doc/FINDLOG.MD) +- [Dealing with Transactions and Logging](doc/DEAL_WITH_TX.MD) +- [Improve Stability via Kernel Buffer Size](doc/KERN_BUF_SIZE.MD) ### Inside Bob -- [Anatomy of Bob](ANATOMY_OF_BOB.MD) -- [How the Indexer Indexes Qubic Data](INDEXER_INDEXING_DATA.MD) +- [Anatomy of Bob](doc/ANATOMY_OF_BOB.MD) +- [How the Indexer Indexes Qubic Data](doc/INDEXER_INDEXING_DATA.MD) diff --git a/commonFunctions.h b/commonFunctions.h deleted file mode 100644 index 1ae9bf7..0000000 --- a/commonFunctions.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include "stdint.h" - -static bool isArrayZero(uint8_t *ptr, int len) { - for (int i = 0; i < len; i++) { - if (ptr[i] != 0) { - return false; - } - } - return true; -} \ No newline at end of file diff --git a/exampleConfigFile/keydb.conf b/conf/keydb.conf similarity index 100% rename from exampleConfigFile/keydb.conf rename to conf/keydb.conf diff --git a/exampleConfigFile/kvrocks.conf b/conf/kvrocks.conf similarity index 100% rename from exampleConfigFile/kvrocks.conf rename to conf/kvrocks.conf diff --git a/exampleConfigFile/kvrocks.service b/conf/kvrocks.service similarity index 100% rename from exampleConfigFile/kvrocks.service rename to conf/kvrocks.service diff --git a/ANATOMY_OF_BOB.MD b/doc/ANATOMY_OF_BOB.MD similarity index 100% rename from ANATOMY_OF_BOB.MD rename to doc/ANATOMY_OF_BOB.MD diff --git a/CONFIG_FILE.MD b/doc/CONFIG_FILE.MD similarity index 100% rename from CONFIG_FILE.MD rename to doc/CONFIG_FILE.MD diff --git a/DEAL_WITH_TX.MD b/doc/DEAL_WITH_TX.MD similarity index 100% rename from DEAL_WITH_TX.MD rename to doc/DEAL_WITH_TX.MD diff --git a/EPOCH_TRANSITION.MD b/doc/EPOCH_TRANSITION.MD similarity index 100% rename from EPOCH_TRANSITION.MD rename to doc/EPOCH_TRANSITION.MD diff --git a/FINDLOG.MD b/doc/FINDLOG.MD similarity index 100% rename from FINDLOG.MD rename to doc/FINDLOG.MD diff --git a/FIND_LOG_ASSET_ADVANCED.MD b/doc/FIND_LOG_ASSET_ADVANCED.MD similarity index 100% rename from FIND_LOG_ASSET_ADVANCED.MD rename to doc/FIND_LOG_ASSET_ADVANCED.MD diff --git a/FIND_LOG_ASSET_EASY.MD b/doc/FIND_LOG_ASSET_EASY.MD similarity index 100% rename from FIND_LOG_ASSET_EASY.MD rename to doc/FIND_LOG_ASSET_EASY.MD diff --git a/INDEXER_INDEXING_DATA.MD b/doc/INDEXER_INDEXING_DATA.MD similarity index 100% rename from INDEXER_INDEXING_DATA.MD rename to doc/INDEXER_INDEXING_DATA.MD diff --git a/KERN_BUF_SIZE.MD b/doc/KERN_BUF_SIZE.MD similarity index 100% rename from KERN_BUF_SIZE.MD rename to doc/KERN_BUF_SIZE.MD diff --git a/KEYDB_INSTALL.md b/doc/KEYDB_INSTALL.md similarity index 100% rename from KEYDB_INSTALL.md rename to doc/KEYDB_INSTALL.md diff --git a/KVROCKS_INSTALL.MD b/doc/KVROCKS_INSTALL.MD similarity index 100% rename from KVROCKS_INSTALL.MD rename to doc/KVROCKS_INSTALL.MD diff --git a/LOGGING_IN_QUBIC.MD b/doc/LOGGING_IN_QUBIC.MD similarity index 100% rename from LOGGING_IN_QUBIC.MD rename to doc/LOGGING_IN_QUBIC.MD diff --git a/LOG_EVENT_STREAM.MD b/doc/LOG_EVENT_STREAM.MD similarity index 100% rename from LOG_EVENT_STREAM.MD rename to doc/LOG_EVENT_STREAM.MD diff --git a/QUBIC_JSON_RPC.md b/doc/QUBIC_JSON_RPC.md similarity index 99% rename from QUBIC_JSON_RPC.md rename to doc/QUBIC_JSON_RPC.md index 188d8ac..d6d26b7 100644 --- a/QUBIC_JSON_RPC.md +++ b/doc/QUBIC_JSON_RPC.md @@ -6,7 +6,7 @@ This guide is for exchange developers and integrators who want to connect to Qub > [!NOTE] > This documentation covers the Qubic JSON-RPC 2.0 API including WebSocket subscriptions. -To have a playground, you can open [rpc_playground.html](rpc_playground.html) in your browser. Or use the published version [here on github gist](https://gistpreview.github.io/?6912d613bc27ae75126859447fca8acf). +To have a playground, you can open [rpc_playground.html](../web/rpc_playground.html) in your browser. Or use the published version [here on github gist](https://gistpreview.github.io/?6912d613bc27ae75126859447fca8acf). ## Table of Contents diff --git a/REST_API.md b/doc/REST_API.md similarity index 100% rename from REST_API.md rename to doc/REST_API.md diff --git a/XKCP/KangarooTwelve.c b/external/XKCP/KangarooTwelve.c similarity index 100% rename from XKCP/KangarooTwelve.c rename to external/XKCP/KangarooTwelve.c diff --git a/XKCP/KangarooTwelve.h b/external/XKCP/KangarooTwelve.h similarity index 100% rename from XKCP/KangarooTwelve.h rename to external/XKCP/KangarooTwelve.h diff --git a/XKCP/KeccakP-1600-AVX2.h b/external/XKCP/KeccakP-1600-AVX2.h similarity index 100% rename from XKCP/KeccakP-1600-AVX2.h rename to external/XKCP/KeccakP-1600-AVX2.h diff --git a/XKCP/KeccakP-1600-AVX2.s b/external/XKCP/KeccakP-1600-AVX2.s similarity index 100% rename from XKCP/KeccakP-1600-AVX2.s rename to external/XKCP/KeccakP-1600-AVX2.s diff --git a/XKCP/KeccakP-1600-SnP.h b/external/XKCP/KeccakP-1600-SnP.h similarity index 100% rename from XKCP/KeccakP-1600-SnP.h rename to external/XKCP/KeccakP-1600-SnP.h diff --git a/XKCP/KeccakP-1600-times4-AVX2.c b/external/XKCP/KeccakP-1600-times4-AVX2.c similarity index 100% rename from XKCP/KeccakP-1600-times4-AVX2.c rename to external/XKCP/KeccakP-1600-times4-AVX2.c diff --git a/XKCP/KeccakP-1600-times4-AVX2.h b/external/XKCP/KeccakP-1600-times4-AVX2.h similarity index 100% rename from XKCP/KeccakP-1600-times4-AVX2.h rename to external/XKCP/KeccakP-1600-times4-AVX2.h diff --git a/XKCP/KeccakP-1600-times4-SnP.h b/external/XKCP/KeccakP-1600-times4-SnP.h similarity index 100% rename from XKCP/KeccakP-1600-times4-SnP.h rename to external/XKCP/KeccakP-1600-times4-SnP.h diff --git a/XKCP/KeccakP-1600-unrolling.macros b/external/XKCP/KeccakP-1600-unrolling.macros similarity index 100% rename from XKCP/KeccakP-1600-unrolling.macros rename to external/XKCP/KeccakP-1600-unrolling.macros diff --git a/XKCP/KeccakSponge.h b/external/XKCP/KeccakSponge.h similarity index 100% rename from XKCP/KeccakSponge.h rename to external/XKCP/KeccakSponge.h diff --git a/XKCP/KeccakSponge.inc b/external/XKCP/KeccakSponge.inc similarity index 100% rename from XKCP/KeccakSponge.inc rename to external/XKCP/KeccakSponge.inc diff --git a/XKCP/Phases.h b/external/XKCP/Phases.h similarity index 100% rename from XKCP/Phases.h rename to external/XKCP/Phases.h diff --git a/XKCP/PlSnP-common.h b/external/XKCP/PlSnP-common.h similarity index 100% rename from XKCP/PlSnP-common.h rename to external/XKCP/PlSnP-common.h diff --git a/XKCP/SnP-common.h b/external/XKCP/SnP-common.h similarity index 100% rename from XKCP/SnP-common.h rename to external/XKCP/SnP-common.h diff --git a/XKCP/TurboSHAKE.c b/external/XKCP/TurboSHAKE.c similarity index 100% rename from XKCP/TurboSHAKE.c rename to external/XKCP/TurboSHAKE.c diff --git a/XKCP/TurboSHAKE.h b/external/XKCP/TurboSHAKE.h similarity index 100% rename from XKCP/TurboSHAKE.h rename to external/XKCP/TurboSHAKE.h diff --git a/XKCP/align.h b/external/XKCP/align.h similarity index 100% rename from XKCP/align.h rename to external/XKCP/align.h diff --git a/XKCP/brg_endian.h b/external/XKCP/brg_endian.h similarity index 100% rename from XKCP/brg_endian.h rename to external/XKCP/brg_endian.h diff --git a/XKCP/xkcp_config.h b/external/XKCP/xkcp_config.h similarity index 100% rename from XKCP/xkcp_config.h rename to external/XKCP/xkcp_config.h diff --git a/usefulScript/delete_log_id.lua b/scripts/delete_log_id.lua similarity index 100% rename from usefulScript/delete_log_id.lua rename to scripts/delete_log_id.lua diff --git a/usefulScript/delete_log_ranges.lua b/scripts/delete_log_ranges.lua similarity index 100% rename from usefulScript/delete_log_ranges.lua rename to scripts/delete_log_ranges.lua diff --git a/usefulScript/delete_tickdata_tickvote.lua b/scripts/delete_tickdata_tickvote.lua similarity index 100% rename from usefulScript/delete_tickdata_tickvote.lua rename to scripts/delete_tickdata_tickvote.lua diff --git a/usefulScript/set_tracker.lua b/scripts/set_tracker.lua similarity index 100% rename from usefulScript/set_tracker.lua rename to scripts/set_tracker.lua diff --git a/test_rpc.sh b/scripts/test_rpc.sh similarity index 100% rename from test_rpc.sh rename to scripts/test_rpc.sh diff --git a/usefulScript/useful_script.sh b/scripts/useful_script.sh similarity index 100% rename from usefulScript/useful_script.sh rename to scripts/useful_script.sh diff --git a/bob.cpp b/src/bob.cpp similarity index 94% rename from bob.cpp rename to src/bob.cpp index 2efb552..685992b 100644 --- a/bob.cpp +++ b/src/bob.cpp @@ -1,37 +1,36 @@ -#include "Config.h" +#include "bob.h" +#include "src/core/k12_and_key_util.h" +#include "src/profiler/profiler.h" +#include "config.h" #include "connection/connection.h" -#include "structs.h" -#include "Logger.h" -#include "GlobalVar.h" +#include "core/structs.h" #include "database/db.h" -#include "Profiler.h" +#include "global_var.h" +#include "logger/logger.h" +#include "shim.h" +#include "version.h" +#include // std::max #include -#include // memcpy #include // strtoull +#include // memcpy #include // std::numeric_limits -#include // std::max -#include // std::random_device, std::mt19937 -#include "K12AndKeyUtil.h" #include // thread naming on POSIX -#include "shim.h" -#include "bob.h" -#include "Version.h" -void IOVerifyThread(); -void IORequestThread(ConnectionPool& conn_pool, std::chrono::milliseconds requestCycle, uint32_t futureOffset); -void EventRequestFromTrustedNode(ConnectionPool& connPoolWithPwd, std::chrono::milliseconds request_logging_cycle_ms); +#include // std::random_device, std::mt19937 + +void tickingVerifyThread(); +void tickingDataRequestThread(ConnectionPool& conn_pool, std::chrono::milliseconds requestCycle, uint32_t futureOffset); +void eventRequestFromTrustedNode(ConnectionPool& connPoolWithPwd, std::chrono::milliseconds request_logging_cycle_ms); void connReceiver(QCPtr conn, const bool isTrustedNode); -void DataProcessorThread(); -void RequestProcessorThread(); +void dataProcessorThread(); +void requestProcessorThread(); void verifyLoggingEvent(); void indexVerifiedTicks(); void querySmartContractThread(ConnectionPool& connPoolAll); // Public helpers from QubicServer.cpp -bool StartQubicServer(ConnectionPool* cp, uint16_t port = 21842); -void StopQubicServer(); +bool startQubicServer(ConnectionPool* cp, uint16_t port = 21842); +void stopQubicServer(); void garbageCleaner(); - - static inline void set_this_thread_name(const char* name_in) { // Linux allows up to 16 bytes including null terminator char buf[16]; @@ -108,7 +107,6 @@ int runBob(int argc, char *argv[]) // Put redis_url in REDIS_CONNECTION_STRING std::string KEYDB_CONNECTION_STRING = cfg.keydb_url; - // Read server flags const bool run_server = cfg.run_server; unsigned int server_port_u = cfg.server_port; @@ -150,14 +148,13 @@ int runBob(int argc, char *argv[]) return -1; } const uint16_t server_port = static_cast(server_port_u); - if (!StartQubicServer(&connPool, server_port)) { + if (!startQubicServer(&connPool, server_port)) { Logger::get()->critical("Failed to start embedded server on port {}", server_port); return -1; } Logger::get()->info("Embedded server enabled on port {}", server_port); } - uint32_t initTick = 0; uint16_t initEpoch = 0; uint32_t endEpochTick = 0; @@ -206,11 +203,10 @@ int runBob(int argc, char *argv[]) } } - auto request_thread = std::thread( [&](){ set_this_thread_name("io-req"); - IORequestThread( + tickingDataRequestThread( std::ref(connPool), std::chrono::milliseconds(request_cycle_ms), static_cast(future_offset) @@ -219,11 +215,11 @@ int runBob(int argc, char *argv[]) ); auto verify_thread = std::thread([&](){ set_this_thread_name("verify"); - IOVerifyThread(); + tickingVerifyThread(); }); auto log_request_trusted_nodes_thread = std::thread([&](){ set_this_thread_name("trusted-log-req"); - EventRequestFromTrustedNode(std::ref(connPool), + eventRequestFromTrustedNode(std::ref(connPool), std::chrono::milliseconds(request_logging_cycle_ms)); }); auto indexer_thread = std::thread([&](){ @@ -263,13 +259,13 @@ int runBob(int argc, char *argv[]) { v_data_thread.emplace_back([&](){ set_this_thread_name("data"); - DataProcessorThread(); + dataProcessorThread(); }); v_data_thread.emplace_back([&, i](){ char nm[16]; std::snprintf(nm, sizeof(nm), "reqp-%d", i); set_this_thread_name(nm); - RequestProcessorThread(); + requestProcessorThread(); }); } std::thread log_event_verifier_thread; @@ -403,7 +399,7 @@ int runBob(int argc, char *argv[]) if (run_server) { - StopQubicServer(); + stopQubicServer(); Logger::get()->info("Closed Qubic server at port 21842"); } diff --git a/bob.h b/src/bob.h similarity index 100% rename from bob.h rename to src/bob.h diff --git a/Config.cpp b/src/config.cpp similarity index 99% rename from Config.cpp rename to src/config.cpp index 217b6ff..9ea630a 100644 --- a/Config.cpp +++ b/src/config.cpp @@ -1,10 +1,10 @@ -#include "Config.h" +#include "config.h" #include "json/reader.h" -#include #include +#include +#include #include #include -#include #include bool LoadConfig(const std::string& path, AppConfig& out, std::string& error) { diff --git a/Config.h b/src/config.h similarity index 98% rename from Config.h rename to src/config.h index d6d4e20..d2b2dbd 100644 --- a/Config.h +++ b/src/config.h @@ -1,9 +1,9 @@ #pragma once +#include "core/m256i.h" +#include #include #include -#include -#include "m256i.h" // Add tick storage mode enum enum class TickStorageMode { diff --git a/connection/connection.cpp b/src/connection/connection.cpp similarity index 99% rename from connection/connection.cpp rename to src/connection/connection.cpp index 480ce5b..34a6732 100644 --- a/connection/connection.cpp +++ b/src/connection/connection.cpp @@ -1,22 +1,23 @@ -#include "structs.h" #include #include #include // For std::min #include -#include -#include +#include "connection.h" #include -#include +#include // for errno #include -#include // for errno #include +#include #include -#include "database/db.h" -#include "connection.h" -#include "Logger.h" -#include "GlobalVar.h" -#include "shim.h" +#include +#include + +#include "src/database/db.h" +#include "src/global_var.h" +#include "src/shim.h" + +#include static int do_connect(const char* nodeIp, int nodePort) { int serverSocket = socket(AF_INET, SOCK_STREAM, 0); diff --git a/connection/connection.h b/src/connection/connection.h similarity index 98% rename from connection/connection.h rename to src/connection/connection.h index 41187da..5948268 100644 --- a/connection/connection.h +++ b/src/connection/connection.h @@ -1,12 +1,12 @@ #pragma once +#include "src/core/structs.h" +#include "src/special_buffer_structs.h" +#include #include -#include -#include #include #include -#include -#include "structs.h" -#include "SpecialBufferStructs.h" +#include +#include // Not thread safe class QubicConnection diff --git a/connection/connectionPool.cpp b/src/connection/connection_pool.cpp similarity index 100% rename from connection/connectionPool.cpp rename to src/connection/connection_pool.cpp diff --git a/connection/NodeIntroducer.cpp b/src/connection/node_introducer.cpp similarity index 99% rename from connection/NodeIntroducer.cpp rename to src/connection/node_introducer.cpp index 7d0fa25..998e1ba 100644 --- a/connection/NodeIntroducer.cpp +++ b/src/connection/node_introducer.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "Logger.h" +#include "src/logger/logger.h" std::vector GetPeerFromDNS() { diff --git a/RequestMap.h b/src/connection/request_map.h similarity index 98% rename from RequestMap.h rename to src/connection/request_map.h index 0633566..4f2138a 100644 --- a/RequestMap.h +++ b/src/connection/request_map.h @@ -1,4 +1,4 @@ -#include "connection/connection.h" +#include "connection.h" // mapping from dejavu to requested data // usage: some response doesn't contain requested info // if code makes several queries, we need this map to know which diff --git a/Asset.h b/src/core/asset.h similarity index 99% rename from Asset.h rename to src/core/asset.h index f0d1359..a0a37eb 100644 --- a/Asset.h +++ b/src/core/asset.h @@ -1,6 +1,6 @@ #pragma once +#include "src/shim.h" #include "common_def.h" -#include "shim.h" #define EMPTY 0 #define ISSUANCE 1 #define OWNERSHIP 2 diff --git a/common_def.h b/src/core/common_def.h similarity index 97% rename from common_def.h rename to src/core/common_def.h index 38bda77..cd53433 100644 --- a/common_def.h +++ b/src/core/common_def.h @@ -1,6 +1,8 @@ #pragma once -#include +#include +#include #include +#include #define SPECTRUM_DEPTH 24 // Defines SPECTRUM_CAPACITY (1 << SPECTRUM_DEPTH) #define SPECTRUM_CAPACITY (1ULL << SPECTRUM_DEPTH) // Must be 2^N diff --git a/defines.h b/src/core/defines.h similarity index 96% rename from defines.h rename to src/core/defines.h index b0f8ee5..d0a5c9c 100644 --- a/defines.h +++ b/src/core/defines.h @@ -1,4 +1,5 @@ -#include "commonFunctions.h" +#include "src/utils/utils.h" + #define NUMBER_OF_TRANSACTIONS_PER_TICK 1024 #define NUMBER_OF_SPECIAL_EVENT_PER_TICK 5 #define SIGNATURE_SIZE 64 diff --git a/Entity.h b/src/core/entity.h similarity index 99% rename from Entity.h rename to src/core/entity.h index 5e18e6f..ec6da74 100644 --- a/Entity.h +++ b/src/core/entity.h @@ -1,6 +1,6 @@ #pragma once +#include "src/shim.h" #include "common_def.h" -#include "shim.h" static char spectrumLock; struct EntityRecord { diff --git a/K12AndKeyUtil.h b/src/core/k12_and_key_util.h similarity index 99% rename from K12AndKeyUtil.h rename to src/core/k12_and_key_util.h index 4ace9e8..5b73d7a 100644 --- a/K12AndKeyUtil.h +++ b/src/core/k12_and_key_util.h @@ -589,6 +589,12 @@ static void KangarooTwelve(const uint8_t *input, unsigned int inputByteLen, uint KeccakP1600_Permute_12rounds(finalNode.state); memcpy(output, finalNode.state, outputByteLen); } + +static void KangarooTwelve64To32(void* input, void* output) +{ + KangarooTwelve((uint8_t*)input, 64, (uint8_t*)output, 32); +} + #define CURVE_ORDER_0 0x2FB2540EC7768CE7 #define CURVE_ORDER_1 0xDFBD004DFE0F7999 #define CURVE_ORDER_2 0xF05397829CBC14E5 @@ -2359,6 +2365,18 @@ BOOL_FUNC_DECL verify(const unsigned char* publicKey, const unsigned char* messa return (memcmp(A, signature, 32) == 0); } +static bool verifySignature(void* ptr, uint8_t* pubkey, int structSize) // structSize include sig 64 bytes +{ + uint8_t* p = (uint8_t*)ptr; + uint8_t digest[32]; + KangarooTwelve(p, structSize - 64, digest, 32); + if (verify(pubkey, digest, p + structSize - 64)) + { + return true; + } + return false; +} + static bool getSubseedFromSeed(const uint8_t* seed, uint8_t* subseed) { uint8_t seedBytes[55]; diff --git a/LogEvent.cpp b/src/core/log_event/log_event.cpp similarity index 99% rename from LogEvent.cpp rename to src/core/log_event/log_event.cpp index f071f02..e93adf5 100644 --- a/LogEvent.cpp +++ b/src/core/log_event/log_event.cpp @@ -1,4 +1,5 @@ -#include "LogEvent.h" +#include "log_event.h" +#include Json::Value LogEvent::parseToJson() const { diff --git a/LogEvent.h b/src/core/log_event/log_event.h similarity index 99% rename from LogEvent.h rename to src/core/log_event/log_event.h index ab74634..6dde053 100644 --- a/LogEvent.h +++ b/src/core/log_event/log_event.h @@ -1,8 +1,8 @@ #pragma once -#include "structs.h" -#include "Logger.h" +#include "src/global_var.h" +#include "src/logger/logger.h" #include "stdint.h" -#include "GlobalVar.h" + #include // ---- Data Structures to be Stored ---- diff --git a/m256i.h b/src/core/m256i.h similarity index 99% rename from m256i.h rename to src/core/m256i.h index d9c4b3e..6b4100f 100644 --- a/m256i.h +++ b/src/core/m256i.h @@ -1,8 +1,8 @@ #pragma once -#include +#include "k12_and_key_util.h" #include #include -#include "K12AndKeyUtil.h" +#include // Used for all kinds of IDs, including in QPI and contracts. // Existing interface and behavior should never be changed! (However, it may be extended.) union m256i diff --git a/structs.h b/src/core/structs.h similarity index 99% rename from structs.h rename to src/core/structs.h index 8d4e6db..83d802b 100644 --- a/structs.h +++ b/src/core/structs.h @@ -1,9 +1,9 @@ #pragma once +#include "src/utils/utils.h" #include "defines.h" -#include "utils.h" +#include "m256i.h" #include #include -#include "m256i.h" struct RequestResponseHeader { private: uint8_t _size[3]; diff --git a/database/db.cpp b/src/database/db.cpp similarity index 99% rename from database/db.cpp rename to src/database/db.cpp index 54008dc..eeff122 100644 --- a/database/db.cpp +++ b/src/database/db.cpp @@ -6,10 +6,10 @@ #include #include #include "zstd.h" // zstd compression/decompression -#include "Logger.h" -#include "K12AndKeyUtil.h" +#include "src/logger/logger.h" +#include "src/core/k12_and_key_util.h" #include // std::exit -#include "shim.h" +#include "src/shim.h" // Global Redis client handle static std::unique_ptr g_redis = nullptr; static std::unique_ptr g_kvrocks = nullptr; diff --git a/database/db.h b/src/database/db.h similarity index 99% rename from database/db.h rename to src/database/db.h index 9044f57..274a2de 100644 --- a/database/db.h +++ b/src/database/db.h @@ -36,15 +36,14 @@ */ #pragma once - -#include +#include "src/core/log_event/log_event.h" +#include "src/core/structs.h" +#include "src/logger/logger.h" #include +#include // For m256i #include +#include #include -#include // For m256i -#include "structs.h" -#include "Logger.h" -#include "LogEvent.h" // Forward declaration for the Redis client namespace sw { namespace redis { class Redis; }} diff --git a/database/garbageCleaner.cpp b/src/database/garbage_cleaner.cpp similarity index 99% rename from database/garbageCleaner.cpp rename to src/database/garbage_cleaner.cpp index 891b2c8..7966b02 100644 --- a/database/garbageCleaner.cpp +++ b/src/database/garbage_cleaner.cpp @@ -1,5 +1,9 @@ -#include "database/db.h" -#include "shim.h" +#include "db.h" +#include "src/core/structs.h" +#include "src/shim.h" + +#include + static const std::string KEY_LAST_CLEAN_TICK_DATA = "garbage_cleaner:last_clean_tick_data"; static const std::string KEY_LAST_CLEAN_TX_TICK = "garbage_cleaner:last_clean_tx_tick"; diff --git a/src/fs/file_io.cpp b/src/fs/file_io.cpp new file mode 100644 index 0000000..62d502b --- /dev/null +++ b/src/fs/file_io.cpp @@ -0,0 +1,70 @@ +#include "file_io.h" + +void saveFiles(const std::string tickSpectrum, const std::string tickUniverse) { + FILE *f = fopen(tickSpectrum.c_str(), "wb"); + if (!f) { + Logger::get()->error("Failed to open spectrum file for writing: {}", + tickSpectrum); + } else { + if (fwrite(spectrum, sizeof(EntityRecord), SPECTRUM_CAPACITY, f) != + SPECTRUM_CAPACITY) { + Logger::get()->error("Failed to write spectrum file: {}", tickSpectrum); + } + fclose(f); + } + + f = fopen(tickUniverse.c_str(), "wb"); + if (!f) { + Logger::get()->error("Failed to open universe file for writing: {}", + tickUniverse); + } else { + if (fwrite(assets, sizeof(AssetRecord), ASSETS_CAPACITY, f) != + ASSETS_CAPACITY) { + Logger::get()->error("Failed to write universe file: {}", tickUniverse); + } + fclose(f); + } +} + +void saveState(uint32_t &tracker, uint32_t lastVerified) { + Logger::get()->info("Saving verified universe/spectrum {} - Do not shutdown", + lastVerified); + std::string tickSpectrum = "spectrum." + std::to_string(lastVerified); + std::string tickUniverse = "universe." + std::to_string(lastVerified); + saveFiles(tickSpectrum, tickUniverse); + db_update_latest_verified_tick(lastVerified); + tickSpectrum = "spectrum." + std::to_string(tracker); + tickUniverse = "universe." + std::to_string(tracker); + if (std::filesystem::exists(tickSpectrum) && + std::filesystem::exists(tickUniverse)) { + std::filesystem::remove(tickSpectrum); + std::filesystem::remove(tickUniverse); + } + Logger::get()->info( + "Saved checkpoints. Deleted old verified universe/spectrum {}. ", + lastVerified); + tracker = lastVerified; + db_insert_u32("verified_history:" + std::to_string(gCurrentProcessingEpoch), + lastVerified); +} + +// Small helper to load a fixed-size array from a binary file with uniform +// logging. +bool loadFile(const std::string &path, void *outBuffer, + size_t elementSize, size_t elementCount, + const char *label) { + Logger::get()->info("Loading file {}", path); + FILE *f = fopen(path.c_str(), "rb"); + if (!f) { + Logger::get()->error("Failed to open {} file: {}", label, path); + return false; + } + size_t readCount = fread(outBuffer, elementSize, elementCount, f); + fclose(f); + if (readCount != elementCount) { + Logger::get()->error("Failed to read {} file. Expected {} records, got {}", + label, elementCount, readCount); + return false; + } + return true; +} diff --git a/src/fs/file_io.h b/src/fs/file_io.h new file mode 100644 index 0000000..bc37952 --- /dev/null +++ b/src/fs/file_io.h @@ -0,0 +1,20 @@ +#pragma once + +#define SAVE_PERIOD 1000 + +#include "src/database/db.h" +#include "src/core/asset.h" +#include "src/core/entity.h" + +#include +#include + +void saveFiles(const std::string tickSpectrum, const std::string tickUniverse); + +void saveState(uint32_t &tracker, uint32_t lastVerified); + +// Small helper to load a fixed-size array from a binary file with uniform +// logging. +bool loadFile(const std::string &path, void *outBuffer, + size_t elementSize, size_t elementCount, + const char *label); \ No newline at end of file diff --git a/GlobalVar.cpp b/src/global_var.cpp similarity index 93% rename from GlobalVar.cpp rename to src/global_var.cpp index 8c32bd9..a4572d8 100644 --- a/GlobalVar.cpp +++ b/src/global_var.cpp @@ -1,6 +1,6 @@ -#include "GlobalVar.h" +#include "global_var.h" -GlobalState& GS() { +GlobalState & GS() { // Allocate once on the heap to avoid gigantic .bss/.data sections. static GlobalState* inst = []() -> GlobalState* { // Use malloc to avoid throwing in low-memory situations; then zero memory. diff --git a/GlobalVar.h b/src/global_var.h similarity index 96% rename from GlobalVar.h rename to src/global_var.h index 29c4d6f..e3fed9a 100644 --- a/GlobalVar.h +++ b/src/global_var.h @@ -1,13 +1,12 @@ #pragma once -#include -#include -#include "Config.h" -#include "structs.h" -#include "SpecialBufferStructs.h" -#include "RequestMap.h" -#include "common_def.h" +#include "config.h" +#include "connection/request_map.h" +#include "core/common_def.h" +#include "core/structs.h" +#include "special_buffer_structs.h" #include #include +#include #include struct GlobalState { diff --git a/Logger.cpp b/src/logger/logger.cpp similarity index 98% rename from Logger.cpp rename to src/logger/logger.cpp index 343b789..e660f61 100644 --- a/Logger.cpp +++ b/src/logger/logger.cpp @@ -1,8 +1,8 @@ -#include "Logger.h" -#include "spdlog/sinks/stdout_color_sinks.h" +#include "logger.h" #include "spdlog/sinks/rotating_file_sink.h" -#include +#include "spdlog/sinks/stdout_color_sinks.h" #include +#include // Define the static member std::shared_ptr Logger::s_logger; diff --git a/Logger.h b/src/logger/logger.h similarity index 100% rename from Logger.h rename to src/logger/logger.h diff --git a/main.cpp b/src/main.cpp similarity index 100% rename from main.cpp rename to src/main.cpp index 2cd3d1e..df9bc12 100644 --- a/main.cpp +++ b/src/main.cpp @@ -1,9 +1,9 @@ -#include -#include #include "bob.h" #include +#include #include #include +#include bool bob_client_stop_flag = false; bool is_watchdog = true; static void onSigInt(int) { diff --git a/migrator/migrator.cpp b/src/migrator/migrator.cpp similarity index 100% rename from migrator/migrator.cpp rename to src/migrator/migrator.cpp diff --git a/QubicServer.cpp b/src/p2p/p2p_server.cpp similarity index 98% rename from QubicServer.cpp rename to src/p2p/p2p_server.cpp index b5638cf..502690d 100644 --- a/QubicServer.cpp +++ b/src/p2p/p2p_server.cpp @@ -15,9 +15,9 @@ #include #include -#include "Logger.h" -#include "connection/connection.h" -#include "shim.h" +#include "src/connection/connection.h" +#include "src/logger/logger.h" +#include "src/shim.h" // Forward declaration from IOProcessor.cpp void connReceiver(QCPtr conn, const bool isTrustedNode); @@ -383,13 +383,13 @@ namespace { } // namespace // Public helpers to control the server -bool StartQubicServer(ConnectionPool* cp, uint16_t port = 21842) +bool startQubicServer(ConnectionPool* cp, uint16_t port = 21842) { QubicServer::instance().setConnectionPool(cp); return QubicServer::instance().start(port, 64, 5); // 64 global, 5 per IP } -void StopQubicServer() { +void stopQubicServer() { QubicServer::instance().stop(); Logger::get()->info("Stop qubic server"); } \ No newline at end of file diff --git a/pop10.cpp b/src/pop10.cpp similarity index 100% rename from pop10.cpp rename to src/pop10.cpp diff --git a/QubicIndexer.cpp b/src/processors/indexer/indexer.cpp similarity index 98% rename from QubicIndexer.cpp rename to src/processors/indexer/indexer.cpp index 0d522e5..b2ea3b0 100644 --- a/QubicIndexer.cpp +++ b/src/processors/indexer/indexer.cpp @@ -1,14 +1,12 @@ +#include "src/core/structs.h" +#include "src/database/db.h" +#include "src/shim.h" +#include "src/rest_api/QubicSubscriptionManager.h" + #include #include #include -#include "SpecialBufferStructs.h" -#include "structs.h" -#include "database/db.h" -#include "Logger.h" -#include "K12AndKeyUtil.h" -#include "GlobalVar.h" -#include "shim.h" -#include "RESTAPI/QubicSubscriptionManager.h" + static bool matchesTransaction(const QuTransfer &transfer, const Transaction &tx) { return transfer.sourcePublicKey == tx.sourcePublicKey && transfer.destinationPublicKey == tx.destinationPublicKey && diff --git a/DataProcessors.cpp b/src/processors/io/io_processor.cpp similarity index 76% rename from DataProcessors.cpp rename to src/processors/io/io_processor.cpp index 3ddd54a..07143b7 100644 --- a/DataProcessors.cpp +++ b/src/processors/io/io_processor.cpp @@ -1,23 +1,43 @@ -#include -#include +#include "src/database/db.h" +#include "src/global_var.h" +#include "src/logger/logger.h" +#include "src/shim.h" +#include "src/core/k12_and_key_util.h" #include -#include "database/db.h" -#include "GlobalVar.h" -#include "Logger.h" -#include "K12AndKeyUtil.h" -#include "shim.h" +#include -bool verifySignature(void* ptr, uint8_t* pubkey, int structSize) // structSize include sig 64 bytes -{ - uint8_t* p = (uint8_t*)ptr; - uint8_t digest[32]; - KangarooTwelve(p, structSize - 64, digest, 32); - if (verify(pubkey, digest, p + structSize - 64)) +class IOProcessorUtils { +public: + static bool checkAllowedTypeForNonTrusted(int type) { + if (type == RespondLog::type()) return false; + if (type == LogRangesPerTxInTick::type()) return false; return true; } - return false; -} + + static bool isRequestType(int type) + { + if (type == REQUEST_COMPUTOR_LIST) return true; // request computor list + if (type == RequestedQuorumTick::type) return true; // request vote + if (type == RequestTickData::type) return true; // request tickdata + if (type == REQUEST_CURRENT_TICK_INFO) return true; // REQUEST_CURRENT_TICK_INFO + if (type == RequestedTickTransactions::type) return true; // request tx + if (type == RequestLog::type()) return true; // request log + if (type == RequestAllLogIdRangesFromTick::type()) return true; // request log range + return false; + } + static bool isDataType(int type) + { + if (type == TickVote::type()) return true; // vote + if (type == TickData::type()) return true; // tickdata + if (type == BROADCAST_TRANSACTION) return true; // tx + if (type == RespondLog::type()) return true; // log + if (type == LogRangesPerTxInTick::type()) return true; // logrange + if (type == RespondContractFunction::type) return true; + return false; + } +}; + void processTickVote(uint8_t* ptr) { TickVote _vote; @@ -193,7 +213,7 @@ void recordSmartContractResponse(uint32_t size, uint32_t dejavu, const uint8_t* responseSCData.add(dejavu, ptr, size, nullptr); } -void DataProcessorThread() +void dataProcessorThread() { std::vector buf; buf.resize(RequestResponseHeader::max_size, 0); @@ -464,8 +484,7 @@ void replyCurrentTickInfo(QCPtr& conn, uint32_t dejavu, uint8_t* ptr) conn->enqueueSend((uint8_t *) &pl, sizeof(pl)); } - -void RequestProcessorThread() +void requestProcessorThread() { std::vector buf; buf.resize(RequestResponseHeader::max_size, 0); @@ -520,4 +539,79 @@ void RequestProcessorThread() } } gExitDataThreadCounter++; +} + +// Receiver thread: continuously receives full packets and enqueues them into the global round buffer (MRB). +void connReceiver(QCPtr conn, const bool isTrustedNode) +{ + using namespace std::chrono_literals; + + const auto errorBackoff = 1000ms; + + std::vector packet; + packet.reserve(64 * 1024); // Optional: initial capacity to minimize reallocations + while (!gStopFlag.load(std::memory_order_relaxed)) { + try { + // Blocking receive of a complete packet from the connection. + RequestResponseHeader hdr{}; + conn->receiveAFullPacket(hdr, packet); + if (packet.empty()) { + // Defensive check; shouldn't happen if receiveAFullPacket succeeds. + if (!conn->isReconnectable()) return; + Logger::get()->trace("connReceiver error on : {}. Disconnecting", conn->getNodeIp()); + conn->disconnect(); + SLEEP(errorBackoff); + conn->reconnect(); + continue; + } + if (!isTrustedNode) + { + if (!gAllowReceiveLogFromIncomingConnection) // if operator already allowed to receive, no need to block + { + if (!IOProcessorUtils::checkAllowedTypeForNonTrusted(hdr.type())) + { + continue; //drop + } + } + } + // trusted conn allowed all packets + if (IOProcessorUtils::isDataType(hdr.type())) + { + // Enqueue the packet into the global MutexRoundBuffer. + bool ok = MRB_Data.EnqueuePacket(packet.data()); + if (!ok) { + Logger::get()->warn("connReceiver: failed to enqueue packet (size={}, type={}). Dropped.", + packet.size(), + static_cast(hdr.type())); + } + } + + if (IOProcessorUtils::isRequestType(hdr.type())) + { + bool ok = MRB_Request.EnqueuePacket(packet.data()); + if (!ok) { + Logger::get()->warn("connReceiver: failed to enqueue packet (size={}, type={}). Dropped.", + packet.size(), + static_cast(hdr.type())); + } + else + { + requestMapperTo.add(hdr.getDejavu(), nullptr, 0, conn); + } + } + + } catch (const std::logic_error& ex) { + if (!conn->isReconnectable()) return; + Logger::get()->trace("connReceiver error on : {}. Disconnecting", conn->getNodeIp()); + conn->disconnect(); + SLEEP(errorBackoff); + conn->reconnect(); + } catch (...) { + if (!conn->isReconnectable()) return; + Logger::get()->trace("connReceiver unknown exception from ip {}", conn->getNodeIp()); + conn->disconnect(); + SLEEP(errorBackoff); + conn->reconnect(); + } + } } \ No newline at end of file diff --git a/LoggingEventProcessor.cpp b/src/processors/logging/logging_event.cpp similarity index 91% rename from LoggingEventProcessor.cpp rename to src/processors/logging/logging_event.cpp index bf4ce08..ae8ec08 100644 --- a/LoggingEventProcessor.cpp +++ b/src/processors/logging/logging_event.cpp @@ -1,26 +1,25 @@ + +#include "src/core/log_event/log_event.h" +#include "src/core/asset.h" +#include "src/core/common_def.h" +#include "src/core/entity.h" +#include "src/core/k12_and_key_util.h" +#include "src/database/db.h" +#include "src/profiler/profiler.h" +#include "src/rest_api/LogSubscriptionManager.h" +#include "src/rest_api/QubicSubscriptionManager.h" +#include "src/shim.h" +#include "src/fs/file_io.h" + #include #include -#include -#include -#include #include -#include "m256i.h" -#include "connection/connection.h" -#include "structs.h" -#include "GlobalVar.h" -#include "Logger.h" -#include "database/db.h" -#include "K12AndKeyUtil.h" -#include "commonFunctions.h" -#include "Entity.h" -#include "Asset.h" -#include #include -#include "Profiler.h" -#include "shim.h" #include -#include "RESTAPI/LogSubscriptionManager.h" -#include "RESTAPI/QubicSubscriptionManager.h" +#include +#include +#include +#include using namespace std::chrono_literals; extern "C" { @@ -30,12 +29,6 @@ int KT128(const unsigned char *input, size_t inputByteLen, const unsigned char *customization, size_t customByteLen); } -static void KangarooTwelve64To32(void* input, void* output) -{ -// KT128((uint8_t*)input, 64, (uint8_t*)output, 32, nullptr, 0); - KangarooTwelve((uint8_t*)input, 64, (uint8_t*)output, 32); -} - void computeSpectrumDigest(const uint32_t tickStart, const uint32_t tickEnd) { unsigned int digestIndex; @@ -288,82 +281,6 @@ void processChangeManagingContract(LogEvent& le0, LogEvent& le1) } } -// Small helper to load a fixed-size array from a binary file with uniform logging. -static bool loadFile(const std::string& path, - void* outBuffer, - size_t elementSize, - size_t elementCount, - const char* label) -{ - Logger::get()->info("Loading file {}", path); - FILE* f = fopen(path.c_str(), "rb"); - if (!f) { - Logger::get()->error("Failed to open {} file: {}", label, path); - return false; - } - size_t readCount = fread(outBuffer, elementSize, elementCount, f); - fclose(f); - if (readCount != elementCount) { - Logger::get()->error("Failed to read {} file. Expected {} records, got {}", - label, elementCount, readCount); - return false; - } - return true; -} - -#define SAVE_PERIOD 1000 - -void saveFiles(const std::string tickSpectrum, const std::string tickUniverse) -{ - FILE *f = fopen(tickSpectrum.c_str(), "wb"); - if (!f) { - Logger::get()->error("Failed to open spectrum file for writing: {}", tickSpectrum); - } else { - if (fwrite(spectrum, sizeof(EntityRecord), SPECTRUM_CAPACITY, f) != SPECTRUM_CAPACITY) { - Logger::get()->error("Failed to write spectrum file: {}", tickSpectrum); - } - fclose(f); - } - - f = fopen(tickUniverse.c_str(), "wb"); - if (!f) { - Logger::get()->error("Failed to open universe file for writing: {}", tickUniverse); - } else { - if (fwrite(assets, sizeof(AssetRecord), ASSETS_CAPACITY, f) != ASSETS_CAPACITY) { - Logger::get()->error("Failed to write universe file: {}", tickUniverse); - } - fclose(f); - } -} - -void saveState(uint32_t& tracker, uint32_t lastVerified) -{ - Logger::get()->info("Saving verified universe/spectrum {} - Do not shutdown", lastVerified); - std::string tickSpectrum = "spectrum." + std::to_string(lastVerified); - std::string tickUniverse = "universe." + std::to_string(lastVerified); - saveFiles(tickSpectrum, tickUniverse); - db_update_latest_verified_tick(lastVerified); - tickSpectrum = "spectrum." + std::to_string(tracker); - tickUniverse = "universe." + std::to_string(tracker); - if (std::filesystem::exists(tickSpectrum) && std::filesystem::exists(tickUniverse)) { - std::filesystem::remove(tickSpectrum); - std::filesystem::remove(tickUniverse); - } - Logger::get()->info("Saved checkpoints. Deleted old verified universe/spectrum {}. ", lastVerified); - tracker = lastVerified; - db_insert_u32("verified_history:" + std::to_string(gCurrentProcessingEpoch), lastVerified); -} - -// Helper to convert byte array to hex string -static std::string bytes_to_hex_string(const unsigned char* bytes, size_t size) { - std::stringstream ss; - ss << std::hex << std::setfill('0'); - for (size_t i = 0; i < size; ++i) { - ss << std::setw(2) << static_cast(bytes[i]); - } - return ss.str(); -} - static bool checkLogExistAndVerify(uint16_t epoch, long long logId) { LogEvent le{}; @@ -868,7 +785,7 @@ void verifyLoggingEvent() } // The logging fetcher thread from trusted nodes only (no signature require) -void EventRequestFromTrustedNode(ConnectionPool& connPoolWithPwd, +void eventRequestFromTrustedNode(ConnectionPool& connPoolWithPwd, std::chrono::milliseconds request_logging_cycle_ms) { auto idleBackoff = request_logging_cycle_ms; diff --git a/IOProcessor.cpp b/src/processors/ticking/ticking.cpp similarity index 66% rename from IOProcessor.cpp rename to src/processors/ticking/ticking.cpp index 1e3502c..cb5c57d 100644 --- a/IOProcessor.cpp +++ b/src/processors/ticking/ticking.cpp @@ -5,15 +5,15 @@ #include #include -#include "connection/connection.h" -#include "structs.h" -#include "GlobalVar.h" -#include "Logger.h" -#include "database/db.h" -#include "K12AndKeyUtil.h" -#include "commonFunctions.h" -#include "Profiler.h" -#include "shim.h" +#include "src/connection/connection.h" +#include "src/core/structs.h" +#include "src/database/db.h" +#include "src/global_var.h" +#include "src/logger/logger.h" +#include "src/shim.h" +#include "src/core/k12_and_key_util.h" +#include "src/profiler/profiler.h" +#include "src/utils/utils.h" using namespace std::chrono_literals; // verify if: @@ -126,7 +126,7 @@ bool verifyQuorum(uint32_t tick, TickData& td, std::vector& votes) // Requester thread: periodically evaluates what to request next and sends requests over the connection. // Placeholders (TODO) are included where the request conditions and payloads will be implemented. -void IORequestThread(ConnectionPool& conn_pool, std::chrono::milliseconds requestCycle, uint32_t futureOffset) +void tickingDataRequestThread(ConnectionPool& conn_pool, std::chrono::milliseconds requestCycle, uint32_t futureOffset) { // Optional: pacing/tuning knobs auto idleBackoff = 10ms; // Backoff when there's nothing immediate to request @@ -233,7 +233,7 @@ void IORequestThread(ConnectionPool& conn_pool, std::chrono::milliseconds reques } // this pre-verify tick votes, not fully verifying all digests -void IOVerifyThread() +void tickingVerifyThread() { const auto idleBackoff = 10ms; TickData td{}; @@ -258,109 +258,4 @@ void IOVerifyThread() if (current_tick == tmp_tick) gCurrentFetchingTick++; } } -} - -static bool checkAllowedTypeForNonTrusted(int type) -{ - if (type == RespondLog::type()) return false; - if (type == LogRangesPerTxInTick::type()) return false; - return true; -} - -static bool isRequestType(int type) -{ - if (type == REQUEST_COMPUTOR_LIST) return true; // request computor list - if (type == RequestedQuorumTick::type) return true; // request vote - if (type == RequestTickData::type) return true; // request tickdata - if (type == REQUEST_CURRENT_TICK_INFO) return true; // REQUEST_CURRENT_TICK_INFO - if (type == RequestedTickTransactions::type) return true; // request tx - if (type == RequestLog::type()) return true; // request log - if (type == RequestAllLogIdRangesFromTick::type()) return true; // request log range - return false; -} -static bool isDataType(int type) -{ - if (type == TickVote::type()) return true; // vote - if (type == TickData::type()) return true; // tickdata - if (type == BROADCAST_TRANSACTION) return true; // tx - if (type == RespondLog::type()) return true; // log - if (type == LogRangesPerTxInTick::type()) return true; // logrange - if (type == RespondContractFunction::type) return true; - return false; -} - - -// Receiver thread: continuously receives full packets and enqueues them into the global round buffer (MRB). -void connReceiver(QCPtr conn, const bool isTrustedNode) -{ - using namespace std::chrono_literals; - - const auto errorBackoff = 1000ms; - - std::vector packet; - packet.reserve(64 * 1024); // Optional: initial capacity to minimize reallocations - while (!gStopFlag.load(std::memory_order_relaxed)) { - try { - // Blocking receive of a complete packet from the connection. - RequestResponseHeader hdr{}; - conn->receiveAFullPacket(hdr, packet); - if (packet.empty()) { - // Defensive check; shouldn't happen if receiveAFullPacket succeeds. - if (!conn->isReconnectable()) return; - Logger::get()->trace("connReceiver error on : {}. Disconnecting", conn->getNodeIp()); - conn->disconnect(); - SLEEP(errorBackoff); - conn->reconnect(); - continue; - } - if (!isTrustedNode) - { - if (!gAllowReceiveLogFromIncomingConnection) // if operator already allowed to receive, no need to block - { - if (!checkAllowedTypeForNonTrusted(hdr.type())) - { - continue; //drop - } - } - } - // trusted conn allowed all packets - if (isDataType(hdr.type())) - { - // Enqueue the packet into the global MutexRoundBuffer. - bool ok = MRB_Data.EnqueuePacket(packet.data()); - if (!ok) { - Logger::get()->warn("connReceiver: failed to enqueue packet (size={}, type={}). Dropped.", - packet.size(), - static_cast(hdr.type())); - } - } - - if (isRequestType(hdr.type())) - { - bool ok = MRB_Request.EnqueuePacket(packet.data()); - if (!ok) { - Logger::get()->warn("connReceiver: failed to enqueue packet (size={}, type={}). Dropped.", - packet.size(), - static_cast(hdr.type())); - } - else - { - requestMapperTo.add(hdr.getDejavu(), nullptr, 0, conn); - } - } - - } catch (const std::logic_error& ex) { - if (!conn->isReconnectable()) return; - Logger::get()->trace("connReceiver error on : {}. Disconnecting", conn->getNodeIp()); - conn->disconnect(); - SLEEP(errorBackoff); - conn->reconnect(); - } catch (...) { - if (!conn->isReconnectable()) return; - Logger::get()->trace("connReceiver unknown exception from ip {}", conn->getNodeIp()); - conn->disconnect(); - SLEEP(errorBackoff); - conn->reconnect(); - } - } } \ No newline at end of file diff --git a/Profiler.h b/src/profiler/profiler.h similarity index 100% rename from Profiler.h rename to src/profiler/profiler.h diff --git a/RESTAPI/ApiHelpers.cpp b/src/rest_api/ApiHelpers.cpp similarity index 98% rename from RESTAPI/ApiHelpers.cpp rename to src/rest_api/ApiHelpers.cpp index 0f9ff10..2997d00 100644 --- a/RESTAPI/ApiHelpers.cpp +++ b/src/rest_api/ApiHelpers.cpp @@ -1,15 +1,15 @@ #include "ApiHelpers.h" -#include "K12AndKeyUtil.h" -#include "Entity.h" -#include "Asset.h" -#include "GlobalVar.h" -#include "shim.h" -#include "database/db.h" -#include "structs.h" -#include "bob.h" -#include -#include +#include "src/bob.h" +#include "src/core/asset.h" +#include "src/core/entity.h" +#include "src/core/k12_and_key_util.h" +#include "src/core/structs.h" +#include "src/database/db.h" +#include "src/shim.h" + #include +#include +#include namespace ApiHelpers { diff --git a/RESTAPI/ApiHelpers.h b/src/rest_api/ApiHelpers.h similarity index 99% rename from RESTAPI/ApiHelpers.h rename to src/rest_api/ApiHelpers.h index 45b8d83..954601d 100644 --- a/RESTAPI/ApiHelpers.h +++ b/src/rest_api/ApiHelpers.h @@ -1,10 +1,11 @@ #pragma once -#include +#include "src/core/m256i.h" + #include #include +#include #include -#include "Entity.h" // ============================================================================ // Shared Data Structures for API responses diff --git a/RESTAPI/LogSubscriptionManager.cpp b/src/rest_api/LogSubscriptionManager.cpp similarity index 99% rename from RESTAPI/LogSubscriptionManager.cpp rename to src/rest_api/LogSubscriptionManager.cpp index 862f8f4..21198be 100644 --- a/RESTAPI/LogSubscriptionManager.cpp +++ b/src/rest_api/LogSubscriptionManager.cpp @@ -1,14 +1,14 @@ #include "LogSubscriptionManager.h" +#include "src/core/structs.h" +#include "src/logger/logger.h" +#include "src/shim.h" #include "QubicSubscriptionManager.h" -#include "database/db.h" -#include "Logger.h" -#include "shim.h" -#include "structs.h" +#include "src/database/db.h" +#include +#include #include #include -#include -#include LogSubscriptionManager& LogSubscriptionManager::instance() { static LogSubscriptionManager inst; diff --git a/RESTAPI/LogSubscriptionManager.h b/src/rest_api/LogSubscriptionManager.h similarity index 98% rename from RESTAPI/LogSubscriptionManager.h rename to src/rest_api/LogSubscriptionManager.h index a57ca63..ef9021b 100644 --- a/RESTAPI/LogSubscriptionManager.h +++ b/src/rest_api/LogSubscriptionManager.h @@ -9,8 +9,8 @@ #include #include +#include "src/core/log_event/log_event.h" #include "drogon/WebSocketConnection.h" -#include "LogEvent.h" // Subscription key: (scIndex, logType) pair struct SubscriptionKey { diff --git a/RESTAPI/LogWebSocket.cpp b/src/rest_api/LogWebSocket.cpp similarity index 99% rename from RESTAPI/LogWebSocket.cpp rename to src/rest_api/LogWebSocket.cpp index 99fd18d..fe9fdca 100644 --- a/RESTAPI/LogWebSocket.cpp +++ b/src/rest_api/LogWebSocket.cpp @@ -1,8 +1,8 @@ #include "LogWebSocket.h" +#include "src/logger/logger.h" +#include "src/shim.h" #include "LogSubscriptionManager.h" -#include "Logger.h" -#include "shim.h" -#include "database/db.h" +#include "src/database/db.h" #include diff --git a/RESTAPI/LogWebSocket.h b/src/rest_api/LogWebSocket.h similarity index 100% rename from RESTAPI/LogWebSocket.h rename to src/rest_api/LogWebSocket.h diff --git a/RESTAPI/QubicRpcHandler.cpp b/src/rest_api/QubicRpcHandler.cpp similarity index 99% rename from RESTAPI/QubicRpcHandler.cpp rename to src/rest_api/QubicRpcHandler.cpp index e650f5b..a8176cc 100644 --- a/RESTAPI/QubicRpcHandler.cpp +++ b/src/rest_api/QubicRpcHandler.cpp @@ -1,8 +1,8 @@ #include "QubicRpcHandler.h" -#include "QubicRpcMethods.h" #include "QubicRpcMapper.h" -#include "Logger.h" -#include "shim.h" +#include "QubicRpcMethods.h" +#include "src/logger/logger.h" +#include "src/shim.h" namespace QubicRpcHandler { diff --git a/RESTAPI/QubicRpcHandler.h b/src/rest_api/QubicRpcHandler.h similarity index 100% rename from RESTAPI/QubicRpcHandler.h rename to src/rest_api/QubicRpcHandler.h diff --git a/RESTAPI/QubicRpcMapper.cpp b/src/rest_api/QubicRpcMapper.cpp similarity index 99% rename from RESTAPI/QubicRpcMapper.cpp rename to src/rest_api/QubicRpcMapper.cpp index 8474c9e..64e0dfa 100644 --- a/RESTAPI/QubicRpcMapper.cpp +++ b/src/rest_api/QubicRpcMapper.cpp @@ -1,13 +1,13 @@ #include "QubicRpcMapper.h" -#include "K12AndKeyUtil.h" -#include "shim.h" -#include "database/db.h" -#include "GlobalVar.h" -#include "defines.h" -#include -#include +#include "src/global_var.h" +#include "src/core/defines.h" +#include "src/shim.h" +#include "src/core/k12_and_key_util.h" +#include "src/database/db.h" #include #include +#include +#include namespace QubicRpc { diff --git a/RESTAPI/QubicRpcMapper.h b/src/rest_api/QubicRpcMapper.h similarity index 97% rename from RESTAPI/QubicRpcMapper.h rename to src/rest_api/QubicRpcMapper.h index 8fc99e4..3b55fa9 100644 --- a/RESTAPI/QubicRpcMapper.h +++ b/src/rest_api/QubicRpcMapper.h @@ -1,11 +1,12 @@ #pragma once +#include "src/core/m256i.h" +#include "src/core/structs.h" +#include "src/core/log_event/log_event.h" + +#include #include #include -#include -#include "m256i.h" -#include "structs.h" -#include "LogEvent.h" namespace QubicRpc { diff --git a/RESTAPI/QubicRpcMethods.cpp b/src/rest_api/QubicRpcMethods.cpp similarity index 99% rename from RESTAPI/QubicRpcMethods.cpp rename to src/rest_api/QubicRpcMethods.cpp index e044b8f..56d5765 100644 --- a/RESTAPI/QubicRpcMethods.cpp +++ b/src/rest_api/QubicRpcMethods.cpp @@ -1,19 +1,19 @@ #include "QubicRpcMethods.h" -#include "QubicSubscriptionManager.h" -#include "QubicRpcMapper.h" +#include "src/bob.h" +#include "src/core/asset.h" +#include "src/core/entity.h" +#include "src/core/k12_and_key_util.h" +#include "src/logger/logger.h" +#include "src/shim.h" +#include "src/version.h" #include "ApiHelpers.h" -#include "shim.h" -#include "database/db.h" -#include "Logger.h" -#include "Version.h" -#include "K12AndKeyUtil.h" -#include "Entity.h" -#include "Asset.h" -#include "GlobalVar.h" -#include "bob.h" -#include +#include "src/global_var.h" +#include "QubicRpcMapper.h" +#include "QubicSubscriptionManager.h" +#include "src/database/db.h" #include #include +#include // ============================================================================ // Helper Methods diff --git a/RESTAPI/QubicRpcMethods.h b/src/rest_api/QubicRpcMethods.h similarity index 100% rename from RESTAPI/QubicRpcMethods.h rename to src/rest_api/QubicRpcMethods.h diff --git a/RESTAPI/QubicRpcWebSocket.cpp b/src/rest_api/QubicRpcWebSocket.cpp similarity index 99% rename from RESTAPI/QubicRpcWebSocket.cpp rename to src/rest_api/QubicRpcWebSocket.cpp index f3a521f..985148d 100644 --- a/RESTAPI/QubicRpcWebSocket.cpp +++ b/src/rest_api/QubicRpcWebSocket.cpp @@ -1,10 +1,10 @@ #include "QubicRpcWebSocket.h" +#include "src/logger/logger.h" +#include "src/shim.h" #include "QubicRpcHandler.h" +#include "QubicRpcMapper.h" #include "QubicRpcMethods.h" #include "QubicSubscriptionManager.h" -#include "QubicRpcMapper.h" -#include "Logger.h" -#include "shim.h" #include void QubicRpcWebSocket::handleNewConnection( diff --git a/RESTAPI/QubicRpcWebSocket.h b/src/rest_api/QubicRpcWebSocket.h similarity index 100% rename from RESTAPI/QubicRpcWebSocket.h rename to src/rest_api/QubicRpcWebSocket.h diff --git a/RESTAPI/QubicSubscriptionManager.cpp b/src/rest_api/QubicSubscriptionManager.cpp similarity index 99% rename from RESTAPI/QubicSubscriptionManager.cpp rename to src/rest_api/QubicSubscriptionManager.cpp index 6d48d68..10ddce8 100644 --- a/RESTAPI/QubicSubscriptionManager.cpp +++ b/src/rest_api/QubicSubscriptionManager.cpp @@ -1,17 +1,17 @@ #include "QubicSubscriptionManager.h" +#include "src/global_var.h" +#include "src/core/defines.h" +#include "src/logger/logger.h" +#include "src/shim.h" +#include "src/core/k12_and_key_util.h" #include "QubicRpcMapper.h" -#include "Logger.h" -#include "GlobalVar.h" -#include "shim.h" -#include "K12AndKeyUtil.h" -#include "database/db.h" -#include "defines.h" +#include "src/database/db.h" #include "drogon/drogon.h" -#include -#include +#include #include +#include +#include #include -#include QubicSubscriptionManager& QubicSubscriptionManager::instance() { static QubicSubscriptionManager instance; diff --git a/RESTAPI/QubicSubscriptionManager.h b/src/rest_api/QubicSubscriptionManager.h similarity index 99% rename from RESTAPI/QubicSubscriptionManager.h rename to src/rest_api/QubicSubscriptionManager.h index e9761e1..ade92b4 100644 --- a/RESTAPI/QubicSubscriptionManager.h +++ b/src/rest_api/QubicSubscriptionManager.h @@ -1,15 +1,15 @@ #pragma once +#include "src/core/structs.h" +#include "src/core/log_event/log_event.h" +#include "drogon/WebSocketConnection.h" +#include +#include +#include #include -#include #include #include -#include -#include -#include -#include "drogon/WebSocketConnection.h" -#include "LogEvent.h" -#include "structs.h" +#include // Subscription types for Qubic API enum class QubicSubscriptionType { diff --git a/RESTAPI/RESTServer.cpp b/src/rest_api/RESTServer.cpp similarity index 99% rename from RESTAPI/RESTServer.cpp rename to src/rest_api/RESTServer.cpp index 9c776b1..f69211f 100644 --- a/RESTAPI/RESTServer.cpp +++ b/src/rest_api/RESTServer.cpp @@ -19,11 +19,11 @@ #include #include -#include "bob.h" -#include "Logger.h" -#include "shim.h" -#include "database/db.h" +#include "src/bob.h" +#include "src/logger/logger.h" +#include "src/shim.h" #include "ApiHelpers.h" +#include "src/database/db.h" // OpenAPI spec - embedded at compile time or loaded from file static std::string g_openApiSpec; diff --git a/RESTAPI/bobAPI.cpp b/src/rest_api/bobAPI.cpp similarity index 99% rename from RESTAPI/bobAPI.cpp rename to src/rest_api/bobAPI.cpp index 0b7d705..19681c7 100644 --- a/RESTAPI/bobAPI.cpp +++ b/src/rest_api/bobAPI.cpp @@ -1,16 +1,16 @@ // interop for other program to interact with BOB -#include "bob.h" -#include "K12AndKeyUtil.h" -#include "shim.h" -#include "Entity.h" -#include "database/db.h" -#include "Asset.h" +#include "src/bob.h" +#include "src/core/asset.h" +#include "src/core/entity.h" +#include "src/core/k12_and_key_util.h" +#include "src/shim.h" +#include "src/version.h" #include "ApiHelpers.h" +#include "src/database/db.h" +#include #include -#include #include -#include -#include "Version.h" +#include // helper: hex-encode static std::string toHex(const std::vector& data) { std::stringstream ss; diff --git a/RESTAPI/openapi.json b/src/rest_api/openapi.json similarity index 100% rename from RESTAPI/openapi.json rename to src/rest_api/openapi.json diff --git a/RESTAPI/querySmartContract.cpp b/src/rest_api/querySmartContract.cpp similarity index 94% rename from RESTAPI/querySmartContract.cpp rename to src/rest_api/querySmartContract.cpp index cc28f8d..9a07cdb 100644 --- a/RESTAPI/querySmartContract.cpp +++ b/src/rest_api/querySmartContract.cpp @@ -1,6 +1,6 @@ +#include "src/shim.h" +#include "src/connection/connection.h" #include -#include "connection/connection.h" -#include "shim.h" void querySmartContractThread(ConnectionPool& connPoolAll) { diff --git a/shim.h b/src/shim.h similarity index 99% rename from shim.h rename to src/shim.h index 6d92f1d..26b574d 100644 --- a/shim.h +++ b/src/shim.h @@ -1,5 +1,5 @@ #pragma once -#include "GlobalVar.h" +#include "global_var.h" #define MRB_Data (GS().MRB_Data) #define MRB_Request (GS().MRB_Request) diff --git a/SpecialBufferStructs.h b/src/special_buffer_structs.h similarity index 99% rename from SpecialBufferStructs.h rename to src/special_buffer_structs.h index 41438fd..2363418 100644 --- a/SpecialBufferStructs.h +++ b/src/special_buffer_structs.h @@ -1,17 +1,17 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include "src/core/structs.h" +#include // For timestamps #include +#include +#include #include // For memcpy +#include #include -#include // For timestamps -#include "structs.h" +#include +#include +#include +#include /** * @class MutexRoundBuffer * @brief A thread-safe circular buffer for storing and retrieving variable-length raw data packets. diff --git a/utils.h b/src/utils/utils.h similarity index 64% rename from utils.h rename to src/utils/utils.h index 308e524..a3ea287 100644 --- a/utils.h +++ b/src/utils/utils.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include static void byteToHex(const uint8_t* byte, char* hex, const int sizeInByte) @@ -33,4 +35,23 @@ static void rand64(uint64_t* r) { static thread_local std::mt19937 generator; std::uniform_int_distribution distribution(0,UINT32_MAX); *r = distribution(generator); +} + +static bool isArrayZero(uint8_t *ptr, int len) { + for (int i = 0; i < len; i++) { + if (ptr[i] != 0) { + return false; + } + } + return true; +} + +// Helper to convert byte array to hex string +static std::string bytes_to_hex_string(const unsigned char* bytes, size_t size) { + std::stringstream ss; + ss << std::hex << std::setfill('0'); + for (size_t i = 0; i < size; ++i) { + ss << std::setw(2) << static_cast(bytes[i]); + } + return ss.str(); } \ No newline at end of file diff --git a/Version.h b/src/version.h similarity index 100% rename from Version.h rename to src/version.h diff --git a/tests/test_round_buffer.cpp b/tests/test_round_buffer.cpp index 7ffee43..3550c66 100644 --- a/tests/test_round_buffer.cpp +++ b/tests/test_round_buffer.cpp @@ -4,8 +4,8 @@ #include #include // Include the headers for the code under test -#include "structs.h" -#include "SpecialBufferStructs.h" +#include "src/special_buffer_structs.h" +#include "src/core/structs.h" // Provide a definition for the extern variable to allow linking. // This instance won't be used in the tests; we'll create local instances. @@ -167,18 +167,17 @@ TEST_F(MutexRoundBufferTest, SingleProducerSingleConsumer) { buffer.EnqueuePacket(packet.data()); } }); + producer.join(); std::thread consumer([&]() { - for (int i = 0; i < num_packets; ++i) { - std::vector out(BUFFER_CAPACITY); - uint32_t outSize = 0; - buffer.TryGetPacket(out.data(), outSize); - out.resize(outSize); - received_packets.push_back(out); - } + for (int i = 0; i < num_packets; ++i) { + std::vector out(BUFFER_CAPACITY); + uint32_t outSize = 0; + buffer.TryGetPacket(out.data(), outSize); + out.resize(outSize); + received_packets.push_back(out); + } }); - - producer.join(); consumer.join(); ASSERT_EQ(sent_packets.size(), num_packets); @@ -205,6 +204,10 @@ TEST_F(MutexRoundBufferTest, MultipleProducersSingleConsumer) { }); } + for (auto &p : producers) { + p.join(); + } + std::vector> received_packets; std::thread consumer([&]() { for (int i = 0; i < total_packets; ++i) { @@ -216,9 +219,6 @@ TEST_F(MutexRoundBufferTest, MultipleProducersSingleConsumer) { } }); - for (auto& p : producers) { - p.join(); - } consumer.join(); ASSERT_EQ(received_packets.size(), total_packets); diff --git a/tests/test_xkcp.cpp b/tests/test_xkcp.cpp index b212950..bfd02a1 100644 --- a/tests/test_xkcp.cpp +++ b/tests/test_xkcp.cpp @@ -1,5 +1,5 @@ -#include "K12AndKeyUtil.h" -//#include "XKCP/KangarooTwelve.h" +#include "src/core/k12_and_key_util.h" +// #include "XKCP/KangarooTwelve.h" #include #include #include diff --git a/rpc_playground.html b/web/rpc_playground.html similarity index 100% rename from rpc_playground.html rename to web/rpc_playground.html