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

rpc: Accept scientific notation for monetary amounts in JSON #6379

Merged
merged 1 commit into from Jul 10, 2015

Conversation

Projects
None yet
5 participants
@laanwj
Member

laanwj commented Jul 6, 2015

Add a function ParseFixedPoint that parses numbers according to the JSON number specification and returns a 64-bit integer.

Then use this in AmountFromValue, rather than ParseMoney.

Also add lots of tests (thanks to @jonasschnelli for some of them).

Fixes issue #6297.

@laanwj laanwj added the RPC/REST/ZMQ label Jul 6, 2015

@jonasschnelli

This comment has been minimized.

Show comment
Hide comment
@jonasschnelli

jonasschnelli Jul 6, 2015

Member

Tested ACK (reviewed code/tests, ran tests, successfully ran smartfees.py, some curl scientific notation tests).

jonasschnelli$ curl --user bitcoinrpc:DP6DvqZtqXarpeNWyN3LZTFchCCyCUuHwNF7E8pX99x1 --data-binary '{"jsonrpc": "1.0", "id" : "1", "method": "sendtoaddress", "params": ["myWgdwSvL2z2WxUbc7wKnRnWnbR925YDaH", 1e-4] }' http://127.0.0.1:18332
{"result":"2ebf06579a36aa8a452924a071460de05d5d87ffc9266d35190ebc42e8476bf9","error":null,"id":"1"}

jonasschnelli$ ./src/bitcoin-cli --regtest gettransaction 2ebf06579a36aa8a452924a071460de05d5d87ffc9266d35190ebc42e8476bf9
{
  "amount": 0.00000000,
  "fee": -0.00000226,
  "confirmations": 0,
  "txid": "2ebf06579a36aa8a452924a071460de05d5d87ffc9266d35190ebc42e8476bf9",
  "walletconflicts": [
  ],
  "time": 1436184739,
  "timereceived": 1436184739,
  "details": [
    {
      "account": "",
      "address": "myWgdwSvL2z2WxUbc7wKnRnWnbR925YDaH",
      "category": "send",
      "amount": -0.00010000,
...
Member

jonasschnelli commented Jul 6, 2015

Tested ACK (reviewed code/tests, ran tests, successfully ran smartfees.py, some curl scientific notation tests).

jonasschnelli$ curl --user bitcoinrpc:DP6DvqZtqXarpeNWyN3LZTFchCCyCUuHwNF7E8pX99x1 --data-binary '{"jsonrpc": "1.0", "id" : "1", "method": "sendtoaddress", "params": ["myWgdwSvL2z2WxUbc7wKnRnWnbR925YDaH", 1e-4] }' http://127.0.0.1:18332
{"result":"2ebf06579a36aa8a452924a071460de05d5d87ffc9266d35190ebc42e8476bf9","error":null,"id":"1"}

jonasschnelli$ ./src/bitcoin-cli --regtest gettransaction 2ebf06579a36aa8a452924a071460de05d5d87ffc9266d35190ebc42e8476bf9
{
  "amount": 0.00000000,
  "fee": -0.00000226,
  "confirmations": 0,
  "txid": "2ebf06579a36aa8a452924a071460de05d5d87ffc9266d35190ebc42e8476bf9",
  "walletconflicts": [
  ],
  "time": 1436184739,
  "timereceived": 1436184739,
  "details": [
    {
      "account": "",
      "address": "myWgdwSvL2z2WxUbc7wKnRnWnbR925YDaH",
      "category": "send",
      "amount": -0.00010000,
...
@morcos

View changes

Show outdated Hide outdated src/test/rpc_tests.cpp
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.01e-6")), COIN/100000000);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.0000000000000000000000000000000000000000000000000000000000000000000000000001e+68")), COIN/100000000);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("10000000000000000000000000000000000000000000000000000000000000000e-64")), COIN);
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("10000000000000000000000000000000000000000000000000000000000000000e-64")), COIN);

This comment has been minimized.

@morcos

morcos Jul 6, 2015

Member

Isn't this a duplicate of the above line?

@morcos

morcos Jul 6, 2015

Member

Isn't this a duplicate of the above line?

This comment has been minimized.

@laanwj

laanwj Jul 6, 2015

Member

Yes, will remove (or if you have another test to suggest, please do :-).

@laanwj

laanwj Jul 6, 2015

Member

Yes, will remove (or if you have another test to suggest, please do :-).

This comment has been minimized.

@morcos

morcos Jul 6, 2015

Member

maybe add some zeroes after the decimal point. you have an excessive number before and and excessive number after, try a test with an excessive number of zeroes both before and after....

@morcos

morcos Jul 6, 2015

Member

maybe add some zeroes after the decimal point. you have an excessive number before and and excessive number after, try a test with an excessive number of zeroes both before and after....

This comment has been minimized.

@laanwj

laanwj Jul 6, 2015

Member

Ok, done

@laanwj

laanwj Jul 6, 2015

Member

Ok, done

@jgarzik

This comment has been minimized.

Show comment
Hide comment
@jgarzik

jgarzik Jul 6, 2015

Contributor

ut ACK

Contributor

jgarzik commented Jul 6, 2015

ut ACK

@morcos

This comment has been minimized.

Show comment
Hide comment
@morcos

morcos Jul 6, 2015

Member

ACK (code review and tested, but not exhaustively)

Member

morcos commented Jul 6, 2015

ACK (code review and tested, but not exhaustively)

@sipa

View changes

Show outdated Hide outdated src/utilstrencodings.cpp
@@ -538,3 +538,118 @@ int atoi(const std::string& str)
{
return atoi(str.c_str());
}
/** Upper bound for mantissa.
* 10^18-1, largest arbitrary decimal that will fit in 64 bit.

This comment has been minimized.

@sipa

sipa Jul 9, 2015

Member

Hypernit: this comment seems inaccurate. There are definitely larger integers that fit in 64 bit.

@sipa

sipa Jul 9, 2015

Member

Hypernit: this comment seems inaccurate. There are definitely larger integers that fit in 64 bit.

This comment has been minimized.

@laanwj

laanwj Jul 10, 2015

Member

it's worded somewhat ackwardly: my point is that larger 64-bit signed integers cannot consist of arbitrary combinations of 0-9:

 999999999999999999   1^18-1
9223372036854775807  (1<<63)-1 (max)
9999999999999999999  would not fit

Wouldn't be impossible to handle it, but I it would complicate bounds checking and don't see the point in our case.
I'll update the comment.

@laanwj

laanwj Jul 10, 2015

Member

it's worded somewhat ackwardly: my point is that larger 64-bit signed integers cannot consist of arbitrary combinations of 0-9:

 999999999999999999   1^18-1
9223372036854775807  (1<<63)-1 (max)
9999999999999999999  would not fit

Wouldn't be impossible to handle it, but I it would complicate bounds checking and don't see the point in our case.
I'll update the comment.

@sipa

This comment has been minimized.

Show comment
Hide comment
@sipa

sipa Jul 9, 2015

Member

ACK. Carefully reviewed the code, but didn't test. The included test cases are convincing.

Member

sipa commented Jul 9, 2015

ACK. Carefully reviewed the code, but didn't test. The included test cases are convincing.

rpc: Accept scientific notation for monetary amounts in JSON
Add a function `ParseFixedPoint` that parses numbers according
to the JSON number specification and returns a 64-bit integer.

Then this in `AmountFromValue`, rather than `ParseMoney`.

Also add lots of tests (thanks to @jonasschnelli for some of them).

Fixes issue #6297.

@laanwj laanwj merged commit 9cc9152 into bitcoin:master Jul 10, 2015

laanwj added a commit that referenced this pull request Jul 10, 2015

Merge pull request #6379
9cc9152 rpc: Accept scientific notation for monetary amounts in JSON (Wladimir J. van der Laan)

@str4d str4d referenced this pull request Feb 10, 2017

Merged

Add UniValue as subtree #2082

zkbot added a commit to zcash/zcash that referenced this pull request Feb 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment