rippled
Loading...
Searching...
No Matches
JTx.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 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#ifndef RIPPLE_TEST_JTX_JTX_H_INCLUDED
21#define RIPPLE_TEST_JTX_JTX_H_INCLUDED
22
23#include <test/jtx/basic_prop.h>
24#include <test/jtx/requires.h>
25#include <xrpl/json/json_value.h>
26#include <xrpl/protocol/ErrorCodes.h>
27#include <xrpl/protocol/STTx.h>
28#include <xrpl/protocol/TER.h>
29
30#include <functional>
31#include <memory>
32#include <vector>
33
34namespace ripple {
35namespace test {
36namespace jtx {
37
38class Env;
39
43struct JTx
44{
50 rpcException = std::nullopt;
51 bool fill_fee = true;
52 bool fill_seq = true;
53 bool fill_sig = true;
56
57 JTx() = default;
58 JTx(JTx const&) = default;
59 JTx&
60 operator=(JTx const&) = default;
61 JTx(JTx&&) = default;
62 JTx&
63 operator=(JTx&&) = default;
64
65 JTx(Json::Value&& jv_) : jv(std::move(jv_))
66 {
67 }
68
69 JTx(Json::Value const& jv_) : jv(jv_)
70 {
71 }
72
73 template <class Key>
75 operator[](Key const& key)
76 {
77 return jv[key];
78 }
79
85 template <class Prop>
86 Prop*
88 {
89 for (auto& prop : props_.list)
90 {
91 if (auto test = dynamic_cast<prop_type<Prop>*>(prop.get()))
92 return &test->t;
93 }
94 return nullptr;
95 }
96
97 template <class Prop>
98 Prop const*
99 get() const
100 {
101 for (auto& prop : props_.list)
102 {
103 if (auto test = dynamic_cast<prop_type<Prop> const*>(prop.get()))
104 return &test->t;
105 }
106 return nullptr;
107 }
115 void
117 {
118 for (auto& prop : props_.list)
119 {
120 if (prop->assignable(p.get()))
121 {
122 prop = std::move(p);
123 return;
124 }
125 }
126 props_.list.emplace_back(std::move(p));
127 }
128
129 template <class Prop, class... Args>
130 void
131 set(Args&&... args)
132 {
133 set(std::make_unique<prop_type<Prop>>(std::forward<Args>(args)...));
134 }
137private:
139 {
140 prop_list() = default;
141
142 prop_list(prop_list const& other)
143 {
144 for (auto const& prop : other.list)
145 list.emplace_back(prop->clone());
146 }
147
148 prop_list&
149 operator=(prop_list const& other)
150 {
151 if (this != &other)
152 {
153 list.clear();
154 for (auto const& prop : other.list)
155 list.emplace_back(prop->clone());
156 }
157 return *this;
158 }
159
160 prop_list(prop_list&& src) = default;
161 prop_list&
162 operator=(prop_list&& src) = default;
163
165 };
166
168};
169
170} // namespace jtx
171} // namespace test
172} // namespace ripple
173
174#endif
Represents a JSON value.
Definition: json_value.h:147
A transaction testing environment.
Definition: Env.h:117
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: ter.h:34
T get(T... args)
T make_unique(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ tesSUCCESS
Definition: TER.h:242
STL namespace.
prop_list & operator=(prop_list &&src)=default
prop_list(prop_list const &other)
Definition: JTx.h:142
prop_list & operator=(prop_list const &other)
Definition: JTx.h:149
std::vector< std::unique_ptr< basic_prop > > list
Definition: JTx.h:164
prop_list(prop_list &&src)=default
Execution context for applying a JSON transaction.
Definition: JTx.h:44
Prop * get()
Return a property if it exists.
Definition: JTx.h:87
JTx(JTx const &)=default
void set(Args &&... args)
Definition: JTx.h:131
JTx(JTx &&)=default
std::optional< std::pair< error_code_i, std::string > > rpcCode
Definition: JTx.h:48
std::shared_ptr< STTx const > stx
Definition: JTx.h:54
Json::Value jv
Definition: JTx.h:45
std::optional< std::pair< std::string, std::optional< std::string > > > rpcException
Definition: JTx.h:50
void set(std::unique_ptr< basic_prop > p)
Set a property If the property already exists, it is replaced.
Definition: JTx.h:116
std::function< void(Env &, JTx &)> signer
Definition: JTx.h:55
JTx(Json::Value &&jv_)
Definition: JTx.h:65
Json::Value & operator[](Key const &key)
Definition: JTx.h:75
JTx & operator=(JTx const &)=default
requires_t require
Definition: JTx.h:46
Prop const * get() const
Definition: JTx.h:99
prop_list props_
Definition: JTx.h:167
JTx(Json::Value const &jv_)
Definition: JTx.h:69
JTx & operator=(JTx &&)=default
Set a property on a JTx.
Definition: prop.h:33