-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontroller.cpp
5891 lines (4980 loc) · 269 KB
/
controller.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/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/account_object.hpp>
#include <eosio/chain/code_object.hpp>
#include <eosio/chain/block_summary_object.hpp>
#include <eosio/chain/eosio_contract.hpp>
#include <eosio/chain/global_property_object.hpp>
#include <eosio/chain/protocol_state_object.hpp>
#include <eosio/chain/contract_table_objects.hpp>
#include <eosio/chain/generated_transaction_object.hpp>
#include <eosio/chain/transaction_object.hpp>
#include <eosio/chain/genesis_intrinsics.hpp>
#include <eosio/chain/whitelisted_intrinsics.hpp>
#include <eosio/chain/database_header_object.hpp>
#include <eosio/chain/protocol_feature_manager.hpp>
#include <eosio/chain/authorization_manager.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/subjective_billing.hpp>
#include <eosio/chain/chain_snapshot.hpp>
#include <eosio/chain/snapshot_detail.hpp>
#include <eosio/chain/thread_utils.hpp>
#include <eosio/chain/platform_timer.hpp>
#include <eosio/chain/block_header_state_utils.hpp>
#include <eosio/chain/deep_mind.hpp>
#include <eosio/chain/finality/finalizer.hpp>
#include <eosio/chain/finality/finalizer_policy.hpp>
#include <eosio/chain/finality/quorum_certificate.hpp>
#include <eosio/chain/finality/vote_message.hpp>
#include <eosio/chain/vote_processor.hpp>
#include <chainbase/chainbase.hpp>
#include <eosio/vm/allocator.hpp>
#include <fc/io/json.hpp>
#include <fc/log/logger_config.hpp>
#include <fc/scoped_exit.hpp>
#include <fc/variant_object.hpp>
#include <bls12-381/bls12-381.hpp>
#include <future>
#include <new>
#include <shared_mutex>
#include <utility>
namespace eosio::chain {
using resource_limits::resource_limits_manager;
using controller_index_set = index_set<
account_index,
account_metadata_index,
account_ram_correction_index,
global_property_multi_index,
protocol_state_multi_index,
dynamic_global_property_multi_index,
block_summary_multi_index,
transaction_multi_index,
generated_transaction_multi_index,
table_id_multi_index,
code_index,
database_header_multi_index
>;
using contract_database_index_set = index_set<
key_value_index,
index64_index,
index128_index,
index256_index,
index_double_index,
index_long_double_index
>;
class maybe_session {
public:
maybe_session() = default;
maybe_session( maybe_session&& other) noexcept
:_session(std::move(other._session))
{
}
explicit maybe_session(database& db) {
_session.emplace(db.start_undo_session(true));
}
maybe_session(const maybe_session&) = delete;
void squash() {
if (_session)
_session->squash();
}
void undo() {
if (_session)
_session->undo();
}
void push() {
if (_session)
_session->push();
}
maybe_session& operator=( maybe_session&& mv ) noexcept {
if (mv._session) {
_session.emplace(std::move(*mv._session));
mv._session.reset();
} else {
_session.reset();
}
return *this;
};
private:
std::optional<database::session> _session;
};
// apply methods of block_handle defined here as access to internal block_handle restricted to controller
template <class R, class F>
R apply(const block_handle& bh, F&& f) {
if constexpr (std::is_same_v<void, R>)
std::visit<void>(overloaded{[&](const block_state_legacy_ptr& head) { std::forward<F>(f)(head); },
[&](const block_state_ptr& head) { std::forward<F>(f)(head); }
}, bh.internal());
else
return std::visit<R>(overloaded{[&](const block_state_legacy_ptr& head) -> R { return std::forward<F>(f)(head); },
[&](const block_state_ptr& head) -> R { return std::forward<F>(f)(head); }
}, bh.internal());
}
template <class R, class F, class S>
R apply(const block_handle& bh, F&& f, S&& s) {
if constexpr (std::is_same_v<void, R>)
std::visit<void>(overloaded{[&](const block_state_legacy_ptr& head) { std::forward<F>(f)(head); },
[&](const block_state_ptr& head) { std::forward<S>(s)(head); }
}, bh.internal());
else
return std::visit<R>(overloaded{[&](const block_state_legacy_ptr& head) -> R { return std::forward<F>(f)(head); },
[&](const block_state_ptr& head) -> R { return std::forward<S>(s)(head); }
}, bh.internal());
}
// apply savanna block_state
template <class R, class F>
R apply_s(const block_handle& bh, F&& f) {
if constexpr (std::is_same_v<void, R>)
std::visit<void>(overloaded{[&](const block_state_legacy_ptr&) {},
[&](const block_state_ptr& head) { std::forward<F>(f)(head); }}, bh.internal());
else
return std::visit<R>(overloaded{[&](const block_state_legacy_ptr&) -> R { return {}; },
[&](const block_state_ptr& head) -> R { return std::forward<F>(f)(head); }}, bh.internal());
}
// apply legancy block_state_legacy
template <class R, class F>
R apply_l(const block_handle& bh, F&& f) {
if constexpr (std::is_same_v<void, R>)
std::visit<void>(overloaded{[&](const block_state_legacy_ptr& head) { std::forward<F>(f)(head); },
[&](const block_state_ptr&) {}}, bh.internal());
else
return std::visit<R>(overloaded{[&](const block_state_legacy_ptr& head) -> R { return std::forward<F>(f)(head); },
[&](const block_state_ptr&) -> R { return {}; }}, bh.internal());
}
struct completed_block {
block_handle bsp;
deque<transaction_metadata_ptr> extract_trx_metas() {
return apply<deque<transaction_metadata_ptr>>(bsp, [](auto& bsp) { return bsp->extract_trxs_metas(); });
}
const flat_set<digest_type>& get_activated_protocol_features() const {
return apply<const flat_set<digest_type>&>(bsp, [](const auto& bsp) -> const flat_set<digest_type>& {
return bsp->get_activated_protocol_features()->protocol_features;
});
}
const block_id_type& id() const { return bsp.id(); }
uint32_t block_num() const { return bsp.block_num(); }
block_timestamp_type timestamp() const { return bsp.block_time(); }
account_name producer() const { return bsp.producer(); }
const producer_authority_schedule& active_producers() const {
return apply<const producer_authority_schedule&>(bsp, [](const auto& bsp) -> const producer_authority_schedule& {
return bsp->active_schedule_auth();
});
}
const producer_authority_schedule* next_producers() const {
return apply<const producer_authority_schedule*>(bsp,
overloaded{[](const block_state_legacy_ptr& bsp) -> const producer_authority_schedule* {
return bsp->pending_schedule_auth();
},
[](const block_state_ptr& bsp) -> const producer_authority_schedule* {
return bsp->proposer_policies.empty()
? nullptr
: &bsp->proposer_policies.begin()->second->proposer_schedule;
}
});
}
const producer_authority_schedule* pending_producers_legacy() const {
return apply<const producer_authority_schedule*>(bsp,
overloaded{[](const block_state_legacy_ptr& bsp) -> const producer_authority_schedule* {
return &bsp->pending_schedule.schedule;
},
[](const block_state_ptr&) -> const producer_authority_schedule* { return nullptr; }
});
}
bool is_protocol_feature_activated(const digest_type& digest) const {
const auto& activated_features = get_activated_protocol_features();
return (activated_features.find(digest) != activated_features.end());
}
const block_signing_authority& pending_block_signing_authority() const {
// this should never be called on completed_block because `controller::is_building_block()` returns false
assert(0);
static block_signing_authority bsa; return bsa; // just so it builds
}
};
struct assembled_block {
// --------------------------------------------------------------------------------
struct assembled_block_legacy {
block_id_type id;
pending_block_header_state_legacy 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;
// Passed to completed_block, to be used by Legacy to Savanna transisition
std::optional<digests_t> action_receipt_digests_savanna;
};
// --------------------------------------------------------------------------------
struct assembled_block_if {
producer_authority active_producer_authority;
block_header_state bhs;
deque<transaction_metadata_ptr> trx_metas; // Comes from building_block::pending_trx_metas
// Carried over to put into block_state (optimization for fork reorgs)
deque<transaction_receipt> trx_receipts; // Comes from building_block::pending_trx_receipts
std::optional<valid_t> valid; // Comes from assemble_block
std::optional<quorum_certificate> qc; // QC to add as block extension to new block
digest_type action_mroot;
block_header_state& get_bhs() { return bhs; }
};
std::variant<assembled_block_legacy, assembled_block_if> v;
template <class R, class F>
R apply_legacy(F&& f) {
if constexpr (std::is_same_v<void, R>)
std::visit(overloaded{[&](assembled_block_legacy& ab) { std::forward<F>(f)(ab); },
[&](assembled_block_if& ab) {}}, v);
else
return std::visit(overloaded{[&](assembled_block_legacy& ab) -> R { return std::forward<F>(f)(ab); },
[&](assembled_block_if& ab) -> R { return {}; }}, v);
}
deque<transaction_metadata_ptr> extract_trx_metas() {
return std::visit([](auto& ab) { return std::move(ab.trx_metas); }, v);
}
bool is_protocol_feature_activated(const digest_type& digest) const {
// 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 block_id_type& id() const {
return std::visit(
overloaded{[](const assembled_block_legacy& ab) -> const block_id_type& { return ab.id; },
[](const assembled_block_if& ab) -> const block_id_type& { return ab.bhs.id(); }},
v);
}
block_timestamp_type timestamp() const {
return std::visit(
overloaded{[](const assembled_block_legacy& ab) { return ab.pending_block_header_state.timestamp; },
[](const assembled_block_if& ab) { return ab.bhs.header.timestamp; }},
v);
}
uint32_t block_num() const {
return std::visit(
overloaded{[](const assembled_block_legacy& ab) { return ab.pending_block_header_state.block_num; },
[](const assembled_block_if& ab) { return ab.bhs.block_num(); }},
v);
}
account_name producer() const {
return std::visit(
overloaded{[](const assembled_block_legacy& ab) { return ab.pending_block_header_state.producer; },
[](const assembled_block_if& ab) { return ab.active_producer_authority.producer_name; }},
v);
}
const block_header& header() const {
return std::visit(
overloaded{[](const assembled_block_legacy& ab) -> const block_header& { return *ab.unsigned_block; },
[](const assembled_block_if& ab) -> const block_header& { return ab.bhs.header; }},
v);
}
const producer_authority_schedule& active_producers() const {
return std::visit(overloaded{[](const assembled_block_legacy& ab) -> const producer_authority_schedule& {
return ab.pending_block_header_state.active_schedule;
},
[](const assembled_block_if& ab) -> const producer_authority_schedule& {
return ab.bhs.active_schedule_auth();
}},
v);
}
std::optional<digests_t> get_action_receipt_digests_savanna() const {
return std::visit(
overloaded{[](const assembled_block_legacy& ab) -> std::optional<digests_t> { return ab.action_receipt_digests_savanna; },
[](const assembled_block_if& ab) -> std::optional<digests_t> { return {}; }},
v);
}
const producer_authority_schedule* next_producers() const {
return std::visit(overloaded{[](const assembled_block_legacy& ab) -> const producer_authority_schedule* {
return ab.new_producer_authority_cache.has_value()
? &ab.new_producer_authority_cache.value()
: nullptr;
},
[](const assembled_block_if& ab) -> const producer_authority_schedule* {
return ab.bhs.proposer_policies.empty()
? nullptr
: &ab.bhs.proposer_policies.begin()->second->proposer_schedule;
}},
v);
}
const producer_authority_schedule* pending_producers_legacy() const {
return std::visit(
overloaded{[](const assembled_block_legacy& ab) -> const producer_authority_schedule* {
return ab.new_producer_authority_cache.has_value() ? &ab.new_producer_authority_cache.value()
: nullptr;
},
[](const assembled_block_if&) -> const producer_authority_schedule* { return nullptr; }},
v);
}
const block_signing_authority& pending_block_signing_authority() const {
return std::visit(overloaded{[](const assembled_block_legacy& ab) -> const block_signing_authority& {
return ab.pending_block_header_state.valid_block_signing_authority;
},
[](const assembled_block_if& ab) -> const block_signing_authority& {
return ab.active_producer_authority.authority;
}},
v);
}
completed_block complete_block(const protocol_feature_set& pfs, validator_t validator,
const signer_callback_type& signer, const block_signing_authority& valid_block_signing_authority) {
return std::visit(overloaded{[&](assembled_block_legacy& ab) {
auto bsp = std::make_shared<block_state_legacy>(
std::move(ab.pending_block_header_state), std::move(ab.unsigned_block),
std::move(ab.trx_metas), ab.action_receipt_digests_savanna, pfs, validator, signer);
return completed_block{block_handle{std::move(bsp)}};
},
[&](assembled_block_if& ab) {
auto bsp = std::make_shared<block_state>(ab.bhs, std::move(ab.trx_metas),
std::move(ab.trx_receipts), ab.valid, ab.qc, signer,
valid_block_signing_authority, ab.action_mroot);
return completed_block{block_handle{std::move(bsp)}};
}},
v);
}
};
struct building_block {
// --------------------------------------------------------------------------------
struct building_block_common {
using checksum_or_digests = std::variant<checksum256_type, digests_t>;
const 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;
checksum_or_digests trx_mroot_or_receipt_digests {digests_t{}};
action_digests_t action_receipt_digests;
trx_block_context trx_blk_context;
building_block_common(const vector<digest_type>& new_protocol_feature_activations,
action_digests_t::store_which_t store_which) :
new_protocol_feature_activations(new_protocol_feature_activations),
action_receipt_digests(store_which)
{
}
bool is_protocol_feature_activated(const digest_type& digest, const flat_set<digest_type>& activated_features) const {
if (activated_features.find(digest) != activated_features.end())
return true;
if (num_new_protocol_features_that_have_activated == 0)
return false;
auto end = new_protocol_feature_activations.begin() + num_new_protocol_features_that_have_activated;
return (std::find(new_protocol_feature_activations.begin(), end, digest) != end);
}
std::function<void()> make_block_restore_point() {
auto orig_trx_receipts_size = pending_trx_receipts.size();
auto orig_trx_metas_size = pending_trx_metas.size();
auto orig_trx_receipt_digests_size = std::holds_alternative<digests_t>(trx_mroot_or_receipt_digests) ?
std::get<digests_t>(trx_mroot_or_receipt_digests).size() : 0;
auto orig_action_receipt_digests_size = action_receipt_digests.size();
return [this,
orig_trx_receipts_size,
orig_trx_metas_size,
orig_trx_receipt_digests_size,
orig_action_receipt_digests_size]()
{
pending_trx_receipts.resize(orig_trx_receipts_size);
pending_trx_metas.resize(orig_trx_metas_size);
if (std::holds_alternative<digests_t>(trx_mroot_or_receipt_digests))
std::get<digests_t>(trx_mroot_or_receipt_digests).resize(orig_trx_receipt_digests_size);
action_receipt_digests.resize(orig_action_receipt_digests_size);
};
}
};
// --------------------------------------------------------------------------------
struct building_block_legacy : public building_block_common {
pending_block_header_state_legacy pending_block_header_state;
std::optional<producer_authority_schedule> new_pending_producer_schedule;
building_block_legacy( const block_header_state_legacy& prev,
block_timestamp_type when,
uint16_t num_prev_blocks_to_confirm,
const vector<digest_type>& new_protocol_feature_activations,
action_digests_t::store_which_t store_which)
: building_block_common(new_protocol_feature_activations, store_which),
pending_block_header_state(prev.next(when, num_prev_blocks_to_confirm))
{}
bool is_protocol_feature_activated(const digest_type& digest) const {
return building_block_common::is_protocol_feature_activated(
digest, pending_block_header_state.prev_activated_protocol_features->protocol_features);
}
uint32_t get_block_num() const { return pending_block_header_state.block_num; }
};
// --------------------------------------------------------------------------------
struct building_block_if : public building_block_common {
const block_state& parent;
const block_timestamp_type timestamp; // Comes from building_block_input::timestamp
const producer_authority active_producer_authority; // Comes from parent.get_scheduled_producer(timestamp)
const protocol_feature_activation_set_ptr prev_activated_protocol_features; // Cached: parent.activated_protocol_features()
const proposer_policy_ptr active_proposer_policy; // Cached: parent.get_next_active_proposer_policy(timestamp)
const uint32_t block_num; // Cached: parent.block_num() + 1
building_block_if(const block_state& parent, const building_block_input& input, action_digests_t::store_which_t store_which)
: building_block_common(input.new_protocol_feature_activations, store_which)
, parent (parent)
, timestamp(input.timestamp)
, active_producer_authority{input.producer,
[&]() -> block_signing_authority {
const auto& pas = parent.active_proposer_policy->proposer_schedule;
for (const auto& pa : pas.producers)
if (pa.producer_name == input.producer)
return pa.authority;
assert(0); // we should find the authority
return {};
}()}
, prev_activated_protocol_features(parent.activated_protocol_features)
, active_proposer_policy(parent.active_proposer_policy)
, block_num(parent.block_num() + 1) {}
bool is_protocol_feature_activated(const digest_type& digest) const {
return building_block_common::is_protocol_feature_activated(digest, prev_activated_protocol_features->protocol_features);
}
uint32_t get_block_num() const { return block_num; }
// returns the next proposer schedule version if producers should be proposed in block
// if producers is not different then returns empty optional
std::optional<uint32_t> get_next_proposer_schedule_version(const std::vector<producer_authority>& producers) const {
assert(active_proposer_policy);
auto get_next_sched = [&]() -> const producer_authority_schedule& {
if (!parent.proposer_policies.empty()) { // proposed in-flight
// return the last proposed policy to use for comparison
return (--parent.proposer_policies.end())->second->proposer_schedule;
}
// none currently in-flight, use active
return active_proposer_policy->proposer_schedule;
};
const producer_authority_schedule& lhs = get_next_sched();
auto v = lhs.version;
if (lhs.producers != producers) {
++v;
return std::optional<uint32_t>{v};
}
return std::nullopt;
}
};
std::variant<building_block_legacy, building_block_if> v;
// legacy constructor
building_block(const block_header_state_legacy& prev, block_timestamp_type when, uint16_t num_prev_blocks_to_confirm,
const vector<digest_type>& new_protocol_feature_activations) :
v(building_block_legacy(prev, when, num_prev_blocks_to_confirm, new_protocol_feature_activations,
action_digests_t::store_which_t::both)) // [todo] should be both only when transition starts
{}
// if constructor
building_block(const block_state& prev, const building_block_input& input) :
v(building_block_if(prev, input, action_digests_t::store_which_t::savanna))
{}
// apply legacy, building_block_legacy
template <class R, class F>
R apply_l(F&& f) {
if constexpr (std::is_same_v<void, R>)
std::visit(overloaded{[&](building_block_legacy& bb) { std::forward<F>(f)(bb); },
[&](building_block_if&) {}}, v);
else
return std::visit(overloaded{[&](building_block_legacy& bb) -> R { return std::forward<F>(f)(bb); },
[&](building_block_if&) -> R { return {}; }}, v);
}
template <class R, class F>
R apply(F&& f) {
if constexpr (std::is_same_v<void, R>)
std::visit(overloaded{[&](building_block_legacy& bb) { std::forward<F>(f)(bb); },
[&](building_block_if& bb) { std::forward<F>(f)(bb); }}, v);
else
return std::visit(overloaded{[&](building_block_legacy& bb) -> R { return std::forward<F>(f)(bb); },
[&](building_block_if& bb) -> R { return std::forward<F>(f)(bb); }}, v);
}
template <class R, class F, class S>
R apply(F&& f, S&& s) {
if constexpr (std::is_same_v<void, R>)
std::visit(overloaded{[&](building_block_legacy& bb) { std::forward<F>(f)(bb); },
[&](building_block_if& bb) { std::forward<S>(s)(bb); }}, v);
else
return std::visit(overloaded{[&](building_block_legacy& bb) -> R { return std::forward<F>(f)(bb); },
[&](building_block_if& bb) -> R { return std::forward<S>(s)(bb); }}, v);
}
deque<transaction_metadata_ptr> extract_trx_metas() {
return std::visit([](auto& bb) { return std::move(bb.pending_trx_metas); }, v);
}
bool is_protocol_feature_activated(const digest_type& digest) const {
return std::visit([&digest](const auto& bb) { return bb.is_protocol_feature_activated(digest); }, v);
}
std::function<void()> make_block_restore_point() {
return std::visit([](auto& bb) { return bb.make_block_restore_point(); }, v);
}
uint32_t block_num() const {
return std::visit([](const auto& bb) { return bb.get_block_num(); }, v);
}
block_timestamp_type timestamp() const {
return std::visit(
overloaded{[](const building_block_legacy& bb) { return bb.pending_block_header_state.timestamp; },
[](const building_block_if& bb) { return bb.timestamp; }},
v);
}
account_name producer() const {
return std::visit(
overloaded{[](const building_block_legacy& bb) { return bb.pending_block_header_state.producer; },
[](const building_block_if& bb) { return bb.active_producer_authority.producer_name; }},
v);
}
const vector<digest_type>& new_protocol_feature_activations() {
return std::visit([](auto& bb) -> const vector<digest_type>& { return bb.new_protocol_feature_activations; }, v);
}
const block_signing_authority& pending_block_signing_authority() const {
return std::visit(overloaded{[](const building_block_legacy& bb) -> const block_signing_authority& {
return bb.pending_block_header_state.valid_block_signing_authority;
},
[](const building_block_if& bb) -> const block_signing_authority& {
return bb.active_producer_authority.authority;
}},
v);
}
std::optional<uint32_t> get_next_proposer_schedule_version(const std::vector<producer_authority>& producers) const {
return std::visit(
overloaded{[](const building_block_legacy&) -> std::optional<uint32_t> { return std::nullopt; },
[&](const building_block_if& bb) -> std::optional<uint32_t> {
return bb.get_next_proposer_schedule_version(producers);
}
},
v);
}
size_t& num_new_protocol_features_activated() {
return std::visit([](auto& bb) -> size_t& { return bb.num_new_protocol_features_that_have_activated; }, v);
}
deque<transaction_metadata_ptr>& pending_trx_metas() {
return std::visit([](auto& bb) -> deque<transaction_metadata_ptr>& { return bb.pending_trx_metas; }, v);
}
deque<transaction_receipt>& pending_trx_receipts() {
return std::visit([](auto& bb) -> deque<transaction_receipt>& { return bb.pending_trx_receipts; }, v);
}
building_block_common::checksum_or_digests& trx_mroot_or_receipt_digests() {
return std::visit(
[](auto& bb) -> building_block_common::checksum_or_digests& { return bb.trx_mroot_or_receipt_digests; }, v);
}
action_digests_t& action_receipt_digests() {
return std::visit([](auto& bb) -> action_digests_t& { return bb.action_receipt_digests; }, v);
}
const producer_authority_schedule& active_producers() const {
return std::visit(overloaded{[](const building_block_legacy& bb) -> const producer_authority_schedule& {
return bb.pending_block_header_state.active_schedule;
},
[](const building_block_if& bb) -> const producer_authority_schedule& {
return bb.active_proposer_policy->proposer_schedule;
}},
v);
}
const producer_authority_schedule* next_producers() const {
return std::visit(overloaded{[](const building_block_legacy& bb) -> const producer_authority_schedule* {
if (bb.new_pending_producer_schedule)
return &bb.new_pending_producer_schedule.value();
return &bb.pending_block_header_state.prev_pending_schedule.schedule;
},
[](const building_block_if& bb) -> const producer_authority_schedule* {
if (!bb.parent.proposer_policies.empty())
return &bb.parent.proposer_policies.begin()->second->proposer_schedule;
return nullptr;
}},
v);
}
const producer_authority_schedule* pending_producers_legacy() const {
return std::visit(overloaded{[](const building_block_legacy& bb) -> const producer_authority_schedule* {
if (bb.new_pending_producer_schedule)
return &bb.new_pending_producer_schedule.value();
return &bb.pending_block_header_state.prev_pending_schedule.schedule;
},
[](const building_block_if&) -> const producer_authority_schedule* {
return nullptr;
}},
v);
}
qc_data_t get_qc_data(fork_database& fork_db, const block_state& parent) {
// find most recent ancestor block that has a QC by traversing fork db
// branch from parent
return fork_db.apply_s<qc_data_t>([&](const auto& forkdb) {
auto branch = forkdb.fetch_branch(parent.id());
for( auto it = branch.begin(); it != branch.end(); ++it ) {
if( auto qc = (*it)->get_best_qc(); qc ) {
EOS_ASSERT( qc->block_num <= block_header::num_from_id(parent.id()), block_validate_exception,
"most recent ancestor QC block number (${a}) cannot be greater than parent's block number (${p})",
("a", qc->block_num)("p", block_header::num_from_id(parent.id())) );
auto qc_claim = qc->to_qc_claim();
if( parent.is_needed(qc_claim) ) {
return qc_data_t{ *qc, qc_claim };
} else {
// no new qc info, repeat existing
return qc_data_t{ {}, parent.core.latest_qc_claim() };
}
}
}
// This only happens when parent block is the IF genesis block or starting from snapshot.
// There is no ancestor block which has a QC. Construct a default QC claim.
return qc_data_t{ {}, parent.core.latest_qc_claim() };
});
}
assembled_block assemble_block(boost::asio::io_context& ioc,
const protocol_feature_set& pfs,
fork_database& fork_db,
std::optional<proposer_policy> new_proposer_policy,
std::optional<finalizer_policy> new_finalizer_policy,
bool validating,
std::optional<qc_data_t> validating_qc_data,
const block_state_ptr& validating_bsp) {
auto& action_receipts = action_receipt_digests();
return std::visit(
overloaded{
[&](building_block_legacy& bb) -> assembled_block {
// compute the action_mroot and transaction_mroot
auto [transaction_mroot, action_mroot] = std::visit(
overloaded{[&](digests_t& trx_receipts) { // calculate the two merkle roots in separate threads
auto trx_merkle_fut =
post_async_task(ioc, [&]() { return calculate_merkle_legacy(std::move(trx_receipts)); });
auto action_merkle_fut =
post_async_task(ioc, [&]() { return calculate_merkle_legacy(std::move(*action_receipts.digests_l)); });
return std::make_pair(trx_merkle_fut.get(), action_merkle_fut.get());
},
[&](const checksum256_type& trx_checksum) {
return std::make_pair(trx_checksum, calculate_merkle_legacy(std::move(*action_receipts.digests_l)));
}},
trx_mroot_or_receipt_digests());
if (validating_qc_data) {
bb.pending_block_header_state.qc_claim = validating_qc_data->qc_claim;
}
// in dpos, we create a signed_block here. In IF mode, we do it later (when we are ready to sign it)
auto block_ptr = std::make_shared<signed_block>(bb.pending_block_header_state.make_block_header(
transaction_mroot, action_mroot, bb.new_pending_producer_schedule, std::move(new_finalizer_policy),
vector<digest_type>(bb.new_protocol_feature_activations), pfs));
block_ptr->transactions = std::move(bb.pending_trx_receipts);
return assembled_block{
.v = assembled_block::assembled_block_legacy{block_ptr->calculate_id(),
std::move(bb.pending_block_header_state),
std::move(bb.pending_trx_metas), std::move(block_ptr),
std::move(bb.new_pending_producer_schedule),
std::move(bb.action_receipt_digests.digests_s)}
};
},
[&](building_block_if& bb) -> assembled_block {
// compute the action_mroot and transaction_mroot
auto [transaction_mroot, action_mroot] = std::visit(
overloaded{[&](digests_t& trx_receipts) {
// calculate_merkle takes 3.2ms for 50,000 digests (legacy version took 11.1ms)
return std::make_pair(calculate_merkle(trx_receipts),
calculate_merkle(*action_receipts.digests_s));
},
[&](const checksum256_type& trx_checksum) {
return std::make_pair(trx_checksum,
calculate_merkle(*action_receipts.digests_s));
}},
trx_mroot_or_receipt_digests());
qc_data_t qc_data;
digest_type finality_mroot_claim;
if (validating) {
// we are simulating a block received from the network. Use the embedded qc from the block
assert(validating_qc_data);
qc_data = *validating_qc_data;
assert(validating_bsp);
// Use the action_mroot from raceived block's header for
// finality_mroot_claim at the first stage such that the next
// block's header and block id can be built. The actual
// finality_mroot will be validated by apply_block at the
// second stage
finality_mroot_claim = validating_bsp->header.action_mroot;
} else {
qc_data = get_qc_data(fork_db, bb.parent);;
finality_mroot_claim = bb.parent.get_finality_mroot_claim(qc_data.qc_claim);
}
building_block_input bb_input {
.parent_id = bb.parent.id(),
.parent_timestamp = bb.parent.timestamp(),
.timestamp = timestamp(),
.producer = producer(),
.new_protocol_feature_activations = new_protocol_feature_activations()
};
block_header_state_input bhs_input{
bb_input,
transaction_mroot,
std::move(new_proposer_policy),
std::move(new_finalizer_policy),
qc_data.qc_claim,
finality_mroot_claim
};
auto bhs = bb.parent.next(bhs_input);
std::optional<valid_t> valid; // used for producing
if (validating) {
// Create the valid structure for validating_bsp if it does not
// have one.
if (!validating_bsp->valid) {
validating_bsp->valid = bb.parent.new_valid(bhs, action_mroot, validating_bsp->strong_digest);
validating_bsp->action_mroot = action_mroot; // caching for constructing finality_data. Only needed when block is commited.
}
} else {
// Create the valid structure for producing
valid = bb.parent.new_valid(bhs, action_mroot, bhs.compute_finality_digest());
}
assembled_block::assembled_block_if ab{
bb.active_producer_authority,
bhs,
std::move(bb.pending_trx_metas),
std::move(bb.pending_trx_receipts),
valid,
qc_data.qc,
action_mroot // caching for constructing finality_data.
};
return assembled_block{.v = std::move(ab)};
}},
v);
}
};
using block_stage_type = std::variant<building_block, assembled_block, completed_block>;
struct pending_state {
maybe_session _db_session;
block_stage_type _block_stage;
controller::block_status _block_status = controller::block_status::ephemeral;
std::optional<block_id_type> _producer_block_id;
controller::block_report _block_report{};
pending_state(maybe_session&& s,
const block_header_state_legacy& 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))
{}
pending_state(maybe_session&& s,
const block_state& prev,
const building_block_input& input) :
_db_session(std::move(s)),
_block_stage(building_block(prev, input))
{}
deque<transaction_metadata_ptr> extract_trx_metas() {
return std::visit([](auto& stage) { return stage.extract_trx_metas(); }, _block_stage);
}
bool is_protocol_feature_activated(const digest_type& digest) const {
return std::visit([&](const auto& stage) { return stage.is_protocol_feature_activated(digest); }, _block_stage);
}
block_timestamp_type timestamp() const {
return std::visit([](const auto& stage) { return stage.timestamp(); }, _block_stage);
}
uint32_t block_num() const {
return std::visit([](const auto& stage) { return stage.block_num(); }, _block_stage);
}
account_name producer() const {
return std::visit([](const auto& stage) { return stage.producer(); }, _block_stage);
}
void push() {
_db_session.push();
}
const block_signing_authority& pending_block_signing_authority() const {
return std::visit(
[](const auto& stage) -> const block_signing_authority& { return stage.pending_block_signing_authority(); },
_block_stage);
}
const producer_authority_schedule& active_producers() const {
return std::visit(
[](const auto& stage) -> const producer_authority_schedule& { return stage.active_producers(); },
_block_stage);
}
const producer_authority_schedule* pending_producers_legacy() const {
return std::visit(
[](const auto& stage) -> const producer_authority_schedule* { return stage.pending_producers_legacy(); },
_block_stage);
}
const producer_authority_schedule* next_producers()const {
return std::visit(
[](const auto& stage) -> const producer_authority_schedule* { return stage.next_producers(); },
_block_stage);
}
std::optional<uint32_t> get_next_proposer_schedule_version(const std::vector<producer_authority>& producers) const {
return std::visit(overloaded{
[&](const building_block& stage) -> std::optional<uint32_t> {
return stage.get_next_proposer_schedule_version(producers);
},
[](const assembled_block&) -> std::optional<uint32_t> { assert(false); return std::nullopt; },
[](const completed_block&) -> std::optional<uint32_t> { assert(false); return std::nullopt; }
},
_block_stage);
}
};
struct controller_impl {
enum class app_window_type {
write, // Only main thread is running; read-only threads are not running.
// All read-write and read-only tasks are sequentially executed.
read // Main thread and read-only threads are running read-ony tasks in parallel.
// Read-write tasks are not being executed.
};
#if LLVM_VERSION_MAJOR < 9
// LLVM versions prior to 9 do a set_new_handler() in a static global initializer. Reset LLVM's custom handler
// back to the default behavior of throwing bad_alloc so we can possibly exit cleanly and not just abort as LLVM's
// handler does.
// LLVM 9+ doesn't install this handler unless calling InitLLVM(), which we don't.
// See https://reviews.llvm.org/D64505
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
#endif
controller& self;
std::function<void()> shutdown;
std::function<bool()> check_shutdown;
chainbase::database db;
block_log blog;
std::optional<pending_state> pending;
block_handle chain_head;
block_state_ptr chain_head_trans_svnn_block; // chain_head's Savanna representation during transition
fork_database fork_db;
large_atomic<block_id_type> if_irreversible_block_id;
resource_limits_manager resource_limits;
subjective_billing subjective_bill;
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
bool replaying = false;
bool is_producer_node = false; // true if node is configured as a block producer
db_read_mode read_mode = db_read_mode::HEAD;
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 trusted_producer_light_validation = false;
uint32_t snapshot_head_block = 0;
struct chain; // chain is a namespace so use an embedded type for the named_thread_pool tag
named_thread_pool<chain> thread_pool;
deep_mind_handler* deep_mind_logger = nullptr;
bool okay_to_print_integrity_hash_on_stop = false;
my_finalizers_t my_finalizers;
std::atomic<bool> writing_snapshot = false;
thread_local static platform_timer timer; // a copy for main thread and each read-only thread
#if defined(EOSIO_EOS_VM_RUNTIME_ENABLED) || defined(EOSIO_EOS_VM_JIT_RUNTIME_ENABLED)
thread_local static vm::wasm_allocator wasm_alloc; // a copy for main thread and each read-only thread
#endif
wasm_interface wasmif;
app_window_type app_window = app_window_type::write;
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;
signal<void(uint32_t)> block_start;
signal<void(const block_signal_params&)> accepted_block_header;
signal<void(const block_signal_params&)> accepted_block;
signal<void(const block_signal_params&)> irreversible_block;
signal<void(std::tuple<const transaction_trace_ptr&, const packed_transaction_ptr&>)> applied_transaction;
vote_signal_t voted_block;
vote_processor_t vote_processor{voted_block,
[this](const block_id_type& id) -> block_state_ptr {
return fork_db.apply_s<block_state_ptr>([&](const auto& forkdb) {
return forkdb.get_block(id);
});
}};
int64_t set_proposed_producers_legacy( vector<producer_authority> producers );
protocol_feature_activation_set_ptr head_activated_protocol_features() const {
return apply<protocol_feature_activation_set_ptr>(chain_head, [](const auto& head) {
return head->get_activated_protocol_features();
});
}
const producer_authority_schedule& head_active_schedule_auth() const {
return apply<const producer_authority_schedule&>(chain_head, [](const auto& head) -> const producer_authority_schedule& {
return head->active_schedule_auth();
});
}