Skip to content

Commit 6d73a47

Browse files
pwang200sophiax851
authored andcommitted
Fix unit test api_version to enable api_version 2 (XRPLF#4785)
The command line API still uses `apiMaximumSupportedVersion`. The unit test RPCs use `apiMinimumSupportedVersion` if unspecified. Context: - XRPLF#4568 - XRPLF#4552
1 parent b467941 commit 6d73a47

File tree

6 files changed

+68
-40
lines changed

6 files changed

+68
-40
lines changed

src/ripple/net/RPCCall.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,22 @@ fromNetwork(
6565
std::unordered_map<std::string, std::string> headers = {});
6666
} // namespace RPCCall
6767

68-
/** Given a rippled command line, return the corresponding JSON.
69-
*/
7068
Json::Value
71-
cmdLineToJSONRPC(std::vector<std::string> const& args, beast::Journal j);
69+
rpcCmdToJson(
70+
std::vector<std::string> const& args,
71+
Json::Value& retParams,
72+
unsigned int apiVersion,
73+
beast::Journal j);
7274

7375
/** Internal invocation of RPC client.
76+
* Used by both rippled command line as well as rippled unit tests
7477
*/
7578
std::pair<int, Json::Value>
7679
rpcClient(
7780
std::vector<std::string> const& args,
7881
Config const& config,
7982
Logs& logs,
83+
unsigned int apiVersion,
8084
std::unordered_map<std::string, std::string> const& headers = {});
8185

8286
} // namespace ripple

src/ripple/net/impl/RPCCall.cpp

+10-35
Original file line numberDiff line numberDiff line change
@@ -1472,10 +1472,11 @@ struct RPCCallImp
14721472
//------------------------------------------------------------------------------
14731473

14741474
// Used internally by rpcClient.
1475-
static Json::Value
1476-
rpcCmdLineToJson(
1475+
Json::Value
1476+
rpcCmdToJson(
14771477
std::vector<std::string> const& args,
14781478
Json::Value& retParams,
1479+
unsigned int apiVersion,
14791480
beast::Journal j)
14801481
{
14811482
Json::Value jvRequest(Json::objectValue);
@@ -1493,11 +1494,11 @@ rpcCmdLineToJson(
14931494

14941495
jvRequest = rpParser.parseCommand(args[0], jvRpcParams, true);
14951496

1496-
auto insert_api_version = [](Json::Value& jr) {
1497+
auto insert_api_version = [apiVersion](Json::Value& jr) {
14971498
if (jr.isObject() && !jr.isMember(jss::error) &&
14981499
!jr.isMember(jss::api_version))
14991500
{
1500-
jr[jss::api_version] = RPC::apiMaximumSupportedVersion;
1501+
jr[jss::api_version] = apiVersion;
15011502
}
15021503
};
15031504

@@ -1510,42 +1511,14 @@ rpcCmdLineToJson(
15101511
return jvRequest;
15111512
}
15121513

1513-
Json::Value
1514-
cmdLineToJSONRPC(std::vector<std::string> const& args, beast::Journal j)
1515-
{
1516-
Json::Value jv = Json::Value(Json::objectValue);
1517-
auto const paramsObj = rpcCmdLineToJson(args, jv, j);
1518-
1519-
// Re-use jv to return our formatted result.
1520-
jv.clear();
1521-
1522-
// Allow parser to rewrite method.
1523-
jv[jss::method] = paramsObj.isMember(jss::method)
1524-
? paramsObj[jss::method].asString()
1525-
: args[0];
1526-
1527-
// If paramsObj is not empty, put it in a [params] array.
1528-
if (paramsObj.begin() != paramsObj.end())
1529-
{
1530-
auto& paramsArray = Json::setArray(jv, jss::params);
1531-
paramsArray.append(paramsObj);
1532-
}
1533-
if (paramsObj.isMember(jss::jsonrpc))
1534-
jv[jss::jsonrpc] = paramsObj[jss::jsonrpc];
1535-
if (paramsObj.isMember(jss::ripplerpc))
1536-
jv[jss::ripplerpc] = paramsObj[jss::ripplerpc];
1537-
if (paramsObj.isMember(jss::id))
1538-
jv[jss::id] = paramsObj[jss::id];
1539-
return jv;
1540-
}
1541-
15421514
//------------------------------------------------------------------------------
15431515

15441516
std::pair<int, Json::Value>
15451517
rpcClient(
15461518
std::vector<std::string> const& args,
15471519
Config const& config,
15481520
Logs& logs,
1521+
unsigned int apiVersion,
15491522
std::unordered_map<std::string, std::string> const& headers)
15501523
{
15511524
static_assert(
@@ -1561,7 +1534,8 @@ rpcClient(
15611534
try
15621535
{
15631536
Json::Value jvRpc = Json::Value(Json::objectValue);
1564-
jvRequest = rpcCmdLineToJson(args, jvRpc, logs.journal("RPCParser"));
1537+
jvRequest =
1538+
rpcCmdToJson(args, jvRpc, apiVersion, logs.journal("RPCParser"));
15651539

15661540
if (jvRequest.isMember(jss::error))
15671541
{
@@ -1698,7 +1672,8 @@ fromCommandLine(
16981672
const std::vector<std::string>& vCmd,
16991673
Logs& logs)
17001674
{
1701-
auto const result = rpcClient(vCmd, config, logs);
1675+
auto const result =
1676+
rpcClient(vCmd, config, logs, RPC::apiMaximumSupportedVersion);
17021677

17031678
std::cout << result.second.toStyledString();
17041679

src/test/jtx/impl/Env.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,13 @@ Env::do_rpc(
463463
std::vector<std::string> const& args,
464464
std::unordered_map<std::string, std::string> const& headers)
465465
{
466-
return rpcClient(args, app().config(), app().logs(), headers).second;
466+
return rpcClient(
467+
args,
468+
app().config(),
469+
app().logs(),
470+
RPC::apiMaximumSupportedVersion,
471+
headers)
472+
.second;
467473
}
468474

469475
void

src/test/jtx/impl/utility.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
//==============================================================================
1919

2020
#include <ripple/basics/contract.h>
21+
#include <ripple/json/Object.h>
22+
#include <ripple/net/RPCCall.h>
2123
#include <ripple/protocol/ErrorCodes.h>
2224
#include <ripple/protocol/HashPrefix.h>
2325
#include <ripple/protocol/Indexes.h>
@@ -73,6 +75,38 @@ fill_seq(Json::Value& jv, ReadView const& view)
7375
jv[jss::Sequence] = ar->getFieldU32(sfSequence);
7476
}
7577

78+
Json::Value
79+
cmdToJSONRPC(
80+
std::vector<std::string> const& args,
81+
beast::Journal j,
82+
unsigned int apiVersion)
83+
{
84+
Json::Value jv = Json::Value(Json::objectValue);
85+
auto const paramsObj = rpcCmdToJson(args, jv, apiVersion, j);
86+
87+
// Re-use jv to return our formatted result.
88+
jv.clear();
89+
90+
// Allow parser to rewrite method.
91+
jv[jss::method] = paramsObj.isMember(jss::method)
92+
? paramsObj[jss::method].asString()
93+
: args[0];
94+
95+
// If paramsObj is not empty, put it in a [params] array.
96+
if (paramsObj.begin() != paramsObj.end())
97+
{
98+
auto& paramsArray = Json::setArray(jv, jss::params);
99+
paramsArray.append(paramsObj);
100+
}
101+
if (paramsObj.isMember(jss::jsonrpc))
102+
jv[jss::jsonrpc] = paramsObj[jss::jsonrpc];
103+
if (paramsObj.isMember(jss::ripplerpc))
104+
jv[jss::ripplerpc] = paramsObj[jss::ripplerpc];
105+
if (paramsObj.isMember(jss::id))
106+
jv[jss::id] = paramsObj[jss::id];
107+
return jv;
108+
}
109+
76110
} // namespace jtx
77111
} // namespace test
78112
} // namespace ripple

src/test/jtx/utility.h

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <ripple/app/ledger/Ledger.h>
2424
#include <ripple/json/json_value.h>
2525
#include <ripple/protocol/STObject.h>
26+
#include <ripple/rpc/impl/RPCHelpers.h>
2627
#include <stdexcept>
2728
#include <test/jtx/Account.h>
2829

@@ -61,6 +62,13 @@ fill_fee(Json::Value& jv, ReadView const& view);
6162
void
6263
fill_seq(Json::Value& jv, ReadView const& view);
6364

65+
/** Given a rippled unit test rpc command, return the corresponding JSON. */
66+
Json::Value
67+
cmdToJSONRPC(
68+
std::vector<std::string> const& args,
69+
beast::Journal j,
70+
unsigned int apiVersion = RPC::apiMaximumSupportedVersion);
71+
6472
} // namespace jtx
6573
} // namespace test
6674
} // namespace ripple

src/test/rpc/RPCCall_test.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <ripple/protocol/ErrorCodes.h>
2222
#include <ripple/rpc/impl/RPCHelpers.h>
2323
#include <test/jtx.h>
24+
#include <test/jtx/utility.h>
2425

2526
#include <boost/algorithm/string.hpp>
2627
#include <initializer_list>
@@ -6442,7 +6443,7 @@ class RPCCall_test : public beast::unit_test::suite
64426443
Json::Value got;
64436444
try
64446445
{
6445-
got = cmdLineToJSONRPC(args, env.journal);
6446+
got = jtx::cmdToJSONRPC(args, env.journal);
64466447
}
64476448
catch (std::bad_cast const&)
64486449
{

0 commit comments

Comments
 (0)