Skip to content

Commit c52e8b3

Browse files
committed
Merge pull request #6379
9cc9152 rpc: Accept scientific notation for monetary amounts in JSON (Wladimir J. van der Laan)
2 parents 943b322 + 9cc9152 commit c52e8b3

File tree

5 files changed

+215
-1
lines changed

5 files changed

+215
-1
lines changed

src/rpcserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ CAmount AmountFromValue(const UniValue& value)
124124
if (!value.isReal() && !value.isNum())
125125
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number");
126126
CAmount amount;
127-
if (!ParseMoney(value.getValStr(), amount))
127+
if (!ParseFixedPoint(value.getValStr(), 8, &amount))
128128
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
129129
if (!MoneyRange(amount))
130130
throw JSONRPCError(RPC_TYPE_ERROR, "Amount out of range");

src/test/rpc_tests.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
142142
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1.00000000")), 100000000LL);
143143
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.9999999")), 2099999999999990LL);
144144
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.99999999")), 2099999999999999LL);
145+
146+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("1e-8")), COIN/100000000);
147+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.1e-7")), COIN/100000000);
148+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.01e-6")), COIN/100000000);
149+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.0000000000000000000000000000000000000000000000000000000000000000000000000001e+68")), COIN/100000000);
150+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("10000000000000000000000000000000000000000000000000000000000000000e-64")), COIN);
151+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000e64")), COIN);
152+
153+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e-9")), UniValue); //should fail
154+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("0.000000019")), UniValue); //should fail
155+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000001000000")), 1LL); //should pass, cut trailing 0
156+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("19e-9")), UniValue); //should fail
157+
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.19e-6")), 19); //should pass, leading 0 is present
158+
159+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("92233720368.54775808")), UniValue); //overflow error
160+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e+11")), UniValue); //overflow error
161+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e11")), UniValue); //overflow error signless
162+
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("93e+9")), UniValue); //overflow error
145163
}
146164

147165
BOOST_AUTO_TEST_CASE(json_parse_errors)
@@ -151,6 +169,9 @@ BOOST_AUTO_TEST_CASE(json_parse_errors)
151169
// Valid, with leading or trailing whitespace
152170
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue(" 1.0").get_real(), 1.0);
153171
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("1.0 ").get_real(), 1.0);
172+
173+
BOOST_CHECK_THROW(AmountFromValue(ParseNonRFCJSONValue(".19e-6")), std::runtime_error); //should fail, missing leading 0, therefore invalid JSON
174+
BOOST_CHECK_EQUAL(AmountFromValue(ParseNonRFCJSONValue("0.00000000000000000000000000000000000001e+30 ")), 1);
154175
// Invalid, initial garbage
155176
BOOST_CHECK_THROW(ParseNonRFCJSONValue("[1.0"), std::runtime_error);
156177
BOOST_CHECK_THROW(ParseNonRFCJSONValue("a1.0"), std::runtime_error);

src/test/util_tests.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,70 @@ BOOST_AUTO_TEST_CASE(test_FormatSubVersion)
418418
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments),std::string("/Test:0.9.99(comment1)/"));
419419
BOOST_CHECK_EQUAL(FormatSubVersion("Test", 99900, comments2),std::string("/Test:0.9.99(comment1; comment2)/"));
420420
}
421+
422+
BOOST_AUTO_TEST_CASE(test_ParseFixedPoint)
423+
{
424+
int64_t amount = 0;
425+
BOOST_CHECK(ParseFixedPoint("0", 8, &amount));
426+
BOOST_CHECK_EQUAL(amount, 0LL);
427+
BOOST_CHECK(ParseFixedPoint("1", 8, &amount));
428+
BOOST_CHECK_EQUAL(amount, 100000000LL);
429+
BOOST_CHECK(ParseFixedPoint("0.0", 8, &amount));
430+
BOOST_CHECK_EQUAL(amount, 0LL);
431+
BOOST_CHECK(ParseFixedPoint("-0.1", 8, &amount));
432+
BOOST_CHECK_EQUAL(amount, -10000000LL);
433+
BOOST_CHECK(ParseFixedPoint("1.1", 8, &amount));
434+
BOOST_CHECK_EQUAL(amount, 110000000LL);
435+
BOOST_CHECK(ParseFixedPoint("1.10000000000000000", 8, &amount));
436+
BOOST_CHECK_EQUAL(amount, 110000000LL);
437+
BOOST_CHECK(ParseFixedPoint("1.1e1", 8, &amount));
438+
BOOST_CHECK_EQUAL(amount, 1100000000LL);
439+
BOOST_CHECK(ParseFixedPoint("1.1e-1", 8, &amount));
440+
BOOST_CHECK_EQUAL(amount, 11000000LL);
441+
BOOST_CHECK(ParseFixedPoint("1000", 8, &amount));
442+
BOOST_CHECK_EQUAL(amount, 100000000000LL);
443+
BOOST_CHECK(ParseFixedPoint("-1000", 8, &amount));
444+
BOOST_CHECK_EQUAL(amount, -100000000000LL);
445+
BOOST_CHECK(ParseFixedPoint("0.00000001", 8, &amount));
446+
BOOST_CHECK_EQUAL(amount, 1LL);
447+
BOOST_CHECK(ParseFixedPoint("0.0000000100000000", 8, &amount));
448+
BOOST_CHECK_EQUAL(amount, 1LL);
449+
BOOST_CHECK(ParseFixedPoint("-0.00000001", 8, &amount));
450+
BOOST_CHECK_EQUAL(amount, -1LL);
451+
BOOST_CHECK(ParseFixedPoint("1000000000.00000001", 8, &amount));
452+
BOOST_CHECK_EQUAL(amount, 100000000000000001LL);
453+
BOOST_CHECK(ParseFixedPoint("9999999999.99999999", 8, &amount));
454+
BOOST_CHECK_EQUAL(amount, 999999999999999999LL);
455+
BOOST_CHECK(ParseFixedPoint("-9999999999.99999999", 8, &amount));
456+
BOOST_CHECK_EQUAL(amount, -999999999999999999LL);
457+
458+
BOOST_CHECK(!ParseFixedPoint("", 8, &amount));
459+
BOOST_CHECK(!ParseFixedPoint("-", 8, &amount));
460+
BOOST_CHECK(!ParseFixedPoint("a-1000", 8, &amount));
461+
BOOST_CHECK(!ParseFixedPoint("-a1000", 8, &amount));
462+
BOOST_CHECK(!ParseFixedPoint("-1000a", 8, &amount));
463+
BOOST_CHECK(!ParseFixedPoint("-01000", 8, &amount));
464+
BOOST_CHECK(!ParseFixedPoint("00.1", 8, &amount));
465+
BOOST_CHECK(!ParseFixedPoint(".1", 8, &amount));
466+
BOOST_CHECK(!ParseFixedPoint("--0.1", 8, &amount));
467+
BOOST_CHECK(!ParseFixedPoint("0.000000001", 8, &amount));
468+
BOOST_CHECK(!ParseFixedPoint("-0.000000001", 8, &amount));
469+
BOOST_CHECK(!ParseFixedPoint("0.00000001000000001", 8, &amount));
470+
BOOST_CHECK(!ParseFixedPoint("-10000000000.00000000", 8, &amount));
471+
BOOST_CHECK(!ParseFixedPoint("10000000000.00000000", 8, &amount));
472+
BOOST_CHECK(!ParseFixedPoint("-10000000000.00000001", 8, &amount));
473+
BOOST_CHECK(!ParseFixedPoint("10000000000.00000001", 8, &amount));
474+
BOOST_CHECK(!ParseFixedPoint("-10000000000.00000009", 8, &amount));
475+
BOOST_CHECK(!ParseFixedPoint("10000000000.00000009", 8, &amount));
476+
BOOST_CHECK(!ParseFixedPoint("-99999999999.99999999", 8, &amount));
477+
BOOST_CHECK(!ParseFixedPoint("99999909999.09999999", 8, &amount));
478+
BOOST_CHECK(!ParseFixedPoint("92233720368.54775807", 8, &amount));
479+
BOOST_CHECK(!ParseFixedPoint("92233720368.54775808", 8, &amount));
480+
BOOST_CHECK(!ParseFixedPoint("-92233720368.54775808", 8, &amount));
481+
BOOST_CHECK(!ParseFixedPoint("-92233720368.54775809", 8, &amount));
482+
BOOST_CHECK(!ParseFixedPoint("1.1e", 8, &amount));
483+
BOOST_CHECK(!ParseFixedPoint("1.1e-", 8, &amount));
484+
BOOST_CHECK(!ParseFixedPoint("1.", 8, &amount));
485+
}
486+
421487
BOOST_AUTO_TEST_SUITE_END()

src/utilstrencodings.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,123 @@ int atoi(const std::string& str)
538538
{
539539
return atoi(str.c_str());
540540
}
541+
542+
/** Upper bound for mantissa.
543+
* 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer.
544+
* Larger integers cannot consist of arbitrary combinations of 0-9:
545+
*
546+
* 999999999999999999 1^18-1
547+
* 9223372036854775807 (1<<63)-1 (max int64_t)
548+
* 9999999999999999999 1^19-1 (would overflow)
549+
*/
550+
static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL;
551+
552+
/** Helper function for ParseFixedPoint */
553+
static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
554+
{
555+
if(ch == '0')
556+
++mantissa_tzeros;
557+
else {
558+
for (int i=0; i<=mantissa_tzeros; ++i) {
559+
if (mantissa > (UPPER_BOUND / 10LL))
560+
return false; /* overflow */
561+
mantissa *= 10;
562+
}
563+
mantissa += ch - '0';
564+
mantissa_tzeros = 0;
565+
}
566+
return true;
567+
}
568+
569+
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
570+
{
571+
int64_t mantissa = 0;
572+
int64_t exponent = 0;
573+
int mantissa_tzeros = 0;
574+
bool mantissa_sign = false;
575+
bool exponent_sign = false;
576+
int ptr = 0;
577+
int end = val.size();
578+
int point_ofs = 0;
579+
580+
if (ptr < end && val[ptr] == '-') {
581+
mantissa_sign = true;
582+
++ptr;
583+
}
584+
if (ptr < end)
585+
{
586+
if (val[ptr] == '0') {
587+
/* pass single 0 */
588+
++ptr;
589+
} else if (val[ptr] >= '1' && val[ptr] <= '9') {
590+
while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') {
591+
if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
592+
return false; /* overflow */
593+
++ptr;
594+
}
595+
} else return false; /* missing expected digit */
596+
} else return false; /* empty string or loose '-' */
597+
if (ptr < end && val[ptr] == '.')
598+
{
599+
++ptr;
600+
if (ptr < end && val[ptr] >= '0' && val[ptr] <= '9')
601+
{
602+
while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') {
603+
if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros))
604+
return false; /* overflow */
605+
++ptr;
606+
++point_ofs;
607+
}
608+
} else return false; /* missing expected digit */
609+
}
610+
if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E'))
611+
{
612+
++ptr;
613+
if (ptr < end && val[ptr] == '+')
614+
++ptr;
615+
else if (ptr < end && val[ptr] == '-') {
616+
exponent_sign = true;
617+
++ptr;
618+
}
619+
if (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') {
620+
while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') {
621+
if (exponent > (UPPER_BOUND / 10LL))
622+
return false; /* overflow */
623+
exponent = exponent * 10 + val[ptr] - '0';
624+
++ptr;
625+
}
626+
} else return false; /* missing expected digit */
627+
}
628+
if (ptr != end)
629+
return false; /* trailing garbage */
630+
631+
/* finalize exponent */
632+
if (exponent_sign)
633+
exponent = -exponent;
634+
exponent = exponent - point_ofs + mantissa_tzeros;
635+
636+
/* finalize mantissa */
637+
if (mantissa_sign)
638+
mantissa = -mantissa;
639+
640+
/* convert to one 64-bit fixed-point value */
641+
exponent += decimals;
642+
if (exponent < 0)
643+
return false; /* cannot represent values smaller than 10^-decimals */
644+
if (exponent >= 18)
645+
return false; /* cannot represent values larger than or equal to 10^(18-decimals) */
646+
647+
for (int i=0; i < exponent; ++i) {
648+
if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL))
649+
return false; /* overflow */
650+
mantissa *= 10;
651+
}
652+
if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND)
653+
return false; /* overflow */
654+
655+
if (amount_out)
656+
*amount_out = mantissa;
657+
658+
return true;
659+
}
660+

src/utilstrencodings.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,11 @@ bool TimingResistantEqual(const T& a, const T& b)
109109
return accumulator == 0;
110110
}
111111

112+
/** Parse number as fixed point according to JSON number syntax.
113+
* See http://json.org/number.gif
114+
* @returns true on success, false on error.
115+
* @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
116+
*/
117+
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
118+
112119
#endif // BITCOIN_UTILSTRENCODINGS_H

0 commit comments

Comments
 (0)