Skip to content

backport: Merge bitcoin#23083 , 23662 #6716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions src/rpc/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
// Elements in a JSON structure (dictionary or array) are separated by a comma
const std::string maybe_separator{outer_type != OuterType::NONE ? "," : ""};

// The key name if recursed into an dictionary
// The key name if recursed into a dictionary
const std::string maybe_key{
outer_type == OuterType::OBJ ?
"\"" + this->m_key_name + "\" : " :
Expand Down Expand Up @@ -816,10 +816,11 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const

bool RPCResult::MatchesType(const UniValue& result) const
{
switch (m_type) {
case Type::ELISION: {
return false;
if (m_skip_type_check) {
return true;
}
switch (m_type) {
case Type::ELISION:
case Type::ANY: {
return true;
}
Expand All @@ -840,11 +841,52 @@ bool RPCResult::MatchesType(const UniValue& result) const
}
case Type::ARR_FIXED:
case Type::ARR: {
return UniValue::VARR == result.getType();
if (UniValue::VARR != result.getType()) return false;
for (size_t i{0}; i < result.get_array().size(); ++i) {
// If there are more results than documented, re-use the last doc_inner.
const RPCResult& doc_inner{m_inner.at(std::min(m_inner.size() - 1, i))};
if (!doc_inner.MatchesType(result.get_array()[i])) return false;
}
return true; // empty result array is valid
}
case Type::OBJ_DYN:
case Type::OBJ: {
return UniValue::VOBJ == result.getType();
if (UniValue::VOBJ != result.getType()) return false;
if (!m_inner.empty() && m_inner.at(0).m_type == Type::ELISION) return true;
if (m_type == Type::OBJ_DYN) {
const RPCResult& doc_inner{m_inner.at(0)}; // Assume all types are the same, randomly pick the first
for (size_t i{0}; i < result.get_obj().size(); ++i) {
if (!doc_inner.MatchesType(result.get_obj()[i])) {
return false;
}
}
return true; // empty result obj is valid
}
std::set<std::string> doc_keys;
for (const auto& doc_entry : m_inner) {
doc_keys.insert(doc_entry.m_key_name);
}
std::map<std::string, UniValue> result_obj;
result.getObjMap(result_obj);
for (const auto& result_entry : result_obj) {
if (doc_keys.find(result_entry.first) == doc_keys.end()) {
return false; // missing documentation
}
}

for (const auto& doc_entry : m_inner) {
const auto result_it{result_obj.find(doc_entry.m_key_name)};
if (result_it == result_obj.end()) {
if (!doc_entry.m_optional) {
return false; // result is missing a required key
}
continue;
}
if (!doc_entry.MatchesType(result_it->second)) {
return false; // wrong type
}
}
return true;
}
} // no default case, so the compiler can warn about missing cases
NONFATAL_UNREACHABLE();
Expand Down
11 changes: 8 additions & 3 deletions src/rpc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ struct RPCResult {
const std::string m_key_name; //!< Only used for dicts
const std::vector<RPCResult> m_inner; //!< Only used for arrays or dicts
const bool m_optional;
const bool m_skip_type_check;
const std::string m_description;
const std::string m_cond;

Expand All @@ -273,6 +274,7 @@ struct RPCResult {
m_key_name{std::move(m_key_name)},
m_inner{std::move(inner)},
m_optional{optional},
m_skip_type_check{false},
m_description{std::move(description)},
m_cond{std::move(cond)}
{
Expand All @@ -293,11 +295,13 @@ struct RPCResult {
const std::string m_key_name,
const bool optional,
const std::string description,
const std::vector<RPCResult> inner = {})
const std::vector<RPCResult> inner = {},
bool skip_type_check = false)
: m_type{std::move(type)},
m_key_name{std::move(m_key_name)},
m_inner{std::move(inner)},
m_optional{optional},
m_skip_type_check{skip_type_check},
m_description{std::move(description)},
m_cond{}
{
Expand All @@ -308,8 +312,9 @@ struct RPCResult {
const Type type,
const std::string m_key_name,
const std::string description,
const std::vector<RPCResult> inner = {})
: RPCResult{type, m_key_name, false, description, inner} {}
const std::vector<RPCResult> inner = {},
bool skip_type_check = false)
: RPCResult{type, m_key_name, false, description, inner, skip_type_check} {}

/** Append the sections of the result. */
void ToSections(Sections& sections, OuterType outer_type = OuterType::NONE, const int current_indent = 0) const;
Expand Down
14 changes: 9 additions & 5 deletions src/wallet/rpc/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@

static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
{
std::vector<CTxDestination> address_vector;
std::vector<CScript> output_scripts;

if (by_label) {
// Get the set of addresses assigned to label
std::string label = LabelFromValue(params[0]);
address_vector = wallet.ListAddrBookAddresses(CWallet::AddrBookFilter{label});
for (const auto& address : wallet.ListAddrBookAddresses(CWallet::AddrBookFilter{label})) {
auto output_script{GetScriptForDestination(address)};
if (wallet.IsMine(output_script)) {
output_scripts.insert(output_script);
}
}
} else {
// Get the address
CTxDestination dest = DecodeDestination(params[0].get_str());
Expand All @@ -32,7 +37,7 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool b
if (!wallet.IsMine(script_pub_key)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
}
address_vector.emplace_back(dest);
output_scripts.insert(script_pub_key);
}

// Minimum confirmations
Expand Down Expand Up @@ -65,8 +70,7 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool b
if (depth < min_depth && !(fAddLocked && wallet.IsTxLockedByInstantSend(wtx))) continue;

for (const CTxOut& txout : wtx.tx->vout) {
CTxDestination address;
if (ExtractDestination(txout.scriptPubKey, address) && wallet.IsMine(address) && std::find(address_vector.begin(), address_vector.end(), address) != address_vector.end()) {
if (output_scripts.count(txout.scriptPubKey) > 0) {
amount += txout.nValue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpc/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static RPCHelpMan getwalletinfo()
{
{RPCResult::Type::NUM, "duration", "elapsed seconds since scan start"},
{RPCResult::Type::NUM, "progress", "scanning progress percentage [0.0, 1.0]"},
}},
}, /*skip_type_check=*/true},
{RPCResult::Type::BOOL, "descriptors", "whether this wallet uses descriptors for scriptPubKey management"},
},
},
Expand Down
Loading