rippled
Loading...
Searching...
No Matches
batch.cpp
1#include <test/jtx/batch.h>
2#include <test/jtx/utility.h>
3
4#include <xrpl/protocol/Batch.h>
5#include <xrpl/protocol/HashPrefix.h>
6#include <xrpl/protocol/Sign.h>
7#include <xrpl/protocol/jss.h>
8
9#include <optional>
10#include <sstream>
11
12namespace xrpl {
13namespace test {
14namespace jtx {
15
16namespace batch {
17
18XRPAmount
19calcBatchFee(test::jtx::Env const& env, uint32_t const& numSigners, uint32_t const& txns)
20{
21 XRPAmount const feeDrops = env.current()->fees().base;
22 return ((numSigners + 2) * feeDrops) + feeDrops * txns;
23}
24
25// Batch.
27outer(jtx::Account const& account, uint32_t seq, STAmount const& fee, std::uint32_t flags)
28{
29 Json::Value jv;
30 jv[jss::TransactionType] = jss::Batch;
31 jv[jss::Account] = account.human();
32 jv[jss::RawTransactions] = Json::Value{Json::arrayValue};
33 jv[jss::Sequence] = seq;
34 jv[jss::Flags] = flags;
35 jv[jss::Fee] = to_string(fee);
36 return jv;
37}
38
39void
40inner::operator()(Env& env, JTx& jt) const
41{
42 auto const index = jt.jv[jss::RawTransactions].size();
43 Json::Value& batchTransaction = jt.jv[jss::RawTransactions][index];
44
45 // Initialize the batch transaction
46 batchTransaction = Json::Value{};
47 batchTransaction[jss::RawTransaction] = txn_;
48}
49
50void
51sig::operator()(Env& env, JTx& jt) const
52{
53 auto const mySigners = signers;
55 try
56 {
57 // required to cast the STObject to STTx
58 jt.jv[jss::SigningPubKey] = "";
59 st = parse(jt.jv);
60 }
61 catch (parse_error const&)
62 {
63 env.test.log << pretty(jt.jv) << std::endl;
64 Rethrow();
65 }
66 STTx const& stx = STTx{std::move(*st)};
67 auto& js = jt[sfBatchSigners.getJsonName()];
68 for (std::size_t i = 0; i < mySigners.size(); ++i)
69 {
70 auto const& e = mySigners[i];
71 auto& jo = js[i][sfBatchSigner.getJsonName()];
72 jo[jss::Account] = e.acct.human();
73 jo[jss::SigningPubKey] = strHex(e.sig.pk().slice());
74
75 Serializer msg;
77 auto const sig = xrpl::sign(*publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
78 jo[sfTxnSignature.getJsonName()] = strHex(Slice{sig.data(), sig.size()});
79 }
80}
81
82void
83msig::operator()(Env& env, JTx& jt) const
84{
85 auto const mySigners = signers;
87 try
88 {
89 // required to cast the STObject to STTx
90 jt.jv[jss::SigningPubKey] = "";
91 st = parse(jt.jv);
92 }
93 catch (parse_error const&)
94 {
95 env.test.log << pretty(jt.jv) << std::endl;
96 Rethrow();
97 }
98 STTx const& stx = STTx{std::move(*st)};
99 auto& bs = jt[sfBatchSigners.getJsonName()];
100 auto const index = jt[sfBatchSigners.jsonName].size();
101 auto& bso = bs[index][sfBatchSigner.getJsonName()];
102 bso[jss::Account] = master.human();
103 bso[jss::SigningPubKey] = "";
104 auto& is = bso[sfSigners.getJsonName()];
105 for (std::size_t i = 0; i < mySigners.size(); ++i)
106 {
107 auto const& e = mySigners[i];
108 auto& iso = is[i][sfSigner.getJsonName()];
109 iso[jss::Account] = e.acct.human();
110 iso[jss::SigningPubKey] = strHex(e.sig.pk().slice());
111
112 Serializer msg;
114 finishMultiSigningData(e.acct.id(), msg);
115 auto const sig = xrpl::sign(*publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
116 iso[sfTxnSignature.getJsonName()] = strHex(Slice{sig.data(), sig.size()});
117 }
118}
119
120} // namespace batch
121
122} // namespace jtx
123} // namespace test
124} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
UInt size() const
Number of values in array or object.
log_os< char > log
Logging output stream.
Definition suite.h:145
std::uint32_t getFlags() const
Definition STObject.cpp:492
std::vector< uint256 > const & getBatchTransactionIDs() const
Retrieves a batch of transaction IDs from the STTx.
Definition STTx.cpp:545
Slice slice() const noexcept
Definition Serializer.h:45
An immutable linear range of bytes.
Definition Slice.h:27
Immutable cryptographic account descriptor.
Definition Account.h:20
std::string const & human() const
Returns the human readable public key.
Definition Account.h:95
A transaction testing environment.
Definition Env.h:98
beast::unit_test::suite & test
Definition Env.h:100
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:298
void operator()(Env &, JTx &jtx) const
Definition batch.cpp:40
std::vector< Reg > signers
Definition batch.h:111
void operator()(Env &, JTx &jt) const
Definition batch.cpp:83
Set a batch signature on a JTx.
Definition batch.h:85
void operator()(Env &, JTx &jt) const
Definition batch.cpp:51
std::vector< Reg > signers
Definition batch.h:87
Set the fee on a JTx.
Definition fee.h:18
Match set account flags.
Definition flags.h:109
T endl(T... args)
@ arrayValue
array value (ordered list)
Definition json_value.h:26
Json::Value outer(jtx::Account const &account, uint32_t seq, STAmount const &fee, std::uint32_t flags)
Batch.
Definition batch.cpp:27
XRPAmount calcBatchFee(jtx::Env const &env, uint32_t const &numSigners, uint32_t const &txns=0)
Calculate Batch Fee.
Definition batch.cpp:19
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
Definition utility.cpp:18
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
void finishMultiSigningData(AccountID const &signingID, Serializer &s)
Definition Sign.h:56
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
void serializeBatch(Serializer &msg, std::uint32_t const &flags, std::vector< uint256 > const &txids)
void Rethrow()
Rethrow the exception currently being handled.
Definition contract.h:29
Execution context for applying a JSON transaction.
Definition JTx.h:26
Json::Value jv
Definition JTx.h:27
Thrown when parse fails.
Definition utility.h:19
Set the sequence number on a JTx.
Definition seq.h:15