forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-node.cpp
376 lines (325 loc) · 14.5 KB
/
test-node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
This file is part of TON Blockchain source code.
TON Blockchain is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
TON Blockchain is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TON Blockchain. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
You must obey the GNU General Public License in all respects for all
of the code used other than OpenSSL. If you modify file(s) with this
exception, you may extend this exception to your version of the file(s),
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version. If you delete this exception statement
from all source files in the program, then also delete it here.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "adnl/adnl.h"
#include "adnl/utils.hpp"
#include "auto/tl/ton_api_json.h"
#include "dht/dht.h"
#include "overlay/overlays.h"
#include "td/utils/OptionsParser.h"
#include "td/utils/Time.h"
#include "td/utils/filesystem.h"
#include "td/utils/format.h"
#include "td/utils/Random.h"
#include "td/utils/port/signals.h"
#include "td/utils/port/FileFd.h"
#include "catchain/catchain.h"
#include "crypto/common/refvector.hpp"
#if TD_DARWIN || TD_LINUX
#include <unistd.h>
#endif
#include <iostream>
#include <sstream>
template <std::size_t size>
std::ostream &operator<<(std::ostream &stream, const td::UInt<size> &x) {
for (size_t i = 0; i < size / 8; i++) {
stream << td::format::hex_digit((x.raw[i] >> 4) & 15) << td::format::hex_digit(x.raw[i] & 15);
}
return stream;
}
class TestNode : public td::actor::Actor {
private:
std::vector<td::UInt256> ping_ids_;
td::Timestamp next_dht_dump_;
td::actor::ActorOwn<ton::adnl::Adnl> adnl_;
std::vector<td::actor::ActorOwn<ton::dht::Dht>> dht_nodes_;
td::actor::ActorOwn<ton::overlay::Overlays> overlay_manager_;
std::vector<std::pair<td::UInt256, td::UInt256>> overlays_;
std::vector<td::actor::ActorOwn<ton::CatChain>> catchains_;
std::string local_config_ = "ton-local.config";
std::string global_config_ = "ton-global.config";
td::int32 broadcast_size_ = 100;
void receive_message(td::UInt256 src, td::UInt256 dst, td::BufferSlice data) {
LOG(ERROR) << "MESSAGE FROM " << src << " to " << dst << " of size " << std::to_string(data.size()) << "\n";
}
void receive_broadcast(td::UInt256 overlay_id, td::BufferSlice data) {
LOG(ERROR) << "BROADCAST IN " << overlay_id << " hash=" << td::sha256(data.as_slice()) << "\n";
}
void receive_query(td::UInt256 src, td::UInt256 dst, td::BufferSlice data, td::Promise<td::BufferSlice> promise) {
auto Q = ton::fetch_tl_object<ton::ton_api::getTestObject>(std::move(data), true);
CHECK(Q.is_ok());
auto R = Q.move_as_ok();
LOG(ERROR) << "QUERY "
<< " FROM " << src << " to " << dst << ": " << ton::ton_api::to_string(R) << "\n";
promise.set_value(serialize_tl_object(ton::create_tl_object<ton::ton_api::testObject>(), true));
}
void catchain_new_block(td::UInt256 src, td::uint64 height, td::BufferSlice data) {
LOG(ERROR) << "CATCHAIN BLOCK: " << src << "@" << height << ": " << td::sha256_uint256(data.as_slice()) << "\n";
}
void catchain_bad_block(td::UInt256 src) {
LOG(ERROR) << "CATCHAIN BAD BLOCK\n";
}
void catchain_broadcast(td::BufferSlice data) {
LOG(ERROR) << "CATCHAIN BROADCAST " << td::sha256_uint256(data.as_slice()) << "\n";
}
std::unique_ptr<ton::adnl::Adnl::Callback> make_callback() {
class Callback : public ton::adnl::Adnl::Callback {
public:
void receive_message(td::UInt256 src, td::UInt256 dst, td::BufferSlice data) override {
td::actor::send_closure(id_, &TestNode::receive_message, src, dst, std::move(data));
}
void receive_query(td::UInt256 src, td::UInt256 dst, td::BufferSlice data,
td::Promise<td::BufferSlice> promise) override {
td::actor::send_closure(id_, &TestNode::receive_query, src, dst, std::move(data), std::move(promise));
}
Callback(td::actor::ActorId<TestNode> id) : id_(std::move(id)) {
}
private:
td::actor::ActorId<TestNode> id_;
};
return std::make_unique<Callback>(actor_id(this));
}
std::unique_ptr<ton::CatChainActor::Callback> make_catchain_callback() {
class Callback : public ton::CatChainActor::Callback {
public:
void new_block(td::UInt256 src, td::uint64 height, td::BufferSlice data) override {
td::actor::send_closure(id_, &TestNode::catchain_new_block, src, height, std::move(data));
}
void bad_block(td::UInt256 src) override {
td::actor::send_closure(id_, &TestNode::catchain_bad_block, src);
}
void broadcast(td::BufferSlice data) override {
td::actor::send_closure(id_, &TestNode::catchain_broadcast, std::move(data));
}
Callback(td::actor::ActorId<TestNode> id) : id_(std::move(id)) {
}
private:
td::actor::ActorId<TestNode> id_;
};
return std::make_unique<Callback>(actor_id(this));
}
std::unique_ptr<ton::overlay::Overlays::Callback> make_overlay_callback() {
class Callback : public ton::overlay::Overlays::Callback {
public:
void receive_message(td::UInt256 src, td::UInt256 overlay_id, td::BufferSlice data) override {
}
void receive_query(td::UInt256 src, td::uint64 query_id, td::UInt256 overlay_id, td::BufferSlice data) override {
}
void receive_broadcast(td::UInt256 overlay_id, td::BufferSlice data) override {
td::actor::send_closure(id_, &TestNode::receive_broadcast, overlay_id, std::move(data));
}
Callback(td::actor::ActorId<TestNode> id) : id_(std::move(id)) {
}
private:
td::actor::ActorId<TestNode> id_;
};
return std::make_unique<Callback>(actor_id(this));
}
public:
void set_broadcast_size(td::int32 size) {
broadcast_size_ = size;
}
void set_local_config(std::string str) {
local_config_ = str;
}
void set_global_config(std::string str) {
global_config_ = str;
}
void start_up() override {
alarm_timestamp() = td::Timestamp::in(1);
}
void alarm() override {
/*if (overlays_.size() > 0 && broadcast_size_ > 0) {
td::BufferSlice s(broadcast_size_);
td::Random::secure_bytes(s.as_slice());
td::actor::send_closure(overlay_manager_, &ton::overlay::OverlayManager::send_broadcast_fer, overlays_[0].first,
overlays_[0].second, ton::create_tl_object<ton::ton_api::testString>(s.as_slice().str()));
}*/
for (auto &chain : catchains_) {
td::BufferSlice s(broadcast_size_);
td::Random::secure_bytes(s.as_slice());
td::actor::send_closure(chain, &ton::CatChainActor::add_event, std::move(s));
}
alarm_timestamp() = td::Timestamp::in(1.0);
if (next_dht_dump_.is_in_past()) {
/*for (auto &node : dht_nodes_) {
char b[10240];
td::StringBuilder sb({b, 10000});
node->get_actor_unsafe().dump(sb);
LOG(DEBUG) << sb.as_cslice().c_str();
}*/
next_dht_dump_ = td::Timestamp::in(60.0);
}
}
TestNode() {
adnl_ = ton::adnl::Adnl::create("/var/ton-work/db.adnl");
}
void run() {
auto L = td::read_file(local_config_).move_as_ok();
auto lc_j = td::json_decode(L.as_slice()).move_as_ok();
ton::ton_api::config_local lc;
ton::ton_api::from_json(lc, lc_j.get_object()).ensure();
auto G = td::read_file(global_config_).move_as_ok();
auto gc_j = td::json_decode(G.as_slice()).move_as_ok();
ton::ton_api::config_global gc;
ton::ton_api::from_json(gc, gc_j.get_object()).ensure();
for (auto &port : lc.udp_ports_) {
td::actor::send_closure(adnl_, &ton::adnl::Adnl::add_listening_udp_port, "0.0.0.0",
static_cast<td::uint16>(port));
}
/*if (!lc.net_) {
LOG(FATAL) << "local config does not contain NET section";
}*/
//td::actor::send_closure(network_manager_, &ton::adnl::AdnlNetworkManager::load_local_config, std::move(lc.net_));
td::actor::send_closure(adnl_, &ton::adnl::Adnl::add_ids_from_config, std::move(lc.local_ids_));
if (gc.adnl_) {
td::actor::send_closure(adnl_, &ton::adnl::Adnl::add_static_nodes_from_config,
std::move(gc.adnl_->static_nodes_));
}
if (!gc.dht_) {
LOG(FATAL) << "global config does not contain dht section";
}
for (auto &it : lc.dht_) {
if (it->get_id() == ton::ton_api::dht_config_local::ID) {
auto R = ton::dht::Dht::create_from_json(
ton::clone_tl_object(gc.dht_), ton::move_tl_object_as<ton::ton_api::dht_config_local>(it), adnl_.get());
if (R.is_error()) {
LOG(FATAL) << "fail creating dht node: " << R.move_as_error();
}
dht_nodes_.push_back(R.move_as_ok());
} else {
auto I = ton::move_tl_object_as<ton::ton_api::dht_config_random_local>(it);
for (int i = 0; i < I->cnt_; i++) {
auto R = ton::dht::Dht::create_random(ton::clone_tl_object(gc.dht_), ton::clone_tl_object(I->addr_list_),
adnl_.get());
if (R.is_error()) {
LOG(FATAL) << "fail creating dht node: " << R.move_as_error();
}
dht_nodes_.push_back(R.move_as_ok());
}
}
}
CHECK(dht_nodes_.size() > 0);
td::actor::send_closure(adnl_, &ton::adnl::Adnl::register_dht_node, dht_nodes_[0].get());
//td::actor::send_closure(overlay_manager_, &ton::overlay::Overlays::register_dht_node, dht_nodes_[0].get());
overlay_manager_ = ton::overlay::Overlays::create(adnl_.get(), dht_nodes_[0].get());
for (auto &it : lc.public_overlays_) {
if (it->get_id() == ton::ton_api::overlay_config_local::ID) {
auto X = ton::move_tl_object_as<ton::ton_api::overlay_config_local>(it);
auto id = ton::create_tl_object<ton::ton_api::adnl_id_overlay>(X->name_.clone());
auto Id = ton::move_tl_object_as<ton::ton_api::adnl_id_Full>(id);
auto sid = ton::adnl_short_id(Id);
overlays_.emplace_back(X->id_->id_, sid);
td::actor::send_closure(overlay_manager_, &ton::overlay::Overlays::create_public_overlay, X->id_->id_,
std::move(Id), make_overlay_callback());
} else {
auto X = ton::move_tl_object_as<ton::ton_api::overlay_config_random_local>(it);
for (int i = 0; i < X->cnt_; i++) {
auto pk = ton::adnl_generate_random_pk();
auto local_id = ton::adnl_short_id(ton::get_public_key(pk));
td::actor::send_closure(adnl_, &ton::adnl::Adnl::add_id, std::move(pk), ton::clone_tl_object(X->addr_list_));
auto id = ton::create_tl_object<ton::ton_api::adnl_id_overlay>(X->name_.clone());
auto Id = ton::move_tl_object_as<ton::ton_api::adnl_id_Full>(id);
auto sid = ton::adnl_short_id(Id);
overlays_.emplace_back(local_id, sid);
td::actor::send_closure(overlay_manager_, &ton::overlay::Overlays::create_public_overlay, local_id,
std::move(Id), make_overlay_callback());
}
}
}
//auto C = ton::CatChainActor::create(nullptr, adnl_.get(), overlay_manager_.get(),
// std::vector<ton::tl_object_ptr<ton::ton_api::adnl_id_Full>>());
for (auto &it : lc.catchains_) {
auto tag = it->tag_;
for (auto &V : gc.catchains_) {
if (V->tag_ == tag) {
auto v = std::move(clone_tl_object(V)->nodes_);
auto C = ton::CatChainActor::create(make_catchain_callback(), adnl_.get(), overlay_manager_.get(),
std::move(v), it->id_->id_, tag);
catchains_.push_back(std::move(C));
}
}
}
}
};
td::Result<td::UInt256> get_uint256(std::string str) {
if (str.size() != 64) {
return td::Status::Error("uint256 must have 64 bytes");
}
td::UInt256 res;
for (size_t i = 0; i < 32; i++) {
res.raw[i] = static_cast<td::uint8>(td::hex_to_int(str[2 * i]) * 16 + td::hex_to_int(str[2 * i + 1]));
}
return res;
}
int main(int argc, char *argv[]) {
SET_VERBOSITY_LEVEL(verbosity_DEBUG);
td::set_default_failure_signal_handler().ensure();
td::actor::ActorOwn<TestNode> x;
td::OptionsParser p;
p.set_description("test basic adnl functionality");
p.add_option('h', "help", "prints_help", [&]() {
char b[10240];
td::StringBuilder sb({b, 10000});
sb << p;
std::cout << sb.as_cslice().c_str();
std::exit(2);
return td::Status::OK();
});
p.add_option('C', "global-config", "file to read global config", [&](td::Slice fname) {
td::actor::send_closure(x, &TestNode::set_global_config, fname.str());
return td::Status::OK();
});
p.add_option('c', "local-config", "file to read local config", [&](td::Slice fname) {
td::actor::send_closure(x, &TestNode::set_local_config, fname.str());
return td::Status::OK();
});
p.add_option('s', "broadcast-size", "size of broadcast", [&](td::Slice fname) {
td::actor::send_closure(x, &TestNode::set_broadcast_size, std::atoi(fname.str().c_str()));
return td::Status::OK();
});
p.add_option('d', "daemonize", "set SIGHUP", [&]() {
td::set_signal_handler(td::SignalType::HangUp, [](int sig) {
#if TD_DARWIN || TD_LINUX
close(0);
setsid();
#endif
}).ensure();
return td::Status::OK();
});
#if TD_DARWIN || TD_LINUX
p.add_option('l', "logname", "log to file", [&](td::Slice fname) {
auto FileLog = td::FileFd::open(td::CSlice(fname.str().c_str()),
td::FileFd::Flags::Create | td::FileFd::Flags::Append | td::FileFd::Flags::Write)
.move_as_ok();
dup2(FileLog.get_native_fd().fd(), 1);
dup2(FileLog.get_native_fd().fd(), 2);
return td::Status::OK();
});
#endif
td::actor::Scheduler scheduler({2});
scheduler.run_in_context([&] { x = td::actor::create_actor<TestNode>("testnode"); });
scheduler.run_in_context([&] { p.run(argc, argv).ensure(); });
scheduler.run_in_context([&] { td::actor::send_closure(x, &TestNode::run); });
scheduler.run();
return 0;
}