-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_extraction.cpp
More file actions
502 lines (437 loc) · 18.6 KB
/
test_extraction.cpp
File metadata and controls
502 lines (437 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#define BOOST_TEST_MODULE trace_data_extraction
#include <boost/test/included/unit_test.hpp>
#include <eosio/chain/types.hpp>
#include <eosio/chain/contract_types.hpp>
#include <eosio/chain/trace.hpp>
#include <eosio/chain/transaction.hpp>
#include <eosio/chain/block.hpp>
#include <eosio/chain/block_state.hpp>
#include <eosio/trace_api/test_common.hpp>
#include <eosio/trace_api/chain_extraction.hpp>
#include <fc/bitutil.hpp>
using namespace eosio;
using namespace eosio::trace_api;
using namespace eosio::trace_api::test_common;
using eosio::chain::name;
using eosio::chain::digest_type;
namespace {
chain::transaction_trace_ptr make_transaction_trace( const chain::transaction_id_type& id, uint32_t block_number,
uint32_t slot, chain::transaction_receipt_header::status_enum status, std::vector<chain::action_trace>&& actions ) {
return std::make_shared<chain::transaction_trace>(chain::transaction_trace{
id,
block_number,
chain::block_timestamp_type(slot),
{},
chain::transaction_receipt_header{status},
fc::microseconds(0),
0,
false,
std::move(actions),
{},
{},
{},
{},
{}
});
}
chain::bytes make_onerror_data( const chain::onerror& one ) {
fc::datastream<size_t> ps;
fc::raw::pack(ps, one);
chain::bytes result(ps.tellp());
if( result.size() ) {
fc::datastream<char*> ds( result.data(), size_t(result.size()) );
fc::raw::pack(ds, one);
}
return result;
}
auto get_private_key( name keyname, std::string role = "owner" ) {
auto secret = fc::sha256::hash( keyname.to_string() + role );
return chain::private_key_type::regenerate<fc::ecc::private_key_shim>( secret );
}
auto get_public_key( name keyname, std::string role = "owner" ) {
return get_private_key( keyname, role ).get_public_key();
}
auto make_transfer_action( chain::name from, chain::name to, chain::asset quantity, std::string memo ) {
return chain::action( std::vector<chain::permission_level> {{from, chain::config::active_name}},
"eosio.token"_n, "transfer"_n, make_transfer_data( from, to, quantity, std::move(memo) ) );
}
auto make_onerror_action( chain::name creator, chain::uint128_t sender_id ) {
return chain::action( std::vector<chain::permission_level>{{creator, chain::config::active_name}},
chain::onerror{ sender_id, "test ", 4 });
}
auto make_packed_trx( std::vector<chain::action> actions ) {
chain::signed_transaction trx;
trx.actions = std::move( actions );
return packed_transaction( std::move(trx), true );
}
auto make_trx_header( const chain::transaction& trx ) {
chain::transaction_header th;
th.expiration = trx.expiration;
th.ref_block_num = trx.ref_block_num;
th.ref_block_prefix = trx.ref_block_prefix;
th.max_net_usage_words = trx.max_net_usage_words;
th.max_cpu_usage_ms = trx.max_cpu_usage_ms;
th.delay_sec = trx.delay_sec;
return th;
}
chain::action_trace make_action_trace( uint64_t global_sequence, chain::action act, chain::name receiver ) {
chain::action_trace result;
// don't think we need any information other than receiver and global sequence
result.receipt.emplace(chain::action_receipt{
receiver,
digest_type::hash(act),
global_sequence,
0,
{},
0,
0
});
result.receiver = receiver;
result.act = std::move(act);
return result;
}
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::signed_block_ptr block = std::make_shared<chain::signed_block>();
for( auto& trx : trxs ) {
block->transactions.emplace_back( trx );
}
block->producer = producer;
block->timestamp = chain::block_timestamp_type(slot);
// make sure previous contains correct block # so block_header::block_num() returns correct value
if( previous == chain::block_id_type() ) {
previous._hash[0] &= 0xffffffff00000000;
previous._hash[0] += fc::endian_reverse_u32(height - 1);
}
block->previous = previous;
auto priv_key = get_private_key( block->producer, "active" );
auto pub_key = get_public_key( block->producer, "active" );
auto prev = std::make_shared<chain::block_state>();
auto header_bmroot = digest_type::hash( std::make_pair( block->digest(), prev->blockroot_merkle.get_root()));
auto sig_digest = digest_type::hash( std::make_pair( header_bmroot, prev->pending_schedule.schedule_hash ));
block->producer_signature = priv_key.sign( sig_digest );
std::vector<chain::private_key_type> signing_keys;
signing_keys.emplace_back( std::move( priv_key ));
auto signer = [&]( digest_type d ) {
std::vector<chain::signature_type> result;
result.reserve( signing_keys.size());
for( const auto& k: signing_keys )
result.emplace_back( k.sign( d ));
return result;
};
chain::pending_block_header_state pbhs;
pbhs.producer = block->producer;
pbhs.timestamp = block->timestamp;
chain::producer_authority_schedule schedule = {0, {chain::producer_authority{block->producer,
chain::block_signing_authority_v0{1, {{pub_key, 1}}}}}};
pbhs.active_schedule = schedule;
pbhs.valid_block_signing_authority = chain::block_signing_authority_v0{1, {{pub_key, 1}}};
auto pfa = pbhs.prev_activated_protocol_features;
chain::protocol_feature_set pfs;
auto bsp = std::make_shared<chain::block_state>(
std::move( pbhs ),
std::move( block ),
eosio::chain::deque<chain::transaction_metadata_ptr>(),
pfs,
[]( chain::block_timestamp_type timestamp,
const fc::flat_set<digest_type>& cur_features,
const std::vector<digest_type>& new_features ) {}
);
bool wtmsig_enabled = eosio::chain::detail::is_builtin_activated(pfa, pfs,
eosio::chain::builtin_protocol_feature_t::wtmsig_block_signatures);
bsp->assign_signatures( signer(bsp->sig_digest()), wtmsig_enabled );
bsp->block_num = height;
return bsp;
}
}
struct extraction_test_fixture {
/**
* MOCK implementation of the logfile input API
*/
struct mock_logfile_provider_type {
mock_logfile_provider_type(extraction_test_fixture& fixture)
:fixture(fixture)
{}
/**
* append an entry to the data store
*
* @param entry : the entry to append
*/
template <typename BlockTrace>
void append( const BlockTrace& entry ) {
fixture.data_log.emplace_back(entry);
}
void append_lib( uint32_t lib ) {
fixture.max_lib = std::max(fixture.max_lib, lib);
}
void append_trx_ids(const block_trxs_entry& tt){
for (const auto& id : tt.ids) {
fixture.id_log[tt.block_num] = tt.ids;
}
}
extraction_test_fixture& fixture;
};
extraction_test_fixture()
: extraction_impl(mock_logfile_provider_type(*this), exception_handler{} )
{
}
void signal_applied_transaction( const chain::transaction_trace_ptr& trace, const chain::packed_transaction_ptr& ptrx ) {
extraction_impl.signal_applied_transaction(trace, ptrx);
}
void signal_start_block( const uint32_t block_num ) {
extraction_impl.signal_block_start(block_num);
}
void signal_accepted_block( const chain::block_state_ptr& bsp ) {
extraction_impl.signal_accepted_block(bsp);
}
// fixture data and methods
uint32_t max_lib = 0;
std::vector<data_log_entry> data_log = {};
std::unordered_map<uint32_t, std::vector<chain::transaction_id_type>> id_log;
chain_extraction_impl_type<mock_logfile_provider_type> extraction_impl;
};
BOOST_AUTO_TEST_SUITE(block_extraction)
BOOST_FIXTURE_TEST_CASE(basic_single_transaction_block, extraction_test_fixture)
{
auto act1 = make_transfer_action( "alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
auto act2 = make_transfer_action( "alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
auto act3 = make_transfer_action( "alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
auto actt1 = make_action_trace( 0, act1, "eosio.token"_n );
auto actt2 = make_action_trace( 1, act2, "alice"_n );
auto actt3 = make_action_trace( 2, act3, "bob"_n );
auto ptrx1 = make_packed_trx( { act1, act2, act3 } );
uint32_t block_num = 1;
signal_start_block( block_num );
// apply a basic transfer
signal_applied_transaction(
make_transaction_trace( ptrx1.id(), 1, 1, chain::transaction_receipt_header::executed,
{ actt1, actt2, actt3 } ),
std::make_shared<packed_transaction>(ptrx1) );
// accept the block with one transaction
auto bsp1 = make_block_state( chain::block_id_type(), block_num, 1, "bp.one"_n,
{ chain::packed_transaction(ptrx1) } );
signal_accepted_block( bsp1 );
const uint32_t expected_lib = 0;
const std::vector<action_trace_v1> expected_action_traces {
{
{
0,
"eosio.token"_n, "eosio.token"_n, "transfer"_n,
{{"alice"_n, "active"_n}},
make_transfer_data("alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!")
},
{}
},
{
{
1,
"alice"_n, "eosio.token"_n, "transfer"_n,
{{"alice"_n, "active"_n}},
make_transfer_data("alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!")
},
{}
},
{
{
2,
"bob"_n, "eosio.token"_n, "transfer"_n,
{{"alice"_n, "active"_n}},
make_transfer_data("alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!")
},
{}
}
};
const transaction_trace_v2 expected_transaction_trace {
ptrx1.id(),
expected_action_traces,
fc::enum_type<uint8_t, chain::transaction_receipt_header::status_enum>{bsp1->block->transactions[0].status},
bsp1->block->transactions[0].cpu_usage_us,
bsp1->block->transactions[0].net_usage_words,
*ptrx1.get_signatures(),
make_trx_header(ptrx1.get_transaction())
};
const block_trace_v2 expected_block_trace {
bsp1->id,
1,
bsp1->prev(),
chain::block_timestamp_type(1),
"bp.one"_n,
bsp1->block->transaction_mroot,
bsp1->block->action_mroot,
bsp1->block->schedule_version,
std::vector<transaction_trace_v2> {
expected_transaction_trace
}
};
BOOST_REQUIRE_EQUAL(max_lib, expected_lib);
BOOST_REQUIRE(data_log.size() == 1);
BOOST_REQUIRE(std::holds_alternative<block_trace_v2>(data_log.at(0)));
BOOST_REQUIRE_EQUAL(std::get<block_trace_v2>(data_log.at(0)), expected_block_trace);
BOOST_REQUIRE_EQUAL(id_log.at(bsp1->block_num).size(), bsp1->block->transactions.size());
}
BOOST_FIXTURE_TEST_CASE(basic_multi_transaction_block, extraction_test_fixture) {
auto act1 = make_transfer_action( "alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
auto act2 = make_transfer_action( "bob"_n, "alice"_n, "0.0001 SYS"_t, "Memo!" );
auto act3 = make_transfer_action( "fred"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" );
auto actt1 = make_action_trace( 0, act1, "eosio.token"_n );
auto actt2 = make_action_trace( 1, act2, "bob"_n );
auto actt3 = make_action_trace( 2, act3, "fred"_n );
auto ptrx1 = make_packed_trx( { act1 } );
auto ptrx2 = make_packed_trx( { act2 } );
auto ptrx3 = make_packed_trx( { act3 } );
uint32_t block_num = 1;
signal_start_block( block_num );
signal_applied_transaction(
make_transaction_trace( ptrx1.id(), 1, 1, chain::transaction_receipt_header::executed,
{ actt1 } ),
std::make_shared<packed_transaction>( ptrx1 ) );
signal_applied_transaction(
make_transaction_trace( ptrx2.id(), 1, 1, chain::transaction_receipt_header::executed,
{ actt2 } ),
std::make_shared<packed_transaction>( ptrx2 ) );
signal_applied_transaction(
make_transaction_trace( ptrx3.id(), 1, 1, chain::transaction_receipt_header::executed,
{ actt3 } ),
std::make_shared<packed_transaction>( ptrx3 ) );
// accept the block with three transaction
auto bsp1 = make_block_state( chain::block_id_type(), block_num, 1, "bp.one"_n,
{ chain::packed_transaction(ptrx1), chain::packed_transaction(ptrx2), chain::packed_transaction(ptrx3) } );
signal_accepted_block( bsp1 );
const uint32_t expected_lib = 0;
const std::vector<action_trace_v1> expected_action_trace1 {
{
{
0,
"eosio.token"_n, "eosio.token"_n, "transfer"_n,
{{"alice"_n, "active"_n}},
make_transfer_data("alice"_n, "bob"_n, "0.0001 SYS"_t, "Memo!")
},
{}
}
};
const std::vector<action_trace_v1> expected_action_trace2 {
{
{
1,
"bob"_n, "eosio.token"_n, "transfer"_n,
{{ "bob"_n, "active"_n }},
make_transfer_data( "bob"_n, "alice"_n, "0.0001 SYS"_t, "Memo!" )
},
{}
}
};
const std::vector<action_trace_v1> expected_action_trace3 {
{
{
2,
"fred"_n, "eosio.token"_n, "transfer"_n,
{{ "fred"_n, "active"_n }},
make_transfer_data( "fred"_n, "bob"_n, "0.0001 SYS"_t, "Memo!" )
},
{}
}
};
const std::vector<transaction_trace_v2> expected_transaction_traces {
{
ptrx1.id(),
expected_action_trace1,
fc::enum_type<uint8_t, chain::transaction_receipt_header::status_enum>{bsp1->block->transactions[0].status},
bsp1->block->transactions[0].cpu_usage_us,
bsp1->block->transactions[0].net_usage_words,
*ptrx1.get_signatures(),
make_trx_header(ptrx1.get_transaction())
},
{
ptrx2.id(),
expected_action_trace2,
fc::enum_type<uint8_t, chain::transaction_receipt_header::status_enum>{bsp1->block->transactions[1].status},
bsp1->block->transactions[1].cpu_usage_us,
bsp1->block->transactions[1].net_usage_words,
*ptrx2.get_signatures(),
make_trx_header(ptrx2.get_transaction())
},
{
ptrx3.id(),
expected_action_trace3,
fc::enum_type<uint8_t, chain::transaction_receipt_header::status_enum>{bsp1->block->transactions[2].status},
bsp1->block->transactions[2].cpu_usage_us,
bsp1->block->transactions[2].net_usage_words,
*ptrx3.get_signatures(),
make_trx_header(ptrx3.get_transaction())
}
};
const block_trace_v2 expected_block_trace{
bsp1->id,
1,
bsp1->prev(),
chain::block_timestamp_type(1),
"bp.one"_n,
bsp1->block->transaction_mroot,
bsp1->block->action_mroot,
bsp1->block->schedule_version,
expected_transaction_traces
};
BOOST_REQUIRE_EQUAL(max_lib, expected_lib);
BOOST_REQUIRE(data_log.size() == 1);
BOOST_REQUIRE(std::holds_alternative<block_trace_v2>(data_log.at(0)));
BOOST_REQUIRE_EQUAL(std::get<block_trace_v2>(data_log.at(0)), expected_block_trace);
}
BOOST_FIXTURE_TEST_CASE(onerror_transaction_block, extraction_test_fixture)
{
auto onerror_act = make_onerror_action( "alice"_n, 1 );
auto actt1 = make_action_trace( 0, onerror_act, "eosio.token"_n );
auto ptrx1 = make_packed_trx( { onerror_act } );
auto act2 = make_transfer_action( "bob"_n, "alice"_n, "0.0001 SYS"_t, "Memo!" );
auto actt2 = make_action_trace( 1, act2, "bob"_n );
auto transfer_trx = make_packed_trx( { act2 } );
auto onerror_trace = make_transaction_trace( ptrx1.id(), 1, 1, chain::transaction_receipt_header::executed,
{ actt1 } );
auto transfer_trace = make_transaction_trace( transfer_trx.id(), 1, 1, chain::transaction_receipt_header::soft_fail,
{ actt2 } );
onerror_trace->failed_dtrx_trace = transfer_trace;
uint32_t block_num = 1;
signal_start_block( block_num );
signal_applied_transaction( onerror_trace, std::make_shared<packed_transaction>( transfer_trx ) );
auto bsp1 = make_block_state( chain::block_id_type(), block_num, 1, "bp.one"_n,
{ chain::packed_transaction(transfer_trx) } );
signal_accepted_block( bsp1 );
const uint32_t expected_lib = 0;
const std::vector<action_trace_v1> expected_action_trace {
{
{
0,
"eosio.token"_n, "eosio"_n, "onerror"_n,
{{ "alice"_n, "active"_n }},
make_onerror_data( chain::onerror{ 1, "test ", 4 } )
},
{}
}
};
const std::vector<transaction_trace_v2> expected_transaction_traces {
{
transfer_trx.id(), // transfer_trx.id() because that is the trx id known to the user
expected_action_trace,
fc::enum_type<uint8_t, chain::transaction_receipt_header::status_enum>{bsp1->block->transactions[0].status},
bsp1->block->transactions[0].cpu_usage_us,
bsp1->block->transactions[0].net_usage_words,
*transfer_trx.get_signatures(),
make_trx_header(transfer_trx.get_transaction())
}
};
const block_trace_v2 expected_block_trace {
bsp1->id,
1,
bsp1->prev(),
chain::block_timestamp_type(1),
"bp.one"_n,
bsp1->block->transaction_mroot,
bsp1->block->action_mroot,
bsp1->block->schedule_version,
expected_transaction_traces
};
BOOST_REQUIRE_EQUAL(max_lib, expected_lib);
BOOST_REQUIRE(data_log.size() == 1);
BOOST_REQUIRE(std::holds_alternative<block_trace_v2>(data_log.at(0)));
BOOST_REQUIRE_EQUAL(std::get<block_trace_v2>(data_log.at(0)), expected_block_trace);
}
BOOST_AUTO_TEST_SUITE_END()