WalletAPI: rescanBlockchain, rescanBlockchainAsync

This commit is contained in:
mmitkevich 2018-10-16 14:58:22 +03:00
parent 1afc1d0d26
commit a7960542a9
3 changed files with 38 additions and 2 deletions

View file

@ -381,6 +381,7 @@ WalletImpl::WalletImpl(NetworkType nettype, uint64_t kdf_rounds)
, m_synchronized(false) , m_synchronized(false)
, m_rebuildWalletCache(false) , m_rebuildWalletCache(false)
, m_is_connected(false) , m_is_connected(false)
, m_refreshShouldRescan(false)
{ {
m_wallet.reset(new tools::wallet2(static_cast<cryptonote::network_type>(nettype), kdf_rounds, true)); m_wallet.reset(new tools::wallet2(static_cast<cryptonote::network_type>(nettype), kdf_rounds, true));
m_history.reset(new TransactionHistoryImpl(this)); m_history.reset(new TransactionHistoryImpl(this));
@ -1011,6 +1012,20 @@ void WalletImpl::refreshAsync()
m_refreshCV.notify_one(); m_refreshCV.notify_one();
} }
bool WalletImpl::rescanBlockchain()
{
clearStatus();
m_refreshShouldRescan = true;
doRefresh();
return status() == Status_Ok;
}
void WalletImpl::rescanBlockchainAsync()
{
m_refreshShouldRescan = true;
refreshAsync();
}
void WalletImpl::setAutoRefreshInterval(int millis) void WalletImpl::setAutoRefreshInterval(int millis)
{ {
if (millis > MAX_REFRESH_INTERVAL_MILLIS) { if (millis > MAX_REFRESH_INTERVAL_MILLIS) {
@ -1984,6 +1999,7 @@ void WalletImpl::refreshThreadFunc()
LOG_PRINT_L3(__FUNCTION__ << ": refresh lock acquired..."); LOG_PRINT_L3(__FUNCTION__ << ": refresh lock acquired...");
LOG_PRINT_L3(__FUNCTION__ << ": m_refreshEnabled: " << m_refreshEnabled); LOG_PRINT_L3(__FUNCTION__ << ": m_refreshEnabled: " << m_refreshEnabled);
LOG_PRINT_L3(__FUNCTION__ << ": m_status: " << status()); LOG_PRINT_L3(__FUNCTION__ << ": m_status: " << status());
LOG_PRINT_L3(__FUNCTION__ << ": m_refreshShouldRescan: " << m_refreshShouldRescan);
if (m_refreshEnabled) { if (m_refreshEnabled) {
LOG_PRINT_L3(__FUNCTION__ << ": refreshing..."); LOG_PRINT_L3(__FUNCTION__ << ": refreshing...");
doRefresh(); doRefresh();
@ -1994,12 +2010,16 @@ void WalletImpl::refreshThreadFunc()
void WalletImpl::doRefresh() void WalletImpl::doRefresh()
{ {
bool rescan = m_refreshShouldRescan.exchange(false);
// synchronizing async and sync refresh calls // synchronizing async and sync refresh calls
boost::lock_guard<boost::mutex> guarg(m_refreshMutex2); boost::lock_guard<boost::mutex> guarg(m_refreshMutex2);
try { do try {
LOG_PRINT_L3(__FUNCTION__ << ": doRefresh, rescan = "<<rescan);
// Syncing daemon and refreshing wallet simultaneously is very resource intensive. // Syncing daemon and refreshing wallet simultaneously is very resource intensive.
// Disable refresh if wallet is disconnected or daemon isn't synced. // Disable refresh if wallet is disconnected or daemon isn't synced.
if (m_wallet->light_wallet() || daemonSynced()) { if (m_wallet->light_wallet() || daemonSynced()) {
if(rescan)
m_wallet->rescan_blockchain(false);
m_wallet->refresh(trustedDaemon()); m_wallet->refresh(trustedDaemon());
if (!m_synchronized) { if (!m_synchronized) {
m_synchronized = true; m_synchronized = true;
@ -2016,7 +2036,9 @@ void WalletImpl::doRefresh()
} }
} catch (const std::exception &e) { } catch (const std::exception &e) {
setStatusError(e.what()); setStatusError(e.what());
} break;
}while(!rescan && (rescan=m_refreshShouldRescan.exchange(false))); // repeat if not rescanned and rescan was requested
if (m_wallet2Callback->getListener()) { if (m_wallet2Callback->getListener()) {
m_wallet2Callback->getListener()->refreshed(); m_wallet2Callback->getListener()->refreshed();
} }

View file

@ -114,6 +114,8 @@ public:
bool synchronized() const override; bool synchronized() const override;
bool refresh() override; bool refresh() override;
void refreshAsync() override; void refreshAsync() override;
bool rescanBlockchain() override;
void rescanBlockchainAsync() override;
void setAutoRefreshInterval(int millis) override; void setAutoRefreshInterval(int millis) override;
int autoRefreshInterval() const override; int autoRefreshInterval() const override;
void setRefreshFromBlockHeight(uint64_t refresh_from_block_height) override; void setRefreshFromBlockHeight(uint64_t refresh_from_block_height) override;
@ -232,6 +234,7 @@ private:
std::atomic<bool> m_refreshEnabled; std::atomic<bool> m_refreshEnabled;
std::atomic<bool> m_refreshThreadDone; std::atomic<bool> m_refreshThreadDone;
std::atomic<int> m_refreshIntervalMillis; std::atomic<int> m_refreshIntervalMillis;
std::atomic<bool> m_refreshShouldRescan;
// synchronizing refresh loop; // synchronizing refresh loop;
boost::mutex m_refreshMutex; boost::mutex m_refreshMutex;

View file

@ -643,6 +643,17 @@ struct Wallet
*/ */
virtual void refreshAsync() = 0; virtual void refreshAsync() = 0;
/**
* @brief rescanBlockchain - rescans the wallet, updating transactions from daemon
* @return - true if refreshed successfully;
*/
virtual bool rescanBlockchain() = 0;
/**
* @brief rescanBlockchainAsync - rescans wallet asynchronously, starting from genesys
*/
virtual void rescanBlockchainAsync() = 0;
/** /**
* @brief setAutoRefreshInterval - setup interval for automatic refresh. * @brief setAutoRefreshInterval - setup interval for automatic refresh.
* @param seconds - interval in millis. if zero or less than zero - automatic refresh disabled; * @param seconds - interval in millis. if zero or less than zero - automatic refresh disabled;