mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
@@ -70,6 +70,8 @@ struct TransactionsCursor
|
||||
{
|
||||
std::uint32_t ledgerSequence;
|
||||
std::uint32_t transactionIndex;
|
||||
bool
|
||||
operator==(TransactionsCursor const& other) const = default;
|
||||
};
|
||||
|
||||
struct TransactionsAndCursor
|
||||
|
||||
231
src/rpc/ngHandlers/AccountTx.cpp
Normal file
231
src/rpc/ngHandlers/AccountTx.cpp
Normal file
@@ -0,0 +1,231 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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/AccountTx.h>
|
||||
|
||||
namespace {
|
||||
clio::Logger gLog{"RPC-AccountTxHandler"};
|
||||
}
|
||||
|
||||
namespace RPCng {
|
||||
AccountTxHandler::Result
|
||||
AccountTxHandler::process(
|
||||
AccountTxHandler::Input input,
|
||||
boost::asio::yield_context& yield) const
|
||||
{
|
||||
auto const range = sharedPtrBackend_->fetchLedgerRange();
|
||||
auto [minIndex, maxIndex] = *range;
|
||||
if (input.ledgerIndexMin)
|
||||
{
|
||||
if (range->maxSequence < input.ledgerIndexMin ||
|
||||
range->minSequence > input.ledgerIndexMin)
|
||||
{
|
||||
return Error{RPC::Status{
|
||||
RPC::RippledError::rpcLGR_IDX_MALFORMED,
|
||||
"ledgerSeqMinOutOfRange"}};
|
||||
}
|
||||
minIndex = *input.ledgerIndexMin;
|
||||
}
|
||||
|
||||
if (input.ledgerIndexMax)
|
||||
{
|
||||
if (range->maxSequence < input.ledgerIndexMax ||
|
||||
range->minSequence > input.ledgerIndexMax)
|
||||
return Error{RPC::Status{
|
||||
RPC::RippledError::rpcLGR_IDX_MALFORMED,
|
||||
"ledgerSeqMaxOutOfRange"}};
|
||||
maxIndex = *input.ledgerIndexMax;
|
||||
}
|
||||
|
||||
if (minIndex > maxIndex)
|
||||
return Error{
|
||||
RPC::Status{RPC::RippledError::rpcINVALID_PARAMS, "invalidIndex"}};
|
||||
|
||||
if (input.ledgerHash || input.ledgerIndex)
|
||||
{
|
||||
// rippled does not have this check
|
||||
if (input.ledgerIndexMax || input.ledgerIndexMin)
|
||||
return Error{RPC::Status{
|
||||
RPC::RippledError::rpcINVALID_PARAMS,
|
||||
"containsLedgerSpecifierAndRange"}};
|
||||
|
||||
auto const lgrInfoOrStatus = RPC::getLedgerInfoFromHashOrSeq(
|
||||
*sharedPtrBackend_,
|
||||
yield,
|
||||
input.ledgerHash,
|
||||
input.ledgerIndex,
|
||||
range->maxSequence);
|
||||
|
||||
if (auto status = std::get_if<RPC::Status>(&lgrInfoOrStatus))
|
||||
return Error{*status};
|
||||
|
||||
maxIndex = minIndex = std::get<ripple::LedgerInfo>(lgrInfoOrStatus).seq;
|
||||
}
|
||||
std::optional<Backend::TransactionsCursor> cursor;
|
||||
// if marker exists
|
||||
if (input.marker)
|
||||
{
|
||||
cursor = {input.marker->ledger, input.marker->seq};
|
||||
}
|
||||
else
|
||||
{
|
||||
if (input.forward)
|
||||
cursor = {minIndex, 0};
|
||||
else
|
||||
cursor = {maxIndex, INT32_MAX};
|
||||
}
|
||||
auto constexpr limitDefault = 50;
|
||||
auto const limit = input.limit.value_or(limitDefault);
|
||||
auto const accountID = RPC::accountFromStringStrict(input.account);
|
||||
auto const [blobs, retCursor] = sharedPtrBackend_->fetchAccountTransactions(
|
||||
*accountID, limit, input.forward, cursor, yield);
|
||||
|
||||
Output response;
|
||||
if (retCursor)
|
||||
response.marker = {
|
||||
retCursor->ledgerSequence, retCursor->transactionIndex};
|
||||
|
||||
for (auto const& txnPlusMeta : blobs)
|
||||
{
|
||||
// over the range
|
||||
if ((txnPlusMeta.ledgerSequence < minIndex && !input.forward) ||
|
||||
(txnPlusMeta.ledgerSequence > maxIndex && input.forward))
|
||||
{
|
||||
response.marker = std::nullopt;
|
||||
break;
|
||||
}
|
||||
else if (txnPlusMeta.ledgerSequence > maxIndex && !input.forward)
|
||||
{
|
||||
gLog.debug() << "Skipping over transactions from incomplete ledger";
|
||||
continue;
|
||||
}
|
||||
|
||||
boost::json::object obj;
|
||||
if (!input.binary)
|
||||
{
|
||||
auto [txn, meta] = RPC::toExpandedJson(txnPlusMeta);
|
||||
obj[JS(meta)] = std::move(meta);
|
||||
obj[JS(tx)] = std::move(txn);
|
||||
obj[JS(tx)].as_object()[JS(ledger_index)] =
|
||||
txnPlusMeta.ledgerSequence;
|
||||
obj[JS(tx)].as_object()[JS(date)] = txnPlusMeta.date;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj[JS(meta)] = ripple::strHex(txnPlusMeta.metadata);
|
||||
obj[JS(tx_blob)] = ripple::strHex(txnPlusMeta.transaction);
|
||||
obj[JS(ledger_index)] = txnPlusMeta.ledgerSequence;
|
||||
// only clio has this field
|
||||
obj[JS(date)] = txnPlusMeta.date;
|
||||
}
|
||||
obj[JS(validated)] = true;
|
||||
response.transactions.push_back(obj);
|
||||
}
|
||||
|
||||
if (input.limit)
|
||||
response.limit = *input.limit;
|
||||
response.account = input.account;
|
||||
response.ledgerIndexMin = minIndex;
|
||||
response.ledgerIndexMax = maxIndex;
|
||||
return response;
|
||||
}
|
||||
|
||||
void
|
||||
tag_invoke(
|
||||
boost::json::value_from_tag,
|
||||
boost::json::value& jv,
|
||||
AccountTxHandler::Output const& output)
|
||||
{
|
||||
jv = {
|
||||
{JS(account), output.account},
|
||||
{JS(ledger_index_min), output.ledgerIndexMin},
|
||||
{JS(ledger_index_max), output.ledgerIndexMax},
|
||||
{JS(transactions), output.transactions},
|
||||
{JS(validated), output.validated}};
|
||||
if (output.marker)
|
||||
jv.as_object()[JS(marker)] = boost::json::value_from(*(output.marker));
|
||||
if (output.limit)
|
||||
jv.as_object()[JS(limit)] = *(output.limit);
|
||||
}
|
||||
|
||||
void
|
||||
tag_invoke(
|
||||
boost::json::value_from_tag,
|
||||
boost::json::value& jv,
|
||||
AccountTxHandler::Marker const& marker)
|
||||
{
|
||||
jv = {{JS(ledger), marker.ledger}, {JS(seq), marker.seq}};
|
||||
}
|
||||
|
||||
AccountTxHandler::Input
|
||||
tag_invoke(
|
||||
boost::json::value_to_tag<AccountTxHandler::Input>,
|
||||
boost::json::value const& jv)
|
||||
{
|
||||
auto const& jsonObject = jv.as_object();
|
||||
AccountTxHandler::Input input;
|
||||
input.account = jsonObject.at(JS(account)).as_string().c_str();
|
||||
if (jsonObject.contains(JS(ledger_index_min)) &&
|
||||
jsonObject.at(JS(ledger_index_min)).as_int64() != -1)
|
||||
{
|
||||
input.ledgerIndexMin = jsonObject.at(JS(ledger_index_min)).as_int64();
|
||||
}
|
||||
if (jsonObject.contains(JS(ledger_index_max)) &&
|
||||
jsonObject.at(JS(ledger_index_max)).as_int64() != -1)
|
||||
{
|
||||
input.ledgerIndexMax = jsonObject.at(JS(ledger_index_max)).as_int64();
|
||||
}
|
||||
if (jsonObject.contains(JS(ledger_hash)))
|
||||
{
|
||||
input.ledgerHash = jsonObject.at(JS(ledger_hash)).as_string().c_str();
|
||||
}
|
||||
if (jsonObject.contains(JS(ledger_index)))
|
||||
{
|
||||
if (!jsonObject.at(JS(ledger_index)).is_string())
|
||||
{
|
||||
input.ledgerIndex = jsonObject.at(JS(ledger_index)).as_int64();
|
||||
}
|
||||
else if (jsonObject.at(JS(ledger_index)).as_string() != "validated")
|
||||
{
|
||||
input.ledgerIndex =
|
||||
std::stoi(jsonObject.at(JS(ledger_index)).as_string().c_str());
|
||||
}
|
||||
}
|
||||
if (jsonObject.contains(JS(binary)))
|
||||
{
|
||||
input.binary = jsonObject.at(JS(binary)).as_bool();
|
||||
}
|
||||
if (jsonObject.contains(JS(forward)))
|
||||
{
|
||||
input.forward = jsonObject.at(JS(forward)).as_bool();
|
||||
}
|
||||
if (jsonObject.contains(JS(limit)))
|
||||
{
|
||||
input.limit = jsonObject.at(JS(limit)).as_int64();
|
||||
}
|
||||
if (jsonObject.contains(JS(marker)))
|
||||
{
|
||||
input.marker = AccountTxHandler::Marker{
|
||||
jsonObject.at(JS(marker)).as_object().at(JS(ledger)).as_int64(),
|
||||
jsonObject.at(JS(marker)).as_object().at(JS(seq)).as_int64()};
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
} // namespace RPCng
|
||||
127
src/rpc/ngHandlers/AccountTx.h
Normal file
127
src/rpc/ngHandlers/AccountTx.h
Normal file
@@ -0,0 +1,127 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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>
|
||||
|
||||
#include <boost/asio/spawn.hpp>
|
||||
|
||||
namespace RPCng {
|
||||
class AccountTxHandler
|
||||
{
|
||||
std::shared_ptr<BackendInterface> sharedPtrBackend_;
|
||||
|
||||
public:
|
||||
struct Marker
|
||||
{
|
||||
uint32_t ledger;
|
||||
uint32_t seq;
|
||||
};
|
||||
|
||||
struct Output
|
||||
{
|
||||
std::string account;
|
||||
uint32_t ledgerIndexMin;
|
||||
uint32_t ledgerIndexMax;
|
||||
std::optional<uint32_t> limit;
|
||||
std::optional<Marker> marker;
|
||||
// TODO: use a better type than json
|
||||
boost::json::array transactions;
|
||||
// validated should be sent via framework
|
||||
bool validated = true;
|
||||
};
|
||||
|
||||
// TODO:we did not implement the "strict" field
|
||||
struct Input
|
||||
{
|
||||
std::string account;
|
||||
// You must use at least one of the following fields in your request:
|
||||
// ledger_index, ledger_hash, ledger_index_min, or ledger_index_max.
|
||||
std::optional<std::string> ledgerHash;
|
||||
std::optional<uint32_t> ledgerIndex;
|
||||
std::optional<int32_t> ledgerIndexMin;
|
||||
std::optional<int32_t> ledgerIndexMax;
|
||||
bool binary = false;
|
||||
bool forward = false;
|
||||
std::optional<uint32_t> limit;
|
||||
std::optional<Marker> marker;
|
||||
};
|
||||
|
||||
using Result = RPCng::HandlerReturnType<Output>;
|
||||
|
||||
AccountTxHandler(std::shared_ptr<BackendInterface> const& sharedPtrBackend)
|
||||
: sharedPtrBackend_(sharedPtrBackend)
|
||||
{
|
||||
}
|
||||
|
||||
RpcSpecConstRef
|
||||
spec() const
|
||||
{
|
||||
static const RpcSpec rpcSpec = {
|
||||
{JS(account), validation::Required{}, validation::AccountValidator},
|
||||
{JS(ledger_hash), validation::Uint256HexStringValidator},
|
||||
{JS(ledger_index), validation::LedgerIndexValidator},
|
||||
{JS(ledger_index_min), validation::Type<int32_t>{}},
|
||||
{JS(ledger_index_max), validation::Type<int32_t>{}},
|
||||
{JS(binary), validation::Type<bool>{}},
|
||||
{JS(forward), validation::Type<bool>{}},
|
||||
{JS(limit),
|
||||
validation::Type<uint32_t>{},
|
||||
validation::Between{1, 100}},
|
||||
{JS(marker),
|
||||
validation::WithCustomError{
|
||||
validation::Type<boost::json::object>{},
|
||||
RPC::Status{
|
||||
RPC::RippledError::rpcINVALID_PARAMS, "invalidMarker"}},
|
||||
validation::Section{
|
||||
{JS(ledger),
|
||||
validation::Required{},
|
||||
validation::Type<uint32_t>{}},
|
||||
{JS(seq),
|
||||
validation::Required{},
|
||||
validation::Type<uint32_t>{}},
|
||||
}}};
|
||||
return rpcSpec;
|
||||
}
|
||||
|
||||
Result
|
||||
process(Input input, boost::asio::yield_context& yield) const;
|
||||
};
|
||||
|
||||
void
|
||||
tag_invoke(
|
||||
boost::json::value_from_tag,
|
||||
boost::json::value& jv,
|
||||
AccountTxHandler::Output const& output);
|
||||
|
||||
void
|
||||
tag_invoke(
|
||||
boost::json::value_from_tag,
|
||||
boost::json::value& jv,
|
||||
AccountTxHandler::Marker const& marker);
|
||||
|
||||
AccountTxHandler::Input
|
||||
tag_invoke(
|
||||
boost::json::value_to_tag<AccountTxHandler::Input>,
|
||||
boost::json::value const& jv);
|
||||
} // namespace RPCng
|
||||
Reference in New Issue
Block a user