Skip to content

Commit

Permalink
feat(nspv): put it into a wip features
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Nov 6, 2019
1 parent e217a27 commit 77ab8b3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 81 deletions.
53 changes: 28 additions & 25 deletions modules/blockchain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
58 changes: 11 additions & 47 deletions modules/blockchain/antara/gaming/blockchain/nspv.system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,7 @@
#include <future>
#include <mutex>
#include <antara/gaming/blockchain/nspv.system.hpp>
#include <antara/gaming/event/fatal.error.hpp>

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<std::mutex> lock(mutex_);
out_.append(reinterpret_cast<const char *>(buffer), size);
return true;
}

private:
std::string &out_;
std::mutex &mutex_;
};

#include <reproc++/sink.hpp>

namespace antara::gaming::blockchain {
nspv::nspv(entt::registry &registry, fs::path tools_path) noexcept :
Expand All @@ -45,16 +28,16 @@ namespace antara::gaming::blockchain {
}

void nspv::update() noexcept {
for (auto &&[coin, _] : registry_) {
std::lock_guard<std::mutex> 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) {
Expand All @@ -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<std::string, 1> 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<std::string, 1> 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<event::fatal_error>(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);
})) {}
}
6 changes: 2 additions & 4 deletions modules/blockchain/antara/gaming/blockchain/nspv.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::error_code> async_drain;
};
};*/

class nspv final : public ecs::logic_update_system<nspv> {
public:
Expand All @@ -45,9 +45,7 @@ namespace antara::gaming::blockchain {
private:
std::filesystem::path tools_path_;
using nspv_registry = std::unordered_map<std::string, reproc::process>;
using async_drain_registry = std::unordered_map<std::string, nspv_output>;
nspv_registry registry_;
async_drain_registry output_registry_;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@

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<entt::dispatcher>()};
antara::gaming::ecs::system_manager mgr{entity_registry};

auto& nspv_system = mgr.create_system<blockchain::nspv>(std::filesystem::current_path() / "nspv/assets/tools");
nspv_system.update();
}*/
}

TEST_CASE("nspv system spawn")
{
Expand All @@ -38,10 +38,10 @@ namespace antara::gaming::blockchain::tests
antara::gaming::ecs::system_manager mgr{entity_registry};

auto& nspv_system = mgr.create_system<blockchain::nspv>(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();
}*/
}
}
}

0 comments on commit 77ab8b3

Please sign in to comment.