Skip to content

Commit 1da94e6

Browse files
Send only first block candidate optimistically (#1260)
* Broadcast only the first block candidate * Fix sending block broadcast --------- Co-authored-by: SpyCheese <[email protected]>
1 parent 8364a24 commit 1da94e6

22 files changed

+164
-82
lines changed

create-hardfork/create-hardfork.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class HardforkCreator : public td::actor::Actor {
249249
void send_block_candidate(ton::BlockIdExt block_id, ton::CatchainSeqno cc_seqno, td::uint32 validator_set_hash,
250250
td::BufferSlice data) override {
251251
}
252-
void send_broadcast(ton::BlockBroadcast broadcast, bool custom_overlays_only) override {
252+
void send_broadcast(ton::BlockBroadcast broadcast, int mode) override {
253253
}
254254
void download_block(ton::BlockIdExt block_id, td::uint32 priority, td::Timestamp timeout,
255255
td::Promise<ton::ReceivedBlock> promise) override {

test/test-ton-collator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class TestNode : public td::actor::Actor {
350350
void send_block_candidate(ton::BlockIdExt block_id, ton::CatchainSeqno cc_seqno, td::uint32 validator_set_hash,
351351
td::BufferSlice data) override {
352352
}
353-
void send_broadcast(ton::BlockBroadcast broadcast, bool custom_overlays_only) override {
353+
void send_broadcast(ton::BlockBroadcast broadcast, int mode) override {
354354
}
355355
void download_block(ton::BlockIdExt block_id, td::uint32 priority, td::Timestamp timeout,
356356
td::Promise<ton::ReceivedBlock> promise) override {

validator-session/validator-session-types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ struct EndValidatorGroupStats {
178178
std::vector<Node> nodes;
179179
};
180180

181+
struct BlockSourceInfo {
182+
td::uint32 round, first_block_round;
183+
PublicKey source;
184+
td::int32 source_priority;
185+
};
186+
181187
} // namespace validatorsession
182188

183189
} // namespace ton

validator-session/validator-session.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,9 @@ void ValidatorSessionImpl::check_generate_slot() {
553553
LOG(WARNING) << print_id << ": failed to generate block candidate: " << R.move_as_error();
554554
}
555555
});
556-
callback_->on_generate_slot(cur_round_, std::move(P));
556+
callback_->on_generate_slot(
557+
BlockSourceInfo{cur_round_, first_block_round_, description().get_source_public_key(local_idx()), priority},
558+
std::move(P));
557559
} else {
558560
alarm_timestamp().relax(t);
559561
}
@@ -631,8 +633,10 @@ void ValidatorSessionImpl::try_approve_block(const SentBlock *block) {
631633
});
632634
pending_approve_.insert(block_id);
633635

634-
callback_->on_candidate(cur_round_, description().get_source_public_key(block->get_src_idx()), B->root_hash_,
635-
B->data_.clone(), B->collated_data_.clone(), std::move(P));
636+
callback_->on_candidate(
637+
BlockSourceInfo{cur_round_, first_block_round_, description().get_source_public_key(block->get_src_idx()),
638+
description().get_node_priority(block->get_src_idx(), cur_round_)},
639+
B->root_hash_, B->data_.clone(), B->collated_data_.clone(), std::move(P));
636640
} else if (T.is_in_past()) {
637641
if (!active_requests_.count(block_id)) {
638642
auto v = virtual_state_->get_block_approvers(description(), block_id);
@@ -905,15 +909,19 @@ void ValidatorSessionImpl::on_new_round(td::uint32 round) {
905909
stats.rounds.pop_back();
906910
}
907911

912+
BlockSourceInfo source_info{cur_round_, first_block_round_,
913+
description().get_source_public_key(block->get_src_idx()),
914+
description().get_node_priority(block->get_src_idx(), cur_round_)};
908915
if (it == blocks_.end()) {
909-
callback_->on_block_committed(cur_round_, description().get_source_public_key(block->get_src_idx()),
910-
block->get_root_hash(), block->get_file_hash(), td::BufferSlice(),
911-
std::move(export_sigs), std::move(export_approve_sigs), std::move(stats));
916+
callback_->on_block_committed(std::move(source_info), block->get_root_hash(), block->get_file_hash(),
917+
td::BufferSlice(), std::move(export_sigs), std::move(export_approve_sigs),
918+
std::move(stats));
912919
} else {
913-
callback_->on_block_committed(cur_round_, description().get_source_public_key(block->get_src_idx()),
914-
block->get_root_hash(), block->get_file_hash(), it->second->data_.clone(),
915-
std::move(export_sigs), std::move(export_approve_sigs), std::move(stats));
920+
callback_->on_block_committed(std::move(source_info), block->get_root_hash(), block->get_file_hash(),
921+
it->second->data_.clone(), std::move(export_sigs), std::move(export_approve_sigs),
922+
std::move(stats));
916923
}
924+
first_block_round_ = cur_round_ + 1;
917925
}
918926
cur_round_++;
919927
if (have_block) {

validator-session/validator-session.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ class ValidatorSession : public td::actor::Actor {
8585

8686
class Callback {
8787
public:
88-
virtual void on_candidate(td::uint32 round, PublicKey source, ValidatorSessionRootHash root_hash,
89-
td::BufferSlice data, td::BufferSlice collated_data,
90-
td::Promise<CandidateDecision> promise) = 0;
91-
virtual void on_generate_slot(td::uint32 round, td::Promise<GeneratedCandidate> promise) = 0;
92-
virtual void on_block_committed(td::uint32 round, PublicKey source, ValidatorSessionRootHash root_hash,
88+
virtual void on_candidate(BlockSourceInfo source_info, ValidatorSessionRootHash root_hash, td::BufferSlice data,
89+
td::BufferSlice collated_data, td::Promise<CandidateDecision> promise) = 0;
90+
virtual void on_generate_slot(BlockSourceInfo source_info, td::Promise<GeneratedCandidate> promise) = 0;
91+
virtual void on_block_committed(BlockSourceInfo source_info, ValidatorSessionRootHash root_hash,
9392
ValidatorSessionFileHash file_hash, td::BufferSlice data,
9493
std::vector<std::pair<PublicKeyHash, td::BufferSlice>> signatures,
9594
std::vector<std::pair<PublicKeyHash, td::BufferSlice>> approve_signatures,

validator-session/validator-session.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ValidatorSessionImpl : public ValidatorSession {
5353
const ValidatorSessionState *real_state_ = nullptr;
5454
const ValidatorSessionState *virtual_state_ = nullptr;
5555

56-
td::uint32 cur_round_ = 0;
56+
td::uint32 cur_round_ = 0, first_block_round_ = 0;
5757
td::Timestamp round_started_at_ = td::Timestamp::never();
5858
td::Timestamp round_debug_at_ = td::Timestamp::never();
5959
std::set<ValidatorSessionCandidateId> pending_approve_;

validator/fabric.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void run_check_external_message(td::Ref<ExtMessage> message, td::actor::ActorId<
5656

5757
void run_accept_block_query(BlockIdExt id, td::Ref<BlockData> data, std::vector<BlockIdExt> prev,
5858
td::Ref<ValidatorSet> validator_set, td::Ref<BlockSignatureSet> signatures,
59-
td::Ref<BlockSignatureSet> approve_signatures, bool send_broadcast,
59+
td::Ref<BlockSignatureSet> approve_signatures, int send_broadcast_mode,
6060
td::actor::ActorId<ValidatorManager> manager, td::Promise<td::Unit> promise);
6161
void run_fake_accept_block_query(BlockIdExt id, td::Ref<BlockData> data, std::vector<BlockIdExt> prev,
6262
td::Ref<ValidatorSet> validator_set, td::actor::ActorId<ValidatorManager> manager,

validator/full-node.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,21 +261,22 @@ void FullNodeImpl::send_block_candidate(BlockIdExt block_id, CatchainSeqno cc_se
261261
}
262262
}
263263

264-
void FullNodeImpl::send_broadcast(BlockBroadcast broadcast, bool custom_overlays_only) {
265-
send_block_broadcast_to_custom_overlays(broadcast);
266-
if (custom_overlays_only) {
267-
return;
264+
void FullNodeImpl::send_broadcast(BlockBroadcast broadcast, int mode) {
265+
if (mode & broadcast_mode_custom) {
266+
send_block_broadcast_to_custom_overlays(broadcast);
268267
}
269268
auto shard = get_shard(ShardIdFull{masterchainId});
270269
if (shard.empty()) {
271270
VLOG(FULL_NODE_WARNING) << "dropping OUT broadcast to unknown shard";
272271
return;
273272
}
274-
if (broadcast.block_id.is_masterchain() && !private_block_overlays_.empty()) {
273+
if (!private_block_overlays_.empty() && (mode & broadcast_mode_private_block)) {
275274
td::actor::send_closure(private_block_overlays_.begin()->second, &FullNodePrivateBlockOverlay::send_broadcast,
276275
broadcast.clone());
277276
}
278-
td::actor::send_closure(shard, &FullNodeShard::send_broadcast, std::move(broadcast));
277+
if (mode & broadcast_mode_public) {
278+
td::actor::send_closure(shard, &FullNodeShard::send_broadcast, std::move(broadcast));
279+
}
279280
}
280281

281282
void FullNodeImpl::download_block(BlockIdExt id, td::uint32 priority, td::Timestamp timeout,
@@ -496,8 +497,8 @@ void FullNodeImpl::start_up() {
496497
td::actor::send_closure(id_, &FullNodeImpl::send_block_candidate, block_id, cc_seqno, validator_set_hash,
497498
std::move(data));
498499
}
499-
void send_broadcast(BlockBroadcast broadcast, bool custom_overlays_only) override {
500-
td::actor::send_closure(id_, &FullNodeImpl::send_broadcast, std::move(broadcast), custom_overlays_only);
500+
void send_broadcast(BlockBroadcast broadcast, int mode) override {
501+
td::actor::send_closure(id_, &FullNodeImpl::send_broadcast, std::move(broadcast), mode);
501502
}
502503
void download_block(BlockIdExt id, td::uint32 priority, td::Timestamp timeout,
503504
td::Promise<ReceivedBlock> promise) override {

validator/full-node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class FullNode : public td::actor::Actor {
9999
static constexpr td::uint64 max_state_size() {
100100
return 4ull << 30;
101101
}
102+
enum { broadcast_mode_public = 1, broadcast_mode_private_block = 2, broadcast_mode_custom = 4 };
102103

103104
static td::actor::ActorOwn<FullNode> create(ton::PublicKeyHash local_id, adnl::AdnlNodeIdShort adnl_id,
104105
FileHash zero_state_file_hash, FullNodeConfig config,

validator/full-node.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class FullNodeImpl : public FullNode {
6868
void send_shard_block_info(BlockIdExt block_id, CatchainSeqno cc_seqnp, td::BufferSlice data);
6969
void send_block_candidate(BlockIdExt block_id, CatchainSeqno cc_seqno, td::uint32 validator_set_hash,
7070
td::BufferSlice data);
71-
void send_broadcast(BlockBroadcast broadcast, bool custom_overlays_only);
71+
void send_broadcast(BlockBroadcast broadcast, int mode);
7272
void download_block(BlockIdExt id, td::uint32 priority, td::Timestamp timeout, td::Promise<ReceivedBlock> promise);
7373
void download_zero_state(BlockIdExt id, td::uint32 priority, td::Timestamp timeout,
7474
td::Promise<td::BufferSlice> promise);

0 commit comments

Comments
 (0)