Fix unit test api_version to enable api_version 2 (#4785)

The command line API still uses `apiMaximumSupportedVersion`.
The unit test RPCs use `apiMinimumSupportedVersion` if unspecified.

Context:
- #4568
- #4552
This commit is contained in:
pwang200
2023-11-02 12:59:19 -04:00
committed by tequ
parent 46b1b9e63c
commit 99225d5aee
6 changed files with 68 additions and 40 deletions

View File

@@ -65,18 +65,22 @@ fromNetwork(
std::unordered_map<std::string, std::string> headers = {});
} // namespace RPCCall
/** Given a rippled command line, return the corresponding JSON.
*/
Json::Value
cmdLineToJSONRPC(std::vector<std::string> const& args, beast::Journal j);
rpcCmdToJson(
std::vector<std::string> const& args,
Json::Value& retParams,
unsigned int apiVersion,
beast::Journal j);
/** Internal invocation of RPC client.
* Used by both rippled command line as well as rippled unit tests
*/
std::pair<int, Json::Value>
rpcClient(
std::vector<std::string> const& args,
Config const& config,
Logs& logs,
unsigned int apiVersion,
std::unordered_map<std::string, std::string> const& headers = {});
} // namespace ripple

View File

@@ -1678,10 +1678,11 @@ struct RPCCallImp
//------------------------------------------------------------------------------
// Used internally by rpcClient.
static Json::Value
rpcCmdLineToJson(
Json::Value
rpcCmdToJson(
std::vector<std::string> const& args,
Json::Value& retParams,
unsigned int apiVersion,
beast::Journal j)
{
Json::Value jvRequest(Json::objectValue);
@@ -1699,11 +1700,11 @@ rpcCmdLineToJson(
jvRequest = rpParser.parseCommand(args[0], jvRpcParams, true);
auto insert_api_version = [](Json::Value& jr) {
auto insert_api_version = [apiVersion](Json::Value& jr) {
if (jr.isObject() && !jr.isMember(jss::error) &&
!jr.isMember(jss::api_version))
{
jr[jss::api_version] = RPC::apiMaximumSupportedVersion;
jr[jss::api_version] = apiVersion;
}
};
@@ -1716,35 +1717,6 @@ rpcCmdLineToJson(
return jvRequest;
}
Json::Value
cmdLineToJSONRPC(std::vector<std::string> const& args, beast::Journal j)
{
Json::Value jv = Json::Value(Json::objectValue);
auto const paramsObj = rpcCmdLineToJson(args, jv, j);
// Re-use jv to return our formatted result.
jv.clear();
// Allow parser to rewrite method.
jv[jss::method] = paramsObj.isMember(jss::method)
? paramsObj[jss::method].asString()
: args[0];
// If paramsObj is not empty, put it in a [params] array.
if (paramsObj.begin() != paramsObj.end())
{
auto& paramsArray = Json::setArray(jv, jss::params);
paramsArray.append(paramsObj);
}
if (paramsObj.isMember(jss::jsonrpc))
jv[jss::jsonrpc] = paramsObj[jss::jsonrpc];
if (paramsObj.isMember(jss::ripplerpc))
jv[jss::ripplerpc] = paramsObj[jss::ripplerpc];
if (paramsObj.isMember(jss::id))
jv[jss::id] = paramsObj[jss::id];
return jv;
}
//------------------------------------------------------------------------------
std::pair<int, Json::Value>
@@ -1752,6 +1724,7 @@ rpcClient(
std::vector<std::string> const& args,
Config const& config,
Logs& logs,
unsigned int apiVersion,
std::unordered_map<std::string, std::string> const& headers)
{
static_assert(
@@ -1767,7 +1740,8 @@ rpcClient(
try
{
Json::Value jvRpc = Json::Value(Json::objectValue);
jvRequest = rpcCmdLineToJson(args, jvRpc, logs.journal("RPCParser"));
jvRequest =
rpcCmdToJson(args, jvRpc, apiVersion, logs.journal("RPCParser"));
if (jvRequest.isMember(jss::error))
{
@@ -1904,7 +1878,8 @@ fromCommandLine(
const std::vector<std::string>& vCmd,
Logs& logs)
{
auto const result = rpcClient(vCmd, config, logs);
auto const result =
rpcClient(vCmd, config, logs, RPC::apiMaximumSupportedVersion);
std::cout << result.second.toStyledString();

View File

@@ -505,7 +505,13 @@ Env::do_rpc(
std::vector<std::string> const& args,
std::unordered_map<std::string, std::string> const& headers)
{
return rpcClient(args, app().config(), app().logs(), headers).second;
return rpcClient(
args,
app().config(),
app().logs(),
RPC::apiMaximumSupportedVersion,
headers)
.second;
}
void

View File

@@ -18,6 +18,8 @@
//==============================================================================
#include <ripple/basics/contract.h>
#include <ripple/json/Object.h>
#include <ripple/net/RPCCall.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/Indexes.h>
@@ -73,6 +75,38 @@ fill_seq(Json::Value& jv, ReadView const& view)
jv[jss::Sequence] = ar->getFieldU32(sfSequence);
}
Json::Value
cmdToJSONRPC(
std::vector<std::string> const& args,
beast::Journal j,
unsigned int apiVersion)
{
Json::Value jv = Json::Value(Json::objectValue);
auto const paramsObj = rpcCmdToJson(args, jv, apiVersion, j);
// Re-use jv to return our formatted result.
jv.clear();
// Allow parser to rewrite method.
jv[jss::method] = paramsObj.isMember(jss::method)
? paramsObj[jss::method].asString()
: args[0];
// If paramsObj is not empty, put it in a [params] array.
if (paramsObj.begin() != paramsObj.end())
{
auto& paramsArray = Json::setArray(jv, jss::params);
paramsArray.append(paramsObj);
}
if (paramsObj.isMember(jss::jsonrpc))
jv[jss::jsonrpc] = paramsObj[jss::jsonrpc];
if (paramsObj.isMember(jss::ripplerpc))
jv[jss::ripplerpc] = paramsObj[jss::ripplerpc];
if (paramsObj.isMember(jss::id))
jv[jss::id] = paramsObj[jss::id];
return jv;
}
} // namespace jtx
} // namespace test
} // namespace ripple

View File

@@ -23,6 +23,7 @@
#include <ripple/app/ledger/Ledger.h>
#include <ripple/json/json_value.h>
#include <ripple/protocol/STObject.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <stdexcept>
#include <test/jtx/Account.h>
@@ -61,6 +62,13 @@ fill_fee(Json::Value& jv, ReadView const& view);
void
fill_seq(Json::Value& jv, ReadView const& view);
/** Given a rippled unit test rpc command, return the corresponding JSON. */
Json::Value
cmdToJSONRPC(
std::vector<std::string> const& args,
beast::Journal j,
unsigned int apiVersion = RPC::apiMaximumSupportedVersion);
} // namespace jtx
} // namespace test
} // namespace ripple

View File

@@ -21,6 +21,7 @@
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <test/jtx.h>
#include <test/jtx/utility.h>
#include <boost/algorithm/string.hpp>
#include <initializer_list>
@@ -6488,7 +6489,7 @@ public:
Json::Value got;
try
{
got = cmdLineToJSONRPC(args, env.journal);
got = jtx::cmdToJSONRPC(args, env.journal);
}
catch (std::bad_cast const&)
{