Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#25629: univalue: Return more detailed type chec…
Browse files Browse the repository at this point in the history
…k error messages

fae5ce8 univalue: Return more detailed type check error messages (MacroFake)
fafab14 move-only: Move UniValue::getInt definition to keep class with definitions only (MacroFake)

Pull request description:

  Print the current type and the expected type

ACKs for top commit:
  aureleoules:
    ACK fae5ce8.

Tree-SHA512: 4ae720a012ff8245baf5cd7f844f93b946c58feebe62de6dfd84ebc5c8afb988295a94de7c01aef98aaf4c6228f7184ed622f37079c738924617e0f336ac5b6e
  • Loading branch information
fanquake committed Jul 19, 2022
2 parents 6d8707b + fae5ce8 commit 47dad42
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 50 deletions.
30 changes: 16 additions & 14 deletions src/univalue/include/univalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class UniValue {
std::vector<std::string> keys;
std::vector<UniValue> values;

void checkType(const VType& expected) const;
bool findKey(const std::string& key, size_t& retIdx) const;
void writeArray(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
void writeObject(unsigned int prettyIndent, unsigned int indentLevel, std::string& s) const;
Expand All @@ -116,19 +117,7 @@ class UniValue {
const std::vector<std::string>& getKeys() const;
const std::vector<UniValue>& getValues() const;
template <typename Int>
auto getInt() const
{
static_assert(std::is_integral<Int>::value);
if (typ != VNUM) {
throw std::runtime_error("JSON value is not an integer as expected");
}
Int result;
const auto [first_nonmatching, error_condition] = std::from_chars(val.data(), val.data() + val.size(), result);
if (first_nonmatching != val.data() + val.size() || error_condition != std::errc{}) {
throw std::runtime_error("JSON integer out of range");
}
return result;
}
Int getInt() const;
bool get_bool() const;
const std::string& get_str() const;
double get_real() const;
Expand All @@ -142,10 +131,23 @@ class UniValue {
template <class It>
void UniValue::push_backV(It first, It last)
{
if (typ != VARR) throw std::runtime_error{"JSON value is not an array as expected"};
checkType(VARR);
values.insert(values.end(), first, last);
}

template <typename Int>
Int UniValue::getInt() const
{
static_assert(std::is_integral<Int>::value);
checkType(VNUM);
Int result;
const auto [first_nonmatching, error_condition] = std::from_chars(val.data(), val.data() + val.size(), result);
if (first_nonmatching != val.data() + val.size() || error_condition != std::errc{}) {
throw std::runtime_error("JSON integer out of range");
}
return result;
}

enum jtokentype {
JTOK_ERR = -1,
JTOK_NONE = 0, // eof
Expand Down
19 changes: 14 additions & 5 deletions src/univalue/lib/univalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,29 @@ bool UniValue::setObject()

void UniValue::push_back(const UniValue& val_)
{
if (typ != VARR) throw std::runtime_error{"JSON value is not an array as expected"};
checkType(VARR);

values.push_back(val_);
}

void UniValue::push_backV(const std::vector<UniValue>& vec)
{
if (typ != VARR) throw std::runtime_error{"JSON value is not an array as expected"};
checkType(VARR);

values.insert(values.end(), vec.begin(), vec.end());
}

void UniValue::__pushKV(const std::string& key, const UniValue& val_)
{
if (typ != VOBJ) throw std::runtime_error{"JSON value is not an object as expected"};
checkType(VOBJ);

keys.push_back(key);
values.push_back(val_);
}

void UniValue::pushKV(const std::string& key, const UniValue& val_)
{
if (typ != VOBJ) throw std::runtime_error{"JSON value is not an object as expected"};
checkType(VOBJ);

size_t idx;
if (findKey(key, idx))
Expand All @@ -143,7 +143,8 @@ void UniValue::pushKV(const std::string& key, const UniValue& val_)

void UniValue::pushKVs(const UniValue& obj)
{
if (typ != VOBJ || obj.typ != VOBJ) throw std::runtime_error{"JSON value is not an object as expected"};
checkType(VOBJ);
obj.checkType(VOBJ);

for (size_t i = 0; i < obj.keys.size(); i++)
__pushKV(obj.keys[i], obj.values.at(i));
Expand Down Expand Up @@ -213,6 +214,14 @@ const UniValue& UniValue::operator[](size_t index) const
return values.at(index);
}

void UniValue::checkType(const VType& expected) const
{
if (typ != expected) {
throw std::runtime_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
std::string{uvTypeName(expected)}};
}
}

const char *uvTypeName(UniValue::VType t)
{
switch (t) {
Expand Down
19 changes: 6 additions & 13 deletions src/univalue/lib/univalue_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ bool ParseDouble(const std::string& str, double *out)

const std::vector<std::string>& UniValue::getKeys() const
{
if (typ != VOBJ)
throw std::runtime_error("JSON value is not an object as expected");
checkType(VOBJ);
return keys;
}

Expand All @@ -60,22 +59,19 @@ const std::vector<UniValue>& UniValue::getValues() const

bool UniValue::get_bool() const
{
if (typ != VBOOL)
throw std::runtime_error("JSON value is not a boolean as expected");
checkType(VBOOL);
return getBool();
}

const std::string& UniValue::get_str() const
{
if (typ != VSTR)
throw std::runtime_error("JSON value is not a string as expected");
checkType(VSTR);
return getValStr();
}

double UniValue::get_real() const
{
if (typ != VNUM)
throw std::runtime_error("JSON value is not a number as expected");
checkType(VNUM);
double retval;
if (!ParseDouble(getValStr(), &retval))
throw std::runtime_error("JSON double out of range");
Expand All @@ -84,15 +80,12 @@ double UniValue::get_real() const

const UniValue& UniValue::get_obj() const
{
if (typ != VOBJ)
throw std::runtime_error("JSON value is not an object as expected");
checkType(VOBJ);
return *this;
}

const UniValue& UniValue::get_array() const
{
if (typ != VARR)
throw std::runtime_error("JSON value is not an array as expected");
checkType(VARR);
return *this;
}

4 changes: 2 additions & 2 deletions test/functional/mining_prioritisetransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def run_test(self):

# Test `prioritisetransaction` invalid `dummy`
txid = '1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000'
assert_raises_rpc_error(-1, "JSON value is not a number as expected", self.nodes[0].prioritisetransaction, txid, 'foo', 0)
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type number", self.nodes[0].prioritisetransaction, txid, 'foo', 0)
assert_raises_rpc_error(-8, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.", self.nodes[0].prioritisetransaction, txid, 1, 0)

# Test `prioritisetransaction` invalid `fee_delta`
assert_raises_rpc_error(-1, "JSON value is not an integer as expected", self.nodes[0].prioritisetransaction, txid=txid, fee_delta='foo')
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type number", self.nodes[0].prioritisetransaction, txid=txid, fee_delta='foo')

self.test_diamond()

Expand Down
6 changes: 3 additions & 3 deletions test/functional/rpc_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ def _test_getchaintxstats(self):
assert_raises_rpc_error(-1, 'getchaintxstats', self.nodes[0].getchaintxstats, 0, '', 0)

# Test `getchaintxstats` invalid `nblocks`
assert_raises_rpc_error(-1, "JSON value is not an integer as expected", self.nodes[0].getchaintxstats, '')
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type number", self.nodes[0].getchaintxstats, '')
assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, -1)
assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, self.nodes[0].getblockcount())

# Test `getchaintxstats` invalid `blockhash`
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].getchaintxstats, blockhash=0)
assert_raises_rpc_error(-1, "JSON value of type number is not of expected type string", self.nodes[0].getchaintxstats, blockhash=0)
assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 1, for '0')", self.nodes[0].getchaintxstats, blockhash='0')
assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].getchaintxstats, blockhash='ZZZ0000000000000000000000000000000000000000000000000000000000000')
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getchaintxstats, blockhash='0000000000000000000000000000000000000000000000000000000000000000')
Expand Down Expand Up @@ -536,7 +536,7 @@ def assert_vin_does_not_contain_prevout(verbosity):
datadir = get_datadir_path(self.options.tmpdir, 0)

self.log.info("Test getblock with invalid verbosity type returns proper error message")
assert_raises_rpc_error(-1, "JSON value is not an integer as expected", node.getblock, blockhash, "2")
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type number", node.getblock, blockhash, "2")

def move_block_file(old, new):
old_path = os.path.join(datadir, self.chain, 'blocks', old)
Expand Down
2 changes: 1 addition & 1 deletion test/functional/rpc_fundrawtransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_change_type(self):
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[2].fundrawtransaction, rawtx, {'change_type': None})
assert_raises_rpc_error(-1, "JSON value of type null is not of expected type string", self.nodes[2].fundrawtransaction, rawtx, {'change_type': None})
assert_raises_rpc_error(-5, "Unknown change type ''", self.nodes[2].fundrawtransaction, rawtx, {'change_type': ''})
rawtx = self.nodes[2].fundrawtransaction(rawtx, {'change_type': 'bech32'})
dec_tx = self.nodes[2].decoderawtransaction(rawtx['hex'])
Expand Down
2 changes: 1 addition & 1 deletion test/functional/rpc_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_categories(self):
assert_raises_rpc_error(-1, 'help', node.help, 'foo', 'bar')

# invalid argument
assert_raises_rpc_error(-1, 'JSON value is not a string as expected', node.help, 0)
assert_raises_rpc_error(-1, "JSON value of type number is not of expected type string", node.help, 0)

# help of unknown command
assert_equal(node.help('foo'), 'help: unknown command: foo')
Expand Down
14 changes: 7 additions & 7 deletions test/functional/rpc_rawtransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ def getrawtransaction_tests(self):

# 6. invalid parameters - supply txid and invalid boolean values (strings) for verbose
for value in ["True", "False"]:
assert_raises_rpc_error(-1, "not a boolean", self.nodes[n].getrawtransaction, txid=txId, verbose=value)
assert_raises_rpc_error(-1, "not of expected type bool", self.nodes[n].getrawtransaction, txid=txId, verbose=value)

# 7. invalid parameters - supply txid and empty array
assert_raises_rpc_error(-1, "not a boolean", self.nodes[n].getrawtransaction, txId, [])
assert_raises_rpc_error(-1, "not of expected type bool", self.nodes[n].getrawtransaction, txId, [])

# 8. invalid parameters - supply txid and empty dict
assert_raises_rpc_error(-1, "not a boolean", self.nodes[n].getrawtransaction, txId, {})
assert_raises_rpc_error(-1, "not of expected type bool", self.nodes[n].getrawtransaction, txId, {})

# Make a tx by sending, then generate 2 blocks; block1 has the tx in it
tx = self.wallet.send_self_transfer(from_node=self.nodes[2])['txid']
Expand All @@ -152,7 +152,7 @@ def getrawtransaction_tests(self):
# We should not get the tx if we provide an unrelated block
assert_raises_rpc_error(-5, "No such transaction found", self.nodes[n].getrawtransaction, txid=tx, blockhash=block2)
# An invalid block hash should raise the correct errors
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[n].getrawtransaction, txid=tx, blockhash=True)
assert_raises_rpc_error(-1, "JSON value of type bool is not of expected type string", self.nodes[n].getrawtransaction, txid=tx, blockhash=True)
assert_raises_rpc_error(-8, "parameter 3 must be of length 64 (not 6, for 'foobar')", self.nodes[n].getrawtransaction, txid=tx, blockhash="foobar")
assert_raises_rpc_error(-8, "parameter 3 must be of length 64 (not 8, for 'abcd1234')", self.nodes[n].getrawtransaction, txid=tx, blockhash="abcd1234")
foo = "ZZZ0000000000000000000000000000000000000000000000000000000000000"
Expand Down Expand Up @@ -181,8 +181,8 @@ def createrawtransaction_tests(self):

# Test `createrawtransaction` invalid `inputs`
assert_raises_rpc_error(-3, "Expected type array", self.nodes[0].createrawtransaction, 'foo', {})
assert_raises_rpc_error(-1, "JSON value is not an object as expected", self.nodes[0].createrawtransaction, ['foo'], {})
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].createrawtransaction, [{}], {})
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type object", self.nodes[0].createrawtransaction, ['foo'], {})
assert_raises_rpc_error(-1, "JSON value of type null is not of expected type string", self.nodes[0].createrawtransaction, [{}], {})
assert_raises_rpc_error(-8, "txid must be of length 64 (not 3, for 'foo')", self.nodes[0].createrawtransaction, [{'txid': 'foo'}], {})
txid = "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844"
assert_raises_rpc_error(-8, f"txid must be hexadecimal string (not '{txid}')", self.nodes[0].createrawtransaction, [{'txid': txid}], {})
Expand All @@ -207,7 +207,7 @@ def createrawtransaction_tests(self):

# Test `createrawtransaction` invalid `outputs`
address = getnewdestination()[2]
assert_raises_rpc_error(-1, "JSON value is not an array as expected", self.nodes[0].createrawtransaction, [], 'foo')
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type array", self.nodes[0].createrawtransaction, [], 'foo')
self.nodes[0].createrawtransaction(inputs=[], outputs={}) # Should not throw for backwards compatibility
self.nodes[0].createrawtransaction(inputs=[], outputs=[])
assert_raises_rpc_error(-8, "Data must be hexadecimal string", self.nodes[0].createrawtransaction, [], {'data': 'foo'})
Expand Down
2 changes: 1 addition & 1 deletion test/functional/wallet_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def run_test(self):
assert_raises_rpc_error(-3, "Invalid amount", self.nodes[0].sendtoaddress, self.nodes[2].getnewaddress(), "1f-4")

# This will raise an exception since generate does not accept a string
assert_raises_rpc_error(-1, "not an integer", self.generate, self.nodes[0], "2")
assert_raises_rpc_error(-1, "not of expected type number", self.generate, self.nodes[0], "2")

if not self.options.descriptors:

Expand Down
4 changes: 2 additions & 2 deletions test/functional/wallet_hd.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def run_test(self):
# Sethdseed parameter validity
assert_raises_rpc_error(-1, 'sethdseed', self.nodes[0].sethdseed, False, new_seed, 0)
assert_raises_rpc_error(-5, "Invalid private key", self.nodes[1].sethdseed, False, "not_wif")
assert_raises_rpc_error(-1, "JSON value is not a boolean as expected", self.nodes[1].sethdseed, "Not_bool")
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[1].sethdseed, False, True)
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type bool", self.nodes[1].sethdseed, "Not_bool")
assert_raises_rpc_error(-1, "JSON value of type bool is not of expected type string", self.nodes[1].sethdseed, False, True)
assert_raises_rpc_error(-5, "Already have this key", self.nodes[1].sethdseed, False, new_seed)
assert_raises_rpc_error(-5, "Already have this key", self.nodes[1].sethdseed, False, self.nodes[1].dumpprivkey(self.nodes[1].getnewaddress()))

Expand Down
2 changes: 1 addition & 1 deletion test/functional/wallet_multiwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def wallet_file(name):
self.log.info("Test dynamic wallet unloading")

# Test `unloadwallet` errors
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].unloadwallet)
assert_raises_rpc_error(-1, "JSON value of type null is not of expected type string", self.nodes[0].unloadwallet)
assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", self.nodes[0].unloadwallet, "dummy")
assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", node.get_wallet_rpc("dummy").unloadwallet)
assert_raises_rpc_error(-8, "RPC endpoint wallet and wallet_name parameter specify different wallets", w1.unloadwallet, "w2"),
Expand Down

0 comments on commit 47dad42

Please sign in to comment.