libwallet_api: do not signal on sent/received tx until wallet completely

synchronized
This commit is contained in:
Ilya Kitaev 2016-10-07 00:29:13 +03:00
parent 11fab41c36
commit 62b3708ea5
3 changed files with 31 additions and 6 deletions

View file

@ -54,8 +54,9 @@ namespace {
struct Wallet2CallbackImpl : public tools::i_wallet2_callback struct Wallet2CallbackImpl : public tools::i_wallet2_callback
{ {
Wallet2CallbackImpl() Wallet2CallbackImpl(WalletImpl * wallet)
: m_listener(nullptr) : m_listener(nullptr)
, m_wallet(wallet)
{ {
} }
@ -93,7 +94,8 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
LOG_PRINT_L3(__FUNCTION__ << ": money received. height: " << height LOG_PRINT_L3(__FUNCTION__ << ": money received. height: " << height
<< ", tx: " << tx_hash << ", tx: " << tx_hash
<< ", amount: " << print_money(amount)); << ", amount: " << print_money(amount));
if (m_listener) { // do not signal on received tx if wallet is not syncronized completely
if (m_listener && m_wallet->synchronized()) {
m_listener->moneyReceived(tx_hash, amount); m_listener->moneyReceived(tx_hash, amount);
m_listener->updated(); m_listener->updated();
} }
@ -107,7 +109,8 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
LOG_PRINT_L3(__FUNCTION__ << ": money spent. height: " << height LOG_PRINT_L3(__FUNCTION__ << ": money spent. height: " << height
<< ", tx: " << tx_hash << ", tx: " << tx_hash
<< ", amount: " << print_money(amount)); << ", amount: " << print_money(amount));
if (m_listener) { // do not signal on sent tx if wallet is not syncronized completely
if (m_listener && m_wallet->synchronized()) {
m_listener->moneySpent(tx_hash, amount); m_listener->moneySpent(tx_hash, amount);
m_listener->updated(); m_listener->updated();
} }
@ -119,6 +122,7 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
} }
WalletListener * m_listener; WalletListener * m_listener;
WalletImpl * m_wallet;
}; };
Wallet::~Wallet() {} Wallet::~Wallet() {}
@ -166,12 +170,16 @@ uint64_t Wallet::maximumAllowedAmount()
///////////////////////// WalletImpl implementation //////////////////////// ///////////////////////// WalletImpl implementation ////////////////////////
WalletImpl::WalletImpl(bool testnet) WalletImpl::WalletImpl(bool testnet)
:m_wallet(nullptr), m_status(Wallet::Status_Ok), m_trustedDaemon(false), :m_wallet(nullptr)
m_wallet2Callback(nullptr), m_recoveringFromSeed(false) , m_status(Wallet::Status_Ok)
, m_trustedDaemon(false)
, m_wallet2Callback(nullptr)
, m_recoveringFromSeed(false)
, m_synchronized(false)
{ {
m_wallet = new tools::wallet2(testnet); m_wallet = new tools::wallet2(testnet);
m_history = new TransactionHistoryImpl(this); m_history = new TransactionHistoryImpl(this);
m_wallet2Callback = new Wallet2CallbackImpl; m_wallet2Callback = new Wallet2CallbackImpl(this);
m_wallet->callback(m_wallet2Callback); m_wallet->callback(m_wallet2Callback);
m_refreshThreadDone = false; m_refreshThreadDone = false;
m_refreshEnabled = false; m_refreshEnabled = false;
@ -454,6 +462,11 @@ uint64_t WalletImpl::daemonBlockChainTargetHeight() const
return result; return result;
} }
bool WalletImpl::synchronized() const
{
return m_synchronized;
}
bool WalletImpl::refresh() bool WalletImpl::refresh()
{ {
clearStatus(); clearStatus();
@ -722,6 +735,9 @@ void WalletImpl::doRefresh()
boost::lock_guard<boost::mutex> guarg(m_refreshMutex2); boost::lock_guard<boost::mutex> guarg(m_refreshMutex2);
try { try {
m_wallet->refresh(); m_wallet->refresh();
if (!m_synchronized) {
m_synchronized = true;
}
// assuming if we have empty history, it wasn't initialized yet // assuming if we have empty history, it wasn't initialized yet
// for futher history changes client need to update history in // for futher history changes client need to update history in
// "on_money_received" and "on_money_sent" callbacks // "on_money_received" and "on_money_sent" callbacks

View file

@ -78,6 +78,7 @@ public:
uint64_t blockChainHeight() const; uint64_t blockChainHeight() const;
uint64_t daemonBlockChainHeight() const; uint64_t daemonBlockChainHeight() const;
uint64_t daemonBlockChainTargetHeight() const; uint64_t daemonBlockChainTargetHeight() const;
bool synchronized() const;
bool refresh(); bool refresh();
void refreshAsync(); void refreshAsync();
void setAutoRefreshInterval(int millis); void setAutoRefreshInterval(int millis);
@ -108,6 +109,7 @@ private:
private: private:
friend class PendingTransactionImpl; friend class PendingTransactionImpl;
friend class TransactionHistoryImpl; friend class TransactionHistoryImpl;
friend class Wallet2CallbackImpl;
tools::wallet2 * m_wallet; tools::wallet2 * m_wallet;
mutable std::atomic<int> m_status; mutable std::atomic<int> m_status;
@ -133,6 +135,7 @@ private:
// so it shouldn't be considered as new and pull blocks (slow-refresh) // so it shouldn't be considered as new and pull blocks (slow-refresh)
// instead of pulling hashes (fast-refresh) // instead of pulling hashes (fast-refresh)
bool m_recoveringFromSeed; bool m_recoveringFromSeed;
std::atomic<bool> m_synchronized;
}; };

View file

@ -255,6 +255,12 @@ struct Wallet
*/ */
virtual uint64_t daemonBlockChainTargetHeight() const = 0; virtual uint64_t daemonBlockChainTargetHeight() const = 0;
/**
* @brief synchronized - checks if wallet was ever synchronized
* @return
*/
virtual bool synchronized() const = 0;
static std::string displayAmount(uint64_t amount); static std::string displayAmount(uint64_t amount);
static uint64_t amountFromString(const std::string &amount); static uint64_t amountFromString(const std::string &amount);
static uint64_t amountFromDouble(double amount); static uint64_t amountFromDouble(double amount);