28
#include "adnl/adnl-network-manager.h"
29
#include "adnl/adnl-test-loopback-implementation.h"
31
#include "rldp2/rldp.h"
33
#include "td/utils/port/signals.h"
34
#include "td/utils/port/path.h"
35
#include "td/utils/Random.h"
41
SET_VERBOSITY_LEVEL(verbosity_INFO);
43
std::string db_root_ = "tmp-dir-test-rldp2";
44
td::rmrf(db_root_).ignore();
45
td::mkdir(db_root_).ensure();
47
td::set_default_failure_signal_handler().ensure();
49
td::actor::ActorOwn<ton::keyring::Keyring> keyring;
50
td::actor::ActorOwn<ton::adnl::TestLoopbackNetworkManager> network_manager;
51
td::actor::ActorOwn<ton::adnl::Adnl> adnl;
52
td::actor::ActorOwn<ton::rldp2::Rldp> rldp;
54
ton::adnl::AdnlNodeIdShort src;
55
ton::adnl::AdnlNodeIdShort dst;
57
td::actor::Scheduler scheduler({0});
59
scheduler.run_in_context([&] {
60
keyring = ton::keyring::Keyring::create(db_root_);
61
network_manager = td::actor::create_actor<ton::adnl::TestLoopbackNetworkManager>("test net");
62
adnl = ton::adnl::Adnl::create(db_root_, keyring.get());
63
rldp = ton::rldp2::Rldp::create(adnl.get());
64
td::actor::send_closure(adnl, &ton::adnl::Adnl::register_network_manager, network_manager.get());
66
auto pk1 = ton::PrivateKey{ton::privkeys::Ed25519::random()};
67
auto pub1 = pk1.compute_public_key();
68
src = ton::adnl::AdnlNodeIdShort{pub1.compute_short_id()};
69
td::actor::send_closure(keyring, &ton::keyring::Keyring::add_key, std::move(pk1), true, [](td::Unit) {});
71
auto pk2 = ton::PrivateKey{ton::privkeys::Ed25519::random()};
72
auto pub2 = pk2.compute_public_key();
73
dst = ton::adnl::AdnlNodeIdShort{pub2.compute_short_id()};
74
td::actor::send_closure(keyring, &ton::keyring::Keyring::add_key, std::move(pk2), true, [](td::Unit) {});
76
auto addr = ton::adnl::TestLoopbackNetworkManager::generate_dummy_addr_list();
78
td::actor::send_closure(adnl, &ton::adnl::Adnl::add_id, ton::adnl::AdnlNodeIdFull{pub1}, addr, td::uint8(0));
79
td::actor::send_closure(adnl, &ton::adnl::Adnl::add_id, ton::adnl::AdnlNodeIdFull{pub2}, addr, td::uint8(0));
80
td::actor::send_closure(rldp, &ton::rldp2::Rldp::add_id, src);
81
td::actor::send_closure(rldp, &ton::rldp2::Rldp::add_id, dst);
83
td::actor::send_closure(adnl, &ton::adnl::Adnl::add_peer, src, ton::adnl::AdnlNodeIdFull{pub2}, addr);
85
td::actor::send_closure(network_manager, &ton::adnl::TestLoopbackNetworkManager::add_node_id, src, true, true);
86
td::actor::send_closure(network_manager, &ton::adnl::TestLoopbackNetworkManager::add_node_id, dst, true, true);
89
auto send_packet = [&](td::uint32 i) {
91
d.as_slice()[0] = '1';
92
d.as_slice().remove_prefix(1).copy_from(td::Slice{reinterpret_cast<td::uint8 *>(&i), 4});
97
std::atomic<td::uint32> remaining{0};
98
scheduler.run_in_context([&] {
99
class Callback : public ton::adnl::Adnl::Callback {
101
void receive_message(ton::adnl::AdnlNodeIdShort src, ton::adnl::AdnlNodeIdShort dst,
102
td::BufferSlice data) override {
105
if (data.size() >= 5) {
106
CHECK(td::crc32c(data.as_slice().truncate(data.size() - 4)) ==
107
*reinterpret_cast<const td::uint32 *>(data.as_slice().remove_prefix(data.size() - 4).begin()));
109
CHECK(remaining_ > 0);
112
void receive_query(ton::adnl::AdnlNodeIdShort src, ton::adnl::AdnlNodeIdShort dst, td::BufferSlice data,
113
td::Promise<td::BufferSlice> promise) override {
114
CHECK(data.size() == 5);
116
td::uint32 s = *reinterpret_cast<const td::uint32 *>(data.as_slice().remove_prefix(1).begin());
118
td::BufferSlice d{s};
120
td::Random::secure_bytes(d.as_slice().truncate(s - 4));
121
auto x = td::crc32c(d.as_slice().truncate(d.size() - 4));
123
d.as_slice().remove_prefix(d.size() - 4).copy_from(td::Slice{reinterpret_cast<td::uint8 *>(&x), 4});
125
td::Random::secure_bytes(d.as_slice());
128
promise.set_value(std::move(d));
130
Callback(std::atomic<td::uint32> &remaining) : remaining_(remaining) {
134
std::atomic<td::uint32> &remaining_;
136
td::actor::send_closure(adnl, &ton::adnl::Adnl::subscribe, dst, "1", std::make_unique<Callback>(remaining));
139
std::vector<td::uint32> sizes{1, 1024, 1 << 20, 2 << 20, 3 << 20, 10 << 20, 16 << 20};
141
for (auto &size : sizes) {
142
LOG(ERROR) << "testing delivering of packet of size " << size;
144
auto f = td::Clocks::system();
145
scheduler.run_in_context([&] {
147
td::actor::send_closure(rldp, &ton::rldp2::Rldp::send_query_ex, src, dst, std::string("t"),
148
td::PromiseCreator::lambda([&](td::Result<td::BufferSlice> R) {
152
td::Timestamp::in(1024.0), send_packet(size), size + 1024);
155
auto t = td::Timestamp::in(1024.0);
156
while (scheduler.run(16)) {
160
if (t.is_in_past()) {
161
LOG(FATAL) << "failed to receive packets: remaining=" << remaining;
165
LOG(ERROR) << "success. Time=" << (td::Clocks::system() - f);
168
scheduler.run_in_context([&] {
169
td::actor::send_closure(network_manager, &ton::adnl::TestLoopbackNetworkManager::set_loss_probability, 0.1);
171
LOG(ERROR) << "set loss to 10%";
173
for (auto &size : sizes) {
174
LOG(ERROR) << "testing delivering of packet of size " << size;
176
auto f = td::Clocks::system();
177
scheduler.run_in_context([&] {
179
td::actor::send_closure(rldp, &ton::rldp2::Rldp::send_query_ex, src, dst, std::string("t"),
180
td::PromiseCreator::lambda([&](td::Result<td::BufferSlice> R) {
184
td::Timestamp::in(1024.0), send_packet(size), size + 1024);
187
auto t = td::Timestamp::in(1024.0);
188
while (scheduler.run(16)) {
192
if (t.is_in_past()) {
193
LOG(FATAL) << "failed to receive packets: remaining=" << remaining;
197
LOG(ERROR) << "success. Time=" << (td::Clocks::system() - f);
200
td::rmrf(db_root_).ensure();