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

@@ -59,6 +59,7 @@ target_sources(clio PRIVATE
## NextGen RPC handler
src/rpc/ngHandlers/AccountChannels.cpp
src/rpc/ngHandlers/AccountCurrencies.cpp
src/rpc/ngHandlers/Tx.cpp
## RPC Methods
# Account
src/rpc/handlers/AccountChannels.cpp
@@ -120,7 +121,8 @@ if(BUILD_TESTS)
unittests/rpc/handlers/AccountCurrenciesTest.cpp
unittests/rpc/handlers/DefaultProcessorTests.cpp
unittests/rpc/handlers/PingTest.cpp
unittests/rpc/handlers/AccountChannelsTest.cpp)
unittests/rpc/handlers/AccountChannelsTest.cpp
unittests/rpc/handlers/TxTest.cpp)
include(CMake/deps/gtest.cmake)
# if CODE_COVERAGE enable, add clio_test-ccov

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

View File

@@ -323,3 +323,22 @@ TEST_F(RPCBaseTest, MarkerValidator)
auto passingInput = json::parse(R"({ "account": "ABAB1234:123" })");
ASSERT_TRUE(spec.validate(passingInput));
}
TEST_F(RPCBaseTest, TxHashValidator)
{
auto const spec = RpcSpec{{"transaction", TxHashValidator}};
auto const passingInput = json::parse(
R"({ "transaction": "1B8590C01B0006EDFA9ED60296DD052DC5E90F99659B25014D08E1BC983515BC"})");
ASSERT_TRUE(spec.validate(passingInput));
auto failingInput = json::parse(R"({ "transaction": 256})");
auto err = spec.validate(failingInput);
ASSERT_FALSE(err);
ASSERT_EQ(err.error().message, "transactionNotString");
failingInput = json::parse(
R"({ "transaction": "1B8590C01B0006EDFA9ED60296DD052DC5E90F99659B25014D08E1BC"})");
err = spec.validate(failingInput);
ASSERT_FALSE(err);
ASSERT_EQ(err.error().message, "malformedTransaction");
}

View File

@@ -0,0 +1,275 @@
//------------------------------------------------------------------------------
/*
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/common/AnyHandler.h>
#include <rpc/ngHandlers/Tx.h>
#include <util/Fixtures.h>
#include <util/TestObject.h>
#include <fmt/core.h>
using namespace RPCng;
namespace json = boost::json;
using namespace testing;
constexpr static auto TXNID =
"05FB0EB4B899F056FA095537C5817163801F544BAFCEA39C995D76DB4D16F9DD";
constexpr static auto ACCOUNT = "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn";
constexpr static auto ACCOUNT2 = "rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun";
constexpr static auto CURRENCY = "0158415500000000C1F76FF6ECB0BAC600000000";
class RPCTxTest : public HandlerBaseTest
{
};
TEST_F(RPCTxTest, ExcessiveLgrRange)
{
boost::asio::spawn(ctx, [this](boost::asio::yield_context yield) {
auto const handler = AnyHandler{TxHandler{mockBackendPtr}};
auto const req = json::parse(fmt::format(
R"({{
"command": "tx",
"transaction": "{}",
"min_ledger": 1,
"max_ledger":1002
}})",
TXNID));
auto const output = handler.process(req, yield);
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "excessiveLgrRange");
EXPECT_EQ(
err.at("error_message").as_string(), "Ledger range exceeds 1000.");
});
ctx.run();
}
TEST_F(RPCTxTest, InvalidLgrRange)
{
boost::asio::spawn(ctx, [this](boost::asio::yield_context yield) {
auto const handler = AnyHandler{TxHandler{mockBackendPtr}};
auto const req = json::parse(fmt::format(
R"({{
"command": "tx",
"transaction": "{}",
"max_ledger": 1,
"min_ledger": 10
}})",
TXNID));
auto const output = handler.process(req, yield);
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "invalidLgrRange");
EXPECT_EQ(
err.at("error_message").as_string(), "Ledger range is invalid.");
});
ctx.run();
}
TEST_F(RPCTxTest, TxnNotFound)
{
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
ON_CALL(*rawBackendPtr, fetchTransaction(ripple::uint256{TXNID}, _))
.WillByDefault(Return(std::optional<TransactionAndMetadata>{}));
EXPECT_CALL(*rawBackendPtr, fetchTransaction).Times(1);
boost::asio::spawn(ctx, [this](boost::asio::yield_context yield) {
auto const handler = AnyHandler{TxHandler{mockBackendPtr}};
auto const req = json::parse(fmt::format(
R"({{
"command": "tx",
"transaction": "{}"
}})",
TXNID));
auto const output = handler.process(req, yield);
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "txnNotFound");
EXPECT_EQ(
err.at("error_message").as_string(), "Transaction not found.");
});
ctx.run();
}
TEST_F(RPCTxTest, TxnNotFoundInGivenRangeSearchAllFalse)
{
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
mockBackendPtr->updateRange(10); // min
mockBackendPtr->updateRange(30); // max
ON_CALL(*rawBackendPtr, fetchTransaction(ripple::uint256{TXNID}, _))
.WillByDefault(Return(std::optional<TransactionAndMetadata>{}));
EXPECT_CALL(*rawBackendPtr, fetchTransaction).Times(1);
boost::asio::spawn(ctx, [this](boost::asio::yield_context yield) {
auto const handler = AnyHandler{TxHandler{mockBackendPtr}};
auto const req = json::parse(fmt::format(
R"({{
"command": "tx",
"transaction": "{}",
"min_ledger": 1,
"max_ledger":1000
}})",
TXNID));
auto const output = handler.process(req, yield);
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "txnNotFound");
EXPECT_EQ(
err.at("error_message").as_string(), "Transaction not found.");
EXPECT_EQ(err.at("searched_all").as_bool(), false);
});
ctx.run();
}
TEST_F(RPCTxTest, TxnNotFoundInGivenRangeSearchAllTrue)
{
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
mockBackendPtr->updateRange(1); // min
mockBackendPtr->updateRange(1000); // max
ON_CALL(*rawBackendPtr, fetchTransaction(ripple::uint256{TXNID}, _))
.WillByDefault(Return(std::optional<TransactionAndMetadata>{}));
EXPECT_CALL(*rawBackendPtr, fetchTransaction).Times(1);
boost::asio::spawn(ctx, [this](boost::asio::yield_context yield) {
auto const handler = AnyHandler{TxHandler{mockBackendPtr}};
auto const req = json::parse(fmt::format(
R"({{
"command": "tx",
"transaction": "{}",
"min_ledger": 1,
"max_ledger":1000
}})",
TXNID));
auto const output = handler.process(req, yield);
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "txnNotFound");
EXPECT_EQ(
err.at("error_message").as_string(), "Transaction not found.");
EXPECT_EQ(err.at("searched_all").as_bool(), true);
});
ctx.run();
}
TEST_F(RPCTxTest, DefaultParameter)
{
auto constexpr static OUT = R"({
"Account":"rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"Fee":"2",
"Sequence":100,
"SigningPubKey":"74657374",
"TakerGets":{
"currency":"0158415500000000C1F76FF6ECB0BAC600000000",
"issuer":"rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun",
"value":"200"
},
"TakerPays":"300",
"TransactionType":"OfferCreate",
"hash":"2E2FBAAFF767227FE4381C4BE9855986A6B9F96C62F6E443731AB36F7BBB8A08",
"meta":{
"AffectedNodes":[
{
"CreatedNode":{
"LedgerEntryType":"Offer",
"NewFields":{
"TakerGets":"200",
"TakerPays":{
"currency":"0158415500000000C1F76FF6ECB0BAC600000000",
"issuer":"rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"value":"300"
}
}
}
}
],
"TransactionIndex":100,
"TransactionResult":"tesSUCCESS"
},
"date":123456,
"ledger_index":100
})";
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
TransactionAndMetadata tx;
tx.metadata = CreateMetaDataForCreateOffer(CURRENCY, ACCOUNT, 100, 200, 300)
.getSerializer()
.peekData();
tx.transaction = CreateCreateOfferTransactionObject(
ACCOUNT, 2, 100, CURRENCY, ACCOUNT2, 200, 300)
.getSerializer()
.peekData();
tx.date = 123456;
tx.ledgerSequence = 100;
ON_CALL(*rawBackendPtr, fetchTransaction(ripple::uint256{TXNID}, _))
.WillByDefault(Return(tx));
EXPECT_CALL(*rawBackendPtr, fetchTransaction).Times(1);
boost::asio::spawn(ctx, [this](boost::asio::yield_context yield) {
auto const handler = AnyHandler{TxHandler{mockBackendPtr}};
auto const req = json::parse(fmt::format(
R"({{
"command": "tx",
"transaction": "{}"
}})",
TXNID));
auto const output = handler.process(req, yield);
ASSERT_TRUE(output);
EXPECT_EQ(*output, json::parse(OUT));
});
ctx.run();
}
TEST_F(RPCTxTest, ReturnBinary)
{
auto constexpr static OUT = R"({
"meta":"201C00000064F8E311006FE864D50AA87BEE5380000158415500000000C1F76FF6ECB0BAC6000000004B4E9C06F24296074F7BC48F92A97916C6DC5EA96540000000000000C8E1E1F1031000",
"tx":"120007240000006464400000000000012C65D5071AFD498D00000158415500000000C1F76FF6ECB0BAC600000000D31252CF902EF8DD8451243869B38667CBD89DF368400000000000000273047465737481144B4E9C06F24296074F7BC48F92A97916C6DC5EA9",
"hash":"05FB0EB4B899F056FA095537C5817163801F544BAFCEA39C995D76DB4D16F9DD",
"date":123456,
"ledger_index":100
})";
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
TransactionAndMetadata tx;
tx.metadata = CreateMetaDataForCreateOffer(CURRENCY, ACCOUNT, 100, 200, 300)
.getSerializer()
.peekData();
tx.transaction = CreateCreateOfferTransactionObject(
ACCOUNT, 2, 100, CURRENCY, ACCOUNT2, 200, 300)
.getSerializer()
.peekData();
tx.date = 123456;
tx.ledgerSequence = 100;
ON_CALL(*rawBackendPtr, fetchTransaction(ripple::uint256{TXNID}, _))
.WillByDefault(Return(tx));
EXPECT_CALL(*rawBackendPtr, fetchTransaction).Times(1);
boost::asio::spawn(ctx, [this](boost::asio::yield_context yield) {
auto const handler = AnyHandler{TxHandler{mockBackendPtr}};
auto const req = json::parse(fmt::format(
R"({{
"command": "tx",
"transaction": "{}",
"binary": true
}})",
TXNID));
auto const output = handler.process(req, yield);
ASSERT_TRUE(output);
EXPECT_EQ(*output, json::parse(OUT));
});
ctx.run();
}

View File

@@ -62,7 +62,7 @@ inline void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
TestOutput output)
TestOutput const& output)
{
jv = {{"computed", output.computed}};
}
@@ -194,7 +194,7 @@ inline void
tag_invoke(
boost::json::value_from_tag,
boost::json::value& jv,
InOutFake output)
InOutFake const& output)
{
jv = {{"something", output.something}};
}