1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-12 19:22:37 +00:00

Minor DHT improvements (#657)

* Remove repeating DHT queries in adnl-peer

* Fix checking dht node signature for non-default network id

* Custom dht network id in generate-random-id
This commit is contained in:
SpyCheese 2023-03-30 07:10:09 +00:00 committed by GitHub
parent 5e0dadfff6
commit 3a30d6f319
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 20 deletions

View file

@ -944,7 +944,6 @@ void AdnlPeerPairImpl::got_data_from_dht(td::Result<AdnlNode> R) {
CHECK(dht_query_active_); CHECK(dht_query_active_);
dht_query_active_ = false; dht_query_active_ = false;
next_dht_query_at_ = td::Timestamp::in(td::Random::fast(60.0, 120.0)); next_dht_query_at_ = td::Timestamp::in(td::Random::fast(60.0, 120.0));
alarm_timestamp().relax(next_dht_query_at_);
if (R.is_error()) { if (R.is_error()) {
VLOG(ADNL_INFO) << this << ": dht query failed: " << R.move_as_error(); VLOG(ADNL_INFO) << this << ": dht query failed: " << R.move_as_error();
return; return;

View file

@ -140,13 +140,7 @@ adnl::AdnlNodeIdFull DhtRemoteNode::get_full_id() const {
td::Result<std::unique_ptr<DhtRemoteNode>> DhtRemoteNode::create(DhtNode node, td::uint32 max_missed_pings, td::Result<std::unique_ptr<DhtRemoteNode>> DhtRemoteNode::create(DhtNode node, td::uint32 max_missed_pings,
td::int32 our_network_id) { td::int32 our_network_id) {
TRY_RESULT(enc, node.adnl_id().pubkey().create_encryptor()); TRY_STATUS(node.check_signature());
auto tl = node.tl();
auto sig = std::move(tl->signature_);
TRY_STATUS_PREFIX(enc->check_signature(serialize_tl_object(tl, true).as_slice(), sig.as_slice()),
"bad node signature: ");
return std::make_unique<DhtRemoteNode>(std::move(node), max_missed_pings, our_network_id); return std::make_unique<DhtRemoteNode>(std::move(node), max_missed_pings, our_network_id);
} }

View file

@ -28,9 +28,6 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <string> #include <string>
#include <cstring>
#include <cassert>
#include "crypto/ellcurve/Ed25519.h"
#include "adnl/utils.hpp" #include "adnl/utils.hpp"
#include "auto/tl/ton_api.h" #include "auto/tl/ton_api.h"
#include "auto/tl/ton_api_json.h" #include "auto/tl/ton_api_json.h"
@ -38,12 +35,13 @@
#include "td/utils/OptionParser.h" #include "td/utils/OptionParser.h"
#include "td/utils/filesystem.h" #include "td/utils/filesystem.h"
#include "keys/encryptor.h" #include "keys/encryptor.h"
#include "keys/keys.hpp"
#include "git.h" #include "git.h"
#include "dht/dht-node.hpp"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
ton::PrivateKey pk; ton::PrivateKey pk;
ton::tl_object_ptr<ton::ton_api::adnl_addressList> addr_list; td::optional<ton::adnl::AdnlAddressList> addr_list;
td::optional<td::int32> network_id_opt;
td::OptionParser p; td::OptionParser p;
p.set_description("generate random id"); p.set_description("generate random id");
@ -78,11 +76,19 @@ int main(int argc, char *argv[]) {
if (addr_list) { if (addr_list) {
return td::Status::Error("duplicate '-a' option"); return td::Status::Error("duplicate '-a' option");
} }
CHECK(!addr_list);
td::BufferSlice bs(key); td::BufferSlice bs(key);
TRY_RESULT_PREFIX(as_json_value, td::json_decode(bs.as_slice()), "bad addr list JSON: "); TRY_RESULT_PREFIX(as_json_value, td::json_decode(bs.as_slice()), "bad addr list JSON: ");
TRY_STATUS_PREFIX(td::from_json(addr_list, std::move(as_json_value)), "bad addr list TL: "); ton::tl_object_ptr<ton::ton_api::adnl_addressList> addr_list_tl;
TRY_STATUS_PREFIX(td::from_json(addr_list_tl, std::move(as_json_value)), "bad addr list TL: ");
TRY_RESULT_PREFIX_ASSIGN(addr_list, ton::adnl::AdnlAddressList::create(addr_list_tl), "bad addr list: ");
return td::Status::OK();
});
p.add_checked_option('i', "network-id", "dht network id (default: -1)", [&](td::Slice key) {
if (network_id_opt) {
return td::Status::Error("duplicate '-i' option");
}
TRY_RESULT_PREFIX_ASSIGN(network_id_opt, td::to_integer_safe<td::int32>(key), "bad network id: ");
return td::Status::OK(); return td::Status::OK();
}); });
@ -118,7 +124,7 @@ int main(int argc, char *argv[]) {
std::cerr << "'-a' option missing" << std::endl; std::cerr << "'-a' option missing" << std::endl;
return 2; return 2;
} }
auto x = ton::create_tl_object<ton::ton_api::adnl_node>(pub_key.tl(), std::move(addr_list)); auto x = ton::create_tl_object<ton::ton_api::adnl_node>(pub_key.tl(), addr_list.value().tl());
auto e = pk.create_decryptor().move_as_ok(); auto e = pk.create_decryptor().move_as_ok();
auto r = e->sign(ton::serialize_tl_object(x, true).as_slice()).move_as_ok(); auto r = e->sign(ton::serialize_tl_object(x, true).as_slice()).move_as_ok();
@ -129,12 +135,17 @@ int main(int argc, char *argv[]) {
std::cerr << "'-a' option missing" << std::endl; std::cerr << "'-a' option missing" << std::endl;
return 2; return 2;
} }
auto x = ton::create_tl_object<ton::ton_api::dht_node>(pub_key.tl(), std::move(addr_list), -1, td::BufferSlice()); td::int32 network_id = network_id_opt ? network_id_opt.value() : -1;
td::BufferSlice to_sign = ton::serialize_tl_object(
ton::dht::DhtNode{ton::adnl::AdnlNodeIdFull{pub_key}, addr_list.value(), -1, network_id, td::BufferSlice{}}
.tl(),
true);
auto e = pk.create_decryptor().move_as_ok(); auto e = pk.create_decryptor().move_as_ok();
auto r = e->sign(ton::serialize_tl_object(x, true).as_slice()).move_as_ok(); auto signature = e->sign(to_sign.as_slice()).move_as_ok();
x->signature_ = std::move(r); auto node =
ton::dht::DhtNode{ton::adnl::AdnlNodeIdFull{pub_key}, addr_list.value(), -1, network_id, std::move(signature)};
auto v = td::json_encode<std::string>(td::ToJson(x)); auto v = td::json_encode<std::string>(td::ToJson(node.tl()));
std::cout << v << "\n"; std::cout << v << "\n";
} else if (mode == "keys") { } else if (mode == "keys") {
td::write_file(name, pk.export_as_slice()).ensure(); td::write_file(name, pk.export_as_slice()).ensure();