forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht-types.h
272 lines (237 loc) · 8.21 KB
/
dht-types.h
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
/*
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-2020 Telegram Systems LLP
*/
#pragma once
#include "td/utils/int_types.h"
#include "td/utils/SharedSlice.h"
#include "adnl/adnl-node-id.hpp"
#include "tl-utils/tl-utils.hpp"
#include "common/io.hpp"
namespace ton {
namespace dht {
using DhtKeyName = std::string;
class DhtKeyId {
public:
explicit DhtKeyId(td::Bits256 value) : value_(value) {
}
explicit DhtKeyId(adnl::AdnlNodeIdShort value) : value_(value.bits256_value()) {
}
DhtKeyId() {
}
td::Bits256 tl() const {
return value_;
}
bool operator<(const DhtKeyId &with) const {
return value_ < with.value_;
}
bool operator==(const DhtKeyId &with) const {
return value_ == with.value_;
}
bool operator!=(const DhtKeyId &with) const {
return value_ != with.value_;
}
DhtKeyId operator^(const DhtKeyId &with) const {
return DhtKeyId{value_ ^ with.value_};
}
DhtKeyId operator^(const adnl::AdnlNodeIdShort &with) const {
return DhtKeyId{value_ ^ with.bits256_value()};
}
bool get_bit(td::uint32 bit) const {
return value_[bit];
}
td::uint32 count_leading_zeroes() const {
return value_.count_leading_zeroes();
}
adnl::AdnlNodeIdShort to_adnl() const {
return adnl::AdnlNodeIdShort{value_};
}
static DhtKeyId zero() {
return DhtKeyId{td::Bits256::zero()};
}
private:
td::Bits256 value_;
};
using DhtXoredKeyId = DhtKeyId;
class DhtKey {
public:
static constexpr td::uint32 max_name_length() {
return 127;
}
static constexpr td::uint32 max_index() {
return 15;
}
DhtKey(PublicKeyHash id, DhtKeyName namestr, td::uint32 idx)
: id_(std::move(id)), namestr_(std::move(namestr)), idx_(idx) {
}
static td::Result<DhtKey> create(tl_object_ptr<ton_api::dht_key> key);
td::Status check() const;
const auto &public_key_hash() const {
return id_;
}
const auto &name() const {
return namestr_;
}
td::uint32 idx() const {
return idx_;
}
tl_object_ptr<ton_api::dht_key> tl() const;
DhtKeyId compute_key_id() const;
DhtKey clone() const;
private:
PublicKeyHash id_;
DhtKeyName namestr_;
td::uint32 idx_;
};
class DhtValue;
class DhtUpdateRule {
public:
virtual ~DhtUpdateRule() = default;
virtual td::Status check_value(const DhtValue &value) = 0;
virtual td::Status update_value(DhtValue &value, DhtValue &&new_value) = 0;
virtual bool need_republish() const = 0;
virtual bool check_is_acceptable(const DhtValue &value) {
return true;
}
virtual tl_object_ptr<ton_api::dht_UpdateRule> tl() const = 0;
static td::Result<std::shared_ptr<DhtUpdateRule>> create(tl_object_ptr<ton_api::dht_UpdateRule> obj);
};
class DhtKeyDescription {
public:
DhtKeyDescription(DhtKey key, PublicKey public_key, std::shared_ptr<DhtUpdateRule> update_rule,
td::BufferSlice signature)
: key_(std::move(key))
, public_key_(std::move(public_key))
, update_rule_(std::move(update_rule))
, signature_(signature.as_slice()) {
}
DhtKeyDescription(DhtKey key, PublicKey public_key, std::shared_ptr<DhtUpdateRule> update_rule,
td::SharedSlice signature)
: key_(std::move(key))
, public_key_(std::move(public_key))
, update_rule_(std::move(update_rule))
, signature_(std::move(signature)) {
}
const auto &key() const {
return key_;
}
const auto &public_key() const {
return public_key_;
}
const auto &update_rule() const {
return update_rule_;
}
void update_signature(td::BufferSlice signature);
void update_signature(td::SharedSlice signature);
td::BufferSlice to_sign() const;
td::Status check() const;
tl_object_ptr<ton_api::dht_keyDescription> tl() const;
DhtKeyDescription clone() const;
static td::Result<DhtKeyDescription> create(tl_object_ptr<ton_api::dht_keyDescription> desc, bool check_signature);
static td::Result<DhtKeyDescription> create(DhtKey key, PublicKey public_key,
std::shared_ptr<DhtUpdateRule> update_rule, td::BufferSlice signature);
static td::Result<DhtKeyDescription> create(DhtKey key, PublicKey public_key,
std::shared_ptr<DhtUpdateRule> update_rule, td::SharedSlice signature);
private:
DhtKey key_;
PublicKey public_key_;
std::shared_ptr<DhtUpdateRule> update_rule_;
td::SharedSlice signature_;
};
class DhtValue {
public:
static constexpr td::uint32 max_value_size() {
return 768;
}
DhtValue(DhtKeyDescription key, td::BufferSlice value, td::uint32 ttl, td::BufferSlice signature)
: key_(std::move(key)), value_(value.as_slice()), ttl_(ttl), signature_(signature.as_slice()) {
}
DhtValue(DhtKeyDescription key, td::SharedSlice value, td::uint32 ttl, td::SharedSlice signature)
: key_(std::move(key)), value_(std::move(value)), ttl_(ttl), signature_(std::move(signature)) {
}
static td::Result<DhtValue> create(tl_object_ptr<ton_api::dht_value> obj, bool check_signature);
static td::Result<DhtValue> create(DhtKeyDescription key, td::BufferSlice value, td::uint32 ttl,
td::BufferSlice signature);
static td::Result<DhtValue> create(DhtKeyDescription key, td::SharedSlice value, td::uint32 ttl,
td::SharedSlice signature);
const auto &key() const {
return key_;
}
const auto &value() const {
return value_;
}
const auto &signature() const {
return signature_;
}
td::uint32 ttl() const {
return ttl_;
}
bool expired() const {
return ttl_ < td::Clocks::system();
}
DhtValue clone() const;
tl_object_ptr<ton_api::dht_value> tl() const;
td::BufferSlice to_sign() const;
td::Status update(DhtValue &&value);
void set(td::BufferSlice value, td::uint32 ttl, td::BufferSlice signature);
void set(td::SharedSlice value, td::uint32 ttl, td::SharedSlice signature);
void update_signature(td::BufferSlice signature);
void update_signature(td::SharedSlice signature);
td::Status check() const;
bool check_is_acceptable() const;
DhtKeyId key_id() const;
private:
DhtKeyDescription key_;
td::SharedSlice value_;
td::uint32 ttl_;
td::SharedSlice signature_;
};
class DhtUpdateRuleSignature : public DhtUpdateRule {
public:
td::Status check_value(const DhtValue &value) override;
td::Status update_value(DhtValue &value, DhtValue &&new_value) override;
bool need_republish() const override {
return true;
}
tl_object_ptr<ton_api::dht_UpdateRule> tl() const override;
static td::Result<std::shared_ptr<DhtUpdateRule>> create();
};
class DhtUpdateRuleAnybody : public DhtUpdateRule {
public:
td::Status check_value(const DhtValue &value) override;
td::Status update_value(DhtValue &value, DhtValue &&new_value) override;
bool need_republish() const override {
return false;
}
tl_object_ptr<ton_api::dht_UpdateRule> tl() const override;
static td::Result<std::shared_ptr<DhtUpdateRule>> create();
};
class DhtUpdateRuleOverlayNodes : public DhtUpdateRule {
public:
td::Status check_value(const DhtValue &value) override;
td::Status update_value(DhtValue &value, DhtValue &&new_value) override;
bool need_republish() const override {
return false;
}
bool check_is_acceptable(const DhtValue &value) override;
tl_object_ptr<ton_api::dht_UpdateRule> tl() const override;
static td::Result<std::shared_ptr<DhtUpdateRule>> create();
};
} // namespace dht
} // namespace ton
namespace td {
inline td::StringBuilder &operator<<(td::StringBuilder &sb, const ton::dht::DhtKeyId &dht) {
sb << dht.tl();
return sb;
}
} // namespace td