Ton

Форк
0
/
test-rldp2.cpp 
203 строки · 7.8 Кб
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-2020 Telegram Systems LLP
27
*/
28
#include "adnl/adnl-network-manager.h"
29
#include "adnl/adnl-test-loopback-implementation.h"
30
#include "adnl/adnl.h"
31
#include "rldp2/rldp.h"
32

33
#include "td/utils/port/signals.h"
34
#include "td/utils/port/path.h"
35
#include "td/utils/Random.h"
36

37
#include <memory>
38
#include <set>
39

40
int main() {
41
  SET_VERBOSITY_LEVEL(verbosity_INFO);
42

43
  std::string db_root_ = "tmp-dir-test-rldp2";
44
  td::rmrf(db_root_).ignore();
45
  td::mkdir(db_root_).ensure();
46

47
  td::set_default_failure_signal_handler().ensure();
48

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;
53

54
  ton::adnl::AdnlNodeIdShort src;
55
  ton::adnl::AdnlNodeIdShort dst;
56

57
  td::actor::Scheduler scheduler({0});
58

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());
65

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) {});
70

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) {});
75

76
    auto addr = ton::adnl::TestLoopbackNetworkManager::generate_dummy_addr_list();
77

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);
82

83
    td::actor::send_closure(adnl, &ton::adnl::Adnl::add_peer, src, ton::adnl::AdnlNodeIdFull{pub2}, addr);
84

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);
87
  });
88

89
  auto send_packet = [&](td::uint32 i) {
90
    td::BufferSlice d{5};
91
    d.as_slice()[0] = '1';
92
    d.as_slice().remove_prefix(1).copy_from(td::Slice{reinterpret_cast<td::uint8 *>(&i), 4});
93

94
    return d;
95
  };
96

97
  std::atomic<td::uint32> remaining{0};
98
  scheduler.run_in_context([&] {
99
    class Callback : public ton::adnl::Adnl::Callback {
100
     public:
101
      void receive_message(ton::adnl::AdnlNodeIdShort src, ton::adnl::AdnlNodeIdShort dst,
102
                           td::BufferSlice data) override {
103
        CHECK(src == src);
104
        CHECK(dst == dst);
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()));
108
        }
109
        CHECK(remaining_ > 0);
110
        remaining_--;
111
      }
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);
115

116
        td::uint32 s = *reinterpret_cast<const td::uint32 *>(data.as_slice().remove_prefix(1).begin());
117

118
        td::BufferSlice d{s};
119
        if (s >= 4) {
120
          td::Random::secure_bytes(d.as_slice().truncate(s - 4));
121
          auto x = td::crc32c(d.as_slice().truncate(d.size() - 4));
122

123
          d.as_slice().remove_prefix(d.size() - 4).copy_from(td::Slice{reinterpret_cast<td::uint8 *>(&x), 4});
124
        } else {
125
          td::Random::secure_bytes(d.as_slice());
126
        }
127

128
        promise.set_value(std::move(d));
129
      }
130
      Callback(std::atomic<td::uint32> &remaining) : remaining_(remaining) {
131
      }
132

133
     private:
134
      std::atomic<td::uint32> &remaining_;
135
    };
136
    td::actor::send_closure(adnl, &ton::adnl::Adnl::subscribe, dst, "1", std::make_unique<Callback>(remaining));
137
  });
138

139
  std::vector<td::uint32> sizes{1, 1024, 1 << 20, 2 << 20, 3 << 20, 10 << 20, 16 << 20};
140

141
  for (auto &size : sizes) {
142
    LOG(ERROR) << "testing delivering of packet of size " << size;
143

144
    auto f = td::Clocks::system();
145
    scheduler.run_in_context([&] {
146
      remaining++;
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) {
149
                                R.ensure();
150
                                remaining--;
151
                              }),
152
                              td::Timestamp::in(1024.0), send_packet(size), size + 1024);
153
    });
154

155
    auto t = td::Timestamp::in(1024.0);
156
    while (scheduler.run(16)) {
157
      if (!remaining) {
158
        break;
159
      }
160
      if (t.is_in_past()) {
161
        LOG(FATAL) << "failed to receive packets: remaining=" << remaining;
162
      }
163
    }
164

165
    LOG(ERROR) << "success. Time=" << (td::Clocks::system() - f);
166
  }
167

168
  scheduler.run_in_context([&] {
169
    td::actor::send_closure(network_manager, &ton::adnl::TestLoopbackNetworkManager::set_loss_probability, 0.1);
170
  });
171
  LOG(ERROR) << "set loss to 10%";
172

173
  for (auto &size : sizes) {
174
    LOG(ERROR) << "testing delivering of packet of size " << size;
175

176
    auto f = td::Clocks::system();
177
    scheduler.run_in_context([&] {
178
      remaining++;
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) {
181
                                R.ensure();
182
                                remaining--;
183
                              }),
184
                              td::Timestamp::in(1024.0), send_packet(size), size + 1024);
185
    });
186

187
    auto t = td::Timestamp::in(1024.0);
188
    while (scheduler.run(16)) {
189
      if (!remaining) {
190
        break;
191
      }
192
      if (t.is_in_past()) {
193
        LOG(FATAL) << "failed to receive packets: remaining=" << remaining;
194
      }
195
    }
196

197
    LOG(ERROR) << "success. Time=" << (td::Clocks::system() - f);
198
  }
199

200
  td::rmrf(db_root_).ensure();
201
  std::_Exit(0);
202
  return 0;
203
}
204

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.