forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht-node.hpp
114 lines (96 loc) · 3.13 KB
/
dht-node.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
/*
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 "adnl/adnl-node-id.hpp"
#include "adnl/adnl-address-list.hpp"
#include "dht-types.h"
namespace ton {
namespace dht {
class DhtNode {
private:
adnl::AdnlNodeIdFull id_;
adnl::AdnlAddressList addr_list_;
td::int32 version_{0};
td::SharedSlice signature_;
public:
DhtNode() {
}
DhtNode(adnl::AdnlNodeIdFull id, adnl::AdnlAddressList addr_list, td::int32 version, td::BufferSlice signature)
: id_(id), addr_list_(addr_list), version_(version), signature_(signature.as_slice()) {
}
DhtNode(adnl::AdnlNodeIdFull id, adnl::AdnlAddressList addr_list, td::int32 version, td::SharedSlice signature)
: id_(id), addr_list_(addr_list), version_(version), signature_(std::move(signature)) {
}
static td::Result<DhtNode> create(tl_object_ptr<ton_api::dht_node> obj) {
if (obj->version_ == 0) {
return td::Status::Error(ErrorCode::protoviolation, "zero version");
}
DhtNode n;
TRY_STATUS(n.update(std::move(obj)));
return std::move(n);
}
td::Status update(tl_object_ptr<ton_api::dht_node> obj);
DhtKeyId get_key() const {
CHECK(!id_.empty());
return DhtKeyId{id_.compute_short_id()};
}
adnl::AdnlNodeIdFull adnl_id() const {
return id_;
}
adnl::AdnlAddressList addr_list() const {
return addr_list_;
}
td::int32 version() const {
return version_;
}
tl_object_ptr<ton_api::dht_node> tl() const {
return create_tl_object<ton_api::dht_node>(id_.tl(), addr_list_.tl(), version_, signature_.clone_as_buffer_slice());
}
DhtNode clone() const {
return DhtNode{id_, addr_list_, version_, signature_.clone()};
}
};
class DhtNodesList {
public:
DhtNodesList() {
}
DhtNodesList(tl_object_ptr<ton_api::dht_nodes> R) {
for (auto &n : R->nodes_) {
auto N = DhtNode::create(std::move(n));
if (N.is_ok()) {
list_.emplace_back(N.move_as_ok());
} else {
LOG(WARNING) << "bad dht node: " << N.move_as_error();
}
}
}
void push_back(DhtNode node) {
list_.emplace_back(std::move(node));
}
tl_object_ptr<ton_api::dht_nodes> tl() const;
std::vector<DhtNode> &list() {
return list_;
}
const std::vector<DhtNode> &list() const {
return list_;
}
td::uint32 size() const {
return static_cast<td::uint32>(list_.size());
}
private:
std::vector<DhtNode> list_;
};
} // namespace dht
} // namespace ton