forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-catchain.cpp
321 lines (271 loc) · 10.8 KB
/
test-catchain.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
/*
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 "adnl/adnl-test-loopback-implementation.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/port/path.h"
#include "td/utils/Random.h"
#include "td/utils/port/signals.h"
#include "td/utils/port/FileFd.h"
#include "td/utils/overloaded.h"
#include "catchain/catchain.h"
#include "common/errorlog.h"
#if TD_DARWIN || TD_LINUX
#include <unistd.h>
#endif
#include <iostream>
#include <sstream>
#include <set>
struct Node {
ton::PublicKeyHash id;
ton::PublicKey id_full;
ton::adnl::AdnlNodeIdShort adnl_id;
ton::adnl::AdnlNodeIdFull adnl_id_full;
};
class CatChainInst : public td::actor::Actor {
public:
class PayloadExtra : public ton::catchain::CatChainBlock::Extra {
public:
PayloadExtra(td::uint64 sum) : sum(sum) {
}
td::uint64 sum;
};
void process_blocks(std::vector<ton::catchain::CatChainBlock *> blocks) {
td::uint64 sum = sum_;
for (auto &B : blocks) {
auto E = dynamic_cast<const PayloadExtra *>(B->extra());
CHECK(E);
sum = std::max(sum, E->sum);
}
td::uint64 value = td::Random::fast_uint64();
sum = std::max(sum, value);
td::uint64 x[2];
x[0] = value;
x[1] = sum;
sum_ = sum;
td::actor::send_closure(catchain_, &ton::catchain::CatChain::processed_block,
td::BufferSlice{td::Slice{reinterpret_cast<char *>(x), 16}});
alarm_timestamp() = td::Timestamp::in(0.1);
height_++;
prev_values_.push_back(sum_);
}
void finished_processing() {
}
void preprocess_block(ton::catchain::CatChainBlock *block) {
td::uint64 sum = 0;
auto prev = block->prev();
if (prev) {
auto E = dynamic_cast<const PayloadExtra *>(prev->extra());
CHECK(E);
sum = std::max(E->sum, sum);
}
for (auto &B : block->deps()) {
auto E = dynamic_cast<const PayloadExtra *>(B->extra());
CHECK(E);
sum = std::max(E->sum, sum);
}
auto &payload = block->payload();
if (!payload.empty()) {
CHECK(payload.size() == 16);
td::uint64 x[2];
td::MutableSlice{reinterpret_cast<td::uint8 *>(x), 16}.copy_from(payload.as_slice());
sum = std::max(sum, x[0]);
LOG_CHECK(sum == x[1]) << sum << " " << x[0];
} else {
CHECK(!block->deps().size());
}
block->set_extra(std::make_unique<PayloadExtra>(sum));
}
void alarm() override {
td::actor::send_closure(catchain_, &ton::catchain::CatChain::need_new_block, td::Timestamp::in(0.1));
}
void start_up() override {
alarm_timestamp() = td::Timestamp::in(0.1);
ton::catchain::CatChainOptions opts;
opts.debug_disable_db = true;
std::vector<ton::catchain::CatChainNode> nodes;
for (auto &n : nodes_) {
nodes.push_back(ton::catchain::CatChainNode{n.adnl_id, n.id_full});
}
catchain_ =
ton::catchain::CatChain::create(make_callback(), opts, keyring_, adnl_, overlay_manager_, std::move(nodes),
nodes_[idx_].id, unique_hash_, std::string(""), "", false);
}
CatChainInst(td::actor::ActorId<ton::keyring::Keyring> keyring, td::actor::ActorId<ton::adnl::Adnl> adnl,
td::actor::ActorId<ton::overlay::Overlays> overlay_manager, std::vector<Node> nodes, td::uint32 idx,
ton::catchain::CatChainSessionId unique_hash)
: keyring_(keyring)
, adnl_(adnl)
, overlay_manager_(overlay_manager)
, nodes_(std::move(nodes))
, idx_(idx)
, unique_hash_(unique_hash) {
}
std::unique_ptr<ton::catchain::CatChain::Callback> make_callback() {
class Callback : public ton::catchain::CatChain::Callback {
public:
void process_blocks(std::vector<ton::catchain::CatChainBlock *> blocks) override {
td::actor::send_closure(id_, &CatChainInst::process_blocks, std::move(blocks));
}
void finished_processing() override {
td::actor::send_closure(id_, &CatChainInst::finished_processing);
}
void preprocess_block(ton::catchain::CatChainBlock *block) override {
td::actor::send_closure(id_, &CatChainInst::preprocess_block, std::move(block));
}
void process_broadcast(ton::PublicKeyHash src, td::BufferSlice data) override {
UNREACHABLE();
}
void process_message(ton::PublicKeyHash src, td::BufferSlice data) override {
UNREACHABLE();
}
void process_query(ton::PublicKeyHash src, td::BufferSlice data, td::Promise<td::BufferSlice> promise) override {
UNREACHABLE();
}
void started() override {
}
Callback(td::actor::ActorId<CatChainInst> id) : id_(std::move(id)) {
}
private:
td::actor::ActorId<CatChainInst> id_;
};
return std::make_unique<Callback>(actor_id(this));
}
td::uint64 value() {
return sum_;
}
void create_fork() {
auto height = height_ - 1; //td::Random::fast(0, height_ - 1);
auto sum = prev_values_[height] + 1;
td::uint64 x[2];
x[0] = sum + 1;
x[1] = sum + 1;
td::actor::send_closure(catchain_, &ton::catchain::CatChain::debug_add_fork,
td::BufferSlice{td::Slice{reinterpret_cast<char *>(x), 16}}, height + 1);
}
private:
td::actor::ActorId<ton::keyring::Keyring> keyring_;
td::actor::ActorId<ton::adnl::Adnl> adnl_;
td::actor::ActorId<ton::overlay::Overlays> overlay_manager_;
std::vector<Node> nodes_;
td::uint32 idx_;
ton::catchain::CatChainSessionId unique_hash_;
td::actor::ActorOwn<ton::catchain::CatChain> catchain_;
td::uint64 sum_ = 0;
td::uint32 height_ = 0;
std::vector<td::uint64> prev_values_;
};
static std::vector<Node> nodes;
static td::uint32 total_nodes = 11;
int main(int argc, char *argv[]) {
SET_VERBOSITY_LEVEL(verbosity_INFO);
td::set_default_failure_signal_handler().ensure();
std::string db_root_ = "tmp-ee";
td::rmrf(db_root_).ignore();
td::mkdir(db_root_).ensure();
td::set_default_failure_signal_handler().ensure();
td::actor::ActorOwn<ton::keyring::Keyring> keyring;
td::actor::ActorOwn<ton::adnl::TestLoopbackNetworkManager> network_manager;
td::actor::ActorOwn<ton::adnl::Adnl> adnl;
td::actor::ActorOwn<ton::overlay::Overlays> overlay_manager;
td::actor::Scheduler scheduler({7});
scheduler.run_in_context([&] {
ton::errorlog::ErrorLog::create(db_root_);
keyring = ton::keyring::Keyring::create(db_root_);
network_manager = td::actor::create_actor<ton::adnl::TestLoopbackNetworkManager>("test net");
adnl = ton::adnl::Adnl::create(db_root_, keyring.get());
overlay_manager =
ton::overlay::Overlays::create(db_root_, keyring.get(), adnl.get(), td::actor::ActorId<ton::dht::Dht>{});
td::actor::send_closure(adnl, &ton::adnl::Adnl::register_network_manager, network_manager.get());
});
for (td::uint32 att = 0; att < 10; att++) {
nodes.resize(total_nodes);
scheduler.run_in_context([&] {
auto addr = ton::adnl::TestLoopbackNetworkManager::generate_dummy_addr_list();
for (auto &n : nodes) {
auto pk1 = ton::PrivateKey{ton::privkeys::Ed25519::random()};
auto pub1 = pk1.compute_public_key();
n.adnl_id_full = ton::adnl::AdnlNodeIdFull{pub1};
n.adnl_id = ton::adnl::AdnlNodeIdShort{pub1.compute_short_id()};
td::actor::send_closure(keyring, &ton::keyring::Keyring::add_key, std::move(pk1), true, [](td::Unit) {});
td::actor::send_closure(adnl, &ton::adnl::Adnl::add_id, ton::adnl::AdnlNodeIdFull{pub1}, addr,
static_cast<td::uint8>(0));
td::actor::send_closure(network_manager, &ton::adnl::TestLoopbackNetworkManager::add_node_id, n.adnl_id, true,
true);
auto pk2 = ton::PrivateKey{ton::privkeys::Ed25519::random()};
auto pub2 = pk2.compute_public_key();
n.id_full = pub2;
n.id = pub2.compute_short_id();
td::actor::send_closure(keyring, &ton::keyring::Keyring::add_key, std::move(pk2), true, [](td::Unit) {});
LOG(DEBUG) << "created node " << n.adnl_id << " " << n.id;
}
for (auto &n1 : nodes) {
for (auto &n2 : nodes) {
td::actor::send_closure(adnl, &ton::adnl::Adnl::add_peer, n1.adnl_id, n2.adnl_id_full, addr);
}
}
});
auto t = td::Timestamp::in(1.0);
ton::catchain::CatChainSessionId unique_id;
td::Random::secure_bytes(unique_id.as_slice());
std::vector<td::actor::ActorOwn<CatChainInst>> inst;
scheduler.run_in_context([&] {
for (td::uint32 idx = 0; idx < total_nodes; idx++) {
inst.push_back(td::actor::create_actor<CatChainInst>("inst", keyring.get(), adnl.get(), overlay_manager.get(),
nodes, idx, unique_id));
}
});
t = td::Timestamp::in(10.0);
while (scheduler.run(1)) {
if (t.is_in_past()) {
break;
}
}
for (auto &n : inst) {
std::cout << "value=" << n.get_actor_unsafe().value() << std::endl;
}
scheduler.run_in_context([&] { td::actor::send_closure(inst[0], &CatChainInst::create_fork); });
t = td::Timestamp::in(10.0);
while (scheduler.run(1)) {
if (t.is_in_past()) {
break;
}
}
for (auto &n : inst) {
std::cout << "value=" << n.get_actor_unsafe().value() << std::endl;
}
scheduler.run_in_context([&] {
nodes.clear();
inst.clear();
});
}
td::rmrf(db_root_).ensure();
std::_Exit(0);
return 0;
}