Skip to content

Commit

Permalink
balance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
gutongjiang committed Oct 21, 2019
1 parent 58606a3 commit ac95ef8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
17 changes: 13 additions & 4 deletions src/cryptonote_basic/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/numeric/conversion/cast.hpp>

typedef boost::multiprecision::cpp_dec_float_50 xmc_float;
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<64> > xmc_float;
typedef boost::multiprecision::cpp_int xmc_int;
static xmc_float XMC_UINT = 1000000000000.0;
typedef boost::multiprecision::uint128_t xmc_uint_128;
const xmc_float XMC_UINT = 1000000000000.0;
const xmc_uint_128 XMC_INT_MAX = xmc_uint_128((uint64_t)10000000000000000000ull);

using namespace epee;

Expand Down Expand Up @@ -1001,15 +1003,22 @@ namespace cryptonote

double xmc_int_to_double(xmc_int amount)
{
xmc_float amount_float = amount.convert_to<xmc_float>();
xmc_uint_128 amount_128 = amount.convert_to<xmc_uint_128>();
if(amount_128 < XMC_INT_MAX)
{
uint64_t int_amount = amount_128.convert_to<uint64_t>();
double ret = int_amount / 1000000000000.0;
return ret;
}

xmc_float amount_float = xmc_float(amount_128);
xmc_float amount_xmc = amount_float / XMC_UINT;
double ret = amount_xmc.convert_to<double>();
return ret;
}

std::string print_money(xmc_int amount, unsigned int decimal_point)
{
std::cout << "**** PRINT MONEY: amount:" << amount << ",amount typeid name:"<< typeid(amount).name()<< ", decimal_point:" << decimal_point << std::endl;
if (decimal_point == (unsigned int)-1)
decimal_point = default_decimal_point;
std::stringstream ss;
Expand Down
15 changes: 13 additions & 2 deletions src/simplewallet/simplewallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,24 +520,35 @@ namespace
}
catch (const tools::error::not_enough_unlocked_money& e)
{
double available = cryptonote::xmc_int_to_double(e.available());
LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, sent amount %s") %
print_money(available) %
print_money(e.tx_amount()));

/*
LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, sent amount %s") %
print_money(e.available()) %
print_money(e.tx_amount()));
*/
fail_msg_writer() << sw::tr("Not enough money in unlocked balance");
warn_of_possible_attack = false;
}
catch (const tools::error::not_enough_money& e)
{
double available = cryptonote::xmc_int_to_double(e.available());
LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, sent amount %s") %
print_money(e.available()) %
// print_money(e.available()) %
print_money(available) %
print_money(e.tx_amount()));
fail_msg_writer() << sw::tr("Not enough money in unlocked balance");
warn_of_possible_attack = false;
}
catch (const tools::error::tx_not_possible& e)
{
double available = cryptonote::xmc_int_to_double(e.available());
LOG_PRINT_L0(boost::format("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)") %
print_money(e.available()) %
// print_money(e.available()) %
print_money(available) %
print_money(e.tx_amount() + e.fee()) %
print_money(e.tx_amount()) %
print_money(e.fee()));
Expand Down
30 changes: 19 additions & 11 deletions src/wallet/api/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1548,22 +1548,26 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const
} catch (const tools::error::not_enough_unlocked_money& e) {
std::ostringstream writer;

writer << boost::format(tr("not enough money to transfer, available only %s, sent amount %s")) %
print_money(e.available()) %
double available = cryptonote::xmc_int_to_double(e.available());
writer << boost::format(tr("not enough money to transfer, available only: %s, sent amount: %s")) %
print_money(available) %
print_money(e.tx_amount());
setStatusError(writer.str());
} catch (const tools::error::not_enough_money& e) {
std::ostringstream writer;

writer << boost::format(tr("not enough money to transfer, overall balance only %s, sent amount %s")) %
print_money(e.available()) %
print_money(e.tx_amount());
double available = cryptonote::xmc_int_to_double(e.available());
writer << boost::format(tr("not enough money to transfer, overall balance only: %s, sent amount: %s, min fee: %s")) %
print_money(available) %
print_money(e.tx_amount()) %
print_money(e.fee());
setStatusError(writer.str());
} catch (const tools::error::tx_not_possible& e) {
std::ostringstream writer;

double available = cryptonote::xmc_int_to_double(e.available());
writer << boost::format(tr("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)")) %
print_money(e.available()) %
print_money(available) %
print_money(e.tx_amount() + e.fee()) %
print_money(e.tx_amount()) %
print_money(e.fee());
Expand Down Expand Up @@ -1631,24 +1635,28 @@ PendingTransaction *WalletImpl::createSweepUnmixableTransaction()
setStatusError("");
std::ostringstream writer;

double available = cryptonote::xmc_int_to_double(e.available());
writer << boost::format(tr("not enough money to transfer, available only %s, sent amount %s")) %
print_money(e.available()) %
print_money(available) %
print_money(e.tx_amount());
setStatusError(writer.str());
} catch (const tools::error::not_enough_money& e) {
setStatusError("");
std::ostringstream writer;

writer << boost::format(tr("not enough money to transfer, overall balance only %s, sent amount %s")) %
print_money(e.available()) %
print_money(e.tx_amount());
double available = cryptonote::xmc_int_to_double(e.available());
writer << boost::format(tr("not enough money to transfer, overall balance only %s, sent amount %s, min fee %s")) %
print_money(available) %
print_money(e.tx_amount()) %
print_money(e.fee());
setStatusError(writer.str());
} catch (const tools::error::tx_not_possible& e) {
setStatusError("");
std::ostringstream writer;

double available = cryptonote::xmc_int_to_double(e.available());
writer << boost::format(tr("not enough money to transfer, available only %s, transaction amount %s = %s + %s (fee)")) %
print_money(e.available()) %
print_money(available) %
print_money(e.tx_amount() + e.fee()) %
print_money(e.tx_amount()) %
print_money(e.fee());
Expand Down
13 changes: 12 additions & 1 deletion src/wallet/wallet2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6161,11 +6161,20 @@ void wallet2::commit_tx(pending_tx& ptx)
m_transfers[idx].m_multisig_k.clear();

//fee includes dust if dust policy specified it.
double xmc_balance = cryptonote::xmc_int_to_double(balance(ptx.construction_data.subaddr_account));
double xmc_unlocked_balance = cryptonote::xmc_int_to_double(unlocked_balance(ptx.construction_data.subaddr_account));
/*
LOG_PRINT_L1("Transaction successfully sent. <" << txid << ">" << ENDL
<< "Commission: " << print_money(ptx.fee) << " (dust sent to dust addr: " << print_money((ptx.dust_added_to_fee ? 0 : ptx.dust)) << ")" << ENDL
<< "Balance: " << print_money(balance(ptx.construction_data.subaddr_account)) << ENDL
<< "Unlocked: " << print_money(unlocked_balance(ptx.construction_data.subaddr_account)) << ENDL
<< "Please, wait for confirmation for your balance to be unlocked.");
*/
LOG_PRINT_L1("Transaction successfully sent. <" << txid << ">" << ENDL
<< "Commission: " << print_money(ptx.fee) << " (dust sent to dust addr: " << print_money((ptx.dust_added_to_fee ? 0 : ptx.dust)) << ")" << ENDL
<< "Balance: " << print_money(xmc_balance) << ENDL
<< "Unlocked: " << print_money(xmc_unlocked_balance) << ENDL
<< "Please, wait for confirmation for your balance to be unlocked.");
}

void wallet2::commit_tx(std::vector<pending_tx>& ptx_vector)
Expand Down Expand Up @@ -9269,8 +9278,10 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp
balance_subtotal += balance_per_subaddr[index_minor];
unlocked_balance_subtotal += unlocked_balance_per_subaddr[index_minor].first;
}
LOG_PRINT_L1("needed_money:" << needed_money << ", min_fee:" << min_fee << ", balance_subtotal:" << balance_subtotal << "needed_money+min_fee:" << needed_money + min_fee);
THROW_WALLET_EXCEPTION_IF(needed_money + min_fee > balance_subtotal, error::not_enough_money,
balance_subtotal, needed_money, 0);
// balance_subtotal, needed_money, 0);
balance_subtotal, needed_money, min_fee);
// first check overall balance is enough, then unlocked one, so we throw distinct exceptions
THROW_WALLET_EXCEPTION_IF(needed_money + min_fee > unlocked_balance_subtotal, error::not_enough_unlocked_money,
unlocked_balance_subtotal, needed_money, 0);
Expand Down
6 changes: 5 additions & 1 deletion src/wallet/wallet_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,26 +482,30 @@ namespace tools
: transfer_error(std::move(loc), "not enough money")
, m_available(available)
, m_tx_amount(tx_amount)
, m_fee(fee)
{
}

// uint64_t available() const { return m_available; }
xmc_int available() const { return m_available; }
uint64_t tx_amount() const { return m_tx_amount; }
uint64_t fee() const { return m_fee; }

std::string to_string() const
{
std::ostringstream ss;
ss << transfer_error::to_string() <<
", available = " << cryptonote::print_money(m_available) <<
", tx_amount = " << cryptonote::print_money(m_tx_amount);
", tx_amount = " << cryptonote::print_money(m_tx_amount) <<
", min fee = " << cryptonote::print_money(m_fee);
return ss.str();
}

private:
// uint64_t m_available;
xmc_int m_available;
uint64_t m_tx_amount;
uint64_t m_fee;
};
//----------------------------------------------------------------------------------------------------
struct tx_not_possible : public transfer_error
Expand Down

0 comments on commit ac95ef8

Please sign in to comment.