xrpld
Loading...
Searching...
No Matches
batch.cpp
1#include <test/jtx/batch.h>
2
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/JTx.h>
6#include <test/jtx/utility.h>
7
8#include <xrpl/basics/Number.h>
9#include <xrpl/basics/contract.h>
10#include <xrpl/basics/strHex.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/json/to_string.h>
13#include <xrpl/protocol/Batch.h>
14#include <xrpl/protocol/Protocol.h>
15#include <xrpl/protocol/PublicKey.h>
16#include <xrpl/protocol/SField.h>
17#include <xrpl/protocol/STAmount.h>
18#include <xrpl/protocol/STObject.h>
19#include <xrpl/protocol/STTx.h>
20#include <xrpl/protocol/SecretKey.h>
21#include <xrpl/protocol/Serializer.h>
22#include <xrpl/protocol/Sign.h>
23#include <xrpl/protocol/XRPAmount.h>
24#include <xrpl/protocol/jss.h>
25
26#include <cstddef>
27#include <cstdint>
28#include <optional>
29#include <ostream>
30#include <source_location>
31#include <string>
32#include <utility>
33
34namespace xrpl::test::jtx::batch {
35
37calcBatchFee(test::jtx::Env const& env, uint32_t const& numSigners, uint32_t const& txns)
38{
39 XRPAmount const feeDrops = env.current()->fees().base;
40 return ((numSigners + 2) * feeDrops) + feeDrops * txns;
41}
42
45 test::jtx::Env const& env,
46 uint32_t const& numSigners,
47 uint32_t const& txns)
48{
49 XRPAmount const feeDrops = env.current()->fees().base;
50 return ((numSigners + 2) * feeDrops) + feeDrops * (kConfidentialFeeMultiplier + 1) * txns;
51}
52
53// Batch.
55outer(jtx::Account const& account, uint32_t seq, STAmount const& fee, std::uint32_t flags)
56{
57 json::Value jv;
58 jv[jss::TransactionType] = jss::Batch;
59 jv[jss::Account] = account.human();
60 jv[jss::RawTransactions] = json::Value{json::ValueType::Array};
61 jv[jss::Sequence] = seq;
62 jv[jss::Flags] = flags;
63 jv[jss::Fee] = to_string(fee);
64 return jv;
65}
66
67void
69 Env& env,
70 std::string const& batchID,
71 std::string const& txHash,
72 std::string const& txType,
73 std::string const& result,
74 std::source_location const& loc)
75{
76 json::Value const jrr = env.rpc("tx", txHash)[jss::result];
77 env.test.expect(
78 jrr[sfTransactionType.jsonName] == txType, "TransactionType", loc.file_name(), loc.line());
79 env.test.expect(
80 jrr[jss::meta][sfTransactionResult.jsonName] == result,
81 "TransactionResult",
82 loc.file_name(),
83 loc.line());
84 env.test.expect(
85 jrr[jss::meta][sfParentBatchID.jsonName] == batchID,
86 "ParentBatchID",
87 loc.file_name(),
88 loc.line());
89}
90
91void
92Inner::operator()(Env& env, JTx& jt) const
93{
94 auto const index = jt.jv[jss::RawTransactions].size();
95 json::Value& batchTransaction = jt.jv[jss::RawTransactions][index];
96
97 // Initialize the batch transaction
98 batchTransaction = json::Value{};
99 batchTransaction[jss::RawTransaction] = txn_;
100}
101
102void
103Sig::operator()(Env& env, JTx& jt) const
104{
105 auto const mySigners = signers;
107 try
108 {
109 // required to cast the STObject to STTx
110 jt.jv[jss::SigningPubKey] = "";
111 st = parse(jt.jv);
112 }
113 catch (ParseError const&)
114 {
115 env.test.log << pretty(jt.jv) << std::endl;
116 rethrow();
117 }
118 STTx const& stx = STTx{std::move(*st)};
119 auto& js = jt[sfBatchSigners.getJsonName()];
120 for (std::size_t i = 0; i < mySigners.size(); ++i)
121 {
122 auto const& e = mySigners[i];
123 auto& jo = js[i][sfBatchSigner.getJsonName()];
124 jo[jss::Account] = e.acct.human();
125 jo[jss::SigningPubKey] = strHex(e.sig.pk().slice());
126
127 Serializer msg;
129 msg,
130 stx.getAccountID(sfAccount),
131 stx.getSeqProxy().value(),
132 stx.getFlags(),
134 finishMultiSigningData(e.acct.id(), msg);
135 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
136 auto const sig = xrpl::sign(*publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
137 jo[sfTxnSignature.getJsonName()] = strHex(Slice{sig.data(), sig.size()});
138 }
139}
140
141void
142Msig::operator()(Env& env, JTx& jt) const
143{
144 auto const mySigners = signers;
146 try
147 {
148 // required to cast the STObject to STTx
149 jt.jv[jss::SigningPubKey] = "";
150 st = parse(jt.jv);
151 }
152 catch (ParseError const&)
153 {
154 env.test.log << pretty(jt.jv) << std::endl;
155 rethrow();
156 }
157 STTx const& stx = STTx{std::move(*st)};
158 auto& bs = jt[sfBatchSigners.getJsonName()];
159 auto const index = jt[sfBatchSigners.jsonName].size();
160 auto& bso = bs[index][sfBatchSigner.getJsonName()];
161 bso[jss::Account] = master.human();
162 bso[jss::SigningPubKey] = "";
163 auto& is = bso[sfSigners.getJsonName()];
164 for (std::size_t i = 0; i < mySigners.size(); ++i)
165 {
166 auto const& e = mySigners[i];
167 auto& iso = is[i][sfSigner.getJsonName()];
168 iso[jss::Account] = e.acct.human();
169 iso[jss::SigningPubKey] = strHex(e.sig.pk().slice());
170
171 Serializer msg;
173 msg,
174 stx.getAccountID(sfAccount),
175 stx.getSeqProxy().value(),
176 stx.getFlags(),
178 msg.addBitString(master.id());
179 finishMultiSigningData(e.acct.id(), msg);
180 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
181 auto const sig = xrpl::sign(*publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
182 iso[sfTxnSignature.getJsonName()] = strHex(Slice{sig.data(), sig.size()});
183 }
184}
185
186} // namespace xrpl::test::jtx::batch
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:235
LogOs< char > log
Logging output stream.
Definition suite.h:150
Represents a JSON value.
Definition json_value.h:117
UInt size() const
Number of values in array or object.
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
std::uint32_t getFlags() const
Definition STObject.cpp:517
SeqProxy getSeqProxy() const
Definition STTx.cpp:198
std::vector< UInt256 > getBatchTransactionIDs() const
The IDs of the inner transactions of a Batch.
Definition STTx.cpp:637
constexpr std::uint32_t value() const
Definition SeqProxy.h:80
int addBitString(BaseUInt< Bits, Tag > const &v)
Definition Serializer.h:202
Slice slice() const noexcept
Definition Serializer.h:141
An immutable linear range of bytes.
Definition Slice.h:28
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
A transaction testing environment.
Definition Env.h:161
json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:1058
beast::unit_test::Suite & test
Definition Env.h:163
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
void operator()(Env &, JTx &jtx) const
Definition batch.cpp:92
std::vector< Reg > signers
Definition batch.h:170
void operator()(Env &, JTx &jt) const
Definition batch.cpp:142
std::vector< Reg > signers
Definition batch.h:144
void operator()(Env &, JTx &jt) const
Definition batch.cpp:103
T endl(T... args)
T file_name(T... args)
@ Array
array value (ordered list)
Definition json_value.h:28
Helpers for constructing Batch test transactions.
Definition batch.h:26
XRPAmount calcConfidentialBatchFee(jtx::Env const &env, uint32_t const &numSigners, uint32_t const &txns=0)
Calculate the expected Batch fee when inner transactions are confidential MPT transactions.
Definition batch.cpp:44
void validateInnerTxn(Env &env, std::string const &batchID, std::string const &txHash, std::string const &txType, std::string const &result, std::source_location const &loc=std::source_location::current())
Expect an inner Batch transaction to be recorded with the given type and result, and with ParentBatch...
Definition batch.cpp:68
json::Value outer(jtx::Account const &account, uint32_t seq, STAmount const &fee, std::uint32_t flags)
Build an outer Batch transaction JSON object.
Definition batch.cpp:55
XRPAmount calcBatchFee(jtx::Env const &env, uint32_t const &numSigners, uint32_t const &txns=0)
Calculate the expected outer Batch transaction fee.
Definition batch.cpp:37
STObject parse(json::Value const &jv)
Convert JSON to STObject.
Definition utility.cpp:33
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:54
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
constexpr std::uint32_t kConfidentialFeeMultiplier
Extra base fee multiplier charged to confidential MPT transactions.
Definition Protocol.h:551
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:657
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
XRPL_NO_SANITIZE_ADDRESS void rethrow()
Rethrow the exception currently being handled.
Definition contract.h:36
void finishMultiSigningData(AccountID const &signingID, Serializer &s)
Definition Sign.h:128
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
void serializeBatch(Serializer &msg, AccountID const &outerAccount, std::uint32_t outerSeqValue, std::uint32_t const &flags, std::vector< UInt256 > const &txids)
Execution context for applying a JSON transaction.
Definition JTx.h:27
json::Value jv
Definition JTx.h:28
Thrown when parse fails.
Definition utility.h:23