Skip to content

Commit

Permalink
Improve getJson for Transaction and STTx
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Nov 2, 2023
1 parent 17e6588 commit 95b6055
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 57 deletions.
6 changes: 2 additions & 4 deletions src/ripple/app/ledger/impl/LedgerToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ fillJsonTx(
{
if (fill.context->apiVersion > 1)
{
std::string hash;
copyFrom(
txJson[jss::tx_json],
txn->getJson(
JsonOptions::none, false, {std::optional(std::ref(hash))}));
txJson[jss::hash] = hash;
txn->getJson(JsonOptions::disable_API_prior_V2, false));
txJson[jss::hash] = to_string(txn->getTransactionID());
RPC::insertDeliverMax(
txJson[jss::tx_json], txnType, fill.context->apiVersion);

Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3106,9 +3106,9 @@ NetworkOPsImp::transJson(
// NOTE jvObj which is not a finished object for either API version. After
// it's populated, we need to finish it for a specific API version. This is
// done in a loop, near the end of this function.
std::string hash = {};
std::string const hash = to_string(transaction->getTransactionID());
jvObj[jss::transaction] =
transaction->getJson(JsonOptions::none, false, {std::ref(hash)});
transaction->getJson(JsonOptions::disable_API_prior_V2, false);

if (meta)
{
Expand Down
7 changes: 2 additions & 5 deletions src/ripple/app/misc/Transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <ripple/beast/utility/Journal.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/Protocol.h>
#include <ripple/protocol/STBase.h>
#include <ripple/protocol/STTx.h>
#include <ripple/protocol/TER.h>
#include <ripple/protocol/TxMeta.h>
Expand Down Expand Up @@ -307,11 +308,7 @@ class Transaction : public std::enable_shared_from_this<Transaction>,

/// Same as similar overload for STTx::getJson
Json::Value
getJson(
JsonOptions options,
bool binary = false,
bool showInLedger = false,
std::optional<std::reference_wrapper<std::string>> hash = {}) const;
getJson(JsonOptions options, bool binary = false) const;

// Information used to locate a transaction.
// Contains a nodestore hash and ledger sequence pair if the transaction was
Expand Down
21 changes: 12 additions & 9 deletions src/ripple/app/misc/impl/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,25 @@ Transaction::load(

// options 1 to include the date of the transaction
Json::Value
Transaction::getJson(
JsonOptions options,
bool binary,
bool showInLedger,
std::optional<std::reference_wrapper<std::string>> hash) const
Transaction::getJson(JsonOptions options, bool binary) const
{
Json::Value ret(mTransaction->getJson(JsonOptions::none, binary, hash));
// Note, we explicitly suppress `include_date` option here
Json::Value ret(
mTransaction->getJson(options % JsonOptions::include_date, binary));

if (mLedgerIndex)
{
if (showInLedger)
ret[jss::inLedger] = mLedgerIndex; // Deprecated.
if (!(options & JsonOptions::disable_API_prior_V2))
{
// Behaviour before API version 2
ret[jss::inLedger] = mLedgerIndex;
}

// TODO: disable_API_prior_V3 to disable output of both `date` and
// `ledger_index` elements (taking precedence over include_date)
ret[jss::ledger_index] = mLedgerIndex;

if (options == JsonOptions::include_date)
if (options & JsonOptions::include_date)
{
auto ct = mApp.getLedgerMaster().getCloseTimeBySeq(mLedgerIndex);
if (ct)
Expand Down
30 changes: 29 additions & 1 deletion src/ripple/protocol/STBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,35 @@
#include <utility>
namespace ripple {

enum class JsonOptions { none = 0, include_date = 1 };
/// Note, should be treated as flags that can be | and &
enum class JsonOptions : unsigned int {
none = 0,
include_date = 1,
disable_API_prior_V2 = 2
};

/// Return: JsonOptions union of lh and rh
[[nodiscard]] constexpr JsonOptions
operator|(JsonOptions lh, JsonOptions rh) noexcept
{
return JsonOptions(
static_cast<unsigned int>(lh) | static_cast<unsigned int>(rh));
}

/// Return: JsonOptions similar to lh, but with rh disabled (i.e. "remainder")
[[nodiscard]] constexpr JsonOptions
operator%(JsonOptions lh, JsonOptions rh) noexcept
{
return JsonOptions(
static_cast<unsigned int>(lh) & ~static_cast<unsigned int>(rh));
}

/// Return: true if lh contains rh
[[nodiscard]] constexpr bool
operator&(JsonOptions lh, JsonOptions rh) noexcept
{
return (static_cast<unsigned int>(lh) & static_cast<unsigned int>(rh)) != 0;
}

namespace detail {
class STVar;
Expand Down
7 changes: 3 additions & 4 deletions src/ripple/protocol/STTx.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/protocol/Rules.h>
#include <ripple/protocol/STBase.h>
#include <ripple/protocol/STObject.h>
#include <ripple/protocol/SecretKey.h>
#include <ripple/protocol/SeqProxy.h>
#include <ripple/protocol/TxFormats.h>
#include <boost/container/flat_set.hpp>

#include <functional>

namespace ripple {
Expand Down Expand Up @@ -116,10 +118,7 @@ class STTx final : public STObject, public CountedObject<STTx>
nested jss::tx for binary hex; instead will return it as JSON string
*/
Json::Value
getJson(
JsonOptions options,
bool binary,
std::optional<std::reference_wrapper<std::string>> hash = {}) const;
getJson(JsonOptions options, bool binary) const;

void
sign(PublicKey const& publicKey, SecretKey const& secretKey);
Expand Down
12 changes: 5 additions & 7 deletions src/ripple/protocol/impl/STTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <ripple/protocol/UintTypes.h>
#include <ripple/protocol/jss.h>
#include <boost/format.hpp>

#include <array>
#include <memory>
#include <type_traits>
Expand Down Expand Up @@ -234,13 +235,11 @@ Json::Value STTx::getJson(JsonOptions) const
}

Json::Value
STTx::getJson(
JsonOptions options,
bool binary,
std::optional<std::reference_wrapper<std::string>> hash) const
STTx::getJson(JsonOptions options, bool binary) const
{
if (!hash) // Old behaviour - default because `hash = {}` in declaration
if (!(options & JsonOptions::disable_API_prior_V2))
{
// Behaviour before API version 2
if (binary)
{
Json::Value ret;
Expand All @@ -252,8 +251,7 @@ STTx::getJson(
return getJson(options);
}

// Since `hash` is set, do not populate `hash` inside JSON output
hash->get() = to_string(getTransactionID());
// API version 2 behaviour
if (binary)
{
Serializer s = STObject::getSerializer();
Expand Down
15 changes: 7 additions & 8 deletions src/ripple/rpc/handlers/AccountTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,12 @@ populateJsonResponse(
(context.apiVersion > 1 ? jss::tx_json : jss::tx);
if (context.apiVersion > 1)
{
std::string hash;
jvObj[json_tx] = txn->getJson(
JsonOptions::include_date,
false,
false,
{std::ref(hash)});
jvObj[jss::hash] = hash;
JsonOptions::include_date |
JsonOptions::disable_API_prior_V2,
false);
jvObj[jss::hash] = to_string(
txn->getSTransaction()->getTransactionID());
jvObj[jss::ledger_index] = txn->getLedger();
jvObj[jss::ledger_hash] =
to_string(context.ledgerMaster.getHashBySeq(
Expand All @@ -348,8 +347,8 @@ populateJsonResponse(
to_string_iso(*closeTime);
}
else
jvObj[json_tx] = txn->getJson(
JsonOptions::include_date, false, true);
jvObj[json_tx] =
txn->getJson(JsonOptions::include_date);

auto const& sttx = txn->getSTransaction();
RPC::insertDeliverMax(
Expand Down
5 changes: 2 additions & 3 deletions src/ripple/rpc/handlers/TransactionEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ doTransactionEntry(RPC::JsonContext& context)
{
if (context.apiVersion > 1)
{
std::string hash;
jvResult[jss::tx_json] =
sttx->getJson(JsonOptions::none, false, {std::ref(hash)});
jvResult[jss::hash] = hash;
sttx->getJson(JsonOptions::disable_API_prior_V2);
jvResult[jss::hash] = to_string(sttx->getTransactionID());

if (!lpLedger->open())
jvResult[jss::ledger_hash] = to_string(
Expand Down
17 changes: 9 additions & 8 deletions src/ripple/rpc/handlers/Tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <ripple/rpc/DeliveredAmount.h>
#include <ripple/rpc/GRPCHandlers.h>
#include <ripple/rpc/impl/RPCHelpers.h>

#include <charconv>
#include <regex>

Expand Down Expand Up @@ -330,14 +331,13 @@ populateJsonResponse(
auto const& sttx = result.txn->getSTransaction();
if (context.apiVersion > 1)
{
std::string hash;
constexpr auto optionsJson =
JsonOptions::include_date | JsonOptions::disable_API_prior_V2;
if (args.binary)
response[jss::tx_blob] = result.txn->getJson(
JsonOptions::include_date, true, false, {std::ref(hash)});
response[jss::tx_blob] = result.txn->getJson(optionsJson, true);
else
{
response[jss::tx_json] = result.txn->getJson(
JsonOptions::include_date, false, false, {std::ref(hash)});
response[jss::tx_json] = result.txn->getJson(optionsJson);
RPC::insertDeliverMax(
response[jss::tx_json],
sttx->getTxnType(),
Expand All @@ -347,7 +347,8 @@ populateJsonResponse(
if (result.ledgerHash)
response[jss::ledger_hash] = to_string(*result.ledgerHash);

response[jss::hash] = hash;
response[jss::hash] =
to_string(result.txn->getSTransaction()->getTransactionID());
if (result.validated)
{
response[jss::ledger_index] = result.txn->getLedger();
Expand All @@ -358,8 +359,8 @@ populateJsonResponse(
}
else
{
response = result.txn->getJson(
JsonOptions::include_date, args.binary, true);
response =
result.txn->getJson(JsonOptions::include_date, args.binary);
if (!args.binary)
RPC::insertDeliverMax(
response, sttx->getTxnType(), context.apiVersion);
Expand Down
11 changes: 5 additions & 6 deletions src/ripple/rpc/impl/TransactionSign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,13 @@ transactionFormatResultImpl(Transaction::pointer tpTrans, unsigned apiVersion)
{
if (apiVersion > 1)
{
std::string hash = {};
jvResult[jss::tx_json] = tpTrans->getJson(
JsonOptions::none, false, false, {std::ref(hash)});
jvResult[jss::hash] = hash;
jvResult[jss::tx_json] =
tpTrans->getJson(JsonOptions::disable_API_prior_V2);
jvResult[jss::hash] =
to_string(tpTrans->getSTransaction()->getTransactionID());
}
else
jvResult[jss::tx_json] =
tpTrans->getJson(JsonOptions::none, false, true);
jvResult[jss::tx_json] = tpTrans->getJson(JsonOptions::none);

jvResult[jss::tx_blob] =
strHex(tpTrans->getSTransaction()->getSerializer().peekData());
Expand Down

0 comments on commit 95b6055

Please sign in to comment.