-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtester.cpp
More file actions
1333 lines (1110 loc) · 54.6 KB
/
tester.cpp
File metadata and controls
1333 lines (1110 loc) · 54.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/block_log.hpp>
#include <eosio/chain/block_state.hpp>
#include <eosio/chain/to_string.hpp>
#include <eosio/chain/wast_to_wasm.hpp>
#include <eosio/chain/eosio_contract.hpp>
#include <eosio/chain/generated_transaction_object.hpp>
#include <eosio/chain/to_string.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <fstream>
#include <contracts.hpp>
namespace bio = boost::iostreams;
eosio::chain::asset core_from_string(const std::string& s) {
return eosio::chain::asset::from_string(s + " " CORE_SYMBOL_NAME);
}
namespace eosio { namespace testing {
std::string read_wast( const char* fn ) {
std::ifstream wast_file(fn);
FC_ASSERT( wast_file.is_open(), "wast file cannot be found" );
wast_file.seekg(0, std::ios::end);
std::vector<char> wast;
int len = wast_file.tellg();
FC_ASSERT( len >= 0, "wast file length is -1" );
wast.resize(len+1);
wast_file.seekg(0, std::ios::beg);
wast_file.read(wast.data(), wast.size());
wast[wast.size()-1] = '\0';
wast_file.close();
return {wast.data()};
}
std::vector<uint8_t> read_wasm( const char* fn ) {
std::ifstream wasm_file(fn, std::ios::binary);
FC_ASSERT( wasm_file.is_open(), "wasm file cannot be found" );
wasm_file.seekg(0, std::ios::end);
std::vector<uint8_t> wasm;
int len = wasm_file.tellg();
FC_ASSERT( len >= 0, "wasm file length is -1" );
wasm.resize(len);
wasm_file.seekg(0, std::ios::beg);
wasm_file.read((char*)wasm.data(), wasm.size());
wasm_file.close();
return wasm;
}
std::vector<char> read_abi( const char* fn ) {
std::ifstream abi_file(fn);
FC_ASSERT( abi_file.is_open(), "abi file cannot be found" );
abi_file.seekg(0, std::ios::end);
std::vector<char> abi;
int len = abi_file.tellg();
FC_ASSERT( len >= 0, "abi file length is -1" );
abi.resize(len+1);
abi_file.seekg(0, std::ios::beg);
abi_file.read(abi.data(), abi.size());
abi[abi.size()-1] = '\0';
abi_file.close();
return abi;
}
namespace {
std::string read_gzipped_snapshot( const char* fn ) {
std::ifstream file(fn, std::ios_base::in | std::ios_base::binary);
bio::filtering_streambuf<bio::input> in;
in.push(bio::gzip_decompressor());
in.push(file);
std::stringstream decompressed;
bio::copy(in, decompressed);
return decompressed.str();
}
}
std::string read_binary_snapshot( const char* fn ) {
return read_gzipped_snapshot(fn);
}
fc::variant read_json_snapshot( const char* fn ) {
return fc::json::from_string( read_gzipped_snapshot(fn) );
}
const fc::microseconds base_tester::abi_serializer_max_time{1000*1000}; // 1s for slow test machines
bool expect_assert_message(const fc::exception& ex, string expected) {
BOOST_TEST_MESSAGE("LOG : " << "expected: " << expected << ", actual: " << ex.get_log().at(0).get_message());
return (ex.get_log().at(0).get_message().find(expected) != std::string::npos);
}
fc::variant_object filter_fields(const fc::variant_object& filter, const fc::variant_object& value) {
fc::mutable_variant_object res;
for( auto& entry : filter ) {
auto it = value.find(entry.key());
res( it->key(), it->value() );
}
return res;
}
void copy_row(const chain::key_value_object& obj, vector<char>& data) {
data.resize( obj.value.size() );
memcpy( data.data(), obj.value.data(), obj.value.size() );
}
protocol_feature_set make_protocol_feature_set(const subjective_restriction_map& custom_subjective_restrictions) {
protocol_feature_set pfs;
map< builtin_protocol_feature_t, std::optional<digest_type> > visited_builtins;
std::function<digest_type(builtin_protocol_feature_t)> add_builtins =
[&pfs, &visited_builtins, &add_builtins, &custom_subjective_restrictions]
( builtin_protocol_feature_t codename ) -> digest_type {
auto res = visited_builtins.emplace( codename, std::optional<digest_type>() );
if( !res.second ) {
EOS_ASSERT( res.first->second, protocol_feature_exception,
"invariant failure: cycle found in builtin protocol feature dependencies"
);
return *res.first->second;
}
auto f = protocol_feature_set::make_default_builtin_protocol_feature( codename,
[&add_builtins]( builtin_protocol_feature_t d ) {
return add_builtins( d );
} );
const auto itr = custom_subjective_restrictions.find(codename);
if( itr != custom_subjective_restrictions.end() ) {
f.subjective_restrictions = itr->second;
}
const auto& pf = pfs.add_feature( f );
res.first->second = pf.feature_digest;
return pf.feature_digest;
};
for( const auto& p : builtin_protocol_feature_codenames ) {
add_builtins( p.first );
}
return pfs;
}
bool base_tester::is_same_chain( base_tester& other ) {
return control->head_block_id() == other.control->head_block_id();
}
void base_tester::init(const setup_policy policy, db_read_mode read_mode,
std::optional<uint32_t> genesis_max_inline_action_size,
std::optional<uint32_t> config_max_nonprivileged_inline_action_size,
std::optional<backing_store_type> config_backing_store) {
auto def_conf = default_config(tempdir, genesis_max_inline_action_size,
config_max_nonprivileged_inline_action_size, config_backing_store);
def_conf.first.read_mode = read_mode;
cfg = def_conf.first;
open(def_conf.second);
execute_setup_policy(policy);
}
void base_tester::init(controller::config config, const snapshot_reader_ptr& snapshot) {
cfg = config;
open(snapshot);
}
void base_tester::init(controller::config config, const genesis_state& genesis) {
cfg = config;
open(genesis);
}
void base_tester::init(controller::config config) {
cfg = config;
open(default_genesis().compute_chain_id());
}
void base_tester::init(controller::config config, protocol_feature_set&& pfs, const snapshot_reader_ptr& snapshot) {
cfg = config;
open(std::move(pfs), snapshot);
}
void base_tester::init(controller::config config, protocol_feature_set&& pfs, const genesis_state& genesis) {
cfg = config;
open(std::move(pfs), genesis);
}
void base_tester::init(controller::config config, protocol_feature_set&& pfs) {
cfg = config;
open(std::move(pfs), default_genesis().compute_chain_id());
}
void base_tester::execute_setup_policy(const setup_policy policy) {
const auto& pfm = control->get_protocol_feature_manager();
auto schedule_preactivate_protocol_feature = [&]() {
auto preactivate_feature_digest = pfm.get_builtin_digest(builtin_protocol_feature_t::preactivate_feature);
FC_ASSERT( preactivate_feature_digest, "PREACTIVATE_FEATURE not found" );
schedule_protocol_features_wo_preactivation( { *preactivate_feature_digest } );
};
switch (policy) {
case setup_policy::old_bios_only: {
set_before_preactivate_bios_contract();
break;
}
case setup_policy::preactivate_feature_only: {
schedule_preactivate_protocol_feature();
produce_block(); // block production is required to activate protocol feature
break;
}
case setup_policy::preactivate_feature_and_new_bios: {
schedule_preactivate_protocol_feature();
produce_block();
set_before_producer_authority_bios_contract();
break;
}
case setup_policy::old_wasm_parser: {
schedule_preactivate_protocol_feature();
produce_block();
set_before_producer_authority_bios_contract();
preactivate_builtin_protocol_features({
builtin_protocol_feature_t::only_link_to_existing_permission,
builtin_protocol_feature_t::replace_deferred,
builtin_protocol_feature_t::no_duplicate_deferred_id,
builtin_protocol_feature_t::fix_linkauth_restriction,
builtin_protocol_feature_t::disallow_empty_producer_schedule,
builtin_protocol_feature_t::restrict_action_to_self,
builtin_protocol_feature_t::only_bill_first_authorizer,
builtin_protocol_feature_t::forward_setcode,
builtin_protocol_feature_t::get_sender,
builtin_protocol_feature_t::ram_restrictions,
builtin_protocol_feature_t::webauthn_key,
builtin_protocol_feature_t::wtmsig_block_signatures,
builtin_protocol_feature_t::kv_database
});
produce_block();
set_bios_contract();
break;
}
case setup_policy::full: {
schedule_preactivate_protocol_feature();
produce_block();
set_before_producer_authority_bios_contract();
preactivate_all_builtin_protocol_features();
produce_block();
set_bios_contract();
break;
}
case setup_policy::none:
default:
break;
};
}
void base_tester::close() {
control.reset();
chain_transactions.clear();
}
void base_tester::open( const snapshot_reader_ptr& snapshot ) {
open( make_protocol_feature_set(), snapshot );
}
void base_tester::open( const genesis_state& genesis ) {
open( make_protocol_feature_set(), genesis );
}
void base_tester::open( std::optional<chain_id_type> expected_chain_id ) {
open( make_protocol_feature_set(), expected_chain_id );
}
void base_tester::open( protocol_feature_set&& pfs, std::optional<chain_id_type> expected_chain_id, const std::function<void()>& lambda ) {
if( !expected_chain_id ) {
expected_chain_id = controller::extract_chain_id_from_db( cfg.state_dir );
if( !expected_chain_id ) {
if( fc::is_regular_file( cfg.blog.log_dir / "blocks.log" ) ) {
expected_chain_id = block_log::extract_chain_id( cfg.blog.log_dir );
} else {
expected_chain_id = genesis_state().compute_chain_id();
}
}
}
control.reset( new controller(cfg, std::move(pfs), *expected_chain_id) );
control->add_indices();
if (lambda) lambda();
chain_transactions.clear();
control->accepted_block.connect([this]( const block_state_ptr& block_state ){
FC_ASSERT( block_state->block );
for( auto receipt : block_state->block->transactions ) {
if( std::holds_alternative<packed_transaction>(receipt.trx) ) {
auto &pt = std::get<packed_transaction>(receipt.trx);
chain_transactions[pt.get_transaction().id()] = std::move(receipt);
} else {
auto& id = std::get<transaction_id_type>(receipt.trx);
chain_transactions[id] = std::move(receipt);
}
}
});
}
void base_tester::open( protocol_feature_set&& pfs, const snapshot_reader_ptr& snapshot ) {
const auto& snapshot_chain_id = controller::extract_chain_id( *snapshot );
snapshot->return_to_header();
open(std::move(pfs), snapshot_chain_id, [&snapshot,&control=this->control]() {
control->startup( [](){}, []() { return false; }, snapshot );
});
}
void base_tester::open( protocol_feature_set&& pfs, const genesis_state& genesis ) {
open(std::move(pfs), genesis.compute_chain_id(), [&genesis,&control=this->control]() {
control->startup( [](){}, []() { return false; }, genesis );
});
}
void base_tester::open( protocol_feature_set&& pfs, std::optional<chain_id_type> expected_chain_id ) {
open(std::move(pfs), expected_chain_id, [&control=this->control]() {
control->startup( [](){}, []() { return false; } );
});
}
void base_tester::push_block(signed_block_ptr b) {
auto bsf = control->create_block_state_future(b->calculate_id(), b);
unapplied_transactions.add_aborted( control->abort_block() );
control->push_block( bsf, [this]( const branch_type& forked_branch ) {
unapplied_transactions.add_forked( forked_branch );
}, [this]( const transaction_id_type& id ) {
return unapplied_transactions.get_trx( id );
} );
auto itr = last_produced_block.find(b->producer);
if (itr == last_produced_block.end() || b->block_num() > block_header::num_from_id(itr->second)) {
last_produced_block[b->producer] = b->calculate_id();
}
}
signed_block_ptr base_tester::_produce_block( fc::microseconds skip_time, bool skip_pending_trxs ) {
std::vector<transaction_trace_ptr> traces;
return _produce_block( skip_time, skip_pending_trxs, false, traces );
}
signed_block_ptr base_tester::_produce_block( fc::microseconds skip_time, bool skip_pending_trxs,
bool no_throw, std::vector<transaction_trace_ptr>& traces ) {
auto head = control->head_block_state();
auto head_time = control->head_block_time();
auto next_time = head_time + skip_time;
if( !control->is_building_block() || control->pending_block_time() != next_time ) {
_start_block( next_time );
}
if( !skip_pending_trxs ) {
for( auto itr = unapplied_transactions.begin(); itr != unapplied_transactions.end(); ) {
auto trace = control->push_transaction( itr->trx_meta, fc::time_point::maximum(), fc::microseconds::maximum(), DEFAULT_BILLED_CPU_TIME_US, true, 0 );
traces.emplace_back( trace );
if(!no_throw && trace->except) {
// this always throws an fc::exception, since the original exception is copied into an fc::exception
trace->except->dynamic_rethrow_exception();
}
itr = unapplied_transactions.erase( itr );
}
}
auto head_block = _finish_block();
_start_block( next_time + fc::microseconds(config::block_interval_us));
return head_block;
}
void base_tester::_start_block(fc::time_point block_time) {
auto head_block_number = control->head_block_num();
auto producer = control->head_block_state()->get_scheduled_producer(block_time);
auto last_produced_block_num = control->last_irreversible_block_num();
auto itr = last_produced_block.find(producer.producer_name);
if (itr != last_produced_block.end()) {
last_produced_block_num = std::max(control->last_irreversible_block_num(), block_header::num_from_id(itr->second));
}
unapplied_transactions.add_aborted( control->abort_block() );
vector<digest_type> feature_to_be_activated;
// First add protocol features to be activated WITHOUT preactivation
feature_to_be_activated.insert(
feature_to_be_activated.end(),
protocol_features_to_be_activated_wo_preactivation.begin(),
protocol_features_to_be_activated_wo_preactivation.end()
);
// Then add protocol features to be activated WITH preactivation
const auto preactivated_protocol_features = control->get_preactivated_protocol_features();
feature_to_be_activated.insert(
feature_to_be_activated.end(),
preactivated_protocol_features.begin(),
preactivated_protocol_features.end()
);
control->start_block( block_time, head_block_number - last_produced_block_num, feature_to_be_activated );
// Clear the list, if start block finishes successfuly, the protocol features should be assumed to be activated
protocol_features_to_be_activated_wo_preactivation.clear();
}
signed_block_ptr base_tester::_finish_block() {
FC_ASSERT( control->is_building_block(), "must first start a block before it can be finished" );
auto producer = control->head_block_state()->get_scheduled_producer( control->pending_block_time() );
vector<private_key_type> signing_keys;
auto default_active_key = get_public_key( producer.producer_name, "active");
producer.for_each_key([&](const public_key_type& key){
const auto& iter = block_signing_private_keys.find(key);
if(iter != block_signing_private_keys.end()) {
signing_keys.push_back(iter->second);
} else if (key == default_active_key) {
signing_keys.emplace_back( get_private_key( producer.producer_name, "active") );
}
});
control->finalize_block([&](block_state_ptr bsp, bool wtmsig_enabled, const digest_type& d) {
std::vector<signature_type> sigs;
sigs.reserve(signing_keys.size());
std::transform(signing_keys.begin(), signing_keys.end(), std::back_inserter(sigs),
[&d](const auto& k) { return k.sign(d); });
bsp->assign_signatures(std::move(sigs), wtmsig_enabled);
}).get()();
last_produced_block[control->head_block_state()->header.producer] =
control->head_block_state()->id;
return control->head_block_state()->block;
}
signed_block_ptr base_tester::produce_block( std::vector<transaction_trace_ptr>& traces ) {
return _produce_block( fc::milliseconds(config::block_interval_ms), false, true, traces );
}
void base_tester::produce_blocks( uint32_t n, bool empty ) {
if( empty ) {
for( uint32_t i = 0; i < n; ++i )
produce_empty_block();
} else {
for( uint32_t i = 0; i < n; ++i )
produce_block();
}
}
void base_tester::produce_blocks_until_end_of_round() {
uint64_t blocks_per_round;
while(true) {
blocks_per_round = control->active_producers().producers.size() * config::producer_repetitions;
produce_block();
if (control->head_block_num() % blocks_per_round == (blocks_per_round - 1)) break;
}
}
void base_tester::produce_blocks_for_n_rounds(const uint32_t num_of_rounds) {
for(uint32_t i = 0; i < num_of_rounds; i++) {
produce_blocks_until_end_of_round();
}
}
void base_tester::produce_min_num_of_blocks_to_spend_time_wo_inactive_prod(const fc::microseconds target_elapsed_time) {
fc::microseconds elapsed_time;
while (elapsed_time < target_elapsed_time) {
for(uint32_t i = 0; i < control->head_block_state()->active_schedule.producers.size(); i++) {
const auto time_to_skip = fc::milliseconds(config::producer_repetitions * config::block_interval_ms);
produce_block(time_to_skip);
elapsed_time += time_to_skip;
}
// if it is more than 24 hours, producer will be marked as inactive
const auto time_to_skip = fc::seconds(23 * 60 * 60);
produce_block(time_to_skip);
elapsed_time += time_to_skip;
}
}
void base_tester::set_transaction_headers( transaction& trx, uint32_t expiration, uint32_t delay_sec ) const {
trx.expiration = control->head_block_time() + fc::seconds(expiration);
trx.set_reference_block( control->head_block_id() );
trx.max_net_usage_words = 0; // No limit
trx.max_cpu_usage_ms = 0; // No limit
trx.delay_sec = delay_sec;
}
transaction_trace_ptr base_tester::create_account( account_name a, account_name creator, bool multisig, bool include_code ) {
signed_transaction trx;
set_transaction_headers(trx);
authority owner_auth;
if( multisig ) {
// multisig between account's owner key and creators active permission
owner_auth = authority(2, {key_weight{get_public_key( a, "owner" ), 1}}, {permission_level_weight{{creator, config::active_name}, 1}});
} else {
owner_auth = authority( get_public_key( a, "owner" ) );
}
authority active_auth( get_public_key( a, "active" ) );
auto sort_permissions = []( authority& auth ) {
std::sort( auth.accounts.begin(), auth.accounts.end(),
[]( const permission_level_weight& lhs, const permission_level_weight& rhs ) {
return lhs.permission < rhs.permission;
}
);
};
if( include_code ) {
FC_ASSERT( owner_auth.threshold <= std::numeric_limits<weight_type>::max(), "threshold is too high" );
FC_ASSERT( active_auth.threshold <= std::numeric_limits<weight_type>::max(), "threshold is too high" );
owner_auth.accounts.push_back( permission_level_weight{ {a, config::eosio_code_name},
static_cast<weight_type>(owner_auth.threshold) } );
sort_permissions(owner_auth);
active_auth.accounts.push_back( permission_level_weight{ {a, config::eosio_code_name},
static_cast<weight_type>(active_auth.threshold) } );
sort_permissions(active_auth);
}
trx.actions.emplace_back( vector<permission_level>{{creator,config::active_name}},
newaccount{
.creator = creator,
.name = a,
.owner = owner_auth,
.active = active_auth,
});
set_transaction_headers(trx);
trx.sign( get_private_key( creator, "active" ), control->get_chain_id() );
return push_transaction( trx );
}
transaction_trace_ptr base_tester::push_transaction( const packed_transaction& trx,
fc::time_point deadline,
uint32_t billed_cpu_time_us
)
{ try {
if( !control->is_building_block() )
_start_block(control->head_block_time() + fc::microseconds(config::block_interval_us));
auto ptrx = std::make_shared<packed_transaction>(trx);
auto time_limit = deadline == fc::time_point::maximum() ?
fc::microseconds::maximum() :
fc::microseconds( deadline - fc::time_point::now() );
auto fut = transaction_metadata::start_recover_keys( ptrx, control->get_thread_pool(), control->get_chain_id(), time_limit, transaction_metadata::trx_type::input );
auto r = control->push_transaction( fut.get(), deadline, fc::microseconds::maximum(), billed_cpu_time_us, billed_cpu_time_us > 0, 0 );
if( r->except_ptr ) std::rethrow_exception( r->except_ptr );
if( r->except ) throw *r->except;
return r;
} FC_RETHROW_EXCEPTIONS( warn, "transaction_header: {header}", ("header", transaction_header(trx.get_transaction()) )) }
transaction_trace_ptr base_tester::push_transaction( const signed_transaction& trx,
fc::time_point deadline,
uint32_t billed_cpu_time_us,
bool no_throw
)
{ try {
if( !control->is_building_block() )
_start_block(control->head_block_time() + fc::microseconds(config::block_interval_us));
auto c = packed_transaction::compression_type::none;
if( fc::raw::pack_size(trx) > 1000 ) {
c = packed_transaction::compression_type::zlib;
}
auto time_limit = deadline == fc::time_point::maximum() ?
fc::microseconds::maximum() :
fc::microseconds( deadline - fc::time_point::now() );
auto ptrx = std::make_shared<packed_transaction>( signed_transaction(trx), true, c );
auto fut = transaction_metadata::start_recover_keys( std::move( ptrx ), control->get_thread_pool(), control->get_chain_id(), time_limit, transaction_metadata::trx_type::input );
auto r = control->push_transaction( fut.get(), deadline, fc::microseconds::maximum(), billed_cpu_time_us, billed_cpu_time_us > 0, 0 );
if (no_throw) return r;
if( r->except_ptr ) std::rethrow_exception( r->except_ptr );
if( r->except) throw *r->except;
return r;
} FC_RETHROW_EXCEPTIONS( warn, "transaction_header: {header}, billed_cpu_time_us: {billed}",
("header", transaction_header(trx) ) ("billed", billed_cpu_time_us))
}
typename base_tester::action_result base_tester::push_action(action&& act, uint64_t authorizer) {
signed_transaction trx;
if (authorizer) {
act.authorization = vector<permission_level>{{account_name(authorizer), config::active_name}};
}
trx.actions.emplace_back(std::move(act));
set_transaction_headers(trx);
if (authorizer) {
trx.sign(get_private_key(account_name(authorizer), "active"), control->get_chain_id());
}
try {
push_transaction(trx);
} catch (const fc::exception& ex) {
edump((ex.to_detail_string()));
return error(ex.top_message()); // top_message() is assumed by many tests; otherwise they fail
//return error(ex.to_detail_string());
}
produce_block();
BOOST_REQUIRE_EQUAL(true, chain_has_transaction(trx.id()));
return success();
}
transaction_trace_ptr base_tester::push_action( const account_name& code,
const action_name& acttype,
const account_name& actor,
const variant_object& data,
uint32_t expiration,
uint32_t delay_sec
)
{
vector<permission_level> auths;
auths.push_back( permission_level{actor, config::active_name} );
return push_action( code, acttype, auths, data, expiration, delay_sec );
}
transaction_trace_ptr base_tester::push_action( const account_name& code,
const action_name& acttype,
const vector<account_name>& actors,
const variant_object& data,
uint32_t expiration,
uint32_t delay_sec
)
{
vector<permission_level> auths;
for (const auto& actor : actors) {
auths.push_back( permission_level{actor, config::active_name} );
}
return push_action( code, acttype, auths, data, expiration, delay_sec );
}
transaction_trace_ptr base_tester::push_action( const account_name& code,
const action_name& acttype,
const vector<permission_level>& auths,
const variant_object& data,
uint32_t expiration,
uint32_t delay_sec
)
{ try {
signed_transaction trx;
trx.actions.emplace_back( get_action( code, acttype, auths, data ) );
set_transaction_headers( trx, expiration, delay_sec );
for (const auto& auth : auths) {
trx.sign( get_private_key( auth.actor, auth.permission.to_string() ), control->get_chain_id() );
}
return push_transaction( trx );
} FC_CAPTURE_AND_RETHROW( (code)(acttype)(auths)(fc::json::to_string(data, fc::time_point::now() + fc::exception::format_time_limit))(expiration)(delay_sec) ) } // ?
action base_tester::get_action( account_name code, action_name acttype, vector<permission_level> auths,
const variant_object& data )const { try {
const auto& acnt = control->get_account(code);
auto abi = acnt.get_abi();
chain::abi_serializer abis(abi, abi_serializer::create_yield_function( abi_serializer_max_time ));
string action_type_name = abis.get_action_type(acttype);
FC_ASSERT( action_type_name != string(), "unknown action type {a}", ("a",acttype) );
action act;
act.account = code;
act.name = acttype;
act.authorization = auths;
act.data = abis.variant_to_binary(action_type_name, data, abi_serializer::create_yield_function( abi_serializer_max_time ));
return act;
} FC_CAPTURE_AND_RETHROW() }
transaction_trace_ptr base_tester::push_reqauth( account_name from, const vector<permission_level>& auths, const vector<private_key_type>& keys ) {
fc::variant pretty_trx = fc::mutable_variant_object()
("actions", fc::variants({
fc::mutable_variant_object()
("account", name(config::system_account_name))
("name", "reqauth")
("authorization", auths)
("data", fc::mutable_variant_object()
("from", from)
)
})
);
signed_transaction trx;
abi_serializer::from_variant(pretty_trx, trx, get_resolver(), abi_serializer::create_yield_function( abi_serializer_max_time ));
set_transaction_headers(trx);
for(auto iter = keys.begin(); iter != keys.end(); iter++)
trx.sign( *iter, control->get_chain_id() );
return push_transaction( trx );
}
transaction_trace_ptr base_tester::push_reqauth(account_name from, string role, bool multi_sig) {
if (!multi_sig) {
return push_reqauth(from, vector<permission_level>{{from, config::owner_name}},
{get_private_key(from, role)});
} else {
return push_reqauth(from, vector<permission_level>{{from, config::owner_name}},
{get_private_key(from, role), get_private_key( config::system_account_name, "active" )} );
}
}
transaction_trace_ptr base_tester::push_dummy(account_name from, const string& v, uint32_t billed_cpu_time_us) {
// use reqauth for a normal action, this could be anything
fc::variant pretty_trx = fc::mutable_variant_object()
("actions", fc::variants({
fc::mutable_variant_object()
("account", name(config::system_account_name))
("name", "reqauth")
("authorization", fc::variants({
fc::mutable_variant_object()
("actor", from)
("permission", name(config::active_name))
}))
("data", fc::mutable_variant_object()
("from", from)
)
})
)
// lets also push a context free action, the multi chain test will then also include a context free action
("context_free_actions", fc::variants({
fc::mutable_variant_object()
("account", name(config::null_account_name))
("name", "nonce")
("data", fc::raw::pack(v))
})
);
signed_transaction trx;
abi_serializer::from_variant(pretty_trx, trx, get_resolver(), abi_serializer::create_yield_function( abi_serializer_max_time ));
set_transaction_headers(trx);
trx.sign( get_private_key( from, "active" ), control->get_chain_id() );
return push_transaction( trx, fc::time_point::maximum(), billed_cpu_time_us );
}
transaction_trace_ptr base_tester::transfer( account_name from, account_name to, string amount, string memo, account_name currency ) {
return transfer( from, to, asset::from_string(amount), memo, currency );
}
transaction_trace_ptr base_tester::transfer( account_name from, account_name to, asset amount, string memo, account_name currency ) {
fc::variant pretty_trx = fc::mutable_variant_object()
("actions", fc::variants({
fc::mutable_variant_object()
("account", currency)
("name", "transfer")
("authorization", fc::variants({
fc::mutable_variant_object()
("actor", from)
("permission", name(config::active_name))
}))
("data", fc::mutable_variant_object()
("from", from)
("to", to)
("quantity", amount)
("memo", memo)
)
})
);
signed_transaction trx;
abi_serializer::from_variant(pretty_trx, trx, get_resolver(), abi_serializer::create_yield_function( abi_serializer_max_time ));
set_transaction_headers(trx);
trx.sign( get_private_key( from, name(config::active_name).to_string() ), control->get_chain_id() );
return push_transaction( trx );
}
transaction_trace_ptr base_tester::issue( account_name to, string amount, account_name currency, string memo ) {
fc::variant pretty_trx = fc::mutable_variant_object()
("actions", fc::variants({
fc::mutable_variant_object()
("account", currency)
("name", "issue")
("authorization", fc::variants({
fc::mutable_variant_object()
("actor", currency )
("permission", name(config::active_name))
}))
("data", fc::mutable_variant_object()
("to", to)
("quantity", amount)
("memo", memo)
)
})
);
signed_transaction trx;
abi_serializer::from_variant(pretty_trx, trx, get_resolver(), abi_serializer::create_yield_function( abi_serializer_max_time ));
set_transaction_headers(trx);
trx.sign( get_private_key( currency, name(config::active_name).to_string() ), control->get_chain_id() );
return push_transaction( trx );
}
void base_tester::link_authority( account_name account, account_name code, permission_name req, action_name type ) {
signed_transaction trx;
trx.actions.emplace_back( vector<permission_level>{{account, config::active_name}},
linkauth(account, code, type, req));
set_transaction_headers(trx);
trx.sign( get_private_key( account, "active" ), control->get_chain_id() );
push_transaction( trx );
}
void base_tester::unlink_authority( account_name account, account_name code, action_name type ) {
signed_transaction trx;
trx.actions.emplace_back( vector<permission_level>{{account, config::active_name}},
unlinkauth(account, code, type ));
set_transaction_headers(trx);
trx.sign( get_private_key( account, "active" ), control->get_chain_id() );
push_transaction( trx );
}
void base_tester::set_authority( account_name account,
permission_name perm,
authority auth,
permission_name parent,
const vector<permission_level>& auths,
const vector<private_key_type>& keys) { try {
signed_transaction trx;
trx.actions.emplace_back( auths,
updateauth{
.account = account,
.permission = perm,
.parent = parent,
.auth = std::move(auth),
});
set_transaction_headers(trx);
for (const auto& key: keys) {
trx.sign( key, control->get_chain_id() );
}
push_transaction( trx );
} FC_CAPTURE_AND_RETHROW( (account)(perm)(auth)(parent) ) }
void base_tester::set_authority( account_name account,
permission_name perm,
authority auth,
permission_name parent) {
set_authority(account, perm, auth, parent, { { account, config::owner_name } }, { get_private_key( account, "owner" ) });
}
void base_tester::delete_authority( account_name account,
permission_name perm,
const vector<permission_level>& auths,
const vector<private_key_type>& keys ) { try {
signed_transaction trx;
trx.actions.emplace_back( auths,
deleteauth(account, perm) );
set_transaction_headers(trx);
for (const auto& key: keys) {
trx.sign( key, control->get_chain_id() );
}
push_transaction( trx );
} FC_CAPTURE_AND_RETHROW( (account)(perm) ) }
void base_tester::delete_authority( account_name account,
permission_name perm ) {
delete_authority(account, perm, { permission_level{ account, config::owner_name } }, { get_private_key( account, "owner" ) });
}
void base_tester::set_code( account_name account, const char* wast, const private_key_type* signer ) try {
set_code(account, wast_to_wasm(wast), signer);
} FC_CAPTURE_AND_RETHROW( (account) )
void base_tester::set_code( account_name account, const vector<uint8_t> wasm, const private_key_type* signer ) try {
signed_transaction trx;
trx.actions.emplace_back( vector<permission_level>{{account,config::active_name}},
setcode{
.account = account,
.vmtype = 0,
.vmversion = 0,
.code = bytes(wasm.begin(), wasm.end())
});
set_transaction_headers(trx);
if( signer ) {
trx.sign( *signer, control->get_chain_id() );
} else {
trx.sign( get_private_key( account, "active" ), control->get_chain_id() );
}
push_transaction( trx );
} FC_CAPTURE_AND_RETHROW( (account) )
void base_tester::set_abi( account_name account, const char* abi_json, const private_key_type* signer ) {
auto abi = fc::json::from_string(abi_json).template as<abi_def>();
signed_transaction trx;
trx.actions.emplace_back( vector<permission_level>{{account,config::active_name}},
setabi{
.account = account,
.abi = fc::raw::pack(abi)
});
set_transaction_headers(trx);
if( signer ) {
trx.sign( *signer, control->get_chain_id() );
} else {
trx.sign( get_private_key( account, "active" ), control->get_chain_id() );
}
push_transaction( trx );
}
bool base_tester::chain_has_transaction( const transaction_id_type& txid ) const {
return chain_transactions.count(txid) != 0;
}
const transaction_receipt& base_tester::get_transaction_receipt( const transaction_id_type& txid ) const {
return chain_transactions.at(txid);
}
/**
* Reads balance as stored by generic_currency contract
*/
asset base_tester::get_currency_balance( const account_name& code,
const symbol& asset_symbol,
const account_name& account ) const {
const auto& db = control->db();
const auto* tbl = db.template find<table_id_object, by_code_scope_table>(boost::make_tuple(code, account, "accounts"_n));
share_type result = 0;
// the balance is implied to be 0 if either the table or row does not exist
if (tbl) {
const auto *obj = db.template find<key_value_object, by_scope_primary>(boost::make_tuple(tbl->id, asset_symbol.to_symbol_code().value));
if (obj) {
//balance is the first field in the serialization
fc::datastream<const char *> ds(obj->value.data(), obj->value.size());
fc::raw::unpack(ds, result);
}
}
return asset(result, asset_symbol);
}
vector<char> base_tester::get_row_by_account( name code, name scope, name table, const account_name& act ) const {
vector<char> data;
const auto& db = control->db();
const auto* t_id = db.find<chain::table_id_object, chain::by_code_scope_table>( boost::make_tuple( code, scope, table ) );
if ( !t_id ) {
return data;
}
//FC_ASSERT( t_id != 0, "object not found" );
const auto& idx = db.get_index<chain::key_value_index, chain::by_scope_primary>();
auto itr = idx.lower_bound( boost::make_tuple( t_id->id, act.to_uint64_t() ) );
if ( itr == idx.end() || itr->t_id != t_id->id || act.to_uint64_t() != itr->primary_key ) {
return data;
}
data.resize( itr->value.size() );
memcpy( data.data(), itr->value.data(), data.size() );
return data;
}
vector<uint8_t> base_tester::to_uint8_vector(const string& s) {
vector<uint8_t> v(s.size());
copy(s.begin(), s.end(), v.begin());
return v;
};
vector<uint8_t> base_tester::to_uint8_vector(uint64_t x) {
vector<uint8_t> v(sizeof(x));
*reinterpret_cast<uint64_t*>(v.data()) = x;
return v;
};