Skip to content

Commit 03bca03

Browse files
authored
Merge pull request dashpay#4331 from pravblockc/backports-v0.18-pr10
Backports v0.18: PR's 14530 and 14365
2 parents 297cdf9 + 12047d7 commit 03bca03

28 files changed

+392
-102
lines changed

.travis/lint_04_install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export LC_ALL=C
88

99
travis_retry pip install codespell==1.13.0
1010
travis_retry pip install flake8==3.5.0
11+
travis_retry pip install vulture==0.29

ci/Dockerfile.builder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RUN pip3 install pyzmq # really needed?
1717
RUN pip3 install jinja2
1818
RUN pip3 install flake8==3.5.0
1919
RUN pip3 install codespell==1.13.0
20+
RUN pip3 install vulture==0.29
2021

2122
# dash_hash
2223
RUN git clone https://github.com/dashpay/dash_hash

src/rpc/blockchain.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,25 @@
1515
#include <coins.h>
1616
#include <node/coinstats.h>
1717
#include <core_io.h>
18+
#include <hash.h>
1819
#include <consensus/validation.h>
1920
#include <index/blockfilterindex.h>
2021
#include <key_io.h>
21-
#include <validation.h>
2222
#include <index/txindex.h>
2323
#include <policy/feerate.h>
2424
#include <policy/policy.h>
2525
#include <primitives/transaction.h>
2626
#include <rpc/server.h>
27+
#include <rpc/util.h>
2728
#include <script/descriptor.h>
2829
#include <streams.h>
2930
#include <sync.h>
3031
#include <txdb.h>
3132
#include <txmempool.h>
32-
#include <util/system.h>
3333
#include <util/strencodings.h>
3434
#include <util/validation.h>
35-
#include <hash.h>
35+
#include <util/system.h>
36+
#include <validation.h>
3637
#include <validationinterface.h>
3738
#include <warnings.h>
3839

src/rpc/mining.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <rpc/blockchain.h>
2020
#include <rpc/mining.h>
2121
#include <rpc/server.h>
22+
#include <rpc/util.h>
2223
#include <shutdown.h>
2324
#include <txmempool.h>
2425
#include <util/fees.h>

src/rpc/net.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99
#include <chainparams.h>
1010
#include <clientversion.h>
1111
#include <core_io.h>
12-
#include <validation.h>
1312
#include <net.h>
1413
#include <net_processing.h>
1514
#include <net_permissions.h>
1615
#include <netbase.h>
1716
#include <policy/policy.h>
1817
#include <rpc/protocol.h>
18+
#include <rpc/util.h>
1919
#include <sync.h>
2020
#include <timedata.h>
2121
#include <ui_interface.h>
22-
#include <util/system.h>
2322
#include <util/strencodings.h>
23+
#include <util/system.h>
24+
#include <validation.h>
2425
#include <version.h>
2526
#include <warnings.h>
2627

src/rpc/rawtransaction.cpp

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@
1010
#include <consensus/validation.h>
1111
#include <core_io.h>
1212
#include <index/txindex.h>
13-
#include <keystore.h>
14-
#include <validation.h>
15-
#include <validationinterface.h>
13+
#include <init.h>
1614
#include <key_io.h>
15+
#include <keystore.h>
1716
#include <merkleblock.h>
1817
#include <net.h>
1918
#include <node/transaction.h>
2019
#include <policy/policy.h>
2120
#include <primitives/transaction.h>
2221
#include <psbt.h>
23-
#include <rpc/util.h>
2422
#include <rpc/rawtransaction.h>
2523
#include <rpc/server.h>
24+
#include <rpc/util.h>
2625
#include <script/script.h>
2726
#include <script/script_error.h>
2827
#include <script/sign.h>
@@ -31,6 +30,8 @@
3130
#include <uint256.h>
3231
#include <util/validation.h>
3332
#include <util/strencodings.h>
33+
#include <validation.h>
34+
#include <validationinterface.h>
3435
#ifdef ENABLE_WALLET
3536
#include <wallet/rpcwallet.h>
3637
#endif
@@ -251,7 +252,16 @@ static UniValue gettxoutproof(const JSONRPCRequest& request)
251252
{
252253
if (request.fHelp || (request.params.size() != 1 && request.params.size() != 2))
253254
throw std::runtime_error(
254-
"gettxoutproof [\"txid\",...] ( blockhash )\n"
255+
RPCHelpMan{"gettxoutproof",
256+
{
257+
{"txids", RPCArg::Type::ARR,
258+
{
259+
{"txid", RPCArg::Type::STR_HEX, false},
260+
},
261+
false},
262+
{"blockhash", RPCArg::Type::STR_HEX, true},
263+
}}
264+
.ToString() +
255265
"\nReturns a hex-encoded proof that \"txid\" was included in a block.\n"
256266
"\nNOTE: By default this function only works sometimes. This is when there is an\n"
257267
"unspent output in the utxo for this transaction. To make it always work,\n"
@@ -672,10 +682,17 @@ static void TxInErrorToJSON(const CTxIn& txin, UniValue& vErrorsRet, const std::
672682

673683
static UniValue combinerawtransaction(const JSONRPCRequest& request)
674684
{
675-
676685
if (request.fHelp || request.params.size() != 1)
677686
throw std::runtime_error(
678-
"combinerawtransaction [\"hexstring\",...]\n"
687+
RPCHelpMan{"combinerawtransaction",
688+
{
689+
{"txs", RPCArg::Type::ARR,
690+
{
691+
{"hexstring", RPCArg::Type::STR_HEX, false},
692+
},
693+
false},
694+
}}
695+
.ToString() +
679696
"\nCombine multiple partially signed transactions into one transaction.\n"
680697
"The combined transaction may be another partially signed transaction or a \n"
681698
"fully signed transaction."
@@ -892,7 +909,30 @@ static UniValue signrawtransactionwithkey(const JSONRPCRequest& request)
892909
{
893910
if (request.fHelp || request.params.size() < 2 || request.params.size() > 4)
894911
throw std::runtime_error(
895-
"signrawtransactionwithkey \"hexstring\" [\"privatekey1\",...] ( [{\"txid\":\"id\",\"vout\":n,\"scriptPubKey\":\"hex\",\"redeemScript\":\"hex\"},...] sighashtype )\n"
912+
RPCHelpMan{"signrawtransactionwithkey",
913+
{
914+
{"hexstring", RPCArg::Type::STR, false},
915+
{"privkyes", RPCArg::Type::ARR,
916+
{
917+
{"privatekey", RPCArg::Type::STR_HEX, false},
918+
},
919+
false},
920+
{"prevtxs", RPCArg::Type::ARR,
921+
{
922+
{"", RPCArg::Type::OBJ,
923+
{
924+
{"txid", RPCArg::Type::STR_HEX, false},
925+
{"vout", RPCArg::Type::NUM, false},
926+
{"scriptPubKey", RPCArg::Type::STR_HEX, false},
927+
{"redeemScript", RPCArg::Type::STR_HEX, false},
928+
{"amount", RPCArg::Type::AMOUNT, false},
929+
},
930+
true},
931+
},
932+
true},
933+
{"sighashtype", RPCArg::Type::STR, true},
934+
}}
935+
.ToString() +
896936
"\nSign inputs for raw transaction (serialized, hex-encoded).\n"
897937
"The second argument is an array of base58-encoded private\n"
898938
"keys that will be the only keys used to sign the transaction.\n"
@@ -1445,7 +1485,15 @@ UniValue combinepsbt(const JSONRPCRequest& request)
14451485
{
14461486
if (request.fHelp || request.params.size() != 1)
14471487
throw std::runtime_error(
1448-
"combinepsbt [\"psbt\",...]\n"
1488+
RPCHelpMan{"combinepsbt",
1489+
{
1490+
{"txs", RPCArg::Type::ARR,
1491+
{
1492+
{"psbt", RPCArg::Type::STR_HEX, false},
1493+
},
1494+
false},
1495+
}}
1496+
.ToString() +
14491497
"\nCombine multiple partially signed Dash transactions into one transaction.\n"
14501498
"Implements the Combiner role.\n"
14511499
"\nArguments:\n"
@@ -1551,7 +1599,36 @@ UniValue createpsbt(const JSONRPCRequest& request)
15511599
{
15521600
if (request.fHelp || request.params.size() < 2 || request.params.size() > 4)
15531601
throw std::runtime_error(
1554-
"createpsbt [{\"txid\":\"id\",\"vout\":n},...] [{\"address\":amount},{\"data\":\"hex\"},...] ( locktime )\n"
1602+
RPCHelpMan{"createpsbt",
1603+
{
1604+
{"inputs", RPCArg::Type::ARR,
1605+
{
1606+
{"", RPCArg::Type::OBJ,
1607+
{
1608+
{"txid", RPCArg::Type::STR_HEX, false},
1609+
{"vout", RPCArg::Type::NUM, false},
1610+
{"sequence", RPCArg::Type::NUM, true},
1611+
},
1612+
false},
1613+
},
1614+
false},
1615+
{"outputs", RPCArg::Type::ARR,
1616+
{
1617+
{"", RPCArg::Type::OBJ,
1618+
{
1619+
{"address", RPCArg::Type::AMOUNT, false},
1620+
},
1621+
true},
1622+
{"", RPCArg::Type::OBJ,
1623+
{
1624+
{"data", RPCArg::Type::STR_HEX, false},
1625+
},
1626+
true},
1627+
},
1628+
false},
1629+
{"locktime", RPCArg::Type::NUM, true},
1630+
}}
1631+
.ToString() +
15551632
"\nCreates a transaction in the Partially Signed Transaction format.\n"
15561633
"Implements the Creator role.\n"
15571634
"\nArguments:\n"

src/rpc/server.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
#include <fs.h>
1010
#include <key_io.h>
1111
#include <random.h>
12+
#include <rpc/util.h>
1213
#include <shutdown.h>
1314
#include <sync.h>
1415
#include <ui_interface.h>
15-
#include <util/system.h>
1616
#include <util/strencodings.h>
17+
#include <util/system.h>
1718

1819
#include <boost/bind.hpp>
1920
#include <boost/signals2/signal.hpp>

src/rpc/util.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,98 @@ UniValue DescribeAddress(const CTxDestination& dest)
9393
return boost::apply_visitor(DescribeAddressVisitor(), dest);
9494
}
9595

96+
std::string RPCHelpMan::ToString() const
97+
{
98+
std::string ret;
99+
100+
ret += m_name;
101+
bool is_optional{false};
102+
for (const auto& arg : m_args) {
103+
ret += " ";
104+
if (arg.m_optional) {
105+
if (!is_optional) ret += "( ";
106+
is_optional = true;
107+
} else {
108+
// Currently we still support unnamed arguments, so any argument following an optional argument must also be optional
109+
// If support for positional arguments is deprecated in the future, remove this line
110+
assert(!is_optional);
111+
}
112+
ret += arg.ToString();
113+
}
114+
if (is_optional) ret += " )";
115+
ret += "\n";
116+
117+
return ret;
118+
}
119+
120+
std::string RPCArg::ToStringObj() const
121+
{
122+
std::string res = "\"" + m_name + "\":";
123+
switch (m_type) {
124+
case Type::STR:
125+
return res + "\"str\"";
126+
case Type::STR_HEX:
127+
return res + "\"hex\"";
128+
case Type::NUM:
129+
return res + "n";
130+
case Type::AMOUNT:
131+
return res + "amount";
132+
case Type::BOOL:
133+
return res + "bool";
134+
case Type::ARR:
135+
res += "[";
136+
for (const auto& i : m_inner) {
137+
res += i.ToString() + ",";
138+
}
139+
return res + "...]";
140+
case Type::OBJ:
141+
case Type::OBJ_USER_KEYS:
142+
// Currently unused, so avoid writing dead code
143+
assert(false);
144+
145+
// no default case, so the compiler can warn about missing cases
146+
}
147+
assert(false);
148+
}
149+
150+
std::string RPCArg::ToString() const
151+
{
152+
switch (m_type) {
153+
case Type::STR_HEX:
154+
case Type::STR: {
155+
return "\"" + m_name + "\"";
156+
}
157+
case Type::NUM:
158+
case Type::AMOUNT:
159+
case Type::BOOL: {
160+
return m_name;
161+
}
162+
case Type::OBJ:
163+
case Type::OBJ_USER_KEYS: {
164+
std::string res;
165+
for (size_t i = 0; i < m_inner.size();) {
166+
res += m_inner[i].ToStringObj();
167+
if (++i < m_inner.size()) res += ",";
168+
}
169+
if (m_type == Type::OBJ) {
170+
return "{" + res + "}";
171+
} else {
172+
return "{" + res + ",...}";
173+
}
174+
}
175+
case Type::ARR: {
176+
std::string res;
177+
for (const auto& i : m_inner) {
178+
res += i.ToString() + ",";
179+
}
180+
return "[" + res + "...]";
181+
}
182+
183+
// no default case, so the compiler can warn about missing cases
184+
}
185+
assert(false);
186+
}
187+
96188
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr)
97189
{
98190
switch (terr) {

src/rpc/util.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,55 @@ CScript CreateMultisigRedeemscript(const int required, const std::vector<CPubKey
2525

2626
UniValue DescribeAddress(const CTxDestination& dest);
2727

28+
struct RPCArg {
29+
enum class Type {
30+
OBJ,
31+
ARR,
32+
STR,
33+
NUM,
34+
BOOL,
35+
OBJ_USER_KEYS, //!< Special type where the user must set the keys e.g. to define multiple addresses; as opposed to e.g. an options object where the keys are predefined
36+
AMOUNT, //!< Special type representing a floating point amount (can be either NUM or STR)
37+
STR_HEX, //!< Special type that is a STR with only hex chars
38+
};
39+
const std::string m_name; //!< The name of the arg (can be empty for inner args)
40+
const Type m_type;
41+
const std::vector<RPCArg> m_inner; //!< Only used for arrays or dicts
42+
const bool m_optional;
43+
44+
RPCArg(const std::string& name, const Type& type, const bool optional)
45+
: m_name{name}, m_type{type}, m_optional{optional}
46+
{
47+
assert(type != Type::ARR && type != Type::OBJ);
48+
}
49+
50+
RPCArg(const std::string& name, const Type& type, const std::vector<RPCArg>& inner, const bool optional)
51+
: m_name{name}, m_type{type}, m_inner{inner}, m_optional{optional}
52+
{
53+
assert(type == Type::ARR || type == Type::OBJ);
54+
}
55+
56+
std::string ToString() const;
57+
58+
private:
59+
std::string ToStringObj() const;
60+
};
61+
62+
class RPCHelpMan
63+
{
64+
public:
65+
RPCHelpMan(const std::string& name, const std::vector<RPCArg>& args)
66+
: m_name{name}, m_args{args}
67+
{
68+
}
69+
70+
std::string ToString() const;
71+
72+
private:
73+
const std::string m_name;
74+
const std::vector<RPCArg> m_args;
75+
};
76+
2877
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr);
2978
UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string = "");
3079

0 commit comments

Comments
 (0)