libwallet_api: Wallet::blockChainHeight, WalletListener::newBlock

This commit is contained in:
Ilya Kitaev 2016-09-23 22:36:37 +03:00
parent e01a9ea507
commit 9de3ec3e2a
4 changed files with 91 additions and 5 deletions

View File

@ -75,8 +75,12 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
virtual void on_new_block(uint64_t height, const cryptonote::block& block) virtual void on_new_block(uint64_t height, const cryptonote::block& block)
{ {
// TODO;
LOG_PRINT_L3(__FUNCTION__ << ": new block. height: " << height); 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) virtual void on_money_received(uint64_t height, const cryptonote::transaction& tx, uint64_t amount)
@ -413,6 +417,10 @@ uint64_t WalletImpl::unlockedBalance() const
return m_wallet->unlocked_balance(); return m_wallet->unlocked_balance();
} }
uint64_t WalletImpl::blockChainHeight() const
{
return m_wallet->get_blockchain_current_height();
}
bool WalletImpl::refresh() bool WalletImpl::refresh()
{ {

View File

@ -75,6 +75,7 @@ public:
bool trustedDaemon() const; bool trustedDaemon() const;
uint64_t balance() const; uint64_t balance() const;
uint64_t unlockedBalance() const; uint64_t unlockedBalance() const;
uint64_t blockChainHeight() const;
bool refresh(); bool refresh();
void refreshAsync(); void refreshAsync();
void setAutoRefreshInterval(int seconds); void setAutoRefreshInterval(int seconds);

View File

@ -114,11 +114,35 @@ struct TransactionHistory
struct WalletListener struct WalletListener
{ {
virtual ~WalletListener() = 0; 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; 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; 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; 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; virtual void refreshed() = 0;
}; };
@ -211,6 +235,12 @@ struct Wallet
virtual uint64_t balance() const = 0; virtual uint64_t balance() const = 0;
virtual uint64_t unlockedBalance() const = 0; virtual uint64_t unlockedBalance() const = 0;
/**
* @brief getBlockChainHeight - returns current blockchain height
* @return
*/
virtual uint64_t blockChainHeight() 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);

View File

@ -473,8 +473,6 @@ TEST_F(WalletTest1, WalletGeneratesIntegratedAddress)
TEST_F(WalletTest1, WalletShowsBalance) TEST_F(WalletTest1, WalletShowsBalance)
{ {
// TODO: temporary disabled;
return;
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true); Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
ASSERT_TRUE(wallet1->balance() > 0); ASSERT_TRUE(wallet1->balance() > 0);
ASSERT_TRUE(wallet1->unlockedBalance() > 0); ASSERT_TRUE(wallet1->unlockedBalance() > 0);
@ -491,6 +489,14 @@ TEST_F(WalletTest1, WalletShowsBalance)
ASSERT_TRUE(wmgr->closeWallet(wallet2)); ASSERT_TRUE(wmgr->closeWallet(wallet2));
} }
TEST_F(WalletTest1, WalletReturnsBlockHeight)
{
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
ASSERT_TRUE(wallet1->blockChainHeight() > 0);
wmgr->closeWallet(wallet1);
}
TEST_F(WalletTest1, WalletRefresh) TEST_F(WalletTest1, WalletRefresh)
{ {
@ -742,8 +748,10 @@ struct MyWalletListener : public Bitmonero::WalletListener
std::condition_variable cv_receive; std::condition_variable cv_receive;
std::condition_variable cv_update; std::condition_variable cv_update;
std::condition_variable cv_refresh; std::condition_variable cv_refresh;
std::condition_variable cv_newblock;
bool send_triggered; bool send_triggered;
bool receive_triggered; bool receive_triggered;
bool newblock_triggered;
bool update_triggered; bool update_triggered;
bool refresh_triggered; bool refresh_triggered;
@ -781,6 +789,14 @@ struct MyWalletListener : public Bitmonero::WalletListener
cv_receive.notify_one(); 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() virtual void updated()
{ {
std::cout << __FUNCTION__ << "Wallet updated"; std::cout << __FUNCTION__ << "Wallet updated";
@ -918,6 +934,37 @@ 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);
ASSERT_TRUE(wallet_listener->update_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) int main(int argc, char** argv)
{ {
// we can override default values for "TESTNET_DAEMON_ADDRESS" and "WALLETS_ROOT_DIR" // we can override default values for "TESTNET_DAEMON_ADDRESS" and "WALLETS_ROOT_DIR"