From 6d32a3d16b8bb1bf2a51a41ce9f826cf31d02352 Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Thu, 14 Jul 2016 12:47:01 +0300 Subject: wallet_api: async init, Wallet::connected status, log level --- src/wallet/api/wallet.cpp | 52 ++++++++++++++++++++++++++++----------- src/wallet/api/wallet.h | 2 ++ src/wallet/api/wallet_manager.cpp | 5 ++++ src/wallet/wallet2_api.h | 50 ++++++++++++++++++++++++++++++++++--- 4 files changed, 91 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index e75f5afdb..2322cac76 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -76,7 +76,7 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback virtual void on_new_block(uint64_t height, const cryptonote::block& block) { // TODO; - LOG_PRINT_L0(__FUNCTION__ << ": new block. height: " << height); + LOG_PRINT_L3(__FUNCTION__ << ": new block. height: " << height); } virtual void on_money_received(uint64_t height, const cryptonote::transaction& tx, size_t out_index) @@ -85,7 +85,7 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback std::string tx_hash = epee::string_tools::pod_to_hex(get_transaction_hash(tx)); uint64_t amount = tx.vout[out_index].amount; - LOG_PRINT_L0(__FUNCTION__ << ": money received. height: " << height + LOG_PRINT_L3(__FUNCTION__ << ": money received. height: " << height << ", tx: " << tx_hash << ", amount: " << print_money(amount)); if (m_listener) { @@ -100,7 +100,7 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback // TODO; std::string tx_hash = epee::string_tools::pod_to_hex(get_transaction_hash(spend_tx)); uint64_t amount = in_tx.vout[out_index].amount; - LOG_PRINT_L0(__FUNCTION__ << ": money spent. height: " << height + LOG_PRINT_L3(__FUNCTION__ << ": money spent. height: " << height << ", tx: " << tx_hash << ", amount: " << print_money(amount)); if (m_listener) { @@ -121,6 +121,7 @@ Wallet::~Wallet() {} WalletListener::~WalletListener() {} + string Wallet::displayAmount(uint64_t amount) { return cryptonote::print_money(amount); @@ -269,8 +270,12 @@ bool WalletImpl::close() clearStatus(); bool result = false; try { + // LOG_PRINT_L0("Calling wallet::store..."); m_wallet->store(); + // LOG_PRINT_L0("wallet::store done"); + // LOG_PRINT_L0("Calling wallet::stop..."); m_wallet->stop(); + // LOG_PRINT_L0("wallet::stop done"); result = true; } catch (const std::exception &e) { m_status = Status_Error; @@ -366,21 +371,30 @@ string WalletImpl::keysFilename() const bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transaction_size_limit) { clearStatus(); - try { - m_wallet->init(daemon_address, upper_transaction_size_limit); - if (Utils::isAddressLocal(daemon_address)) { - this->setTrustedDaemon(true); - } - refresh(); - } catch (const std::exception &e) { - LOG_ERROR("Error initializing wallet: " << e.what()); - m_status = Status_Error; - m_errorString = e.what(); + + m_wallet->init(daemon_address, upper_transaction_size_limit); + if (Utils::isAddressLocal(daemon_address)) { + this->setTrustedDaemon(true); } + bool result = this->refresh(); + // enabling background refresh thread + startRefresh(); + return result; - return m_status == Status_Ok; } +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); + } + startRefresh(); +} + + + uint64_t WalletImpl::balance() const { return m_wallet->balance(); @@ -594,6 +608,11 @@ bool WalletImpl::connectToDaemon() return result; } +bool WalletImpl::connected() const +{ + return m_wallet->check_connection(); +} + void WalletImpl::setTrustedDaemon(bool arg) { m_trustedDaemon = arg; @@ -619,8 +638,13 @@ void WalletImpl::refreshThreadFunc() if (m_refreshThreadDone) { break; } + LOG_PRINT_L3(__FUNCTION__ << ": waiting for refresh..."); m_refreshCV.wait_for(lock, std::chrono::seconds(m_refreshIntervalSeconds)); + LOG_PRINT_L3(__FUNCTION__ << ": refresh lock acquired..."); + LOG_PRINT_L3(__FUNCTION__ << ": m_refreshEnabled: " << m_refreshEnabled); + LOG_PRINT_L3(__FUNCTION__ << ": m_status: " << m_status); if (m_refreshEnabled && m_status == Status_Ok) { + LOG_PRINT_L3(__FUNCTION__ << ": refreshing..."); doRefresh(); } } diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index e9da05347..68ca364cf 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -68,7 +68,9 @@ public: std::string filename() const; std::string keysFilename() const; bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit); + void initAsync(const std::string &daemon_address, uint64_t upper_transaction_size_limit); bool connectToDaemon(); + bool connected() const; void setTrustedDaemon(bool arg); bool trustedDaemon() const; uint64_t balance() const; diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index bf072ccca..ca83806ff 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -137,6 +137,11 @@ WalletManager *WalletManagerFactory::getWalletManager() return g_walletManager; } +void WalletManagerFactory::setLogLevel(int level) +{ + epee::log_space::log_singletone::get_set_log_detalisation_level(true, level); +} + } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index f6c573673..2c5836573 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -116,11 +116,10 @@ struct WalletListener virtual ~WalletListener() = 0; virtual void moneySpent(const std::string &txId, uint64_t amount) = 0; virtual void moneyReceived(const std::string &txId, uint64_t amount) = 0; - // generic callback, called when any event happened with the wallet; + // generic callback, called when any event (sent/received/block reveived/etc) happened with the wallet; virtual void updated() = 0; - // called when wallet refreshed by background thread or explicitly + // called when wallet refreshed by background thread or explicitly called be calling "refresh" synchronously virtual void refreshed() = 0; - }; @@ -175,9 +174,38 @@ struct Wallet * \return */ virtual std::string keysFilename() const = 0; - + /*! + * \brief init - initializes wallet with daemon connection params. implicitly connects to the daemon + * and refreshes the wallet. "refreshed" callback will be invoked. if daemon_address is + * local address, "trusted daemon" will be set to true forcibly + * + * \param daemon_address - daemon address in "hostname:port" format + * \param upper_transaction_size_limit + * \return - true if initialized and refreshed successfully + */ virtual bool init(const std::string &daemon_address, uint64_t upper_transaction_size_limit) = 0; + + /*! + * \brief init - initalizes wallet asynchronously. logic is the same as "init" but returns immediately. + * "refreshed" callback will be invoked. + * + * \param daemon_address - daemon address in "hostname:port" format + * \param upper_transaction_size_limit + * \return - true if initialized and refreshed successfully + */ + virtual void initAsync(const std::string &daemon_address, uint64_t upper_transaction_size_limit) = 0; + + /** + * @brief connectToDaemon - connects to the daemon. TODO: check if it can be removed + * @return + */ virtual bool connectToDaemon() = 0; + + /** + * @brief connected - checks if the wallet connected to the daemon + * @return - true if connected + */ + virtual bool connected() const = 0; virtual void setTrustedDaemon(bool arg) = 0; virtual bool trustedDaemon() const = 0; virtual uint64_t balance() const = 0; @@ -296,8 +324,22 @@ struct WalletManager struct WalletManagerFactory { + // logging levels for underlying library + enum LogLevel { + LogLevel_Silent = -1, + LogLevel_0 = 0, + LogLevel_1 = 1, + LogLevel_2 = 2, + LogLevel_3 = 3, + LogLevel_4 = 4, + LogLevel_Min = LogLevel_Silent, + LogLevel_Max = LogLevel_4 + }; + static WalletManager * getWalletManager(); + static void setLogLevel(int level); }; + } -- cgit v1.2.3