Skip to content

Commit

Permalink
Merge branch 'develop' into nft-reserve
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnxie999 authored Nov 3, 2023
2 parents 4fdd168 + 09e0f10 commit 553d76b
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 72 deletions.
32 changes: 0 additions & 32 deletions docs/Dockerfile

This file was deleted.

3 changes: 3 additions & 0 deletions src/ripple/app/tx/impl/XChainBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,9 @@ attestationPreflight(PreflightContext const& ctx)
if (ctx.tx.getFlags() & tfUniversalMask)
return temINVALID_FLAG;

if (!publicKeyType(ctx.tx[sfPublicKey]))
return temMALFORMED;

auto const att = toClaim<TAttestation>(ctx.tx);
if (!att)
return temMALFORMED;
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
68 changes: 68 additions & 0 deletions src/test/app/XChain_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4209,6 +4209,73 @@ struct XChain_test : public beast::unit_test::suite,
}
}

void
testBadPublicKey()
{
using namespace jtx;

testcase("Bad attestations");
{
// Create a bridge and add an attestation with a bad public key
XEnv scEnv(*this, true);
std::uint32_t const claimID = 1;
std::optional<Account> dst{scBob};
auto const amt = XRP(1000);
scEnv.tx(create_bridge(Account::master, jvb))
.tx(jtx::signers(Account::master, quorum, signers))
.close();
scEnv.tx(xchain_create_claim_id(scAlice, jvb, reward, mcAlice))
.close();
auto jvAtt = claim_attestation(
scAttester,
jvb,
mcAlice,
amt,
payees[UT_XCHAIN_DEFAULT_QUORUM],
true,
claimID,
dst,
signers[UT_XCHAIN_DEFAULT_QUORUM]);
{
// Change to an invalid keytype
auto k = jvAtt["PublicKey"].asString();
k.at(1) = '9';
jvAtt["PublicKey"] = k;
}
scEnv.tx(jvAtt, ter(temMALFORMED)).close();
}
{
// Create a bridge and add an create account attestation with a bad
// public key
XEnv scEnv(*this, true);
std::uint32_t const createCount = 1;
Account dst{scBob};
auto const amt = XRP(1000);
auto const rewardAmt = XRP(1);
scEnv.tx(create_bridge(Account::master, jvb))
.tx(jtx::signers(Account::master, quorum, signers))
.close();
auto jvAtt = create_account_attestation(
scAttester,
jvb,
mcAlice,
amt,
rewardAmt,
payees[UT_XCHAIN_DEFAULT_QUORUM],
true,
createCount,
dst,
signers[UT_XCHAIN_DEFAULT_QUORUM]);
{
// Change to an invalid keytype
auto k = jvAtt["PublicKey"].asString();
k.at(1) = '9';
jvAtt["PublicKey"] = k;
}
scEnv.tx(jvAtt, ter(temMALFORMED)).close();
}
}

void
run() override
{
Expand All @@ -4226,6 +4293,7 @@ struct XChain_test : public beast::unit_test::suite,
testXChainCreateAccount();
testFeeDipsIntoReserve();
testXChainDeleteDoor();
testBadPublicKey();
}
};

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 553d76b

Please sign in to comment.