Wallet::transfer() continued

This commit is contained in:
Ilya Kitaev 2016-04-04 14:56:17 +03:00
parent c37c856d6d
commit ee5bb17f26
3 changed files with 175 additions and 4 deletions

View file

@ -33,10 +33,16 @@
#include "mnemonics/electrum-words.h" #include "mnemonics/electrum-words.h"
#include "cryptonote_core/cryptonote_format_utils.h" #include "cryptonote_core/cryptonote_format_utils.h"
#include "cryptonote_core/cryptonote_basic_impl.h" #include "cryptonote_core/cryptonote_basic_impl.h"
#include "cryptonote_core/cryptonote_format_utils.h"
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <sstream>
#include <boost/format.hpp>
#define tr(x) (x)
namespace epee { namespace epee {
unsigned int g_test_dbg_lock_sleep = 0; unsigned int g_test_dbg_lock_sleep = 0;
@ -51,14 +57,75 @@ namespace {
// copy-pasted from // copy-pasted from
static const size_t DEFAULT_MIX = 4; static const size_t DEFAULT_MIX = 4;
}
using namespace std;
using namespace cryptonote;
Wallet::~Wallet() {}
///////////////////////// Transaction implementation ///////////////////////////
class TransactionImpl : public Transaction
{
public:
TransactionImpl(Wallet * wallet);
~TransactionImpl();
int status() const;
std::string errorString() const;
bool commit();
private:
std::vector<tools::wallet2::pending_tx> & transactions();
private:
friend class WalletImpl;
Wallet * m_wallet;
int m_status;
std::string m_errorString;
std::vector<tools::wallet2::pending_tx> m_pending_tx;
};
TransactionImpl::TransactionImpl(Wallet *wallet)
: m_wallet(wallet)
{
}
TransactionImpl::~TransactionImpl()
{
}
int TransactionImpl::status() const
{
return m_status;
}
string TransactionImpl::errorString() const
{
return m_errorString;
}
bool TransactionImpl::commit()
{
// while (!m_pending_tx.empty()) {
// }
return false;
} }
using namespace std;
Wallet::~Wallet() {}
///////////////////////// Wallet implementation //////////////////////////////// ///////////////////////// Wallet implementation ////////////////////////////////
class WalletImpl : public Wallet class WalletImpl : public Wallet
@ -324,7 +391,11 @@ bool WalletImpl::transfer(const std::string &dst_addr, uint64_t amount)
bool has_payment_id; bool has_payment_id;
bool payment_id_seen = false; bool payment_id_seen = false;
crypto::hash8 new_payment_id; crypto::hash8 new_payment_id;
size_t fake_outs_count = DEFAULT_MIX; size_t fake_outs_count = m_wallet->default_mixin();
if (fake_outs_count == 0)
fake_outs_count = DEFAULT_MIX;
if(!cryptonote::get_account_integrated_address_from_str(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), dst_addr)) { if(!cryptonote::get_account_integrated_address_from_str(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), dst_addr)) {
// TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982 // TODO: copy-paste 'if treating as an address fails, try as url' from simplewallet.cpp:1982
m_status = Status_Error; m_status = Status_Error;
@ -339,10 +410,78 @@ bool WalletImpl::transfer(const std::string &dst_addr, uint64_t amount)
return false; return false;
} }
dsts.push_back(de); dsts.push_back(de);
std::vector<tools::wallet2::pending_tx> ptx_vector;
std::vector<uint8_t> extra;
try {
ptx_vector = m_wallet->create_transactions(dsts, fake_outs_count, 0 /* unlock_time */, 0 /* unused fee arg*/, extra);
// TODO: move it to transaction class
while (!ptx_vector.empty()) {
auto & ptx = ptx_vector.back();
m_wallet->commit_tx(ptx);
// success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx);
// if no exception, remove element from vector
ptx_vector.pop_back();
} // TODO: extract method;
} catch (const tools::error::daemon_busy&) {
// TODO: make it translatable with "tr"?
m_errorString = tr("daemon is busy. Please try again later.");
m_status = Status_Error;
} catch (const tools::error::no_connection_to_daemon&) {
m_errorString = tr("no connection to daemon. Please make sure daemon is running.");
m_status = Status_Error;
} catch (const tools::error::wallet_rpc_error& e) {
m_errorString = tr("RPC error: ") + e.to_string();
m_status = Status_Error;
} catch (const tools::error::get_random_outs_error&) {
m_errorString = tr("failed to get random outputs to mix");
m_status = Status_Error;
} catch (const tools::error::not_enough_money& e) {
m_status = Status_Error;
std::ostringstream writer(m_errorString);
writer << boost::format(tr("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)")) %
print_money(e.available()) %
print_money(e.tx_amount() + e.fee()) %
print_money(e.tx_amount()) %
print_money(e.fee());
} catch (const tools::error::not_enough_outs_to_mix& e) {
std::ostringstream writer(m_errorString);
writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":";
for (const cryptonote::COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& outs_for_amount : e.scanty_outs()) {
writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.amount) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.outs.size();
}
m_status = Status_Error;
} catch (const tools::error::tx_not_constructed&) {
m_errorString = tr("transaction was not constructed");
m_status = Status_Error;
} catch (const tools::error::tx_rejected& e) {
std::ostringstream writer(m_errorString);
writer << (boost::format(tr("transaction %s was rejected by daemon with status: ")) % get_transaction_hash(e.tx())) << e.status();
m_status = Status_Error;
} catch (const tools::error::tx_sum_overflow& e) {
m_errorString = e.what();
m_status = Status_Error;
} catch (const tools::error::zero_destination&) {
m_errorString = tr("one of destinations is zero");
m_status = Status_Error;
} catch (const tools::error::tx_too_big& e) {
m_errorString = tr("failed to find a suitable way to split transactions");
m_status = Status_Error;
} catch (const tools::error::transfer_error& e) {
m_errorString = string(tr("unknown transfer error: ")) + e.what();
m_status = Status_Error;
} catch (const tools::error::wallet_internal_error& e) {
m_errorString = string(tr("internal error: ")) + e.what();
m_status = Status_Error;
} catch (const std::exception& e) {
m_errorString = string(tr("unexpected error: ")) + e.what();
m_status = Status_Error;
} catch (...) {
m_errorString = tr("unknown error");
m_status = Status_Error;
}
return m_status == Status_Ok; return m_status == Status_Ok;
} }
@ -449,4 +588,6 @@ WalletManager *WalletManagerFactory::getWalletManager()
} }
} }

View file

@ -36,6 +36,20 @@
// Public interface for libwallet library // Public interface for libwallet library
namespace Bitmonero { namespace Bitmonero {
/**
* @brief Transaction interface
*/
struct Transaction
{
enum Status {
Status_Ok,
Status_Error
};
virtual int status() const = 0;
virtual std::string errorString() const = 0;
virtual bool commit() = 0;
};
/** /**
* @brief Interface for wallet operations. * @brief Interface for wallet operations.

View file

@ -77,6 +77,9 @@ struct WalletManagerTest : public testing::Test
const char * TESTNET_WALLET_PASS = ""; const char * TESTNET_WALLET_PASS = "";
const char * TESTNET_DAEMON_ADDRESS = "localhost:38081"; 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";
WalletManagerTest() WalletManagerTest()
{ {
@ -261,6 +264,19 @@ TEST_F(WalletManagerTest, WalletRefresh)
ASSERT_TRUE(wmgr->closeWallet(wallet1)); ASSERT_TRUE(wmgr->closeWallet(wallet1));
} }
TEST_F(WalletManagerTest, WalletTransfer)
{
Bitmonero::Wallet * wallet1 = wmgr->openWallet(TESTNET_WALLET_NAME, TESTNET_WALLET_PASS, true);
// make sure testnet daemon is running
ASSERT_TRUE(wallet1->init(TESTNET_DAEMON_ADDRESS, 0));
ASSERT_TRUE(wallet1->refresh());
uint64_t balance = wallet1->balance();
ASSERT_TRUE(wallet1->transfer(RECIPIENT_WALLET_ADDRESS, AMOUNT_10XMR));
ASSERT_FALSE(wallet1->balance() == balance);
ASSERT_TRUE(wmgr->closeWallet(wallet1));
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {