Skip to content

Commit

Permalink
WIP: First dibs at storing close_time_iso
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Oct 20, 2023
1 parent 4b2ddea commit 83c1934
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/ripple/app/ledger/impl/LedgerToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
//==============================================================================

#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/ledger/LedgerToJson.h>
#include <ripple/app/main/Application.h>
#include <ripple/app/misc/DeliverMax.h>
Expand All @@ -26,6 +27,7 @@
#include <ripple/protocol/jss.h>
#include <ripple/rpc/Context.h>
#include <ripple/rpc/DeliveredAmount.h>
#include <ripple/rpc/impl/RPCHelpers.h>

namespace ripple {

Expand Down Expand Up @@ -153,6 +155,17 @@ fillJsonTx(
txn,
{txn->getTransactionID(), fill.ledger.seq(), *stMeta});
}

const bool validated = RPC::isValidated(
fill.context->ledgerMaster, fill.ledger, fill.context->app);
txJson[jss::validated] = validated;
if (validated)
{
if (auto close_time =
fill.context->ledgerMaster.getCloseTimeBySeq(
fill.ledger.seq()))
txJson[jss::close_time_iso] = to_string_iso(*close_time);
}
}
else
{
Expand Down
9 changes: 3 additions & 6 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3125,6 +3125,8 @@ NetworkOPsImp::transJson(
jvObj[jss::transaction][jss::date] =
ledger->info().closeTime.time_since_epoch().count();
jvObj[jss::validated] = true;
if (auto close_time = m_ledgerMaster.getCloseTimeBySeq(ledger->seq()))
jvObj[jss::close_time_iso] = to_string_iso(*close_time);

// WRITEME: Put the account next seq here
}
Expand Down Expand Up @@ -3179,13 +3181,8 @@ NetworkOPsImp::transJson(
{
jvTx[jss::tx_json] = jvTx.removeMember(jss::transaction);
jvTx[jss::hash] = hash;

if (!owner_funds.empty())
{
if (!jvTx.isMember(jss::meta))
jvTx[jss::meta] = Json::Value(Json::objectValue);
jvTx[jss::meta][jss::owner_funds] = owner_funds;
}
jvTx[jss::owner_funds] = owner_funds;

// TODO set `jvObj[jss::close_time_iso]` if validated
}
Expand Down
16 changes: 16 additions & 0 deletions src/ripple/basics/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ to_string(NetClock::time_point tp)
system_clock::time_point{tp.time_since_epoch() + 946684800s});
}

template <class Duration>
std::string
to_string_iso(date::sys_time<Duration> tp)
{
return date::format("%FT%TZ", tp);
}

inline std::string
to_string_iso(NetClock::time_point tp)
{
// 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC
using namespace std::chrono;
return to_string_iso(
system_clock::time_point{tp.time_since_epoch() + 946684800s});
}

/** A clock for measuring elapsed time.
The epoch is unspecified.
Expand Down
2 changes: 2 additions & 0 deletions src/ripple/protocol/jss.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ JSS(close); // out: BookChanges
JSS(close_flags); // out: LedgerToJson
JSS(close_time); // in: Application, out: NetworkOPs,
// RCLCxPeerPos, LedgerToJson
JSS(close_time_iso); // out: Tx, NetworkOPs, TransactionEntry
// AccountTx, LedgerToJson
JSS(close_time_estimated); // in: Application, out: LedgerToJson
JSS(close_time_human); // out: LedgerToJson
JSS(close_time_offset); // out: NetworkOPs
Expand Down
6 changes: 5 additions & 1 deletion src/ripple/rpc/handlers/AccountTx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ populateJsonResponse(
jvObj[json_tx] = txn->getJson(
JsonOptions::include_date, false, {std::ref(hash)});
jvObj[jss::hash] = hash;
// TODO set `jvObj[jss::close_time_iso]`
}
else
jvObj[json_tx] =
Expand All @@ -351,6 +350,11 @@ populateJsonResponse(
insertDeliveredAmount(
jvObj[jss::meta], context, txn, *txnMeta);
insertNFTSyntheticInJson(jvObj, sttx, *txnMeta);
if (auto closeTime =
context.ledgerMaster.getCloseTimeBySeq(
txnMeta->getIndex()))
jvObj[jss::close_time_iso] =
to_string_iso(*closeTime);
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/ripple/rpc/handlers/TransactionEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
//==============================================================================

#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/main/Application.h>
#include <ripple/app/misc/DeliverMax.h>
#include <ripple/ledger/ReadView.h>
Expand Down Expand Up @@ -77,7 +78,18 @@ doTransactionEntry(RPC::JsonContext& context)
jvResult[jss::tx_json] =
sttx->getJson(JsonOptions::none, false, {std::ref(hash)});
jvResult[jss::hash] = hash;
// TODO set `jvResult[jss::close_time_iso]`

bool const validated = RPC::isValidated(
context.ledgerMaster, *lpLedger, context.app);

jvResult[jss::validated] = validated;
if (validated)
{
if (auto closeTime = context.ledgerMaster.getCloseTimeBySeq(
lpLedger->seq()))
jvResult[jss::close_time_iso] =
to_string_iso(*closeTime);
}
}
else
jvResult[jss::tx_json] = sttx->getJson(JsonOptions::none);
Expand Down
15 changes: 14 additions & 1 deletion src/ripple/rpc/handlers/Tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <ripple/app/misc/DeliverMax.h>
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/app/misc/Transaction.h>
#include <ripple/app/rdb/RelationalDatabase.h>
#include <ripple/basics/ToString.h>
#include <ripple/net/RPCErr.h>
#include <ripple/protocol/ErrorCodes.h>
Expand Down Expand Up @@ -55,6 +56,7 @@ struct TxResult
std::variant<std::shared_ptr<TxMeta>, Blob> meta;
bool validated = false;
std::optional<std::string> ctid;
std::optional<NetClock::time_point> closeTime;
TxSearched searchedAll;
};

Expand Down Expand Up @@ -140,6 +142,12 @@ doTxPostgres(RPC::Context& context, TxArgs const& args)
*(args.hash), res.txn->getLedger(), *meta);
}
res.validated = true;

auto const ledgerInfo =
context.app.getRelationalDatabase().getLedgerInfoByIndex(
locator.getLedgerSequence());
res.closeTime = ledgerInfo->closeTime;

return {res, rpcSUCCESS};
}
else
Expand Down Expand Up @@ -269,6 +277,9 @@ doTxHelp(RPC::Context& context, TxArgs args)
}
result.validated = isValidated(
context.ledgerMaster, ledger->info().seq, ledger->info().hash);
if (result.validated)
result.closeTime =
context.ledgerMaster.getCloseTimeBySeq(txn->getLedger());

// compute outgoing CTID
uint32_t lgrSeq = ledger->info().seq;
Expand Down Expand Up @@ -328,7 +339,9 @@ populateJsonResponse(
context.apiVersion);
}
response[jss::hash] = hash;
// TODO set `response[jss::close_time_iso]`
if (result.closeTime)
response[jss::close_time_iso] =
to_string_iso(*result.closeTime);
}
else
{
Expand Down

0 comments on commit 83c1934

Please sign in to comment.