Skip to content

Commit e173f11

Browse files
committed
Wait for block in validator-session.cpp
1 parent 501790f commit e173f11

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

validator-session/validator-session.cpp

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ void ValidatorSessionImpl::process_received_block(td::uint32 block_round, Public
361361
}
362362

363363
blocks_[block_id] = std::move(candidate);
364+
if (auto it = block_waiters_.find(block_id); it != block_waiters_.end()) {
365+
for (auto &promise : it->second) {
366+
promise.set_result(td::Unit());
367+
}
368+
block_waiters_.erase(it);
369+
}
364370

365371
VLOG(VALIDATOR_SESSION_WARNING) << this << ": received broadcast " << block_id;
366372
if (block_round != cur_round_) {
@@ -552,6 +558,12 @@ void ValidatorSessionImpl::generated_block(td::uint32 round, GeneratedCandidate
552558
blocks_[block_id] = create_tl_object<ton_api::validatorSession_candidate>(
553559
local_id().tl(), round, c.candidate.id.root_hash, std::move(c.candidate.data),
554560
std::move(c.candidate.collated_data));
561+
if (auto it = block_waiters_.find(block_id); it != block_waiters_.end()) {
562+
for (auto &promise : it->second) {
563+
promise.set_result(td::Unit());
564+
}
565+
block_waiters_.erase(it);
566+
}
555567
pending_generate_ = false;
556568
generated_ = true;
557569
generated_block_ = block_id;
@@ -742,7 +754,6 @@ void ValidatorSessionImpl::try_approve_block(const SentBlock *block) {
742754
VLOG(VALIDATOR_SESSION_WARNING) << print_id << ": failed to get candidate " << hash << " from " << id
743755
<< ": " << R.move_as_error();
744756
} else {
745-
LOG(ERROR) << "QQQQQ Got block " << R.ok().size();
746757
td::actor::send_closure(SelfId, &ValidatorSessionImpl::process_broadcast, src_id, R.move_as_ok(),
747758
candidate_id, false, false);
748759
}
@@ -1369,25 +1380,48 @@ void ValidatorSessionImpl::process_approve(td::uint32 node_id, td::uint32 round,
13691380
bool is_approved_66pct = stat->approved_66pct_at > 0.0;
13701381

13711382
if (allow_optimistic_generation_ && !was_approved_66pct && is_approved_66pct && cur_round_ == round &&
1372-
description().get_node_priority(local_idx(), round + 1) == 0 && blocks_.contains(candidate_id)) {
1373-
auto &block = blocks_[candidate_id];
1374-
if (cur_round_ == first_block_round_ &&
1375-
description().get_node_priority(description().get_source_idx(PublicKeyHash{block->src_}), cur_round_) == 0) {
1376-
callback_->generate_block_optimistic(BlockSourceInfo{description().get_source_public_key(local_idx()),
1377-
BlockCandidatePriority{round + 1, round + 1, 0}},
1378-
block->data_.clone(), block->root_hash_, stat->block_id.file_hash,
1379-
[=, SelfId = actor_id(this)](td::Result<GeneratedCandidate> R) {
1380-
if (R.is_error()) {
1381-
return;
1382-
}
1383-
td::actor::send_closure(
1384-
SelfId, &ValidatorSessionImpl::generated_optimistic_candidate,
1385-
round + 1, R.move_as_ok(), candidate_id);
1386-
});
1383+
cur_round_ == first_block_round_ && description().get_node_priority(local_idx(), round + 1) == 0 &&
1384+
blocks_.contains(candidate_id) &&
1385+
description().get_node_priority(description().get_source_idx(stat->validator_id), cur_round_) == 0) {
1386+
if (blocks_.contains(candidate_id)) {
1387+
generate_block_optimistic(round, candidate_id);
1388+
} else {
1389+
block_waiters_[candidate_id].push_back([=, SelfId = actor_id(this)](td::Result<td::Unit> R) {
1390+
if (R.is_ok()) {
1391+
td::actor::send_closure(SelfId, &ValidatorSessionImpl::generate_block_optimistic, round, candidate_id);
1392+
}
1393+
});
13871394
}
13881395
}
13891396
}
13901397

1398+
void ValidatorSessionImpl::generate_block_optimistic(td::uint32 cur_round,
1399+
ValidatorSessionCandidateId prev_candidate_id) {
1400+
if (cur_round != cur_round_) {
1401+
return;
1402+
}
1403+
auto it = blocks_.find(prev_candidate_id);
1404+
if (it == blocks_.end()) {
1405+
return;
1406+
}
1407+
auto &block = it->second;
1408+
auto stat = stats_get_candidate_stat_by_id(cur_round, prev_candidate_id);
1409+
if (!stat) {
1410+
return;
1411+
}
1412+
callback_->generate_block_optimistic(BlockSourceInfo{description().get_source_public_key(local_idx()),
1413+
BlockCandidatePriority{cur_round + 1, cur_round + 1, 0}},
1414+
block->data_.clone(), block->root_hash_, stat->block_id.file_hash,
1415+
[=, SelfId = actor_id(this)](td::Result<GeneratedCandidate> R) {
1416+
if (R.is_error()) {
1417+
return;
1418+
}
1419+
td::actor::send_closure(SelfId,
1420+
&ValidatorSessionImpl::generated_optimistic_candidate,
1421+
cur_round + 1, R.move_as_ok(), prev_candidate_id);
1422+
});
1423+
}
1424+
13911425
void ValidatorSessionImpl::generated_optimistic_candidate(td::uint32 round, GeneratedCandidate candidate,
13921426
ValidatorSessionCandidateId prev_candidate) {
13931427
if (cur_round_ > round) {

validator-session/validator-session.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class ValidatorSessionImpl : public ValidatorSession {
7676
std::map<ValidatorSessionCandidateId, tl_object_ptr<ton_api::validatorSession_candidate>> blocks_;
7777
// src_round_candidate_[src_id][round] -> candidate id
7878
std::vector<std::map<td::uint32, ValidatorSessionCandidateId>> src_round_candidate_;
79+
std::map<ValidatorSessionCandidateId, std::vector<td::Promise<td::Unit>>> block_waiters_;
7980

8081
catchain::CatChainSessionId unique_hash_;
8182

@@ -202,7 +203,7 @@ class ValidatorSessionImpl : public ValidatorSession {
202203
ValidatorSessionCandidateId candidate_id);
203204
void stats_process_action(td::uint32 node_id, ton_api::validatorSession_round_Message &action);
204205
void process_approve(td::uint32 node_id, td::uint32 round, ValidatorSessionCandidateId candidate_id);
205-
206+
void generate_block_optimistic(td::uint32 cur_round, ValidatorSessionCandidateId prev_candidate_id);
206207
void generated_optimistic_candidate(td::uint32 round, GeneratedCandidate candidate,
207208
ValidatorSessionCandidateId prev_candidate);
208209

0 commit comments

Comments
 (0)