Skip to content

Commit d8244ef

Browse files
author
ton
committed
changed validate broadcast logic, added new queries to
validator-engine-console
1 parent 47814dc commit d8244ef

24 files changed

+690
-79
lines changed

adnl/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ target_include_directories(adnl-proxy PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_S
8787
target_link_libraries(adnl-proxy PUBLIC tdactor ton_crypto tl_api tdnet common
8888
tl-utils)
8989

90+
add_executable(adnl-pong adnl-pong.cpp)
91+
target_include_directories(adnl-pong PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
92+
target_link_libraries(adnl-pong PUBLIC tdactor ton_crypto tl_api tdnet common
93+
tl-utils adnl dht)
94+
9095
add_library(adnltest STATIC ${ADNL_TEST_SOURCE})
9196
target_include_directories(adnltest PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>)
9297
target_link_libraries(adnltest PUBLIC adnl )

adnl/adnl-peer-table.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,29 @@ void AdnlPeerTableImpl::start_up() {
283283

284284
void AdnlPeerTableImpl::write_new_addr_list_to_db(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id, AdnlDbItem node,
285285
td::Promise<td::Unit> promise) {
286+
if (db_.empty()) {
287+
promise.set_value(td::Unit());
288+
return;
289+
}
286290
td::actor::send_closure(db_, &AdnlDb::update, local_id, peer_id, std::move(node), std::move(promise));
287291
}
288292

289293
void AdnlPeerTableImpl::get_addr_list_from_db(AdnlNodeIdShort local_id, AdnlNodeIdShort peer_id,
290294
td::Promise<AdnlDbItem> promise) {
295+
if (db_.empty()) {
296+
promise.set_error(td::Status::Error(ErrorCode::notready, "db not inited"));
297+
return;
298+
}
291299
td::actor::send_closure(db_, &AdnlDb::get, local_id, peer_id, std::move(promise));
292300
}
293301

294302
AdnlPeerTableImpl::AdnlPeerTableImpl(std::string db_root, td::actor::ActorId<keyring::Keyring> keyring) {
295303
keyring_ = keyring;
296304
static_nodes_manager_ = AdnlStaticNodesManager::create();
297305

298-
db_ = AdnlDb::create(db_root + "/adnl");
306+
if (!db_root.empty()) {
307+
db_ = AdnlDb::create(db_root + "/adnl");
308+
}
299309
}
300310

301311
void AdnlPeerTableImpl::deliver(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) {

adnl/adnl-pong.cpp

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
This file is part of TON Blockchain source code.
3+
4+
TON Blockchain is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU General Public License
6+
as published by the Free Software Foundation; either version 2
7+
of the License, or (at your option) any later version.
8+
9+
TON Blockchain is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with TON Blockchain. If not, see <http://www.gnu.org/licenses/>.
16+
17+
In addition, as a special exception, the copyright holders give permission
18+
to link the code of portions of this program with the OpenSSL library.
19+
You must obey the GNU General Public License in all respects for all
20+
of the code used other than OpenSSL. If you modify file(s) with this
21+
exception, you may extend this exception to your version of the file(s),
22+
but you are not obligated to do so. If you do not wish to do so, delete this
23+
exception statement from your version. If you delete this exception statement
24+
from all source files in the program, then also delete it here.
25+
26+
Copyright 2017-2019 Telegram Systems LLP
27+
*/
28+
#include "td/actor/actor.h"
29+
#include "td/utils/buffer.h"
30+
#include "td/utils/port/IPAddress.h"
31+
#include "td/net/UdpServer.h"
32+
#include "td/utils/port/signals.h"
33+
#include "td/utils/OptionsParser.h"
34+
#include "td/utils/FileLog.h"
35+
#include "td/utils/port/path.h"
36+
#include "td/utils/port/user.h"
37+
#include "td/utils/filesystem.h"
38+
#include "common/checksum.h"
39+
#include "common/errorcode.h"
40+
#include "tl-utils/tl-utils.hpp"
41+
#include "auto/tl/ton_api_json.h"
42+
#include "adnl/adnl.h"
43+
#include <map>
44+
45+
#if TD_DARWIN || TD_LINUX
46+
#include <unistd.h>
47+
#endif
48+
49+
namespace ton {
50+
51+
namespace adnl {
52+
53+
class Callback : public adnl::Adnl::Callback {
54+
public:
55+
void receive_message(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) override {
56+
}
57+
void receive_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data,
58+
td::Promise<td::BufferSlice> promise) override {
59+
TRY_RESULT_PROMISE_PREFIX(promise, f, fetch_tl_object<ton_api::adnl_ping>(std::move(data), true),
60+
"adnl.ping expected");
61+
promise.set_value(create_serialize_tl_object<ton_api::adnl_pong>(f->value_));
62+
}
63+
64+
Callback() {
65+
}
66+
67+
private:
68+
};
69+
70+
} // namespace adnl
71+
72+
} // namespace ton
73+
74+
std::atomic<bool> rotate_logs_flags{false};
75+
void force_rotate_logs(int sig) {
76+
rotate_logs_flags.store(true);
77+
}
78+
79+
int main(int argc, char *argv[]) {
80+
SET_VERBOSITY_LEVEL(verbosity_INFO);
81+
82+
ton::PrivateKey pk;
83+
td::IPAddress addr;
84+
85+
td::set_default_failure_signal_handler().ensure();
86+
87+
std::unique_ptr<td::LogInterface> logger_;
88+
SCOPE_EXIT {
89+
td::log_interface = td::default_log_interface;
90+
};
91+
92+
std::string config = "/var/ton-work/etc/adnl-proxy.conf.json";
93+
94+
td::OptionsParser p;
95+
p.set_description("adnl pinger");
96+
p.add_option('v', "verbosity", "set verbosity level", [&](td::Slice arg) {
97+
int v = VERBOSITY_NAME(FATAL) + (td::to_integer<int>(arg));
98+
SET_VERBOSITY_LEVEL(v);
99+
return td::Status::OK();
100+
});
101+
p.add_option('h', "help", "prints_help", [&]() {
102+
char b[10240];
103+
td::StringBuilder sb(td::MutableSlice{b, 10000});
104+
sb << p;
105+
std::cout << sb.as_cslice().c_str();
106+
std::exit(2);
107+
return td::Status::OK();
108+
});
109+
p.add_option('d', "daemonize", "set SIGHUP", [&]() {
110+
#if TD_DARWIN || TD_LINUX
111+
close(0);
112+
setsid();
113+
#endif
114+
td::set_signal_handler(td::SignalType::HangUp, force_rotate_logs).ensure();
115+
return td::Status::OK();
116+
});
117+
p.add_option('l', "logname", "log to file", [&](td::Slice fname) {
118+
auto F = std::make_unique<td::FileLog>();
119+
TRY_STATUS(F->init(fname.str(), std::numeric_limits<td::uint64>::max(), true));
120+
logger_ = std::move(F);
121+
td::log_interface = logger_.get();
122+
return td::Status::OK();
123+
});
124+
td::uint32 threads = 7;
125+
p.add_option('t', "threads", PSTRING() << "number of threads (default=" << threads << ")", [&](td::Slice fname) {
126+
td::int32 v;
127+
try {
128+
v = std::stoi(fname.str());
129+
} catch (...) {
130+
return td::Status::Error(ton::ErrorCode::error, "bad value for --threads: not a number");
131+
}
132+
if (v < 1 || v > 256) {
133+
return td::Status::Error(ton::ErrorCode::error, "bad value for --threads: should be in range [1..256]");
134+
}
135+
threads = v;
136+
return td::Status::OK();
137+
});
138+
p.add_option('u', "user", "change user", [&](td::Slice user) { return td::change_user(user); });
139+
p.add_option('k', "key", "private key", [&](td::Slice key) {
140+
TRY_RESULT_ASSIGN(pk, ton::PrivateKey::import(key));
141+
return td::Status::OK();
142+
});
143+
p.add_option('a', "addr", "ip:port of instance", [&](td::Slice key) {
144+
TRY_STATUS(addr.init_host_port(key.str()));
145+
return td::Status::OK();
146+
});
147+
148+
p.run(argc, argv).ensure();
149+
150+
if (pk.empty()) {
151+
LOG(FATAL) << "no --key given";
152+
}
153+
if (!addr.is_valid()) {
154+
LOG(FATAL) << "no --addr given";
155+
}
156+
157+
td::actor::Scheduler scheduler({threads});
158+
159+
td::actor::ActorOwn<ton::keyring::Keyring> keyring;
160+
td::actor::ActorOwn<ton::adnl::Adnl> adnl;
161+
td::actor::ActorOwn<ton::adnl::AdnlNetworkManager> network_manager;
162+
163+
auto pub = pk.compute_public_key();
164+
165+
scheduler.run_in_context([&]() {
166+
keyring = ton::keyring::Keyring::create("");
167+
td::actor::send_closure(keyring, &ton::keyring::Keyring::add_key, std::move(pk), true, [](td::Unit) {});
168+
169+
adnl = ton::adnl::Adnl::create("", keyring.get());
170+
171+
network_manager = ton::adnl::AdnlNetworkManager::create(static_cast<td::uint16>(addr.get_port()));
172+
173+
td::actor::send_closure(network_manager, &ton::adnl::AdnlNetworkManager::add_self_addr, addr, 0);
174+
175+
auto tladdr = ton::create_tl_object<ton::ton_api::adnl_address_udp>(addr.get_ipv4(), addr.get_port());
176+
auto addr_vec = std::vector<ton::tl_object_ptr<ton::ton_api::adnl_Address>>();
177+
addr_vec.push_back(std::move(tladdr));
178+
auto tladdrlist = ton::create_tl_object<ton::ton_api::adnl_addressList>(
179+
std::move(addr_vec), ton::adnl::Adnl::adnl_start_time(), ton::adnl::Adnl::adnl_start_time(), 0, 2000000000);
180+
auto addrlist = ton::adnl::AdnlAddressList::create(tladdrlist).move_as_ok();
181+
182+
td::actor::send_closure(adnl, &ton::adnl::Adnl::add_id, ton::adnl::AdnlNodeIdFull{pub}, std::move(addrlist));
183+
td::actor::send_closure(adnl, &ton::adnl::Adnl::subscribe, ton::adnl::AdnlNodeIdShort{pub.compute_short_id()},
184+
ton::adnl::Adnl::int_to_bytestring(ton::ton_api::adnl_ping::ID),
185+
std::make_unique<ton::adnl::Callback>());
186+
});
187+
188+
while (scheduler.run(1)) {
189+
if (rotate_logs_flags.exchange(false)) {
190+
if (td::log_interface) {
191+
td::log_interface->rotate();
192+
}
193+
}
194+
}
195+
}

crypto/block/mc-config.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ td::Result<std::unique_ptr<Config>> Config::extract_from_key_block(Ref<vm::Cell>
6767
tlb::unpack_cell(extra.custom->prefetch_ref(), mc_extra) && mc_extra.key_block && mc_extra.config.not_null())) {
6868
return td::Status::Error(-400, "cannot unpack extra header of key block to extract configuration");
6969
}
70-
return block::Config::unpack_config(std::move(mc_extra.config));
70+
return block::Config::unpack_config(std::move(mc_extra.config), mode);
7171
}
7272

7373
td::Result<std::unique_ptr<Config>> Config::extract_from_state(Ref<vm::Cell> mc_state_root, int mode) {
@@ -1482,6 +1482,7 @@ std::vector<ton::ValidatorDescr> Config::compute_validator_set(ton::ShardIdFull
14821482
std::vector<ton::ValidatorDescr> Config::compute_validator_set(ton::ShardIdFull shard, ton::UnixTime time,
14831483
ton::CatchainSeqno cc_seqno) const {
14841484
if (!cur_validators_) {
1485+
LOG(DEBUG) << "failed to compute validator set: cur_validators_ is empty";
14851486
return {};
14861487
} else {
14871488
return compute_validator_set(shard, *cur_validators_, time, cc_seqno);

tl/generate/scheme/ton_api.tl

+13
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,14 @@ engine.adnlProxy.config ports:(vector engine.adnlProxy.port) = engine.adnlProxy.
498498

499499
---types---
500500

501+
adnl.pong value:long = adnl.Pong;
502+
503+
---functions---
504+
505+
adnl.ping value:long = adnl.Pong;
506+
507+
---types---
508+
501509
engine.validator.keyHash key_hash:int256 = engine.validator.KeyHash;
502510
engine.validator.signature signature:bytes = engine.validator.Signature;
503511

@@ -513,6 +521,9 @@ engine.validator.jsonConfig data:string = engine.validator.JsonConfig;
513521

514522
engine.validator.electionBid election_date:int perm_key:int256 adnl_addr:int256 to_send_payload:bytes = engine.validator.ElectionBid;
515523

524+
engine.validator.dhtServerStatus id:int256 status:int = engine.validator.DhtServerStatus;
525+
engine.validator.dhtServersStatus servers:(vector engine.validator.dhtServerStatus) = engine.validator.DhtServersStatus;
526+
516527
---functions---
517528

518529
engine.validator.getTime = engine.validator.Time;
@@ -550,4 +561,6 @@ engine.validator.setVerbosity verbosity:int = engine.validator.Success;
550561

551562
engine.validator.createElectionBid election_date:int election_addr:string wallet:string = engine.validator.ElectionBid;
552563

564+
engine.validator.checkDhtServers id:int256 = engine.validator.DhtServersStatus;
565+
553566
engine.validator.controlQuery data:bytes = Object;

tl/generate/scheme/ton_api.tlo

704 Bytes
Binary file not shown.

validator-engine-console/validator-engine-console-query.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,23 @@ td::Status CreateElectionBidQuery::receive(td::BufferSlice data) {
655655
TRY_STATUS(td::write_file(fname_, f->to_send_payload_.as_slice()));
656656
return td::Status::OK();
657657
}
658+
659+
td::Status CheckDhtServersQuery::run() {
660+
TRY_RESULT_ASSIGN(id_, tokenizer_.get_token<ton::PublicKeyHash>());
661+
return td::Status::OK();
662+
}
663+
664+
td::Status CheckDhtServersQuery::send() {
665+
auto b = ton::create_serialize_tl_object<ton::ton_api::engine_validator_checkDhtServers>(id_.tl());
666+
td::actor::send_closure(console_, &ValidatorEngineConsole::envelope_send_query, std::move(b), create_promise());
667+
return td::Status::OK();
668+
}
669+
670+
td::Status CheckDhtServersQuery::receive(td::BufferSlice data) {
671+
TRY_RESULT_PREFIX(f, ton::fetch_tl_object<ton::ton_api::engine_validator_dhtServersStatus>(data.as_slice(), true),
672+
"received incorrect answer: ");
673+
for (auto &s : f->servers_) {
674+
td::TerminalIO::out() << "id=" << s->id_ << " status=" << (s->status_ ? "SUCCESS" : "FAIL") << "\n";
675+
}
676+
return td::Status::OK();
677+
}

validator-engine-console/validator-engine-console-query.h

+21
Original file line numberDiff line numberDiff line change
@@ -821,3 +821,24 @@ class CreateElectionBidQuery : public Query {
821821
std::string fname_;
822822
};
823823

824+
class CheckDhtServersQuery : public Query {
825+
public:
826+
CheckDhtServersQuery(td::actor::ActorId<ValidatorEngineConsole> console, Tokenizer tokenizer)
827+
: Query(console, std::move(tokenizer)) {
828+
}
829+
td::Status run() override;
830+
td::Status send() override;
831+
td::Status receive(td::BufferSlice data) override;
832+
static std::string get_name() {
833+
return "checkdht";
834+
}
835+
static std::string get_help() {
836+
return "checkdht <adnlid>\tchecks, which root DHT servers are accessible from this ADNL addr";
837+
}
838+
std::string name() const override {
839+
return get_name();
840+
}
841+
842+
private:
843+
ton::PublicKeyHash id_;
844+
};

validator-engine-console/validator-engine-console.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ void ValidatorEngineConsole::run() {
130130
add_query_runner(std::make_unique<QueryRunnerImpl<AddNetworkAddressQuery>>());
131131
add_query_runner(std::make_unique<QueryRunnerImpl<AddNetworkProxyAddressQuery>>());
132132
add_query_runner(std::make_unique<QueryRunnerImpl<CreateElectionBidQuery>>());
133+
add_query_runner(std::make_unique<QueryRunnerImpl<CheckDhtServersQuery>>());
133134
}
134135

135136
bool ValidatorEngineConsole::envelope_send_query(td::BufferSlice query, td::Promise<td::BufferSlice> promise) {

0 commit comments

Comments
 (0)