mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
221
src/rpc/ngHandlers/LedgerData.cpp
Normal file
221
src/rpc/ngHandlers/LedgerData.cpp
Normal file
@@ -0,0 +1,221 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2023, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <rpc/ngHandlers/LedgerData.h>
|
||||
|
||||
#include <ripple/app/ledger/LedgerToJson.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace RPCng {
|
||||
|
||||
LedgerDataHandler::Result
|
||||
LedgerDataHandler::process(Input input, Context const& ctx) const
|
||||
{
|
||||
// marker must be int if outOfOrder is true
|
||||
if (input.outOfOrder && input.marker)
|
||||
return Error{RPC::Status{RPC::RippledError::rpcINVALID_PARAMS, "outOfOrderMarkerNotInt"}};
|
||||
if (!input.outOfOrder && input.diffMarker)
|
||||
return Error{RPC::Status{RPC::RippledError::rpcINVALID_PARAMS, "markerNotString"}};
|
||||
|
||||
auto const range = sharedPtrBackend_->fetchLedgerRange();
|
||||
auto const lgrInfoOrStatus = RPC::getLedgerInfoFromHashOrSeq(
|
||||
*sharedPtrBackend_, ctx.yield, input.ledgerHash, input.ledgerIndex, range->maxSequence);
|
||||
|
||||
if (auto const status = std::get_if<RPC::Status>(&lgrInfoOrStatus))
|
||||
return Error{*status};
|
||||
|
||||
auto const lgrInfo = std::get<ripple::LedgerInfo>(lgrInfoOrStatus);
|
||||
|
||||
Output output;
|
||||
// no marker -> first call, return header information
|
||||
auto header = boost::json::object();
|
||||
if ((!input.marker) && (!input.diffMarker))
|
||||
{
|
||||
if (input.binary)
|
||||
{
|
||||
header[JS(ledger_data)] = ripple::strHex(RPC::ledgerInfoToBlob(lgrInfo));
|
||||
}
|
||||
else
|
||||
{
|
||||
header[JS(accepted)] = true;
|
||||
header[JS(account_hash)] = ripple::strHex(lgrInfo.accountHash);
|
||||
header[JS(close_flags)] = lgrInfo.closeFlags;
|
||||
header[JS(close_time)] = lgrInfo.closeTime.time_since_epoch().count();
|
||||
header[JS(close_time_human)] = ripple::to_string(lgrInfo.closeTime);
|
||||
header[JS(close_time_resolution)] = lgrInfo.closeTimeResolution.count();
|
||||
header[JS(hash)] = ripple::strHex(lgrInfo.hash);
|
||||
header[JS(ledger_hash)] = ripple::strHex(lgrInfo.hash);
|
||||
header[JS(ledger_index)] = std::to_string(lgrInfo.seq);
|
||||
header[JS(parent_close_time)] = lgrInfo.parentCloseTime.time_since_epoch().count();
|
||||
header[JS(parent_hash)] = ripple::strHex(lgrInfo.parentHash);
|
||||
header[JS(seqNum)] = std::to_string(lgrInfo.seq);
|
||||
header[JS(totalCoins)] = ripple::to_string(lgrInfo.drops);
|
||||
header[JS(total_coins)] = ripple::to_string(lgrInfo.drops);
|
||||
header[JS(transaction_hash)] = ripple::strHex(lgrInfo.txHash);
|
||||
}
|
||||
header[JS(closed)] = true;
|
||||
output.header = std::move(header);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (input.marker && !sharedPtrBackend_->fetchLedgerObject(*(input.marker), lgrInfo.seq, ctx.yield))
|
||||
return Error{RPC::Status{RPC::RippledError::rpcINVALID_PARAMS, "markerDoesNotExist"}};
|
||||
}
|
||||
|
||||
output.ledgerHash = ripple::strHex(lgrInfo.hash);
|
||||
output.ledgerIndex = lgrInfo.seq;
|
||||
|
||||
auto const start = std::chrono::system_clock::now();
|
||||
std::vector<Backend::LedgerObject> results;
|
||||
if (input.diffMarker)
|
||||
{
|
||||
// keep the same logic as previous implementation
|
||||
auto diff = sharedPtrBackend_->fetchLedgerDiff(*(input.diffMarker), ctx.yield);
|
||||
std::vector<ripple::uint256> keys;
|
||||
for (auto& [key, object] : diff)
|
||||
{
|
||||
if (!object.size())
|
||||
keys.push_back(std::move(key));
|
||||
}
|
||||
auto objs = sharedPtrBackend_->fetchLedgerObjects(keys, lgrInfo.seq, ctx.yield);
|
||||
for (size_t i = 0; i < objs.size(); ++i)
|
||||
{
|
||||
auto& obj = objs[i];
|
||||
if (obj.size())
|
||||
results.push_back({std::move(keys[i]), std::move(obj)});
|
||||
}
|
||||
if (*(input.diffMarker) > lgrInfo.seq)
|
||||
output.diffMarker = *(input.diffMarker) - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// limit's limitation is different based on binary or json
|
||||
// framework can not handler the check right now, adjust the value here
|
||||
auto const limit =
|
||||
std::min(input.limit, input.binary ? LedgerDataHandler::LIMITBINARY : LedgerDataHandler::LIMITJSON);
|
||||
auto page = sharedPtrBackend_->fetchLedgerPage(input.marker, lgrInfo.seq, limit, input.outOfOrder, ctx.yield);
|
||||
results = std::move(page.objects);
|
||||
if (page.cursor)
|
||||
output.marker = ripple::strHex(*(page.cursor));
|
||||
else if (input.outOfOrder)
|
||||
output.diffMarker = sharedPtrBackend_->fetchLedgerRange()->maxSequence;
|
||||
}
|
||||
auto const end = std::chrono::system_clock::now();
|
||||
|
||||
log_.debug() << "Number of results = " << results.size() << " fetched in "
|
||||
<< std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " microseconds";
|
||||
|
||||
output.states.reserve(results.size());
|
||||
for (auto const& [key, object] : results)
|
||||
{
|
||||
ripple::STLedgerEntry const sle{ripple::SerialIter{object.data(), object.size()}, key};
|
||||
if (input.binary)
|
||||
{
|
||||
boost::json::object entry;
|
||||
entry[JS(data)] = ripple::serializeHex(sle);
|
||||
entry[JS(index)] = ripple::to_string(sle.key());
|
||||
output.states.push_back(std::move(entry));
|
||||
}
|
||||
else
|
||||
{
|
||||
output.states.push_back(RPC::toJson(sle));
|
||||
}
|
||||
}
|
||||
if (input.outOfOrder)
|
||||
output.cacheFull = sharedPtrBackend_->cache().isFull();
|
||||
|
||||
auto const end2 = std::chrono::system_clock::now();
|
||||
log_.debug() << "Number of results = " << results.size() << " serialized in "
|
||||
<< std::chrono::duration_cast<std::chrono::microseconds>(end2 - end).count() << " microseconds";
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void
|
||||
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, LedgerDataHandler::Output const& output)
|
||||
{
|
||||
auto obj = boost::json::object{
|
||||
{JS(ledger_hash), output.ledgerHash},
|
||||
{JS(ledger_index), output.ledgerIndex},
|
||||
{JS(validated), output.validated},
|
||||
{JS(state), output.states},
|
||||
};
|
||||
if (output.header)
|
||||
obj[JS(ledger)] = *(output.header);
|
||||
if (output.cacheFull)
|
||||
obj["cache_full"] = *(output.cacheFull);
|
||||
|
||||
if (output.diffMarker)
|
||||
obj[JS(marker)] = *(output.diffMarker);
|
||||
else if (output.marker)
|
||||
obj[JS(marker)] = *(output.marker);
|
||||
|
||||
jv = std::move(obj);
|
||||
}
|
||||
|
||||
LedgerDataHandler::Input
|
||||
tag_invoke(boost::json::value_to_tag<LedgerDataHandler::Input>, boost::json::value const& jv)
|
||||
{
|
||||
LedgerDataHandler::Input input;
|
||||
auto const& jsonObject = jv.as_object();
|
||||
if (jsonObject.contains(JS(binary)))
|
||||
{
|
||||
input.binary = jsonObject.at(JS(binary)).as_bool();
|
||||
input.limit = input.binary ? LedgerDataHandler::LIMITBINARY : LedgerDataHandler::LIMITJSON;
|
||||
}
|
||||
if (jsonObject.contains(JS(limit)))
|
||||
{
|
||||
input.limit = jsonObject.at(JS(limit)).as_int64();
|
||||
}
|
||||
if (jsonObject.contains("out_of_order"))
|
||||
{
|
||||
input.outOfOrder = jsonObject.at("out_of_order").as_bool();
|
||||
}
|
||||
if (jsonObject.contains("marker"))
|
||||
{
|
||||
if (jsonObject.at("marker").is_string())
|
||||
{
|
||||
input.marker = ripple::uint256{jsonObject.at("marker").as_string().c_str()};
|
||||
}
|
||||
else
|
||||
{
|
||||
input.diffMarker = jsonObject.at("marker").as_int64();
|
||||
}
|
||||
}
|
||||
if (jsonObject.contains(JS(ledger_hash)))
|
||||
{
|
||||
input.ledgerHash = jv.at(JS(ledger_hash)).as_string().c_str();
|
||||
}
|
||||
if (jsonObject.contains(JS(ledger_index)))
|
||||
{
|
||||
if (!jsonObject.at(JS(ledger_index)).is_string())
|
||||
{
|
||||
input.ledgerIndex = jv.at(JS(ledger_index)).as_int64();
|
||||
}
|
||||
else if (jsonObject.at(JS(ledger_index)).as_string() != "validated")
|
||||
{
|
||||
input.ledgerIndex = std::stoi(jv.at(JS(ledger_index)).as_string().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
} // namespace RPCng
|
||||
95
src/rpc/ngHandlers/LedgerData.h
Normal file
95
src/rpc/ngHandlers/LedgerData.h
Normal file
@@ -0,0 +1,95 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of clio: https://github.com/XRPLF/clio
|
||||
Copyright (c) 2023, the clio developers.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <backend/BackendInterface.h>
|
||||
#include <rpc/RPCHelpers.h>
|
||||
#include <rpc/common/Types.h>
|
||||
#include <rpc/common/Validators.h>
|
||||
|
||||
namespace RPCng {
|
||||
class LedgerDataHandler
|
||||
{
|
||||
std::shared_ptr<BackendInterface> sharedPtrBackend_;
|
||||
clio::Logger log_{"RPC"};
|
||||
|
||||
public:
|
||||
struct Output
|
||||
{
|
||||
uint32_t ledgerIndex;
|
||||
std::string ledgerHash;
|
||||
std::optional<boost::json::object> header;
|
||||
boost::json::array states;
|
||||
std::optional<std::string> marker;
|
||||
std::optional<uint32_t> diffMarker;
|
||||
std::optional<bool> cacheFull;
|
||||
bool validated = true;
|
||||
};
|
||||
|
||||
// TODO: Clio does not implement "type" filter
|
||||
// outOfOrder only for clio, there is no document, traverse via seq diff
|
||||
// outOfOrder implementation is copied from old rpc handler
|
||||
struct Input
|
||||
{
|
||||
std::optional<std::string> ledgerHash;
|
||||
std::optional<uint32_t> ledgerIndex;
|
||||
bool binary = false;
|
||||
uint32_t limit = LedgerDataHandler::LIMITJSON; // max 256 for json ; 2048 for binary
|
||||
std::optional<ripple::uint256> marker;
|
||||
std::optional<uint32_t> diffMarker;
|
||||
bool outOfOrder = false;
|
||||
};
|
||||
|
||||
using Result = RPCng::HandlerReturnType<Output>;
|
||||
|
||||
LedgerDataHandler(std::shared_ptr<BackendInterface> const& sharedPtrBackend) : sharedPtrBackend_(sharedPtrBackend)
|
||||
{
|
||||
}
|
||||
|
||||
RpcSpecConstRef
|
||||
spec() const
|
||||
{
|
||||
static const auto rpcSpec = RpcSpec{
|
||||
{JS(binary), validation::Type<bool>{}},
|
||||
{"out_of_order", validation::Type<bool>{}},
|
||||
{JS(ledger_hash), validation::Uint256HexStringValidator},
|
||||
{JS(ledger_index), validation::LedgerIndexValidator},
|
||||
{JS(limit), validation::Type<uint32_t>{}},
|
||||
{JS(marker),
|
||||
validation::Type<uint32_t, std::string>{},
|
||||
validation::IfType<std::string>{validation::Uint256HexStringValidator}},
|
||||
};
|
||||
return rpcSpec;
|
||||
}
|
||||
|
||||
Result
|
||||
process(Input input, Context const& ctx) const;
|
||||
|
||||
private:
|
||||
static uint32_t constexpr LIMITBINARY = 2048;
|
||||
static uint32_t constexpr LIMITJSON = 256;
|
||||
|
||||
friend void
|
||||
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output);
|
||||
|
||||
friend Input
|
||||
tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv);
|
||||
};
|
||||
} // namespace RPCng
|
||||
Reference in New Issue
Block a user