/*
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 .
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 create(tl_object_ptr 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 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 tl() const {
return create_tl_object(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 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 tl() const;
std::vector &list() {
return list_;
}
const std::vector &list() const {
return list_;
}
td::uint32 size() const {
return static_cast(list_.size());
}
private:
std::vector list_;
};
} // namespace dht
} // namespace ton