Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account_info add flags #812

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ if(tests)
unittests/rpc/APIVersionTests.cpp
unittests/rpc/ForwardingProxyTests.cpp
unittests/rpc/WorkQueueTest.cpp
unittests/rpc/AmendmentsTest.cpp
## RPC handlers
unittests/rpc/handlers/DefaultProcessorTests.cpp
unittests/rpc/handlers/TestHandlerTests.cpp
Expand Down
41 changes: 41 additions & 0 deletions src/rpc/Amendments.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//------------------------------------------------------------------------------
/*
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 <ripple/basics/base_uint.h>

#include <ripple/protocol/digest.h>

namespace RPC {

#define REGISTER_AMENDMENT(name) inline static const ripple::uint256 name = GetAmendmentId(#name);

struct Amendments
{
static ripple::uint256 const
GetAmendmentId(std::string_view const name)
{
return ripple::sha512Half(ripple::Slice(name.data(), name.size()));
}

REGISTER_AMENDMENT(DisallowIncoming)
REGISTER_AMENDMENT(Clawback)
};
} // namespace RPC
17 changes: 17 additions & 0 deletions src/rpc/RPCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1325,4 +1325,21 @@ getNFTID(boost::json::object const& request)
return tokenid;
}

bool
isAmendmentEnabled(
std::shared_ptr<Backend::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 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();
}

} // namespace RPC
8 changes: 8 additions & 0 deletions src/rpc/RPCHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include <backend/BackendInterface.h>
#include <rpc/Amendments.h>
#include <rpc/JS.h>
#include <rpc/common/Types.h>
#include <util/JsonUtils.h>
Expand Down Expand Up @@ -216,6 +217,13 @@ specifiesCurrentOrClosedLedger(boost::json::object const& request);
std::variant<ripple::uint256, Status>
getNFTID(boost::json::object const& request);

bool
isAmendmentEnabled(
std::shared_ptr<Backend::BackendInterface const> const& backend,
boost::asio::yield_context yield,
uint32_t seq,
ripple::uint256 amendmentId);

template <class T>
void
logDuration(Web::Context const& ctx, T const& dur)
Expand Down
36 changes: 27 additions & 9 deletions src/rpc/handlers/AccountInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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 const isClawbackEnabled =
RPC::isAmendmentEnabled(sharedPtrBackend_, ctx.yield, lgrInfo.seq, RPC::Amendments::Clawback);

// Return SignerList(s) if that is requested.
if (input.signerLists)
{
Expand All @@ -73,10 +79,11 @@ AccountInfoHandler::process(AccountInfoHandler::Input input, Context const& ctx)
signerList.push_back(sleSigners);
}

return Output(lgrInfo.seq, ripple::strHex(lgrInfo.hash), sle, signerList);
return Output(
lgrInfo.seq, ripple::strHex(lgrInfo.hash), sle, isDisallowIncomingEnabled, isClawbackEnabled, signerList);
}

return Output(lgrInfo.seq, ripple::strHex(lgrInfo.hash), sle);
return Output(lgrInfo.seq, ripple::strHex(lgrInfo.hash), sle, isDisallowIncomingEnabled, isClawbackEnabled);
}

void
Expand All @@ -89,7 +96,7 @@ tag_invoke(boost::json::value_from_tag, boost::json::value& jv, AccountInfoHandl
{JS(validated), output.validated},
};

static constexpr std::array<std::pair<std::string_view, ripple::LedgerSpecificFlags>, 9> lsFlags{{
std::vector<std::pair<std::string_view, ripple::LedgerSpecificFlags>> lsFlags{{
{"defaultRipple", ripple::lsfDefaultRipple},
{"depositAuth", ripple::lsfDepositAuth},
{"disableMasterKey", ripple::lsfDisableMaster},
Expand All @@ -98,14 +105,25 @@ tag_invoke(boost::json::value_from_tag, boost::json::value& jv, AccountInfoHandl
{"noFreeze", ripple::lsfNoFreeze},
{"passwordSpent", ripple::lsfPasswordSpent},
{"requireAuthorization", ripple::lsfRequireAuth},
{"requireDestinationTag", ripple::lsfRequireDestTag}
// TODO: wait for conan integration
// {"disallowIncomingNFTokenOffer", ripple::lsfDisallowIncomingNFTokenOffer},
// {"disallowIncomingCheck", ripple::lsfDisallowIncomingCheck},
// {"disallowIncomingPayChan", ripple::lsfDisallowIncomingPayChan},
// {"disallowIncomingTrustline", ripple::lsfDisallowIncomingTrustline}
{"requireDestinationTag", ripple::lsfRequireDestTag},
}};

if (output.isDisallowIncomingEnabled)
{
std::vector<std::pair<std::string_view, ripple::LedgerSpecificFlags>> const disallowIncomingFlags = {
{"disallowIncomingNFTokenOffer", ripple::lsfDisallowIncomingNFTokenOffer},
{"disallowIncomingCheck", ripple::lsfDisallowIncomingCheck},
{"disallowIncomingPayChan", ripple::lsfDisallowIncomingPayChan},
{"disallowIncomingTrustline", ripple::lsfDisallowIncomingTrustline},
};
lsFlags.insert(lsFlags.end(), disallowIncomingFlags.begin(), disallowIncomingFlags.end());
}

if (output.isClawbackEnabled)
{
lsFlags.push_back({"allowTrustLineClawback", ripple::lsfAllowTrustLineClawback});
}

boost::json::object acctFlags;
for (auto const& lsf : lsFlags)
acctFlags[lsf.first.data()] = output.accountData.isFlag(lsf.second);
Expand Down
13 changes: 7 additions & 6 deletions src/rpc/handlers/AccountInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class AccountInfoHandler
uint32_t ledgerIndex;
std::string ledgerHash;
ripple::STLedgerEntry accountData;
bool isDisallowIncomingEnabled = false;
bool isClawbackEnabled = false;
std::optional<std::vector<ripple::STLedgerEntry>> signerLists;
// validated should be sent via framework
bool validated = true;
Expand All @@ -50,18 +52,17 @@ class AccountInfoHandler
uint32_t ledgerId,
std::string ledgerHash,
ripple::STLedgerEntry sle,
std::vector<ripple::STLedgerEntry> signerLists)
bool isDisallowIncomingEnabled,
bool isClawbackEnabled,
std::optional<std::vector<ripple::STLedgerEntry>> signerLists = std::nullopt)
: ledgerIndex(ledgerId)
, ledgerHash(std::move(ledgerHash))
, accountData(std::move(sle))
, isDisallowIncomingEnabled(isDisallowIncomingEnabled)
, isClawbackEnabled(isClawbackEnabled)
, signerLists(std::move(signerLists))
{
}

Output(uint32_t ledgerId, std::string ledgerHash, ripple::STLedgerEntry sle)
: ledgerIndex(ledgerId), ledgerHash(std::move(ledgerHash)), accountData(std::move(sle))
{
}
};

// "queue" is not available in Reporting mode
Expand Down
32 changes: 32 additions & 0 deletions unittests/rpc/AmendmentsTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//------------------------------------------------------------------------------
/*
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/Amendments.h>

#include <gtest/gtest.h>

using namespace RPC;

TEST(RPCAmendmentsTest, GenerateAmendmentId)
{
// https://xrpl.org/known-amendments.html#disallowincoming refer to the published id
EXPECT_EQ(
ripple::uint256("47C3002ABA31628447E8E9A8B315FAA935CE30183F9A9B86845E469CA2CDC3DF"),
Amendments::GetAmendmentId("DisallowIncoming"));
}
Loading