Skip to content

Commit

Permalink
Merge branch 'develop' into feature/unify_transaction_json
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Nov 2, 2023
2 parents 942e209 + 056255e commit c1f2ea4
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 76 deletions.
32 changes: 0 additions & 32 deletions docs/Dockerfile

This file was deleted.

4 changes: 0 additions & 4 deletions src/ripple/app/rdb/backend/impl/PostgresDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ loadLedgerInfos(
"total_coins, closing_time, prev_closing_time, close_time_res, "
"close_flags, ledger_seq FROM ledgers ";

uint32_t expNumResults = 1;

if (auto ledgerSeq = std::get_if<uint32_t>(&whichLedger))
{
sql << "WHERE ledger_seq = " + std::to_string(*ledgerSeq);
Expand All @@ -189,8 +187,6 @@ loadLedgerInfos(
auto minAndMax =
std::get_if<std::pair<uint32_t, uint32_t>>(&whichLedger))
{
expNumResults = minAndMax->second - minAndMax->first;

sql
<< ("WHERE ledger_seq >= " + std::to_string(minAndMax->first) +
" AND ledger_seq <= " + std::to_string(minAndMax->second));
Expand Down
10 changes: 7 additions & 3 deletions src/ripple/net/RPCCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 10 additions & 35 deletions src/ripple/net/impl/RPCCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,10 +1472,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);
Expand All @@ -1493,11 +1494,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;
}
};

Expand All @@ -1510,42 +1511,14 @@ 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>
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(
Expand All @@ -1561,7 +1534,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))
{
Expand Down Expand Up @@ -1698,7 +1672,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();

Expand Down
8 changes: 7 additions & 1 deletion src/test/jtx/impl/Env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,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
Expand Down
34 changes: 34 additions & 0 deletions src/test/jtx/impl/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions src/test/jtx/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/test/rpc/RPCCall_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -6442,7 +6443,7 @@ class RPCCall_test : public beast::unit_test::suite
Json::Value got;
try
{
got = cmdLineToJSONRPC(args, env.journal);
got = jtx::cmdToJSONRPC(args, env.journal);
}
catch (std::bad_cast const&)
{
Expand Down

0 comments on commit c1f2ea4

Please sign in to comment.