WalletListener functionality

This commit is contained in:
Ilya Kitaev 2016-07-10 17:17:23 +03:00
parent d27b883b2d
commit 9d2cb4f36c
5 changed files with 196 additions and 53 deletions

View file

@ -39,6 +39,8 @@
#include <vector>
#include <mutex>
#include <thread>
#include <atomic>
#include <condition_variable>
using namespace std;
@ -71,7 +73,7 @@ const std::string TESTNET_WALLET6_NAME = WALLETS_ROOT_DIR + "wallet_06.bin";
const char * TESTNET_WALLET_PASS = "";
const std::string CURRENT_SRC_WALLET = TESTNET_WALLET1_NAME;
const std::string CURRENT_SRC_WALLET = TESTNET_WALLET3_NAME;
const std::string CURRENT_DST_WALLET = TESTNET_WALLET6_NAME;
const char * TESTNET_DAEMON_ADDRESS = "localhost:38081";
@ -176,6 +178,7 @@ struct WalletTest2 : public testing::Test
};
/*
TEST_F(WalletManagerTest, WalletManagerCreatesWallet)
{
@ -235,7 +238,7 @@ TEST_F(WalletManagerTest, WalletManagerMovesWallet)
}
/*
TEST_F(WalletManagerTest, WalletManagerChangesPassword)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
@ -345,6 +348,8 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet4)
}
*/
/*
TEST_F(WalletManagerTest, WalletManagerFindsWallet)
{
std::vector<std::string> wallets = wmgr->findWallets(WALLETS_ROOT_DIR);
@ -415,7 +420,7 @@ TEST_F(WalletTest1, WalletConvertsToString)
}
/*
TEST_F(WalletTest1, WalletTransaction)
{
Bitmonero::Wallet * wallet1 = wmgr->openWallet(CURRENT_SRC_WALLET, TESTNET_WALLET_PASS, true);
@ -429,8 +434,10 @@ TEST_F(WalletTest1, WalletTransaction)
wallet1->setDefaultMixin(1);
ASSERT_TRUE(wallet1->defaultMixin() == 1);
Bitmonero::PendingTransaction * transaction = wallet1->createTransaction(
recepient_address, AMOUNT_10XMR);
Bitmonero::PendingTransaction * transaction = wallet1->createTransaction(recepient_address,
PAYMENT_ID_EMPTY,
AMOUNT_10XMR,
1);
ASSERT_TRUE(transaction->status() == Bitmonero::PendingTransaction::Status_Ok);
wallet1->refresh();
@ -440,7 +447,6 @@ TEST_F(WalletTest1, WalletTransaction)
ASSERT_FALSE(wallet1->balance() == balance);
ASSERT_TRUE(wmgr->closeWallet(wallet1));
}
*/
TEST_F(WalletTest1, WalletTransactionWithMixin)
{
@ -564,7 +570,7 @@ TEST_F(WalletTest1, WalletTransactionAndHistory)
Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(wallet4_addr,
PAYMENT_ID_EMPTY,
AMOUNT_10XMR * 5, 0);
AMOUNT_10XMR * 5, 1);
ASSERT_TRUE(tx->status() == Bitmonero::PendingTransaction::Status_Ok);
ASSERT_TRUE(tx->commit());
@ -627,6 +633,7 @@ TEST_F(WalletTest1, WalletTransactionWithPaymentId)
ASSERT_TRUE(payment_id_in_history);
}
*/
struct MyWalletListener : public Bitmonero::WalletListener
{
@ -634,7 +641,8 @@ struct MyWalletListener : public Bitmonero::WalletListener
Bitmonero::Wallet * wallet;
uint64_t total_tx;
uint64_t total_rx;
std::timed_mutex guard;
std::mutex mutex;
std::condition_variable cv;
MyWalletListener(Bitmonero::Wallet * wallet)
: total_tx(0), total_rx(0)
@ -645,79 +653,110 @@ struct MyWalletListener : public Bitmonero::WalletListener
virtual void moneySpent(const string &txId, uint64_t amount)
{
std::cout << "wallet: " << wallet->address() << " just spent money ("
std::cerr << "wallet: " << wallet->address() << "**** just spent money ("
<< txId << ", " << wallet->displayAmount(amount) << ")" << std::endl;
total_tx += amount;
guard.unlock();
cv.notify_one();
}
virtual void moneyReceived(const string &txId, uint64_t amount)
{
std::cout << "wallet: " << wallet->address() << " just received money ("
std::cout << "wallet: " << wallet->address() << "**** just received money ("
<< txId << ", " << wallet->displayAmount(amount) << ")" << std::endl;
total_rx += amount;
guard.unlock();
cv.notify_one();
}
virtual void updated()
{
cv.notify_one();
}
virtual void refreshed()
{
std::cout << "Wallet refreshed";
}
};
/*
TEST_F(WalletTest2, WalletCallbackSent)
{
Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET3_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());
MyWalletListener * wallet_src_listener = new MyWalletListener(wallet_src);
uint64_t balance = wallet_src->balance();
std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl;
Bitmonero::Wallet * wallet_dst = wmgr->openWallet(CURRENT_DST_WALLET, TESTNET_WALLET_PASS, true);
uint64_t amount = AMOUNT_1XMR * 5;
std::cout << "** Sending " << Bitmonero::Wallet::displayAmount(amount) << " to " << wallet_dst->address();
uint64_t amount = AMOUNT_10XMR * 5;
std::cout << "** Sending " << Bitmonero::Wallet::displayAmount(amount) << " to " << TESTNET_WALLET4_ADDRESS;
Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(TESTNET_WALLET4_ADDRESS, AMOUNT_1XMR * 5);
Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->address(),
PAYMENT_ID_EMPTY,
amount, 1);
std::cout << "** Committing transaction: " << Bitmonero::Wallet::displayAmount(tx->amount())
<< " with fee: " << Bitmonero::Wallet::displayAmount(tx->fee());
ASSERT_TRUE(tx->status() == Bitmonero::PendingTransaction::Status_Ok);
ASSERT_TRUE(tx->commit());
std::chrono::seconds wait_for = std::chrono::seconds(60*3);
wallet_src_listener->guard.lock();
wallet_src_listener->guard.try_lock_for(wait_for);
ASSERT_TRUE(wallet_src_listener->total_tx != 0);
std::unique_lock<std::mutex> lock (wallet_src_listener->mutex);
wallet_src_listener->cv.wait_for(lock, wait_for);
std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl;
ASSERT_TRUE(wallet_src->balance() < balance);
wmgr->closeWallet(wallet_src);
wmgr->closeWallet(wallet_dst);
}
*/
/*
TEST_F(WalletTest2, WalletCallbackReceived)
{
Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET3_NAME, TESTNET_WALLET_PASS, true);
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());
std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl;
Bitmonero::Wallet * wallet_dst = wmgr->openWallet(TESTNET_WALLET4_NAME, TESTNET_WALLET_PASS, true);
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();
MyWalletListener * wallet_dst_listener = new MyWalletListener(wallet_dst);
uint64_t amount = AMOUNT_1XMR * 5;
std::cout << "** Sending " << Bitmonero::Wallet::displayAmount(amount) << " to " << TESTNET_WALLET4_ADDRESS;
Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(TESTNET_WALLET4_ADDRESS, AMOUNT_1XMR * 5);
std::cout << "** Sending " << Bitmonero::Wallet::displayAmount(amount) << " to " << wallet_dst->address();
Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(wallet_dst->address(),
PAYMENT_ID_EMPTY,
amount, 1);
std::cout << "** Committing transaction: " << Bitmonero::Wallet::displayAmount(tx->amount())
<< " with fee: " << Bitmonero::Wallet::displayAmount(tx->fee());
ASSERT_TRUE(tx->status() == Bitmonero::PendingTransaction::Status_Ok);
ASSERT_TRUE(tx->commit());
std::chrono::seconds wait_for = std::chrono::seconds(60*4);
std::unique_lock<std::mutex> lock (wallet_dst_listener->mutex);
wallet_dst_listener->cv.wait_for(lock, wait_for);
std::cout << "** Balance: " << wallet_dst->displayAmount(wallet_src->balance()) << std::endl;
ASSERT_TRUE(wallet_dst->balance() > balance);
wallet_dst_listener->guard.lock();
wallet_dst_listener->guard.try_lock_for(wait_for);
ASSERT_TRUE(wallet_dst_listener->total_tx != 0);
wmgr->closeWallet(wallet_src);
wmgr->closeWallet(wallet_dst);
}
*/
int main(int argc, char** argv)
{