-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathprotocol_feature_tests.cpp
2274 lines (1816 loc) · 96.2 KB
/
protocol_feature_tests.cpp
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 <eosio/chain/abi_serializer.hpp>
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/generated_transaction_object.hpp>
#include <eosio/testing/tester.hpp>
#include <fc/variant_object.hpp>
#include <boost/test/unit_test.hpp>
#include <contracts.hpp>
#include <test_contracts.hpp>
#include "fork_test_utilities.hpp"
using namespace eosio::chain;
using namespace eosio::testing;
using namespace std::literals;
BOOST_AUTO_TEST_SUITE(protocol_feature_tests)
BOOST_AUTO_TEST_CASE( activate_preactivate_feature ) try {
tester c( setup_policy::none );
const auto& pfm = c.control->get_protocol_feature_manager();
c.produce_block();
// Cannot set latest bios contract since it requires intrinsics that have not yet been whitelisted.
BOOST_CHECK_EXCEPTION( c.set_code( config::system_account_name, contracts::eosio_bios_wasm() ),
wasm_exception, fc_exception_message_is("env.set_proposed_producers_ex unresolveable")
);
// But the old bios contract can still be set.
c.set_code( config::system_account_name, contracts::before_preactivate_eosio_bios_wasm() );
c.set_abi( config::system_account_name, contracts::before_preactivate_eosio_bios_abi() );
auto t = c.control->pending_block_time();
c.control->abort_block();
BOOST_REQUIRE_EXCEPTION( c.control->start_block( t, 0, {digest_type()}, controller::block_status::incomplete ), protocol_feature_exception,
fc_exception_message_is( "protocol feature with digest '0000000000000000000000000000000000000000000000000000000000000000' is unrecognized" )
);
auto d = pfm.get_builtin_digest( builtin_protocol_feature_t::preactivate_feature );
BOOST_REQUIRE( d );
// Activate PREACTIVATE_FEATURE.
c.schedule_protocol_features_wo_preactivation({ *d });
c.produce_block();
// Now the latest bios contract can be set.
c.set_before_producer_authority_bios_contract();
c.produce_block();
BOOST_CHECK_EXCEPTION( c.push_action( config::system_account_name, "reqactivated"_n, config::system_account_name,
mutable_variant_object()("feature_digest", digest_type()) ),
eosio_assert_message_exception,
eosio_assert_message_is( "protocol feature is not activated" )
);
c.push_action( config::system_account_name, "reqactivated"_n, config::system_account_name, mutable_variant_object()
("feature_digest", *d )
);
c.produce_block();
// Ensure validator node accepts the blockchain
tester c2(setup_policy::none, db_read_mode::HEAD);
push_blocks( c, c2 );
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( activate_and_restart ) try {
tester c( setup_policy::none );
const auto& pfm = c.control->get_protocol_feature_manager();
auto pfs = pfm.get_protocol_feature_set(); // make copy of protocol feature set
auto d = pfm.get_builtin_digest( builtin_protocol_feature_t::preactivate_feature );
BOOST_REQUIRE( d );
BOOST_CHECK( !c.control->is_builtin_activated( builtin_protocol_feature_t::preactivate_feature ) );
// Activate PREACTIVATE_FEATURE.
c.schedule_protocol_features_wo_preactivation({ *d });
c.produce_blocks(2);
auto head_block_num = c.control->head_block_num();
BOOST_CHECK( c.control->is_builtin_activated( builtin_protocol_feature_t::preactivate_feature ) );
c.close();
c.open( std::move( pfs ) );
BOOST_CHECK_EQUAL( head_block_num, c.control->head_block_num() );
BOOST_CHECK( c.control->is_builtin_activated( builtin_protocol_feature_t::preactivate_feature ) );
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( double_preactivation ) try {
tester c( setup_policy::preactivate_feature_and_new_bios );
const auto& pfm = c.control->get_protocol_feature_manager();
auto d = pfm.get_builtin_digest( builtin_protocol_feature_t::only_link_to_existing_permission );
BOOST_REQUIRE( d );
c.push_action( config::system_account_name, "activate"_n, config::system_account_name,
fc::mutable_variant_object()("feature_digest", *d), 10 );
std::string expected_error_msg("protocol feature with digest '");
{
fc::variant v;
to_variant( *d, v );
expected_error_msg += v.get_string();
expected_error_msg += "' is already pre-activated";
}
BOOST_CHECK_EXCEPTION( c.push_action( config::system_account_name, "activate"_n, config::system_account_name,
fc::mutable_variant_object()("feature_digest", *d), 20 ),
protocol_feature_exception,
fc_exception_message_is( expected_error_msg )
);
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( double_activation ) try {
tester c( setup_policy::preactivate_feature_and_new_bios );
const auto& pfm = c.control->get_protocol_feature_manager();
auto d = pfm.get_builtin_digest( builtin_protocol_feature_t::only_link_to_existing_permission );
BOOST_REQUIRE( d );
BOOST_CHECK( !c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
c.preactivate_protocol_features( {*d} );
BOOST_CHECK( !c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
c.schedule_protocol_features_wo_preactivation( {*d} );
BOOST_CHECK_EXCEPTION( c.produce_block();,
block_validate_exception,
fc_exception_message_starts_with( "attempted duplicate activation within a single block:" )
);
c.protocol_features_to_be_activated_wo_preactivation.clear();
BOOST_CHECK( !c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
c.produce_block();
BOOST_CHECK( c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
c.produce_block();
BOOST_CHECK( c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( require_preactivation_test ) try {
tester c( setup_policy::preactivate_feature_and_new_bios );
const auto& pfm = c.control->get_protocol_feature_manager();
auto d = pfm.get_builtin_digest( builtin_protocol_feature_t::only_link_to_existing_permission );
BOOST_REQUIRE( d );
BOOST_CHECK( !c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
c.schedule_protocol_features_wo_preactivation( {*d} );
BOOST_CHECK_EXCEPTION( c.produce_block(),
protocol_feature_exception,
fc_exception_message_starts_with( "attempted to activate protocol feature without prior required preactivation:" )
);
c.protocol_features_to_be_activated_wo_preactivation.clear();
BOOST_CHECK( !c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
c.preactivate_protocol_features( {*d} );
c.finish_block();
BOOST_CHECK( !c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
BOOST_CHECK_EXCEPTION( c.control->start_block(
c.control->head_block_time() + fc::milliseconds(config::block_interval_ms),
0,
{},
controller::block_status::incomplete
),
block_validate_exception,
fc_exception_message_is( "There are pre-activated protocol features that were not activated at the start of this block" )
);
BOOST_CHECK( !c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
c.produce_block();
BOOST_CHECK( c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( only_link_to_existing_permission_test ) try {
tester c( setup_policy::preactivate_feature_and_new_bios );
const auto& pfm = c.control->get_protocol_feature_manager();
auto d = pfm.get_builtin_digest( builtin_protocol_feature_t::only_link_to_existing_permission );
BOOST_REQUIRE( d );
c.create_accounts( {"alice"_n, "bob"_n, "charlie"_n} );
BOOST_CHECK_EXCEPTION( c.push_action( config::system_account_name, "linkauth"_n, "bob"_n, fc::mutable_variant_object()
("account", "bob")
("code", name(config::system_account_name))
("type", "")
("requirement", "test" )
), permission_query_exception,
fc_exception_message_is( "Failed to retrieve permission: test" )
);
BOOST_CHECK_EXCEPTION( c.push_action( config::system_account_name, "linkauth"_n, "charlie"_n, fc::mutable_variant_object()
("account", "charlie")
("code", name(config::system_account_name))
("type", "")
("requirement", "test" )
), permission_query_exception,
fc_exception_message_is( "Failed to retrieve permission: test" )
);
c.push_action( config::system_account_name, "updateauth"_n, "alice"_n, fc::mutable_variant_object()
("account", "alice")
("permission", "test")
("parent", "active")
("auth", authority(get_public_key(name("testapi"), "test")))
);
c.produce_block();
// Verify the incorrect behavior prior to ONLY_LINK_TO_EXISTING_PERMISSION activation.
c.push_action( config::system_account_name, "linkauth"_n, "bob"_n, fc::mutable_variant_object()
("account", "bob")
("code", name(config::system_account_name))
("type", "")
("requirement", "test" )
);
c.preactivate_protocol_features( {*d} );
c.produce_block();
// Verify the correct behavior after ONLY_LINK_TO_EXISTING_PERMISSION activation.
BOOST_CHECK_EXCEPTION( c.push_action( config::system_account_name, "linkauth"_n, "charlie"_n, fc::mutable_variant_object()
("account", "charlie")
("code", name(config::system_account_name))
("type", "")
("requirement", "test" )
), permission_query_exception,
fc_exception_message_is( "Failed to retrieve permission: test" )
);
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( subjective_restrictions_test ) try {
tester c( setup_policy::none );
const auto& pfm = c.control->get_protocol_feature_manager();
auto restart_with_new_pfs = [&c]( protocol_feature_set&& pfs ) {
c.close();
c.open(std::move(pfs));
};
auto get_builtin_digest = [&pfm]( builtin_protocol_feature_t codename ) -> digest_type {
auto res = pfm.get_builtin_digest( codename );
BOOST_REQUIRE( res );
return *res;
};
auto preactivate_feature_digest = get_builtin_digest( builtin_protocol_feature_t::preactivate_feature );
auto only_link_to_existing_permission_digest = get_builtin_digest( builtin_protocol_feature_t::only_link_to_existing_permission );
auto invalid_act_time = fc::time_point::from_iso_string( "2200-01-01T00:00:00" );
auto valid_act_time = fc::time_point{};
// First, test subjective_restrictions on feature that can be activated WITHOUT preactivation (PREACTIVATE_FEATURE)
c.schedule_protocol_features_wo_preactivation({ preactivate_feature_digest });
// schedule PREACTIVATE_FEATURE activation (persists until next successful start_block)
subjective_restriction_map custom_subjective_restrictions = {
{ builtin_protocol_feature_t::preactivate_feature, {invalid_act_time, false, true} }
};
restart_with_new_pfs( make_protocol_feature_set(custom_subjective_restrictions) );
// When a block is produced, the protocol feature activation should fail and throw an error
BOOST_CHECK_EXCEPTION( c.produce_block(),
protocol_feature_exception,
fc_exception_message_starts_with(
c.control->head_block_time().to_iso_string() +
" is too early for the earliest allowed activation time of the protocol feature"
)
);
BOOST_CHECK_EQUAL( c.protocol_features_to_be_activated_wo_preactivation.size(), 1u );
// Revert to the valid earliest allowed activation time, however with enabled == false
custom_subjective_restrictions = {
{ builtin_protocol_feature_t::preactivate_feature, {valid_act_time, false, false} }
};
restart_with_new_pfs( make_protocol_feature_set(custom_subjective_restrictions) );
// This should also fail, but with different exception
BOOST_CHECK_EXCEPTION( c.produce_block(),
protocol_feature_exception,
fc_exception_message_is(
std::string("protocol feature with digest '") +
std::string(preactivate_feature_digest) +
"' is disabled"
)
);
BOOST_CHECK_EQUAL( c.protocol_features_to_be_activated_wo_preactivation.size(), 1u );
// Revert to the valid earliest allowed activation time, however with subjective_restrictions enabled == true
custom_subjective_restrictions = {
{ builtin_protocol_feature_t::preactivate_feature, {valid_act_time, false, true} }
};
restart_with_new_pfs( make_protocol_feature_set(custom_subjective_restrictions) );
// Now it should be fine, the feature should be activated after the block is produced
BOOST_CHECK_NO_THROW( c.produce_block() );
BOOST_CHECK( c.control->is_builtin_activated( builtin_protocol_feature_t::preactivate_feature ) );
BOOST_CHECK_EQUAL( c.protocol_features_to_be_activated_wo_preactivation.size(), 0u );
// Second, test subjective_restrictions on feature that need to be activated WITH preactivation (ONLY_LINK_TO_EXISTING_PERMISSION)
c.set_before_producer_authority_bios_contract();
c.produce_block();
custom_subjective_restrictions = {
{ builtin_protocol_feature_t::only_link_to_existing_permission, {invalid_act_time, true, true} }
};
restart_with_new_pfs( make_protocol_feature_set(custom_subjective_restrictions) );
// It should fail
BOOST_CHECK_EXCEPTION( c.preactivate_protocol_features({only_link_to_existing_permission_digest}),
subjective_block_production_exception,
fc_exception_message_starts_with(
(c.control->head_block_time() + fc::milliseconds(config::block_interval_ms)).to_iso_string() +
" is too early for the earliest allowed activation time of the protocol feature"
)
);
// Revert with valid time and subjective_restrictions enabled == false
custom_subjective_restrictions = {
{ builtin_protocol_feature_t::only_link_to_existing_permission, {valid_act_time, true, false} }
};
restart_with_new_pfs( make_protocol_feature_set(custom_subjective_restrictions) );
// It should fail but with different exception
BOOST_CHECK_EXCEPTION( c.preactivate_protocol_features({only_link_to_existing_permission_digest}),
subjective_block_production_exception,
fc_exception_message_is(
std::string("protocol feature with digest '") +
std::string(only_link_to_existing_permission_digest)+
"' is disabled"
)
);
// Revert with valid time and subjective_restrictions enabled == true
custom_subjective_restrictions = {
{ builtin_protocol_feature_t::only_link_to_existing_permission, {valid_act_time, true, true} }
};
restart_with_new_pfs( make_protocol_feature_set(custom_subjective_restrictions) );
// Should be fine now, and activated in the next block
BOOST_CHECK_NO_THROW( c.preactivate_protocol_features({only_link_to_existing_permission_digest}) );
c.produce_block();
BOOST_CHECK( c.control->is_builtin_activated( builtin_protocol_feature_t::only_link_to_existing_permission ) );
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( replace_deferred_test ) try {
tester c( setup_policy::preactivate_feature_and_new_bios );
c.preactivate_builtin_protocol_features( {builtin_protocol_feature_t::crypto_primitives} );
c.produce_block();
c.create_accounts( {"alice"_n, "bob"_n, "test"_n} );
c.set_code( "test"_n, test_contracts::deferred_test_wasm() );
c.set_abi( "test"_n, test_contracts::deferred_test_abi() );
c.produce_block();
auto alice_ram_usage0 = c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n );
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 42)
("contract", "test")
("payload", 100)
);
auto alice_ram_usage1 = c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n );
// Verify subjective mitigation is in place
BOOST_CHECK_EXCEPTION(
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 42)
("contract", "test")
("payload", 101 )
),
subjective_block_production_exception,
fc_exception_message_is( "Replacing a deferred transaction is temporarily disabled." )
);
BOOST_CHECK_EQUAL( c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n ), alice_ram_usage1 );
c.control->abort_block();
c.close();
auto cfg = c.get_config();
cfg.disable_all_subjective_mitigations = true;
c.init( cfg );
transaction_trace_ptr trace;
auto h = c.control->applied_transaction.connect( [&](std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&> x) {
auto& t = std::get<0>(x);
if( t && !eosio::chain::is_onblock(*t)) {
trace = t;
}
} );
BOOST_CHECK_EQUAL( c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n ), alice_ram_usage0 );
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 42)
("contract", "test")
("payload", 100)
);
BOOST_CHECK_EQUAL( c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n ), alice_ram_usage1 );
auto dtrxs = c.get_scheduled_transactions();
BOOST_CHECK_EQUAL( dtrxs.size(), 1u );
auto first_dtrx_id = dtrxs[0];
// With the subjective mitigation disabled, replacing the deferred transaction is allowed.
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 42)
("contract", "test")
("payload", 101)
);
auto alice_ram_usage2 = c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n );
BOOST_CHECK_EQUAL( alice_ram_usage2, alice_ram_usage1 + (alice_ram_usage1 - alice_ram_usage0) );
dtrxs = c.get_scheduled_transactions();
BOOST_CHECK_EQUAL( dtrxs.size(), 1u );
BOOST_CHECK_EQUAL( first_dtrx_id, dtrxs[0] ); // Incorrectly kept as the old transaction ID.
c.produce_block();
auto alice_ram_usage3 = c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n );
BOOST_CHECK_EQUAL( alice_ram_usage3, alice_ram_usage1 );
dtrxs = c.get_scheduled_transactions();
BOOST_CHECK_EQUAL( dtrxs.size(), 0u );
// must be equal before builtin_protocol_feature_t::replace_deferred to support replay of blocks before activation
BOOST_CHECK( first_dtrx_id.str() == trace->id.str() );
c.produce_block();
c.close();
cfg.disable_all_subjective_mitigations = false;
c.init( cfg );
const auto& pfm = c.control->get_protocol_feature_manager();
auto d = pfm.get_builtin_digest( builtin_protocol_feature_t::replace_deferred );
BOOST_REQUIRE( d );
c.preactivate_protocol_features( {*d} );
c.produce_block();
BOOST_CHECK_EQUAL( c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n ), alice_ram_usage0 );
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 42)
("contract", "test")
("payload", 100)
);
BOOST_CHECK_EQUAL( c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n ), alice_ram_usage1 );
dtrxs = c.get_scheduled_transactions();
BOOST_CHECK_EQUAL( dtrxs.size(), 1u );
auto first_dtrx_id2 = dtrxs[0];
// With REPLACE_DEFERRED activated, replacing the deferred transaction is allowed and now should work properly.
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 42)
("contract", "test")
("payload", 101)
);
BOOST_CHECK_EQUAL( c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n ), alice_ram_usage1 );
dtrxs = c.get_scheduled_transactions();
BOOST_CHECK_EQUAL( dtrxs.size(), 1u );
BOOST_CHECK( first_dtrx_id2 != dtrxs[0] );
// Replace again with a deferred transaction identical to the first one
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 42)
("contract", "test")
("payload", 100),
100 // Needed to make this input transaction unique
);
BOOST_CHECK_EQUAL( c.control->get_resource_limits_manager().get_account_ram_usage( "alice"_n ), alice_ram_usage1 );
dtrxs = c.get_scheduled_transactions();
BOOST_CHECK_EQUAL( dtrxs.size(), 1u );
BOOST_CHECK_EQUAL( first_dtrx_id2, dtrxs[0] );
c.produce_block();
dtrxs = c.get_scheduled_transactions();
BOOST_CHECK_EQUAL( dtrxs.size(), 0u );
// Not equal after builtin_protocol_feature_t::replace_deferred activated
BOOST_CHECK( first_dtrx_id2.str() != trace->id.str() );
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( no_duplicate_deferred_id_test ) try {
tester c( setup_policy::preactivate_feature_and_new_bios );
tester c2( setup_policy::none );
c.preactivate_builtin_protocol_features( {builtin_protocol_feature_t::crypto_primitives} );
c.produce_block();
c.create_accounts( {"alice"_n, "test"_n} );
c.set_code( "test"_n, test_contracts::deferred_test_wasm() );
c.set_abi( "test"_n, test_contracts::deferred_test_abi() );
c.produce_block();
push_blocks( c, c2 );
c2.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 1)
("contract", "test")
("payload", 50)
);
c2.finish_block();
BOOST_CHECK_EXCEPTION(
c2.produce_block(),
fc::exception,
fc_exception_message_is( "no transaction extensions supported yet for deferred transactions" )
);
c2.produce_empty_block( fc::minutes(10) );
transaction_trace_ptr trace0;
auto h2 = c2.control->applied_transaction.connect( [&](std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&> x) {
auto& t = std::get<0>(x);
if( t && t->receipt && t->receipt->status == transaction_receipt::expired) {
trace0 = t;
}
} );
c2.produce_block();
h2.disconnect();
BOOST_REQUIRE( trace0 );
c.produce_block();
const auto& index = c.control->db().get_index<generated_transaction_multi_index,by_trx_id>();
transaction_trace_ptr trace1;
auto h = c.control->applied_transaction.connect( [&](std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&> x) {
auto& t = std::get<0>(x);
if( t && t->receipt && t->receipt->status == transaction_receipt::executed) {
trace1 = t;
}
} );
BOOST_REQUIRE_EQUAL(0u, index.size());
BOOST_REQUIRE_EQUAL(0u, index.size());
const auto& pfm = c.control->get_protocol_feature_manager();
auto d1 = pfm.get_builtin_digest( builtin_protocol_feature_t::replace_deferred );
BOOST_REQUIRE( d1 );
auto d2 = pfm.get_builtin_digest( builtin_protocol_feature_t::no_duplicate_deferred_id );
BOOST_REQUIRE( d2 );
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 1)
("contract", "test")
("payload", 42)
);
BOOST_REQUIRE_EQUAL(1u, index.size());
c.preactivate_protocol_features( {*d1, *d2} );
c.produce_block();
// The deferred transaction with payload 42 that was scheduled prior to the activation of the protocol features should now be retired.
BOOST_REQUIRE( trace1 );
BOOST_REQUIRE_EQUAL(0u, index.size());
trace1 = nullptr;
// Retire the delayed eosio::reqauth transaction.
c.produce_blocks(5);
BOOST_REQUIRE( trace1 );
BOOST_REQUIRE_EQUAL(0u, index.size());
h.disconnect();
auto check_generation_context = []( auto&& data,
const transaction_id_type& sender_trx_id,
unsigned __int128 sender_id,
account_name sender )
{
transaction trx;
fc::datastream<const char*> ds1( data.data(), data.size() );
fc::raw::unpack( ds1, trx );
BOOST_REQUIRE_EQUAL( trx.transaction_extensions.size(), 1u );
BOOST_REQUIRE_EQUAL( trx.transaction_extensions.back().first, 0u );
fc::datastream<const char*> ds2( trx.transaction_extensions.back().second.data(),
trx.transaction_extensions.back().second.size() );
transaction_id_type actual_sender_trx_id;
fc::raw::unpack( ds2, actual_sender_trx_id );
BOOST_CHECK_EQUAL( actual_sender_trx_id, sender_trx_id );
unsigned __int128 actual_sender_id;
fc::raw::unpack( ds2, actual_sender_id );
BOOST_CHECK( actual_sender_id == sender_id );
uint64_t actual_sender;
fc::raw::unpack( ds2, actual_sender );
BOOST_CHECK_EQUAL( account_name(actual_sender), sender );
};
BOOST_CHECK_EXCEPTION(
c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 1)
("contract", "test")
("payload", 77 )
),
ill_formed_deferred_transaction_generation_context,
fc_exception_message_is( "deferred transaction generaction context contains mismatching sender" )
);
BOOST_REQUIRE_EQUAL(0u, index.size());
auto trace2 = c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 1)
("contract", "test")
("payload", 40)
);
BOOST_REQUIRE_EQUAL(1u, index.size());
check_generation_context( index.begin()->packed_trx,
trace2->id,
((static_cast<unsigned __int128>("alice"_n.to_uint64_t()) << 64) | 1),
"test"_n );
c.produce_block();
BOOST_REQUIRE_EQUAL(0u, index.size());
auto trace3 = c.push_action( "test"_n, "defercall"_n, "alice"_n, fc::mutable_variant_object()
("payer", "alice")
("sender_id", 1)
("contract", "test")
("payload", 50)
);
BOOST_REQUIRE_EQUAL(1u, index.size());
check_generation_context( index.begin()->packed_trx,
trace3->id,
((static_cast<unsigned __int128>("alice"_n.to_uint64_t()) << 64) | 1),
"test"_n );
c.produce_block();
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_CASE( fix_linkauth_restriction ) { try {
tester chain( setup_policy::preactivate_feature_and_new_bios );
const auto& tester_account = "tester"_n;
chain.produce_blocks();
chain.create_account("currency"_n);
chain.create_account(tester_account);
chain.produce_blocks();
chain.push_action(config::system_account_name, updateauth::get_name(), tester_account, fc::mutable_variant_object()
("account", name(tester_account).to_string())
("permission", "first")
("parent", "active")
("auth", authority(chain.get_public_key(tester_account, "first"), 5))
);
auto validate_disallow = [&] (const char *code, const char *type) {
BOOST_REQUIRE_EXCEPTION(
chain.push_action(config::system_account_name, linkauth::get_name(), tester_account, fc::mutable_variant_object()
("account", name(tester_account).to_string())
("code", code)
("type", type)
("requirement", "first")),
action_validate_exception,
fc_exception_message_is(std::string("Cannot link eosio::") + std::string(type) + std::string(" to a minimum permission"))
);
};
validate_disallow("eosio", "linkauth");
validate_disallow("eosio", "unlinkauth");
validate_disallow("eosio", "deleteauth");
validate_disallow("eosio", "updateauth");
validate_disallow("eosio", "canceldelay");
validate_disallow("currency", "linkauth");
validate_disallow("currency", "unlinkauth");
validate_disallow("currency", "deleteauth");
validate_disallow("currency", "updateauth");
validate_disallow("currency", "canceldelay");
const auto& pfm = chain.control->get_protocol_feature_manager();
auto d = pfm.get_builtin_digest( builtin_protocol_feature_t::fix_linkauth_restriction );
BOOST_REQUIRE( d );
chain.preactivate_protocol_features( {*d} );
chain.produce_block();
auto validate_allowed = [&] (const char *code, const char *type) {
chain.push_action(config::system_account_name, linkauth::get_name(), tester_account, fc::mutable_variant_object()
("account", name(tester_account).to_string())
("code", code)
("type", type)
("requirement", "first"));
};
validate_disallow("eosio", "linkauth");
validate_disallow("eosio", "unlinkauth");
validate_disallow("eosio", "deleteauth");
validate_disallow("eosio", "updateauth");
validate_disallow("eosio", "canceldelay");
validate_allowed("currency", "linkauth");
validate_allowed("currency", "unlinkauth");
validate_allowed("currency", "deleteauth");
validate_allowed("currency", "updateauth");
validate_allowed("currency", "canceldelay");
} FC_LOG_AND_RETHROW() }
BOOST_AUTO_TEST_CASE( disallow_empty_producer_schedule_test ) { try {
tester c( setup_policy::preactivate_feature_and_new_bios );
const auto& pfm = c.control->get_protocol_feature_manager();
const auto& d = pfm.get_builtin_digest( builtin_protocol_feature_t::disallow_empty_producer_schedule );
BOOST_REQUIRE( d );
// Before activation, it is allowed to set empty producer schedule
c.set_producers_legacy( {} );
// After activation, it should not be allowed
c.preactivate_protocol_features( {*d} );
c.produce_block();
BOOST_REQUIRE_EXCEPTION( c.set_producers_legacy( {} ),
wasm_execution_error,
fc_exception_message_is( "Producer schedule cannot be empty" ) );
// Setting non empty producer schedule should still be fine
vector<name> producer_names = {"alice"_n,"bob"_n,"carol"_n};
c.create_accounts( producer_names );
c.set_producers_legacy( producer_names );
c.produce_blocks(2);
const auto& schedule = c.get_producer_authorities( producer_names );
BOOST_CHECK( std::equal( schedule.begin(), schedule.end(), c.control->active_producers().producers.begin()) );
} FC_LOG_AND_RETHROW() }
BOOST_AUTO_TEST_CASE( restrict_action_to_self_test ) { try {
tester c( setup_policy::preactivate_feature_and_new_bios );
const auto& pfm = c.control->get_protocol_feature_manager();
const auto& d = pfm.get_builtin_digest( builtin_protocol_feature_t::restrict_action_to_self );
BOOST_REQUIRE( d );
c.create_accounts( {"testacc"_n, "acctonotify"_n, "alice"_n} );
c.set_code( "testacc"_n, test_contracts::restrict_action_test_wasm() );
c.set_abi( "testacc"_n, test_contracts::restrict_action_test_abi() );
c.set_code( "acctonotify"_n, test_contracts::restrict_action_test_wasm() );
c.set_abi( "acctonotify"_n, test_contracts::restrict_action_test_abi() );
// Before the protocol feature is preactivated
// - Sending inline action to self = no problem
// - Sending deferred trx to self = throw subjective exception
// - Sending inline action to self from notification = throw subjective exception
// - Sending deferred trx to self from notification = throw subjective exception
BOOST_CHECK_NO_THROW( c.push_action( "testacc"_n, "sendinline"_n, "alice"_n, mutable_variant_object()("authorizer", "alice")) );
BOOST_REQUIRE_EXCEPTION( c.push_action( "testacc"_n, "senddefer"_n, "alice"_n,
mutable_variant_object()("authorizer", "alice")("senderid", 0)),
subjective_block_production_exception,
fc_exception_message_starts_with( "Authorization failure with sent deferred transaction" ) );
BOOST_REQUIRE_EXCEPTION( c.push_action( "testacc"_n, "notifyinline"_n, "alice"_n,
mutable_variant_object()("acctonotify", "acctonotify")("authorizer", "alice")),
subjective_block_production_exception,
fc_exception_message_starts_with( "Authorization failure with inline action sent to self" ) );
BOOST_REQUIRE_EXCEPTION( c.push_action( "testacc"_n, "notifydefer"_n, "alice"_n,
mutable_variant_object()("acctonotify", "acctonotify")("authorizer", "alice")("senderid", 1)),
subjective_block_production_exception,
fc_exception_message_starts_with( "Authorization failure with sent deferred transaction" ) );
c.preactivate_protocol_features( {*d} );
c.produce_block();
// After the protocol feature is preactivated, all the 4 cases will throw an objective unsatisfied_authorization exception
BOOST_REQUIRE_EXCEPTION( c.push_action( "testacc"_n, "sendinline"_n, "alice"_n, mutable_variant_object()("authorizer", "alice") ),
unsatisfied_authorization,
fc_exception_message_starts_with( "transaction declares authority" ) );
BOOST_REQUIRE_EXCEPTION( c.push_action( "testacc"_n, "senddefer"_n, "alice"_n,
mutable_variant_object()("authorizer", "alice")("senderid", 3)),
unsatisfied_authorization,
fc_exception_message_starts_with( "transaction declares authority" ) );
BOOST_REQUIRE_EXCEPTION( c.push_action( "testacc"_n, "notifyinline"_n, "alice"_n,
mutable_variant_object()("acctonotify", "acctonotify")("authorizer", "alice") ),
unsatisfied_authorization,
fc_exception_message_starts_with( "transaction declares authority" ) );
BOOST_REQUIRE_EXCEPTION( c.push_action( "testacc"_n, "notifydefer"_n, "alice"_n,
mutable_variant_object()("acctonotify", "acctonotify")("authorizer", "alice")("senderid", 4)),
unsatisfied_authorization,
fc_exception_message_starts_with( "transaction declares authority" ) );
} FC_LOG_AND_RETHROW() }
BOOST_AUTO_TEST_CASE( only_bill_to_first_authorizer ) { try {
tester chain( setup_policy::preactivate_feature_and_new_bios );
const auto& tester_account = "tester"_n;
const auto& tester_account2 = "tester2"_n;
chain.produce_blocks();
chain.create_account(tester_account);
chain.create_account(tester_account2);
chain.push_action(config::system_account_name, "setalimits"_n, config::system_account_name, fc::mutable_variant_object()
("account", name(tester_account).to_string())
("ram_bytes", 10000)
("net_weight", 1000)
("cpu_weight", 1000));
chain.push_action(config::system_account_name, "setalimits"_n, config::system_account_name, fc::mutable_variant_object()
("account", name(tester_account2).to_string())
("ram_bytes", 10000)
("net_weight", 1000)
("cpu_weight", 1000));
const resource_limits_manager& mgr = chain.control->get_resource_limits_manager();
chain.produce_blocks();
{
action act;
act.account = tester_account;
act.name = "null"_n;
act.authorization = vector<permission_level>{
{tester_account, config::active_name},
{tester_account2, config::active_name}
};
signed_transaction trx;
trx.actions.emplace_back(std::move(act));
chain.set_transaction_headers(trx);
trx.sign(get_private_key(tester_account, "active"), chain.control->get_chain_id());
trx.sign(get_private_key(tester_account2, "active"), chain.control->get_chain_id());
auto tester_cpu_limit0 = mgr.get_account_cpu_limit_ex(tester_account).first;
auto tester2_cpu_limit0 = mgr.get_account_cpu_limit_ex(tester_account2).first;
auto tester_net_limit0 = mgr.get_account_net_limit_ex(tester_account).first;
auto tester2_net_limit0 = mgr.get_account_net_limit_ex(tester_account2).first;
chain.push_transaction(trx);
auto tester_cpu_limit1 = mgr.get_account_cpu_limit_ex(tester_account).first;
auto tester2_cpu_limit1 = mgr.get_account_cpu_limit_ex(tester_account2).first;
auto tester_net_limit1 = mgr.get_account_net_limit_ex(tester_account).first;
auto tester2_net_limit1 = mgr.get_account_net_limit_ex(tester_account2).first;
BOOST_CHECK(tester_cpu_limit1.used > tester_cpu_limit0.used);
BOOST_CHECK(tester2_cpu_limit1.used > tester2_cpu_limit0.used);
BOOST_CHECK(tester_net_limit1.used > tester_net_limit0.used);
BOOST_CHECK(tester2_net_limit1.used > tester2_net_limit0.used);
BOOST_CHECK_EQUAL(tester_cpu_limit1.used - tester_cpu_limit0.used, tester2_cpu_limit1.used - tester2_cpu_limit0.used);
BOOST_CHECK_EQUAL(tester_net_limit1.used - tester_net_limit0.used, tester2_net_limit1.used - tester2_net_limit0.used);
}
const auto& pfm = chain.control->get_protocol_feature_manager();
const auto& d = pfm.get_builtin_digest( builtin_protocol_feature_t::only_bill_first_authorizer );
BOOST_REQUIRE( d );
chain.preactivate_protocol_features( {*d} );
chain.produce_blocks();
{
action act;
act.account = tester_account;
act.name = "null2"_n;
act.authorization = vector<permission_level>{
{tester_account, config::active_name},
{tester_account2, config::active_name}
};
signed_transaction trx;
trx.actions.emplace_back(std::move(act));
chain.set_transaction_headers(trx);
trx.sign(get_private_key(tester_account, "active"), chain.control->get_chain_id());
trx.sign(get_private_key(tester_account2, "active"), chain.control->get_chain_id());
auto tester_cpu_limit0 = mgr.get_account_cpu_limit_ex(tester_account).first;
auto tester2_cpu_limit0 = mgr.get_account_cpu_limit_ex(tester_account2).first;
auto tester_net_limit0 = mgr.get_account_net_limit_ex(tester_account).first;
auto tester2_net_limit0 = mgr.get_account_net_limit_ex(tester_account2).first;
chain.push_transaction(trx);
auto tester_cpu_limit1 = mgr.get_account_cpu_limit_ex(tester_account).first;
auto tester2_cpu_limit1 = mgr.get_account_cpu_limit_ex(tester_account2).first;
auto tester_net_limit1 = mgr.get_account_net_limit_ex(tester_account).first;
auto tester2_net_limit1 = mgr.get_account_net_limit_ex(tester_account2).first;
BOOST_CHECK(tester_cpu_limit1.used > tester_cpu_limit0.used);
BOOST_CHECK(tester2_cpu_limit1.used == tester2_cpu_limit0.used);
BOOST_CHECK(tester_net_limit1.used > tester_net_limit0.used);
BOOST_CHECK(tester2_net_limit1.used == tester2_net_limit0.used);
}
} FC_LOG_AND_RETHROW() }
BOOST_AUTO_TEST_CASE( forward_setcode_test ) { try {
tester c( setup_policy::preactivate_feature_only );
const auto& tester1_account = "tester1"_n;
const auto& tester2_account = "tester2"_n;
c.create_accounts( {tester1_account, tester2_account} );
// Deploy contract that rejects all actions dispatched to it with the following exceptions:
// * eosio::setcode to set code on the eosio is allowed (unless the rejectall account exists)
// * eosio::newaccount is allowed only if it creates the rejectall account.
c.set_code( config::system_account_name, test_contracts::reject_all_wasm() );
c.produce_block();
// Before activation, deploying a contract should work since setcode won't be forwarded to the WASM on eosio.
c.set_code( tester1_account, test_contracts::noop_wasm() );
// Activate FORWARD_SETCODE protocol feature and then return contract on eosio back to what it was.
const auto& pfm = c.control->get_protocol_feature_manager();
const auto& d = pfm.get_builtin_digest( builtin_protocol_feature_t::forward_setcode );
BOOST_REQUIRE( d );
c.set_before_producer_authority_bios_contract();
c.preactivate_protocol_features( {*d} );
c.produce_block();
c.set_code( config::system_account_name, test_contracts::reject_all_wasm() );
c.produce_block();
// After activation, deploying a contract causes setcode to be dispatched to the WASM on eosio,
// and in this case the contract is configured to reject the setcode action.
BOOST_REQUIRE_EXCEPTION( c.set_code( tester2_account, test_contracts::noop_wasm() ),
eosio_assert_message_exception,
eosio_assert_message_is( "rejecting all actions" ) );
tester c2(setup_policy::none);
push_blocks( c, c2 ); // make a backup of the chain to enable testing further conditions.
c.set_before_producer_authority_bios_contract(); // To allow pushing further actions for setting up the other part of the test.
c.create_account( "rejectall"_n );
c.produce_block();
// The existence of the rejectall account will make the reject_all contract reject all actions with no exception.