xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
tx
tests/libxrpl/tx/ProposalHelpers.cpp
1
#include <xrpl/ledger/helpers/ProposalHelpers.h>
2
3
#include <xrpl/protocol/SField.h>
4
#include <xrpl/protocol/STArray.h>
5
#include <xrpl/protocol/STObject.h>
6
#include <xrpl/protocol/TxFlags.h>
7
#include <xrpl/protocol/TxFormats.h>
8
9
#include <gtest/gtest.h>
10
11
#include <
cstdint
>
12
#include <
utility
>
13
14
namespace
xrpl::test
{
15
16
namespace
{
17
18
// A bare STObject carrying only sfTransactionType. isValidProposalTxnType accepts an
19
// STObject (not an STTx) precisely so that this file's defense-in-depth
20
// checks can be tested without the surrounding STTx format validation.
21
inline
STObject
22
txOfType(std::uint16_t txType)
23
{
24
STObject tx(
sfGeneric
);
25
tx.setFieldU16(sfTransactionType, txType);
26
return
tx;
27
}
28
29
// A bare Batch STObject wrapping the given inner transaction as its single
30
// sfRawTransactions entry.
31
inline
STObject
32
batchWrapping(STObject inner)
33
{
34
STArray rawTxns(sfRawTransactions);
35
rawTxns.push_back(std::move(inner));
36
37
STObject
batch
(
sfGeneric
);
38
batch
.setFieldU16(sfTransactionType, ttBATCH);
39
batch
.setFieldArray(sfRawTransactions, rawTxns);
40
return
batch
;
41
}
42
43
}
// namespace
44
45
// The happy path — an ordinary Payment is independently submittable.
46
TEST
(ProposalHelpers, plain_payment_is_valid)
47
{
48
EXPECT_TRUE(
proposal::isValidProposalTxnType
(txOfType(ttPAYMENT)));
49
}
50
51
// No TransactionType at all. STTx construction rejects this first, but the
52
// helper takes any STObject, so it must return false rather than throw.
53
TEST
(ProposalHelpers, typeless_object_is_rejected)
54
{
55
EXPECT_FALSE(
proposal::isValidProposalTxnType
(
STObject
{
sfGeneric
}));
56
}
57
58
// A nested TransactionProposalCreate. In practice STTx construction rejects
59
// this earlier (the payload lacks TransactionProposalCreate's own template
60
// fields), but the defense here re-checks that guard so the two cannot
61
// drift apart.
62
TEST
(ProposalHelpers, nested_proposal_is_rejected)
63
{
64
EXPECT_FALSE(
proposal::isValidProposalTxnType
(txOfType(ttTRANSACTION_PROPOSAL_CREATE)));
65
}
66
67
// Any pseudo-transaction — see STTx::isPseudoTx. Also normally caught earlier
68
// by STTx construction / preflight0.
69
TEST
(ProposalHelpers, pseudo_tx_is_rejected)
70
{
71
EXPECT_FALSE(
proposal::isValidProposalTxnType
(txOfType(ttAMENDMENT)));
72
EXPECT_FALSE(
proposal::isValidProposalTxnType
(txOfType(ttFEE)));
73
EXPECT_FALSE(
proposal::isValidProposalTxnType
(txOfType(ttUNL_MODIFY)));
74
}
75
76
// tfInnerBatchTxn marks a transaction as an inner leg of an enclosing Batch,
77
// so it must never stand on its own as a proposed transaction. preflight0
78
// rejects the standalone case with temINVALID_INNER_BATCH before we get
79
// here; the guard is re-checked so the two cannot drift apart.
80
TEST
(ProposalHelpers, inner_batch_flag_is_rejected)
81
{
82
STObject
tx = txOfType(ttPAYMENT);
83
tx.
setFieldU32
(sfFlags,
tfInnerBatchTxn
);
84
EXPECT_FALSE(
proposal::isValidProposalTxnType
(tx));
85
}
86
87
// A Flags value that is present but does not include tfInnerBatchTxn must
88
// not be rejected — the check is bit-specific, not "any flag present".
89
TEST
(ProposalHelpers, other_flags_are_accepted)
90
{
91
STObject
tx = txOfType(ttPAYMENT);
92
tx.
setFieldU32
(sfFlags,
tfFullyCanonicalSig
);
93
EXPECT_TRUE(
proposal::isValidProposalTxnType
(tx));
94
}
95
96
// A Batch wrapping a plain inner is fine — the loop is only there to catch
97
// specifically forbidden inner types.
98
TEST
(ProposalHelpers, batch_with_plain_inner_is_valid)
99
{
100
EXPECT_TRUE(
proposal::isValidProposalTxnType
(batchWrapping(txOfType(ttPAYMENT))));
101
}
102
103
// A Batch whose inner is itself a proposal must be rejected.
104
TEST
(ProposalHelpers, batch_with_nested_proposal_inner_is_rejected)
105
{
106
EXPECT_FALSE(
107
proposal::isValidProposalTxnType
(batchWrapping(txOfType(ttTRANSACTION_PROPOSAL_CREATE))));
108
}
109
110
// A Batch whose inner is a pseudo-transaction must be rejected.
111
TEST
(ProposalHelpers, batch_with_pseudo_inner_is_rejected)
112
{
113
EXPECT_FALSE(
proposal::isValidProposalTxnType
(batchWrapping(txOfType(ttAMENDMENT))));
114
}
115
116
// A Batch with no sfRawTransactions field skips the inner-loop entirely.
117
// Not something the transactor would ever emit, but the branch exists in the
118
// helper (the field is optional at the STObject level) and should hold.
119
TEST
(ProposalHelpers, batch_without_raw_transactions_is_valid)
120
{
121
EXPECT_TRUE(
proposal::isValidProposalTxnType
(txOfType(ttBATCH)));
122
}
123
124
}
// namespace xrpl::test
xrpl::STObject
Definition
STObject.h:50
xrpl::STObject::setFieldU32
void setFieldU32(SField const &field, std::uint32_t)
Definition
STObject.cpp:743
cstdint
xrpl::proposal::isValidProposalTxnType
bool isValidProposalTxnType(STObject const &proposedTx)
Whether the proposed transaction is independently submittable through the ordinary multi-sign path: n...
Definition
libxrpl/ledger/helpers/ProposalHelpers.cpp:44
xrpl::test::jtx::batch
Helpers for constructing Batch test transactions.
Definition
batch.h:26
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::test::TEST
TEST(UnitsTest, types)
Definition
Units.cpp:16
xrpl::tfInnerBatchTxn
constexpr FlagValue tfInnerBatchTxn
Definition
TxFlags.h:44
xrpl::sfGeneric
SField const sfGeneric
xrpl::tfFullyCanonicalSig
constexpr FlagValue tfFullyCanonicalSig
Definition
TxFlags.h:43
utility
Generated by
1.17.0