Skip to content

Commit 240b30e

Browse files
committed
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)
2 parents d43297c + 9127e97 commit 240b30e

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

doc/release-notes.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ https://www.torproject.org/docs/tor-manual.html.en
1919

2020
This allows running bitcoind without having to do any manual configuration.
2121

22-
Example header
23-
----------------------
22+
Low-level RPC API changes
23+
--------------------------
2424

25-
Example content.
25+
- Monetary amounts can be provided as strings. This means that for example the
26+
argument to sendtoaddress can be "0.0001" instead of 0.0001. This can be an
27+
advantage if a JSON library insists on using a lossy floating point type for
28+
numbers, which would be dangerous for monetary amounts.
2629

2730
0.12.0 Change log
2831
=================

qa/rpc-tests/wallet.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#
7-
# Exercise the wallet. Ported from wallet.sh.
7+
# Exercise the wallet. Ported from wallet.sh.
88
# Does the following:
99
# a) creates 3 nodes, with an empty chain (no blocks).
1010
# b) node0 mines a block
11-
# c) node1 mines 101 blocks, so now nodes 0 and 1 have 50btc, node2 has none.
11+
# c) node1 mines 101 blocks, so now nodes 0 and 1 have 50btc, node2 has none.
1212
# d) node0 sends 21 btc to node2, in two transactions (11 btc, then 10 btc).
1313
# e) node0 mines a block, collects the fee on the second transaction
1414
# f) node1 mines 100 blocks, to mature node0's just-mined block
@@ -75,14 +75,14 @@ def run_test (self):
7575
assert_equal(self.nodes[2].getbalance(), 21)
7676

7777
# Node0 should have two unspent outputs.
78-
# Create a couple of transactions to send them to node2, submit them through
79-
# node1, and make sure both node0 and node2 pick them up properly:
78+
# Create a couple of transactions to send them to node2, submit them through
79+
# node1, and make sure both node0 and node2 pick them up properly:
8080
node0utxos = self.nodes[0].listunspent(1)
8181
assert_equal(len(node0utxos), 2)
8282

8383
# create both transactions
8484
txns_to_send = []
85-
for utxo in node0utxos:
85+
for utxo in node0utxos:
8686
inputs = []
8787
outputs = {}
8888
inputs.append({ "txid" : utxo["txid"], "vout" : utxo["vout"]})
@@ -149,35 +149,35 @@ def run_test (self):
149149
sync_mempools(self.nodes)
150150

151151
assert(txid1 in self.nodes[3].getrawmempool())
152-
152+
153153
#check if we can list zero value tx as available coins
154154
#1. create rawtx
155-
#2. hex-changed one output to 0.0
155+
#2. hex-changed one output to 0.0
156156
#3. sign and send
157157
#4. check if recipient (node0) can list the zero value tx
158158
usp = self.nodes[1].listunspent()
159159
inputs = [{"txid":usp[0]['txid'], "vout":usp[0]['vout']}]
160160
outputs = {self.nodes[1].getnewaddress(): 49.998, self.nodes[0].getnewaddress(): 11.11}
161-
161+
162162
rawTx = self.nodes[1].createrawtransaction(inputs, outputs).replace("c0833842", "00000000") #replace 11.11 with 0.0 (int32)
163163
decRawTx = self.nodes[1].decoderawtransaction(rawTx)
164164
signedRawTx = self.nodes[1].signrawtransaction(rawTx)
165165
decRawTx = self.nodes[1].decoderawtransaction(signedRawTx['hex'])
166166
zeroValueTxid= decRawTx['txid']
167167
sendResp = self.nodes[1].sendrawtransaction(signedRawTx['hex'])
168-
168+
169169
self.sync_all()
170170
self.nodes[1].generate(1) #mine a block
171171
self.sync_all()
172-
172+
173173
unspentTxs = self.nodes[0].listunspent() #zero value tx must be in listunspents output
174174
found = False
175175
for uTx in unspentTxs:
176176
if uTx['txid'] == zeroValueTxid:
177177
found = True
178178
assert_equal(uTx['amount'], Decimal('0.00000000'));
179179
assert(found)
180-
180+
181181
#do some -walletbroadcast tests
182182
stop_nodes(self.nodes)
183183
wait_bitcoinds()
@@ -192,17 +192,17 @@ def run_test (self):
192192
self.nodes[1].generate(1) #mine a block, tx should not be in there
193193
self.sync_all()
194194
assert_equal(self.nodes[2].getbalance(), Decimal('59.99800000')); #should not be changed because tx was not broadcasted
195-
195+
196196
#now broadcast from another node, mine a block, sync, and check the balance
197197
self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex'])
198198
self.nodes[1].generate(1)
199199
self.sync_all()
200200
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted)
201201
assert_equal(self.nodes[2].getbalance(), Decimal('61.99800000')); #should not be
202-
202+
203203
#create another tx
204204
txIdNotBroadcasted = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 2);
205-
205+
206206
#restart the nodes with -walletbroadcast=1
207207
stop_nodes(self.nodes)
208208
wait_bitcoinds()
@@ -211,12 +211,44 @@ def run_test (self):
211211
connect_nodes_bi(self.nodes,1,2)
212212
connect_nodes_bi(self.nodes,0,2)
213213
sync_blocks(self.nodes)
214-
214+
215215
self.nodes[0].generate(1)
216216
sync_blocks(self.nodes)
217-
217+
218218
#tx should be added to balance because after restarting the nodes tx should be broadcastet
219219
assert_equal(self.nodes[2].getbalance(), Decimal('63.99800000')); #should not be
220-
220+
221+
#send a tx with value in a string (PR#6380 +)
222+
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "2")
223+
txObj = self.nodes[0].gettransaction(txId)
224+
assert_equal(txObj['amount'], Decimal('-2.00000000'))
225+
226+
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "0.0001")
227+
txObj = self.nodes[0].gettransaction(txId)
228+
assert_equal(txObj['amount'], Decimal('-0.00010000'))
229+
230+
#check if JSON parser can handle scientific notation in strings
231+
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1e-4")
232+
txObj = self.nodes[0].gettransaction(txId)
233+
assert_equal(txObj['amount'], Decimal('-0.00010000'))
234+
235+
#this should fail
236+
errorString = ""
237+
try:
238+
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), "1f-4")
239+
except JSONRPCException,e:
240+
errorString = e.error['message']
241+
242+
assert_equal("Invalid amount" in errorString, True);
243+
244+
errorString = ""
245+
try:
246+
self.nodes[0].generate("2") #use a string to as block amount parameter must fail because it's not interpreted as amount
247+
except JSONRPCException,e:
248+
errorString = e.error['message']
249+
250+
assert_equal("not an integer" in errorString, True);
251+
252+
221253
if __name__ == '__main__':
222254
WalletTest ().main ()

src/rpcserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ void RPCTypeCheckObj(const UniValue& o,
120120

121121
CAmount AmountFromValue(const UniValue& value)
122122
{
123-
if (!value.isNum())
124-
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number");
123+
if (!value.isNum() && !value.isStr())
124+
throw JSONRPCError(RPC_TYPE_ERROR, "Amount is not a number or string");
125125
CAmount amount;
126126
if (!ParseFixedPoint(value.getValStr(), 8, &amount))
127127
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");

0 commit comments

Comments
 (0)