xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
protocol_autogen
transactions
PaymentChannelCreateTests.cpp
1
// Auto-generated unit tests for transaction PaymentChannelCreate
2
3
4
#include <gtest/gtest.h>
5
6
#include <protocol_autogen/TestHelpers.h>
7
8
#include <xrpl/protocol/SecretKey.h>
9
#include <xrpl/protocol/Seed.h>
10
#include <xrpl/protocol/STTx.h>
11
#include <xrpl/protocol_autogen/transactions/PaymentChannelCreate.h>
12
#include <xrpl/protocol_autogen/transactions/AccountSet.h>
13
14
#include <
string
>
15
16
namespace
xrpl::transactions
{
17
18
// 1 & 4) Set fields via builder setters, build, then read them back via
19
// wrapper getters. After build(), validate() should succeed.
20
TEST
(TransactionsPaymentChannelCreateTests, BuilderSettersRoundTrip)
21
{
22
// Generate a deterministic keypair for signing
23
auto
const
[publicKey, secretKey] =
24
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testPaymentChannelCreate"
));
25
26
// Common transaction fields
27
auto
const
accountValue =
calcAccountID
(publicKey);
28
std::uint32_t
const
sequenceValue = 1;
29
auto
const
feeValue =
canonical_AMOUNT
();
30
31
// Transaction-specific field values
32
auto
const
destinationValue =
canonical_ACCOUNT
();
33
auto
const
amountValue =
canonical_AMOUNT
();
34
auto
const
settleDelayValue =
canonical_UINT32
();
35
auto
const
publicKeyValue =
canonical_VL
();
36
auto
const
cancelAfterValue =
canonical_UINT32
();
37
auto
const
destinationTagValue =
canonical_UINT32
();
38
39
PaymentChannelCreateBuilder
builder{
40
accountValue,
41
destinationValue,
42
amountValue,
43
settleDelayValue,
44
publicKeyValue,
45
sequenceValue,
46
feeValue
47
};
48
49
// Set optional fields
50
builder.
setCancelAfter
(cancelAfterValue);
51
builder.
setDestinationTag
(destinationTagValue);
52
53
auto
tx = builder.
build
(publicKey, secretKey);
54
55
std::string
reason;
56
EXPECT_TRUE(tx.validate(reason)) << reason;
57
58
// Verify signing was applied
59
EXPECT_FALSE(tx.getSigningPubKey().empty());
60
EXPECT_TRUE(tx.hasTxnSignature());
61
62
// Verify common fields
63
EXPECT_EQ(tx.getAccount(), accountValue);
64
EXPECT_EQ(tx.getSequence(), sequenceValue);
65
EXPECT_EQ(tx.getFee(), feeValue);
66
67
// Verify required fields
68
{
69
auto
const
& expected = destinationValue;
70
auto
const
actual = tx.getDestination();
71
expectEqualField
(expected, actual,
"sfDestination"
);
72
}
73
74
{
75
auto
const
& expected = amountValue;
76
auto
const
actual = tx.getAmount();
77
expectEqualField
(expected, actual,
"sfAmount"
);
78
}
79
80
{
81
auto
const
& expected = settleDelayValue;
82
auto
const
actual = tx.getSettleDelay();
83
expectEqualField
(expected, actual,
"sfSettleDelay"
);
84
}
85
86
{
87
auto
const
& expected = publicKeyValue;
88
auto
const
actual = tx.getPublicKey();
89
expectEqualField
(expected, actual,
"sfPublicKey"
);
90
}
91
92
// Verify optional fields
93
{
94
auto
const
& expected = cancelAfterValue;
95
auto
const
actualOpt = tx.getCancelAfter();
96
ASSERT_TRUE(actualOpt.has_value()) <<
"Optional field sfCancelAfter should be present"
;
97
expectEqualField
(expected, *actualOpt,
"sfCancelAfter"
);
98
EXPECT_TRUE(tx.hasCancelAfter());
99
}
100
101
{
102
auto
const
& expected = destinationTagValue;
103
auto
const
actualOpt = tx.getDestinationTag();
104
ASSERT_TRUE(actualOpt.has_value()) <<
"Optional field sfDestinationTag should be present"
;
105
expectEqualField
(expected, *actualOpt,
"sfDestinationTag"
);
106
EXPECT_TRUE(tx.hasDestinationTag());
107
}
108
109
}
110
111
// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
112
// and verify all fields match.
113
TEST
(TransactionsPaymentChannelCreateTests, BuilderFromStTxRoundTrip)
114
{
115
// Generate a deterministic keypair for signing
116
auto
const
[publicKey, secretKey] =
117
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testPaymentChannelCreateFromTx"
));
118
119
// Common transaction fields
120
auto
const
accountValue =
calcAccountID
(publicKey);
121
std::uint32_t
const
sequenceValue = 2;
122
auto
const
feeValue =
canonical_AMOUNT
();
123
124
// Transaction-specific field values
125
auto
const
destinationValue =
canonical_ACCOUNT
();
126
auto
const
amountValue =
canonical_AMOUNT
();
127
auto
const
settleDelayValue =
canonical_UINT32
();
128
auto
const
publicKeyValue =
canonical_VL
();
129
auto
const
cancelAfterValue =
canonical_UINT32
();
130
auto
const
destinationTagValue =
canonical_UINT32
();
131
132
// Build an initial transaction
133
PaymentChannelCreateBuilder
initialBuilder{
134
accountValue,
135
destinationValue,
136
amountValue,
137
settleDelayValue,
138
publicKeyValue,
139
sequenceValue,
140
feeValue
141
};
142
143
initialBuilder.
setCancelAfter
(cancelAfterValue);
144
initialBuilder.
setDestinationTag
(destinationTagValue);
145
146
auto
initialTx = initialBuilder.
build
(publicKey, secretKey);
147
148
// Create builder from existing STTx
149
PaymentChannelCreateBuilder
builderFromTx{initialTx.getSTTx()};
150
151
auto
rebuiltTx = builderFromTx.
build
(publicKey, secretKey);
152
153
std::string
reason;
154
EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
155
156
// Verify common fields
157
EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
158
EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
159
EXPECT_EQ(rebuiltTx.getFee(), feeValue);
160
161
// Verify required fields
162
{
163
auto
const
& expected = destinationValue;
164
auto
const
actual = rebuiltTx.getDestination();
165
expectEqualField
(expected, actual,
"sfDestination"
);
166
}
167
168
{
169
auto
const
& expected = amountValue;
170
auto
const
actual = rebuiltTx.getAmount();
171
expectEqualField
(expected, actual,
"sfAmount"
);
172
}
173
174
{
175
auto
const
& expected = settleDelayValue;
176
auto
const
actual = rebuiltTx.getSettleDelay();
177
expectEqualField
(expected, actual,
"sfSettleDelay"
);
178
}
179
180
{
181
auto
const
& expected = publicKeyValue;
182
auto
const
actual = rebuiltTx.getPublicKey();
183
expectEqualField
(expected, actual,
"sfPublicKey"
);
184
}
185
186
// Verify optional fields
187
{
188
auto
const
& expected = cancelAfterValue;
189
auto
const
actualOpt = rebuiltTx.getCancelAfter();
190
ASSERT_TRUE(actualOpt.has_value()) <<
"Optional field sfCancelAfter should be present"
;
191
expectEqualField
(expected, *actualOpt,
"sfCancelAfter"
);
192
}
193
194
{
195
auto
const
& expected = destinationTagValue;
196
auto
const
actualOpt = rebuiltTx.getDestinationTag();
197
ASSERT_TRUE(actualOpt.has_value()) <<
"Optional field sfDestinationTag should be present"
;
198
expectEqualField
(expected, *actualOpt,
"sfDestinationTag"
);
199
}
200
201
}
202
203
// 3) Verify wrapper throws when constructed from wrong transaction type.
204
TEST
(TransactionsPaymentChannelCreateTests, WrapperThrowsOnWrongTxType)
205
{
206
// Build a valid transaction of a different type
207
auto
const
[pk, sk] =
208
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testWrongType"
));
209
auto
const
account =
calcAccountID
(pk);
210
211
AccountSetBuilder
wrongBuilder{account, 1,
canonical_AMOUNT
()};
212
auto
wrongTx = wrongBuilder.
build
(pk, sk);
213
214
EXPECT_THROW(
PaymentChannelCreate
{wrongTx.getSTTx()},
std::runtime_error
);
215
}
216
217
// 4) Verify builder throws when constructed from wrong transaction type.
218
TEST
(TransactionsPaymentChannelCreateTests, BuilderThrowsOnWrongTxType)
219
{
220
// Build a valid transaction of a different type
221
auto
const
[pk, sk] =
222
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testWrongTypeBuilder"
));
223
auto
const
account =
calcAccountID
(pk);
224
225
AccountSetBuilder
wrongBuilder{account, 1,
canonical_AMOUNT
()};
226
auto
wrongTx = wrongBuilder.
build
(pk, sk);
227
228
EXPECT_THROW(
PaymentChannelCreateBuilder
{wrongTx.getSTTx()},
std::runtime_error
);
229
}
230
231
// 5) Build with only required fields and verify optional fields return nullopt.
232
TEST
(TransactionsPaymentChannelCreateTests, OptionalFieldsReturnNullopt)
233
{
234
// Generate a deterministic keypair for signing
235
auto
const
[publicKey, secretKey] =
236
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testPaymentChannelCreateNullopt"
));
237
238
// Common transaction fields
239
auto
const
accountValue =
calcAccountID
(publicKey);
240
std::uint32_t
const
sequenceValue = 3;
241
auto
const
feeValue =
canonical_AMOUNT
();
242
243
// Transaction-specific required field values
244
auto
const
destinationValue =
canonical_ACCOUNT
();
245
auto
const
amountValue =
canonical_AMOUNT
();
246
auto
const
settleDelayValue =
canonical_UINT32
();
247
auto
const
publicKeyValue =
canonical_VL
();
248
249
PaymentChannelCreateBuilder
builder{
250
accountValue,
251
destinationValue,
252
amountValue,
253
settleDelayValue,
254
publicKeyValue,
255
sequenceValue,
256
feeValue
257
};
258
259
// Do NOT set optional fields
260
261
auto
tx = builder.
build
(publicKey, secretKey);
262
263
// Verify optional fields are not present
264
EXPECT_FALSE(tx.hasCancelAfter());
265
EXPECT_FALSE(tx.getCancelAfter().has_value());
266
EXPECT_FALSE(tx.hasDestinationTag());
267
EXPECT_FALSE(tx.getDestinationTag().has_value());
268
}
269
270
}
std::string
xrpl::transactions::AccountSetBuilder
Builder for AccountSet transactions.
Definition
protocol_autogen/transactions/AccountSet.h:319
xrpl::transactions::AccountSetBuilder::build
AccountSet build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the AccountSet wrapper.
Definition
protocol_autogen/transactions/AccountSet.h:470
xrpl::transactions::PaymentChannelCreateBuilder
Builder for PaymentChannelCreate transactions.
Definition
protocol_autogen/transactions/PaymentChannelCreate.h:155
xrpl::transactions::PaymentChannelCreateBuilder::build
PaymentChannelCreate build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the PaymentChannelCreate wrapper.
Definition
protocol_autogen/transactions/PaymentChannelCreate.h:270
xrpl::transactions::PaymentChannelCreateBuilder::setDestinationTag
PaymentChannelCreateBuilder & setDestinationTag(std::decay_t< typename SF_UINT32::type::value_type > const &value)
Set sfDestinationTag (SoeOptional).
Definition
protocol_autogen/transactions/PaymentChannelCreate.h:257
xrpl::transactions::PaymentChannelCreateBuilder::setCancelAfter
PaymentChannelCreateBuilder & setCancelAfter(std::decay_t< typename SF_UINT32::type::value_type > const &value)
Set sfCancelAfter (SoeOptional).
Definition
protocol_autogen/transactions/PaymentChannelCreate.h:246
xrpl::transactions::PaymentChannelCreate
Transaction: PaymentChannelCreate.
Definition
protocol_autogen/transactions/PaymentChannelCreate.h:30
std::uint32_t
xrpl::transactions
Definition
TransactionBase.h:15
xrpl::transactions::TEST
TEST(TransactionsAccountDeleteTests, BuilderSettersRoundTrip)
Definition
AccountDeleteTests.cpp:20
xrpl::KeyType::Secp256k1
@ Secp256k1
Definition
KeyType.h:9
xrpl::canonical_UINT32
UInt32Value canonical_UINT32()
Definition
/libxrpl/protocol_autogen/TestHelpers.h:48
xrpl::canonical_ACCOUNT
AccountValue canonical_ACCOUNT()
Definition
/libxrpl/protocol_autogen/TestHelpers.h:111
xrpl::canonical_VL
BlobValue canonical_VL()
Definition
/libxrpl/protocol_autogen/TestHelpers.h:139
xrpl::generateSeed
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition
Seed.cpp:58
xrpl::generateKeyPair
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
Definition
SecretKey.cpp:338
xrpl::canonical_AMOUNT
AmountValue canonical_AMOUNT()
Definition
/libxrpl/protocol_autogen/TestHelpers.h:104
xrpl::calcAccountID
AccountID calcAccountID(PublicKey const &pk)
Definition
AccountID.cpp:149
xrpl::expectEqualField
void expectEqualField(T const &expected, T const &actual, char const *fieldName)
Definition
/libxrpl/protocol_autogen/TestHelpers.h:178
std::runtime_error
string
Generated by
1.17.0