WalletListener::moneySpent test

This commit is contained in:
Ilya Kitaev 2016-05-16 13:11:44 +03:00
parent 27d86b73c6
commit 40087a745f
3 changed files with 173 additions and 64 deletions

View file

@ -50,6 +50,12 @@ namespace {
struct Wallet2CallbackImpl : public tools::i_wallet2_callback
{
Wallet2CallbackImpl()
: m_listener(nullptr)
{
}
~Wallet2CallbackImpl()
{
@ -109,6 +115,8 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
Wallet::~Wallet() {}
WalletListener::~WalletListener() {}
string Wallet::displayAmount(uint64_t amount)
{
return cryptonote::print_money(amount);

View file

@ -106,8 +106,8 @@ struct TransactionHistory
struct WalletListener
{
virtual ~WalletListener() = 0;
virtual void moneySpent(const std::string &txId, uint64_t amount);
virtual void moneyReceived(const std::string &txId, uint64_t amount);
virtual void moneySpent(const std::string &txId, uint64_t amount) = 0;
virtual void moneyReceived(const std::string &txId, uint64_t amount) = 0;
// TODO: on_skip_transaction;
};

View file

@ -37,12 +37,47 @@
#include <iostream>
#include <vector>
#include <mutex>
#include <thread>
using namespace std;
//unsigned int epee::g_test_dbg_lock_sleep = 0;
namespace Consts
{
// TODO: add test wallets to the source tree (as they have some balance mined)?
// TODO: get rid of hardcoded paths
const char * WALLET_NAME = "testwallet";
const char * WALLET_NAME_COPY = "testwallet_copy";
const char * WALLET_NAME_WITH_DIR = "walletdir/testwallet_test";
const char * WALLET_NAME_WITH_DIR_NON_WRITABLE = "/var/walletdir/testwallet_test";
const char * WALLET_PASS = "password";
const char * WALLET_PASS2 = "password22";
const char * WALLET_LANG = "English";
const char * TESTNET_WALLET_NAME = "/home/mbg033/dev/monero/testnet/wallet_01.bin";
const char * TESTNET_WALLET3_NAME = "/home/mbg033/dev/monero/testnet/wallet_03.bin";
const char * TESTNET_WALLET4_NAME = "/home/mbg033/dev/monero/testnet/wallet_04.bin";
const char * TESTNET_WALLET_PASS = "";
const char * TESTNET_DAEMON_ADDRESS = "localhost:38081";
const uint64_t AMOUNT_10XMR = 10000000000000L;
const uint64_t AMOUNT_5XMR = 50000000000000L;
const char * RECIPIENT_WALLET_ADDRESS = "9uekQVGj7NjSAREnZ8cUsRagWDdjvdhpwUKhsL95oXngBnZXZ1RzH8R6UJbU1R7wim9yKbSjxuoQ22ERRkEochGECj66oP3";
const char * TESTNET_WALLET3_ADDRESS = "A11cBpRDqpTCneSL3KNBvGWM6PfxG7QrxNVCcMiZeuAD3fQA9Z366DegFLYHKrMnDm8QixPziRn4kVcWPFtn6aCSR1Hp7sg";
const char * TESTNET_WALLET4_ADDRESS = "A21wicxbhUSKa6twequhKCCG8wYEGZ7viYRLW7mBXtWyheyY8C8XwUJG5PSjULDs1q7hndkihtFgybWjagvchrNg1Y588hM";
}
using namespace Consts;
struct Utils
{
static void deleteWallet(const std::string & walletname)
@ -58,52 +93,6 @@ struct Utils
std::cout << "** removing dir recursively: " << path << std::endl;
boost::filesystem::remove_all(path);
}
};
struct WalletManagerTest : public testing::Test
{
Bitmonero::WalletManager * wmgr;
const char * WALLET_NAME = "testwallet";
const char * WALLET_NAME_COPY = "testwallet_copy";
const char * WALLET_NAME_WITH_DIR = "walletdir/testwallet_test";
const char * WALLET_NAME_WITH_DIR_NON_WRITABLE = "/var/walletdir/testwallet_test";
const char * WALLET_PASS = "password";
const char * WALLET_PASS2 = "password22";
const char * WALLET_LANG = "English";
// TODO: add test wallets to the source tree (as they have some balance mined)?
const char * TESTNET_WALLET_NAME = "/home/mbg033/dev/monero/testnet/wallet_01.bin";
const char * TESTNET_WALLET3_NAME = "/home/mbg033/dev/monero/testnet/wallet_03.bin";
const char * TESTNET_WALLET4_NAME = "/home/mbg033/dev/monero/testnet/wallet_04.bin";
const char * TESTNET_WALLET_PASS = "";
const char * TESTNET_DAEMON_ADDRESS = "localhost:38081";
const uint64_t AMOUNT_10XMR = 10000000000000L;
const uint64_t AMOUNT_5XMR = 50000000000000L;
const char * RECIPIENT_WALLET_ADDRESS = "9uekQVGj7NjSAREnZ8cUsRagWDdjvdhpwUKhsL95oXngBnZXZ1RzH8R6UJbU1R7wim9yKbSjxuoQ22ERRkEochGECj66oP3";
const char * TESTNET_WALLET3_ADDRESS = "A11cBpRDqpTCneSL3KNBvGWM6PfxG7QrxNVCcMiZeuAD3fQA9Z366DegFLYHKrMnDm8QixPziRn4kVcWPFtn6aCSR1Hp7sg";
const char * TESTNET_WALLET4_ADDRESS = "A21wicxbhUSKa6twequhKCCG8wYEGZ7viYRLW7mBXtWyheyY8C8XwUJG5PSjULDs1q7hndkihtFgybWjagvchrNg1Y588hM";
WalletManagerTest()
{
std::cout << __FUNCTION__ << std::endl;
wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
Utils::deleteWallet(WALLET_NAME);
Utils::deleteDir(boost::filesystem::path(WALLET_NAME_WITH_DIR).parent_path().string());
}
~WalletManagerTest()
{
std::cout << __FUNCTION__ << std::endl;
//deleteWallet(WALLET_NAME);
}
static void print_transaction(Bitmonero::TransactionInfo * t)
{
@ -121,7 +110,56 @@ struct WalletManagerTest : public testing::Test
};
TEST_F(WalletManagerTest, WalletManagerCreatesWallet)
struct DISABLED_WalletManagerTest : public testing::Test
{
Bitmonero::WalletManager * wmgr;
DISABLED_WalletManagerTest()
{
std::cout << __FUNCTION__ << std::endl;
wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
Utils::deleteWallet(WALLET_NAME);
Utils::deleteDir(boost::filesystem::path(WALLET_NAME_WITH_DIR).parent_path().string());
}
~DISABLED_WalletManagerTest()
{
std::cout << __FUNCTION__ << std::endl;
//deleteWallet(WALLET_NAME);
}
};
struct DISABLED_WalletTest1 : public testing::Test
{
Bitmonero::WalletManager * wmgr;
DISABLED_WalletTest1()
{
wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
}
};
struct WalletTest2 : public testing::Test
{
Bitmonero::WalletManager * wmgr;
WalletTest2()
{
wmgr = Bitmonero::WalletManagerFactory::getWalletManager();
}
};
TEST_F(DISABLED_WalletManagerTest, WalletManagerCreatesWallet)
{
Bitmonero::Wallet * wallet = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
@ -138,7 +176,7 @@ TEST_F(WalletManagerTest, WalletManagerCreatesWallet)
}
TEST_F(WalletManagerTest, WalletManagerOpensWallet)
TEST_F(DISABLED_WalletManagerTest, WalletManagerOpensWallet)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
@ -151,7 +189,7 @@ TEST_F(WalletManagerTest, WalletManagerOpensWallet)
}
TEST_F(WalletManagerTest, WalletManagerChangesPassword)
TEST_F(DISABLED_WalletManagerTest, WalletManagerChangesPassword)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
@ -167,7 +205,7 @@ TEST_F(WalletManagerTest, WalletManagerChangesPassword)
TEST_F(WalletManagerTest, WalletManagerRecoversWallet)
TEST_F(DISABLED_WalletManagerTest, WalletManagerRecoversWallet)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
@ -183,7 +221,7 @@ TEST_F(WalletManagerTest, WalletManagerRecoversWallet)
}
TEST_F(WalletManagerTest, WalletManagerStoresWallet1)
TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet1)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
@ -200,7 +238,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet1)
}
TEST_F(WalletManagerTest, WalletManagerStoresWallet2)
TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet2)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
@ -216,7 +254,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet2)
ASSERT_TRUE(wmgr->closeWallet(wallet1));
}
TEST_F(WalletManagerTest, WalletManagerStoresWallet3)
TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet3)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
@ -238,7 +276,7 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet3)
}
TEST_F(WalletManagerTest, WalletManagerStoresWallet4)
TEST_F(DISABLED_WalletManagerTest, WalletManagerStoresWallet4)
{
Bitmonero::Wallet * wallet1 = wmgr->createWallet(WALLET_NAME, WALLET_PASS, WALLET_LANG);
std::string seed1 = wallet1->seed();
@ -260,7 +298,8 @@ TEST_F(WalletManagerTest, WalletManagerStoresWallet4)
}
TEST_F(WalletManagerTest, WalletShowsBalance)
TEST_F(DISABLED_WalletTest1, WalletShowsBalance)
{
Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true);
ASSERT_TRUE(wallet1->balance() > 0);
@ -278,7 +317,7 @@ TEST_F(WalletManagerTest, WalletShowsBalance)
ASSERT_TRUE(wmgr->closeWallet(wallet2));
}
TEST_F(WalletManagerTest, WalletRefresh)
TEST_F(DISABLED_WalletTest1, WalletRefresh)
{
Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true);
// make sure testnet daemon is running
@ -287,7 +326,7 @@ TEST_F(WalletManagerTest, WalletRefresh)
ASSERT_TRUE(wmgr->closeWallet(wallet1));
}
TEST_F(WalletManagerTest, WalletTransaction)
TEST_F(DISABLED_WalletTest1, WalletTransaction)
{
Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true);
// make sure testnet daemon is running
@ -308,7 +347,7 @@ TEST_F(WalletManagerTest, WalletTransaction)
ASSERT_TRUE(wmgr->closeWallet(wallet1));
}
TEST_F(WalletManagerTest, WalletHistory)
TEST_F(DISABLED_WalletTest1, WalletHistory)
{
Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true);
// make sure testnet daemon is running
@ -321,11 +360,11 @@ TEST_F(WalletManagerTest, WalletHistory)
for (auto t: history->getAll()) {
ASSERT_TRUE(t != nullptr);
print_transaction(t);
Utils::print_transaction(t);
}
}
TEST_F(WalletManagerTest, WalletTransactionAndHistory)
TEST_F(DISABLED_WalletTest1, WalletTransactionAndHistory)
{
Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET3_NAME, TESTNET_WALLET_PASS, true);
// make sure testnet daemon is running
@ -339,7 +378,7 @@ TEST_F(WalletManagerTest, WalletTransactionAndHistory)
std::cout << "**** Transactions before transfer (" << count1 << ")" << std::endl;
for (auto t: history->getAll()) {
ASSERT_TRUE(t != nullptr);
print_transaction(t);
Utils::print_transaction(t);
}
Bitmonero::PendingTransaction * tx = wallet_src->createTransaction(TESTNET_WALLET4_ADDRESS, AMOUNT_10XMR * 5);
@ -352,11 +391,73 @@ TEST_F(WalletManagerTest, WalletTransactionAndHistory)
std::cout << "**** Transactions after transfer (" << history->count() << ")" << std::endl;
for (auto t: history->getAll()) {
ASSERT_TRUE(t != nullptr);
print_transaction(t);
Utils::print_transaction(t);
}
}
struct MyWalletListener : public Bitmonero::WalletListener
{
Bitmonero::Wallet * wallet;
uint64_t total_tx;
uint64_t total_rx;
std::timed_mutex guard;
MyWalletListener(Bitmonero::Wallet * wallet)
: total_tx(0), total_rx(0)
{
this->wallet = wallet;
this->wallet->setListener(this);
}
virtual void moneySpent(const string &txId, uint64_t amount)
{
std::cout << "wallet: " << wallet->address() << " just spent money ("
<< txId << ", " << wallet->displayAmount(amount) << ")" << std::endl;
total_tx += amount;
guard.unlock();
}
virtual void moneyReceived(const string &txId, uint64_t amount)
{
std::cout << "wallet: " << wallet->address() << " just received money ("
<< txId << ", " << wallet->displayAmount(amount) << ")" << std::endl;
total_rx += amount;
guard.unlock();
}
};
TEST_F(WalletTest2, WalletCallbackSent)
{
Bitmonero::Wallet * wallet_src = wmgr->openWallet(TESTNET_WALLET3_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());
MyWalletListener * wallet_src_listener = new MyWalletListener(wallet_src);
std::cout << "** Balance: " << wallet_src->displayAmount(wallet_src->balance()) << std::endl;
/*
Bitmonero::Wallet * wallet_dst = wmgr->openWallet(TESTNET_WALLET4_NAME, TESTNET_WALLET_PASS, true);
ASSERT_TRUE(wallet_dst->init(TESTNET_DAEMON_ADDRESS, 0));
ASSERT_TRUE(wallet_dst->refresh());
MyWalletListener * wallet_dst_listener = new MyWalletListener(wallet_dst);
*/
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_10XMR * 5);
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);
}
int main(int argc, char** argv)
{