1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

initial commit

This commit is contained in:
initial commit 2019-09-07 14:03:22 +04:00 committed by vvaltman
commit c2da007f40
1610 changed files with 398047 additions and 0 deletions

25
rldp/CMakeLists.txt Normal file
View file

@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
if (NOT OPENSSL_FOUND)
find_package(OpenSSL REQUIRED)
endif()
set(RLDP_SOURCE
rldp.cpp
rldp-peer.cpp
rldp.h
rldp.hpp
rldp-peer.h
rldp-peer.hpp
)
add_library(rldp STATIC ${RLDP_SOURCE})
target_include_directories(rldp PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/..
${OPENSSL_INCLUDE_DIR}
)
target_link_libraries(rldp PRIVATE tdutils tdactor fec adnl tl_api)

126
rldp/rldp-in.hpp Normal file
View file

@ -0,0 +1,126 @@
/*
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 "rldp.hpp"
#include "rldp-peer.h"
#include "tl-utils/tl-utils.hpp"
#include "adnl/adnl-query.h"
#include "adnl/adnl-peer-table.h"
#include "td/utils/List.h"
#include <map>
#include <set>
namespace ton {
namespace rldp {
class RldpLru : public td::ListNode {
public:
TransferId transfer_id() {
return transfer_id_;
}
RldpLru(TransferId transfer_id) : transfer_id_(transfer_id) {
}
RldpLru() {
}
static RldpLru *from_list_node(td::ListNode *node) {
return static_cast<RldpLru *>(node);
}
private:
TransferId transfer_id_;
};
class RldpIn : public RldpImpl {
public:
static constexpr td::uint64 mtu() {
return (1ull << 37);
}
static constexpr td::uint32 lru_size() {
return 128;
}
TransferId transfer(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::Timestamp timeout, td::BufferSlice data,
TransferId t = TransferId::zero());
void transfer_completed(TransferId transfer_id) override;
void send_message(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::BufferSlice data) override;
void send_message_ex(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::Timestamp timeout,
td::BufferSlice data) override;
void send_query(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, std::string name,
td::Promise<td::BufferSlice> promise, td::Timestamp timeout, td::BufferSlice data) override {
send_query_ex(src, dst, name, std::move(promise), timeout, std::move(data), default_mtu());
}
void send_query_ex(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, std::string name,
td::Promise<td::BufferSlice> promise, td::Timestamp timeout, td::BufferSlice data,
td::uint64 max_answer_size) override;
void answer_query(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::Timestamp timeout,
adnl::AdnlQueryId query_id, TransferId transfer_id, td::BufferSlice data);
void alarm_query(adnl::AdnlQueryId query_id, TransferId transfer_id);
void process_message_part(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id,
ton_api::rldp_messagePart &part);
void process_message_part(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, ton_api::rldp_confirm &part);
void process_message_part(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, ton_api::rldp_complete &part);
void receive_message_part(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, td::BufferSlice data);
void process_message(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, TransferId transfer_id,
ton_api::rldp_message &message);
void process_message(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, TransferId transfer_id,
ton_api::rldp_query &message);
void process_message(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, TransferId transfer_id,
ton_api::rldp_answer &message);
void receive_message(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, TransferId transfer_id,
td::BufferSlice data);
void in_transfer_completed(TransferId transfer_id);
void add_id(adnl::AdnlNodeIdShort local_id) override;
RldpIn(td::actor::ActorId<adnl::AdnlPeerTable> adnl) : adnl_(adnl) {
}
private:
std::unique_ptr<adnl::Adnl::Callback> make_adnl_callback();
td::actor::ActorId<adnl::AdnlPeerTable> adnl_;
std::map<adnl::AdnlQueryId, td::actor::ActorOwn<adnl::AdnlQuery>> queries_;
std::map<TransferId, td::actor::ActorOwn<RldpTransferSender>> senders_;
std::map<TransferId, td::actor::ActorOwn<RldpTransferReceiver>> receivers_;
std::set<TransferId> lru_set_;
RldpLru lru_;
td::uint32 lru_size_ = 0;
std::map<TransferId, td::uint64> max_size_;
std::set<adnl::AdnlNodeIdShort> local_ids_;
};
} // namespace rldp
} // namespace ton

193
rldp/rldp-peer.cpp Normal file
View file

@ -0,0 +1,193 @@
/*
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
*/
#include "rldp-peer.hpp"
#include "adnl/utils.hpp"
#include "td/utils/Random.h"
#include "td/utils/overloaded.h"
#include "auto/tl/ton_api.hpp"
#include "common/io.hpp"
namespace ton {
namespace rldp {
void RldpTransferSenderImpl::finish() {
td::actor::send_closure(rldp_, &RldpImpl::transfer_completed, transfer_id_);
stop();
}
void RldpTransferSenderImpl::create_encoder() {
if (part_ * slice_size() >= data_.size()) {
finish();
return;
}
td::BufferSlice D = data_.clone();
D.confirm_read(part_ * slice_size());
if (D.size() > slice_size()) {
D.truncate(slice_size());
}
fec_type_ = td::fec::RaptorQEncoder::Parameters{D.size(), symbol_size(), 0};
auto E = fec_type_.create_encoder(std::move(D));
E.ensure();
encoder_ = E.move_as_ok();
seqno_ = 0;
confirmed_seqno_ = 0;
}
void RldpTransferSenderImpl::start_up() {
create_encoder();
alarm();
}
void RldpTransferSenderImpl::alarm() {
CHECK(confirmed_seqno_ <= seqno_);
if (timeout_.is_in_past()) {
finish();
return;
}
alarm_timestamp() = td::Timestamp::in(0.01);
send_part();
}
void RldpTransferSenderImpl::send_part() {
for (td::uint32 cnt = 0; cnt < 10; cnt++) {
if (seqno_ - confirmed_seqno_ <= window_size()) {
send_one_part(seqno_++);
} else {
send_one_part(seqno_);
break;
}
}
}
void RldpTransferSenderImpl::send_one_part(td::uint32 seqno) {
if (encoder_->get_info().ready_symbol_count <= seqno) {
encoder_->prepare_more_symbols();
}
auto symbol = encoder_->gen_symbol(seqno);
auto obj = create_tl_object<ton_api::rldp_messagePart>(transfer_id_, fec_type_.tl(), part_, data_.size(), seqno,
std::move(symbol.data));
td::actor::send_closure(adnl_, &adnl::Adnl::send_message, local_id_, peer_id_, serialize_tl_object(obj, true));
}
void RldpTransferSenderImpl::confirm(td::uint32 part, td::uint32 seqno) {
if (part == part_) {
if (seqno >= confirmed_seqno_ && seqno <= seqno_) {
confirmed_seqno_ = seqno;
}
}
}
void RldpTransferSenderImpl::complete(td::uint32 part) {
if (part == part_) {
part_++;
create_encoder();
}
}
td::actor::ActorOwn<RldpTransferSender> RldpTransferSender::create(
TransferId transfer_id, adnl::AdnlNodeIdShort local_id, adnl::AdnlNodeIdShort peer_id, td::BufferSlice data,
td::Timestamp timeout, td::actor::ActorId<RldpImpl> rldp, td::actor::ActorId<adnl::Adnl> adnl) {
return td::actor::create_actor<RldpTransferSenderImpl>("sender", transfer_id, local_id, peer_id, std::move(data),
timeout, rldp, adnl);
}
void RldpTransferReceiverImpl::receive_part(fec::FecType fec_type, td::uint32 part, td::uint64 total_size,
td::uint32 seqno, td::BufferSlice data) {
if (part < part_) {
auto obj = create_tl_object<ton_api::rldp_complete>(transfer_id_, part);
td::actor::send_closure(adnl_, &adnl::Adnl::send_message, local_id_, peer_id_, serialize_tl_object(obj, true));
return;
}
if (part > part_) {
return;
}
cnt_++;
if (seqno > max_seqno_) {
max_seqno_ = seqno;
}
if (!decoder_) {
auto D = fec_type.create_decoder();
if (D.is_error()) {
VLOG(RLDP_WARNING) << "failed to create decoder: " << D.move_as_error();
return;
}
decoder_ = D.move_as_ok();
}
decoder_->add_symbol(td::fec::Symbol{seqno, std::move(data)});
if (decoder_->may_try_decode()) {
auto D = decoder_->try_decode(false);
if (D.is_ok()) {
auto data = D.move_as_ok();
if (data.data.size() + offset_ > total_size_) {
abort(td::Status::Error(ErrorCode::protoviolation,
PSTRING() << "too big part: offset=" << offset_ << " total_size=" << total_size_
<< " data_size=" << data.data.size() << " part=" << part_));
return;
}
data_.as_slice().remove_prefix(offset_).copy_from(data.data.as_slice());
offset_ += data.data.size();
auto obj = create_tl_object<ton_api::rldp_complete>(transfer_id_, part_);
td::actor::send_closure(adnl_, &adnl::Adnl::send_message, local_id_, peer_id_, serialize_tl_object(obj, true));
part_++;
cnt_ = 0;
max_seqno_ = 0;
decoder_ = nullptr;
if (offset_ == total_size_) {
finish();
return;
}
}
}
if (cnt_ >= 10) {
auto obj = create_tl_object<ton_api::rldp_confirm>(transfer_id_, part_, max_seqno_);
td::actor::send_closure(adnl_, &adnl::Adnl::send_message, local_id_, peer_id_, serialize_tl_object(obj, true));
cnt_ = 0;
}
}
void RldpTransferReceiverImpl::abort(td::Status reason) {
VLOG(RLDP_NOTICE) << "aborted transfer receive: " << reason;
promise_.set_error(reason.move_as_error_prefix(PSTRING() << "rldptransfer " << transfer_id_ << ": "));
stop();
}
void RldpTransferReceiverImpl::finish() {
promise_.set_value(data_.clone());
stop();
}
void RldpTransferReceiverImpl::alarm() {
abort(td::Status::Error(ErrorCode::timeout, "timeout"));
}
td::actor::ActorOwn<RldpTransferReceiver> RldpTransferReceiver::create(
TransferId transfer_id, adnl::AdnlNodeIdShort local_id, adnl::AdnlNodeIdShort peer_id, td::uint64 total_size,
td::Timestamp timeout, td::actor::ActorId<RldpImpl> rldp, td::actor::ActorId<adnl::Adnl> adnl,
td::Promise<td::BufferSlice> promise) {
return td::actor::create_actor<RldpTransferReceiverImpl>("receiver", transfer_id, local_id, peer_id, total_size,
timeout, rldp, adnl, std::move(promise));
}
} // namespace rldp
} // namespace ton

61
rldp/rldp-peer.h Normal file
View file

@ -0,0 +1,61 @@
/*
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.h"
#include "fec/fec.h"
namespace ton {
namespace rldp {
class RldpImpl;
using TransferId = td::Bits256;
class RldpTransferSender : public td::actor::Actor {
public:
virtual ~RldpTransferSender() = default;
virtual void confirm(td::uint32 part, td::uint32 seqno) = 0;
virtual void complete(td::uint32 part) = 0;
static td::actor::ActorOwn<RldpTransferSender> create(TransferId transfer_id, adnl::AdnlNodeIdShort local_id,
adnl::AdnlNodeIdShort peer_id, td::BufferSlice data,
td::Timestamp timeout, td::actor::ActorId<RldpImpl> rldp,
td::actor::ActorId<adnl::Adnl> adnl);
};
class RldpTransferReceiver : public td::actor::Actor {
public:
virtual ~RldpTransferReceiver() = default;
virtual void receive_part(fec::FecType fec_type, td::uint32 part, td::uint64 total_size, td::uint32 seqno,
td::BufferSlice data) = 0;
static td::actor::ActorOwn<RldpTransferReceiver> create(TransferId transfer_id, adnl::AdnlNodeIdShort local_id,
adnl::AdnlNodeIdShort peer_id, td::uint64 total_size,
td::Timestamp timeout, td::actor::ActorId<RldpImpl> rldp,
td::actor::ActorId<adnl::Adnl> adnl,
td::Promise<td::BufferSlice> promise);
};
} // namespace rldp
} // namespace ton

133
rldp/rldp-peer.hpp Normal file
View file

@ -0,0 +1,133 @@
/*
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 "rldp-peer.h"
#include "fec/fec.h"
#include "rldp.hpp"
#include <map>
namespace ton {
namespace rldp {
class RldpTransferSenderImpl : public RldpTransferSender {
public:
static constexpr td::uint64 slice_size() {
return 2000000;
}
static constexpr td::uint32 symbol_size() {
return 768;
}
static constexpr td::uint32 window_size() {
return 1000;
}
void start_up() override;
void alarm() override;
void send_part();
void send_one_part(td::uint32 seqno);
void confirm(td::uint32 part, td::uint32 seqno) override;
void complete(td::uint32 part) override;
RldpTransferSenderImpl(TransferId transfer_id, adnl::AdnlNodeIdShort local_id, adnl::AdnlNodeIdShort peer_id,
td::BufferSlice data, td::Timestamp timeout, td::actor::ActorId<RldpImpl> rldp,
td::actor::ActorId<adnl::Adnl> adnl)
: transfer_id_(transfer_id)
, local_id_(local_id)
, peer_id_(peer_id)
, data_(std::move(data))
, timeout_(timeout)
, rldp_(rldp)
, adnl_(adnl) {
}
private:
void create_encoder();
void finish();
TransferId transfer_id_;
adnl::AdnlNodeIdShort local_id_;
adnl::AdnlNodeIdShort peer_id_;
td::uint32 seqno_ = 0;
td::uint32 confirmed_seqno_ = 0;
std::unique_ptr<td::fec::Encoder> encoder_;
fec::FecType fec_type_;
td::BufferSlice data_;
td::uint32 part_ = 0;
td::Timestamp timeout_;
td::actor::ActorId<RldpImpl> rldp_;
td::actor::ActorId<adnl::Adnl> adnl_;
};
class RldpTransferReceiverImpl : public RldpTransferReceiver {
public:
void receive_part(fec::FecType fec_type, td::uint32 part, td::uint64 total_size, td::uint32 seqno,
td::BufferSlice data) override;
void alarm() override;
void start_up() override {
data_ = td::BufferSlice(total_size_);
}
RldpTransferReceiverImpl(TransferId transfer_id, adnl::AdnlNodeIdShort local_id, adnl::AdnlNodeIdShort peer_id,
td::uint64 total_size, td::Timestamp timeout, td::actor::ActorId<RldpImpl> rldp,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<td::BufferSlice> promise)
: transfer_id_(transfer_id)
, local_id_(local_id)
, peer_id_(peer_id)
, total_size_(total_size)
, timeout_(timeout)
, rldp_(rldp)
, adnl_(adnl)
, promise_(std::move(promise)) {
}
private:
void abort(td::Status reason);
void finish();
TransferId transfer_id_;
adnl::AdnlNodeIdShort local_id_;
adnl::AdnlNodeIdShort peer_id_;
td::uint64 total_size_;
td::uint64 offset_ = 0;
td::uint32 part_ = 0;
td::uint32 cnt_ = 0;
td::uint32 max_seqno_ = 0;
td::BufferSlice data_;
std::unique_ptr<td::fec::Decoder> decoder_;
td::Timestamp timeout_;
td::actor::ActorId<RldpImpl> rldp_;
td::actor::ActorId<adnl::Adnl> adnl_;
td::Promise<td::BufferSlice> promise_;
};
} // namespace rldp
} // namespace ton

288
rldp/rldp.cpp Normal file
View file

@ -0,0 +1,288 @@
/*
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
*/
#include "rldp-in.hpp"
#include "auto/tl/ton_api.h"
#include "auto/tl/ton_api.hpp"
#include "td/utils/Random.h"
#include "fec/fec.h"
namespace ton {
namespace rldp {
TransferId RldpIn::transfer(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::Timestamp timeout,
td::BufferSlice data, TransferId t) {
TransferId transfer_id;
if (!t.is_zero()) {
transfer_id = t;
} else {
td::Random::secure_bytes(transfer_id.as_slice());
}
senders_.emplace(transfer_id,
RldpTransferSender::create(transfer_id, src, dst, std::move(data), timeout, actor_id(this), adnl_));
return transfer_id;
}
void RldpIn::send_message(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::BufferSlice data) {
td::Bits256 id;
td::Random::secure_bytes(id.as_slice());
auto B = serialize_tl_object(create_tl_object<ton_api::rldp_message>(id, std::move(data)), true);
transfer(src, dst, td::Timestamp::in(10.0), std::move(B));
}
void RldpIn::send_message_ex(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::Timestamp timeout,
td::BufferSlice data) {
td::Bits256 id;
td::Random::secure_bytes(id.as_slice());
auto B = serialize_tl_object(create_tl_object<ton_api::rldp_message>(id, std::move(data)), true);
transfer(src, dst, timeout, std::move(B));
}
void RldpIn::send_query_ex(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, std::string name,
td::Promise<td::BufferSlice> promise, td::Timestamp timeout, td::BufferSlice data,
td::uint64 max_answer_size) {
auto query_id = adnl::AdnlQuery::random_query_id();
auto date = static_cast<td::uint32>(timeout.at_unix()) + 1;
auto B = serialize_tl_object(create_tl_object<ton_api::rldp_query>(query_id, max_answer_size, date, std::move(data)),
true);
auto transfer_id = transfer(src, dst, timeout, std::move(B));
max_size_[transfer_id ^ TransferId::ones()] = max_answer_size;
auto Q = adnl::AdnlQuery::create(
std::move(promise),
[SelfId = actor_id(this), transfer_id](adnl::AdnlQueryId query_id) {
td::actor::send_closure(SelfId, &RldpIn::alarm_query, query_id, transfer_id);
},
name, timeout, query_id);
queries_.emplace(query_id, std::move(Q));
}
void RldpIn::answer_query(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::Timestamp timeout,
adnl::AdnlQueryId query_id, TransferId transfer_id, td::BufferSlice data) {
auto B = serialize_tl_object(create_tl_object<ton_api::rldp_answer>(query_id, std::move(data)), true);
transfer(src, dst, timeout, std::move(B), transfer_id);
}
void RldpIn::alarm_query(adnl::AdnlQueryId query_id, TransferId transfer_id) {
queries_.erase(query_id);
max_size_.erase(transfer_id);
}
void RldpIn::receive_message_part(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, td::BufferSlice data) {
auto F = fetch_tl_object<ton_api::rldp_MessagePart>(std::move(data), true);
if (F.is_error()) {
VLOG(RLDP_INFO) << "failed to parse rldp packet [" << source << "->" << local_id << "]: " << F.move_as_error();
return;
}
ton_api::downcast_call(*F.move_as_ok().get(), [&](auto &obj) { this->process_message_part(source, local_id, obj); });
}
void RldpIn::process_message_part(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id,
ton_api::rldp_messagePart &part) {
auto it = receivers_.find(part.transfer_id_);
if (it == receivers_.end()) {
if (part.part_ != 0) {
VLOG(RLDP_INFO) << "dropping new part";
return;
}
if (static_cast<td::uint64>(part.total_size_) > mtu()) {
VLOG(RLDP_NOTICE) << "dropping too big rldp packet of size=" << part.total_size_ << " mtu=" << mtu();
return;
}
auto ite = max_size_.find(part.transfer_id_);
if (ite == max_size_.end()) {
if (static_cast<td::uint64>(part.total_size_) > default_mtu()) {
VLOG(RLDP_NOTICE) << "dropping too big rldp packet of size=" << part.total_size_
<< " default_mtu=" << default_mtu();
return;
}
} else {
if (static_cast<td::uint64>(part.total_size_) > ite->second) {
VLOG(RLDP_NOTICE) << "dropping too big rldp packet of size=" << part.total_size_ << " allowed=" << ite->second;
return;
}
}
if (lru_set_.count(part.transfer_id_) == 1) {
auto obj = create_tl_object<ton_api::rldp_complete>(part.transfer_id_, part.part_);
td::actor::send_closure(adnl_, &adnl::Adnl::send_message, local_id, source, serialize_tl_object(obj, true));
return;
}
auto P = td::PromiseCreator::lambda(
[SelfId = actor_id(this), source, local_id, transfer_id = part.transfer_id_](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
VLOG(RLDP_INFO) << "failed to receive: " << R.move_as_error();
return;
}
td::actor::send_closure(SelfId, &RldpIn::in_transfer_completed, transfer_id);
td::actor::send_closure(SelfId, &RldpIn::receive_message, source, local_id, transfer_id, R.move_as_ok());
});
receivers_.emplace(part.transfer_id_,
RldpTransferReceiver::create(part.transfer_id_, local_id, source, part.total_size_,
td::Timestamp::in(60.0), actor_id(this), adnl_, std::move(P)));
it = receivers_.find(part.transfer_id_);
}
auto F = fec::FecType::create(std::move(part.fec_type_));
if (F.is_ok()) {
td::actor::send_closure(it->second, &RldpTransferReceiver::receive_part, F.move_as_ok(), part.part_,
part.total_size_, part.seqno_, std::move(part.data_));
} else {
VLOG(RLDP_NOTICE) << "received bad fec type: " << F.move_as_error();
}
}
void RldpIn::process_message_part(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id,
ton_api::rldp_confirm &part) {
auto it = senders_.find(part.transfer_id_);
if (it != senders_.end()) {
td::actor::send_closure(it->second, &RldpTransferSender::confirm, part.part_, part.seqno_);
}
}
void RldpIn::process_message_part(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id,
ton_api::rldp_complete &part) {
auto it = senders_.find(part.transfer_id_);
if (it != senders_.end()) {
td::actor::send_closure(it->second, &RldpTransferSender::complete, part.part_);
}
}
void RldpIn::receive_message(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, TransferId transfer_id,
td::BufferSlice data) {
auto F = fetch_tl_object<ton_api::rldp_Message>(std::move(data), true);
if (F.is_error()) {
VLOG(RLDP_INFO) << "failed to parse rldp packet [" << source << "->" << local_id << "]: " << F.move_as_error();
return;
}
ton_api::downcast_call(*F.move_as_ok().get(),
[&](auto &obj) { this->process_message(source, local_id, transfer_id, obj); });
}
void RldpIn::process_message(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, TransferId transfer_id,
ton_api::rldp_message &message) {
td::actor::send_closure(adnl_, &adnl::AdnlPeerTable::deliver, source, local_id, std::move(message.data_));
}
void RldpIn::process_message(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, TransferId transfer_id,
ton_api::rldp_query &message) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), source, local_id,
timeout = td::Timestamp::at_unix(message.timeout_), query_id = message.query_id_,
max_answer_size = static_cast<td::uint64>(message.max_answer_size_),
transfer_id](td::Result<td::BufferSlice> R) {
if (R.is_ok()) {
auto data = R.move_as_ok();
if (data.size() > max_answer_size) {
VLOG(RLDP_NOTICE) << "rldp query failed: answer too big";
} else {
td::actor::send_closure(SelfId, &RldpIn::answer_query, local_id, source, timeout, query_id,
transfer_id ^ TransferId::ones(), std::move(data));
}
} else {
VLOG(RLDP_NOTICE) << "rldp query failed: " << R.move_as_error();
}
});
VLOG(RLDP_DEBUG) << "delivering rldp query";
td::actor::send_closure(adnl_, &adnl::AdnlPeerTable::deliver_query, source, local_id, std::move(message.data_),
std::move(P));
}
void RldpIn::process_message(adnl::AdnlNodeIdShort source, adnl::AdnlNodeIdShort local_id, TransferId transfer_id,
ton_api::rldp_answer &message) {
auto it = queries_.find(message.query_id_);
if (it != queries_.end()) {
td::actor::send_closure(it->second, &adnl::AdnlQuery::result, std::move(message.data_));
queries_.erase(it);
} else {
VLOG(RLDP_INFO) << "received answer to unknown query " << message.query_id_;
}
}
void RldpIn::transfer_completed(TransferId transfer_id) {
senders_.erase(transfer_id);
VLOG(RLDP_DEBUG) << "rldp: completed transfer " << transfer_id << "; " << senders_.size() << " out transfer pending ";
}
void RldpIn::in_transfer_completed(TransferId transfer_id) {
if (lru_set_.count(transfer_id) == 1) {
return;
}
while (lru_size_ >= lru_size()) {
auto N = RldpLru::from_list_node(lru_.get());
lru_set_.erase(N->transfer_id());
lru_size_--;
delete N;
}
lru_set_.insert(transfer_id);
auto N = new RldpLru{transfer_id};
lru_.put(N);
lru_size_++;
}
void RldpIn::add_id(adnl::AdnlNodeIdShort local_id) {
if (local_ids_.count(local_id) == 1) {
return;
}
std::vector<std::string> X{adnl::Adnl::int_to_bytestring(ton_api::rldp_messagePart::ID),
adnl::Adnl::int_to_bytestring(ton_api::rldp_confirm::ID),
adnl::Adnl::int_to_bytestring(ton_api::rldp_complete::ID)};
for (auto &x : X) {
td::actor::send_closure(adnl_, &adnl::Adnl::subscribe, local_id, x, make_adnl_callback());
}
local_ids_.insert(local_id);
}
std::unique_ptr<adnl::Adnl::Callback> RldpIn::make_adnl_callback() {
class Callback : public adnl::Adnl::Callback {
private:
td::actor::ActorId<RldpIn> id_;
public:
Callback(td::actor::ActorId<RldpIn> id) : id_(id) {
}
void receive_message(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::BufferSlice data) override {
td::actor::send_closure(id_, &RldpIn::receive_message_part, src, dst, std::move(data));
}
void receive_query(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::BufferSlice data,
td::Promise<td::BufferSlice> promise) override {
promise.set_error(td::Status::Error(ErrorCode::notready, "rldp does not support queries"));
}
};
return std::make_unique<Callback>(actor_id(this));
}
td::actor::ActorOwn<Rldp> Rldp::create(td::actor::ActorId<adnl::Adnl> adnl) {
return td::actor::create_actor<RldpIn>("rldp", td::actor::actor_dynamic_cast<adnl::AdnlPeerTable>(adnl));
}
} // namespace rldp
} // namespace ton

45
rldp/rldp.h Normal file
View file

@ -0,0 +1,45 @@
/*
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.h"
namespace ton {
namespace rldp {
class Rldp : public adnl::AdnlSenderInterface {
public:
virtual ~Rldp() = default;
static constexpr td::uint64 default_mtu() {
return adnl::Adnl::get_mtu();
}
virtual void add_id(adnl::AdnlNodeIdShort local_id) = 0;
virtual void send_message_ex(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdShort dst, td::Timestamp timeout,
td::BufferSlice data) = 0;
static td::actor::ActorOwn<Rldp> create(td::actor::ActorId<adnl::Adnl> adnl);
};
} // namespace rldp
} // namespace ton

49
rldp/rldp.hpp Normal file
View file

@ -0,0 +1,49 @@
/*
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 "rldp.h"
#include "rldp-peer.h"
#include "tl-utils/tl-utils.hpp"
#include "adnl/adnl-query.h"
#include <map>
namespace ton {
namespace rldp {
constexpr int VERBOSITY_NAME(RLDP_WARNING) = verbosity_WARNING;
constexpr int VERBOSITY_NAME(RLDP_NOTICE) = verbosity_INFO;
constexpr int VERBOSITY_NAME(RLDP_INFO) = verbosity_DEBUG;
constexpr int VERBOSITY_NAME(RLDP_DEBUG) = verbosity_DEBUG;
constexpr int VERBOSITY_NAME(RLDP_EXTRA_DEBUG) = verbosity_DEBUG + 1;
using TransferId = td::Bits256;
class RldpImpl : public Rldp {
public:
virtual void transfer_completed(TransferId transfer_id) = 0;
//virtual void in_transfer_completed(TransferId transfer_id) = 0;
};
} // namespace rldp
} // namespace ton