xrpld
Loading...
Searching...
No Matches
PermissionedDEXHelpers.cpp
1#include <xrpl/ledger/helpers/PermissionedDEXHelpers.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/helpers/CredentialHelpers.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/UintTypes.h>
16
17#include <algorithm>
18
19namespace xrpl::permissioned_dex {
20
21bool
22accountInDomain(ReadView const& view, AccountID const& account, Domain const& domainID)
23{
24 // Avoid constructing a zero-key PermissionedDomain keylet.
25 // keylet::permissionedDomain(uint256) uses the DomainID as the ledger key.
26 if (view.rules().enabled(fixCleanup3_2_0) && domainID == beast::kZero)
27 {
28 // LCOV_EXCL_START
29 UNREACHABLE("xrpl::permissioned_dex::accountInDomain : domainID is zero");
30 return false;
31 // LCOV_EXCL_STOP
32 }
33
34 auto const sleDomain = view.read(keylet::permissionedDomain(domainID));
35 if (!sleDomain)
36 return false;
37
38 // domain owner is in the domain
39 if (sleDomain->getAccountID(sfOwner) == account)
40 return true;
41
42 auto const& credentials = sleDomain->getFieldArray(sfAcceptedCredentials);
43
44 bool const inDomain = std::ranges::any_of(credentials, [&](auto const& credential) {
45 auto const sleCred = view.read(
46 keylet::credential(account, credential[sfIssuer], credential[sfCredentialType]));
47 if (!sleCred || !sleCred->isFlag(lsfAccepted))
48 return false;
49
50 return !credentials::checkExpired(*sleCred, view.header().parentCloseTime);
51 });
52
53 return inDomain;
54}
55
56bool
58 ReadView const& view,
59 uint256 const& offerID,
60 Domain const& domainID,
62{
63 auto const sleOffer = view.read(keylet::offer(offerID));
64
65 // The following are defensive checks that should never happen, since this
66 // function is used to check against the order book offers, which should not
67 // have any of the following wrong behavior
68 if (!sleOffer)
69 return false; // LCOV_EXCL_LINE
70 if (!sleOffer->isFieldPresent(sfDomainID))
71 return false; // LCOV_EXCL_LINE
72 if (sleOffer->getFieldH256(sfDomainID) != domainID)
73 return false; // LCOV_EXCL_LINE
74
75 if (view.rules().enabled(fixCleanup3_1_3))
76 {
77 // post-fixCleanup3_1_3: a valid hybrid offer must have
78 // sfAdditionalBooks present with exactly 1 entry
79 if (sleOffer->isFlag(lsfHybrid) &&
80 (!sleOffer->isFieldPresent(sfAdditionalBooks) ||
81 sleOffer->getFieldArray(sfAdditionalBooks).size() != 1))
82 {
83 JLOG(j.error()) << "Hybrid offer " << offerID
84 << " missing or malformed AdditionalBooks field";
85 return false; // LCOV_EXCL_LINE
86 }
87 }
88 else
89 {
90 // pre-fixCleanup3_1_3: a valid hybrid offer must have
91 // sfAdditionalBooks present (size is not checked)
92 if (sleOffer->isFlag(lsfHybrid) && !sleOffer->isFieldPresent(sfAdditionalBooks))
93 {
94 JLOG(j.error()) << "Hybrid offer " << offerID << " missing AdditionalBooks field";
95 return false; // LCOV_EXCL_LINE
96 }
97 }
98
99 return accountInDomain(view, sleOffer->getAccountID(sfAccount), domainID);
100}
101
102} // namespace xrpl::permissioned_dex
T any_of(T... args)
A generic endpoint for log messages.
Definition Journal.h:38
Stream error() const
Definition Journal.h:315
A view into a ledger.
Definition ReadView.h:31
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:171
bool checkExpired(SLE const &sleCredential, NetClock::time_point const &closed)
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition Indexes.cpp:264
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Definition Indexes.cpp:569
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Definition Indexes.cpp:545
bool offerInDomain(ReadView const &view, uint256 const &offerID, Domain const &domainID, beast::Journal j)
bool accountInDomain(ReadView const &view, AccountID const &account, Domain const &domainID)
BaseUInt< 256 > Domain
Domain is a 256-bit hash representing a specific domain.
Definition UintTypes.h:47
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
BaseUInt< 256 > uint256
Definition base_uint.h:562
NetClock::time_point parentCloseTime