xrpld
Loading...
Searching...
No Matches
PermissionedDomainInvariant.cpp
1#include <xrpl/tx/invariants/PermissionedDomainInvariant.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/ledger/ReadView.h>
6#include <xrpl/ledger/helpers/CredentialHelpers.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/LedgerFormats.h>
9#include <xrpl/protocol/Protocol.h>
10#include <xrpl/protocol/SField.h>
11#include <xrpl/protocol/STAccount.h> // IWYU pragma: keep
12#include <xrpl/protocol/STArray.h>
13#include <xrpl/protocol/STLedgerEntry.h>
14#include <xrpl/protocol/STTx.h>
15#include <xrpl/protocol/TER.h>
16#include <xrpl/protocol/TxFormats.h>
17#include <xrpl/protocol/XRPAmount.h>
18
19#include <vector>
20
21namespace xrpl {
22
23void
25{
26 if (before && before->getType() != ltPERMISSIONED_DOMAIN)
27 return;
28 if (after && after->getType() != ltPERMISSIONED_DOMAIN)
29 return;
30
31 auto const check = [isDel](std::vector<SleStatus>& sleStatus, SLE::ConstRef sle) {
32 auto const& credentials = sle->getFieldArray(sfAcceptedCredentials);
33 auto const sorted = credentials::makeSorted(credentials);
34
35 SleStatus ss{
36 .credentialsSize = credentials.size(),
37 .isSorted = false,
38 .isUnique = !sorted.empty(),
39 .isDelete = isDel,
40 };
41
42 // If array have duplicates then all the other checks are invalid
43 if (ss.isUnique)
44 {
45 unsigned i = 0;
46 for (auto const& cred : sorted)
47 {
48 auto const& credTx = credentials[i++];
49 ss.isSorted =
50 (cred.first == credTx[sfIssuer]) && (cred.second == credTx[sfCredentialType]);
51 if (!ss.isSorted)
52 break;
53 }
54 }
55 sleStatus.emplace_back(ss);
56 };
57
58 if (after)
59 check(sleStatus_, after);
60}
61
62bool
64 STTx const& tx,
65 TER const result,
66 XRPAmount const,
67 ReadView const& view,
68 beast::Journal const& j)
69{
70 auto const check = [](SleStatus const& sleStatus, beast::Journal const& j) {
71 if (!sleStatus.credentialsSize)
72 {
73 JLOG(j.fatal()) << "Invariant failed: permissioned domain with "
74 "no rules.";
75 return false;
76 }
77
79 {
80 JLOG(j.fatal()) << "Invariant failed: permissioned domain bad "
81 "credentials size "
82 << sleStatus.credentialsSize;
83 return false;
84 }
85
86 if (!sleStatus.isUnique)
87 {
88 JLOG(j.fatal()) << "Invariant failed: permissioned domain credentials "
89 "aren't unique";
90 return false;
91 }
92
93 if (!sleStatus.isSorted)
94 {
95 JLOG(j.fatal()) << "Invariant failed: permissioned domain credentials "
96 "aren't sorted";
97 return false;
98 }
99
100 return true;
101 };
102
103 if (view.rules().enabled(fixCleanup3_1_3))
104 {
105 // No permissioned domains should be affected if the transaction failed
106 if (!isTesSuccess(result))
107 {
108 // If nothing changed, all is good. If there were changes, that's bad.
109 return sleStatus_.empty();
110 }
111
112 if (sleStatus_.size() > 1)
113 {
114 JLOG(j.fatal()) << "Invariant failed: transaction affected more "
115 "than 1 permissioned domain entry.";
116 return false;
117 }
118
119 switch (tx.getTxnType())
120 {
121 case ttPERMISSIONED_DOMAIN_SET: {
122 if (sleStatus_.empty())
123 {
124 JLOG(j.fatal()) << "Invariant failed: no domain objects affected by "
125 "PermissionedDomainSet";
126 return false;
127 }
128
129 auto const& sleStatus = sleStatus_[0];
130 if (sleStatus.isDelete)
131 {
132 JLOG(j.fatal()) << "Invariant failed: domain object "
133 "deleted by PermissionedDomainSet";
134 return false;
135 }
136 return check(sleStatus, j);
137 }
138 case ttPERMISSIONED_DOMAIN_DELETE: {
139 if (sleStatus_.empty())
140 {
141 JLOG(j.fatal()) << "Invariant failed: no domain objects affected by "
142 "PermissionedDomainDelete";
143 return false;
144 }
145
146 if (!sleStatus_[0].isDelete)
147 {
148 JLOG(j.fatal()) << "Invariant failed: domain object "
149 "modified, but not deleted by "
150 "PermissionedDomainDelete";
151 return false;
152 }
153 return true;
154 }
155 default: {
156 if (!sleStatus_.empty())
157 {
158 JLOG(j.fatal()) << "Invariant failed: " << sleStatus_.size()
159 << " domain object(s) affected by an "
160 "unauthorized transaction. "
161 << tx.getTxnType();
162 return false;
163 }
164 return true;
165 }
166 }
167 }
168 else
169 {
170 if (tx.getTxnType() != ttPERMISSIONED_DOMAIN_SET || !isTesSuccess(result) ||
171 sleStatus_.empty())
172 return true;
173 return check(sleStatus_[0], j);
174 }
175}
176
177} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
std::shared_ptr< STLedgerEntry const > const & ConstRef
TxType getTxnType() const
Definition STTx.h:250
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
T emplace_back(T... args)
std::set< std::pair< AccountID, Slice > > makeSorted(STArray const &credentials)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
constexpr std::size_t kMaxPermissionedDomainCredentialsArraySize
The maximum number of credentials can be passed in array for permissioned domain.
Definition Protocol.h:287