Browse Source

Merge pull request #6380

9127e97 doc: Mention RPC strings for monetary amounts in release notes (Wladimir J. van der Laan)
7d226b7 [QA] add testcases for parsing strings as values (Jonas Schnelli)
614601b rpc: Accept strings in AmountFromValue (Wladimir J. van der Laan)
0.13
Wladimir J. van der Laan 10 years ago
parent
commit
240b30eaf0
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
  1. 9
      doc/release-notes.md
  2. 32
      qa/rpc-tests/wallet.py
  3. 4
      src/rpcserver.cpp

9
doc/release-notes.md

@ -19,10 +19,13 @@ https://www.torproject.org/docs/tor-manual.html.en
This allows running bitcoind without having to do any manual configuration. This allows running bitcoind without having to do any manual configuration.
Example header Low-level RPC API changes
---------------------- --------------------------
Example content. - Monetary amounts can be provided as strings. This means that for example the
argument to sendtoaddress can be "0.0001" instead of 0.0001. This can be an
advantage if a JSON library insists on using a lossy floating point type for
numbers, which would be dangerous for monetary amounts.
0.12.0 Change log 0.12.0 Change log
================= =================

32
qa/rpc-tests/wallet.py

@ -218,5 +218,37 @@ class WalletTest (BitcoinTestFramework):
#tx should be added to balance because after restarting the nodes tx should be broadcastet #tx should be added to balance because after restarting the nodes tx should be broadcastet
assert_equal(self.nodes[2].getbalance(), Decimal('63.99800000')); #should not be assert_equal(self.nodes[2].getbalance(), Decimal('63.99800000')); #should not be
#send a tx with value in a string (PR#6380 +)
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2")
txObj = self.nodes[0].gettransaction(txId)
assert_equal(txObj['amount'], Decimal('-2.00000000'))
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001")
txObj = self.nodes[0].gettransaction(txId)
assert_equal(txObj['amount'], Decimal('-0.00010000'))
#check if JSON parser can handle scientific notation in strings
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4")
txObj = self.nodes[0].gettransaction(txId)
assert_equal(txObj['amount'], Decimal('-0.00010000'))
#this should fail
errorString = ""
try:
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1f-4")
except JSONRPCException,e:
errorString = e.error['message']
assert_equal("Invalid amount" in errorString, True);
errorString = ""
try:
self.nodes[0].generate("2") #use a string to as block amount parameter must fail because it's not interpreted as amount
except JSONRPCException,e:
errorString = e.error['message']
assert_equal("not an integer" in errorString, True);
if __name__ == '__main__': if __name__ == '__main__':
WalletTest ().main () WalletTest ().main ()

4
src/rpcserver.cpp

@ -120,8 +120,8 @@ void RPCTypeCheckObj(const UniValue& o,
CAmount AmountFromValue(const UniValue& value) CAmount AmountFromValue(const UniValue& value)
{ {
if (!value.isNum()) if (!value.isNum() && !value.isStr())
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number"); throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
CAmount amount; CAmount amount;
if (!ParseFixedPoint(value.getValStr(), 8, &amount)) if (!ParseFixedPoint(value.getValStr(), 8, &amount))
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount"); throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");

Loading…
Cancel
Save