xrpld
Loading...
Searching...
No Matches
MPTokenTests.cpp
1// Auto-generated unit tests for ledger entry MPToken
2
3
4#include <gtest/gtest.h>
5
6#include <protocol_autogen/TestHelpers.h>
7
8#include <xrpl/protocol/STLedgerEntry.h>
9#include <xrpl/protocol_autogen/ledger_entries/MPToken.h>
10#include <xrpl/protocol_autogen/ledger_entries/Ticket.h>
11
12#include <string>
13
14namespace xrpl::ledger_entries {
15
16// 1 & 4) Set fields via builder setters, build, then read them back via
17// wrapper getters. After build(), validate() should succeed for both the
18// builder's STObject and the wrapper's SLE.
19TEST(MPTokenTests, BuilderSettersRoundTrip)
20{
21 uint256 const index{1u};
22
23 auto const accountValue = canonical_ACCOUNT();
24 auto const mPTokenIssuanceIDValue = canonical_UINT192();
25 auto const mPTAmountValue = canonical_UINT64();
26 auto const lockedAmountValue = canonical_UINT64();
27 auto const ownerNodeValue = canonical_UINT64();
28 auto const previousTxnIDValue = canonical_UINT256();
29 auto const previousTxnLgrSeqValue = canonical_UINT32();
30 auto const confidentialBalanceInboxValue = canonical_VL();
31 auto const confidentialBalanceSpendingValue = canonical_VL();
32 auto const confidentialBalanceVersionValue = canonical_UINT32();
33 auto const issuerEncryptedBalanceValue = canonical_VL();
34 auto const auditorEncryptedBalanceValue = canonical_VL();
35 auto const holderEncryptionKeyValue = canonical_VL();
36
37 MPTokenBuilder builder{
38 accountValue,
39 mPTokenIssuanceIDValue,
40 ownerNodeValue,
41 previousTxnIDValue,
42 previousTxnLgrSeqValue
43 };
44
45 builder.setMPTAmount(mPTAmountValue);
46 builder.setLockedAmount(lockedAmountValue);
47 builder.setConfidentialBalanceInbox(confidentialBalanceInboxValue);
48 builder.setConfidentialBalanceSpending(confidentialBalanceSpendingValue);
49 builder.setConfidentialBalanceVersion(confidentialBalanceVersionValue);
50 builder.setIssuerEncryptedBalance(issuerEncryptedBalanceValue);
51 builder.setAuditorEncryptedBalance(auditorEncryptedBalanceValue);
52 builder.setHolderEncryptionKey(holderEncryptionKeyValue);
53
54 builder.setLedgerIndex(index);
55 builder.setFlags(0x1u);
56
57 EXPECT_TRUE(builder.validate());
58
59 auto const entry = builder.build(index);
60
61 EXPECT_TRUE(entry.validate());
62
63 {
64 auto const& expected = accountValue;
65 auto const actual = entry.getAccount();
66 expectEqualField(expected, actual, "sfAccount");
67 }
68
69 {
70 auto const& expected = mPTokenIssuanceIDValue;
71 auto const actual = entry.getMPTokenIssuanceID();
72 expectEqualField(expected, actual, "sfMPTokenIssuanceID");
73 }
74
75 {
76 auto const& expected = ownerNodeValue;
77 auto const actual = entry.getOwnerNode();
78 expectEqualField(expected, actual, "sfOwnerNode");
79 }
80
81 {
82 auto const& expected = previousTxnIDValue;
83 auto const actual = entry.getPreviousTxnID();
84 expectEqualField(expected, actual, "sfPreviousTxnID");
85 }
86
87 {
88 auto const& expected = previousTxnLgrSeqValue;
89 auto const actual = entry.getPreviousTxnLgrSeq();
90 expectEqualField(expected, actual, "sfPreviousTxnLgrSeq");
91 }
92
93 {
94 auto const& expected = mPTAmountValue;
95 auto const actualOpt = entry.getMPTAmount();
96 ASSERT_TRUE(actualOpt.has_value());
97 expectEqualField(expected, *actualOpt, "sfMPTAmount");
98 EXPECT_TRUE(entry.hasMPTAmount());
99 }
100
101 {
102 auto const& expected = lockedAmountValue;
103 auto const actualOpt = entry.getLockedAmount();
104 ASSERT_TRUE(actualOpt.has_value());
105 expectEqualField(expected, *actualOpt, "sfLockedAmount");
106 EXPECT_TRUE(entry.hasLockedAmount());
107 }
108
109 {
110 auto const& expected = confidentialBalanceInboxValue;
111 auto const actualOpt = entry.getConfidentialBalanceInbox();
112 ASSERT_TRUE(actualOpt.has_value());
113 expectEqualField(expected, *actualOpt, "sfConfidentialBalanceInbox");
114 EXPECT_TRUE(entry.hasConfidentialBalanceInbox());
115 }
116
117 {
118 auto const& expected = confidentialBalanceSpendingValue;
119 auto const actualOpt = entry.getConfidentialBalanceSpending();
120 ASSERT_TRUE(actualOpt.has_value());
121 expectEqualField(expected, *actualOpt, "sfConfidentialBalanceSpending");
122 EXPECT_TRUE(entry.hasConfidentialBalanceSpending());
123 }
124
125 {
126 auto const& expected = confidentialBalanceVersionValue;
127 auto const actualOpt = entry.getConfidentialBalanceVersion();
128 ASSERT_TRUE(actualOpt.has_value());
129 expectEqualField(expected, *actualOpt, "sfConfidentialBalanceVersion");
130 EXPECT_TRUE(entry.hasConfidentialBalanceVersion());
131 }
132
133 {
134 auto const& expected = issuerEncryptedBalanceValue;
135 auto const actualOpt = entry.getIssuerEncryptedBalance();
136 ASSERT_TRUE(actualOpt.has_value());
137 expectEqualField(expected, *actualOpt, "sfIssuerEncryptedBalance");
138 EXPECT_TRUE(entry.hasIssuerEncryptedBalance());
139 }
140
141 {
142 auto const& expected = auditorEncryptedBalanceValue;
143 auto const actualOpt = entry.getAuditorEncryptedBalance();
144 ASSERT_TRUE(actualOpt.has_value());
145 expectEqualField(expected, *actualOpt, "sfAuditorEncryptedBalance");
146 EXPECT_TRUE(entry.hasAuditorEncryptedBalance());
147 }
148
149 {
150 auto const& expected = holderEncryptionKeyValue;
151 auto const actualOpt = entry.getHolderEncryptionKey();
152 ASSERT_TRUE(actualOpt.has_value());
153 expectEqualField(expected, *actualOpt, "sfHolderEncryptionKey");
154 EXPECT_TRUE(entry.hasHolderEncryptionKey());
155 }
156
157 EXPECT_TRUE(entry.hasLedgerIndex());
158 auto const ledgerIndex = entry.getLedgerIndex();
159 ASSERT_TRUE(ledgerIndex.has_value());
160 EXPECT_EQ(*ledgerIndex, index);
161 EXPECT_EQ(entry.getKey(), index);
162}
163
164// 2 & 4) Start from an SLE, set fields directly on it, construct a builder
165// from that SLE, build a new wrapper, and verify all fields (and validate()).
166TEST(MPTokenTests, BuilderFromSleRoundTrip)
167{
168 uint256 const index{2u};
169
170 auto const accountValue = canonical_ACCOUNT();
171 auto const mPTokenIssuanceIDValue = canonical_UINT192();
172 auto const mPTAmountValue = canonical_UINT64();
173 auto const lockedAmountValue = canonical_UINT64();
174 auto const ownerNodeValue = canonical_UINT64();
175 auto const previousTxnIDValue = canonical_UINT256();
176 auto const previousTxnLgrSeqValue = canonical_UINT32();
177 auto const confidentialBalanceInboxValue = canonical_VL();
178 auto const confidentialBalanceSpendingValue = canonical_VL();
179 auto const confidentialBalanceVersionValue = canonical_UINT32();
180 auto const issuerEncryptedBalanceValue = canonical_VL();
181 auto const auditorEncryptedBalanceValue = canonical_VL();
182 auto const holderEncryptionKeyValue = canonical_VL();
183
184 auto sle = std::make_shared<SLE>(MPToken::entryType, index);
185
186 sle->at(sfAccount) = accountValue;
187 sle->at(sfMPTokenIssuanceID) = mPTokenIssuanceIDValue;
188 sle->at(sfMPTAmount) = mPTAmountValue;
189 sle->at(sfLockedAmount) = lockedAmountValue;
190 sle->at(sfOwnerNode) = ownerNodeValue;
191 sle->at(sfPreviousTxnID) = previousTxnIDValue;
192 sle->at(sfPreviousTxnLgrSeq) = previousTxnLgrSeqValue;
193 sle->at(sfConfidentialBalanceInbox) = confidentialBalanceInboxValue;
194 sle->at(sfConfidentialBalanceSpending) = confidentialBalanceSpendingValue;
195 sle->at(sfConfidentialBalanceVersion) = confidentialBalanceVersionValue;
196 sle->at(sfIssuerEncryptedBalance) = issuerEncryptedBalanceValue;
197 sle->at(sfAuditorEncryptedBalance) = auditorEncryptedBalanceValue;
198 sle->at(sfHolderEncryptionKey) = holderEncryptionKeyValue;
199
200 MPTokenBuilder builderFromSle{sle};
201 EXPECT_TRUE(builderFromSle.validate());
202
203 auto const entryFromBuilder = builderFromSle.build(index);
204
205 MPToken entryFromSle{sle};
206 EXPECT_TRUE(entryFromBuilder.validate());
207 EXPECT_TRUE(entryFromSle.validate());
208
209 {
210 auto const& expected = accountValue;
211
212 auto const fromSle = entryFromSle.getAccount();
213 auto const fromBuilder = entryFromBuilder.getAccount();
214
215 expectEqualField(expected, fromSle, "sfAccount");
216 expectEqualField(expected, fromBuilder, "sfAccount");
217 }
218
219 {
220 auto const& expected = mPTokenIssuanceIDValue;
221
222 auto const fromSle = entryFromSle.getMPTokenIssuanceID();
223 auto const fromBuilder = entryFromBuilder.getMPTokenIssuanceID();
224
225 expectEqualField(expected, fromSle, "sfMPTokenIssuanceID");
226 expectEqualField(expected, fromBuilder, "sfMPTokenIssuanceID");
227 }
228
229 {
230 auto const& expected = ownerNodeValue;
231
232 auto const fromSle = entryFromSle.getOwnerNode();
233 auto const fromBuilder = entryFromBuilder.getOwnerNode();
234
235 expectEqualField(expected, fromSle, "sfOwnerNode");
236 expectEqualField(expected, fromBuilder, "sfOwnerNode");
237 }
238
239 {
240 auto const& expected = previousTxnIDValue;
241
242 auto const fromSle = entryFromSle.getPreviousTxnID();
243 auto const fromBuilder = entryFromBuilder.getPreviousTxnID();
244
245 expectEqualField(expected, fromSle, "sfPreviousTxnID");
246 expectEqualField(expected, fromBuilder, "sfPreviousTxnID");
247 }
248
249 {
250 auto const& expected = previousTxnLgrSeqValue;
251
252 auto const fromSle = entryFromSle.getPreviousTxnLgrSeq();
253 auto const fromBuilder = entryFromBuilder.getPreviousTxnLgrSeq();
254
255 expectEqualField(expected, fromSle, "sfPreviousTxnLgrSeq");
256 expectEqualField(expected, fromBuilder, "sfPreviousTxnLgrSeq");
257 }
258
259 {
260 auto const& expected = mPTAmountValue;
261
262 auto const fromSleOpt = entryFromSle.getMPTAmount();
263 auto const fromBuilderOpt = entryFromBuilder.getMPTAmount();
264
265 ASSERT_TRUE(fromSleOpt.has_value());
266 ASSERT_TRUE(fromBuilderOpt.has_value());
267
268 expectEqualField(expected, *fromSleOpt, "sfMPTAmount");
269 expectEqualField(expected, *fromBuilderOpt, "sfMPTAmount");
270 }
271
272 {
273 auto const& expected = lockedAmountValue;
274
275 auto const fromSleOpt = entryFromSle.getLockedAmount();
276 auto const fromBuilderOpt = entryFromBuilder.getLockedAmount();
277
278 ASSERT_TRUE(fromSleOpt.has_value());
279 ASSERT_TRUE(fromBuilderOpt.has_value());
280
281 expectEqualField(expected, *fromSleOpt, "sfLockedAmount");
282 expectEqualField(expected, *fromBuilderOpt, "sfLockedAmount");
283 }
284
285 {
286 auto const& expected = confidentialBalanceInboxValue;
287
288 auto const fromSleOpt = entryFromSle.getConfidentialBalanceInbox();
289 auto const fromBuilderOpt = entryFromBuilder.getConfidentialBalanceInbox();
290
291 ASSERT_TRUE(fromSleOpt.has_value());
292 ASSERT_TRUE(fromBuilderOpt.has_value());
293
294 expectEqualField(expected, *fromSleOpt, "sfConfidentialBalanceInbox");
295 expectEqualField(expected, *fromBuilderOpt, "sfConfidentialBalanceInbox");
296 }
297
298 {
299 auto const& expected = confidentialBalanceSpendingValue;
300
301 auto const fromSleOpt = entryFromSle.getConfidentialBalanceSpending();
302 auto const fromBuilderOpt = entryFromBuilder.getConfidentialBalanceSpending();
303
304 ASSERT_TRUE(fromSleOpt.has_value());
305 ASSERT_TRUE(fromBuilderOpt.has_value());
306
307 expectEqualField(expected, *fromSleOpt, "sfConfidentialBalanceSpending");
308 expectEqualField(expected, *fromBuilderOpt, "sfConfidentialBalanceSpending");
309 }
310
311 {
312 auto const& expected = confidentialBalanceVersionValue;
313
314 auto const fromSleOpt = entryFromSle.getConfidentialBalanceVersion();
315 auto const fromBuilderOpt = entryFromBuilder.getConfidentialBalanceVersion();
316
317 ASSERT_TRUE(fromSleOpt.has_value());
318 ASSERT_TRUE(fromBuilderOpt.has_value());
319
320 expectEqualField(expected, *fromSleOpt, "sfConfidentialBalanceVersion");
321 expectEqualField(expected, *fromBuilderOpt, "sfConfidentialBalanceVersion");
322 }
323
324 {
325 auto const& expected = issuerEncryptedBalanceValue;
326
327 auto const fromSleOpt = entryFromSle.getIssuerEncryptedBalance();
328 auto const fromBuilderOpt = entryFromBuilder.getIssuerEncryptedBalance();
329
330 ASSERT_TRUE(fromSleOpt.has_value());
331 ASSERT_TRUE(fromBuilderOpt.has_value());
332
333 expectEqualField(expected, *fromSleOpt, "sfIssuerEncryptedBalance");
334 expectEqualField(expected, *fromBuilderOpt, "sfIssuerEncryptedBalance");
335 }
336
337 {
338 auto const& expected = auditorEncryptedBalanceValue;
339
340 auto const fromSleOpt = entryFromSle.getAuditorEncryptedBalance();
341 auto const fromBuilderOpt = entryFromBuilder.getAuditorEncryptedBalance();
342
343 ASSERT_TRUE(fromSleOpt.has_value());
344 ASSERT_TRUE(fromBuilderOpt.has_value());
345
346 expectEqualField(expected, *fromSleOpt, "sfAuditorEncryptedBalance");
347 expectEqualField(expected, *fromBuilderOpt, "sfAuditorEncryptedBalance");
348 }
349
350 {
351 auto const& expected = holderEncryptionKeyValue;
352
353 auto const fromSleOpt = entryFromSle.getHolderEncryptionKey();
354 auto const fromBuilderOpt = entryFromBuilder.getHolderEncryptionKey();
355
356 ASSERT_TRUE(fromSleOpt.has_value());
357 ASSERT_TRUE(fromBuilderOpt.has_value());
358
359 expectEqualField(expected, *fromSleOpt, "sfHolderEncryptionKey");
360 expectEqualField(expected, *fromBuilderOpt, "sfHolderEncryptionKey");
361 }
362
363 EXPECT_EQ(entryFromSle.getKey(), index);
364 EXPECT_EQ(entryFromBuilder.getKey(), index);
365}
366
367// 3) Verify wrapper throws when constructed from wrong ledger entry type.
368TEST(MPTokenTests, WrapperThrowsOnWrongEntryType)
369{
370 uint256 const index{3u};
371
372 // Build a valid ledger entry of a different type
373 // Ticket requires: Account, OwnerNode, TicketSequence, PreviousTxnID, PreviousTxnLgrSeq
374 // Check requires: Account, Destination, SendMax, Sequence, OwnerNode, DestinationNode, PreviousTxnID, PreviousTxnLgrSeq
375 TicketBuilder wrongBuilder{
381 auto wrongEntry = wrongBuilder.build(index);
382
383 EXPECT_THROW(MPToken{wrongEntry.getSle()}, std::runtime_error);
384}
385
386// 4) Verify builder throws when constructed from wrong ledger entry type.
387TEST(MPTokenTests, BuilderThrowsOnWrongEntryType)
388{
389 uint256 const index{4u};
390
391 // Build a valid ledger entry of a different type
392 TicketBuilder wrongBuilder{
398 auto wrongEntry = wrongBuilder.build(index);
399
400 EXPECT_THROW(MPTokenBuilder{wrongEntry.getSle()}, std::runtime_error);
401}
402
403// 5) Build with only required fields and verify optional fields return nullopt.
404TEST(MPTokenTests, OptionalFieldsReturnNullopt)
405{
406 uint256 const index{3u};
407
408 auto const accountValue = canonical_ACCOUNT();
409 auto const mPTokenIssuanceIDValue = canonical_UINT192();
410 auto const ownerNodeValue = canonical_UINT64();
411 auto const previousTxnIDValue = canonical_UINT256();
412 auto const previousTxnLgrSeqValue = canonical_UINT32();
413
414 MPTokenBuilder builder{
415 accountValue,
416 mPTokenIssuanceIDValue,
417 ownerNodeValue,
418 previousTxnIDValue,
419 previousTxnLgrSeqValue
420 };
421
422 auto const entry = builder.build(index);
423
424 // Verify optional fields are not present
425 EXPECT_FALSE(entry.hasMPTAmount());
426 EXPECT_FALSE(entry.getMPTAmount().has_value());
427 EXPECT_FALSE(entry.hasLockedAmount());
428 EXPECT_FALSE(entry.getLockedAmount().has_value());
429 EXPECT_FALSE(entry.hasConfidentialBalanceInbox());
430 EXPECT_FALSE(entry.getConfidentialBalanceInbox().has_value());
431 EXPECT_FALSE(entry.hasConfidentialBalanceSpending());
432 EXPECT_FALSE(entry.getConfidentialBalanceSpending().has_value());
433 EXPECT_FALSE(entry.hasConfidentialBalanceVersion());
434 EXPECT_FALSE(entry.getConfidentialBalanceVersion().has_value());
435 EXPECT_FALSE(entry.hasIssuerEncryptedBalance());
436 EXPECT_FALSE(entry.getIssuerEncryptedBalance().has_value());
437 EXPECT_FALSE(entry.hasAuditorEncryptedBalance());
438 EXPECT_FALSE(entry.getAuditorEncryptedBalance().has_value());
439 EXPECT_FALSE(entry.hasHolderEncryptionKey());
440 EXPECT_FALSE(entry.getHolderEncryptionKey().has_value());
441}
442}
bool validate() const
Validate the ledger entry.
uint256 const & getKey() const
Get the key (index) of this ledger entry.
Derived & setLedgerIndex(uint256 const &value)
Set the ledger index.
Derived & setFlags(uint32_t value)
Set the flags.
bool validate() const
Validate the ledger entry.
Builder for MPToken ledger entries.
Definition MPToken.h:304
MPTokenBuilder & setConfidentialBalanceSpending(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfConfidentialBalanceSpending (SoeOptional).
Definition MPToken.h:433
MPTokenBuilder & setIssuerEncryptedBalance(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfIssuerEncryptedBalance (SoeOptional).
Definition MPToken.h:455
MPToken build(uint256 const &index)
Build and return the completed MPToken wrapper.
Definition MPToken.h:489
MPTokenBuilder & setLockedAmount(std::decay_t< typename SF_UINT64::type::value_type > const &value)
Set sfLockedAmount (SoeOptional).
Definition MPToken.h:378
MPTokenBuilder & setConfidentialBalanceInbox(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfConfidentialBalanceInbox (SoeOptional).
Definition MPToken.h:422
MPTokenBuilder & setHolderEncryptionKey(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfHolderEncryptionKey (SoeOptional).
Definition MPToken.h:477
MPTokenBuilder & setMPTAmount(std::decay_t< typename SF_UINT64::type::value_type > const &value)
Set sfMPTAmount (SoeDefault).
Definition MPToken.h:367
MPTokenBuilder & setAuditorEncryptedBalance(std::decay_t< typename SF_VL::type::value_type > const &value)
Set sfAuditorEncryptedBalance (SoeOptional).
Definition MPToken.h:466
MPTokenBuilder & setConfidentialBalanceVersion(std::decay_t< typename SF_UINT32::type::value_type > const &value)
Set sfConfidentialBalanceVersion (SoeDefault).
Definition MPToken.h:444
Ledger Entry: MPToken.
Definition MPToken.h:28
SF_UINT256::type::value_type getPreviousTxnID() const
Get sfPreviousTxnID (SoeRequired).
Definition MPToken.h:135
protocol_autogen::Optional< SF_VL::type::value_type > getHolderEncryptionKey() const
Get sfHolderEncryptionKey (SoeOptional).
Definition MPToken.h:277
SF_UINT192::type::value_type getMPTokenIssuanceID() const
Get sfMPTokenIssuanceID (SoeRequired).
Definition MPToken.h:65
SF_ACCOUNT::type::value_type getAccount() const
Get sfAccount (SoeRequired).
Definition MPToken.h:54
static constexpr LedgerEntryType entryType
Definition MPToken.h:30
protocol_autogen::Optional< SF_UINT64::type::value_type > getMPTAmount() const
Get sfMPTAmount (SoeDefault).
Definition MPToken.h:76
protocol_autogen::Optional< SF_VL::type::value_type > getAuditorEncryptedBalance() const
Get sfAuditorEncryptedBalance (SoeOptional).
Definition MPToken.h:253
SF_UINT32::type::value_type getPreviousTxnLgrSeq() const
Get sfPreviousTxnLgrSeq (SoeRequired).
Definition MPToken.h:146
SF_UINT64::type::value_type getOwnerNode() const
Get sfOwnerNode (SoeRequired).
Definition MPToken.h:124
protocol_autogen::Optional< SF_VL::type::value_type > getIssuerEncryptedBalance() const
Get sfIssuerEncryptedBalance (SoeOptional).
Definition MPToken.h:229
protocol_autogen::Optional< SF_VL::type::value_type > getConfidentialBalanceInbox() const
Get sfConfidentialBalanceInbox (SoeOptional).
Definition MPToken.h:157
protocol_autogen::Optional< SF_VL::type::value_type > getConfidentialBalanceSpending() const
Get sfConfidentialBalanceSpending (SoeOptional).
Definition MPToken.h:181
protocol_autogen::Optional< SF_UINT32::type::value_type > getConfidentialBalanceVersion() const
Get sfConfidentialBalanceVersion (SoeDefault).
Definition MPToken.h:205
protocol_autogen::Optional< SF_UINT64::type::value_type > getLockedAmount() const
Get sfLockedAmount (SoeOptional).
Definition MPToken.h:100
Builder for Ticket ledger entries.
Definition Ticket.h:112
Ticket build(uint256 const &index)
Build and return the completed Ticket wrapper.
Definition Ticket.h:209
T make_shared(T... args)
TEST(AccountRootTests, BuilderSettersRoundTrip)
BaseUInt< 256 > uint256
Definition base_uint.h:562
void expectEqualField(T const &expected, T const &actual, char const *fieldName)