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 ripple {
13namespace test {
14namespace jtx {
15
16namespace batch {
17
18XRPAmount
20 test::jtx::Env const& env,
21 uint32_t const& numSigners,
22 uint32_t const& txns)
23{
24 XRPAmount const feeDrops = env.current()->fees().base;
25 return ((numSigners + 2) * feeDrops) + feeDrops * txns;
26}
27
28// Batch.
31 jtx::Account const& account,
32 uint32_t seq,
33 STAmount const& fee,
35{
36 Json::Value jv;
37 jv[jss::TransactionType] = jss::Batch;
38 jv[jss::Account] = account.human();
39 jv[jss::RawTransactions] = Json::Value{Json::arrayValue};
40 jv[jss::Sequence] = seq;
41 jv[jss::Flags] = flags;
42 jv[jss::Fee] = to_string(fee);
43 return jv;
44}
45
46void
47inner::operator()(Env& env, JTx& jt) const
48{
49 auto const index = jt.jv[jss::RawTransactions].size();
50 Json::Value& batchTransaction = jt.jv[jss::RawTransactions][index];
51
52 // Initialize the batch transaction
53 batchTransaction = Json::Value{};
54 batchTransaction[jss::RawTransaction] = txn_;
55}
56
57void
58sig::operator()(Env& env, JTx& jt) const
59{
60 auto const mySigners = signers;
62 try
63 {
64 // required to cast the STObject to STTx
65 jt.jv[jss::SigningPubKey] = "";
66 st = parse(jt.jv);
67 }
68 catch (parse_error const&)
69 {
70 env.test.log << pretty(jt.jv) << std::endl;
71 Rethrow();
72 }
73 STTx const& stx = STTx{std::move(*st)};
74 auto& js = jt[sfBatchSigners.getJsonName()];
75 for (std::size_t i = 0; i < mySigners.size(); ++i)
76 {
77 auto const& e = mySigners[i];
78 auto& jo = js[i][sfBatchSigner.getJsonName()];
79 jo[jss::Account] = e.acct.human();
80 jo[jss::SigningPubKey] = strHex(e.sig.pk().slice());
81
82 Serializer msg;
84 auto const sig = ripple::sign(
85 *publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
86 jo[sfTxnSignature.getJsonName()] =
87 strHex(Slice{sig.data(), sig.size()});
88 }
89}
90
91void
92msig::operator()(Env& env, JTx& jt) const
93{
94 auto const mySigners = signers;
96 try
97 {
98 // required to cast the STObject to STTx
99 jt.jv[jss::SigningPubKey] = "";
100 st = parse(jt.jv);
101 }
102 catch (parse_error const&)
103 {
104 env.test.log << pretty(jt.jv) << std::endl;
105 Rethrow();
106 }
107 STTx const& stx = STTx{std::move(*st)};
108 auto& bs = jt[sfBatchSigners.getJsonName()];
109 auto const index = jt[sfBatchSigners.jsonName].size();
110 auto& bso = bs[index][sfBatchSigner.getJsonName()];
111 bso[jss::Account] = master.human();
112 bso[jss::SigningPubKey] = "";
113 auto& is = bso[sfSigners.getJsonName()];
114 for (std::size_t i = 0; i < mySigners.size(); ++i)
115 {
116 auto const& e = mySigners[i];
117 auto& iso = is[i][sfSigner.getJsonName()];
118 iso[jss::Account] = e.acct.human();
119 iso[jss::SigningPubKey] = strHex(e.sig.pk().slice());
120
121 Serializer msg;
123 finishMultiSigningData(e.acct.id(), msg);
124 auto const sig = ripple::sign(
125 *publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
126 iso[sfTxnSignature.getJsonName()] =
127 strHex(Slice{sig.data(), sig.size()});
128 }
129}
130
131} // namespace batch
132
133} // namespace jtx
134} // namespace test
135} // namespace ripple
Represents a JSON value.
Definition json_value.h:130
UInt size() const
Number of values in array or object.
log_os< char > log
Logging output stream.
Definition suite.h:149
std::uint32_t getFlags() const
Definition STObject.cpp:518
std::vector< uint256 > const & getBatchTransactionIDs() const
Retrieves a batch of transaction IDs from the STTx.
Definition STTx.cpp:610
Slice slice() const noexcept
Definition Serializer.h:47
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:99
A transaction testing environment.
Definition Env.h:102
beast::unit_test::suite & test
Definition Env.h:104
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:312
void operator()(Env &, JTx &jtx) const
Definition batch.cpp:47
void operator()(Env &, JTx &jt) const
Definition batch.cpp:92
std::vector< Reg > signers
Definition batch.h:119
Set a batch signature on a JTx.
Definition batch.h:93
void operator()(Env &, JTx &jt) const
Definition batch.cpp:58
std::vector< Reg > signers
Definition batch.h:95
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:25
Json::Value outer(jtx::Account const &account, uint32_t seq, STAmount const &fee, std::uint32_t flags)
Batch.
Definition batch.cpp:30
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:19
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
void finishMultiSigningData(AccountID const &signingID, Serializer &s)
Definition Sign.h:65
void serializeBatch(Serializer &msg, std::uint32_t const &flags, std::vector< uint256 > const &txids)
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
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