Skip to content

Commit c614786

Browse files
committed
[Modularization] allow to add entries to the JSON conversion table
- expose CRPCConvertTable class - expose CRPCConvertTable rpcCvtTable - add method to insert custom conversion entries
1 parent 7c3fbc3 commit c614786

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

src/rpcclient.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,28 @@ static const CRPCConvertParam vRPCConvertParams[] =
9090
{ "prioritisetransaction", 2 },
9191
};
9292

93-
class CRPCConvertTable
94-
{
95-
private:
96-
std::set<std::pair<std::string, int> > members;
97-
98-
public:
99-
CRPCConvertTable();
100-
101-
bool convert(const std::string& method, int idx) {
102-
return (members.count(std::make_pair(method, idx)) > 0);
103-
}
104-
};
105-
10693
CRPCConvertTable::CRPCConvertTable()
10794
{
10895
const unsigned int n_elem =
10996
(sizeof(vRPCConvertParams) / sizeof(vRPCConvertParams[0]));
11097

11198
for (unsigned int i = 0; i < n_elem; i++) {
112-
members.insert(std::make_pair(vRPCConvertParams[i].methodName,
113-
vRPCConvertParams[i].paramIdx));
99+
addConversion(vRPCConvertParams[i].methodName,
100+
vRPCConvertParams[i].paramIdx);
114101
}
115102
}
116103

117-
static CRPCConvertTable rpcCvtTable;
104+
void CRPCConvertTable::addConversion(const std::string& method, int idx)
105+
{
106+
members.insert(std::make_pair(method, idx));
107+
}
108+
109+
bool CRPCConvertTable::convert(const std::string& method, int idx)
110+
{
111+
return (members.count(std::make_pair(method, idx)) > 0);
112+
}
113+
114+
CRPCConvertTable rpcCvtTable;
118115

119116
/** Convert strings to command-specific RPC representation */
120117
Array RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams)

src/rpcclient.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@
1010
#include "json/json_spirit_utils.h"
1111
#include "json/json_spirit_writer_template.h"
1212

13+
#include <set>
14+
#include <string>
15+
#include <utility>
16+
17+
class CRPCConvertTable
18+
{
19+
private:
20+
std::set<std::pair<std::string, int> > members;
21+
22+
public:
23+
CRPCConvertTable();
24+
25+
/**
26+
* Adds new conversion to the conversion table
27+
* @param method name of the method
28+
* @param idx 0-based index of the paramter
29+
*/
30+
void addConversion(const std::string& method, int idx);
31+
32+
/**
33+
* Determines if a paramter should be handled as quoted or unquoted string
34+
* @param method name of the method
35+
* @param idx 0-based index of the paramter
36+
* @return whether the paramter should be converted
37+
*/
38+
bool convert(const std::string& method, int idx);
39+
};
40+
41+
extern CRPCConvertTable rpcCvtTable;
42+
1343
json_spirit::Array RPCConvertValues(const std::string& strMethod, const std::vector<std::string>& strParams);
1444

1545
#endif // BITCOIN_RPCCLIENT_H

src/test/rpc_tests.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,28 @@ BOOST_AUTO_TEST_CASE(rpc_boostasiotocnetaddr)
153153
BOOST_CHECK_EQUAL(BoostAsioToCNetAddr(boost::asio::ip::address::from_string("::ffff:127.0.0.1")).ToString(), "127.0.0.1");
154154
}
155155

156+
BOOST_AUTO_TEST_CASE(rpc_add_conversion)
157+
{
158+
rpcCvtTable.addConversion("testcommand", 1);
159+
rpcCvtTable.addConversion("testcommand", 2);
160+
161+
std::vector<std::string> vstrParams;
162+
163+
vstrParams.push_back("1234");
164+
vstrParams.push_back("false");
165+
vstrParams.push_back("481516");
166+
167+
Array params = RPCConvertValues("testcommand", vstrParams);
168+
169+
BOOST_CHECK_NO_THROW(params[0].get_str());
170+
BOOST_CHECK_NO_THROW(params[1].get_bool());
171+
BOOST_CHECK_NO_THROW(params[2].get_int());
172+
173+
BOOST_CHECK_THROW(params[0].get_int(), std::runtime_error);
174+
175+
BOOST_CHECK_EQUAL(params[0].get_str(), "1234");
176+
BOOST_CHECK_EQUAL(params[1].get_bool(), false);
177+
BOOST_CHECK_EQUAL(params[2].get_int(), 481516);
178+
}
179+
156180
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)