Skip to content

Commit cda99db

Browse files
committed
Merge ElementsProject#1559: rpc: use null for optional parameters
14a52bf rpc: use null for optional parameters (Ruslan Kasheparov) Pull request description: Fixes: ElementsProject#1558 ACKs for top commit: tomt1664: Tested ACK 14a52bf Tree-SHA512: 161da840bb23c8573dca19b3216e1da03924d65a2ac17075e37c2fb8697994fe9a14b82ee8453f6122692409cb417c5dfe1f726fa0710458c20d207c153a124c
1 parent df752b8 commit cda99db

11 files changed

Lines changed: 56 additions & 13 deletions

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2776,7 +2776,7 @@ static RPCHelpMan scantxoutset()
27762776
throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan already in progress, use action \"abort\" or \"status\"");
27772777
}
27782778

2779-
if (request.params.size() < 2) {
2779+
if (request.params[1].isNull()) {
27802780
throw JSONRPCError(RPC_MISC_ERROR, "scanobjects argument is required for the start action");
27812781
}
27822782

src/rpc/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static RPCHelpMan help()
145145
[&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
146146
{
147147
std::string strCommand;
148-
if (jsonRequest.params.size() > 0) {
148+
if (!jsonRequest.params[0].isNull()) {
149149
strCommand = jsonRequest.params[0].get_str();
150150
}
151151
if (strCommand == "dump_all_command_conversions") {

src/wallet/rpc/elements.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ RPCHelpMan initpegoutwallet()
330330

331331
// Generate a new key that is added to wallet or set from argument
332332
CPubKey online_pubkey;
333-
if (request.params.size() < 3) {
333+
if (request.params[2].isNull()) {
334334
std::string error;
335335
if (!pwallet->GetOnlinePakKey(online_pubkey, error)) {
336336
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error);
@@ -347,7 +347,7 @@ RPCHelpMan initpegoutwallet()
347347

348348
// Parse offline counter
349349
int counter = 0;
350-
if (request.params.size() > 1) {
350+
if (!request.params[1].isNull()) {
351351
counter = request.params[1].get_int();
352352
if (counter < 0 || counter > 1000000000) {
353353
throw JSONRPCError(RPC_INVALID_PARAMETER, "bip32_counter must be between 0 and 1,000,000,000, inclusive.");
@@ -502,7 +502,7 @@ RPCHelpMan sendtomainchain_base()
502502
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
503503

504504
bool subtract_fee = false;
505-
if (request.params.size() > 2) {
505+
if (!request.params[2].isNull()) {
506506
subtract_fee = request.params[2].get_bool();
507507
}
508508

@@ -523,7 +523,7 @@ RPCHelpMan sendtomainchain_base()
523523

524524
EnsureWalletIsUnlocked(*pwallet);
525525

526-
bool verbose = request.params[3].isNull() ? false: request.params[3].get_bool();
526+
bool verbose = request.params[3].isNull() ? false : request.params[3].get_bool();
527527
mapValue_t mapValue;
528528
CCoinControl no_coin_control; // This is a deprecated API
529529
return SendMoney(*pwallet, no_coin_control, recipients, std::move(mapValue), verbose, true /* ignore_blind_fail */);
@@ -615,7 +615,7 @@ RPCHelpMan sendtomainchain_pak()
615615
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount for send, must send more than 0.00100000 BTC");
616616

617617
bool subtract_fee = false;
618-
if (request.params.size() > 2) {
618+
if (!request.params[2].isNull()) {
619619
subtract_fee = request.params[2].get_bool();
620620
}
621621

@@ -827,7 +827,7 @@ static UniValue createrawpegin(const JSONRPCRequest& request, T_tx_ref& txBTCRef
827827
std::vector<unsigned char> txOutProofData = ParseHex(request.params[1].get_str());
828828

829829
std::set<CScript> claim_scripts;
830-
if (request.params.size() > 2) {
830+
if (!request.params[2].isNull()) {
831831
const std::string claim_script = request.params[2].get_str();
832832
if (!IsHex(claim_script)) {
833833
throw JSONRPCError(RPC_INVALID_PARAMETER, "Given claim_script is not hex.");
@@ -1273,12 +1273,12 @@ RPCHelpMan blindrawtransaction()
12731273
}
12741274

12751275
bool ignore_blind_fail = true;
1276-
if (request.params.size() > 1) {
1276+
if (!request.params[1].isNull()) {
12771277
ignore_blind_fail = request.params[1].get_bool();
12781278
}
12791279

12801280
std::vector<std::vector<unsigned char> > auxiliary_generators;
1281-
if (request.params.size() > 2) {
1281+
if (!request.params[2].isNull()) {
12821282
UniValue assetCommitments = request.params[2].get_array();
12831283
if (assetCommitments.size() != 0 && assetCommitments.size() < tx.vin.size()) {
12841284
throw JSONRPCError(RPC_INVALID_PARAMETER, "Asset commitment array must have at least as many entries as transaction inputs.");
@@ -1542,11 +1542,11 @@ RPCHelpMan issueasset()
15421542
throw JSONRPCError(RPC_TYPE_ERROR, "Issuance must have one non-zero component");
15431543
}
15441544

1545-
bool blind_issuances = request.params.size() < 3 || request.params[2].get_bool();
1545+
bool blind_issuances = request.params[2].isNull() || request.params[2].get_bool();
15461546

15471547
// Check for optional contract to hash into definition
15481548
uint256 contract_hash;
1549-
if (request.params.size() >= 4) {
1549+
if (!request.params[3].isNull()) {
15501550
contract_hash = ParseHashV(request.params[3], "contract_hash");
15511551
}
15521552

@@ -1733,7 +1733,7 @@ RPCHelpMan listissuances()
17331733

17341734
std::string assetstr;
17351735
CAsset asset_filter;
1736-
if (request.params.size() > 0) {
1736+
if (!request.params[0].isNull()) {
17371737
assetstr = request.params[0].get_str();
17381738
asset_filter = GetAssetFromString(assetstr);
17391739
}

test/functional/feature_fedpeg.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,12 @@ def run_test(self):
600600

601601
peg_out_txid = sidechain.sendtomainchain(some_btc_addr, 1)
602602

603+
self.log.info("sendtomainchain with null argument")
604+
verbose_result = sidechain.sendtomainchain(some_btc_addr, 1, None, True)
605+
assert isinstance(verbose_result, dict)
606+
assert 'txid' in verbose_result
607+
assert 'fee_reason' in verbose_result
608+
603609
peg_out_details = sidechain.decoderawtransaction(sidechain.getrawtransaction(peg_out_txid))
604610
# peg-out, change, fee
605611
assert len(peg_out_details["vout"]) == 3

test/functional/feature_issuance.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,19 @@ def run_test(self):
112112
# Make sure test starts with no initial issuance.
113113
assert_equal(len(self.nodes[0].listissuances()), 0)
114114

115+
self.log.info("listissuances with null argument")
116+
assert_equal(self.nodes[0].listissuances(None), [])
117+
115118
# Unblinded issuance of asset
116119
contract_hash = "deadbeef"*8
117120
issued = self.nodes[0].issueasset(1, 1, False, contract_hash)
118121
balance = self.nodes[0].getwalletinfo()["balance"]
119122
assert_equal(balance[issued["asset"]], 1)
120123
assert_equal(balance[issued["token"]], 1)
124+
125+
self.log.info("issueasset with null argument")
126+
assert_equal(len(self.nodes[0].listissuances(None)), len(self.nodes[0].listissuances()))
127+
121128
# Quick unblinded reissuance check, making 2*COIN total
122129
self.nodes[0].reissueasset(issued["asset"], 1)
123130

test/functional/feature_pak.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def run_test(self):
7171
assert_equal(new_init["address_lookahead"][0], init_results[1]["address_lookahead"][2])
7272
assert(new_init["liquid_pak"] != init_results[1]["liquid_pak"])
7373

74+
self.log.info("initpegoutwallet with null argument")
75+
null_pak_init = self.nodes[2].initpegoutwallet(xpub, 5, None)
76+
assert_equal(self.nodes[2].getwalletpakinfo()["bip32_counter"], "5")
77+
assert null_pak_init["liquid_pak"]
78+
7479
# Restart and connect peers to check wallet persistence
7580
self.stop_nodes()
7681
self.start_nodes()
@@ -187,6 +192,12 @@ def run_test(self):
187192
wpkh_stmc = self.nodes[1].sendtomainchain("", 1)
188193
wpkh_txid = wpkh_stmc['txid']
189194

195+
self.log.info("sendtomainchain with null argument")
196+
verbose_stmc = self.nodes[1].sendtomainchain("", 1, None, True)
197+
assert isinstance(verbose_stmc, dict)
198+
assert 'txid' in verbose_stmc
199+
assert 'fee_reason' in verbose_stmc
200+
190201
# Also check some basic return fields of sendtomainchain with pak
191202
assert_equal(wpkh_stmc["bitcoin_address"], wpkh_info["address_lookahead"][0])
192203
validata = self.nodes[1].validateaddress(wpkh_stmc["bitcoin_address"])

test/functional/feature_pegin_subsidy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ def parent_pegin(parent, node, amount=1.0, feerate=DEFAULT_FEERATE):
339339
assert_equal(len(pegin_tx["decoded"]["vout"]), 2)
340340
self.generate(sidechain2, 1, sync_fun=sync_sidechain)
341341

342+
self.log.info("createrawpegin with null argument")
343+
txid, vout, txoutproof, bitcoin_txhex, claim_script = parent_pegin(parent, sidechain2, amount=1.0, feerate=2.0)
344+
pegintx = sidechain2.createrawpegin(bitcoin_txhex, txoutproof, None, 2.0)
345+
signed = sidechain2.signrawtransactionwithwallet(pegintx["hex"])
346+
assert_equal(signed["complete"], True)
347+
pegin_txid = sidechain2.sendrawtransaction(signed["hex"])
348+
pegin_tx = sidechain2.gettransaction(pegin_txid, True, True)
349+
assert_equal(len(pegin_tx["decoded"]["vout"]), 2)
350+
self.generate(sidechain2, 1, sync_fun=sync_sidechain)
351+
342352
self.log.info("claimpegin before enforcement, with validatepegin, below threshold")
343353
txid, vout, txoutproof, bitcoin_txhex, claim_script = parent_pegin(parent, sidechain, amount=1.0, feerate=2.0)
344354
pegin_txid = sidechain.claimpegin(bitcoin_txhex, txoutproof, claim_script)

test/functional/rpc_deriveaddresses.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def run_test(self):
1717
descriptor = "wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/0)#t6wfjs64"
1818
address = "ert1qjqmxmkpmxt80xz4y3746zgt0q3u3ferrfpgxn5"
1919
assert_equal(self.nodes[0].deriveaddresses(descriptor), [address])
20+
assert_equal(self.nodes[0].deriveaddresses(descriptor, None), [address])
2021

2122
descriptor = descriptor[:-9]
2223
assert_raises_rpc_error(-5, "Missing checksum", self.nodes[0].deriveaddresses, descriptor)

test/functional/rpc_fundrawtransaction.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ def test_locked_wallet(self):
649649
rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
650650
fundedTx = self.nodes[1].fundrawtransaction(rawtx)
651651
blindedTx = self.nodes[1].blindrawtransaction(fundedTx['hex'])
652+
assert fundedTx["changepos"] != -1
653+
self.log.info("blindrawtransaction with null argument")
654+
blindedTx_null_commitments = self.nodes[1].blindrawtransaction(fundedTx['hex'], None, None, False)
655+
assert blindedTx_null_commitments
652656

653657
# Now we need to unlock.
654658
self.nodes[1].walletpassphrase("test", 600)

test/functional/rpc_help.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def test_categories(self):
9494
# invalid argument
9595
assert_raises_rpc_error(-1, 'JSON value is not a string as expected', node.help, 0)
9696

97+
# null argument
98+
assert_equal(node.help(None), node.help())
99+
97100
# help of unknown command
98101
assert_equal(node.help('foo'), 'help: unknown command: foo')
99102

0 commit comments

Comments
 (0)