aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/api/wallet.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/wallet/api/wallet.cpp')
-rw-r--r--src/wallet/api/wallet.cpp111
1 files changed, 93 insertions, 18 deletions
diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp
index 74552bc03..16321a5a2 100644
--- a/src/wallet/api/wallet.cpp
+++ b/src/wallet/api/wallet.cpp
@@ -46,7 +46,9 @@ namespace Bitmonero {
namespace {
// copy-pasted from simplewallet
static const size_t DEFAULT_MIXIN = 4;
- static const int DEFAULT_REFRESH_INTERVAL_SECONDS = 10;
+ static const int DEFAULT_REFRESH_INTERVAL_MILLIS = 1000 * 10;
+ // limit maximum refresh interval as one minute
+ static const int MAX_REFRESH_INTERVAL_MILLIS = 1000 * 60 * 1;
}
struct Wallet2CallbackImpl : public tools::i_wallet2_callback
@@ -75,8 +77,12 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
virtual void on_new_block(uint64_t height, const cryptonote::block& block)
{
- // TODO;
LOG_PRINT_L3(__FUNCTION__ << ": new block. height: " << height);
+
+ if (m_listener) {
+ m_listener->newBlock(height);
+ // m_listener->updated();
+ }
}
virtual void on_money_received(uint64_t height, const cryptonote::transaction& tx, uint64_t amount)
@@ -161,7 +167,7 @@ uint64_t Wallet::maximumAllowedAmount()
///////////////////////// WalletImpl implementation ////////////////////////
WalletImpl::WalletImpl(bool testnet)
:m_wallet(nullptr), m_status(Wallet::Status_Ok), m_trustedDaemon(false),
- m_wallet2Callback(nullptr)
+ m_wallet2Callback(nullptr), m_recoveringFromSeed(false)
{
m_wallet = new tools::wallet2(testnet);
m_history = new TransactionHistoryImpl(this);
@@ -169,7 +175,10 @@ WalletImpl::WalletImpl(bool testnet)
m_wallet->callback(m_wallet2Callback);
m_refreshThreadDone = false;
m_refreshEnabled = false;
- m_refreshIntervalSeconds = DEFAULT_REFRESH_INTERVAL_SECONDS;
+
+
+ m_refreshIntervalMillis = DEFAULT_REFRESH_INTERVAL_MILLIS;
+
m_refreshThread = boost::thread([this] () {
this->refreshThreadFunc();
});
@@ -188,7 +197,7 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co
{
clearStatus();
-
+ m_recoveringFromSeed = false;
bool keys_file_exists;
bool wallet_file_exists;
tools::wallet2::wallet_exists(path, keys_file_exists, wallet_file_exists);
@@ -225,6 +234,7 @@ bool WalletImpl::create(const std::string &path, const std::string &password, co
bool WalletImpl::open(const std::string &path, const std::string &password)
{
clearStatus();
+ m_recoveringFromSeed = false;
try {
// TODO: handle "deprecated"
m_wallet->load(path, password);
@@ -249,6 +259,7 @@ bool WalletImpl::recover(const std::string &path, const std::string &seed)
return false;
}
+ m_recoveringFromSeed = true;
crypto::secret_key recovery_key;
std::string old_language;
if (!crypto::ElectrumWords::words_to_bytes(seed, recovery_key, old_language)) {
@@ -261,6 +272,7 @@ bool WalletImpl::recover(const std::string &path, const std::string &seed)
m_wallet->set_seed_language(old_language);
m_wallet->generate(path, "", recovery_key, true, false);
// TODO: wallet->init(daemon_address);
+
} catch (const std::exception &e) {
m_status = Status_Error;
m_errorString = e.what();
@@ -272,14 +284,15 @@ bool WalletImpl::close()
{
bool result = false;
+ LOG_PRINT_L3("closing wallet...");
try {
// do not store wallet with invalid status
if (status() == Status_Ok)
m_wallet->store();
- // LOG_PRINT_L0("wallet::store done");
- // LOG_PRINT_L0("Calling wallet::stop...");
+ LOG_PRINT_L3("wallet::store done");
+ LOG_PRINT_L3("Calling wallet::stop...");
m_wallet->stop();
- // LOG_PRINT_L0("wallet::stop done");
+ LOG_PRINT_L3("wallet::stop done");
result = true;
clearStatus();
} catch (const std::exception &e) {
@@ -376,11 +389,7 @@ string WalletImpl::keysFilename() const
bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit)
{
clearStatus();
-
- m_wallet->init(daemon_address, upper_transaction_size_limit);
- if (Utils::isAddressLocal(daemon_address)) {
- this->setTrustedDaemon(true);
- }
+ doInit(daemon_address, upper_transaction_size_limit);
bool result = this->refresh();
// enabling background refresh thread
startRefresh();
@@ -391,10 +400,7 @@ bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transact
void WalletImpl::initAsync(const string &daemon_address, uint64_t upper_transaction_size_limit)
{
clearStatus();
- m_wallet->init(daemon_address, upper_transaction_size_limit);
- if (Utils::isAddressLocal(daemon_address)) {
- this->setTrustedDaemon(true);
- }
+ doInit(daemon_address, upper_transaction_size_limit);
startRefresh();
}
@@ -410,6 +416,27 @@ uint64_t WalletImpl::unlockedBalance() const
return m_wallet->unlocked_balance();
}
+uint64_t WalletImpl::blockChainHeight() const
+{
+ return m_wallet->get_blockchain_current_height();
+}
+
+uint64_t WalletImpl::daemonBlockChainHeight() const
+{
+ std::string err;
+ uint64_t result = m_wallet->get_daemon_blockchain_height(err);
+ if (!err.empty()) {
+ LOG_ERROR(__FUNCTION__ << ": " << err);
+ result = 0;
+ m_errorString = err;
+ m_status = Status_Error;
+
+ } else {
+ m_status = Status_Ok;
+ m_errorString = "";
+ }
+ return result;
+}
bool WalletImpl::refresh()
{
@@ -425,6 +452,22 @@ void WalletImpl::refreshAsync()
m_refreshCV.notify_one();
}
+void WalletImpl::setAutoRefreshInterval(int millis)
+{
+ if (millis > MAX_REFRESH_INTERVAL_MILLIS) {
+ LOG_ERROR(__FUNCTION__<< ": invalid refresh interval " << millis
+ << " ms, maximum allowed is " << MAX_REFRESH_INTERVAL_MILLIS << " ms");
+ m_refreshIntervalMillis = MAX_REFRESH_INTERVAL_MILLIS;
+ } else {
+ m_refreshIntervalMillis = millis;
+ }
+}
+
+int WalletImpl::autoRefreshInterval() const
+{
+ return m_refreshIntervalMillis;
+}
+
// TODO:
// 1 - properly handle payment id (add another menthod with explicit 'payment_id' param)
// 2 - check / design how "Transaction" can be single interface
@@ -637,7 +680,15 @@ void WalletImpl::refreshThreadFunc()
break;
}
LOG_PRINT_L3(__FUNCTION__ << ": waiting for refresh...");
- m_refreshCV.wait(lock);
+ // if auto refresh enabled, we wait for the "m_refreshIntervalSeconds" interval.
+ // if not - we wait forever
+ if (m_refreshIntervalMillis > 0) {
+ boost::posix_time::milliseconds wait_for_ms(m_refreshIntervalMillis);
+ m_refreshCV.timed_wait(lock, wait_for_ms);
+ } else {
+ m_refreshCV.wait(lock);
+ }
+
LOG_PRINT_L3(__FUNCTION__ << ": refresh lock acquired...");
LOG_PRINT_L3(__FUNCTION__ << ": m_refreshEnabled: " << m_refreshEnabled);
LOG_PRINT_L3(__FUNCTION__ << ": m_status: " << m_status);
@@ -680,6 +731,7 @@ void WalletImpl::stopRefresh()
if (!m_refreshThreadDone) {
m_refreshEnabled = false;
m_refreshThreadDone = true;
+ m_refreshCV.notify_one();
m_refreshThread.join();
}
}
@@ -693,4 +745,27 @@ void WalletImpl::pauseRefresh()
}
+bool WalletImpl::isNewWallet() const
+{
+ // in case wallet created without daemon connection, closed and opened again,
+ // it's the same case as if it created from scratch, i.e. we need "fast sync"
+ // with the daemon (pull hashes instead of pull blocks)
+ return !(blockChainHeight() > 1 || m_recoveringFromSeed);
+}
+
+void WalletImpl::doInit(const string &daemon_address, uint64_t upper_transaction_size_limit)
+{
+ m_wallet->init(daemon_address, upper_transaction_size_limit);
+
+ // in case new wallet, this will force fast-refresh (pulling hashes instead of blocks)
+ if (isNewWallet()) {
+ m_wallet->set_refresh_from_block_height(daemonBlockChainHeight());
+ }
+
+ if (Utils::isAddressLocal(daemon_address)) {
+ this->setTrustedDaemon(true);
+ }
+
+}
+
} // namespace