Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_extraction.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE trace_data_extraction
2#include <boost/test/included/unit_test.hpp>
3
10
13
14#include <fc/bitutil.hpp>
15
16using namespace sysio;
17using namespace sysio::trace_api;
18using namespace sysio::trace_api::test_common;
21
22namespace {
23 chain::transaction_trace_ptr make_transaction_trace( const chain::transaction_id_type& id, uint32_t block_number,
24 uint32_t slot, chain::transaction_receipt_header::status_enum status, std::vector<chain::action_trace>&& actions ) {
25 return std::make_shared<chain::transaction_trace>(chain::transaction_trace{
26 id,
27 block_number,
29 {},
32 0,
33 false,
34 std::move(actions),
35 {},
36 {},
37 {},
38 {},
39 {}
40 });
41 }
42
43 chain::bytes make_onerror_data( const chain::onerror& one ) {
45 fc::raw::pack(ps, one);
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, one);
51 }
52 return result;
53 }
54
55 auto make_transfer_action( chain::name from, chain::name to, chain::asset quantity, std::string memo ) {
56 return chain::action( std::vector<chain::permission_level> {{from, chain::config::active_name}},
57 "sysio.token"_n, "transfer"_n, make_transfer_data( from, to, quantity, std::move(memo) ) );
58 }
59
60 auto make_onerror_action( chain::name creator, chain::uint128_t sender_id ) {
61 return chain::action( std::vector<chain::permission_level>{{creator, chain::config::active_name}},
62 chain::onerror{ sender_id, "test ", 4 });
63 }
64
65 auto make_packed_trx( std::vector<chain::action> actions ) {
67 trx.actions = std::move( actions );
68 return packed_transaction( std::move(trx) );
69 }
70
71 auto make_trx_header( const chain::transaction& trx ) {
73 th.expiration = trx.expiration;
78 th.delay_sec = trx.delay_sec;
79 return th;
80 }
81
82 chain::action_trace make_action_trace( uint64_t global_sequence, chain::action act, chain::name receiver ) {
84 // don't think we need any information other than receiver and global sequence
85 result.receipt.emplace(chain::action_receipt{
86 receiver,
88 global_sequence,
89 0,
90 {},
91 0,
92 0
93 });
94 result.receiver = receiver;
95 result.act = std::move(act);
96 return result;
97 }
98
99}
100
109
115 template <typename BlockTrace>
116 void append( const BlockTrace& entry ) {
117 fixture.data_log.emplace_back(entry);
118 }
119
120 void append_lib( uint32_t lib ) {
121 fixture.max_lib = std::max(fixture.max_lib, lib);
122 }
123
125 fixture.id_log[tt.block_num] = tt.ids;
126 }
127
129 };
130
135
137 extraction_impl.signal_applied_transaction(trace, ptrx);
138 }
139
141 extraction_impl.signal_accepted_block(bsp);
142 }
143
144 // fixture data and methods
146 std::vector<data_log_entry> data_log = {};
147 std::unordered_map<uint32_t, std::vector<chain::transaction_id_type>> id_log;
148
150};
151
152
153BOOST_AUTO_TEST_SUITE(block_extraction)
154
155 BOOST_FIXTURE_TEST_CASE(basic_single_transaction_block, extraction_test_fixture)
156 {
157 auto act1 = make_transfer_action( "alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
158 auto act2 = make_transfer_action( "alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
159 auto act3 = make_transfer_action( "alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
160 auto actt1 = make_action_trace( 0, act1, "sysio.token"_n );
161 auto actt2 = make_action_trace( 1, act2, "alice"_n );
162 auto actt3 = make_action_trace( 2, act3, "bob"_n );
163 auto ptrx1 = make_packed_trx( { act1, act2, act3 } );
164
165 // apply a basic transfer
166 signal_applied_transaction(
167 make_transaction_trace( ptrx1.id(), 1, 1, chain::transaction_receipt_header::executed,
168 { actt1, actt2, actt3 } ),
169 std::make_shared<packed_transaction>(ptrx1) );
170
171 // accept the block with one transaction
172 auto bsp1 = make_block_state( chain::block_id_type(), 1, 1, "bp.one"_n,
173 { chain::packed_transaction(ptrx1) } );
174 signal_accepted_block( bsp1 );
175
176 const std::vector<action_trace_v1> expected_action_traces {
177 {
178 {
179 0,
180 "sysio.token"_n, "sysio.token"_n, "transfer"_n,
181 {{"alice"_n, "active"_n}},
182 make_transfer_data("alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!")
183 },
184 {}
185 },
186 {
187 {
188 1,
189 "alice"_n, "sysio.token"_n, "transfer"_n,
190 {{"alice"_n, "active"_n}},
191 make_transfer_data("alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!")
192 },
193 {}
194 },
195 {
196 {
197 2,
198 "bob"_n, "sysio.token"_n, "transfer"_n,
199 {{"alice"_n, "active"_n}},
200 make_transfer_data("alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!")
201 },
202 {}
203 }
204 };
205
206 const transaction_trace_v3 expected_transaction_trace {
207 {
208 ptrx1.id(),
209 expected_action_traces,
211 bsp1->block->transactions[0].cpu_usage_us,
212 bsp1->block->transactions[0].net_usage_words,
213 ptrx1.get_signatures(),
214 make_trx_header(ptrx1.get_transaction())
215 }
216 };
217
218 const block_trace_v2 expected_block_trace {
219 bsp1->id,
220 1,
221 bsp1->prev(),
223 "bp.one"_n,
224 bsp1->block->transaction_mroot,
225 bsp1->block->action_mroot,
226 bsp1->block->schedule_version,
227 std::vector<transaction_trace_v3> {
228 expected_transaction_trace
229 }
230 };
231
232 BOOST_REQUIRE_EQUAL(max_lib, 0);
233 BOOST_REQUIRE(data_log.size() == 1);
234 BOOST_REQUIRE(std::holds_alternative<block_trace_v2>(data_log.at(0)));
235 BOOST_REQUIRE_EQUAL(std::get<block_trace_v2>(data_log.at(0)), expected_block_trace);
236 BOOST_REQUIRE_EQUAL(id_log.at(bsp1->block_num).size(), bsp1->block->transactions.size());
237 }
238
239 BOOST_FIXTURE_TEST_CASE(basic_multi_transaction_block, extraction_test_fixture) {
240 auto act1 = make_transfer_action( "alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
241 auto act2 = make_transfer_action( "bob"_n, "alice"_n, "0.0001 SYS"_t, "Memo!" );
242 auto act3 = make_transfer_action( "fred"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
243 auto actt1 = make_action_trace( 0, act1, "sysio.token"_n );
244 auto actt2 = make_action_trace( 1, act2, "bob"_n );
245 auto actt3 = make_action_trace( 2, act3, "fred"_n );
246 auto ptrx1 = make_packed_trx( { act1 } );
247 auto ptrx2 = make_packed_trx( { act2 } );
248 auto ptrx3 = make_packed_trx( { act3 } );
249
250 signal_applied_transaction(
251 make_transaction_trace( ptrx1.id(), 1, 1, chain::transaction_receipt_header::executed,
252 { actt1 } ),
253 std::make_shared<packed_transaction>( ptrx1 ) );
254 signal_applied_transaction(
255 make_transaction_trace( ptrx2.id(), 1, 1, chain::transaction_receipt_header::executed,
256 { actt2 } ),
257 std::make_shared<packed_transaction>( ptrx2 ) );
258 signal_applied_transaction(
259 make_transaction_trace( ptrx3.id(), 1, 1, chain::transaction_receipt_header::executed,
260 { actt3 } ),
261 std::make_shared<packed_transaction>( ptrx3 ) );
262
263 // accept the block with three transaction
264 auto bsp1 = make_block_state( chain::block_id_type(), 1, 1, "bp.one"_n,
266 signal_accepted_block( bsp1 );
267
268 const std::vector<action_trace_v1> expected_action_trace1 {
269 {
270 {
271 0,
272 "sysio.token"_n, "sysio.token"_n, "transfer"_n,
273 {{"alice"_n, "active"_n}},
274 make_transfer_data("alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!")
275 },
276 {}
277 }
278 };
279
280 const std::vector<action_trace_v1> expected_action_trace2 {
281 {
282 {
283 1,
284 "bob"_n, "sysio.token"_n, "transfer"_n,
285 {{ "bob"_n, "active"_n }},
286 make_transfer_data( "bob"_n, "alice"_n, "0.0001 SYS"_t, "Memo!" )
287 },
288 {}
289 }
290 };
291
292 const std::vector<action_trace_v1> expected_action_trace3 {
293 {
294 {
295 2,
296 "fred"_n, "sysio.token"_n, "transfer"_n,
297 {{ "fred"_n, "active"_n }},
298 make_transfer_data( "fred"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" )
299 },
300 {}
301 }
302 };
303
304 const std::vector<transaction_trace_v3> expected_transaction_traces {
305 {
306 {
307 ptrx1.id(),
308 expected_action_trace1,
310 bsp1->block->transactions[0].cpu_usage_us,
311 bsp1->block->transactions[0].net_usage_words,
312 ptrx1.get_signatures(),
313 make_trx_header(ptrx1.get_transaction())
314 }
315 },
316 {
317 {
318 ptrx2.id(),
319 expected_action_trace2,
321 bsp1->block->transactions[1].cpu_usage_us,
322 bsp1->block->transactions[1].net_usage_words,
323 ptrx2.get_signatures(),
324 make_trx_header(ptrx2.get_transaction())
325 }
326 },
327 {
328 {
329 ptrx3.id(),
330 expected_action_trace3,
332 bsp1->block->transactions[2].cpu_usage_us,
333 bsp1->block->transactions[2].net_usage_words,
334 ptrx3.get_signatures(),
335 make_trx_header(ptrx3.get_transaction())
336 }
337 }
338 };
339
340 const block_trace_v2 expected_block_trace {
341 bsp1->id,
342 1,
343 bsp1->prev(),
345 "bp.one"_n,
346 bsp1->block->transaction_mroot,
347 bsp1->block->action_mroot,
348 bsp1->block->schedule_version,
349 expected_transaction_traces
350 };
351
352 BOOST_REQUIRE_EQUAL(max_lib, 0);
353 BOOST_REQUIRE(data_log.size() == 1);
354 BOOST_REQUIRE(std::holds_alternative<block_trace_v2>(data_log.at(0)));
355 BOOST_REQUIRE_EQUAL(std::get<block_trace_v2>(data_log.at(0)), expected_block_trace);
356 }
357
359 {
360 auto onerror_act = make_onerror_action( "alice"_n, 1 );
361 auto actt1 = make_action_trace( 0, onerror_act, "sysio.token"_n );
362 auto ptrx1 = make_packed_trx( { onerror_act } );
363
364 auto act2 = make_transfer_action( "bob"_n, "alice"_n, "0.0001 SYS"_t, "Memo!" );
365 auto actt2 = make_action_trace( 1, act2, "bob"_n );
366 auto transfer_trx = make_packed_trx( { act2 } );
367
368 auto onerror_trace = make_transaction_trace( ptrx1.id(), 1, 1, chain::transaction_receipt_header::executed,
369 { actt1 } );
370 auto transfer_trace = make_transaction_trace( transfer_trx.id(), 1, 1, chain::transaction_receipt_header::soft_fail,
371 { actt2 } );
372 onerror_trace->failed_dtrx_trace = transfer_trace;
373
374 signal_applied_transaction( onerror_trace, std::make_shared<packed_transaction>( transfer_trx ) );
375
376 auto bsp1 = make_block_state( chain::block_id_type(), 1, 1, "bp.one"_n,
377 { chain::packed_transaction(transfer_trx) } );
378 signal_accepted_block( bsp1 );
379
380 const std::vector<action_trace_v1> expected_action_trace {
381 {
382 {
383 0,
384 "sysio.token"_n, "sysio"_n, "onerror"_n,
385 {{ "alice"_n, "active"_n }},
386 make_onerror_data( chain::onerror{ 1, "test ", 4 } )
387 },
388 {}
389 }
390 };
391
392 const std::vector<transaction_trace_v3> expected_transaction_traces {
393 {
394 {
395 transfer_trx.id(), // transfer_trx.id() because that is the trx id known to the user
396 expected_action_trace,
398 bsp1->block->transactions[0].cpu_usage_us,
399 bsp1->block->transactions[0].net_usage_words,
400 transfer_trx.get_signatures(),
401 make_trx_header(transfer_trx.get_transaction())
402 }
403 }
404 };
405
406 const block_trace_v2 expected_block_trace {
407 bsp1->id,
408 1,
409 bsp1->prev(),
411 "bp.one"_n,
412 bsp1->block->transaction_mroot,
413 bsp1->block->action_mroot,
414 bsp1->block->schedule_version,
415 expected_transaction_traces
416 };
417
418 BOOST_REQUIRE_EQUAL(max_lib, 0);
419 BOOST_REQUIRE(data_log.size() == 1);
420 BOOST_REQUIRE(std::holds_alternative<block_trace_v2>(data_log.at(0)));
421 BOOST_REQUIRE_EQUAL(std::get<block_trace_v2>(data_log.at(0)), expected_block_trace);
422 }
423
424BOOST_AUTO_TEST_SUITE_END()
std::string one()
static sha256 hash(const char *d, uint32_t dlen)
Definition sha256.cpp:44
uint64_t id
Definition code_cache.cpp:0
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
std::shared_ptr< transaction_trace > transaction_trace_ptr
Definition trace.hpp:20
std::shared_ptr< const packed_transaction > packed_transaction_ptr
unsigned __int128 uint128_t
Definition types.hpp:242
std::shared_ptr< block_state > block_state_ptr
auto make_block_state(chain::block_id_type previous, uint32_t height, uint32_t slot, chain::name producer, std::vector< chain::packed_transaction > trxs)
chain::bytes make_transfer_data(chain::name from, chain::name to, chain::asset quantity, std::string &&memo)
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
mock_logfile_provider_type(extraction_test_fixture &fixture)
void signal_accepted_block(const chain::block_state_ptr &bsp)
chain_extraction_impl_type< mock_logfile_provider_type > extraction_impl
void signal_applied_transaction(const chain::transaction_trace_ptr &trace, const chain::packed_transaction_ptr &ptrx)
std::unordered_map< uint32_t, std::vector< chain::transaction_id_type > > id_log
std::vector< data_log_entry > data_log
Immutable except for fc::from_variant.
Definition name.hpp:43
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
@ executed
succeed, no error handler executed
Definition block.hpp:14
@ soft_fail
objectively failed (not executed), error handler executed
Definition block.hpp:15
vector< action > actions
Definition trace.hpp:93
uint32_t block_num
Definition trace.hpp:95
std::vector< chain::transaction_id_type > ids
Definition trace.hpp:94
BOOST_FIXTURE_TEST_CASE(basic_single_transaction_block, extraction_test_fixture)
yubihsm_pkcs11_slot * slot