mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-26 22:55:53 +00:00
@@ -193,6 +193,7 @@ if(tests)
|
||||
unittests/rpc/handlers/AccountObjectsTest.cpp
|
||||
unittests/rpc/handlers/BookChangesTest.cpp
|
||||
unittests/rpc/handlers/LedgerTest.cpp
|
||||
unittests/rpc/handlers/VersionHandlerTest.cpp
|
||||
# Backend
|
||||
unittests/backend/BackendFactoryTest.cpp
|
||||
unittests/backend/cassandra/BaseTests.cpp
|
||||
|
||||
@@ -201,8 +201,8 @@ try
|
||||
|
||||
auto workQueue = WorkQueue::make_WorkQueue(config);
|
||||
auto counters = RPC::Counters::make_Counters(workQueue);
|
||||
auto const handlerProvider =
|
||||
std::make_shared<RPC::detail::ProductionHandlerProvider const>(backend, subscriptions, balancer, etl, counters);
|
||||
auto const handlerProvider = std::make_shared<RPC::detail::ProductionHandlerProvider const>(
|
||||
backend, subscriptions, balancer, etl, counters, config);
|
||||
auto const rpcEngine = RPC::RPCEngine::make_RPCEngine(
|
||||
config, backend, subscriptions, balancer, etl, dosGuard, workQueue, counters, handlerProvider);
|
||||
|
||||
|
||||
@@ -74,6 +74,24 @@ public:
|
||||
|
||||
util::Expected<uint32_t, std::string>
|
||||
parse(boost::json::object const& request) const override;
|
||||
|
||||
inline uint32_t
|
||||
getDefaultVersion() const
|
||||
{
|
||||
return defaultVersion_;
|
||||
}
|
||||
|
||||
inline uint32_t
|
||||
getMinVersion() const
|
||||
{
|
||||
return minVersion_;
|
||||
}
|
||||
|
||||
inline uint32_t
|
||||
getMaxVersion() const
|
||||
{
|
||||
return maxVersion_;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace RPC::detail
|
||||
|
||||
@@ -51,6 +51,7 @@
|
||||
#include <rpc/handlers/TransactionEntry.h>
|
||||
#include <rpc/handlers/Tx.h>
|
||||
#include <rpc/handlers/Unsubscribe.h>
|
||||
#include <rpc/handlers/VersionHandler.h>
|
||||
|
||||
namespace RPC::detail {
|
||||
|
||||
@@ -59,7 +60,8 @@ ProductionHandlerProvider::ProductionHandlerProvider(
|
||||
std::shared_ptr<SubscriptionManager> const& subscriptionManager,
|
||||
std::shared_ptr<LoadBalancer> const& balancer,
|
||||
std::shared_ptr<ETLService const> const& etl,
|
||||
Counters const& counters)
|
||||
Counters const& counters,
|
||||
clio::Config const& config)
|
||||
: handlerMap_{
|
||||
{"account_channels", {AccountChannelsHandler{backend}}},
|
||||
{"account_currencies", {AccountCurrenciesHandler{backend}}},
|
||||
@@ -89,6 +91,7 @@ ProductionHandlerProvider::ProductionHandlerProvider(
|
||||
{"tx", {TxHandler{backend}}},
|
||||
{"subscribe", {SubscribeHandler{backend, subscriptionManager}}},
|
||||
{"unsubscribe", {UnsubscribeHandler{backend, subscriptionManager}}},
|
||||
{"version", {VersionHandler{config}}},
|
||||
}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -54,7 +54,8 @@ public:
|
||||
std::shared_ptr<SubscriptionManager> const& subscriptionManager,
|
||||
std::shared_ptr<LoadBalancer> const& balancer,
|
||||
std::shared_ptr<ETLService const> const& etl,
|
||||
Counters const& counters);
|
||||
Counters const& counters,
|
||||
clio::Config const& config);
|
||||
|
||||
bool
|
||||
contains(std::string const& method) const override;
|
||||
|
||||
83
src/rpc/handlers/VersionHandler.h
Normal file
83
src/rpc/handlers/VersionHandler.h
Normal file
@@ -0,0 +1,83 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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 <rpc/Errors.h>
|
||||
#include <rpc/RPCHelpers.h>
|
||||
#include <rpc/common/APIVersion.h>
|
||||
#include <rpc/common/Types.h>
|
||||
#include <rpc/common/impl/APIVersionParser.h>
|
||||
|
||||
namespace RPC {
|
||||
|
||||
/**
|
||||
* @brief The version command returns the min,max and current api Version we are using
|
||||
*
|
||||
*/
|
||||
class VersionHandler
|
||||
{
|
||||
RPC::detail::ProductionAPIVersionParser apiVersionParser_;
|
||||
|
||||
public:
|
||||
struct Output
|
||||
{
|
||||
uint32_t minVersion;
|
||||
uint32_t maxVersion;
|
||||
uint32_t currVersion;
|
||||
};
|
||||
|
||||
explicit VersionHandler(clio::Config const& config)
|
||||
: apiVersionParser_(
|
||||
config.valueOr("default", API_VERSION_DEFAULT),
|
||||
config.valueOr("min", API_VERSION_MIN),
|
||||
config.valueOr("max", API_VERSION_MAX))
|
||||
{
|
||||
}
|
||||
|
||||
using Result = HandlerReturnType<Output>;
|
||||
|
||||
Result
|
||||
process([[maybe_unused]] Context const& ctx) const
|
||||
{
|
||||
using namespace RPC;
|
||||
|
||||
auto output = Output{};
|
||||
output.currVersion = apiVersionParser_.getDefaultVersion();
|
||||
output.minVersion = apiVersionParser_.getMinVersion();
|
||||
output.maxVersion = apiVersionParser_.getMaxVersion();
|
||||
return output;
|
||||
}
|
||||
|
||||
private:
|
||||
friend void
|
||||
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output)
|
||||
{
|
||||
jv = {
|
||||
{"version",
|
||||
{
|
||||
{"first", output.minVersion},
|
||||
{"last", output.maxVersion},
|
||||
{"good", output.currVersion},
|
||||
}},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace RPC
|
||||
66
unittests/rpc/handlers/VersionHandlerTest.cpp
Normal file
66
unittests/rpc/handlers/VersionHandlerTest.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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 <util/Fixtures.h>
|
||||
#include <util/TestObject.h>
|
||||
|
||||
#include <rpc/common/AnyHandler.h>
|
||||
#include <rpc/handlers/VersionHandler.h>
|
||||
|
||||
|
||||
constexpr static auto DEFAULT_API_VERSION = 3u;
|
||||
constexpr static auto MIN_API_VERSION = 2u;
|
||||
constexpr static auto MAX_API_VERSION = 10u;
|
||||
|
||||
using namespace RPC;
|
||||
namespace json = boost::json;
|
||||
|
||||
class RPCVersionHandlerTest : public HandlerBaseTest
|
||||
{
|
||||
};
|
||||
|
||||
TEST_F(RPCVersionHandlerTest, Default)
|
||||
{
|
||||
clio::Config cfg{json::parse(fmt::format(
|
||||
R"({{
|
||||
"min": {},
|
||||
"max": {},
|
||||
"default": {}
|
||||
}})",
|
||||
MIN_API_VERSION,
|
||||
MAX_API_VERSION,
|
||||
DEFAULT_API_VERSION))};
|
||||
|
||||
runSpawn([&](auto& yield) {
|
||||
auto const handler = AnyHandler{VersionHandler{cfg}};
|
||||
auto const output = handler.process(static_cast<json::value>(cfg), Context{std::ref(yield)});
|
||||
ASSERT_TRUE(output);
|
||||
|
||||
// check all against all the correct values
|
||||
auto const& result = output.value().as_object();
|
||||
EXPECT_TRUE(result.contains("version"));
|
||||
auto const& info = result.at("version").as_object();
|
||||
EXPECT_TRUE(info.contains("first"));
|
||||
EXPECT_TRUE(info.contains("last"));
|
||||
EXPECT_TRUE(info.contains("good"));
|
||||
EXPECT_EQ(info.at("first"), 2u);
|
||||
EXPECT_EQ(info.at("last"), 10u);
|
||||
EXPECT_EQ(info.at("good"), 3u);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user