Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_common.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <fc/bitutil.hpp>
4#include <fc/io/json.hpp>
5#include <fc/io/raw.hpp>
6
11#include <sysio/chain/name.hpp>
12
15
16namespace sysio::trace_api {
21 namespace test_common {
22 fc::sha256 operator"" _h(const char* input, std::size_t) {
23 return fc::sha256(input);
24 }
25
26 chain::name operator"" _n(const char* input, std::size_t) {
27 return chain::name(input);
28 }
29
30 chain::asset operator"" _t(const char* input, std::size_t) {
31 return chain::asset::from_string(input);
32 }
33
34 auto get_private_key( chain::name keyname, std::string role = "owner" ) {
35 auto secret = fc::sha256::hash( keyname.to_string() + role );
37 }
38
39 auto get_public_key( chain::name keyname, std::string role = "owner" ) {
40 return get_private_key( keyname, role ).get_public_key();
41 }
42
43 chain::bytes make_transfer_data( chain::name from, chain::name to, chain::asset quantity, std::string&& memo) {
45 fc::raw::pack(ps, from, to, quantity, memo);
46 chain::bytes result( ps.tellp());
47
48 if( result.size()) {
49 fc::datastream<char *> ds( result.data(), size_t( result.size()));
50 fc::raw::pack(ds, from, to, quantity, memo);
51 }
52 return result;
53 }
54
56 std::vector<chain::packed_transaction> trxs ) {
57 chain::signed_block_ptr block = std::make_shared<chain::signed_block>();
58 for( auto& trx : trxs ) {
59 block->transactions.emplace_back( trx );
60 }
61 block->producer = producer;
62 block->timestamp = chain::block_timestamp_type(slot);
63 // make sure previous contains correct block # so block_header::block_num() returns correct value
64 if( previous == chain::block_id_type() ) {
65 previous._hash[0] &= 0xffffffff00000000;
66 previous._hash[0] += fc::endian_reverse_u32(height - 1);
67 }
68 block->previous = previous;
69
70 auto priv_key = get_private_key( block->producer, "active" );
71 auto pub_key = get_public_key( block->producer, "active" );
72
73 auto prev = std::make_shared<chain::block_state>();
74 auto header_bmroot = chain::digest_type::hash( std::make_pair( block->digest(), prev->blockroot_merkle.get_root()));
75 auto sig_digest = chain::digest_type::hash( std::make_pair( header_bmroot, prev->pending_schedule.schedule_hash ));
76 block->producer_signature = priv_key.sign( sig_digest );
77
78 std::vector<chain::private_key_type> signing_keys;
79 signing_keys.emplace_back( std::move( priv_key ));
80 auto signer = [&]( chain::digest_type d ) {
81 std::vector<chain::signature_type> result;
82 result.reserve( signing_keys.size());
83 for( const auto& k: signing_keys )
84 result.emplace_back( k.sign( d ));
85 return result;
86 };
88 pbhs.producer = block->producer;
89 pbhs.timestamp = block->timestamp;
91 chain::block_signing_authority_v0{1, {{pub_key, 1}}}}}};
92 pbhs.active_schedule = schedule;
94 auto bsp = std::make_shared<chain::block_state>(
95 std::move( pbhs ),
96 std::move( block ),
97 std::vector<chain::transaction_metadata_ptr>(),
99 []( chain::block_timestamp_type timestamp,
100 const fc::flat_set<chain::digest_type>& cur_features,
101 const std::vector<chain::digest_type>& new_features ) {},
102 signer
103 );
104 bsp->block_num = height;
105
106 return bsp;
107 }
108
109 void to_kv_helper(const fc::variant& v, std::function<void(const std::string&, const std::string&)>&& append){
110 if (v.is_object() ) {
111 const auto& obj = v.get_object();
112 static const std::string sep = ".";
113
114 for (const auto& entry: obj) {
115 to_kv_helper( entry.value(), [&append, &entry](const std::string& path, const std::string& value){
116 append(sep + entry.key() + path, value);
117 });
118 }
119 } else if (v.is_array()) {
120 const auto& arr = v.get_array();
121 for (size_t idx = 0; idx < arr.size(); idx++) {
122 const auto& entry = arr.at(idx);
123 to_kv_helper( entry, [&append, idx](const std::string& path, const std::string& value){
124 append(std::string("[") + std::to_string(idx) + std::string("]") + path, value);
125 });
126 }
127 } else if (!v.is_null()) {
128 append("", v.as_string());
129 }
130 }
131
132 auto to_kv(const fc::variant& v) {
133 std::map<std::string, std::string> result;
134 to_kv_helper(v, [&result](const std::string& k, const std::string& v){
135 result.emplace(k, v);
136 });
137 return result;
138 }
139 }
140
141 // TODO: promote these to the main files?
142 // I prefer not to have these operators but they are convenient for BOOST TEST integration
143 //
144
146 return
147 lhs.account == rhs.account &&
148 lhs.permission == rhs.permission;
149 }
150
151 bool operator==(const action_trace_v0& lhs, const action_trace_v0& rhs) {
152 return
153 lhs.global_sequence == rhs.global_sequence &&
154 lhs.receiver == rhs.receiver &&
155 lhs.account == rhs.account &&
156 lhs.action == rhs.action &&
157 lhs.authorization == rhs.authorization &&
158 lhs.data == rhs.data;
159 }
160
162 return
163 lhs.id == rhs.id &&
164 lhs.actions == rhs.actions;
165 }
166
168 return
169 lhs.id == rhs.id &&
170 lhs.actions == rhs.actions &&
171 lhs.status == rhs.status &&
172 lhs.cpu_usage_us == rhs.cpu_usage_us &&
173 lhs.net_usage_words == rhs.net_usage_words &&
174 lhs.signatures == rhs.signatures &&
181 }
182
183 bool operator==(const block_trace_v0 &lhs, const block_trace_v0 &rhs) {
184 return
185 lhs.id == rhs.id &&
186 lhs.number == rhs.number &&
187 lhs.previous_id == rhs.previous_id &&
188 lhs.timestamp == rhs.timestamp &&
189 lhs.producer == rhs.producer &&
190 lhs.transactions == rhs.transactions;
191 }
192
193 bool operator==(const block_trace_v2 &lhs, const block_trace_v2 &rhs) {
194 return
195 lhs.id == rhs.id &&
196 lhs.number == rhs.number &&
197 lhs.previous_id == rhs.previous_id &&
198 lhs.timestamp == rhs.timestamp &&
199 lhs.producer == rhs.producer &&
201 lhs.action_mroot == rhs.action_mroot &&
203 lhs.transactions == rhs.transactions;
204 }
205
206 std::ostream& operator<<(std::ostream &os, const block_trace_v0 &bt) {
208 return os;
209 }
210
211 std::ostream& operator<<(std::ostream &os, const block_trace_v2 &bt) {
213 return os;
214 }
215
216 bool operator==(const block_entry_v0& lhs, const block_entry_v0& rhs) {
217 return
218 lhs.id == rhs.id &&
219 lhs.number == rhs.number &&
220 lhs.offset == rhs.offset;
221 }
222
223 bool operator!=(const block_entry_v0& lhs, const block_entry_v0& rhs) {
224 return !(lhs == rhs);
225 }
226
227 bool operator==(const lib_entry_v0& lhs, const lib_entry_v0& rhs) {
228 return
229 lhs.lib == rhs.lib;
230 }
231
232 bool operator!=(const lib_entry_v0& lhs, const lib_entry_v0& rhs) {
233 return !(lhs == rhs);
234 }
235
236 std::ostream& operator<<(std::ostream& os, const block_entry_v0& be) {
238 return os;
239 }
240
241 std::ostream& operator<<(std::ostream& os, const lib_entry_v0& le) {
243 return os;
244 }
245}
246
247namespace fc {
248 template<typename ...Ts>
249 std::ostream& operator<<(std::ostream &os, const std::variant<Ts...>& v ) {
251 return os;
252 }
253
254 std::ostream& operator<<(std::ostream &os, const fc::microseconds& t ) {
255 os << t.count();
256 return os;
257 }
258
259}
260
261namespace sysio::chain {
262 bool operator==(const abi_def& lhs, const abi_def& rhs) {
263 return fc::raw::pack(lhs) == fc::raw::pack(rhs);
264 }
265
266 bool operator!=(const abi_def& lhs, const abi_def& rhs) {
267 return !(lhs == rhs);
268 }
269
270 std::ostream& operator<<(std::ostream& os, const abi_def& abi) {
272 return os;
273 }
274}
275
276namespace std {
277 /*
278 * operator for printing to_kv entries
279 */
280 ostream& operator<<(ostream& os, const pair<string, string>& entry) {
281 os << entry.first + "=" + entry.second;
282 return os;
283 }
284}
static private_key regenerate(const typename KeyType::data_type &data)
static string to_string(const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.cpp:674
constexpr int64_t count() const
Definition time.hpp:26
wraps boost::filesystem::path to provide platform independent path manipulation.
static sha256 hash(const char *d, uint32_t dlen)
Definition sha256.cpp:44
static constexpr time_point maximum()
Definition time.hpp:46
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
bool is_array() const
Definition variant.cpp:368
variant_object & get_object()
Definition variant.cpp:554
bool is_null() const
Definition variant.cpp:309
bool is_object() const
Definition variant.cpp:363
string as_string() const
Definition variant.cpp:469
variants & get_array()
Definition variant.cpp:496
os_t os
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
namespace sysio::chain
Definition authority.cpp:3
datastream< ST > & operator<<(datastream< ST > &s, const sysio::chain::may_not_exist< T > &v)
Definition abi_def.hpp:146
uint32_t endian_reverse_u32(uint32_t x)
Definition bitutil.hpp:19
Definition name.hpp:106
std::ostream & operator<<(std::ostream &st, const std::variant< fc::alt_bn128_error, bytes > &err)
bool operator==(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:13
block_timestamp< config::block_interval_ms, config::block_timestamp_epoch > block_timestamp_type
bool operator!=(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:17
std::shared_ptr< signed_block > signed_block_ptr
Definition block.hpp:105
auto get_private_key(chain::name keyname, std::string role="owner")
auto to_kv(const fc::variant &v)
void to_kv_helper(const fc::variant &v, std::function< void(const std::string &, const std::string &)> &&append)
auto make_block_state(chain::block_id_type previous, uint32_t height, uint32_t slot, chain::name producer, std::vector< chain::packed_transaction > trxs)
auto get_public_key(chain::name keyname, std::string role="owner")
chain::bytes make_transfer_data(chain::name from, chain::name to, chain::asset quantity, std::string &&memo)
bool operator!=(const block_entry_v0 &lhs, const block_entry_v0 &rhs)
std::ostream & operator<<(std::ostream &os, const block_trace_v0 &bt)
bool operator==(const authorization_trace_v0 &lhs, const authorization_trace_v0 &rhs)
#define value
Definition pkcs11.h:157
unsigned int uint32_t
Definition stdint.h:126
static asset from_string(const string &from)
Definition asset.cpp:31
Immutable except for fc::from_variant.
Definition name.hpp:43
std::string to_string() const
Definition name.cpp:19
fc::unsigned_int delay_sec
upper limit on the total CPU time billed for this transaction
uint16_t ref_block_num
specifies a block num in the last 2^16 blocks.
uint32_t ref_block_prefix
specifies the lower 32 bits of the blockid at get_ref_blocknum
uint8_t max_cpu_usage_ms
upper limit on total network bandwidth (in 8 byte words) billed for this transaction
time_point_sec expiration
the time at which a transaction expires
std::vector< authorization_trace_v0 > authorization
Definition trace.hpp:20
uint64_t offset
chain::block_id_type id
uint32_t number
std::vector< transaction_trace_v0 > transactions
Definition trace.hpp:66
chain::block_id_type previous_id
Definition trace.hpp:63
chain::block_id_type id
Definition trace.hpp:61
chain::block_timestamp_type timestamp
Definition trace.hpp:64
chain::block_id_type previous_id
Definition trace.hpp:79
std::variant< std::vector< transaction_trace_v2 >, std::vector< transaction_trace_v3 > > transactions
Definition trace.hpp:85
chain::checksum256_type action_mroot
Definition trace.hpp:83
chain::checksum256_type transaction_mroot
Definition trace.hpp:82
chain::block_id_type id
Definition trace.hpp:77
chain::block_timestamp_type timestamp
Definition trace.hpp:80
uint32_t lib
chain::transaction_id_type id
Definition trace.hpp:31
std::vector< action_trace_v0 > actions
Definition trace.hpp:32
std::variant< std::vector< action_trace_v1 > > actions
Definition trace.hpp:46
std::vector< chain::signature_type > signatures
Definition trace.hpp:50
fc::enum_type< uint8_t, status_type > status
Definition trace.hpp:47
chain::transaction_id_type id
Definition trace.hpp:45
chain::transaction_header trx_header
Definition trace.hpp:51
void bt(const Operand &op, const Reg &reg)
yubihsm_pkcs11_slot * slot
CK_ULONG d