diff --git a/modules/blockchain/CMakeLists.txt b/modules/blockchain/CMakeLists.txt index 40400ff7..b82278f6 100644 --- a/modules/blockchain/CMakeLists.txt +++ b/modules/blockchain/CMakeLists.txt @@ -4,35 +4,38 @@ target_include_directories(antara_nspv PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(antara_nspv PUBLIC antara::default_settings antara::ecs reproc++) add_library(antara::nspv ALIAS antara_nspv) -if (ANTARA_BUILD_UNIT_TESTS) - ##! antara blockchain tests - add_executable(antara_blockchain_tests) - target_link_libraries(antara_blockchain_tests PRIVATE doctest PUBLIC antara::nspv) - target_sources(antara_blockchain_tests PRIVATE - antara/gaming/blockchain/antara.blockchain.tests.cpp - antara/gaming/blockchain/nspv.system.tests.cpp) - set_target_properties(antara_blockchain_tests - PROPERTIES - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/unit_tests" - ) +if (ANTARA_WIP_FEATURES) + if (ANTARA_BUILD_UNIT_TESTS) + ##! antara blockchain tests + add_executable(antara_blockchain_tests) - file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/assets DESTINATION ${CMAKE_BINARY_DIR}/bin/unit_tests/nspv/) + target_link_libraries(antara_blockchain_tests PRIVATE doctest PUBLIC antara::nspv) + target_sources(antara_blockchain_tests PRIVATE + antara/gaming/blockchain/antara.blockchain.tests.cpp + antara/gaming/blockchain/nspv.system.tests.cpp) + set_target_properties(antara_blockchain_tests + PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/unit_tests" + ) + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/assets DESTINATION ${CMAKE_BINARY_DIR}/bin/unit_tests/nspv/) - target_enable_coverage(antara_blockchain_tests) - target_enable_tsan(antara_blockchain_tests) - target_enable_asan(antara_blockchain_tests) - target_enable_ubsan(antara_blockchain_tests) - if (EMSCRIPTEN) - message(STATUS "Emscripten detected") - if (ENABLE_HTML_COMPILATION) - message(STATUS "Html compilation enabled") - set_target_properties(antara_blockchain_tests PROPERTIES LINK_FLAGS "-s FORCE_FILESYSTEM=1 -s EXIT_RUNTIME=1" - SUFFIX ".html") - else () - message(STATUS "Local js compilation") - set_target_properties(antara_blockchain_tests PROPERTIES LINK_FLAGS "-s FORCE_FILESYSTEM=1 -s NODERAWFS=1 -s EXIT_RUNTIME=1") + target_enable_coverage(antara_blockchain_tests) + target_enable_tsan(antara_blockchain_tests) + target_enable_asan(antara_blockchain_tests) + target_enable_ubsan(antara_blockchain_tests) + + if (EMSCRIPTEN) + message(STATUS "Emscripten detected") + if (ENABLE_HTML_COMPILATION) + message(STATUS "Html compilation enabled") + set_target_properties(antara_blockchain_tests PROPERTIES LINK_FLAGS "-s FORCE_FILESYSTEM=1 -s EXIT_RUNTIME=1" + SUFFIX ".html") + else () + message(STATUS "Local js compilation") + set_target_properties(antara_blockchain_tests PROPERTIES LINK_FLAGS "-s FORCE_FILESYSTEM=1 -s NODERAWFS=1 -s EXIT_RUNTIME=1") + endif () endif () endif () endif () \ No newline at end of file diff --git a/modules/blockchain/antara/gaming/blockchain/nspv.system.cpp b/modules/blockchain/antara/gaming/blockchain/nspv.system.cpp index 4b27e1e8..ffbfad37 100644 --- a/modules/blockchain/antara/gaming/blockchain/nspv.system.cpp +++ b/modules/blockchain/antara/gaming/blockchain/nspv.system.cpp @@ -18,24 +18,7 @@ #include #include #include -#include - -class thread_safe_string_sink { -public: - thread_safe_string_sink(std::string &out, std::mutex &mutex) - : out_(out), mutex_(mutex) {} - - bool operator()(const uint8_t *buffer, unsigned int size) { - std::lock_guard lock(mutex_); - out_.append(reinterpret_cast(buffer), size); - return true; - } - -private: - std::string &out_; - std::mutex &mutex_; -}; - +#include namespace antara::gaming::blockchain { nspv::nspv(entt::registry ®istry, fs::path tools_path) noexcept : @@ -45,16 +28,16 @@ namespace antara::gaming::blockchain { } void nspv::update() noexcept { - for (auto &&[coin, _] : registry_) { - std::lock_guard lock(output_registry_.at(coin).output_mutex); - DVLOG_F(loguru::Verbosity_INFO, "nspv process output: \n{}", output_registry_.at(coin).output); - output_registry_.at(coin).output.clear(); + for (auto &&[coin, background] : registry_) { + std::stringstream ss; + background.drain(reproc::stream::out, reproc::sink::ostream(ss)); + DVLOG_F(loguru::Verbosity_INFO, "nspv output: {}", ss.str()); } } nspv::~nspv() noexcept { LOG_SCOPE_FUNCTION(INFO); - for (auto &&[_, background] : registry_) { + for (auto &&[coin, background] : registry_) { auto ec = background.stop(reproc::cleanup::terminate, reproc::milliseconds(2000), reproc::cleanup::kill, reproc::infinite); if (ec) { @@ -65,33 +48,14 @@ namespace antara::gaming::blockchain { bool nspv::spawn_nspv_instance(const std::string &coin) noexcept { LOG_SCOPE_FUNCTION(INFO); - auto it = registry_.emplace(coin, reproc::process{reproc::cleanup::terminate, - reproc::milliseconds(2000), - reproc::cleanup::kill, - reproc::infinite}).first; - reproc::process &background = it->second; - std::array args = {"nspvs"}; - auto ec = background.start(args, tools_path_.string().c_str()); + registry_[coin] = reproc::process(reproc::cleanup::terminate, reproc::milliseconds(2000), + reproc::cleanup::kill, reproc::infinite); + std::array args = {tools_path_ / "nspv"}; + auto ec = registry_[coin].start(args, tools_path_.string().c_str()); if (ec) { - DVLOG_F(loguru::Verbosity_FATAL, "couldn't start nspv process with coin: {}, error: {}", coin, - ec.message()); - this->dispatcher_.trigger(ec); + DVLOG_F(loguru::Verbosity_ERROR, "error: {}", ec.message()); return false; } - output_registry_.try_emplace(coin, background); return true; } - - nspv_output::nspv_output(reproc::process &background) noexcept : - output(""), - output_mutex(), - async_drain( - std::async(std::launch::async, [this, &background]() { - thread_safe_string_sink sink( - output, - output_mutex); - return background.drain( - reproc::stream::out, - sink); - })) {} } \ No newline at end of file diff --git a/modules/blockchain/antara/gaming/blockchain/nspv.system.hpp b/modules/blockchain/antara/gaming/blockchain/nspv.system.hpp index 5f961f73..0cde94c2 100644 --- a/modules/blockchain/antara/gaming/blockchain/nspv.system.hpp +++ b/modules/blockchain/antara/gaming/blockchain/nspv.system.hpp @@ -28,13 +28,13 @@ namespace fs = std::filesystem; namespace antara::gaming::blockchain { - struct nspv_output + /*struct nspv_output { nspv_output(reproc::process& background) noexcept; std::string output; std::mutex output_mutex; std::future async_drain; - }; + };*/ class nspv final : public ecs::logic_update_system { public: @@ -45,9 +45,7 @@ namespace antara::gaming::blockchain { private: std::filesystem::path tools_path_; using nspv_registry = std::unordered_map; - using async_drain_registry = std::unordered_map; nspv_registry registry_; - async_drain_registry output_registry_; }; } diff --git a/modules/blockchain/antara/gaming/blockchain/nspv.system.tests.cpp b/modules/blockchain/antara/gaming/blockchain/nspv.system.tests.cpp index e9f3a458..dbfdec96 100644 --- a/modules/blockchain/antara/gaming/blockchain/nspv.system.tests.cpp +++ b/modules/blockchain/antara/gaming/blockchain/nspv.system.tests.cpp @@ -21,7 +21,7 @@ namespace antara::gaming::blockchain::tests { - /*TEST_CASE("nspv system creation") + TEST_CASE("nspv system creation") { entt::registry entity_registry; [[ maybe_unused ]] entt::dispatcher& dispatcher{entity_registry.set()}; @@ -29,7 +29,7 @@ namespace antara::gaming::blockchain::tests auto& nspv_system = mgr.create_system(std::filesystem::current_path() / "nspv/assets/tools"); nspv_system.update(); - }*/ + } TEST_CASE("nspv system spawn") { @@ -38,10 +38,10 @@ namespace antara::gaming::blockchain::tests antara::gaming::ecs::system_manager mgr{entity_registry}; auto& nspv_system = mgr.create_system(std::filesystem::current_path() / "nspv/assets/tools"); - nspv_system.spawn_nspv_instance("RICK"); - /*for (auto start = std::chrono::steady_clock::now(), now = start; now < start + std::chrono::seconds{15}; now = std::chrono::steady_clock::now()) + CHECK(nspv_system.spawn_nspv_instance("RICK")); + for (auto start = std::chrono::steady_clock::now(), now = start; now < start + std::chrono::seconds{1}; now = std::chrono::steady_clock::now()) { nspv_system.update(); - }*/ + } } } \ No newline at end of file