xrpld
Loading...
Searching...
No Matches
InvariantsEscrowNFT_test.cpp
1#include <test/app/invariants/InvariantsBase.h>
2#include <test/jtx/Account.h>
3#include <test/jtx/amount.h>
4
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/beast/unit_test/suite.h>
7#include <xrpl/ledger/ApplyView.h>
8#include <xrpl/protocol/AccountID.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/InnerObjectFormats.h>
11#include <xrpl/protocol/Issue.h>
12#include <xrpl/protocol/Keylet.h>
13#include <xrpl/protocol/MPTIssue.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/SOTemplate.h>
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/STArray.h>
18#include <xrpl/protocol/STLedgerEntry.h>
19#include <xrpl/protocol/STObject.h>
20#include <xrpl/protocol/SeqProxy.h>
21#include <xrpl/protocol/SystemParameters.h>
22#include <xrpl/protocol/UintTypes.h>
23#include <xrpl/tx/ApplyContext.h>
24
25#include <algorithm>
26#include <cstdint>
27#include <initializer_list>
28#include <limits>
29#include <memory>
30#include <utility>
31#include <vector>
32
33namespace xrpl::test {
34
36{
37 void
39 {
40 using namespace test::jtx;
41 testcase << "no zero escrow";
42
44 {
45 {"XRP net change of -1000000 doesn't match fee 0"},
46 {"escrow specifies invalid amount"},
47 },
48 [](Account const& a1, Account const&, ApplyContext& ac) {
49 // escrow with negative amount
50 auto const sle = ac.view().peek(keylet::account(a1.id()));
51 if (!sle)
52 return false;
53 auto const sleNew = std::make_shared<SLE>(
54 keylet::escrow(a1, SeqProxy::rawSequence((*sle)[sfSequence] + 2)));
55 sleNew->setFieldAmount(sfAmount, XRP(-1));
56 ac.view().insert(sleNew);
57 return true;
58 });
59
61 {
62 {"XRP net change was positive: 100000000000000001"},
63 {"escrow specifies invalid amount"},
64 },
65 [](Account const& a1, Account const&, ApplyContext& ac) {
66 // escrow with too-large amount
67 auto const sle = ac.view().peek(keylet::account(a1.id()));
68 if (!sle)
69 return false;
70 auto const sleNew = std::make_shared<SLE>(
71 keylet::escrow(a1, SeqProxy::rawSequence((*sle)[sfSequence] + 2)));
72 // Use `drops(1)` to bypass a call to STAmount::canonicalize
73 // with an invalid value
74 sleNew->setFieldAmount(sfAmount, kInitialXrp + drops(1));
75 ac.view().insert(sleNew);
76 return true;
77 });
78
79 // IOU < 0
81 {{"escrow specifies invalid amount"}},
82 [](Account const& a1, Account const&, ApplyContext& ac) {
83 // escrow with too-little iou
84 auto const sle = ac.view().peek(keylet::account(a1.id()));
85 if (!sle)
86 return false;
87 auto const sleNew = std::make_shared<SLE>(
88 keylet::escrow(a1, SeqProxy::rawSequence((*sle)[sfSequence] + 2)));
89
90 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
91 STAmount const amt(usd, -1);
92 sleNew->setFieldAmount(sfAmount, amt);
93 ac.view().insert(sleNew);
94 return true;
95 });
96
97 // IOU bad currency
99 {{"escrow specifies invalid amount"}},
100 [](Account const& a1, Account const&, ApplyContext& ac) {
101 // escrow with bad iou currency
102 auto const sle = ac.view().peek(keylet::account(a1.id()));
103 if (!sle)
104 return false;
105 auto const sleNew = std::make_shared<SLE>(
106 keylet::escrow(a1, SeqProxy::rawSequence((*sle)[sfSequence] + 2)));
107
108 Issue const bad{badCurrency(), AccountID(0x4985601)};
109 STAmount const amt(bad, 1);
110 sleNew->setFieldAmount(sfAmount, amt);
111 ac.view().insert(sleNew);
112 return true;
113 });
114
115 // MPT < 0
117 {{"escrow specifies invalid amount"}},
118 [](Account const& a1, Account const&, ApplyContext& ac) {
119 // escrow with too-little mpt
120 auto const sle = ac.view().peek(keylet::account(a1.id()));
121 if (!sle)
122 return false;
123 auto const sleNew = std::make_shared<SLE>(
124 keylet::escrow(a1, SeqProxy::rawSequence((*sle)[sfSequence] + 2)));
125
126 MPTIssue const mpt{makeMptID(1, AccountID(0x4985601))};
127 STAmount const amt(mpt, -1);
128 sleNew->setFieldAmount(sfAmount, amt);
129 ac.view().insert(sleNew);
130 return true;
131 });
132
133 // MPT OutstandingAmount < 0
135 {{"escrow specifies invalid amount"}},
136 [](Account const& a1, Account const&, ApplyContext& ac) {
137 // mptissuance outstanding is negative
138 auto const sle = ac.view().peek(keylet::account(a1.id()));
139 if (!sle)
140 return false;
141
142 MPTIssue const mpt{makeMptID(1, AccountID(0x4985601))};
143 auto const sleNew = std::make_shared<SLE>(keylet::mptokenIssuance(mpt.getMptID()));
144 sleNew->setFieldU64(sfOutstandingAmount, std::numeric_limits<std::uint64_t>::max());
145 ac.view().insert(sleNew);
146 return true;
147 });
148
149 // MPT LockedAmount < 0
151 {{"escrow specifies invalid amount"}},
152 [](Account const& a1, Account const&, ApplyContext& ac) {
153 // mptissuance locked is less than locked
154 auto const sle = ac.view().peek(keylet::account(a1.id()));
155 if (!sle)
156 return false;
157
158 MPTIssue const mpt{makeMptID(1, AccountID(0x4985601))};
159 auto const sleNew = std::make_shared<SLE>(keylet::mptokenIssuance(mpt.getMptID()));
160 sleNew->setFieldU64(sfLockedAmount, std::numeric_limits<std::uint64_t>::max());
161 ac.view().insert(sleNew);
162 return true;
163 });
164
165 // MPT OutstandingAmount < LockedAmount
167 {{"escrow specifies invalid amount"}},
168 [](Account const& a1, Account const&, ApplyContext& ac) {
169 // mptissuance outstanding is less than locked
170 auto const sle = ac.view().peek(keylet::account(a1.id()));
171 if (!sle)
172 return false;
173
174 MPTIssue const mpt{makeMptID(1, AccountID(0x4985601))};
175 auto const sleNew = std::make_shared<SLE>(keylet::mptokenIssuance(mpt.getMptID()));
176 sleNew->setFieldU64(sfOutstandingAmount, 1);
177 sleNew->setFieldU64(sfLockedAmount, 10);
178 ac.view().insert(sleNew);
179 return true;
180 });
181
182 // MPT MPTAmount < 0
184 {{"escrow specifies invalid amount"}},
185 [](Account const& a1, Account const&, ApplyContext& ac) {
186 // mptoken amount is negative
187 auto const sle = ac.view().peek(keylet::account(a1.id()));
188 if (!sle)
189 return false;
190
191 MPTIssue const mpt{makeMptID(1, AccountID(0x4985601))};
192 auto const sleNew = std::make_shared<SLE>(keylet::mptoken(mpt.getMptID(), a1));
193 sleNew->setFieldU64(sfMPTAmount, std::numeric_limits<std::uint64_t>::max());
194 ac.view().insert(sleNew);
195 return true;
196 });
197
198 // MPT LockedAmount < 0
200 {{"escrow specifies invalid amount"}},
201 [](Account const& a1, Account const&, ApplyContext& ac) {
202 // mptoken locked amount is negative
203 auto const sle = ac.view().peek(keylet::account(a1.id()));
204 if (!sle)
205 return false;
206
207 MPTIssue const mpt{makeMptID(1, AccountID(0x4985601))};
208 auto const sleNew = std::make_shared<SLE>(keylet::mptoken(mpt.getMptID(), a1));
209 sleNew->setFieldU64(sfLockedAmount, std::numeric_limits<std::uint64_t>::max());
210 ac.view().insert(sleNew);
211 return true;
212 });
213 }
214
215 void
217 {
218 using namespace test::jtx;
219 testcase << "NFTokenPage";
220
221 // lambda that returns an STArray of NFTokenIDs.
222 UInt256 const firstNFTID(
223 "0000000000000000000000000000000000000001FFFFFFFFFFFFFFFF00000000");
224 auto makeNFTokenIDs = [&firstNFTID](unsigned int nftCount) {
225 SOTemplate const* nfTokenTemplate =
227
228 UInt256 nftID(firstNFTID);
229 STArray ret;
230 for (int i = 0; i < nftCount; ++i)
231 {
232 STObject newNFToken(*nfTokenTemplate, sfNFToken, [&nftID](STObject& object) {
233 object.setFieldH256(sfNFTokenID, nftID);
234 });
235 ret.pushBack(std::move(newNFToken));
236 ++nftID;
237 }
238 return ret;
239 };
240
242 {{"NFT page has invalid size"}},
243 [&makeNFTokenIDs](Account const& a1, Account const&, ApplyContext& ac) {
244 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPageMax(a1));
245 nftPage->setFieldArray(sfNFTokens, makeNFTokenIDs(0));
246
247 ac.view().insert(nftPage);
248 return true;
249 });
250
252 {{"NFT page has invalid size"}},
253 [&makeNFTokenIDs](Account const& a1, Account const&, ApplyContext& ac) {
254 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPageMax(a1));
255 nftPage->setFieldArray(sfNFTokens, makeNFTokenIDs(33));
256
257 ac.view().insert(nftPage);
258 return true;
259 });
260
262 {{"NFTs on page are not sorted"}},
263 [&makeNFTokenIDs](Account const& a1, Account const&, ApplyContext& ac) {
264 STArray nfTokens = makeNFTokenIDs(2);
265 std::iter_swap(nfTokens.begin(), nfTokens.begin() + 1);
266
267 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPageMax(a1));
268 nftPage->setFieldArray(sfNFTokens, nfTokens);
269
270 ac.view().insert(nftPage);
271 return true;
272 });
273
275 {{"NFT contains empty URI"}},
276 [&makeNFTokenIDs](Account const& a1, Account const&, ApplyContext& ac) {
277 STArray nfTokens = makeNFTokenIDs(1);
278 nfTokens[0].setFieldVL(sfURI, Blob{});
279
280 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPageMax(a1));
281 nftPage->setFieldArray(sfNFTokens, nfTokens);
282
283 ac.view().insert(nftPage);
284 return true;
285 });
286
288 {{"NFT page is improperly linked"}},
289 [&makeNFTokenIDs](Account const& a1, Account const&, ApplyContext& ac) {
290 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPageMax(a1));
291 nftPage->setFieldArray(sfNFTokens, makeNFTokenIDs(1));
292 nftPage->setFieldH256(sfPreviousPageMin, keylet::nftokenPageMax(a1).key);
293
294 ac.view().insert(nftPage);
295 return true;
296 });
297
299 {{"NFT page is improperly linked"}},
300 [&makeNFTokenIDs](Account const& a1, Account const& a2, ApplyContext& ac) {
301 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPageMax(a1));
302 nftPage->setFieldArray(sfNFTokens, makeNFTokenIDs(1));
303 nftPage->setFieldH256(sfPreviousPageMin, keylet::nftokenPageMin(a2).key);
304
305 ac.view().insert(nftPage);
306 return true;
307 });
308
310 {{"NFT page is improperly linked"}},
311 [&makeNFTokenIDs](Account const& a1, Account const&, ApplyContext& ac) {
312 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPageMax(a1));
313 nftPage->setFieldArray(sfNFTokens, makeNFTokenIDs(1));
314 nftPage->setFieldH256(sfNextPageMin, nftPage->key());
315
316 ac.view().insert(nftPage);
317 return true;
318 });
319
321 {{"NFT page is improperly linked"}},
322 [&makeNFTokenIDs](Account const& a1, Account const& a2, ApplyContext& ac) {
323 STArray nfTokens = makeNFTokenIDs(1);
324 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPage(
325 keylet::nftokenPageMax(a1), ++nfTokens[0].getFieldH256(sfNFTokenID)));
326 nftPage->setFieldArray(sfNFTokens, nfTokens);
327 nftPage->setFieldH256(sfNextPageMin, keylet::nftokenPageMax(a2).key);
328
329 ac.view().insert(nftPage);
330 return true;
331 });
332
334 {{"NFT found in incorrect page"}},
335 [&makeNFTokenIDs](Account const& a1, Account const&, ApplyContext& ac) {
336 STArray nfTokens = makeNFTokenIDs(2);
337 auto const nftPage = std::make_shared<SLE>(keylet::nftokenPage(
338 keylet::nftokenPageMax(a1), nfTokens[1].getFieldH256(sfNFTokenID)));
339 nftPage->setFieldArray(sfNFTokens, nfTokens);
340
341 ac.view().insert(nftPage);
342 return true;
343 });
344 }
345
346 void
347 run() override
348 {
351 }
352};
353
354BEAST_DEFINE_TESTSUITE(InvariantsEscrowNFT, app, xrpl);
355
356} // namespace xrpl::test
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
State information when applying a tx.
SOTemplate const * findSOTemplateBySField(SField const &sField) const
static InnerObjectFormats const & getInstance()
A currency issued by an account.
Definition Issue.h:18
constexpr MPTID const & getMptID() const
Definition MPTIssue.h:43
Defines the fields and their attributes within a STObject.
Definition SOTemplate.h:105
iterator begin()
Definition STArray.h:224
void pushBack(STObject const &object)
Definition STArray.h:212
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
void doInvariantCheck(std::vector< std::string > const &expectLogs, Precheck const &precheck, XRPAmount fee=XRPAmount{}, STTx tx=STTx{ttACCOUNT_SET, [](STObject &) {}}, std::initializer_list< TER > ters={tecINVARIANT_FAILED, tefINVARIANT_FAILED}, Preclose const &preclose={}, TxAccount setTxAccount=TxAccount::None, std::source_location const &loc=std::source_location::current(), TER initialResult=tesSUCCESS)
Run a specific test case to put the ledger into a state that will be detected by an invariant.
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
AccountID id() const
Returns the Account ID.
T iter_swap(T... args)
T make_shared(T... args)
T max(T... args)
Keylet escrow(AccountID const &src, SeqProxy const &seq) noexcept
An escrow entry.
Definition Indexes.cpp:430
Keylet nftokenPage(Keylet const &k, UInt256 const &token)
Definition Indexes.cpp:458
Keylet nftokenPageMin(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:442
Keylet nftokenPageMax(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:450
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
BaseUInt< 256 > UInt256
Definition base_uint.h:581
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
constexpr XRPAmount kInitialXrp
Configure the native currency.