Skip to content

Commit 14a52bf

Browse files
rpc: use null for optional parameters
1 parent 8806605 commit 14a52bf

12 files changed

Lines changed: 56 additions & 14 deletions

src/rpc/blockchain.cpp

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

2529-
if (request.params.size() < 2) {
2529+
if (request.params[1].isNull()) {
25302530
throw JSONRPCError(RPC_MISC_ERROR, "scanobjects argument is required for the start action");
25312531
}
25322532

src/rpc/output_script.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ static RPCHelpMan deriveaddresses()
353353
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
354354
}
355355
auto& desc = descs.at(0);
356-
if (!desc->IsRange() && request.params.size() > 1) {
356+
if (!desc->IsRange() && !request.params[1].isNull()) {
357357
throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor");
358358
}
359359

src/rpc/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static RPCHelpMan help()
139139
[&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue
140140
{
141141
std::string strCommand;
142-
if (jsonRequest.params.size() > 0) {
142+
if (!jsonRequest.params[0].isNull()) {
143143
strCommand = jsonRequest.params[0].get_str();
144144
}
145145
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
@@ -325,7 +325,7 @@ RPCHelpMan initpegoutwallet()
325325

326326
// Generate a new key that is added to wallet or set from argument
327327
CPubKey online_pubkey;
328-
if (request.params.size() < 3) {
328+
if (request.params[2].isNull()) {
329329
std::string error;
330330
if (!pwallet->GetOnlinePakKey(online_pubkey, error)) {
331331
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error);
@@ -342,7 +342,7 @@ RPCHelpMan initpegoutwallet()
342342

343343
// Parse offline counter
344344
int counter = 0;
345-
if (request.params.size() > 1) {
345+
if (!request.params[1].isNull()) {
346346
counter = request.params[1].getInt<int>();
347347
if (counter < 0 || counter > 1000000000) {
348348
throw JSONRPCError(RPC_INVALID_PARAMETER, "bip32_counter must be between 0 and 1,000,000,000, inclusive.");
@@ -499,7 +499,7 @@ RPCHelpMan sendtomainchain_base()
499499
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
500500

501501
bool subtract_fee = false;
502-
if (request.params.size() > 2) {
502+
if (!request.params[2].isNull()) {
503503
subtract_fee = request.params[2].get_bool();
504504
}
505505

@@ -520,7 +520,7 @@ RPCHelpMan sendtomainchain_base()
520520

521521
EnsureWalletIsUnlocked(*pwallet);
522522

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

614614
bool subtract_fee = false;
615-
if (request.params.size() > 2) {
615+
if (!request.params[2].isNull()) {
616616
subtract_fee = request.params[2].get_bool();
617617
}
618618

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

827827
std::set<CScript> claim_scripts;
828-
if (request.params.size() > 2) {
828+
if (!request.params[2].isNull()) {
829829
const std::string claim_script = request.params[2].get_str();
830830
if (!IsHex(claim_script)) {
831831
throw JSONRPCError(RPC_INVALID_PARAMETER, "Given claim_script is not hex.");
@@ -1270,12 +1270,12 @@ RPCHelpMan blindrawtransaction()
12701270
}
12711271

12721272
bool ignore_blind_fail = true;
1273-
if (request.params.size() > 1) {
1273+
if (!request.params[1].isNull()) {
12741274
ignore_blind_fail = request.params[1].get_bool();
12751275
}
12761276

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

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

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

@@ -1739,7 +1739,7 @@ RPCHelpMan listissuances()
17391739

17401740
std::string assetstr;
17411741
CAsset asset_filter;
1742-
if (request.params.size() > 0) {
1742+
if (!request.params[0].isNull()) {
17431743
assetstr = request.params[0].get_str();
17441744
asset_filter = GetAssetFromString(assetstr);
17451745
}

test/functional/feature_fedpeg.py

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

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

604+
self.log.info("sendtomainchain with null argument")
605+
verbose_result = sidechain.sendtomainchain(some_btc_addr, 1, None, True)
606+
assert isinstance(verbose_result, dict)
607+
assert 'txid' in verbose_result
608+
assert 'fee_reason' in verbose_result
609+
604610
peg_out_details = sidechain.decoderawtransaction(sidechain.getrawtransaction(peg_out_txid))
605611
# peg-out, change, fee
606612
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
@@ -115,12 +115,19 @@ def run_test(self):
115115
# Make sure test starts with no initial issuance.
116116
assert_equal(len(self.nodes[0].listissuances()), 0)
117117

118+
self.log.info("listissuances with null argument")
119+
assert_equal(self.nodes[0].listissuances(None), [])
120+
118121
# Unblinded issuance of asset
119122
contract_hash = "deadbeef"*8
120123
issued = self.nodes[0].issueasset(1, 1, False, contract_hash)
121124
balance = self.nodes[0].getwalletinfo()["balance"]
122125
assert_equal(balance[issued["asset"]], 1)
123126
assert_equal(balance[issued["token"]], 1)
127+
128+
self.log.info("issueasset with null argument")
129+
assert_equal(len(self.nodes[0].listissuances(None)), len(self.nodes[0].listissuances()))
130+
124131
# Quick unblinded reissuance check, making 2*COIN total
125132
self.nodes[0].reissueasset(issued["asset"], 1)
126133

test/functional/feature_pak.py

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

79+
self.log.info("initpegoutwallet with null argument")
80+
null_pak_init = self.nodes[2].initpegoutwallet(xpub, 5, None)
81+
assert_equal(self.nodes[2].getwalletpakinfo()["bip32_counter"], "5")
82+
assert null_pak_init["liquid_pak"]
83+
7984
# Restart and connect peers to check wallet persistence
8085
self.stop_nodes()
8186
self.start_nodes()
@@ -192,6 +197,12 @@ def run_test(self):
192197
wpkh_stmc = self.nodes[1].sendtomainchain("", 1)
193198
wpkh_txid = wpkh_stmc['txid']
194199

200+
self.log.info("sendtomainchain with null argument")
201+
verbose_stmc = self.nodes[1].sendtomainchain("", 1, None, True)
202+
assert isinstance(verbose_stmc, dict)
203+
assert 'txid' in verbose_stmc
204+
assert 'fee_reason' in verbose_stmc
205+
195206
# Also check some basic return fields of sendtomainchain with pak
196207
assert_equal(wpkh_stmc["bitcoin_address"], wpkh_info["address_lookahead"][0])
197208
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
@@ -340,6 +340,16 @@ def parent_pegin(parent, node, amount=1.0, feerate=DEFAULT_FEERATE):
340340
assert_equal(len(pegin_tx["decoded"]["vout"]), 2)
341341
self.generate(sidechain2, 1, sync_fun=sync_sidechain)
342342

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def test_categories(self):
9797
# invalid argument
9898
assert_raises_rpc_error(-3, "JSON value of type number is not of expected type string", node.help, 0)
9999

100+
# null argument
101+
assert_equal(node.help(None), node.help())
102+
100103
# help of unknown command
101104
assert_equal(node.help('foo'), 'help: unknown command: foo')
102105

0 commit comments

Comments
 (0)