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