feat: AmendmentCenter (#1418)

Fixes #1416
This commit is contained in:
Alex Kremer
2024-06-27 18:21:30 +01:00
committed by GitHub
parent 2ff51ff416
commit 7bd21345a1
21 changed files with 865 additions and 151 deletions

View File

@@ -1,49 +0,0 @@
//------------------------------------------------------------------------------
/*
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 <xrpl/basics/Slice.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/digest.h>
#include <string_view>
namespace rpc {
#define REGISTER_AMENDMENT(name) inline static const ripple::uint256 name = GetAmendmentId(#name);
/**
* @brief Represents a list of amendments in the XRPL.
*/
struct Amendments {
/**
* @param name The name of the amendment
* @return The corresponding amendment Id
*/
static ripple::uint256
GetAmendmentId(std::string_view const name)
{
return ripple::sha512Half(ripple::Slice(name.data(), name.size()));
}
REGISTER_AMENDMENT(DisallowIncoming)
REGISTER_AMENDMENT(Clawback)
};
} // namespace rpc

View File

@@ -1288,25 +1288,6 @@ getNFTID(boost::json::object const& request)
return tokenid;
}
bool
isAmendmentEnabled(
std::shared_ptr<data::BackendInterface const> const& backend,
boost::asio::yield_context yield,
uint32_t seq,
ripple::uint256 amendmentId
)
{
// the amendments should always be present in ledger
auto const& amendments = backend->fetchLedgerObject(ripple::keylet::amendments().key, seq, yield);
ripple::SLE const amendmentsSLE{
ripple::SerialIter{amendments->data(), amendments->size()}, ripple::keylet::amendments().key
};
auto const listAmendments = amendmentsSLE.getFieldV256(ripple::sfAmendments);
return std::find(listAmendments.begin(), listAmendments.end(), amendmentId) != listAmendments.end();
}
boost::json::object
toJsonWithBinaryTx(data::TransactionAndMetadata const& txnPlusMeta, std::uint32_t const apiVersion)
{

View File

@@ -566,23 +566,6 @@ specifiesCurrentOrClosedLedger(boost::json::object const& request);
std::variant<ripple::uint256, Status>
getNFTID(boost::json::object const& request);
/**
* @brief Check if the amendment is enabled
*
* @param backend The backend to use
* @param yield The yield context
* @param seq The ledger sequence
* @param amendmentId The amendment ID
* @return true if the amendment is enabled
*/
bool
isAmendmentEnabled(
std::shared_ptr<data::BackendInterface const> const& backend,
boost::asio::yield_context yield,
uint32_t seq,
ripple::uint256 amendmentId
);
/**
* @brief Encode CTID as string
*

View File

@@ -19,6 +19,8 @@
#include "rpc/common/impl/HandlerProvider.hpp"
#include "data/AmendmentCenter.hpp"
#include "data/AmendmentCenterInterface.hpp"
#include "data/BackendInterface.hpp"
#include "etl/ETLService.hpp"
#include "feed/SubscriptionManager.hpp"
@@ -71,12 +73,13 @@ ProductionHandlerProvider::ProductionHandlerProvider(
std::shared_ptr<feed::SubscriptionManager> const& subscriptionManager,
std::shared_ptr<etl::LoadBalancer> const& balancer,
std::shared_ptr<etl::ETLService const> const& etl,
std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter,
Counters const& counters
)
: handlerMap_{
{"account_channels", {AccountChannelsHandler{backend}}},
{"account_currencies", {AccountCurrenciesHandler{backend}}},
{"account_info", {AccountInfoHandler{backend}}},
{"account_info", {AccountInfoHandler{backend, amendmentCenter}}},
{"account_lines", {AccountLinesHandler{backend}}},
{"account_nfts", {AccountNFTsHandler{backend}}},
{"account_objects", {AccountObjectsHandler{backend}}},

View File

@@ -19,6 +19,7 @@
#pragma once
#include "data/AmendmentCenterInterface.hpp"
#include "data/BackendInterface.hpp"
#include "feed/SubscriptionManager.hpp"
#include "rpc/common/AnyHandler.hpp"
@@ -59,6 +60,7 @@ public:
std::shared_ptr<feed::SubscriptionManager> const& subscriptionManager,
std::shared_ptr<etl::LoadBalancer> const& balancer,
std::shared_ptr<etl::ETLService const> const& etl,
std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter,
Counters const& counters
);

View File

@@ -19,7 +19,7 @@
#include "rpc/handlers/AccountInfo.hpp"
#include "rpc/Amendments.hpp"
#include "data/AmendmentCenter.hpp"
#include "rpc/Errors.hpp"
#include "rpc/JS.hpp"
#include "rpc/RPCHelpers.hpp"
@@ -52,6 +52,8 @@ namespace rpc {
AccountInfoHandler::Result
AccountInfoHandler::process(AccountInfoHandler::Input input, Context const& ctx) const
{
using namespace data;
if (!input.account && !input.ident)
return Error{Status{RippledError::rpcINVALID_PARAMS, ripple::RPC::missing_field_message(JS(account))}};
@@ -79,11 +81,12 @@ AccountInfoHandler::process(AccountInfoHandler::Input input, Context const& ctx)
if (!accountKeylet.check(sle))
return Error{Status{RippledError::rpcDB_DESERIALIZATION}};
auto const isDisallowIncomingEnabled =
rpc::isAmendmentEnabled(sharedPtrBackend_, ctx.yield, lgrInfo.seq, rpc::Amendments::DisallowIncoming);
auto isEnabled = [this, &ctx, seq = lgrInfo.seq](auto key) {
return amendmentCenter_->isEnabled(ctx.yield, key, seq);
};
auto const isClawbackEnabled =
rpc::isAmendmentEnabled(sharedPtrBackend_, ctx.yield, lgrInfo.seq, rpc::Amendments::Clawback);
auto const isDisallowIncomingEnabled = isEnabled(Amendments::DisallowIncoming);
auto const isClawbackEnabled = isEnabled(Amendments::Clawback);
// Return SignerList(s) if that is requested.
if (input.signerLists) {

View File

@@ -19,6 +19,7 @@
#pragma once
#include "data/AmendmentCenterInterface.hpp"
#include "data/BackendInterface.hpp"
#include "rpc/JS.hpp"
#include "rpc/common/Checkers.hpp"
@@ -48,6 +49,7 @@ namespace rpc {
*/
class AccountInfoHandler {
std::shared_ptr<BackendInterface> sharedPtrBackend_;
std::shared_ptr<data::AmendmentCenterInterface const> amendmentCenter_;
public:
/**
@@ -115,8 +117,13 @@ public:
* @brief Construct a new AccountInfoHandler object
*
* @param sharedPtrBackend The backend to use
* @param amendmentCenter The amendment center to use
*/
AccountInfoHandler(std::shared_ptr<BackendInterface> const& sharedPtrBackend) : sharedPtrBackend_(sharedPtrBackend)
AccountInfoHandler(
std::shared_ptr<BackendInterface> const& sharedPtrBackend,
std::shared_ptr<data::AmendmentCenterInterface const> const& amendmentCenter
)
: sharedPtrBackend_(sharedPtrBackend), amendmentCenter_{amendmentCenter}
{
}