Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Freicoin: Move away from 8 decimals #4

Open
wants to merge 3 commits into
base: ar-0.13-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,27 @@ void RPCTypeCheckObj(const UniValue& o,
}
}

CAmount AmountFromValue(const UniValue& value)
CAmount AmountFromValue(const UniValue& value, const bool f8Decimals)
{
if (!value.isNum() && !value.isStr())
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
CAmount amount;
if (!ParseFixedPoint(value.getValStr(), 8, &amount))
// TODO Decimals: Just don't call ParseFixedPoint if !f8Decimals
int decimals = f8Decimals ? 8 : 0;
if (!ParseFixedPoint(value.getValStr(), decimals, &amount))
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
if (!MoneyRange(amount))
throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");
return amount;
}

UniValue ValueFromAmount(const CAmount& amount)
UniValue ValueFromAmount(const CAmount& amount, const bool f8Decimals)
{
bool sign = amount < 0;
if (!f8Decimals) {
assert(!sign); // FIX throw exception ?
return UniValue(UniValue::VNUM, strprintf("%d", amount));
}
int64_t n_abs = (sign ? -amount : amount);
int64_t quotient = n_abs / COIN;
int64_t remainder = n_abs % COIN;
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strNa
extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);

extern int64_t nWalletUnlockTime;
extern CAmount AmountFromValue(const UniValue& value);
extern UniValue ValueFromAmount(const CAmount& amount);
CAmount AmountFromValue(const UniValue& value, const bool f8Decimals=true);
UniValue ValueFromAmount(const CAmount& amount, const bool f8Decimals=true);
extern double GetDifficulty(const CBlockIndex* blockindex = NULL);
extern std::string HelpRequiringPassphrase();
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
Expand Down
27 changes: 27 additions & 0 deletions src/test/rpc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ BOOST_AUTO_TEST_CASE(rpc_createraw_op_return)

BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
{
BOOST_CHECK(ValueFromAmount(0LL, false).write() == "0");
BOOST_CHECK(ValueFromAmount(1LL, false).write() == "1");
BOOST_CHECK(ValueFromAmount(17622195LL, false).write() == "17622195");
BOOST_CHECK(ValueFromAmount(50000000LL, false).write() == "50000000");
BOOST_CHECK(ValueFromAmount(89898989LL, false).write() == "89898989");
BOOST_CHECK(ValueFromAmount(100000000LL, false).write() == "100000000"); // 1 CURRENCY_UNIT
BOOST_CHECK(ValueFromAmount(100000000000000LL, false).write() == "100000000000000"); // 1 M CURRENCY_UNIT
BOOST_CHECK(ValueFromAmount(2100000000000000LL, false).write() == "2100000000000000"); // 21 M CURRENCY_UNIT
BOOST_CHECK(ValueFromAmount(10000000000000000LL, false).write() == "10000000000000000"); // 100 M CURRENCY_UNIT (100 000 000 0000 0000 MINIMAL_UNIT)
BOOST_CHECK(ValueFromAmount(2099999999999990LL, false).write() == "2099999999999990");
BOOST_CHECK(ValueFromAmount(2099999999999999LL, false).write() == "2099999999999999");

BOOST_CHECK(ValueFromAmount(0LL).write() == "0.00000000");
BOOST_CHECK(ValueFromAmount(1LL).write() == "0.00000001");
BOOST_CHECK(ValueFromAmount(17622195LL).write() == "0.17622195");
Expand Down Expand Up @@ -174,6 +186,21 @@ static UniValue ValueFromString(const std::string &str)

BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
{
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("-1"), false), UniValue);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0"), false), 0LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1"), false), 1LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("17622195"), false), 17622195LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("50000000"), false), 50000000LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("89898989"), false), 89898989LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("100000000"), false), 100000000LL); // 1 CURRENCY_UNIT
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("100000000000000"), false), 100000000000000LL); // 1 M CURRENCY_UNIT
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("2100000000000000"), false), 2100000000000000LL); // 21 M CURRENCY_UNIT
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("2099999999999999"), false), 2099999999999999LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("2099999999999990"), false), 2099999999999990LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("209999999999999"), false), 209999999999999LL);
// FIX Decimals: This shouldn't fail? or make sure it always fails
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("10000000000000000"), false), 10000000000000000LL); // 100 M CURRENCY_UNIT (100 000 000 0000 0000 MINIMAL_UNIT)

BOOST_CHECK_THROW(AmountFromValue(ValueFromString("-0.00000001")), UniValue);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0")), 0LL);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000000")), 0LL);
Expand Down
1 change: 1 addition & 0 deletions src/utilstrencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantiss

bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
{
assert(decimals == 8 || decimals == 0);
int64_t mantissa = 0;
int64_t exponent = 0;
int mantissa_tzeros = 0;
Expand Down