Merge pull request #1136
2dacb19
wallet2: wallet2::get_daemon_blockchain_height() clean error message on success (Ilya Kitaev)25e5efc
libwallet_api: Wallet::setAutoRefreshInterval sanity check (Ilya Kitaev)a668820
libwallet_api: explicitly return 0 in Wallet::daemonBlockChainHeight() on error (Ilya Kitaev)aef92f2
libwallet_api: tests: fixed WalletCallbackReceived test (Ilya Kitaev)15c0882
libwallet_api: tests: test fixed according implementation (Ilya Kitaev)a7882da
libwallet_api: tests: compilation errors fixed (Ilya Kitaev)cda4cb9
formatting: 2-spaces indentation (Ilya Kitaev)545a48f
formatting: 2-spaces indentation (Ilya Kitaev)3079c57
wallet2_api: milliseconds resolution for auto-refresh interval (Ilya Kitaev)7b4a85b
wallet2_api: added Wallet::daemonBlockChainHeight() (Ilya Kitaev)9de3ec3
libwallet_api: Wallet::blockChainHeight, WalletListener::newBlock (Ilya Kitaev)
This commit is contained in:
commit
f195a447cc
7 changed files with 196 additions and 24 deletions
|
@ -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)
|
||||
|
@ -170,7 +176,7 @@ WalletImpl::WalletImpl(bool testnet)
|
|||
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();
|
||||
|
@ -413,6 +419,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()
|
||||
{
|
||||
|
@ -428,14 +455,20 @@ void WalletImpl::refreshAsync()
|
|||
m_refreshCV.notify_one();
|
||||
}
|
||||
|
||||
void WalletImpl::setAutoRefreshInterval(int seconds)
|
||||
void WalletImpl::setAutoRefreshInterval(int millis)
|
||||
{
|
||||
m_refreshIntervalSeconds = seconds;
|
||||
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_refreshIntervalSeconds;
|
||||
return m_refreshIntervalMillis;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
|
@ -652,8 +685,8 @@ void WalletImpl::refreshThreadFunc()
|
|||
LOG_PRINT_L3(__FUNCTION__ << ": waiting for refresh...");
|
||||
// if auto refresh enabled, we wait for the "m_refreshIntervalSeconds" interval.
|
||||
// if not - we wait forever
|
||||
if (m_refreshIntervalSeconds > 0) {
|
||||
boost::posix_time::milliseconds wait_for_ms(m_refreshIntervalSeconds * 1000);
|
||||
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);
|
||||
|
|
|
@ -75,9 +75,11 @@ public:
|
|||
bool trustedDaemon() const;
|
||||
uint64_t balance() const;
|
||||
uint64_t unlockedBalance() const;
|
||||
uint64_t blockChainHeight() const;
|
||||
uint64_t daemonBlockChainHeight() const;
|
||||
bool refresh();
|
||||
void refreshAsync();
|
||||
void setAutoRefreshInterval(int seconds);
|
||||
void setAutoRefreshInterval(int millis);
|
||||
int autoRefreshInterval() const;
|
||||
|
||||
|
||||
|
@ -105,8 +107,8 @@ private:
|
|||
friend class TransactionHistoryImpl;
|
||||
|
||||
tools::wallet2 * m_wallet;
|
||||
std::atomic<int> m_status;
|
||||
std::string m_errorString;
|
||||
mutable std::atomic<int> m_status;
|
||||
mutable std::string m_errorString;
|
||||
std::string m_password;
|
||||
TransactionHistoryImpl * m_history;
|
||||
bool m_trustedDaemon;
|
||||
|
@ -116,7 +118,7 @@ private:
|
|||
// multi-threaded refresh stuff
|
||||
std::atomic<bool> m_refreshEnabled;
|
||||
std::atomic<bool> m_refreshThreadDone;
|
||||
std::atomic<int> m_refreshIntervalSeconds;
|
||||
std::atomic<int> m_refreshIntervalMillis;
|
||||
// synchronizing refresh loop;
|
||||
boost::mutex m_refreshMutex;
|
||||
|
||||
|
|
|
@ -4053,6 +4053,38 @@ std::string wallet2::get_daemon_address() const
|
|||
return m_daemon_address;
|
||||
}
|
||||
|
||||
uint64_t wallet2::get_daemon_blockchain_height(string &err)
|
||||
{
|
||||
// XXX: DRY violation. copy-pasted from simplewallet.cpp:get_daemon_blockchain_height()
|
||||
// consider to move it from simplewallet to wallet2 ?
|
||||
COMMAND_RPC_GET_HEIGHT::request req;
|
||||
COMMAND_RPC_GET_HEIGHT::response res = boost::value_initialized<COMMAND_RPC_GET_HEIGHT::response>();
|
||||
m_daemon_rpc_mutex.lock();
|
||||
bool ok = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/getheight", req, res, m_http_client);
|
||||
m_daemon_rpc_mutex.unlock();
|
||||
// XXX: DRY violation. copy-pasted from simplewallet.cpp:interpret_rpc_response()
|
||||
if (ok)
|
||||
{
|
||||
if (res.status == CORE_RPC_STATUS_BUSY)
|
||||
{
|
||||
err = "daemon is busy. Please try again later.";
|
||||
}
|
||||
else if (res.status != CORE_RPC_STATUS_OK)
|
||||
{
|
||||
err = res.status;
|
||||
}
|
||||
else // success, cleaning up error message
|
||||
{
|
||||
err = "";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
err = "possibly lost connection to daemon";
|
||||
}
|
||||
return res.height;
|
||||
}
|
||||
|
||||
void wallet2::set_tx_note(const crypto::hash &txid, const std::string ¬e)
|
||||
{
|
||||
m_tx_notes[txid] = note;
|
||||
|
|
|
@ -400,6 +400,7 @@ namespace tools
|
|||
std::string get_wallet_file() const;
|
||||
std::string get_keys_file() const;
|
||||
std::string get_daemon_address() const;
|
||||
uint64_t get_daemon_blockchain_height(std::string& err);
|
||||
|
||||
std::vector<size_t> select_available_outputs_from_histogram(uint64_t count, bool atleast, bool unlocked, bool trusted_daemon);
|
||||
std::vector<size_t> select_available_outputs(const std::function<bool(const transfer_details &td)> &f);
|
||||
|
|
|
@ -114,11 +114,35 @@ struct TransactionHistory
|
|||
struct WalletListener
|
||||
{
|
||||
virtual ~WalletListener() = 0;
|
||||
/**
|
||||
* @brief moneySpent - called when money spent
|
||||
* @param txId - transaction id
|
||||
* @param amount - amount
|
||||
*/
|
||||
virtual void moneySpent(const std::string &txId, uint64_t amount) = 0;
|
||||
|
||||
/**
|
||||
* @brief moneyReceived - called when money received
|
||||
* @param txId - transaction id
|
||||
* @param amount - amount
|
||||
*/
|
||||
virtual void moneyReceived(const std::string &txId, uint64_t amount) = 0;
|
||||
// generic callback, called when any event (sent/received/block reveived/etc) happened with the wallet;
|
||||
|
||||
/**
|
||||
* @brief newBlock - called when new block received
|
||||
* @param height - block height
|
||||
*/
|
||||
virtual void newBlock(uint64_t height) = 0;
|
||||
|
||||
/**
|
||||
* @brief updated - 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 be calling "refresh" synchronously
|
||||
|
||||
|
||||
/**
|
||||
* @brief refreshed - called when wallet refreshed by background thread or explicitly refreshed by calling "refresh" synchronously
|
||||
*/
|
||||
virtual void refreshed() = 0;
|
||||
};
|
||||
|
||||
|
@ -211,6 +235,20 @@ struct Wallet
|
|||
virtual uint64_t balance() const = 0;
|
||||
virtual uint64_t unlockedBalance() const = 0;
|
||||
|
||||
/**
|
||||
* @brief blockChainHeight - returns current blockchain height
|
||||
* @return
|
||||
*/
|
||||
virtual uint64_t blockChainHeight() const = 0;
|
||||
|
||||
/**
|
||||
* @brief daemonBlockChainHeight - returns daemon blockchain height
|
||||
* @return 0 - in case error communicating with the daemon.
|
||||
* status() will return Status_Error and errorString() will return verbose error description
|
||||
*/
|
||||
virtual uint64_t daemonBlockChainHeight() const = 0;
|
||||
|
||||
|
||||
static std::string displayAmount(uint64_t amount);
|
||||
static uint64_t amountFromString(const std::string &amount);
|
||||
static uint64_t amountFromDouble(double amount);
|
||||
|
@ -231,12 +269,12 @@ struct Wallet
|
|||
|
||||
/**
|
||||
* @brief setAutoRefreshInterval - setup interval for automatic refresh.
|
||||
* @param seconds - interval in seconds. if zero or less than zero - automatic refresh disabled;
|
||||
* @param seconds - interval in millis. if zero or less than zero - automatic refresh disabled;
|
||||
*/
|
||||
virtual void setAutoRefreshInterval(int seconds) = 0;
|
||||
virtual void setAutoRefreshInterval(int millis) = 0;
|
||||
|
||||
/**
|
||||
* @brief autoRefreshInterval - returns automatic refresh interval in seconds
|
||||
* @brief autoRefreshInterval - returns automatic refresh interval in millis
|
||||
* @return
|
||||
*/
|
||||
virtual int autoRefreshInterval() const = 0;
|
||||
|
|
|
@ -473,8 +473,6 @@ TEST_F(WalletTest1, WalletGeneratesIntegratedAddress)
|
|||
|
||||
TEST_F(WalletTest1, WalletShowsBalance)
|
||||
{
|
||||
// TODO: temporary disabled;
|
||||
return;
|
||||
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
|
||||
ASSERT_TRUE(wallet1->balance() > 0);
|
||||
ASSERT_TRUE(wallet1->unlockedBalance() > 0);
|
||||
|
@ -491,6 +489,32 @@ TEST_F(WalletTest1, WalletShowsBalance)
|
|||
ASSERT_TRUE(wmgr->closeWallet(wallet2));
|
||||
}
|
||||
|
||||
TEST_F(WalletTest1, WalletReturnsCurrentBlockHeight)
|
||||
{
|
||||
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
|
||||
ASSERT_TRUE(wallet1->blockChainHeight() > 0);
|
||||
wmgr->closeWallet(wallet1);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(WalletTest1, WalletReturnsDaemonBlockHeight)
|
||||
{
|
||||
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
|
||||
// wallet not connected to daemon
|
||||
ASSERT_TRUE(wallet1->daemonBlockChainHeight() == 0);
|
||||
ASSERT_TRUE(wallet1->status() != Bitmonero::Wallet::Status_Ok);
|
||||
ASSERT_FALSE(wallet1->errorString().empty());
|
||||
wmgr->closeWallet(wallet1);
|
||||
|
||||
wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
|
||||
// wallet connected to daemon
|
||||
wallet1->init(TESTNET_DAEMON_ADDRESS, 0);
|
||||
ASSERT_TRUE(wallet1->daemonBlockChainHeight() > 0);
|
||||
std::cout << "daemonBlockChainHeight: " << wallet1->daemonBlockChainHeight() << std::endl;
|
||||
wmgr->closeWallet(wallet1);
|
||||
}
|
||||
|
||||
|
||||
TEST_F(WalletTest1, WalletRefresh)
|
||||
{
|
||||
|
||||
|
@ -742,8 +766,10 @@ struct MyWalletListener : public Bitmonero::WalletListener
|
|||
std::condition_variable cv_receive;
|
||||
std::condition_variable cv_update;
|
||||
std::condition_variable cv_refresh;
|
||||
std::condition_variable cv_newblock;
|
||||
bool send_triggered;
|
||||
bool receive_triggered;
|
||||
bool newblock_triggered;
|
||||
bool update_triggered;
|
||||
bool refresh_triggered;
|
||||
|
||||
|
@ -781,6 +807,14 @@ struct MyWalletListener : public Bitmonero::WalletListener
|
|||
cv_receive.notify_one();
|
||||
}
|
||||
|
||||
virtual void newBlock(uint64_t height)
|
||||
{
|
||||
std::cout << "wallet: " << wallet->address()
|
||||
<<", new block received, blockHeight: " << height << std::endl;
|
||||
newblock_triggered = true;
|
||||
cv_newblock.notify_one();
|
||||
}
|
||||
|
||||
virtual void updated()
|
||||
{
|
||||
std::cout << __FUNCTION__ << "Wallet updated";
|
||||
|
@ -879,16 +913,17 @@ TEST_F(WalletTest2, WalletCallbackSent)
|
|||
TEST_F(WalletTest2, WalletCallbackReceived)
|
||||
{
|
||||
|
||||
Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET5_NAME, TESTNET_WALLET_PASS, true);
|
||||
Bitmonero::Wallet * wallet_src = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
|
||||
// make sure testnet daemon is running
|
||||
ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0));
|
||||
ASSERT_TRUE(wallet_src->refresh());
|
||||
std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl;
|
||||
std::cout << "** Balance src1: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl;
|
||||
|
||||
Bitmonero::Wallet * wallet_dst = wmgr->openWallet(CURRENT_DST_WALLET, TESTNET_WALLET_PASS, true);
|
||||
ASSERT_TRUE(wallet_dst->init(TESTNET_DAEMON_ADDRESS, 0));
|
||||
ASSERT_TRUE(wallet_dst->refresh());
|
||||
uint64_t balance = wallet_dst->balance();
|
||||
std::cout << "** Balance dst1: " << wallet_dst->displayAmount(wallet_dst->balance()) << std::endl;
|
||||
MyWalletListener * wallet_dst_listener = new MyWalletListener(wallet_dst);
|
||||
|
||||
uint64_t amount = AMOUNT_1XMR * 5;
|
||||
|
@ -910,7 +945,10 @@ TEST_F(WalletTest2, WalletCallbackReceived)
|
|||
std::cerr << "TEST: receive lock acquired...\n";
|
||||
ASSERT_TRUE(wallet_dst_listener->receive_triggered);
|
||||
ASSERT_TRUE(wallet_dst_listener->update_triggered);
|
||||
std::cout << "** Balance: " << wallet_dst->displayAmount(wallet_src->balance()) << std::endl;
|
||||
|
||||
std::cout << "** Balance src2: " << wallet_dst->displayAmount(wallet_src->balance()) << std::endl;
|
||||
std::cout << "** Balance dst2: " << wallet_dst->displayAmount(wallet_dst->balance()) << std::endl;
|
||||
|
||||
ASSERT_TRUE(wallet_dst->balance() > balance);
|
||||
|
||||
wmgr->closeWallet(wallet_src);
|
||||
|
@ -918,6 +956,36 @@ TEST_F(WalletTest2, WalletCallbackReceived)
|
|||
}
|
||||
|
||||
|
||||
|
||||
TEST_F(WalletTest2, WalletCallbackNewBlock)
|
||||
{
|
||||
|
||||
Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET5_NAME, TESTNET_WALLET_PASS, true);
|
||||
// make sure testnet daemon is running
|
||||
ASSERT_TRUE(wallet_src->init(TESTNET_DAEMON_ADDRESS, 0));
|
||||
ASSERT_TRUE(wallet_src->refresh());
|
||||
uint64_t bc1 = wallet_src->blockChainHeight();
|
||||
std::cout << "** Block height: " << bc1 << std::endl;
|
||||
|
||||
|
||||
MyWalletListener * wallet_listener = new MyWalletListener(wallet_src);
|
||||
|
||||
// wait max 4 min for new block
|
||||
std::chrono::seconds wait_for = std::chrono::seconds(60*4);
|
||||
std::unique_lock<std::mutex> lock (wallet_listener->mutex);
|
||||
std::cerr << "TEST: waiting on newblock lock...\n";
|
||||
wallet_listener->cv_newblock.wait_for(lock, wait_for);
|
||||
std::cerr << "TEST: newblock lock acquired...\n";
|
||||
ASSERT_TRUE(wallet_listener->newblock_triggered);
|
||||
uint64_t bc2 = wallet_src->blockChainHeight();
|
||||
std::cout << "** Block height: " << bc2 << std::endl;
|
||||
ASSERT_TRUE(bc2 > bc1);
|
||||
wmgr->closeWallet(wallet_src);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// we can override default values for "TESTNET_DAEMON_ADDRESS" and "WALLETS_ROOT_DIR"
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
|
||||
|
||||
function send_funds {
|
||||
local amount=$1
|
||||
local dest=$(cat "$2.address.txt")
|
||||
|
|
Loading…
Reference in a new issue