xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
protocol_autogen
transactions
PaymentChannelFundTests.cpp
1
// Auto-generated unit tests for transaction PaymentChannelFund
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/PaymentChannelFund.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
(TransactionsPaymentChannelFundTests, BuilderSettersRoundTrip)
21
{
22
// Generate a deterministic keypair for signing
23
auto
const
[publicKey, secretKey] =
24
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testPaymentChannelFund"
));
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
channelValue =
canonical_UINT256
();
33
auto
const
amountValue =
canonical_AMOUNT
();
34
auto
const
expirationValue =
canonical_UINT32
();
35
36
PaymentChannelFundBuilder
builder{
37
accountValue,
38
channelValue,
39
amountValue,
40
sequenceValue,
41
feeValue
42
};
43
44
// Set optional fields
45
builder.
setExpiration
(expirationValue);
46
47
auto
tx = builder.
build
(publicKey, secretKey);
48
49
std::string
reason;
50
EXPECT_TRUE(tx.validate(reason)) << reason;
51
52
// Verify signing was applied
53
EXPECT_FALSE(tx.getSigningPubKey().empty());
54
EXPECT_TRUE(tx.hasTxnSignature());
55
56
// Verify common fields
57
EXPECT_EQ(tx.getAccount(), accountValue);
58
EXPECT_EQ(tx.getSequence(), sequenceValue);
59
EXPECT_EQ(tx.getFee(), feeValue);
60
61
// Verify required fields
62
{
63
auto
const
& expected = channelValue;
64
auto
const
actual = tx.getChannel();
65
expectEqualField
(expected, actual,
"sfChannel"
);
66
}
67
68
{
69
auto
const
& expected = amountValue;
70
auto
const
actual = tx.getAmount();
71
expectEqualField
(expected, actual,
"sfAmount"
);
72
}
73
74
// Verify optional fields
75
{
76
auto
const
& expected = expirationValue;
77
auto
const
actualOpt = tx.getExpiration();
78
ASSERT_TRUE(actualOpt.has_value()) <<
"Optional field sfExpiration should be present"
;
79
expectEqualField
(expected, *actualOpt,
"sfExpiration"
);
80
EXPECT_TRUE(tx.hasExpiration());
81
}
82
83
}
84
85
// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
86
// and verify all fields match.
87
TEST
(TransactionsPaymentChannelFundTests, BuilderFromStTxRoundTrip)
88
{
89
// Generate a deterministic keypair for signing
90
auto
const
[publicKey, secretKey] =
91
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testPaymentChannelFundFromTx"
));
92
93
// Common transaction fields
94
auto
const
accountValue =
calcAccountID
(publicKey);
95
std::uint32_t
const
sequenceValue = 2;
96
auto
const
feeValue =
canonical_AMOUNT
();
97
98
// Transaction-specific field values
99
auto
const
channelValue =
canonical_UINT256
();
100
auto
const
amountValue =
canonical_AMOUNT
();
101
auto
const
expirationValue =
canonical_UINT32
();
102
103
// Build an initial transaction
104
PaymentChannelFundBuilder
initialBuilder{
105
accountValue,
106
channelValue,
107
amountValue,
108
sequenceValue,
109
feeValue
110
};
111
112
initialBuilder.
setExpiration
(expirationValue);
113
114
auto
initialTx = initialBuilder.
build
(publicKey, secretKey);
115
116
// Create builder from existing STTx
117
PaymentChannelFundBuilder
builderFromTx{initialTx.getSTTx()};
118
119
auto
rebuiltTx = builderFromTx.
build
(publicKey, secretKey);
120
121
std::string
reason;
122
EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
123
124
// Verify common fields
125
EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
126
EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
127
EXPECT_EQ(rebuiltTx.getFee(), feeValue);
128
129
// Verify required fields
130
{
131
auto
const
& expected = channelValue;
132
auto
const
actual = rebuiltTx.getChannel();
133
expectEqualField
(expected, actual,
"sfChannel"
);
134
}
135
136
{
137
auto
const
& expected = amountValue;
138
auto
const
actual = rebuiltTx.getAmount();
139
expectEqualField
(expected, actual,
"sfAmount"
);
140
}
141
142
// Verify optional fields
143
{
144
auto
const
& expected = expirationValue;
145
auto
const
actualOpt = rebuiltTx.getExpiration();
146
ASSERT_TRUE(actualOpt.has_value()) <<
"Optional field sfExpiration should be present"
;
147
expectEqualField
(expected, *actualOpt,
"sfExpiration"
);
148
}
149
150
}
151
152
// 3) Verify wrapper throws when constructed from wrong transaction type.
153
TEST
(TransactionsPaymentChannelFundTests, WrapperThrowsOnWrongTxType)
154
{
155
// Build a valid transaction of a different type
156
auto
const
[pk, sk] =
157
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testWrongType"
));
158
auto
const
account =
calcAccountID
(pk);
159
160
AccountSetBuilder
wrongBuilder{account, 1,
canonical_AMOUNT
()};
161
auto
wrongTx = wrongBuilder.
build
(pk, sk);
162
163
EXPECT_THROW(
PaymentChannelFund
{wrongTx.getSTTx()},
std::runtime_error
);
164
}
165
166
// 4) Verify builder throws when constructed from wrong transaction type.
167
TEST
(TransactionsPaymentChannelFundTests, BuilderThrowsOnWrongTxType)
168
{
169
// Build a valid transaction of a different type
170
auto
const
[pk, sk] =
171
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testWrongTypeBuilder"
));
172
auto
const
account =
calcAccountID
(pk);
173
174
AccountSetBuilder
wrongBuilder{account, 1,
canonical_AMOUNT
()};
175
auto
wrongTx = wrongBuilder.
build
(pk, sk);
176
177
EXPECT_THROW(
PaymentChannelFundBuilder
{wrongTx.getSTTx()},
std::runtime_error
);
178
}
179
180
// 5) Build with only required fields and verify optional fields return nullopt.
181
TEST
(TransactionsPaymentChannelFundTests, OptionalFieldsReturnNullopt)
182
{
183
// Generate a deterministic keypair for signing
184
auto
const
[publicKey, secretKey] =
185
generateKeyPair
(
KeyType::Secp256k1
,
generateSeed
(
"testPaymentChannelFundNullopt"
));
186
187
// Common transaction fields
188
auto
const
accountValue =
calcAccountID
(publicKey);
189
std::uint32_t
const
sequenceValue = 3;
190
auto
const
feeValue =
canonical_AMOUNT
();
191
192
// Transaction-specific required field values
193
auto
const
channelValue =
canonical_UINT256
();
194
auto
const
amountValue =
canonical_AMOUNT
();
195
196
PaymentChannelFundBuilder
builder{
197
accountValue,
198
channelValue,
199
amountValue,
200
sequenceValue,
201
feeValue
202
};
203
204
// Do NOT set optional fields
205
206
auto
tx = builder.
build
(publicKey, secretKey);
207
208
// Verify optional fields are not present
209
EXPECT_FALSE(tx.hasExpiration());
210
EXPECT_FALSE(tx.getExpiration().has_value());
211
}
212
213
}
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::PaymentChannelFundBuilder
Builder for PaymentChannelFund transactions.
Definition
protocol_autogen/transactions/PaymentChannelFund.h:107
xrpl::transactions::PaymentChannelFundBuilder::build
PaymentChannelFund build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the PaymentChannelFund wrapper.
Definition
protocol_autogen/transactions/PaymentChannelFund.h:185
xrpl::transactions::PaymentChannelFundBuilder::setExpiration
PaymentChannelFundBuilder & setExpiration(std::decay_t< typename SF_UINT32::type::value_type > const &value)
Set sfExpiration (SoeOptional).
Definition
protocol_autogen/transactions/PaymentChannelFund.h:172
xrpl::transactions::PaymentChannelFund
Transaction: PaymentChannelFund.
Definition
protocol_autogen/transactions/PaymentChannelFund.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_UINT256
UInt256Value canonical_UINT256()
Definition
/libxrpl/protocol_autogen/TestHelpers.h:83
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