rippled
Loading...
Searching...
No Matches
AmendmentsTests.cpp
1// Auto-generated unit tests for ledger entry Amendments
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/Amendments.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(AmendmentsTests, BuilderSettersRoundTrip)
20{
21 uint256 const index{1u};
22
23 auto const amendmentsValue = canonical_VECTOR256();
24 auto const majoritiesValue = canonical_ARRAY();
25 auto const previousTxnIDValue = canonical_UINT256();
26 auto const previousTxnLgrSeqValue = canonical_UINT32();
27
28 AmendmentsBuilder builder{
29 };
30
31 builder.setAmendments(amendmentsValue);
32 builder.setMajorities(majoritiesValue);
33 builder.setPreviousTxnID(previousTxnIDValue);
34 builder.setPreviousTxnLgrSeq(previousTxnLgrSeqValue);
35
36 builder.setLedgerIndex(index);
37 builder.setFlags(0x1u);
38
39 EXPECT_TRUE(builder.validate());
40
41 auto const entry = builder.build(index);
42
43 EXPECT_TRUE(entry.validate());
44
45 {
46 auto const& expected = amendmentsValue;
47 auto const actualOpt = entry.getAmendments();
48 ASSERT_TRUE(actualOpt.has_value());
49 expectEqualField(expected, *actualOpt, "sfAmendments");
50 EXPECT_TRUE(entry.hasAmendments());
51 }
52
53 {
54 auto const& expected = majoritiesValue;
55 auto const actualOpt = entry.getMajorities();
56 ASSERT_TRUE(actualOpt.has_value());
57 expectEqualField(expected, *actualOpt, "sfMajorities");
58 EXPECT_TRUE(entry.hasMajorities());
59 }
60
61 {
62 auto const& expected = previousTxnIDValue;
63 auto const actualOpt = entry.getPreviousTxnID();
64 ASSERT_TRUE(actualOpt.has_value());
65 expectEqualField(expected, *actualOpt, "sfPreviousTxnID");
66 EXPECT_TRUE(entry.hasPreviousTxnID());
67 }
68
69 {
70 auto const& expected = previousTxnLgrSeqValue;
71 auto const actualOpt = entry.getPreviousTxnLgrSeq();
72 ASSERT_TRUE(actualOpt.has_value());
73 expectEqualField(expected, *actualOpt, "sfPreviousTxnLgrSeq");
74 EXPECT_TRUE(entry.hasPreviousTxnLgrSeq());
75 }
76
77 EXPECT_TRUE(entry.hasLedgerIndex());
78 auto const ledgerIndex = entry.getLedgerIndex();
79 ASSERT_TRUE(ledgerIndex.has_value());
80 EXPECT_EQ(*ledgerIndex, index);
81 EXPECT_EQ(entry.getKey(), index);
82}
83
84// 2 & 4) Start from an SLE, set fields directly on it, construct a builder
85// from that SLE, build a new wrapper, and verify all fields (and validate()).
86TEST(AmendmentsTests, BuilderFromSleRoundTrip)
87{
88 uint256 const index{2u};
89
90 auto const amendmentsValue = canonical_VECTOR256();
91 auto const majoritiesValue = canonical_ARRAY();
92 auto const previousTxnIDValue = canonical_UINT256();
93 auto const previousTxnLgrSeqValue = canonical_UINT32();
94
96
97 sle->at(sfAmendments) = amendmentsValue;
98 sle->setFieldArray(sfMajorities, majoritiesValue);
99 sle->at(sfPreviousTxnID) = previousTxnIDValue;
100 sle->at(sfPreviousTxnLgrSeq) = previousTxnLgrSeqValue;
101
102 AmendmentsBuilder builderFromSle{sle};
103 EXPECT_TRUE(builderFromSle.validate());
104
105 auto const entryFromBuilder = builderFromSle.build(index);
106
107 Amendments entryFromSle{sle};
108 EXPECT_TRUE(entryFromBuilder.validate());
109 EXPECT_TRUE(entryFromSle.validate());
110
111 {
112 auto const& expected = amendmentsValue;
113
114 auto const fromSleOpt = entryFromSle.getAmendments();
115 auto const fromBuilderOpt = entryFromBuilder.getAmendments();
116
117 ASSERT_TRUE(fromSleOpt.has_value());
118 ASSERT_TRUE(fromBuilderOpt.has_value());
119
120 expectEqualField(expected, *fromSleOpt, "sfAmendments");
121 expectEqualField(expected, *fromBuilderOpt, "sfAmendments");
122 }
123
124 {
125 auto const& expected = majoritiesValue;
126
127 auto const fromSleOpt = entryFromSle.getMajorities();
128 auto const fromBuilderOpt = entryFromBuilder.getMajorities();
129
130 ASSERT_TRUE(fromSleOpt.has_value());
131 ASSERT_TRUE(fromBuilderOpt.has_value());
132
133 expectEqualField(expected, *fromSleOpt, "sfMajorities");
134 expectEqualField(expected, *fromBuilderOpt, "sfMajorities");
135 }
136
137 {
138 auto const& expected = previousTxnIDValue;
139
140 auto const fromSleOpt = entryFromSle.getPreviousTxnID();
141 auto const fromBuilderOpt = entryFromBuilder.getPreviousTxnID();
142
143 ASSERT_TRUE(fromSleOpt.has_value());
144 ASSERT_TRUE(fromBuilderOpt.has_value());
145
146 expectEqualField(expected, *fromSleOpt, "sfPreviousTxnID");
147 expectEqualField(expected, *fromBuilderOpt, "sfPreviousTxnID");
148 }
149
150 {
151 auto const& expected = previousTxnLgrSeqValue;
152
153 auto const fromSleOpt = entryFromSle.getPreviousTxnLgrSeq();
154 auto const fromBuilderOpt = entryFromBuilder.getPreviousTxnLgrSeq();
155
156 ASSERT_TRUE(fromSleOpt.has_value());
157 ASSERT_TRUE(fromBuilderOpt.has_value());
158
159 expectEqualField(expected, *fromSleOpt, "sfPreviousTxnLgrSeq");
160 expectEqualField(expected, *fromBuilderOpt, "sfPreviousTxnLgrSeq");
161 }
162
163 EXPECT_EQ(entryFromSle.getKey(), index);
164 EXPECT_EQ(entryFromBuilder.getKey(), index);
165}
166
167// 3) Verify wrapper throws when constructed from wrong ledger entry type.
168TEST(AmendmentsTests, WrapperThrowsOnWrongEntryType)
169{
170 uint256 const index{3u};
171
172 // Build a valid ledger entry of a different type
173 // Ticket requires: Account, OwnerNode, TicketSequence, PreviousTxnID, PreviousTxnLgrSeq
174 // Check requires: Account, Destination, SendMax, Sequence, OwnerNode, DestinationNode, PreviousTxnID, PreviousTxnLgrSeq
175 TicketBuilder wrongBuilder{
181 auto wrongEntry = wrongBuilder.build(index);
182
183 EXPECT_THROW(Amendments{wrongEntry.getSle()}, std::runtime_error);
184}
185
186// 4) Verify builder throws when constructed from wrong ledger entry type.
187TEST(AmendmentsTests, BuilderThrowsOnWrongEntryType)
188{
189 uint256 const index{4u};
190
191 // Build a valid ledger entry of a different type
192 TicketBuilder wrongBuilder{
198 auto wrongEntry = wrongBuilder.build(index);
199
200 EXPECT_THROW(AmendmentsBuilder{wrongEntry.getSle()}, std::runtime_error);
201}
202
203// 5) Build with only required fields and verify optional fields return nullopt.
204TEST(AmendmentsTests, OptionalFieldsReturnNullopt)
205{
206 uint256 const index{3u};
207
208
209 AmendmentsBuilder builder{
210 };
211
212 auto const entry = builder.build(index);
213
214 // Verify optional fields are not present
215 EXPECT_FALSE(entry.hasAmendments());
216 EXPECT_FALSE(entry.getAmendments().has_value());
217 EXPECT_FALSE(entry.hasMajorities());
218 EXPECT_FALSE(entry.getMajorities().has_value());
219 EXPECT_FALSE(entry.hasPreviousTxnID());
220 EXPECT_FALSE(entry.getPreviousTxnID().has_value());
221 EXPECT_FALSE(entry.hasPreviousTxnLgrSeq());
222 EXPECT_FALSE(entry.getPreviousTxnLgrSeq().has_value());
223}
224}
Builder for Amendments ledger entries.
Definition Amendments.h:154
AmendmentsBuilder & setPreviousTxnID(std::decay_t< typename SF_UINT256::type::value_type > const &value)
Set sfPreviousTxnID (soeOPTIONAL)
Definition Amendments.h:207
AmendmentsBuilder & setAmendments(std::decay_t< typename SF_VECTOR256::type::value_type > const &value)
Ledger entry-specific field setters.
Definition Amendments.h:185
AmendmentsBuilder & setPreviousTxnLgrSeq(std::decay_t< typename SF_UINT32::type::value_type > const &value)
Set sfPreviousTxnLgrSeq (soeOPTIONAL)
Definition Amendments.h:218
AmendmentsBuilder & setMajorities(STArray const &value)
Set sfMajorities (soeOPTIONAL)
Definition Amendments.h:196
Ledger Entry: Amendments.
Definition Amendments.h:28
static constexpr LedgerEntryType entryType
Definition Amendments.h:30
std::shared_ptr< SLE const > getSle() const
Get the underlying SLE object.
Derived & setLedgerIndex(uint256 const &value)
Set the ledger index.
Derived & setFlags(uint32_t value)
Set the flags.
Builder for Ticket ledger entries.
Definition Ticket.h:112
T is_same_v
TEST(AccountRootTests, BuilderSettersRoundTrip)
Vector256Value canonical_VECTOR256()
void expectEqualField(T const &expected, T const &actual, char const *fieldName)