rippled
Loading...
Searching...
No Matches
batch.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2024 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx/batch.h>
21#include <test/jtx/utility.h>
22
23#include <xrpl/protocol/Batch.h>
24#include <xrpl/protocol/HashPrefix.h>
25#include <xrpl/protocol/Sign.h>
26#include <xrpl/protocol/jss.h>
27
28#include <optional>
29#include <sstream>
30
31namespace ripple {
32namespace test {
33namespace jtx {
34
35namespace batch {
36
37XRPAmount
39 test::jtx::Env const& env,
40 uint32_t const& numSigners,
41 uint32_t const& txns)
42{
43 XRPAmount const feeDrops = env.current()->fees().base;
44 return ((numSigners + 2) * feeDrops) + feeDrops * txns;
45}
46
47// Batch.
50 jtx::Account const& account,
51 uint32_t seq,
52 STAmount const& fee,
54{
55 Json::Value jv;
56 jv[jss::TransactionType] = jss::Batch;
57 jv[jss::Account] = account.human();
58 jv[jss::RawTransactions] = Json::Value{Json::arrayValue};
59 jv[jss::Sequence] = seq;
60 jv[jss::Flags] = flags;
61 jv[jss::Fee] = to_string(fee);
62 return jv;
63}
64
65void
66inner::operator()(Env& env, JTx& jt) const
67{
68 auto const index = jt.jv[jss::RawTransactions].size();
69 Json::Value& batchTransaction = jt.jv[jss::RawTransactions][index];
70
71 // Initialize the batch transaction
72 batchTransaction = Json::Value{};
73 batchTransaction[jss::RawTransaction] = txn_;
74}
75
76void
77sig::operator()(Env& env, JTx& jt) const
78{
79 auto const mySigners = signers;
81 try
82 {
83 // required to cast the STObject to STTx
84 jt.jv[jss::SigningPubKey] = "";
85 st = parse(jt.jv);
86 }
87 catch (parse_error const&)
88 {
89 env.test.log << pretty(jt.jv) << std::endl;
90 Rethrow();
91 }
92 STTx const& stx = STTx{std::move(*st)};
93 auto& js = jt[sfBatchSigners.getJsonName()];
94 for (std::size_t i = 0; i < mySigners.size(); ++i)
95 {
96 auto const& e = mySigners[i];
97 auto& jo = js[i][sfBatchSigner.getJsonName()];
98 jo[jss::Account] = e.acct.human();
99 jo[jss::SigningPubKey] = strHex(e.sig.pk().slice());
100
101 Serializer msg;
103 auto const sig = ripple::sign(
104 *publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
105 jo[sfTxnSignature.getJsonName()] =
106 strHex(Slice{sig.data(), sig.size()});
107 }
108}
109
110void
111msig::operator()(Env& env, JTx& jt) const
112{
113 auto const mySigners = signers;
115 try
116 {
117 // required to cast the STObject to STTx
118 jt.jv[jss::SigningPubKey] = "";
119 st = parse(jt.jv);
120 }
121 catch (parse_error const&)
122 {
123 env.test.log << pretty(jt.jv) << std::endl;
124 Rethrow();
125 }
126 STTx const& stx = STTx{std::move(*st)};
127 auto& bs = jt[sfBatchSigners.getJsonName()];
128 auto const index = jt[sfBatchSigners.jsonName].size();
129 auto& bso = bs[index][sfBatchSigner.getJsonName()];
130 bso[jss::Account] = master.human();
131 bso[jss::SigningPubKey] = "";
132 auto& is = bso[sfSigners.getJsonName()];
133 for (std::size_t i = 0; i < mySigners.size(); ++i)
134 {
135 auto const& e = mySigners[i];
136 auto& iso = is[i][sfSigner.getJsonName()];
137 iso[jss::Account] = e.acct.human();
138 iso[jss::SigningPubKey] = strHex(e.sig.pk().slice());
139
140 Serializer msg;
142 finishMultiSigningData(e.acct.id(), msg);
143 auto const sig = ripple::sign(
144 *publicKeyType(e.sig.pk().slice()), e.sig.sk(), msg.slice());
145 iso[sfTxnSignature.getJsonName()] =
146 strHex(Slice{sig.data(), sig.size()});
147 }
148}
149
150} // namespace batch
151
152} // namespace jtx
153} // namespace test
154} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
UInt size() const
Number of values in array or object.
log_os< char > log
Logging output stream.
Definition suite.h:152
std::uint32_t getFlags() const
Definition STObject.cpp:537
std::vector< uint256 > getBatchTransactionIDs() const
Retrieves a batch of transaction IDs from the STTx.
Definition STTx.cpp:583
Slice slice() const noexcept
Definition Serializer.h:66
An immutable linear range of bytes.
Definition Slice.h:46
Immutable cryptographic account descriptor.
Definition Account.h:39
std::string const & human() const
Returns the human readable public key.
Definition Account.h:118
A transaction testing environment.
Definition Env.h:121
beast::unit_test::suite & test
Definition Env.h:123
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:331
void operator()(Env &, JTx &jtx) const
Definition batch.cpp:66
void operator()(Env &, JTx &jt) const
Definition batch.cpp:111
std::vector< Reg > signers
Definition batch.h:138
Set a batch signature on a JTx.
Definition batch.h:112
void operator()(Env &, JTx &jt) const
Definition batch.cpp:77
std::vector< Reg > signers
Definition batch.h:114
Set the fee on a JTx.
Definition fee.h:37
Match set account flags.
Definition flags.h:128
T endl(T... args)
@ arrayValue
array value (ordered list)
Definition json_value.h:44
Json::Value outer(jtx::Account const &account, uint32_t seq, STAmount const &fee, std::uint32_t flags)
Batch.
Definition batch.cpp:49
XRPAmount calcBatchFee(jtx::Env const &env, uint32_t const &numSigners, uint32_t const &txns=0)
Calculate Batch Fee.
Definition batch.cpp:38
STObject parse(Json::Value const &jv)
Convert JSON to STObject.
Definition utility.cpp:38
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
void finishMultiSigningData(AccountID const &signingID, Serializer &s)
Definition Sign.h:84
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:30
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
void Rethrow()
Rethrow the exception currently being handled.
Definition contract.h:48
Execution context for applying a JSON transaction.
Definition JTx.h:45
Json::Value jv
Definition JTx.h:46
Thrown when parse fails.
Definition utility.h:39
Set the sequence number on a JTx.
Definition seq.h:34