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