game: fix race condition when refreshing wallet balance

This commit is contained in:
Crypto City 2020-09-09 15:51:32 +00:00
parent 477f6e1022
commit dba0851fc7
3 changed files with 35 additions and 10 deletions

View File

@ -74,8 +74,11 @@ void PlayerState::update(const std::shared_ptr<GameWallet> &wallet)
has_wallet = true;
// continue to fill up what we can
}
wallet_balance = w->balance(0, false);
wallet_unlocked_balance = w->unlocked_balance(0, false);
if (!wallet->get_wallet_balance(wallet_balance, wallet_unlocked_balance))
{
wallet_balance = 0;
wallet_unlocked_balance = 0;
}
}
GameState::GameState(Urho3D::Context *ctx):

View File

@ -346,11 +346,12 @@ public:
bool get_game_events(uint64_t min_height, uint64_t max_height, uint32_t account, uint32_t flag, uint32_t item, uint8_t cmd, cc::game_events_t &events);
bool send_command(cryptonote::cc_command_t cmd, uint64_t *nonce = NULL);
void set_attribute(const std::string &key, const std::string &value);
bool get_attribute(const std::string &key, std::string &value) const;
uint32_t lookup_account(const std::string &pkey) const;
bool get_attribute(const std::string &key, std::string &value);
uint32_t lookup_account(const std::string &pkey);
bool get_invitation_status(const std::string &pkey, bool &used, bool &balance_ok) const;
bool generate_invitation(uint64_t amount, uint64_t expiration, std::string &invitation) const;
std::vector<uint64_t> get_item_count(const std::vector<uint32_t> &id) const;
bool get_wallet_balance(uint64_t &full, uint64_t &unlocked);
bool get_new_cc_flag_cost(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint64_t &cost);
bool get_cc_order_book(std::vector<cryptonote::matchable_order_as_string_t> *bids, std::vector<cryptonote::matchable_order_as_string_t> *offers, const std::vector<uint32_t> &type, const std::vector<uint32_t> &id);
@ -841,6 +842,7 @@ bool GameWalletInternal::generate_new_wallet(const char *wallet_file)
void GameWalletInternal::save()
{
boost::unique_lock<boost::mutex> lock(mutex);
try
{
if (w)
@ -854,6 +856,7 @@ void GameWalletInternal::save()
bool GameWalletInternal::deposit(const GameState *game, uint64_t amount, const std::string &invitation, const String &name, uint32_t inviting_account, bool confirmed)
{
boost::unique_lock<boost::mutex> lock(mutex);
// send dummy 0 output, the other output will be change back
cryptonote::account_base dummy;
dummy.generate();
@ -927,6 +930,7 @@ bool GameWalletInternal::deposit(const GameState *game, uint64_t amount, const s
bool GameWalletInternal::withdraw(uint64_t amount)
{
boost::unique_lock<boost::mutex> lock(mutex);
try
{
@ -1174,10 +1178,11 @@ bool GameWalletInternal::get_game_events(uint64_t min_height, uint64_t max_heigh
return w->get_cc_game_events(min_height, max_height, account, flag, item, cmd, events);
}
uint32_t GameWalletInternal::lookup_account(const std::string &pkey) const
uint32_t GameWalletInternal::lookup_account(const std::string &pkey)
{
uint64_t balance;
uint32_t id;
boost::unique_lock<boost::mutex> lock(mutex);
if (!w->lookup_cc_account(pkey, id, balance))
return 0;
return id;
@ -1196,6 +1201,14 @@ std::vector<uint64_t> GameWalletInternal::get_item_count(const std::vector<uint3
return counts;
}
bool GameWalletInternal::get_wallet_balance(uint64_t &full, uint64_t &unlocked)
{
boost::unique_lock<boost::mutex> lock(mutex);
full = w->balance(0, false);
unlocked = w->unlocked_balance(0, false);
return true;
}
bool GameWalletInternal::generate_invitation(uint64_t amount, uint64_t expiration, std::string &invitation) const
{
const uint32_t id = w->get_cc_account();
@ -1223,6 +1236,7 @@ bool GameWalletInternal::send_command(cryptonote::cc_command_t cmd, uint64_t *no
basenonce->cc_nonce = nonce[0];
}
boost::unique_lock<boost::mutex> lock(mutex);
#if 0
if (!try_connect_to_daemon())
return false;
@ -1434,12 +1448,14 @@ bool GameWalletInternal::send_command(cryptonote::cc_command_t cmd, uint64_t *no
void GameWalletInternal::set_attribute(const std::string &key, const std::string &value)
{
boost::unique_lock<boost::mutex> lock(mutex);
if (w)
w->set_attribute(key, value);
}
bool GameWalletInternal::get_attribute(const std::string &key, std::string &value) const
bool GameWalletInternal::get_attribute(const std::string &key, std::string &value)
{
boost::unique_lock<boost::mutex> lock(mutex);
return w && w->get_attribute(key, value);
}
@ -1723,7 +1739,7 @@ void GameWallet::set_attribute(const std::string &key, const std::string &value)
return internal->set_attribute(key, value);
}
bool GameWallet::get_attribute(const std::string &key, std::string &value) const
bool GameWallet::get_attribute(const std::string &key, std::string &value)
{
return internal->get_attribute(key, value);
}
@ -1843,7 +1859,7 @@ uint32_t GameWallet::get_authorized_game_account() const
return internal->get_authorized_game_account();
}
uint32_t GameWallet::lookup_account(const std::string &pkey) const
uint32_t GameWallet::lookup_account(const std::string &pkey)
{
return internal->lookup_account(pkey);
}
@ -1858,6 +1874,11 @@ std::vector<uint64_t> GameWallet::get_item_count(const std::vector<uint32_t> &id
return internal->get_item_count(id);
}
bool GameWallet::get_wallet_balance(uint64_t &full, uint64_t &unlocked)
{
return internal->get_wallet_balance(full, unlocked);
}
bool GameWallet::generate_invitation(uint64_t amount, uint64_t expiration, std::string &invitation) const
{
return internal->generate_invitation(amount, expiration, invitation);

View File

@ -67,7 +67,7 @@ public:
bool get_game_events(uint64_t min_height, uint64_t max_height, uint32_t account, uint32_t flag, uint32_t item, uint8_t cmd, cc::game_events_t &events);
bool send_command(cryptonote::cc_command_t cmd, uint64_t *nonce = NULL);
void set_attribute(const std::string &key, const std::string &value);
bool get_attribute(const std::string &key, std::string &value) const;
bool get_attribute(const std::string &key, std::string &value);
bool get_new_cc_flag_cost(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint64_t &cost);
bool get_cc_order_book(std::vector<cryptonote::matchable_order_as_string_t> *bids, std::vector<cryptonote::matchable_order_as_string_t> *offers, const std::vector<uint32_t> &type, const std::vector<uint32_t> &id);
@ -86,9 +86,10 @@ public:
bool get_cc_research_bonuses(const std::vector<uint32_t> &accounts, std::vector<uint32_t> &bonuses) const;
bool are_cc_discoveries_enabled(uint32_t account, const std::vector<uint32_t> &discoveries, std::vector<bool> &enabled) const;
bool is_cc_discovery_enabled(uint32_t account, uint32_t discovery, bool &enabled) const;
uint32_t lookup_account(const std::string &pkey) const;
uint32_t lookup_account(const std::string &pkey);
bool get_invitation_status(const std::string &pkey, bool &used, bool &balance_ok) const;
std::vector<uint64_t> get_item_count(const std::vector<uint32_t> &id) const;
bool get_wallet_balance(uint64_t &full, uint64_t &unlocked);
std::shared_ptr<tools::wallet2> wallet();
bool is_spectator() const;