-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontroller.cpp
More file actions
3060 lines (2537 loc) · 133 KB
/
controller.cpp
File metadata and controls
3060 lines (2537 loc) · 133 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 <eosio/chain/controller.hpp>
#include <eosio/chain/transaction_context.hpp>
#include <eosio/chain/block_log.hpp>
#include <eosio/chain/fork_database.hpp>
#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/eosio_contract.hpp>
#include <eosio/chain/protocol_feature_manager.hpp>
#include <eosio/chain/authorization_manager.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/thread_utils.hpp>
#include <eosio/chain/platform_timer.hpp>
#include <eosio/chain/genesis_intrinsics.hpp>
#include <eosio/chain/to_string.hpp>
#include <chainbase/chainbase.hpp>
#include <fc/io/json.hpp>
#include <fc/log/logger_config.hpp>
#include <fc/scoped_exit.hpp>
#include <fc/variant_object.hpp>
#include <b1/chain_kv/chain_kv.hpp>
#include <fc/log/trace.hpp>
#include <fc/log/custom_formatter.hpp>
#include <new>
#include <eosio/vm/allocator.hpp>
namespace eosio { namespace chain {
using resource_limits::resource_limits_manager;
using namespace db_util;
struct building_block {
building_block( const block_header_state& prev,
block_timestamp_type when,
uint16_t num_prev_blocks_to_confirm,
const vector<digest_type>& new_protocol_feature_activations )
:_pending_block_header_state( prev.next( when, num_prev_blocks_to_confirm ) )
,_new_protocol_feature_activations( new_protocol_feature_activations )
,_trx_mroot_or_receipt_digests( digests_t{} )
{}
pending_block_header_state _pending_block_header_state;
std::optional<producer_authority_schedule> _new_pending_producer_schedule;
vector<digest_type> _new_protocol_feature_activations;
size_t _num_new_protocol_features_that_have_activated = 0;
deque<transaction_metadata_ptr> _pending_trx_metas;
deque<transaction_receipt> _pending_trx_receipts; // boost deque in 1.71 with 1024 elements performs better
std::variant<checksum256_type, digests_t> _trx_mroot_or_receipt_digests;
digests_t _action_receipt_digests;
};
struct assembled_block {
block_id_type _id;
pending_block_header_state _pending_block_header_state;
deque<transaction_metadata_ptr> _trx_metas;
signed_block_ptr _unsigned_block;
// if the _unsigned_block pre-dates block-signing authorities this may be present.
std::optional<producer_authority_schedule> _new_producer_authority_cache;
};
struct completed_block {
block_state_ptr _block_state;
};
using block_stage_type = std::variant<building_block, assembled_block, completed_block>;
struct pending_state {
pending_state( maybe_session&& s, const block_header_state& prev,
block_timestamp_type when,
uint16_t num_prev_blocks_to_confirm,
const vector<digest_type>& new_protocol_feature_activations )
:_db_session( std::move(s) )
,_block_stage( building_block( prev, when, num_prev_blocks_to_confirm, new_protocol_feature_activations ) )
{}
maybe_session _db_session;
block_stage_type _block_stage;
controller::block_status _block_status = controller::block_status::incomplete;
std::optional<block_id_type> _producer_block_id;
/** @pre _block_stage cannot hold completed_block alternative */
const pending_block_header_state& get_pending_block_header_state()const {
if( std::holds_alternative<building_block>(_block_stage) )
return std::get<building_block>(_block_stage)._pending_block_header_state;
return std::get<assembled_block>(_block_stage)._pending_block_header_state;
}
const deque<transaction_receipt>& get_trx_receipts()const {
if( std::holds_alternative<building_block>(_block_stage) )
return std::get<building_block>(_block_stage)._pending_trx_receipts;
if( std::holds_alternative<assembled_block>(_block_stage) )
return std::get<assembled_block>(_block_stage)._unsigned_block->transactions;
return std::get<completed_block>(_block_stage)._block_state->block->transactions;
}
deque<transaction_metadata_ptr> extract_trx_metas() {
if( std::holds_alternative<building_block>(_block_stage) )
return std::move( std::get<building_block>(_block_stage)._pending_trx_metas );
if( std::holds_alternative<assembled_block>(_block_stage) )
return std::move( std::get<assembled_block>(_block_stage)._trx_metas );
return std::get<completed_block>(_block_stage)._block_state->extract_trxs_metas();
}
bool is_protocol_feature_activated( const digest_type& feature_digest )const {
if( std::holds_alternative<building_block>(_block_stage) ) {
auto& bb = std::get<building_block>(_block_stage);
const auto& activated_features = bb._pending_block_header_state.prev_activated_protocol_features->protocol_features;
if( activated_features.find( feature_digest ) != activated_features.end() ) return true;
if( bb._num_new_protocol_features_that_have_activated == 0 ) return false;
auto end = bb._new_protocol_feature_activations.begin() + bb._num_new_protocol_features_that_have_activated;
return (std::find( bb._new_protocol_feature_activations.begin(), end, feature_digest ) != end);
}
if( std::holds_alternative<assembled_block>(_block_stage) ) {
// Calling is_protocol_feature_activated during the assembled_block stage is not efficient.
// We should avoid doing it.
// In fact for now it isn't even implemented.
EOS_THROW( misc_exception,
"checking if protocol feature is activated in the assembled_block stage is not yet supported" );
// TODO: implement this
}
const auto& activated_features = std::get<completed_block>(_block_stage)._block_state->activated_protocol_features->protocol_features;
return (activated_features.find( feature_digest ) != activated_features.end());
}
uint32_t get_block_num()const {
if( std::holds_alternative<building_block>(_block_stage) )
return std::get<building_block>(_block_stage)._pending_block_header_state.block_num;
if( std::holds_alternative<assembled_block>(_block_stage) )
return std::get<assembled_block>(_block_stage)._pending_block_header_state.block_num;
return std::get<completed_block>(_block_stage)._block_state->block_num;
}
void push() {
_db_session.push();
}
};
struct controller_impl {
// LLVM sets the new handler, we need to reset this to throw a bad_alloc exception so we can possibly exit cleanly
// and not just abort.
struct reset_new_handler {
reset_new_handler() { std::set_new_handler([](){ throw std::bad_alloc(); }); }
};
reset_new_handler rnh; // placed here to allow for this to be set before constructing the other fields
controller& self;
std::function<void()> shutdown;
chainbase::database db;
block_log blog;
std::optional<pending_state> pending;
block_state_ptr head;
fork_database fork_db;
wasm_interface wasmif;
resource_limits_manager resource_limits;
authorization_manager authorization;
protocol_feature_manager protocol_features;
controller::config conf;
const chain_id_type chain_id; // read by thread_pool threads, value will not be changed
std::optional<fc::time_point> replay_head_time;
db_read_mode read_mode = db_read_mode::SPECULATIVE;
bool in_trx_requiring_checks = false; ///< if true, checks that are normally skipped on replay (e.g. auth checks) cannot be skipped
std::optional<fc::microseconds> subjective_cpu_leeway;
bool override_chain_cpu_limits = false;
bool trusted_producer_light_validation = false;
uint32_t snapshot_head_block = 0;
named_thread_pool thread_pool;
named_thread_pool block_sign_pool;
// The completing_succeeded_blockid is for plugins to tell the controller that block should NOT be aborted.
// Block completing is async. The failed and succeeded marking may be from different tasks. We use 2 variables.
// These variables should be only updated by tasks executed by the main thread.
block_id_type completing_failed_blockid = block_id_type{};
block_id_type completing_succeeded_blockid = block_id_type{};
platform_timer timer;
bool okay_to_print_integrity_hash_on_stop = false;
vm::wasm_allocator wasm_alloc;
std::function<void(const char* data, size_t size)> push_event_function;
typedef pair<scope_name,action_name> handler_key;
map< account_name, map<handler_key, apply_handler> > apply_handlers;
unordered_map< builtin_protocol_feature_t, std::function<void(controller_impl&)>, enum_hash<builtin_protocol_feature_t> > protocol_feature_activation_handlers;
block_state_ptr pop_block() {
auto prev = fork_db.get_block( head->header.previous );
if( !prev ) {
EOS_ASSERT( fork_db.root()->id == head->header.previous, block_validate_exception, "attempt to pop beyond last irreversible block" );
prev = fork_db.root();
}
if ( read_mode == db_read_mode::SPECULATIVE ) {
EOS_ASSERT( head->block, block_validate_exception, "attempting to pop a block that was sparsely loaded from a snapshot");
}
auto result = head;
head = prev;
db.undo();
protocol_features.popped_blocks_to( prev->block_num );
return result;
}
template<builtin_protocol_feature_t F>
void on_activation();
template<builtin_protocol_feature_t F>
inline void set_activation_handler() {
auto res = protocol_feature_activation_handlers.emplace( F, &controller_impl::on_activation<F> );
EOS_ASSERT( res.second, misc_exception, "attempting to set activation handler twice" );
}
inline void trigger_activation_handler( builtin_protocol_feature_t f ) {
auto itr = protocol_feature_activation_handlers.find( f );
if( itr == protocol_feature_activation_handlers.end() ) return;
(itr->second)( *this );
}
void set_apply_handler( account_name receiver, account_name contract, action_name action, apply_handler v ) {
apply_handlers[receiver][make_pair(contract,action)] = v;
}
controller_impl( const controller::config& cfg, controller& s, protocol_feature_set&& pfs, const chain_id_type& chain_id )
:rnh(),
self(s),
db( cfg.state_dir,
cfg.read_only ? database::read_only : database::read_write,
cfg.state_size, cfg.db_on_invalid, cfg.db_map_mode, cfg.db_persistent),
blog( cfg.blog ),
fork_db( cfg.blog.log_dir / config::reversible_blocks_dir_name, true),
wasmif( cfg.wasm_runtime, db, cfg.state_dir, cfg.eosvmoc_config, !cfg.profile_accounts.empty(), cfg.native_config ),
resource_limits( db ),
authorization( s, db ),
protocol_features( std::move(pfs) ),
conf( cfg ),
chain_id( chain_id ),
read_mode( cfg.read_mode ),
thread_pool( "chain", cfg.thread_pool_size ),
block_sign_pool( "sign", 1 )
{
#ifdef EOSIO_REQUIRE_CHAIN_ID
EOS_ASSERT(chain_id == chain_id_type(EOSIO_REQUIRE_CHAIN_ID), disallowed_chain_id_exception,
"required chain id:{c} runtime chain id:{r}", ("r", chain_id)("c", EOSIO_REQUIRE_CHAIN_ID) );
#endif
fork_db.open( [this]( block_timestamp_type timestamp,
const flat_set<digest_type>& cur_features,
const vector<digest_type>& new_features )
{ check_protocol_features( timestamp, cur_features, new_features ); }
);
set_activation_handler<builtin_protocol_feature_t::preactivate_feature>();
set_activation_handler<builtin_protocol_feature_t::replace_deferred>();
set_activation_handler<builtin_protocol_feature_t::get_sender>();
set_activation_handler<builtin_protocol_feature_t::webauthn_key>();
set_activation_handler<builtin_protocol_feature_t::wtmsig_block_signatures>();
set_activation_handler<builtin_protocol_feature_t::action_return_value>();
set_activation_handler<builtin_protocol_feature_t::kv_database>();
set_activation_handler<builtin_protocol_feature_t::configurable_wasm_limits>();
set_activation_handler<builtin_protocol_feature_t::blockchain_parameters>();
set_activation_handler<builtin_protocol_feature_t::event_generation>();
set_activation_handler<builtin_protocol_feature_t::verify_rsa_sha256_sig>();
set_activation_handler<builtin_protocol_feature_t::verify_ecdsa_sig>();
self.irreversible_block.connect([this](const block_state_ptr& bsp) {
wasmif.current_lib(bsp->block_num);
});
#define SET_APP_HANDLER( receiver, contract, action) \
set_apply_handler( account_name(#receiver), account_name(#contract), action_name(#action), \
&BOOST_PP_CAT(apply_, BOOST_PP_CAT(contract, BOOST_PP_CAT(_,action) ) ) )
SET_APP_HANDLER( eosio, eosio, newaccount );
SET_APP_HANDLER( eosio, eosio, setcode );
SET_APP_HANDLER( eosio, eosio, setabi );
SET_APP_HANDLER( eosio, eosio, updateauth );
SET_APP_HANDLER( eosio, eosio, deleteauth );
SET_APP_HANDLER( eosio, eosio, linkauth );
SET_APP_HANDLER( eosio, eosio, unlinkauth );
/*
SET_APP_HANDLER( eosio, eosio, postrecovery );
SET_APP_HANDLER( eosio, eosio, passrecovery );
SET_APP_HANDLER( eosio, eosio, vetorecovery );
*/
SET_APP_HANDLER( eosio, eosio, canceldelay );
}
/**
* Plugins / observers listening to signals emited (such as accepted_transaction) might trigger
* errors and throw exceptions. Unless those exceptions are caught it could impact consensus and/or
* cause a node to fork.
*
* If it is ever desirable to let a signal handler bubble an exception out of this method
* a full audit of its uses needs to be undertaken.
*
*/
template<typename Signal, typename Arg>
void emit( const Signal& s, Arg&& a ) {
try {
s( std::forward<Arg>( a ));
} catch (std::bad_alloc& e) {
wlog( "std::bad_alloc: {w}", ("w", e.what()) );
throw e;
} catch (boost::interprocess::bad_alloc& e) {
wlog( "boost::interprocess::bad alloc: {w}", ("w", e.what()) );
throw e;
} catch ( controller_emit_signal_exception& e ) {
wlog( "controller_emit_signal_exception: {details}", ("details", e.to_detail_string()) );
throw e;
} catch ( fc::exception& e ) {
wlog( "fc::exception: {details}", ("details", e.to_detail_string()) );
} catch ( std::exception& e ) {
wlog( "std::exception: {details}", ("details", e.what()) );
} catch ( ... ) {
wlog( "signal handler threw exception" );
}
}
void log_irreversible() {
EOS_ASSERT( fork_db.root(), fork_database_exception, "fork database not properly initialized" );
const auto& log_head = blog.head();
auto lib_num = log_head ? log_head->block_num() : (blog.first_block_num() - 1);
auto root_id = fork_db.root()->id;
if( log_head ) {
// todo: move this check to startup so id does not have to be calculated
EOS_ASSERT( root_id == log_head->calculate_id(), fork_database_exception, "fork database root does not match block log head" );
} else if (conf.db_persistent) {
EOS_ASSERT( fork_db.root()->block_num == lib_num, fork_database_exception,
"empty block log expects the first appended block to build off a block that is not the fork database root. root block number: {block_num}, lib: {lib_num}", ("block_num", fork_db.root()->block_num) ("lib_num", lib_num) );
}
auto fork_head = (read_mode == db_read_mode::IRREVERSIBLE) ? fork_db.pending_head() : fork_db.head();
if( fork_head->dpos_irreversible_blocknum <= lib_num )
return;
auto branch = fork_db.fetch_branch( fork_head->id, fork_head->dpos_irreversible_blocknum );
try {
std::vector<std::future<std::tuple<signed_block_ptr, std::vector<char>>>> v;
v.reserve( branch.size() );
for( auto bitr = branch.rbegin(); bitr != branch.rend(); ++bitr ) {
v.emplace_back( blog.create_append_future( thread_pool.get_executor(), (*bitr)->block,
packed_transaction::cf_compression_type::none ) );
}
auto it = v.begin();
for( auto bitr = branch.rbegin(); bitr != branch.rend(); ++bitr ) {
if( read_mode == db_read_mode::IRREVERSIBLE ) {
apply_block( *bitr, controller::block_status::complete, trx_meta_cache_lookup{} );
head = (*bitr);
fork_db.mark_valid( head );
}
emit( self.irreversible_block, *bitr );
// blog.append could fail due to failures like running out of space.
// Do it before commit so that in case it throws, DB can be rolled back.
blog.append( std::move( *it ) );
++it;
db.commit( (*bitr)->block_num );
root_id = (*bitr)->id;
}
} catch( std::exception& ) {
if( root_id != fork_db.root()->id ) {
fork_db.advance_root( root_id );
}
throw;
}
if( root_id != fork_db.root()->id ) {
branch.emplace_back(fork_db.root());
fork_db.advance_root( root_id );
}
// delete branch in thread pool
boost::asio::post( thread_pool.get_executor(), [branch{std::move(branch)}]() {} );
}
/**
* Sets fork database head to the genesis state.
*/
void initialize_blockchain_state(const genesis_state& genesis) {
wlog( "Initializing new blockchain with genesis state" );
producer_authority_schedule initial_schedule = { 0, { producer_authority{config::system_account_name, block_signing_authority_v0{ 1, {{genesis.initial_key, 1}} } } } };
legacy::producer_schedule_type initial_legacy_schedule{ 0, {{config::system_account_name, genesis.initial_key}} };
block_header_state genheader;
genheader.active_schedule = initial_schedule;
genheader.pending_schedule.schedule = initial_schedule;
// NOTE: if wtmsig block signatures are enabled at genesis time this should be the hash of a producer authority schedule
genheader.pending_schedule.schedule_hash = fc::sha256::hash(initial_legacy_schedule);
genheader.header.timestamp = genesis.initial_timestamp;
genheader.header.action_mroot = genesis.compute_chain_id();
genheader.id = genheader.header.calculate_id();
genheader.block_num = genheader.header.block_num();
head = std::make_shared<block_state>();
static_cast<block_header_state&>(*head) = genheader;
head->activated_protocol_features = std::make_shared<protocol_feature_activation_set>();
head->block = std::make_shared<signed_block>(genheader.header);
db.set_revision( head->block_num );
initialize_database(genesis);
}
void replay(std::function<bool()> check_shutdown) {
if( !blog.head() && !fork_db.root() ) {
fork_db.reset( *head );
return;
}
auto blog_head = blog.head();
auto blog_head_time = blog_head ? blog_head->timestamp.to_time_point() : fork_db.root()->header.timestamp.to_time_point();
replay_head_time = blog_head_time;
auto start_block_num = head->block_num + 1;
auto start = fc::time_point::now();
std::exception_ptr except_ptr;
if( blog_head && start_block_num <= blog_head->block_num() ) {
ilog( "existing block log, attempting to replay from {s} to {n} blocks",
("s", start_block_num)("n", blog_head->block_num()) );
try {
while( std::unique_ptr<signed_block> next = blog.read_signed_block_by_num( head->block_num + 1 ) ) {
auto block_num = next->block_num();
replay_push_block( std::move(next), controller::block_status::irreversible );
if( check_shutdown() ) break;
if( block_num % 500 == 0 ) {
ilog( "{n} of {head}", ("n", block_num)("head", blog_head->block_num()) );
}
}
} catch( const database_guard_exception& e ) {
except_ptr = std::current_exception();
}
ilog( "{n} irreversible blocks replayed", ("n", 1 + head->block_num - start_block_num) );
auto pending_head = fork_db.pending_head();
if( pending_head ) {
ilog( "fork database head {h}, root {r}", ("h", pending_head->block_num)( "r", fork_db.root()->block_num ) );
if( pending_head->block_num < head->block_num || head->block_num < fork_db.root()->block_num ) {
ilog( "resetting fork database with new last irreversible block as the new root: {id}", ("id", head->id) );
fork_db.reset( *head );
} else if( head->block_num != fork_db.root()->block_num ) {
auto new_root = fork_db.search_on_branch( pending_head->id, head->block_num );
EOS_ASSERT( new_root, fork_database_exception,
"unexpected error: could not find new LIB in fork database" );
ilog( "advancing fork database root to new last irreversible block within existing fork database: {id}",
("id", new_root->id) );
fork_db.mark_valid( new_root );
fork_db.advance_root( new_root->id );
}
}
// if the irreverible log is played without undo sessions enabled, we need to sync the
// revision ordinal to the appropriate expected value here.
if( self.skip_db_sessions( controller::block_status::irreversible ) )
db.set_revision( head->block_num );
} else {
ilog( "no irreversible blocks need to be replayed" );
}
if( !except_ptr && !check_shutdown() && fork_db.head() ) {
auto head_block_num = head->block_num;
auto branch = fork_db.fetch_branch( fork_db.head()->id );
int rev = 0;
for( auto i = branch.rbegin(); i != branch.rend(); ++i ) {
if( check_shutdown() ) break;
if( (*i)->block_num <= head_block_num ) continue;
++rev;
replay_push_block( (*i)->block, controller::block_status::validated );
}
ilog( "{n} reversible blocks replayed", ("n",rev) );
}
if( !fork_db.head() ) {
fork_db.reset( *head );
}
auto end = fc::time_point::now();
ilog( "replayed {n} blocks in {duration} seconds, {mspb} ms/block",
("n", head->block_num + 1 - start_block_num)("duration", (end-start).count()/1000000)
("mspb", ((end-start).count()/1000.0)/(head->block_num-start_block_num)) );
replay_head_time.reset();
if( except_ptr ) {
std::rethrow_exception( except_ptr );
}
}
void startup(std::function<void()> shutdown, std::function<bool()> check_shutdown, const snapshot_reader_ptr& snapshot) {
EOS_ASSERT( snapshot, snapshot_exception, "No snapshot reader provided" );
this->shutdown = shutdown;
ilog( "Starting initialization from snapshot, this may take a significant amount of time" );
try {
snapshot->validate();
if( blog.head() ) {
// if the blocklog exists, this snapshot can be a lib based snapshot or a state snapshot created during shutdown
uint32_t max_block_num = 0;
if (blog.head()) max_block_num = blog.head()->block_num();
if (fork_db.head()) max_block_num = std::max(max_block_num, fork_db.head()->block_num);
read_from_snapshot( db, snapshot, blog.first_block_num(), max_block_num,
authorization, resource_limits,
head, snapshot_head_block, chain_id );
} else {
// if the blocklog does not exist, this snapshot must be a lib based snapshot
read_from_snapshot( db, snapshot, 0, std::numeric_limits<uint32_t>::max(),
authorization, resource_limits,
head, snapshot_head_block, chain_id );
const uint32_t lib_num = head->block_num;
EOS_ASSERT( lib_num > 0, snapshot_exception,
"Snapshot indicates controller head at block number 0, but that is not allowed. "
"Snapshot is invalid." );
blog.reset( chain_id, lib_num + 1 );
}
init(check_shutdown, true);
} catch (boost::interprocess::bad_alloc& e) {
elog( "db storage not configured to have enough storage for the provided snapshot, please increase and retry snapshot" );
throw e;
}
ilog( "Finished initialization from snapshot" );
}
void startup(std::function<void()> shutdown, std::function<bool()> check_shutdown, const genesis_state& genesis) {
EOS_ASSERT( db.revision() < 1, database_exception, "This version of controller::startup only works with a fresh state database." );
const auto& genesis_chain_id = genesis.compute_chain_id();
EOS_ASSERT( genesis_chain_id == chain_id, chain_id_type_exception,
"genesis state provided to startup corresponds to a chain ID ({genesis_chain_id}) that does not match the chain ID that controller was constructed with ({controller_chain_id})",
("genesis_chain_id", genesis_chain_id.str())("controller_chain_id", chain_id.str())
);
this->shutdown = shutdown;
if( fork_db.head() ) {
if( read_mode == db_read_mode::IRREVERSIBLE && fork_db.head()->id != fork_db.root()->id ) {
fork_db.rollback_head_to_root();
}
wlog( "No existing chain state. Initializing fresh blockchain state." );
} else {
wlog( "No existing chain state or fork database. Initializing fresh blockchain state and resetting fork database.");
}
initialize_blockchain_state(genesis); // sets head to genesis state
if( !fork_db.head() ) {
fork_db.reset( *head );
}
if( blog.head() ) {
EOS_ASSERT( blog.first_block_num() == 1, block_log_exception,
"block log does not start with genesis block"
);
} else {
blog.reset( genesis, head->block, packed_transaction::cf_compression_type::none );
}
init(check_shutdown, true);
}
void startup(std::function<void()> shutdown, std::function<bool()> check_shutdown) {
EOS_ASSERT( db.revision() >= 1, database_exception, "This version of controller::startup does not work with a fresh state database." );
EOS_ASSERT( fork_db.head(), fork_database_exception, "No existing fork database despite existing chain state. Replay required." );
this->shutdown = shutdown;
uint32_t lib_num = fork_db.root()->block_num;
auto first_block_num = blog.first_block_num();
if( blog.head() ) {
EOS_ASSERT( first_block_num <= lib_num && lib_num <= blog.head()->block_num(),
block_log_exception,
"block log (ranging from {block_log_first_num} to {block_log_last_num}) does not contain the last irreversible block ({fork_db_lib})",
("block_log_first_num", first_block_num)
("block_log_last_num", blog.head()->block_num())
("fork_db_lib", lib_num)
);
lib_num = blog.head()->block_num();
} else {
if( first_block_num != (lib_num + 1) ) {
blog.reset( chain_id, lib_num + 1 );
}
}
if( read_mode == db_read_mode::IRREVERSIBLE && fork_db.head()->id != fork_db.root()->id ) {
fork_db.rollback_head_to_root();
}
head = fork_db.head();
init(check_shutdown, false);
}
static auto validate_db_version( const chainbase::database& db ) {
// check database version
const auto& header_idx = db.get_index<database_header_multi_index>().indices().get<by_id>();
EOS_ASSERT(header_idx.begin() != header_idx.end(), bad_database_version_exception,
"state database version pre-dates versioning, please restore from a compatible snapshot or replay!");
auto header_itr = header_idx.begin();
header_itr->validate();
return header_itr;
}
void init(std::function<bool()> check_shutdown, bool clean_startup) {
uint32_t lib_num = (blog.head() ? blog.head()->block_num() : fork_db.head() ? fork_db.root()->block_num : 0);
EOS_ASSERT( lib_num >= this->conf.min_initial_block_num, misc_exception, "Controller latest irreversible block "
"at block number {lib_num}, which is smaller than the minimum required {required}", ("lib_num", lib_num)("required",this->conf.min_initial_block_num) );
auto header_itr = validate_db_version( db );
{
const auto& state_chain_id = db.get<global_property_object>().chain_id;
EOS_ASSERT( state_chain_id == chain_id, chain_id_type_exception,
"chain ID in state ({state_chain_id}) does not match the chain ID that controller was constructed with ({controller_chain_id})",
("state_chain_id", state_chain_id.str())("controller_chain_id", chain_id.str())
);
}
// upgrade to the latest compatible version
if (header_itr->version != database_header_object::current_version) {
db.modify(*header_itr, [](auto& header) {
header.version = database_header_object::current_version;
});
}
check_backing_store_setting( db, clean_startup );
// At this point head != nullptr
EOS_ASSERT( db.revision() >= head->block_num, fork_database_exception,
"fork database head ({head}) is inconsistent with state ({db})",
("db",db.revision())("head",head->block_num) );
if( db.revision() > head->block_num ) {
wlog( "database revision ({db}) is greater than head block number ({head}), "
"attempting to undo pending changes",
("db",db.revision())("head",head->block_num) );
}
while( db.revision() > head->block_num ) {
db.undo();
}
protocol_features.init( db );
if( conf.integrity_hash_on_start )
ilog( "chain database started with hash: {hash}", ("hash", calculate_integrity_hash()) );
okay_to_print_integrity_hash_on_stop = true;
replay( check_shutdown ); // replay any irreversible and reversible blocks ahead of current head
if( check_shutdown() ) return;
// At this point head != nullptr && fork_db.head() != nullptr && fork_db.root() != nullptr.
// Furthermore, fork_db.root()->block_num <= lib_num.
// Also, even though blog.head() may still be nullptr, blog.first_block_num() is guaranteed to be lib_num + 1.
if( read_mode != db_read_mode::IRREVERSIBLE
&& fork_db.pending_head()->id != fork_db.head()->id
&& fork_db.head()->id == fork_db.root()->id
) {
wlog( "read_mode has changed from irreversible: applying best branch from fork database" );
for( auto pending_head = fork_db.pending_head();
pending_head->id != fork_db.head()->id;
pending_head = fork_db.pending_head()
) {
wlog( "applying branch from fork database ending with block: {id}", ("id", pending_head->id) );
maybe_switch_forks( pending_head, controller::block_status::complete, forked_branch_callback{}, trx_meta_cache_lookup{} );
}
}
}
~controller_impl() {
thread_pool.stop();
block_sign_pool.stop();
pending.reset();
//only log this not just if configured to, but also if initialization made it to the point we'd log the startup too
if(okay_to_print_integrity_hash_on_stop && conf.integrity_hash_on_stop)
ilog( "chain database stopped with hash: {hash}", ("hash", calculate_integrity_hash()) );
}
void add_indices() {
controller_index_set::add_indices(db);
db.add_index<kv_index>();
contract_database_index_set::add_indices(db);
authorization.add_indices();
resource_limits.add_indices();
}
sha256 calculate_integrity_hash() const {
sha256::encoder enc;
auto hash_writer = std::make_shared<integrity_hash_snapshot_writer>(enc);
add_to_snapshot(db, hash_writer, *head, authorization, resource_limits);
hash_writer->finalize();
return enc.result();
}
void create_native_account( const fc::time_point& initial_timestamp, account_name name, const authority& owner, const authority& active, bool is_privileged = false ) {
db.create<account_object>([&](auto& a) {
a.name = name;
a.creation_date = initial_timestamp;
if( name == config::system_account_name ) {
// The initial eosio ABI value affects consensus; see https://github.com/EOSIO/eos/issues/7794
// TODO: This doesn't charge RAM; a fix requires a consensus upgrade.
a.abi.assign(eosio_abi_bin, sizeof(eosio_abi_bin));
}
});
db.create<account_metadata_object>([&](auto & a) {
a.name = name;
a.set_privileged( is_privileged );
});
const auto& owner_permission = authorization.create_permission(name, config::owner_name, 0,
owner, 0, initial_timestamp );
const auto& active_permission = authorization.create_permission(name, config::active_name, owner_permission.id,
active, 0, initial_timestamp );
resource_limits.initialize_account(name);
int64_t ram_delta = config::overhead_per_account_ram_bytes;
ram_delta += 2*config::billable_size_v<permission_object>;
ram_delta += owner_permission.auth.get_billable_size();
ram_delta += active_permission.auth.get_billable_size();
resource_limits.add_pending_ram_usage(name, ram_delta);
resource_limits.verify_account_ram_usage(name);
}
void initialize_database(const genesis_state& genesis) {
// create the database header sigil
db.create<database_header_object>([&]( auto& header ){
// nothing to do for now
});
// Initialize block summary index
for (int i = 0; i < 0x10000; i++)
db.create<block_summary_object>([&](block_summary_object&) {});
const auto& tapos_block_summary = db.get<block_summary_object>(1);
db.modify( tapos_block_summary, [&]( auto& bs ) {
bs.block_id = head->id;
});
genesis.initial_configuration.validate();
db.create<global_property_object>([&genesis,&chain_id=this->chain_id](auto& gpo ){
gpo.configuration = genesis.initial_configuration;
gpo.kv_configuration = kv_database_config{};
// TODO: Update this when genesis protocol features are enabled.
gpo.wasm_configuration = genesis_state::default_initial_wasm_configuration;
gpo.chain_id = chain_id;
});
db.create<protocol_state_object>([&](auto& pso ){
pso.num_supported_key_types = config::genesis_num_supported_key_types;
for( const auto& i : genesis_intrinsics ) {
add_intrinsic_to_whitelist( pso.whitelisted_intrinsics, i );
}
});
db.create<dynamic_global_property_object>([](auto&){});
db.create<kv_db_config_object>([](auto&){});
authorization.initialize_database();
resource_limits.initialize_database();
authority system_auth(genesis.initial_key);
create_native_account( genesis.initial_timestamp, config::system_account_name, system_auth, system_auth, true );
auto empty_authority = authority(1, {}, {});
auto active_producers_authority = authority(1, {}, {});
active_producers_authority.accounts.push_back({{config::system_account_name, config::active_name}, 1});
create_native_account( genesis.initial_timestamp, config::null_account_name, empty_authority, empty_authority );
create_native_account( genesis.initial_timestamp, config::producers_account_name, empty_authority, active_producers_authority );
const auto& active_permission = authorization.get_permission({config::producers_account_name, config::active_name});
const auto& majority_permission = authorization.create_permission( config::producers_account_name,
config::majority_producers_permission_name,
active_permission.id,
active_producers_authority,
0,
genesis.initial_timestamp );
const auto& minority_permission = authorization.create_permission( config::producers_account_name,
config::minority_producers_permission_name,
majority_permission.id,
active_producers_authority,
0,
genesis.initial_timestamp );
}
// The returned scoped_exit should not exceed the lifetime of the pending which existed when make_block_restore_point was called.
fc::scoped_exit<std::function<void()>> make_block_restore_point() {
auto& bb = std::get<building_block>(pending->_block_stage);
auto orig_trx_receipts_size = bb._pending_trx_receipts.size();
auto orig_trx_metas_size = bb._pending_trx_metas.size();
auto orig_trx_receipt_digests_size = std::holds_alternative<digests_t>(bb._trx_mroot_or_receipt_digests) ?
std::get<digests_t>(bb._trx_mroot_or_receipt_digests).size() : 0;
auto orig_action_receipt_digests_size = bb._action_receipt_digests.size();
std::function<void()> callback = [this,
orig_trx_receipts_size,
orig_trx_metas_size,
orig_trx_receipt_digests_size,
orig_action_receipt_digests_size]()
{
auto& bb = std::get<building_block>(pending->_block_stage);
bb._pending_trx_receipts.resize(orig_trx_receipts_size);
bb._pending_trx_metas.resize(orig_trx_metas_size);
if( std::holds_alternative<digests_t>(bb._trx_mroot_or_receipt_digests) )
std::get<digests_t>(bb._trx_mroot_or_receipt_digests).resize(orig_trx_receipt_digests_size);
bb._action_receipt_digests.resize(orig_action_receipt_digests_size);
};
return fc::make_scoped_exit( std::move(callback) );
}
/**
* Adds the transaction receipt to the pending block and returns it.
*/
template<typename T>
const transaction_receipt& push_receipt( const T& trx, transaction_receipt_header::status_enum status,
uint64_t cpu_usage_us, uint64_t net_usage ) {
uint64_t net_usage_words = net_usage / 8;
EOS_ASSERT( net_usage_words*8 == net_usage, transaction_exception, "net_usage is not divisible by 8" );
auto& receipts = std::get<building_block>(pending->_block_stage)._pending_trx_receipts;
receipts.emplace_back( trx );
transaction_receipt& r = receipts.back();
r.cpu_usage_us = cpu_usage_us;
r.net_usage_words = net_usage_words;
r.status = status;
auto& bb = std::get<building_block>(pending->_block_stage);
if( std::holds_alternative<digests_t>(bb._trx_mroot_or_receipt_digests) )
std::get<digests_t>(bb._trx_mroot_or_receipt_digests).emplace_back( r.digest() );
return r;
}
/**
* This is the entry point for new transactions to the block state. It will check authorization and
* determine whether to execute it now or to delay it. Lastly it inserts a transaction receipt into
* the pending block.
*/
transaction_trace_ptr push_transaction( const transaction_metadata_ptr& trx,
fc::time_point block_deadline,
fc::microseconds max_transaction_time,
uint32_t billed_cpu_time_us,
bool explicit_billed_cpu_time,
std::optional<uint32_t> explicit_net_usage_words,
uint32_t subjective_cpu_bill_us )
{
EOS_ASSERT(block_deadline != fc::time_point(), transaction_exception, "deadline cannot be uninitialized");
transaction_trace_ptr trace;
try {
auto start = fc::time_point::now();
const bool check_auth = !self.skip_auth_check() && !trx->implicit;
const fc::microseconds sig_cpu_usage = trx->signature_cpu_usage();
if( !explicit_billed_cpu_time ) {
fc::microseconds already_consumed_time( EOS_PERCENT(sig_cpu_usage.count(), conf.sig_cpu_bill_pct) );
if( start.time_since_epoch() < already_consumed_time ) {
start = fc::time_point();
} else if ( max_transaction_time != fc::microseconds::maximum() ) {
// max_transaction_time could be negative after this call which will just shorten the deadline in trx_context
max_transaction_time -= already_consumed_time;
}
}
transaction_checktime_timer trx_timer(timer);
transaction_context trx_context(self, *trx->packed_trx(), std::move(trx_timer), start);
if ((bool)subjective_cpu_leeway && pending->_block_status == controller::block_status::incomplete) {
trx_context.leeway = *subjective_cpu_leeway;
}
trx_context.block_deadline = block_deadline;
trx_context.max_transaction_time_subjective = max_transaction_time;
trx_context.explicit_billed_cpu_time = explicit_billed_cpu_time;
trx_context.billed_cpu_time_us = billed_cpu_time_us;
trx_context.subjective_cpu_bill_us = subjective_cpu_bill_us;
trace = trx_context.trace;
auto handle_exception =[&](const auto& e)
{
trace->error_code = controller::convert_exception_to_error_code( e );
trace->except = e;
trace->except_ptr = std::current_exception();
trace->elapsed = fc::time_point::now() - trx_context.start;
};
try {
const transaction& trn = trx->packed_trx()->get_transaction();
if( trx->implicit ) {
EOS_ASSERT( !explicit_net_usage_words, transaction_exception, "NET usage cannot be explicitly set for implicit transactions" );
trx_context.init_for_implicit_trx();
trx_context.enforce_whiteblacklist = false;
} else {
bool skip_recording = replay_head_time && (time_point(trn.expiration) <= *replay_head_time);
if( explicit_net_usage_words ) {
trx_context.init_for_input_trx_with_explicit_net( *explicit_net_usage_words, skip_recording );
} else {
trx_context.init_for_input_trx( trx->packed_trx()->get_unprunable_size(),
trx->packed_trx()->get_prunable_size(),
skip_recording );
}
}
EOS_ASSERT( trn.delay_sec == fc::unsigned_int(0), block_validate_exception, "trn.delay_sec {delay_sec} not 0", ("delay_sec",trn.delay_sec) ); // trn.delay_sec dedeprecated
if( check_auth ) {
authorization.check_authorization(
trn.actions,
trx->recovered_keys(),
{},
[&trx_context](){ trx_context.checktime(); },
false
);
}
trx_context.exec();
trx_context.finalize(); // Automatically rounds up network and CPU usage in trace and bills payers if successful
auto restore = make_block_restore_point();
if (!trx->implicit) {
transaction_receipt::status_enum s = transaction_receipt::executed;
trace->receipt = push_receipt(*trx->packed_trx(), s, trx_context.billed_cpu_time_us, trace->net_usage);
trx->billed_cpu_time_us = trx_context.billed_cpu_time_us;
std::get<building_block>(pending->_block_stage)._pending_trx_metas.emplace_back(trx);
} else {
transaction_receipt_header r;
r.status = transaction_receipt::executed;
r.cpu_usage_us = trx_context.billed_cpu_time_us;
r.net_usage_words = trace->net_usage / 8;
trace->receipt = r;
}
fc::move_append( std::get<building_block>(pending->_block_stage)._action_receipt_digests,
std::move(trx_context.executed_action_receipt_digests) );
// call the accept signal but only once for this transaction
if (!trx->accepted) {
trx->accepted = true;
emit( self.accepted_transaction, trx);
}
emit(self.applied_transaction, std::tie(trace, trx->packed_trx()));
if ( (read_mode != db_read_mode::SPECULATIVE && pending->_block_status == controller::block_status::incomplete) || trx->read_only ) {
//this may happen automatically in destructor, but I prefer to make it more explicit
trx_context.undo();
} else {
restore.cancel();
trx_context.squash();
}
return trace;
} catch( const objective_block_validation_exception& ) {
throw;
} catch ( const std::bad_alloc& ) {
throw;
} catch ( const boost::interprocess::bad_alloc& ) {
throw;
} catch (const fc::exception& e) {
handle_exception(e);
} catch (const std::exception& e) {
auto wrapper = fc::std_exception_wrapper::from_current_exception(e);
handle_exception(wrapper);
}
emit( self.accepted_transaction, trx );
emit( self.applied_transaction, std::tie(trace, trx->packed_trx()) );
return trace;
} FC_CAPTURE_AND_RETHROW((trace))
} /// push_transaction