Skip to content

Commit

Permalink
Sign broadcasts in shard overlays with validator keys
Browse files Browse the repository at this point in the history
  • Loading branch information
SpyCheese committed Oct 22, 2024
1 parent f82827e commit 657aa1a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
40 changes: 22 additions & 18 deletions validator/full-node-shard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,12 @@ void FullNodeShardImpl::send_ihr_message(td::BufferSlice data) {
auto B = create_serialize_tl_object<ton_api::tonNode_ihrMessageBroadcast>(
create_tl_object<ton_api::tonNode_ihrMessage>(std::move(data)));
if (B.size() <= overlay::Overlays::max_simple_broadcast_size()) {
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_ex, adnl_id_, overlay_id_, local_id_, 0,
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_ex, adnl_id_, overlay_id_,
send_broadcast_as(), 0,
std::move(B));
} else {
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_, local_id_, 0,
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_,
send_broadcast_as(), 0,
std::move(B));
}
}
Expand Down Expand Up @@ -744,10 +746,12 @@ void FullNodeShardImpl::send_external_message(td::BufferSlice data) {
auto B = create_serialize_tl_object<ton_api::tonNode_externalMessageBroadcast>(
create_tl_object<ton_api::tonNode_externalMessage>(std::move(data)));
if (B.size() <= overlay::Overlays::max_simple_broadcast_size()) {
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_ex, adnl_id_, overlay_id_, local_id_, 0,
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_ex, adnl_id_, overlay_id_,
send_broadcast_as(), 0,
std::move(B));
} else {
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_, local_id_, 0,
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_,
send_broadcast_as(), 0,
std::move(B));
}
}
Expand All @@ -761,11 +765,11 @@ void FullNodeShardImpl::send_shard_block_info(BlockIdExt block_id, CatchainSeqno
auto B = create_serialize_tl_object<ton_api::tonNode_newShardBlockBroadcast>(
create_tl_object<ton_api::tonNode_newShardBlock>(create_tl_block_id(block_id), cc_seqno, std::move(data)));
if (B.size() <= overlay::Overlays::max_simple_broadcast_size()) {
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_ex, adnl_id_, overlay_id_, local_id_, 0,
std::move(B));
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_ex, adnl_id_, overlay_id_,
send_broadcast_as(), 0, std::move(B));
} else {
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_, local_id_,
overlay::Overlays::BroadcastFlagAnySender(), std::move(B));
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_,
send_broadcast_as(), overlay::Overlays::BroadcastFlagAnySender(), std::move(B));
}
}

Expand All @@ -782,8 +786,8 @@ void FullNodeShardImpl::send_block_candidate(BlockIdExt block_id, CatchainSeqno
return;
}
VLOG(FULL_NODE_DEBUG) << "Sending newBlockCandidate: " << block_id.to_str();
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_, local_id_,
overlay::Overlays::BroadcastFlagAnySender(), B.move_as_ok());
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_,
send_broadcast_as(), overlay::Overlays::BroadcastFlagAnySender(), B.move_as_ok());
}

void FullNodeShardImpl::send_broadcast(BlockBroadcast broadcast) {
Expand All @@ -797,8 +801,8 @@ void FullNodeShardImpl::send_broadcast(BlockBroadcast broadcast) {
VLOG(FULL_NODE_WARNING) << "failed to serialize block broadcast: " << B.move_as_error();
return;
}
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_, local_id_,
overlay::Overlays::BroadcastFlagAnySender(), B.move_as_ok());
td::actor::send_closure(overlays_, &overlay::Overlays::send_broadcast_fec_ex, adnl_id_, overlay_id_,
send_broadcast_as(), overlay::Overlays::BroadcastFlagAnySender(), B.move_as_ok());
}

void FullNodeShardImpl::download_block(BlockIdExt id, td::uint32 priority, td::Timestamp timeout,
Expand Down Expand Up @@ -898,8 +902,8 @@ void FullNodeShardImpl::alarm() {
ping_neighbours_at_ = td::Timestamp::in(td::Random::fast(0.5, 1.0));
}
if (update_certificate_at_ && update_certificate_at_.is_in_past()) {
if (!sign_cert_by_.is_zero()) {
sign_new_certificate(sign_cert_by_);
if (!local_validator_key_.is_zero()) {
sign_new_certificate(local_validator_key_);
update_certificate_at_ = td::Timestamp::in(30.0);
} else {
update_certificate_at_ = td::Timestamp::never();
Expand Down Expand Up @@ -969,7 +973,7 @@ void FullNodeShardImpl::signed_new_certificate(ton::overlay::Certificate cert) {
}

void FullNodeShardImpl::sign_overlay_certificate(PublicKeyHash signed_key, td::uint32 expire_at, td::uint32 max_size, td::Promise<td::BufferSlice> promise) {
auto sign_by = sign_cert_by_;
auto sign_by = local_validator_key_;
if (sign_by.is_zero()) {
promise.set_error(td::Status::Error("Node has no key with signing authority"));
return;
Expand Down Expand Up @@ -1005,10 +1009,10 @@ void FullNodeShardImpl::update_validators(std::vector<PublicKeyHash> public_key_
return;
}
bool update_cert = false;
if (!local_hash.is_zero() && local_hash != sign_cert_by_) {
if (!local_hash.is_zero() && local_hash != local_validator_key_) {
update_cert = true;
}
sign_cert_by_ = local_hash;
local_validator_key_ = local_hash;

std::map<PublicKeyHash, td::uint32> authorized_keys;
for (auto &key : public_key_hashes) {
Expand All @@ -1019,7 +1023,7 @@ void FullNodeShardImpl::update_validators(std::vector<PublicKeyHash> public_key_
td::actor::send_closure(overlays_, &overlay::Overlays::set_privacy_rules, adnl_id_, overlay_id_, rules_);

if (update_cert) {
sign_new_certificate(sign_cert_by_);
sign_new_certificate(local_validator_key_);
update_certificate_at_ = td::Timestamp::in(30.0);
alarm_timestamp().relax(update_certificate_at_);
}
Expand Down
6 changes: 5 additions & 1 deletion validator/full-node-shard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class FullNodeShardImpl : public FullNodeShard {

overlay::OverlayIdFull overlay_id_full_;
overlay::OverlayIdShort overlay_id_;
PublicKeyHash sign_cert_by_ = PublicKeyHash::zero();
PublicKeyHash local_validator_key_ = PublicKeyHash::zero();
td::Timestamp update_certificate_at_;
td::Timestamp sync_completed_at_;

Expand All @@ -263,6 +263,10 @@ class FullNodeShardImpl : public FullNodeShard {
std::set<td::Bits256> my_ext_msg_broadcasts_;
std::set<td::Bits256> processed_ext_msg_broadcasts_;
td::Timestamp cleanup_processed_ext_msg_at_;

PublicKeyHash send_broadcast_as() const {
return local_validator_key_.is_zero() ? local_id_ : local_validator_key_;
}
};

} // namespace fullnode
Expand Down
21 changes: 11 additions & 10 deletions validator/full-node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ void FullNodeImpl::add_permanent_key(PublicKeyHash key, td::Promise<td::Unit> pr
update_custom_overlay(p.second);
}

if (!sign_cert_by_.is_zero()) {
if (!local_validator_key_.is_zero()) {
promise.set_value(td::Unit());
return;
}

for (auto &x : all_validators_) {
if (x == key) {
sign_cert_by_ = key;
local_validator_key_ = key;
}
}

for (auto &shard : shards_) {
td::actor::send_closure(shard.second, &FullNodeShard::update_validators, all_validators_, sign_cert_by_);
td::actor::send_closure(shard.second, &FullNodeShard::update_validators, all_validators_, local_validator_key_);
}
promise.set_value(td::Unit());
}
Expand All @@ -68,20 +68,20 @@ void FullNodeImpl::del_permanent_key(PublicKeyHash key, td::Promise<td::Unit> pr
update_custom_overlay(p.second);
}

if (sign_cert_by_ != key) {
if (local_validator_key_ != key) {
promise.set_value(td::Unit());
return;
}
sign_cert_by_ = PublicKeyHash::zero();
local_validator_key_ = PublicKeyHash::zero();

for (auto &x : all_validators_) {
if (local_keys_.count(x)) {
sign_cert_by_ = x;
local_validator_key_ = x;
}
}

for (auto &shard : shards_) {
td::actor::send_closure(shard.second, &FullNodeShard::update_validators, all_validators_, sign_cert_by_);
td::actor::send_closure(shard.second, &FullNodeShard::update_validators, all_validators_, local_validator_key_);
}
promise.set_value(td::Unit());
}
Expand Down Expand Up @@ -183,7 +183,8 @@ void FullNodeImpl::add_shard(ShardIdFull shard) {
FullNodeShard::create(shard, local_id_, adnl_id_, zero_state_file_hash_, config_, keyring_, adnl_,
rldp_, rldp2_, overlays_, validator_manager_, client_, actor_id(this)));
if (all_validators_.size() > 0) {
td::actor::send_closure(shards_[shard], &FullNodeShard::update_validators, all_validators_, sign_cert_by_);
td::actor::send_closure(shards_[shard], &FullNodeShard::update_validators, all_validators_,
local_validator_key_);
}
} else {
break;
Expand Down Expand Up @@ -402,11 +403,11 @@ void FullNodeImpl::got_key_block_config(td::Ref<ConfigHolder> config) {
// }

all_validators_ = keys;
sign_cert_by_ = l;
local_validator_key_ = l;
CHECK(all_validators_.size() > 0);

for (auto &shard : shards_) {
td::actor::send_closure(shard.second, &FullNodeShard::update_validators, all_validators_, sign_cert_by_);
td::actor::send_closure(shard.second, &FullNodeShard::update_validators, all_validators_, local_validator_key_);
}
}

Expand Down
2 changes: 1 addition & 1 deletion validator/full-node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class FullNodeImpl : public FullNode {

std::string db_root_;

PublicKeyHash sign_cert_by_;
PublicKeyHash local_validator_key_;
std::vector<PublicKeyHash> all_validators_;
std::map<PublicKeyHash, adnl::AdnlNodeIdShort> current_validators_;

Expand Down

0 comments on commit 657aa1a

Please sign in to comment.