Skip to content

Commit

Permalink
feat(nspv): try to fix multithread stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Dec 3, 2019
1 parent 0b13d72 commit 9c9d0ad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ get_nspv_assets(${CMAKE_CURRENT_SOURCE_DIR})
set(ICON)
configure_icon_osx(data/osx/kmd_logo.icns ICON kmd_logo.icns)

add_executable(antara-blockchain-ingame-shop-example MACOSX_BUNDLE ${ICON} main.cpp)
add_executable(antara-blockchain-ingame-shop-example MACOSX_BUNDLE ${ICON} nspv_fake_ingame_shop.cpp)
target_enable_tsan(antara-blockchain-ingame-shop-example)
target_link_libraries(antara-blockchain-ingame-shop-example PUBLIC antara::world antara::scenes antara::nspv antara::sfml antara::audio antara::animation2d)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <imgui.h>

#include <thread>
#include <mutex>
#include <chrono>

#include <antara/gaming/world/world.app.hpp>
Expand Down Expand Up @@ -53,6 +54,8 @@ class fake_shop final : public ecs::post_update_system<fake_shop> {

public:
void update_balances() {
std::scoped_lock st_lock(store_mutex, user_mutex);

store.nspv_balance = nspv_system_store_.get_balance(currency);
user.nspv_balance = nspv_system_user_.get_balance(currency);

Expand Down Expand Up @@ -88,7 +91,12 @@ class fake_shop final : public ecs::post_update_system<fake_shop> {
std::this_thread::sleep_for(5s);
// Update pending transactions count
blockchain::nspv_api::mempool_request request{user.wallet_address};
mempool_transaction_count = blockchain::nspv_api::mempool(nspv_system_user_.get_endpoint(currency), request).txids.size();

{
std::scoped_lock st_lock(user_mutex);
mempool_transaction_count = blockchain::nspv_api::mempool(
nspv_system_user_.get_endpoint(currency), request).txids.size();
}

// Update balances
update_balances();
Expand Down Expand Up @@ -126,6 +134,7 @@ class fake_shop final : public ecs::post_update_system<fake_shop> {
}

void display_balance(const inventory& inv, bool show_pending_count = false) {
std::scoped_lock st_lock(store_mutex, user_mutex);
if(show_pending_count) ImGui::Text("Pending Transactions: %d, Mempool: %d", pending_transaction_count, mempool_transaction_count);

bool pending = inv.pending_balance != 0;
Expand All @@ -136,6 +145,7 @@ class fake_shop final : public ecs::post_update_system<fake_shop> {
}

bool user_has_enough_funds(int price) {
std::scoped_lock lock(user_mutex);
return user.balance >= price;
}

Expand Down Expand Up @@ -341,6 +351,8 @@ class fake_shop final : public ecs::post_update_system<fake_shop> {
bool application_quits{false};

// Inventories
std::mutex store_mutex;
std::mutex user_mutex;
inventory store{0};
inventory user{0};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ namespace antara::gaming::blockchain {
LOG_SCOPE_FUNCTION(INFO);
auto result = nspv_api::mempool(get_endpoint(coin));
if (result.rpc_result_code == -1) {
//! TODO: add a out parameter for error
VLOG_F(loguru::Verbosity_ERROR, "mempool rpc call failed: {}", result.raw_result);
return true;
}
Expand Down

0 comments on commit 9c9d0ad

Please sign in to comment.