Skip to content

Commit

Permalink
Store close_time_iso in API v2 output
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Oct 23, 2023
1 parent 5f6b079 commit 43237ff
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 7 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
2 changes: 2 additions & 0 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3124,6 +3124,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
28 changes: 26 additions & 2 deletions src/ripple/basics/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <ripple/beast/clock/abstract_clock.h>
#include <ripple/beast/clock/basic_seconds_clock.h>
#include <ripple/beast/clock/manual_clock.h>

#include <chrono>
#include <cstdint>
#include <string>
Expand All @@ -43,8 +44,15 @@ using weeks = std::chrono::
/** Clock for measuring the network time.
The epoch is January 1, 2000
epoch_offset = days(10957); // 2000-01-01
epoch_offset
= date(2000-01-01) - date(1970-0-01)
= days(10957)
= seconds(946684800)
*/

constexpr static std::chrono::seconds epoch_offset{946684800};

class NetClock
{
public:
Expand All @@ -71,7 +79,23 @@ to_string(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(
system_clock::time_point{tp.time_since_epoch() + 946684800s});
system_clock::time_point{tp.time_since_epoch() + epoch_offset});
}

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() + epoch_offset});
}

/** A clock for measuring elapsed time.
Expand Down
3 changes: 2 additions & 1 deletion src/ripple/core/TimeKeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <ripple/basics/chrono.h>
#include <ripple/beast/clock/abstract_clock.h>

#include <atomic>

namespace ripple {
Expand All @@ -37,7 +38,7 @@ class TimeKeeper : public beast::abstract_clock<NetClock>
adjust(std::chrono::system_clock::time_point when)
{
return time_point(std::chrono::duration_cast<duration>(
when.time_since_epoch() - days(10957)));
when.time_since_epoch() - epoch_offset));
}

public:
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
2 changes: 1 addition & 1 deletion src/ripple/rpc/handlers/AMMInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ to_iso8601(NetClock::time_point tp)
return date::format(
"%Y-%Om-%dT%H:%M:%OS%z",
date::sys_time<system_clock::duration>(
system_clock::time_point{tp.time_since_epoch() + 946684800s}));
system_clock::time_point{tp.time_since_epoch() + epoch_offset}));
}

Json::Value
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 43237ff

Please sign in to comment.