From d97e9ef8a55f92841b30dd31807beb1c06ec264b Mon Sep 17 00:00:00 2001 From: Ilya Kitaev Date: Wed, 6 Apr 2016 13:56:57 +0300 Subject: [PATCH] Transaction API continued --- src/wallet/wallet2_api.cpp | 24 ++++++++++++++++++++---- src/wallet/wallet2_api.h | 1 + tests/libwallet_api_tests/main.cpp | 2 ++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/wallet/wallet2_api.cpp b/src/wallet/wallet2_api.cpp index ae26a1eb3..3947084e3 100644 --- a/src/wallet/wallet2_api.cpp +++ b/src/wallet/wallet2_api.cpp @@ -81,6 +81,7 @@ public: int status() const; std::string errorString() const; bool commit(); + uint64_t amount() const; uint64_t dust() const; uint64_t fee() const; // TODO: continue with interface; @@ -365,11 +366,13 @@ Transaction *WalletImpl::createTransaction(const string &dst_addr, uint64_t amou bool has_payment_id; bool payment_id_seen = false; crypto::hash8 new_payment_id; + // TODO: how this number affects (https://bitcointalk.org/index.php?topic=753252.msg9985441#msg9985441) size_t fake_outs_count = m_wallet->default_mixin(); if (fake_outs_count == 0) fake_outs_count = DEFAULT_MIX; TransactionImpl * transaction = new TransactionImpl(this); + do { if(!cryptonote::get_account_integrated_address_from_str(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), dst_addr)) { @@ -377,7 +380,6 @@ Transaction *WalletImpl::createTransaction(const string &dst_addr, uint64_t amou m_status = Status_Error; m_errorString = "Invalid destination address"; break; - } de.amount = amount; @@ -510,6 +512,9 @@ string TransactionImpl::errorString() const bool TransactionImpl::commit() { + + LOG_PRINT_L0("m_pending_tx size: " << m_pending_tx.size()); + assert(m_pending_tx.size() == 1); try { while (!m_pending_tx.empty()) { auto & ptx = m_pending_tx.back(); @@ -541,10 +546,21 @@ bool TransactionImpl::commit() return m_status == Status_Ok; } +uint64_t TransactionImpl::amount() const +{ + uint64_t result = 0; + for (const auto &ptx : m_pending_tx) { + for (const auto &dest : ptx.dests) { + result += dest.amount; + } + } + return result; +} + uint64_t TransactionImpl::dust() const { uint32_t result = 0; - for (auto ptx : m_pending_tx) { + for (const auto & ptx : m_pending_tx) { result += ptx.dust; } return result; @@ -553,7 +569,7 @@ uint64_t TransactionImpl::dust() const uint64_t TransactionImpl::fee() const { uint32_t result = 0; - for (auto ptx : m_pending_tx) { + for (const auto ptx : m_pending_tx) { result += ptx.fee; } return result; @@ -638,7 +654,7 @@ WalletManager *WalletManagerFactory::getWalletManager() { if (!g_walletManager) { - epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_0); + epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL, LOG_LEVEL_MAX); g_walletManager = new WalletManagerImpl(); } diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index c85cda2de..c064aeba8 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -49,6 +49,7 @@ struct Transaction virtual int status() const = 0; virtual std::string errorString() const = 0; virtual bool commit() = 0; + virtual uint64_t amount() const = 0; virtual uint64_t dust() const = 0; virtual uint64_t fee() const = 0; }; diff --git a/tests/libwallet_api_tests/main.cpp b/tests/libwallet_api_tests/main.cpp index f073b47b9..6e993d70c 100644 --- a/tests/libwallet_api_tests/main.cpp +++ b/tests/libwallet_api_tests/main.cpp @@ -271,12 +271,14 @@ TEST_F(WalletManagerTest, WalletTransaction) ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0)); ASSERT_TRUE(wallet1->refresh()); uint64_t balance = wallet1->balance(); + ASSERT_TRUE(wallet1->status() == Bitmonero::Transaction::Status_Ok); Bitmonero::Transaction * transaction = wallet1->createTransaction( RECIPIENT_WALLET_ADDRESS, AMOUNT_10XMR); ASSERT_TRUE(transaction->status() == Bitmonero::Transaction::Status_Ok); ASSERT_TRUE(wallet1->balance() == balance); + ASSERT_TRUE(transaction->amount() == AMOUNT_10XMR); ASSERT_TRUE(transaction->commit()); ASSERT_FALSE(wallet1->balance() == balance); ASSERT_TRUE(wmgr->closeWallet(wallet1));