Tx handler in new RPC framework (#526)

Fixes #527
This commit is contained in:
cyan317
2023-02-28 09:35:13 +00:00
committed by GitHub
parent a3211f4458
commit 67f0fa26ae
13 changed files with 549 additions and 14 deletions

View File

@@ -58,7 +58,10 @@ struct VoidOutput
};
inline void
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, VoidOutput)
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
VoidOutput const&)
{
jv = boost::json::object{};
}

View File

@@ -165,4 +165,21 @@ CustomValidator MarkerValidator = CustomValidator{
return MaybeError{};
}};
CustomValidator TxHashValidator = CustomValidator{
[](boost::json::value const& value, std::string_view key) -> MaybeError {
if (!value.is_string())
{
return Error{RPC::Status{
RPC::RippledError::rpcINVALID_PARAMS,
std::string(key) + "NotString"}};
}
ripple::uint256 txHash;
if (!txHash.parseHex(value.as_string().c_str()))
{
return Error{RPC::Status{
RPC::RippledError::rpcINVALID_PARAMS, "malformedTransaction"}};
}
return MaybeError{};
}};
} // namespace RPCng::validation

View File

@@ -373,4 +373,10 @@ extern CustomValidator AccountValidator;
*/
extern CustomValidator MarkerValidator;
/**
* @brief Provide a common used validator for transaction hash
* It must be a string and hex
*/
extern CustomValidator TxHashValidator;
} // namespace RPCng::validation

View File

@@ -164,7 +164,7 @@ void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
AccountChannelsHandler::Output output)
AccountChannelsHandler::Output const& output)
{
boost::json::object obj;
obj = {
@@ -173,7 +173,7 @@ tag_invoke(
{"ledger_index", output.ledgerIndex},
{"validated", output.validated},
{"limit", output.limit},
{"channels", boost::json::value_from(output.channels)}};
{"channels", output.channels}};
if (output.marker)
obj["marker"] = output.marker.value();
jv = obj;
@@ -183,7 +183,7 @@ void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
AccountChannelsHandler::ChannelResponse channel)
AccountChannelsHandler::ChannelResponse const& channel)
{
boost::json::object obj;
obj = {

View File

@@ -116,11 +116,11 @@ void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
AccountChannelsHandler::Output output);
AccountChannelsHandler::Output const& output);
void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
AccountChannelsHandler::ChannelResponse channel);
AccountChannelsHandler::ChannelResponse const& channel);
} // namespace RPCng

View File

@@ -88,15 +88,14 @@ void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
AccountCurrenciesHandler::Output output)
AccountCurrenciesHandler::Output const& output)
{
jv = {
{"ledger_hash", output.ledgerHash},
{"ledger_index", output.ledgerIndex},
{"validated", output.validated},
{"receive_currencies",
boost::json::value_from(output.receiveCurrencies)},
{"send_currencies", boost::json::value_from(output.sendCurrencies)}};
{"receive_currencies", output.receiveCurrencies},
{"send_currencies", output.sendCurrencies}};
}
AccountCurrenciesHandler::Input

View File

@@ -78,7 +78,7 @@ void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
AccountCurrenciesHandler::Output output);
AccountCurrenciesHandler::Output const& output);
AccountCurrenciesHandler::Input
tag_invoke(

123
src/rpc/ngHandlers/Tx.cpp Normal file
View File

@@ -0,0 +1,123 @@
//------------------------------------------------------------------------------
/*
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/RPCHelpers.h>
#include <rpc/ngHandlers/Tx.h>
namespace RPCng {
TxHandler::Result
TxHandler::process(Input input, boost::asio::yield_context& yield) const
{
constexpr static auto maxLedgerRange = 1000u;
auto const rangeSupplied = input.minLedger && input.maxLedger;
if (rangeSupplied)
{
if (*input.minLedger > *input.maxLedger)
return Error{RPC::Status{RPC::RippledError::rpcINVALID_LGR_RANGE}};
if (*input.maxLedger - *input.minLedger > maxLedgerRange)
return Error{
RPC::Status{RPC::RippledError::rpcEXCESSIVE_LGR_RANGE}};
}
TxHandler::Output output;
auto const dbResponse = sharedPtrBackend_->fetchTransaction(
ripple::uint256{std::string_view(input.transaction)}, yield);
if (!dbResponse)
{
if (rangeSupplied)
{
auto const range = sharedPtrBackend_->fetchLedgerRange();
auto const searchedAll = range->maxSequence >= *input.maxLedger &&
range->minSequence <= *input.minLedger;
boost::json::object extra;
extra["searched_all"] = searchedAll;
return Error{RPC::Status{
RPC::RippledError::rpcTXN_NOT_FOUND, std::move(extra)}};
}
return Error{RPC::Status{RPC::RippledError::rpcTXN_NOT_FOUND}};
}
// clio does not implement 'inLedger' which is a deprecated field
if (!input.binary)
{
auto const [txn, meta] = RPC::toExpandedJson(*dbResponse);
output.tx = txn;
output.meta = meta;
}
else
{
output.txStr = ripple::strHex(dbResponse->transaction);
output.metaStr = ripple::strHex(dbResponse->metadata);
output.hash = std::move(input.transaction);
}
output.date = dbResponse->date;
output.ledgerIndex = dbResponse->ledgerSequence;
return output;
}
void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
TxHandler::Output const& output)
{
auto obj = boost::json::object{};
if (output.tx)
{
obj = *output.tx;
obj["meta"] = *output.meta;
}
else
{
obj["meta"] = *output.metaStr;
obj["tx"] = *output.txStr;
obj["hash"] = output.hash;
}
obj["date"] = output.date;
obj["ledger_index"] = output.ledgerIndex;
jv = std::move(obj);
}
TxHandler::Input
tag_invoke(
boost::json::value_to_tag<TxHandler::Input>,
boost::json::value const& jv)
{
TxHandler::Input input;
auto const& jsonObject = jv.as_object();
input.transaction = jv.at("transaction").as_string().c_str();
if (jsonObject.contains("binary"))
{
input.binary = jv.at("binary").as_bool();
}
if (jsonObject.contains("min_ledger"))
{
input.minLedger = jv.at("min_ledger").as_int64();
}
if (jsonObject.contains("max_ledger"))
{
input.maxLedger = jv.at("max_ledger").as_int64();
}
return input;
}
} // namespace RPCng

91
src/rpc/ngHandlers/Tx.h Normal file
View File

@@ -0,0 +1,91 @@
//------------------------------------------------------------------------------
/*
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/common/Types.h>
#include <rpc/common/Validators.h>
#include <boost/asio/spawn.hpp>
namespace RPCng {
class TxHandler
{
std::shared_ptr<BackendInterface> sharedPtrBackend_;
public:
struct Output
{
uint32_t date;
std::string hash;
uint32_t ledgerIndex;
std::optional<boost::json::object> meta;
std::optional<boost::json::object> tx;
std::optional<std::string> metaStr;
std::optional<std::string> txStr;
bool validated = true;
};
// TODO: we did not implement the "strict" field
struct Input
{
std::string transaction;
bool binary = false;
std::optional<uint32_t> minLedger;
std::optional<uint32_t> maxLedger;
};
using Result = RPCng::HandlerReturnType<Output>;
TxHandler(std::shared_ptr<BackendInterface> const& sharedPtrBackend)
: sharedPtrBackend_(sharedPtrBackend)
{
}
RpcSpecConstRef
spec() const
{
static const RpcSpec rpcSpec = {
{"transaction",
validation::Required{},
validation::TxHashValidator},
{"binary", validation::Type<bool>{}},
{"min_ledger", validation::Type<uint32_t>{}},
{"max_ledger", 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,
TxHandler::Output const& output);
TxHandler::Input
tag_invoke(
boost::json::value_to_tag<TxHandler::Input>,
boost::json::value const& jv);
} // namespace RPCng