-
Notifications
You must be signed in to change notification settings - Fork 795
/
Copy pathactive_transactions.cpp
1390 lines (1257 loc) · 55.1 KB
/
active_transactions.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 <nano/lib/threading.hpp>
#include <nano/node/active_transactions.hpp>
#include <nano/node/confirmation_height_processor.hpp>
#include <nano/node/confirmation_solicitor.hpp>
#include <nano/node/election.hpp>
#include <nano/node/node.hpp>
#include <nano/node/repcrawler.hpp>
#include <nano/secure/blockstore.hpp>
#include <boost/format.hpp>
#include <boost/variant/get.hpp>
#include <numeric>
using namespace std::chrono;
size_t constexpr nano::active_transactions::max_active_elections_frontier_insertion;
constexpr std::chrono::minutes nano::active_transactions::expired_optimistic_election_info_cutoff;
nano::active_transactions::active_transactions (nano::node & node_a, nano::confirmation_height_processor & confirmation_height_processor_a) :
scheduler{ node_a.scheduler }, // Move dependencies requiring this circular reference
confirmation_height_processor{ confirmation_height_processor_a },
node{ node_a },
generator{ node_a.config, node_a.ledger, node_a.wallets, node_a.vote_processor, node_a.history, node_a.network, node_a.stats, false },
final_generator{ node_a.config, node_a.ledger, node_a.wallets, node_a.vote_processor, node_a.history, node_a.network, node_a.stats, true },
election_time_to_live{ node_a.network_params.network.is_dev_network () ? 0s : 2s },
thread ([this] () {
nano::thread_role::set (nano::thread_role::name::request_loop);
request_loop ();
})
{
// Register a callback which will get called after a block is cemented
confirmation_height_processor.add_cemented_observer ([this] (std::shared_ptr<nano::block> const & callback_block_a) {
this->block_cemented_callback (callback_block_a);
});
// Register a callback which will get called if a block is already cemented
confirmation_height_processor.add_block_already_cemented_observer ([this] (nano::block_hash const & hash_a) {
this->block_already_cemented_callback (hash_a);
});
nano::unique_lock<nano::mutex> lock (mutex);
condition.wait (lock, [&started = started] { return started; });
}
nano::active_transactions::~active_transactions ()
{
stop ();
}
bool nano::active_transactions::insert_election_from_frontiers_confirmation (std::shared_ptr<nano::block> const & block_a, nano::account const & account_a, nano::uint128_t previous_balance_a, nano::election_behavior election_behavior_a)
{
bool inserted{ false };
nano::unique_lock<nano::mutex> lock (mutex);
if (roots.get<tag_root> ().find (block_a->qualified_root ()) == roots.get<tag_root> ().end ())
{
std::function<void (std::shared_ptr<nano::block> const &)> election_confirmation_cb;
if (election_behavior_a == nano::election_behavior::optimistic)
{
election_confirmation_cb = [this] (std::shared_ptr<nano::block> const & block_a) {
--optimistic_elections_count;
};
}
auto insert_result = insert_impl (lock, block_a, previous_balance_a, election_behavior_a, election_confirmation_cb);
inserted = insert_result.inserted;
if (inserted)
{
insert_result.election->transition_active ();
if (insert_result.election->optimistic ())
{
++optimistic_elections_count;
}
}
}
return inserted;
}
nano::frontiers_confirmation_info nano::active_transactions::get_frontiers_confirmation_info ()
{
// Limit maximum count of elections to start
auto rep_counts (node.wallets.reps ());
bool representative (node.config.enable_voting && rep_counts.voting > 0);
bool half_princpal_representative (representative && rep_counts.half_principal > 0);
/* Check less frequently for regular nodes in auto mode */
bool agressive_mode (half_princpal_representative || node.config.frontiers_confirmation == nano::frontiers_confirmation_mode::always);
auto is_dev_network = node.network_params.network.is_dev_network ();
auto roots_size = size ();
auto check_time_exceeded = std::chrono::steady_clock::now () >= next_frontier_check;
auto max_elections = max_active_elections_frontier_insertion;
auto low_active_elections = roots_size < max_elections;
bool wallets_check_required = (!skip_wallets || !priority_wallet_cementable_frontiers.empty ()) && !agressive_mode;
// Minimise dropping real-time transactions, set the number of frontiers added to a factor of the maximum number of possible active elections
auto max_active = node.config.active_elections_size / 20;
if (roots_size <= max_active && (check_time_exceeded || wallets_check_required || (!is_dev_network && low_active_elections && agressive_mode)))
{
// When the number of active elections is low increase max number of elections for setting confirmation height.
if (max_active > roots_size + max_elections)
{
max_elections = max_active - roots_size;
}
}
else
{
max_elections = 0;
}
return nano::frontiers_confirmation_info{ max_elections, agressive_mode };
}
void nano::active_transactions::set_next_frontier_check (bool agressive_mode_a)
{
auto request_interval (std::chrono::milliseconds (node.network_params.network.request_interval_ms));
auto rel_time_next_frontier_check = request_interval * (agressive_mode_a ? 20 : 60);
// Decrease check time for dev network
int dev_network_factor = node.network_params.network.is_dev_network () ? 1000 : 1;
next_frontier_check = steady_clock::now () + (rel_time_next_frontier_check / dev_network_factor);
}
void nano::active_transactions::confirm_prioritized_frontiers (nano::transaction const & transaction_a, uint64_t max_elections_a, uint64_t & elections_count_a)
{
nano::unique_lock<nano::mutex> lk (mutex);
auto start_elections_for_prioritized_frontiers = [&transaction_a, &elections_count_a, max_elections_a, &lk, this] (prioritize_num_uncemented & cementable_frontiers) {
while (!cementable_frontiers.empty () && !this->stopped && elections_count_a < max_elections_a && optimistic_elections_count < max_optimistic ())
{
auto cementable_account_front_it = cementable_frontiers.get<tag_uncemented> ().begin ();
auto cementable_account = *cementable_account_front_it;
cementable_frontiers.get<tag_uncemented> ().erase (cementable_account_front_it);
if (expired_optimistic_election_infos.get<tag_account> ().count (cementable_account.account) == 0)
{
lk.unlock ();
nano::account_info info;
auto error = this->node.store.account_get (transaction_a, cementable_account.account, info);
if (!error)
{
if (!this->confirmation_height_processor.is_processing_block (info.head))
{
nano::confirmation_height_info confirmation_height_info;
this->node.store.confirmation_height_get (transaction_a, cementable_account.account, confirmation_height_info);
if (info.block_count > confirmation_height_info.height)
{
auto block (this->node.store.block_get (transaction_a, info.head));
auto previous_balance (this->node.ledger.balance (transaction_a, block->previous ()));
auto inserted_election = this->insert_election_from_frontiers_confirmation (block, cementable_account.account, previous_balance, nano::election_behavior::optimistic);
if (inserted_election)
{
++elections_count_a;
}
}
}
}
lk.lock ();
}
}
};
start_elections_for_prioritized_frontiers (priority_wallet_cementable_frontiers);
start_elections_for_prioritized_frontiers (priority_cementable_frontiers);
}
void nano::active_transactions::block_cemented_callback (std::shared_ptr<nano::block> const & block_a)
{
auto transaction = node.store.tx_begin_read ();
boost::optional<nano::election_status_type> election_status_type;
if (!confirmation_height_processor.is_processing_added_block (block_a->hash ()))
{
election_status_type = confirm_block (transaction, block_a);
}
else
{
// This block was explicitly added to the confirmation height_processor
election_status_type = nano::election_status_type::active_confirmed_quorum;
}
if (election_status_type.is_initialized ())
{
if (election_status_type == nano::election_status_type::inactive_confirmation_height)
{
nano::account account (0);
nano::uint128_t amount (0);
bool is_state_send (false);
nano::account pending_account (0);
node.process_confirmed_data (transaction, block_a, block_a->hash (), account, amount, is_state_send, pending_account);
node.observers.blocks.notify (nano::election_status{ block_a, 0, 0, std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now ().time_since_epoch ()), std::chrono::duration_values<std::chrono::milliseconds>::zero (), 0, 1, 0, nano::election_status_type::inactive_confirmation_height }, {}, account, amount, is_state_send);
}
else
{
auto hash (block_a->hash ());
nano::unique_lock<nano::mutex> election_winners_lk (election_winner_details_mutex);
auto existing (election_winner_details.find (hash));
if (existing != election_winner_details.end ())
{
auto election = existing->second;
election_winner_details.erase (hash);
election_winners_lk.unlock ();
if (election->confirmed () && election->winner ()->hash () == hash)
{
nano::unique_lock<nano::mutex> election_lk (election->mutex);
auto status_l = election->status;
election_lk.unlock ();
add_recently_cemented (status_l);
auto destination (block_a->link ().is_zero () ? block_a->destination () : block_a->link ().as_account ());
node.receive_confirmed (transaction, hash, destination);
nano::account account (0);
nano::uint128_t amount (0);
bool is_state_send (false);
nano::account pending_account (0);
node.process_confirmed_data (transaction, block_a, hash, account, amount, is_state_send, pending_account);
election_lk.lock ();
election->status.type = *election_status_type;
election->status.confirmation_request_count = election->confirmation_request_count;
status_l = election->status;
election_lk.unlock ();
auto votes (election->votes_with_weight ());
node.observers.blocks.notify (status_l, votes, account, amount, is_state_send);
if (amount > 0)
{
node.observers.account_balance.notify (account, false);
if (!pending_account.is_zero ())
{
node.observers.account_balance.notify (pending_account, true);
}
}
}
}
}
auto const & account (!block_a->account ().is_zero () ? block_a->account () : block_a->sideband ().account);
debug_assert (!account.is_zero ());
if (!node.ledger.cache.final_votes_confirmation_canary.load () && account == node.network_params.ledger.final_votes_canary_account && block_a->sideband ().height >= node.network_params.ledger.final_votes_canary_height)
{
node.ledger.cache.final_votes_confirmation_canary.store (true);
}
// Next-block activations are done after cementing hardcoded bootstrap count to allow confirming very large chains without interference
bool const cemented_bootstrap_count_reached{ node.ledger.cache.cemented_count >= node.ledger.bootstrap_weight_max_blocks };
// Next-block activations are only done for blocks with previously active elections
bool const was_active{ *election_status_type == nano::election_status_type::active_confirmed_quorum || *election_status_type == nano::election_status_type::active_confirmation_height };
// Activations are only done if there is not a large amount of active elections, ensuring frontier confirmation takes place
auto const low_active_elections = [this] { return this->size () < nano::active_transactions::max_active_elections_frontier_insertion / 2; };
if (cemented_bootstrap_count_reached && was_active && low_active_elections ())
{
// Start or vote for the next unconfirmed block
scheduler.activate (account, transaction);
// Start or vote for the next unconfirmed block in the destination account
auto const & destination (node.ledger.block_destination (transaction, *block_a));
if (!destination.is_zero () && destination != account)
{
scheduler.activate (destination, transaction);
}
}
}
}
void nano::active_transactions::add_election_winner_details (nano::block_hash const & hash_a, std::shared_ptr<nano::election> const & election_a)
{
nano::lock_guard<nano::mutex> guard (election_winner_details_mutex);
election_winner_details.emplace (hash_a, election_a);
}
void nano::active_transactions::remove_election_winner_details (nano::block_hash const & hash_a)
{
nano::lock_guard<nano::mutex> guard (election_winner_details_mutex);
election_winner_details.erase (hash_a);
}
void nano::active_transactions::block_already_cemented_callback (nano::block_hash const & hash_a)
{
// Depending on timing there is a situation where the election_winner_details is not reset.
// This can happen when a block wins an election, and the block is confirmed + observer
// called before the block hash gets added to election_winner_details. If the block is confirmed
// callbacks have already been done, so we can safely just remove it.
remove_election_winner_details (hash_a);
}
int64_t nano::active_transactions::vacancy () const
{
nano::lock_guard<nano::mutex> lock{ mutex };
auto result = static_cast<int64_t> (node.config.active_elections_size) - static_cast<int64_t> (roots.size ());
return result;
}
void nano::active_transactions::request_confirm (nano::unique_lock<nano::mutex> & lock_a)
{
debug_assert (lock_a.owns_lock ());
size_t const this_loop_target_l (roots.size ());
auto const elections_l{ list_active_impl (this_loop_target_l) };
lock_a.unlock ();
nano::confirmation_solicitor solicitor (node.network, node.config);
solicitor.prepare (node.rep_crawler.principal_representatives (std::numeric_limits<size_t>::max ()));
nano::vote_generator_session generator_session (generator);
nano::vote_generator_session final_generator_session (generator);
auto const election_ttl_cutoff_l (std::chrono::steady_clock::now () - election_time_to_live);
size_t unconfirmed_count_l (0);
nano::timer<std::chrono::milliseconds> elapsed (nano::timer_state::started);
/*
* Loop through active elections in descending order of proof-of-work difficulty, requesting confirmation
*
* Only up to a certain amount of elections are queued for confirmation request and block rebroadcasting. The remaining elections can still be confirmed if votes arrive
* Elections extending the soft config.active_elections_size limit are flushed after a certain time-to-live cutoff
* Flushed elections are later re-activated via frontier confirmation
*/
for (auto const & election_l : elections_l)
{
bool const confirmed_l (election_l->confirmed ());
if (election_l->transition_time (solicitor))
{
if (election_l->optimistic () && election_l->failed ())
{
if (election_l->confirmation_request_count != 0)
{
// Locks active mutex
add_expired_optimistic_election (*election_l);
}
--optimistic_elections_count;
}
// Locks active mutex, cleans up the election and erases it from the main container
erase (election_l->qualified_root);
}
}
solicitor.flush ();
generator_session.flush ();
final_generator_session.flush ();
lock_a.lock ();
if (node.config.logging.timing_logging ())
{
node.logger.try_log (boost::str (boost::format ("Processed %1% elections (%2% were already confirmed) in %3% %4%") % this_loop_target_l % (this_loop_target_l - unconfirmed_count_l) % elapsed.value ().count () % elapsed.unit ()));
}
}
void nano::active_transactions::cleanup_election (nano::unique_lock<nano::mutex> & lock_a, nano::election_cleanup_info const & info_a)
{
debug_assert (lock_a.owns_lock ());
if (!info_a.confirmed)
{
node.stats.inc (nano::stat::type::election, nano::stat::detail::election_drop);
}
for (auto const & [hash, block] : info_a.blocks)
{
auto erased (blocks.erase (hash));
(void)erased;
debug_assert (erased == 1);
erase_inactive_votes_cache (hash);
}
lock_a.unlock ();
for (auto const & [hash, block] : info_a.blocks)
{
// Notify observers about dropped elections & blocks lost confirmed elections
if (!info_a.confirmed || hash != info_a.winner)
{
node.observers.active_stopped.notify (hash);
}
if (!info_a.confirmed)
{
// Clear from publish filter
node.network.publish_filter.clear (block);
}
}
lock_a.lock ();
}
std::vector<std::shared_ptr<nano::election>> nano::active_transactions::list_active (size_t max_a)
{
nano::lock_guard<nano::mutex> guard (mutex);
return list_active_impl (max_a);
}
std::vector<std::shared_ptr<nano::election>> nano::active_transactions::list_active_impl (size_t max_a) const
{
std::vector<std::shared_ptr<nano::election>> result_l;
result_l.reserve (std::min (max_a, roots.size ()));
{
auto & sorted_roots_l (roots.get<tag_random_access> ());
size_t count_l{ 0 };
for (auto i = sorted_roots_l.begin (), n = sorted_roots_l.end (); i != n && count_l < max_a; ++i, ++count_l)
{
result_l.push_back (i->election);
}
}
return result_l;
}
void nano::active_transactions::add_expired_optimistic_election (nano::election const & election_a)
{
nano::lock_guard<nano::mutex> guard (mutex);
auto account = election_a.status.winner->account ();
if (account.is_zero ())
{
account = election_a.status.winner->sideband ().account;
}
auto it = expired_optimistic_election_infos.get<tag_account> ().find (account);
if (it != expired_optimistic_election_infos.get<tag_account> ().end ())
{
expired_optimistic_election_infos.get<tag_account> ().modify (it, [] (auto & expired_optimistic_election) {
expired_optimistic_election.expired_time = std::chrono::steady_clock::now ();
expired_optimistic_election.election_started = false;
});
}
else
{
expired_optimistic_election_infos.emplace (std::chrono::steady_clock::now (), account);
}
// Expire the oldest one if a maximum is reached
auto const max_expired_optimistic_election_infos = 10000;
if (expired_optimistic_election_infos.size () > max_expired_optimistic_election_infos)
{
expired_optimistic_election_infos.get<tag_expired_time> ().erase (expired_optimistic_election_infos.get<tag_expired_time> ().begin ());
}
expired_optimistic_election_infos_size = expired_optimistic_election_infos.size ();
}
unsigned nano::active_transactions::max_optimistic ()
{
return node.ledger.cache.cemented_count < node.ledger.bootstrap_weight_max_blocks ? std::numeric_limits<unsigned>::max () : 50u;
}
void nano::active_transactions::frontiers_confirmation (nano::unique_lock<nano::mutex> & lock_a)
{
// Spend some time prioritizing accounts with the most uncemented blocks to reduce voting traffic
auto request_interval = std::chrono::milliseconds (node.network_params.network.request_interval_ms);
// Spend longer searching ledger accounts when there is a low amount of elections going on
auto low_active = roots.size () < 1000;
auto time_to_spend_prioritizing_ledger_accounts = request_interval / (low_active ? 20 : 100);
auto time_to_spend_prioritizing_wallet_accounts = request_interval / 250;
auto time_to_spend_confirming_pessimistic_accounts = time_to_spend_prioritizing_ledger_accounts;
lock_a.unlock ();
auto transaction = node.store.tx_begin_read ();
prioritize_frontiers_for_confirmation (transaction, node.network_params.network.is_dev_network () ? std::chrono::milliseconds (50) : time_to_spend_prioritizing_ledger_accounts, time_to_spend_prioritizing_wallet_accounts);
auto frontiers_confirmation_info = get_frontiers_confirmation_info ();
if (frontiers_confirmation_info.can_start_elections ())
{
uint64_t elections_count (0);
confirm_prioritized_frontiers (transaction, frontiers_confirmation_info.max_elections, elections_count);
confirm_expired_frontiers_pessimistically (transaction, frontiers_confirmation_info.max_elections, elections_count);
set_next_frontier_check (frontiers_confirmation_info.aggressive_mode);
}
lock_a.lock ();
}
/*
* This function takes the expired_optimistic_election_infos generated from failed elections from frontiers confirmations and starts
* confirming blocks at cemented height + 1 (cemented frontier successor) for an account only if all dependent blocks already
* confirmed.
*/
void nano::active_transactions::confirm_expired_frontiers_pessimistically (nano::transaction const & transaction_a, uint64_t max_elections_a, uint64_t & elections_count_a)
{
auto i{ node.store.accounts_begin (transaction_a, next_frontier_account) };
auto n{ node.store.accounts_end () };
nano::timer<std::chrono::milliseconds> timer (nano::timer_state::started);
nano::confirmation_height_info confirmation_height_info;
// Loop through any expired optimistic elections which have not been started yet. This tag stores already started ones first
std::vector<nano::account> elections_started_for_account;
for (auto i = expired_optimistic_election_infos.get<tag_election_started> ().lower_bound (false); i != expired_optimistic_election_infos.get<tag_election_started> ().end ();)
{
if (stopped || elections_count_a >= max_elections_a)
{
break;
}
auto const & account{ i->account };
nano::account_info account_info;
bool should_delete{ true };
if (!node.store.account_get (transaction_a, account, account_info))
{
node.store.confirmation_height_get (transaction_a, account, confirmation_height_info);
if (account_info.block_count > confirmation_height_info.height)
{
should_delete = false;
std::shared_ptr<nano::block> previous_block;
std::shared_ptr<nano::block> block;
if (confirmation_height_info.height == 0)
{
block = node.store.block_get (transaction_a, account_info.open_block);
}
else
{
previous_block = node.store.block_get (transaction_a, confirmation_height_info.frontier);
block = node.store.block_get (transaction_a, previous_block->sideband ().successor);
}
if (block && !node.confirmation_height_processor.is_processing_block (block->hash ()) && node.ledger.dependents_confirmed (transaction_a, *block))
{
nano::uint128_t previous_balance{ 0 };
if (previous_block && previous_block->balance ().is_zero ())
{
previous_balance = previous_block->sideband ().balance.number ();
}
auto inserted_election = insert_election_from_frontiers_confirmation (block, account, previous_balance, nano::election_behavior::normal);
if (inserted_election)
{
++elections_count_a;
}
elections_started_for_account.push_back (i->account);
}
}
}
if (should_delete)
{
// This account is confirmed already or doesn't exist.
i = expired_optimistic_election_infos.get<tag_election_started> ().erase (i);
expired_optimistic_election_infos_size = expired_optimistic_election_infos.size ();
}
else
{
++i;
}
}
for (auto const & account : elections_started_for_account)
{
auto it = expired_optimistic_election_infos.get<tag_account> ().find (account);
debug_assert (it != expired_optimistic_election_infos.get<tag_account> ().end ());
expired_optimistic_election_infos.get<tag_account> ().modify (it, [] (auto & expired_optimistic_election_info_a) {
expired_optimistic_election_info_a.election_started = true;
});
}
}
bool nano::active_transactions::should_do_frontiers_confirmation () const
{
auto pending_confirmation_height_size (confirmation_height_processor.awaiting_processing_size ());
auto disabled_confirmation_mode = (node.config.frontiers_confirmation == nano::frontiers_confirmation_mode::disabled);
auto conf_height_capacity_reached = pending_confirmation_height_size > confirmed_frontiers_max_pending_size;
auto all_cemented = node.ledger.cache.block_count == node.ledger.cache.cemented_count;
return (!disabled_confirmation_mode && !conf_height_capacity_reached && !all_cemented);
}
void nano::active_transactions::request_loop ()
{
nano::unique_lock<nano::mutex> lock (mutex);
started = true;
lock.unlock ();
condition.notify_all ();
// The wallets and active_transactions objects are mutually dependent, so we need a fully
// constructed node before proceeding.
this->node.node_initialized_latch.wait ();
lock.lock ();
while (!stopped && !node.flags.disable_request_loop)
{
// If many votes are queued, ensure at least the currently active ones finish processing
lock.unlock ();
if (node.vote_processor.half_full ())
{
node.vote_processor.flush_active ();
}
lock.lock ();
const auto stamp_l = std::chrono::steady_clock::now ();
request_confirm (lock);
if (!stopped)
{
const auto min_sleep_l = std::chrono::milliseconds (node.network_params.network.request_interval_ms / 2);
const auto wakeup_l = std::max (stamp_l + std::chrono::milliseconds (node.network_params.network.request_interval_ms), std::chrono::steady_clock::now () + min_sleep_l);
condition.wait_until (lock, wakeup_l, [&wakeup_l, &stopped = stopped] { return stopped || std::chrono::steady_clock::now () >= wakeup_l; });
}
}
}
bool nano::active_transactions::prioritize_account_for_confirmation (nano::active_transactions::prioritize_num_uncemented & cementable_frontiers_a, size_t & cementable_frontiers_size_a, nano::account const & account_a, nano::account_info const & info_a, uint64_t confirmation_height_a)
{
auto inserted_new{ false };
if (info_a.block_count > confirmation_height_a && !confirmation_height_processor.is_processing_block (info_a.head))
{
auto num_uncemented = info_a.block_count - confirmation_height_a;
nano::lock_guard<nano::mutex> guard (mutex);
auto it = cementable_frontiers_a.get<tag_account> ().find (account_a);
if (it != cementable_frontiers_a.get<tag_account> ().end ())
{
if (it->blocks_uncemented != num_uncemented)
{
// Account already exists and there is now a different uncemented block count so update it in the container
cementable_frontiers_a.get<tag_account> ().modify (it, [num_uncemented] (nano::cementable_account & info) {
info.blocks_uncemented = num_uncemented;
});
}
}
else
{
debug_assert (cementable_frontiers_size_a <= max_priority_cementable_frontiers);
if (cementable_frontiers_size_a == max_priority_cementable_frontiers)
{
// The maximum amount of frontiers stored has been reached. Check if the current frontier
// has more uncemented blocks than the lowest uncemented frontier in the collection if so replace it.
auto least_uncemented_frontier_it = cementable_frontiers_a.get<tag_uncemented> ().end ();
--least_uncemented_frontier_it;
if (num_uncemented > least_uncemented_frontier_it->blocks_uncemented)
{
cementable_frontiers_a.get<tag_uncemented> ().erase (least_uncemented_frontier_it);
cementable_frontiers_a.get<tag_account> ().emplace (account_a, num_uncemented);
}
}
else
{
inserted_new = true;
cementable_frontiers_a.get<tag_account> ().emplace (account_a, num_uncemented);
}
}
cementable_frontiers_size_a = cementable_frontiers_a.size ();
}
return inserted_new;
}
void nano::active_transactions::prioritize_frontiers_for_confirmation (nano::transaction const & transaction_a, std::chrono::milliseconds ledger_account_traversal_max_time_a, std::chrono::milliseconds wallet_account_traversal_max_time_a)
{
// Don't try to prioritize when there are a large number of pending confirmation heights as blocks can be cemented in the meantime, making the prioritization less reliable
if (confirmation_height_processor.awaiting_processing_size () < confirmed_frontiers_max_pending_size)
{
size_t priority_cementable_frontiers_size;
size_t priority_wallet_cementable_frontiers_size;
{
nano::lock_guard<nano::mutex> guard (mutex);
priority_cementable_frontiers_size = priority_cementable_frontiers.size ();
priority_wallet_cementable_frontiers_size = priority_wallet_cementable_frontiers.size ();
}
nano::timer<std::chrono::milliseconds> wallet_account_timer (nano::timer_state::started);
// Remove any old expired optimistic elections so they are no longer excluded in subsequent checks
auto expired_cutoff_it (expired_optimistic_election_infos.get<tag_expired_time> ().lower_bound (std::chrono::steady_clock::now () - expired_optimistic_election_info_cutoff));
expired_optimistic_election_infos.get<tag_expired_time> ().erase (expired_optimistic_election_infos.get<tag_expired_time> ().begin (), expired_cutoff_it);
expired_optimistic_election_infos_size = expired_optimistic_election_infos.size ();
auto num_new_inserted{ 0u };
auto should_iterate = [this, &num_new_inserted] () {
auto max_optimistic_l = max_optimistic ();
return !stopped && (max_optimistic_l > optimistic_elections_count && max_optimistic_l - optimistic_elections_count > num_new_inserted);
};
if (!skip_wallets)
{
// Prioritize wallet accounts first
{
nano::lock_guard<nano::mutex> lock (node.wallets.mutex);
auto wallet_transaction (node.wallets.tx_begin_read ());
auto const & items = node.wallets.items;
if (items.empty ())
{
skip_wallets = true;
}
for (auto item_it = items.cbegin (); item_it != items.cend () && should_iterate (); ++item_it)
{
// Skip this wallet if it has been traversed already while there are others still awaiting
if (wallet_ids_already_iterated.find (item_it->first) != wallet_ids_already_iterated.end ())
{
continue;
}
nano::account_info info;
auto & wallet (item_it->second);
nano::lock_guard<std::recursive_mutex> wallet_lock (wallet->store.mutex);
auto & next_wallet_frontier_account = next_wallet_id_accounts.emplace (item_it->first, wallet_store::special_count).first->second;
auto i (wallet->store.begin (wallet_transaction, next_wallet_frontier_account));
auto n (wallet->store.end ());
for (; i != n && should_iterate (); ++i)
{
auto const & account (i->first);
if (expired_optimistic_election_infos.get<tag_account> ().count (account) == 0 && !node.store.account_get (transaction_a, account, info))
{
nano::confirmation_height_info confirmation_height_info;
node.store.confirmation_height_get (transaction_a, account, confirmation_height_info);
// If it exists in normal priority collection delete from there.
auto it = priority_cementable_frontiers.find (account);
if (it != priority_cementable_frontiers.end ())
{
nano::lock_guard<nano::mutex> guard (mutex);
priority_cementable_frontiers.erase (it);
priority_cementable_frontiers_size = priority_cementable_frontiers.size ();
}
auto insert_newed = prioritize_account_for_confirmation (priority_wallet_cementable_frontiers, priority_wallet_cementable_frontiers_size, account, info, confirmation_height_info.height);
if (insert_newed)
{
++num_new_inserted;
}
if (wallet_account_timer.since_start () >= wallet_account_traversal_max_time_a)
{
break;
}
}
next_wallet_frontier_account = account.number () + 1;
}
// Go back to the beginning when we have reached the end of the wallet accounts for this wallet
if (i == n)
{
wallet_ids_already_iterated.emplace (item_it->first);
next_wallet_id_accounts.at (item_it->first) = wallet_store::special_count;
// Skip wallet accounts when they have all been traversed
if (std::next (item_it) == items.cend ())
{
wallet_ids_already_iterated.clear ();
skip_wallets = true;
}
}
}
}
}
nano::timer<std::chrono::milliseconds> timer (nano::timer_state::started);
auto i (node.store.accounts_begin (transaction_a, next_frontier_account));
auto n (node.store.accounts_end ());
for (; i != n && should_iterate (); ++i)
{
auto const & account (i->first);
auto const & info (i->second);
if (priority_wallet_cementable_frontiers.find (account) == priority_wallet_cementable_frontiers.end ())
{
if (expired_optimistic_election_infos.get<tag_account> ().count (account) == 0)
{
nano::confirmation_height_info confirmation_height_info;
node.store.confirmation_height_get (transaction_a, account, confirmation_height_info);
auto insert_newed = prioritize_account_for_confirmation (priority_cementable_frontiers, priority_cementable_frontiers_size, account, info, confirmation_height_info.height);
if (insert_newed)
{
++num_new_inserted;
}
}
}
next_frontier_account = account.number () + 1;
if (timer.since_start () >= ledger_account_traversal_max_time_a)
{
break;
}
}
// Go back to the beginning when we have reached the end of the accounts and start with wallet accounts next time
if (i == n)
{
next_frontier_account = 0;
skip_wallets = false;
}
}
}
void nano::active_transactions::stop ()
{
nano::unique_lock<nano::mutex> lock (mutex);
if (!started)
{
condition.wait (lock, [&started = started] { return started; });
}
stopped = true;
lock.unlock ();
condition.notify_all ();
if (thread.joinable ())
{
thread.join ();
}
generator.stop ();
final_generator.stop ();
lock.lock ();
roots.clear ();
}
nano::election_insertion_result nano::active_transactions::insert_impl (nano::unique_lock<nano::mutex> & lock_a, std::shared_ptr<nano::block> const & block_a, boost::optional<nano::uint128_t> const & previous_balance_a, nano::election_behavior election_behavior_a, std::function<void (std::shared_ptr<nano::block> const &)> const & confirmation_action_a)
{
debug_assert (lock_a.owns_lock ());
debug_assert (block_a->has_sideband ());
nano::election_insertion_result result;
if (!stopped)
{
auto root (block_a->qualified_root ());
auto existing (roots.get<tag_root> ().find (root));
if (existing == roots.get<tag_root> ().end ())
{
if (recently_confirmed.get<tag_root> ().find (root) == recently_confirmed.get<tag_root> ().end ())
{
result.inserted = true;
auto hash (block_a->hash ());
auto epoch (block_a->sideband ().details.epoch);
nano::uint128_t previous_balance (previous_balance_a.value_or (0));
debug_assert (!(previous_balance_a.value_or (0) > 0 && block_a->previous ().is_zero ()));
if (!previous_balance_a.is_initialized () && !block_a->previous ().is_zero ())
{
auto transaction (node.store.tx_begin_read ());
if (node.store.block_exists (transaction, block_a->previous ()))
{
previous_balance = node.ledger.balance (transaction, block_a->previous ());
}
}
result.election = nano::make_shared<nano::election> (
node, block_a, confirmation_action_a, [&node = node] (auto const & rep_a) {
// Representative is defined as online if replying to live votes or rep_crawler queries
node.online_reps.observe (rep_a);
},
election_behavior_a);
roots.get<tag_root> ().emplace (nano::active_transactions::conflict_info{ root, result.election, epoch, previous_balance });
blocks.emplace (hash, result.election);
auto const cache = find_inactive_votes_cache_impl (hash);
lock_a.unlock ();
result.election->insert_inactive_votes_cache (cache);
node.stats.inc (nano::stat::type::election, nano::stat::detail::election_start);
vacancy_update ();
}
}
else
{
result.election = existing->election;
}
if (lock_a.owns_lock ())
{
lock_a.unlock ();
}
// Votes are generated for inserted or ongoing elections
if (result.election)
{
result.election->generate_votes ();
}
}
return result;
}
// Validate a vote and apply it to the current election if one exists
nano::vote_code nano::active_transactions::vote (std::shared_ptr<nano::vote> const & vote_a)
{
nano::vote_code result{ nano::vote_code::indeterminate };
// If all hashes were recently confirmed then it is a replay
unsigned recently_confirmed_counter (0);
std::vector<std::pair<std::shared_ptr<nano::election>, nano::block_hash>> process;
{
nano::unique_lock<nano::mutex> lock (mutex);
for (auto vote_block : vote_a->blocks)
{
auto & recently_confirmed_by_hash (recently_confirmed.get<tag_hash> ());
if (vote_block.which ())
{
auto const & block_hash (boost::get<nano::block_hash> (vote_block));
auto existing (blocks.find (block_hash));
if (existing != blocks.end ())
{
process.emplace_back (existing->second, block_hash);
}
else if (recently_confirmed_by_hash.count (block_hash) == 0)
{
add_inactive_votes_cache (lock, block_hash, vote_a->account, vote_a->timestamp);
}
else
{
++recently_confirmed_counter;
}
}
else
{
auto block (boost::get<std::shared_ptr<nano::block>> (vote_block));
auto existing (roots.get<tag_root> ().find (block->qualified_root ()));
if (existing != roots.get<tag_root> ().end ())
{
process.emplace_back (existing->election, block->hash ());
}
else if (recently_confirmed_by_hash.count (block->hash ()) == 0)
{
add_inactive_votes_cache (lock, block->hash (), vote_a->account, vote_a->timestamp);
}
else
{
++recently_confirmed_counter;
}
}
}
}
if (!process.empty ())
{
bool replay (false);
bool processed (false);
for (auto const & [election, block_hash] : process)
{
auto const result_l = election->vote (vote_a->account, vote_a->timestamp, block_hash);
processed = processed || result_l.processed;
replay = replay || result_l.replay;
}
// Republish vote if it is new and the node does not host a principal representative (or close to)
if (processed)
{
auto const reps (node.wallets.reps ());
if (!reps.have_half_rep () && !reps.exists (vote_a->account))
{
node.network.flood_vote (vote_a, 0.5f);
}
}
result = replay ? nano::vote_code::replay : nano::vote_code::vote;
}
else if (recently_confirmed_counter == vote_a->blocks.size ())
{
result = nano::vote_code::replay;
}
return result;
}
bool nano::active_transactions::active (nano::qualified_root const & root_a)
{
nano::lock_guard<nano::mutex> lock (mutex);
return roots.get<tag_root> ().find (root_a) != roots.get<tag_root> ().end ();
}
bool nano::active_transactions::active (nano::block const & block_a)
{
nano::lock_guard<nano::mutex> guard (mutex);
return roots.get<tag_root> ().find (block_a.qualified_root ()) != roots.get<tag_root> ().end () && blocks.find (block_a.hash ()) != blocks.end ();
}
std::shared_ptr<nano::election> nano::active_transactions::election (nano::qualified_root const & root_a) const
{
std::shared_ptr<nano::election> result;
nano::lock_guard<nano::mutex> lock (mutex);
auto existing = roots.get<tag_root> ().find (root_a);
if (existing != roots.get<tag_root> ().end ())
{
result = existing->election;
}
return result;
}
std::shared_ptr<nano::block> nano::active_transactions::winner (nano::block_hash const & hash_a) const
{
std::shared_ptr<nano::block> result;
nano::unique_lock<nano::mutex> lock (mutex);
auto existing = blocks.find (hash_a);
if (existing != blocks.end ())
{
auto election = existing->second;
lock.unlock ();
result = election->winner ();
}
return result;
}
void nano::active_transactions::restart (nano::transaction const & transaction_a, std::shared_ptr<nano::block> const & block_a)
{
auto hash (block_a->hash ());
auto ledger_block (node.store.block_get (transaction_a, hash));
if (ledger_block != nullptr && ledger_block->block_work () != block_a->block_work () && !node.block_confirmed_or_being_confirmed (transaction_a, hash))
{
if (block_a->difficulty () > ledger_block->difficulty ())
{
// Re-writing the block is necessary to avoid the same work being received later to force restarting the election
// The existing block is re-written, not the arriving block, as that one might not have gone through a full signature check
ledger_block->block_work_set (block_a->block_work ());
// Deferred write
node.block_processor.update (ledger_block);
// Restart election for the upgraded block, previously dropped from elections
if (node.ledger.dependents_confirmed (transaction_a, *ledger_block))
{
node.stats.inc (nano::stat::type::election, nano::stat::detail::election_restart);
auto previous_balance = node.ledger.balance (transaction_a, ledger_block->previous ());
auto block_has_account = ledger_block->type () == nano::block_type::state || ledger_block->type () == nano::block_type::open;
auto account = block_has_account ? ledger_block->account () : ledger_block->sideband ().account;
scheduler.activate (account, transaction_a);
}
}
}
}
std::deque<nano::election_status> nano::active_transactions::list_recently_cemented ()
{
nano::lock_guard<nano::mutex> lock (mutex);
return recently_cemented;
}
void nano::active_transactions::add_recently_cemented (nano::election_status const & status_a)
{
nano::lock_guard<nano::mutex> guard (mutex);