Skip to content

Commit d4991c0

Browse files
committed
Merge #11877: Improve createrawtransaction functional tests
88af502 test: Add createrawtransaction functional tests (João Barbosa) 27c6199 test: Add multidict to support dictionary with duplicate key (laanwj) (João Barbosa) 320669a rpc: Validate replaceable type in createrawtransaction (João Barbosa) Pull request description: This was motivated by the `Invalid parameter, duplicated address` test. Credit to @laanwj for `multidict` implementation. Tree-SHA512: a87139ae11004b73b467db1e8a072b75e23a0622b173a5668eed383b3575d8abc709817ddd2dfdc53f55afc90750fb61331199ad5de38c1ef6d482f2bc220f74
2 parents 68e021e + 88af502 commit d4991c0

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ UniValue createrawtransaction(const JSONRPCRequest& request)
349349
+ HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"data\\\":\\\"00010203\\\"}\"")
350350
);
351351

352-
RPCTypeCheck(request.params, {UniValue::VARR, UniValue::VOBJ, UniValue::VNUM}, true);
352+
RPCTypeCheck(request.params, {UniValue::VARR, UniValue::VOBJ, UniValue::VNUM, UniValue::VBOOL}, true);
353353
if (request.params[0].isNull() || request.params[1].isNull())
354354
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, arguments 1 and 2 must be non-null");
355355

test/functional/rawtransactions.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@
1515
from test_framework.test_framework import BitcoinTestFramework
1616
from test_framework.util import *
1717

18+
19+
class multidict(dict):
20+
"""Dictionary that allows duplicate keys.
21+
22+
Constructed with a list of (key, value) tuples. When dumped by the json module,
23+
will output invalid json with repeated keys, eg:
24+
>>> json.dumps(multidict([(1,2),(1,2)])
25+
'{"1": 2, "1": 2}'
26+
27+
Used to test calls to rpc methods with repeated keys in the json object."""
28+
29+
def __init__(self, x):
30+
dict.__init__(self, x)
31+
self.x = x
32+
33+
def items(self):
34+
return self.x
35+
36+
1837
# Create one-input, one-output, no-fee transaction:
1938
class RawTransactionsTest(BitcoinTestFramework):
2039
def set_test_params(self):
@@ -39,6 +58,41 @@ def run_test(self):
3958
self.nodes[0].generate(5)
4059
self.sync_all()
4160

61+
# Test `createrawtransaction` required parameters
62+
assert_raises_rpc_error(-1, "createrawtransaction", self.nodes[0].createrawtransaction)
63+
assert_raises_rpc_error(-1, "createrawtransaction", self.nodes[0].createrawtransaction, [])
64+
65+
# Test `createrawtransaction` invalid extra parameters
66+
assert_raises_rpc_error(-1, "createrawtransaction", self.nodes[0].createrawtransaction, [], {}, 0, False, 'foo')
67+
68+
# Test `createrawtransaction` invalid `inputs`
69+
txid = '1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000'
70+
assert_raises_rpc_error(-3, "Expected type array", self.nodes[0].createrawtransaction, 'foo', {})
71+
assert_raises_rpc_error(-1, "JSON value is not an object as expected", self.nodes[0].createrawtransaction, ['foo'], {})
72+
assert_raises_rpc_error(-8, "txid must be hexadecimal string", self.nodes[0].createrawtransaction, [{}], {})
73+
assert_raises_rpc_error(-8, "txid must be hexadecimal string", self.nodes[0].createrawtransaction, [{'txid': 'foo'}], {})
74+
assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", self.nodes[0].createrawtransaction, [{'txid': txid}], {})
75+
assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", self.nodes[0].createrawtransaction, [{'txid': txid, 'vout': 'foo'}], {})
76+
assert_raises_rpc_error(-8, "Invalid parameter, vout must be positive", self.nodes[0].createrawtransaction, [{'txid': txid, 'vout': -1}], {})
77+
assert_raises_rpc_error(-8, "Invalid parameter, sequence number is out of range", self.nodes[0].createrawtransaction, [{'txid': txid, 'vout': 0, 'sequence': -1}], {})
78+
79+
# Test `createrawtransaction` invalid `outputs`
80+
address = self.nodes[0].getnewaddress()
81+
assert_raises_rpc_error(-3, "Expected type object", self.nodes[0].createrawtransaction, [], 'foo')
82+
assert_raises_rpc_error(-8, "Data must be hexadecimal string", self.nodes[0].createrawtransaction, [], {'data': 'foo'})
83+
assert_raises_rpc_error(-5, "Invalid Bitcoin address", self.nodes[0].createrawtransaction, [], {'foo': 0})
84+
assert_raises_rpc_error(-3, "Invalid amount", self.nodes[0].createrawtransaction, [], {address: 'foo'})
85+
assert_raises_rpc_error(-3, "Amount out of range", self.nodes[0].createrawtransaction, [], {address: -1})
86+
assert_raises_rpc_error(-8, "Invalid parameter, duplicated address: %s" % address, self.nodes[0].createrawtransaction, [], multidict([(address, 1), (address, 1)]))
87+
88+
# Test `createrawtransaction` invalid `locktime`
89+
assert_raises_rpc_error(-3, "Expected type number", self.nodes[0].createrawtransaction, [], {}, 'foo')
90+
assert_raises_rpc_error(-8, "Invalid parameter, locktime out of range", self.nodes[0].createrawtransaction, [], {}, -1)
91+
assert_raises_rpc_error(-8, "Invalid parameter, locktime out of range", self.nodes[0].createrawtransaction, [], {}, 4294967296)
92+
93+
# Test `createrawtransaction` invalid `replaceable`
94+
assert_raises_rpc_error(-3, "Expected type bool", self.nodes[0].createrawtransaction, [], {}, 0, 'foo')
95+
4296
#########################################
4397
# sendrawtransaction with missing input #
4498
#########################################

0 commit comments

Comments
 (0)