forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht-in.hpp
168 lines (128 loc) · 5.57 KB
/
dht-in.hpp
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
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2019 Telegram Systems LLP
*/
#pragma once
#include "dht.hpp"
#include "td/db/KeyValueAsync.h"
#include <map>
namespace ton {
namespace dht {
class DhtRemoteNode;
class DhtBucket;
class DhtMemberImpl : public DhtMember {
private:
class DhtKeyValueLru : public td::ListNode {
public:
DhtKeyValueLru(DhtValue value) : kv_(std::move(value)) {
}
DhtValue kv_;
static inline DhtKeyValueLru *from_list_node(ListNode *node) {
return static_cast<DhtKeyValueLru *>(node);
}
};
//std::unique_ptr<adnl::AdnlDecryptor> decryptor_;
adnl::AdnlNodeIdShort id_;
DhtKeyId key_;
td::uint32 k_;
td::uint32 a_;
td::uint32 max_cache_time_ = 60;
td::uint32 max_cache_size_ = 100;
std::vector<DhtBucket> buckets_;
std::string db_root_;
// to be republished once in a while
std::map<DhtKeyId, DhtValue> our_values_;
std::map<DhtKeyId, DhtKeyValueLru> cached_values_;
td::ListNode cached_values_lru_;
std::map<DhtKeyId, DhtValue> values_;
td::Timestamp fill_att_ = td::Timestamp::in(0);
td::Timestamp republish_att_ = td::Timestamp::in(0);
DhtKeyId last_republish_key_ = DhtKeyId::zero();
DhtKeyId last_check_key_ = DhtKeyId::zero();
class Callback : public adnl::Adnl::Callback {
public:
void receive_message(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::BufferSlice data) override {
CHECK(dst == id_);
td::actor::send_closure(self_, &DhtMemberImpl::receive_message, src, std::move(data));
}
void receive_query(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::BufferSlice data,
td::Promise<td::BufferSlice> promise) override {
CHECK(dst == id_);
td::actor::send_closure(self_, &DhtMemberImpl::receive_query, src, std::move(data), std::move(promise));
}
Callback(td::actor::ActorId<DhtMemberImpl> self, adnl::AdnlNodeIdShort id) : self_(self), id_(id) {
}
private:
td::actor::ActorId<DhtMemberImpl> self_;
adnl::AdnlNodeIdShort id_;
};
void receive_query(adnl::AdnlNodeIdShort src, td::BufferSlice data, td::Promise<td::BufferSlice> promise);
void receive_message(adnl::AdnlNodeIdShort src, td::BufferSlice data);
td::actor::ActorId<keyring::Keyring> keyring_;
td::actor::ActorId<adnl::Adnl> adnl_;
td::uint64 ping_queries_{0};
td::uint64 find_node_queries_{0};
td::uint64 find_value_queries_{0};
td::uint64 store_queries_{0};
td::uint64 get_addr_list_queries_{0};
using DbType = td::KeyValueAsync<td::Bits256, td::BufferSlice>;
DbType db_;
td::Timestamp next_save_to_db_at_ = td::Timestamp::in(10.0);
void save_to_db();
DhtNodesList get_nearest_nodes(DhtKeyId id, td::uint32 k);
void check();
template <class T>
void process_query(adnl::AdnlNodeIdShort src, T &query, td::Promise<td::BufferSlice> promise) {
promise.set_error(td::Status::Error(ErrorCode::protoviolation, "bad DHT query"));
}
void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_ping &query, td::Promise<td::BufferSlice> promise);
void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_findNode &query, td::Promise<td::BufferSlice> promise);
void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_findValue &query, td::Promise<td::BufferSlice> promise);
void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_store &query, td::Promise<td::BufferSlice> promise);
void process_query(adnl::AdnlNodeIdShort src, ton_api::dht_getSignedAddressList &query,
td::Promise<td::BufferSlice> promise);
public:
DhtMemberImpl(adnl::AdnlNodeIdShort id, std::string db_root, td::actor::ActorId<keyring::Keyring> keyring,
td::actor::ActorId<adnl::Adnl> adnl, td::uint32 k, td::uint32 a = 3)
: id_(id), key_{id_}, k_(k), a_(a), db_root_(db_root), keyring_(keyring), adnl_(adnl) {
for (size_t i = 0; i < 256; i++) {
buckets_.emplace_back(k_);
}
}
void add_full_node(DhtKeyId id, DhtNode node) override;
adnl::AdnlNodeIdShort get_id() const override {
return id_;
}
void receive_ping(DhtKeyId id, DhtNode result) override;
void set_value(DhtValue key_value, td::Promise<td::Unit> result) override;
td::uint32 distance(DhtKeyId key_id, td::uint32 max_value);
td::Status store_in(DhtValue value) override;
void send_store(DhtValue value, td::Promise<td::Unit> promise);
void get_value_in(DhtKeyId key, td::Promise<DhtValue> result) override;
void get_value(DhtKey key, td::Promise<DhtValue> result) override {
get_value_in(key.compute_key_id(), std::move(result));
}
void alarm() override {
alarm_timestamp() = td::Timestamp::in(1.0);
check();
}
void start_up() override;
void tear_down() override;
void dump(td::StringBuilder &sb) const override;
PrintId print_id() const override {
return PrintId{id_};
}
void get_self_node(td::Promise<DhtNode> promise) override;
};
} // namespace dht
} // namespace ton