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

View file

@ -0,0 +1,367 @@
/*
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 "download-block.hpp"
#include "ton/ton-tl.hpp"
#include "adnl/utils.hpp"
#include "ton/ton-shard.h"
#include "td/utils/overloaded.h"
#include "ton/ton-io.hpp"
#include "validator/full-node.h"
namespace ton {
namespace validator {
namespace fullnode {
DownloadBlock::DownloadBlock(BlockIdExt block_id, adnl::AdnlNodeIdShort local_id, overlay::OverlayIdShort overlay_id,
adnl::AdnlNodeIdShort download_from, td::uint32 priority, td::Timestamp timeout,
td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<ReceivedBlock> promise)
: block_id_(block_id)
, local_id_(local_id)
, overlay_id_(overlay_id)
, download_from_(download_from)
, priority_(priority)
, timeout_(timeout)
, validator_manager_(validator_manager)
, rldp_(rldp)
, overlays_(overlays)
, adnl_(adnl)
, promise_(std::move(promise))
, block_{block_id, td::BufferSlice()}
, allow_partial_proof_{!block_id_.is_masterchain()} {
}
DownloadBlock::DownloadBlock(BlockIdExt block_id, adnl::AdnlNodeIdShort local_id, overlay::OverlayIdShort overlay_id,
BlockHandle prev, adnl::AdnlNodeIdShort download_from, td::uint32 priority,
td::Timestamp timeout, td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<ReceivedBlock> promise)
: block_id_(block_id)
, local_id_(local_id)
, overlay_id_(overlay_id)
, prev_(prev)
, download_from_(download_from)
, priority_(priority)
, timeout_(timeout)
, validator_manager_(validator_manager)
, rldp_(rldp)
, overlays_(overlays)
, adnl_(adnl)
, promise_(std::move(promise))
, block_{block_id, td::BufferSlice()} {
}
void DownloadBlock::abort_query(td::Status reason) {
if (promise_) {
if (reason.code() == ErrorCode::notready || reason.code() == ErrorCode::timeout) {
VLOG(FULL_NODE_DEBUG) << "failed to download block " << block_id_ << "from " << download_from_ << ": " << reason;
} else {
VLOG(FULL_NODE_NOTICE) << "failed to download block " << block_id_ << " from " << download_from_ << ": "
<< reason;
}
promise_.set_error(std::move(reason));
}
stop();
}
void DownloadBlock::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void DownloadBlock::finish_query() {
if (promise_) {
promise_.set_value(std::move(block_));
}
stop();
}
void DownloadBlock::start_up() {
alarm_timestamp() = timeout_;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
if (R.is_error()) {
auto S = R.move_as_error();
if (S.code() == ErrorCode::notready) {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_handle, nullptr);
} else {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, std::move(S));
}
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_handle, R.move_as_ok());
}
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_block_handle, block_id_, false,
std::move(P));
}
void DownloadBlock::got_block_handle(BlockHandle handle) {
handle_ = std::move(handle);
if (handle_ && (handle_->inited_proof() || (handle_->inited_proof_link() && allow_partial_proof_) || skip_proof_) &&
handle_->received()) {
short_ = true;
got_download_token(nullptr);
return;
}
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<std::unique_ptr<DownloadToken>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query,
R.move_as_error_prefix("failed to get download token: "));
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_download_token, R.move_as_ok());
}
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_download_token, 1, priority_, timeout_,
std::move(P));
}
void DownloadBlock::got_download_token(std::unique_ptr<DownloadToken> token) {
token_ = std::move(token);
if (download_from_.is_zero() && !short_) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<std::vector<adnl::AdnlNodeIdShort>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
auto vec = R.move_as_ok();
if (vec.size() == 0) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query,
td::Status::Error(ErrorCode::notready, "no nodes"));
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_node_to_download, vec[0]);
}
}
});
td::actor::send_closure(overlays_, &overlay::Overlays::get_overlay_random_peers, local_id_, overlay_id_, 1,
std::move(P));
} else {
got_node_to_download(download_from_);
}
}
void DownloadBlock::got_node_to_download(adnl::AdnlNodeIdShort node) {
download_from_ = node;
if (skip_proof_ || (handle_ && (handle_->inited_proof() || (handle_->inited_proof_link() && allow_partial_proof_)))) {
checked_block_proof();
return;
}
VLOG(FULL_NODE_DEBUG) << "downloading proof for " << block_id_;
CHECK(!short_);
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) mutable {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_proof_description, R.move_as_ok());
}
});
td::actor::send_closure(overlays_, &overlay::Overlays::send_query, download_from_, local_id_, overlay_id_,
"get_prepare", std::move(P), td::Timestamp::in(1.0),
create_serialize_tl_object<ton_api::tonNode_prepareBlockProof>(create_tl_block_id(block_id_),
allow_partial_proof_));
}
void DownloadBlock::got_block_proof_description(td::BufferSlice proof_description) {
VLOG(FULL_NODE_DEBUG) << "downloaded proof description for " << block_id_;
auto F = fetch_tl_object<ton_api::tonNode_PreparedProof>(std::move(proof_description), true);
if (F.is_error()) {
abort_query(F.move_as_error());
return;
}
auto self = this;
ton_api::downcast_call(
*F.move_as_ok().get(),
td::overloaded(
[&](ton_api::tonNode_preparedProof &obj) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(self)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_proof, R.move_as_ok());
}
});
td::actor::send_closure(
overlays_, &overlay::Overlays::send_query_via, download_from_, local_id_, overlay_id_,
"download block proof", std::move(P), td::Timestamp::in(3.0),
create_serialize_tl_object<ton_api::tonNode_downloadBlockProof>(create_tl_block_id(block_id_)),
FullNode::max_proof_size(), rldp_);
},
[&](ton_api::tonNode_preparedProofLink &obj) {
if (!allow_partial_proof_) {
abort_query(td::Status::Error(ErrorCode::protoviolation, "received partial proof, though did not allow"));
return;
}
auto P = td::PromiseCreator::lambda([SelfId = actor_id(self)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_partial_proof, R.move_as_ok());
}
});
td::actor::send_closure(
overlays_, &overlay::Overlays::send_query_via, download_from_, local_id_, overlay_id_,
"download block proof link", std::move(P), td::Timestamp::in(3.0),
create_serialize_tl_object<ton_api::tonNode_downloadBlockProofLink>(create_tl_block_id(block_id_)),
FullNode::max_proof_size(), rldp_);
},
[&](ton_api::tonNode_preparedProofEmpty &obj) {
abort_query(td::Status::Error(ErrorCode::notready, "proof not found"));
}));
}
void DownloadBlock::got_block_proof(td::BufferSlice proof) {
VLOG(FULL_NODE_DEBUG) << "downloaded proof for " << block_id_;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Unit> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::checked_block_proof);
}
});
if (!prev_) {
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::validate_block_proof, block_id_,
std::move(proof), std::move(P));
} else {
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::validate_block_is_next_proof, prev_->id(),
block_id_, std::move(proof), std::move(P));
}
}
void DownloadBlock::got_block_partial_proof(td::BufferSlice proof) {
CHECK(allow_partial_proof_);
CHECK(!prev_);
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Unit> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::checked_block_proof);
}
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::validate_block_proof_link, block_id_,
std::move(proof), std::move(P));
}
void DownloadBlock::checked_block_proof() {
VLOG(FULL_NODE_DEBUG) << "checked proof for " << block_id_;
if (!handle_) {
CHECK(!short_);
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_handle_2, R.move_as_ok());
}
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_block_handle, block_id_, true,
std::move(P));
} else {
got_block_handle_2(handle_);
}
}
void DownloadBlock::got_block_handle_2(BlockHandle handle) {
handle_ = std::move(handle);
LOG_CHECK(skip_proof_ || handle_->inited_proof() || (allow_partial_proof_ && handle_->inited_proof_link()))
<< handle_->id() << " allowpartial=" << allow_partial_proof_;
if (handle_->received()) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) mutable {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_data, R.move_as_ok());
}
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_block_data, handle_, std::move(P));
} else {
CHECK(!short_);
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) mutable {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_data_description, R.move_as_ok());
}
});
td::actor::send_closure(overlays_, &overlay::Overlays::send_query, download_from_, local_id_, overlay_id_,
"get_prepare", std::move(P), td::Timestamp::in(1.0),
create_serialize_tl_object<ton_api::tonNode_prepareBlock>(create_tl_block_id(block_id_)));
}
}
void DownloadBlock::got_block_data_description(td::BufferSlice data_description) {
VLOG(FULL_NODE_DEBUG) << "downloaded data description for " << block_id_;
auto F = fetch_tl_object<ton_api::tonNode_Prepared>(std::move(data_description), true);
if (F.is_error()) {
abort_query(F.move_as_error());
return;
}
auto f = F.move_as_ok();
ton_api::downcast_call(
*f.get(), td::overloaded(
[&, self = this](ton_api::tonNode_prepared &val) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(self)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadBlock::got_block_data, R.move_as_ok());
}
});
td::actor::send_closure(
overlays_, &overlay::Overlays::send_query_via, download_from_, local_id_, overlay_id_,
"download block", std::move(P), td::Timestamp::in(3.0),
create_serialize_tl_object<ton_api::tonNode_downloadBlock>(create_tl_block_id(block_id_)),
FullNode::max_block_size(), rldp_);
},
[&](ton_api::tonNode_notFound &val) {
abort_query(td::Status::Error(ErrorCode::notready, "dst node does not have block"));
}));
}
void DownloadBlock::got_block_data(td::BufferSlice data) {
VLOG(FULL_NODE_DEBUG) << "downloaded data for " << block_id_;
block_.data = std::move(data);
finish_query();
}
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,94 @@
/*
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 "overlay/overlays.h"
#include "ton/ton-types.h"
#include "validator/validator.h"
#include "rldp/rldp.h"
namespace ton {
namespace validator {
namespace fullnode {
class DownloadBlock : public td::actor::Actor {
public:
DownloadBlock(BlockIdExt block_id, adnl::AdnlNodeIdShort local_id, overlay::OverlayIdShort overlay_id,
adnl::AdnlNodeIdShort download_from, td::uint32 priority, td::Timestamp timeout,
td::actor::ActorId<ValidatorManagerInterface> validator_manager, td::actor::ActorId<rldp::Rldp> rldp,
td::actor::ActorId<overlay::Overlays> overlays, td::actor::ActorId<adnl::Adnl> adnl,
td::Promise<ReceivedBlock> promise);
DownloadBlock(BlockIdExt block_id, adnl::AdnlNodeIdShort local_id, overlay::OverlayIdShort overlay_id,
BlockHandle prev, adnl::AdnlNodeIdShort download_from, td::uint32 priority, td::Timestamp timeout,
td::actor::ActorId<ValidatorManagerInterface> validator_manager, td::actor::ActorId<rldp::Rldp> rldp,
td::actor::ActorId<overlay::Overlays> overlays, td::actor::ActorId<adnl::Adnl> adnl,
td::Promise<ReceivedBlock> promise);
void abort_query(td::Status reason);
void alarm() override;
void finish_query();
void start_up() override;
void got_block_handle(BlockHandle handle);
void got_download_token(std::unique_ptr<DownloadToken> token);
void got_node_to_download(adnl::AdnlNodeIdShort node);
void got_block_proof_description(td::BufferSlice proof_description);
void got_block_proof(td::BufferSlice data);
void got_block_partial_proof(td::BufferSlice data);
void checked_block_proof();
void got_block_handle_2(BlockHandle handle);
void got_block_data_description(td::BufferSlice data_description);
void got_block_data(td::BufferSlice data);
void checked_block();
private:
BlockIdExt block_id_;
adnl::AdnlNodeIdShort local_id_;
overlay::OverlayIdShort overlay_id_;
BlockHandle prev_ = nullptr;
adnl::AdnlNodeIdShort download_from_ = adnl::AdnlNodeIdShort::zero();
td::uint32 priority_;
td::Timestamp timeout_;
td::actor::ActorId<ValidatorManagerInterface> validator_manager_;
td::actor::ActorId<rldp::Rldp> rldp_;
td::actor::ActorId<overlay::Overlays> overlays_;
td::actor::ActorId<adnl::Adnl> adnl_;
td::Promise<ReceivedBlock> promise_;
BlockHandle handle_;
ReceivedBlock block_;
bool skip_proof_ = false;
bool short_ = false;
bool allow_partial_proof_ = false;
std::unique_ptr<DownloadToken> token_;
};
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,152 @@
/*
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 "download-next-block.hpp"
#include "ton/ton-tl.hpp"
#include "adnl/utils.hpp"
#include "download-block.hpp"
#include "validator/full-node.h"
namespace ton {
namespace validator {
namespace fullnode {
DownloadNextBlock::DownloadNextBlock(adnl::AdnlNodeIdShort local_id, overlay::OverlayIdShort overlay_id,
BlockHandle prev, td::uint32 priority, td::Timestamp timeout,
td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp,
td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<ReceivedBlock> promise)
: local_id_(local_id)
, overlay_id_(overlay_id)
, prev_(prev)
, priority_(priority)
, timeout_(timeout)
, validator_manager_(validator_manager)
, rldp_(rldp)
, overlays_(overlays)
, adnl_(adnl)
, promise_(std::move(promise)) {
}
void DownloadNextBlock::abort_query(td::Status reason) {
if (promise_) {
if (reason.code() == ErrorCode::notready || reason.code() == ErrorCode::timeout) {
VLOG(FULL_NODE_DEBUG) << "failed to download next block after " << prev_->id() << " from " << node_ << ": "
<< reason;
} else {
VLOG(FULL_NODE_INFO) << "failed to download next block after " << prev_->id() << " from " << node_ << ": "
<< reason;
}
promise_.set_error(std::move(reason));
}
stop();
}
void DownloadNextBlock::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void DownloadNextBlock::start_up() {
alarm_timestamp() = timeout_;
if (prev_->inited_next_left()) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
R.ensure();
td::actor::send_closure(SelfId, &DownloadNextBlock::got_next_node_handle, R.move_as_ok());
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_block_handle, prev_->one_next(true),
false, std::move(P));
return;
}
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<std::vector<adnl::AdnlNodeIdShort>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadNextBlock::abort_query, R.move_as_error());
} else {
auto vec = R.move_as_ok();
if (vec.size() == 0) {
td::actor::send_closure(SelfId, &DownloadNextBlock::abort_query,
td::Status::Error(ErrorCode::notready, "no neighbours found"));
} else {
td::actor::send_closure(SelfId, &DownloadNextBlock::got_node, vec[0]);
}
}
});
td::actor::send_closure(overlays_, &overlay::Overlays::get_overlay_random_peers, local_id_, overlay_id_, 1,
std::move(P));
}
void DownloadNextBlock::got_node(adnl::AdnlNodeIdShort id) {
node_ = id;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadNextBlock::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadNextBlock::got_next_node, R.move_as_ok());
}
});
auto query = create_serialize_tl_object<ton_api::tonNode_getNextBlockDescription>(create_tl_block_id(prev_->id()));
td::actor::send_closure(overlays_, &overlay::Overlays::send_query, id, local_id_, overlay_id_, "get_prepare",
std::move(P), td::Timestamp::in(1.0), std::move(query));
}
void DownloadNextBlock::got_next_node(td::BufferSlice data) {
auto F = fetch_tl_object<ton_api::tonNode_BlockDescription>(std::move(data), true);
if (F.is_error()) {
abort_query(F.move_as_error());
return;
}
auto f = F.move_as_ok();
if (f->get_id() == ton_api::tonNode_blockDescriptionEmpty::ID) {
abort_query(td::Status::Error(ErrorCode::notready, "not found"));
return;
}
auto g = move_tl_object_as<ton_api::tonNode_blockDescription>(std::move(f));
next_block_id_ = create_block_id(g->id_);
finish_query();
}
void DownloadNextBlock::got_next_node_handle(BlockHandle handle) {
//CHECK(handle->inited_proof());
//CHECK(handle->inited_masterchain());
next_block_id_ = handle->id();
finish_query();
}
void DownloadNextBlock::finish_query() {
if (promise_) {
td::actor::create_actor<DownloadBlock>("downloadnext", next_block_id_, local_id_, overlay_id_, prev_, node_,
priority_, timeout_, validator_manager_, rldp_, overlays_, adnl_,
std::move(promise_))
.release();
}
stop();
}
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,74 @@
/*
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 "overlay/overlays.h"
#include "ton/ton-types.h"
#include "validator/validator.h"
#include "rldp/rldp.h"
#include "ton/ton-io.hpp"
namespace ton {
namespace validator {
namespace fullnode {
class DownloadNextBlock : public td::actor::Actor {
public:
DownloadNextBlock(adnl::AdnlNodeIdShort local_id, overlay::OverlayIdShort overlay_id, BlockHandle prev,
td::uint32 priority, td::Timestamp timeout,
td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<ReceivedBlock> promise);
void abort_query(td::Status reason);
void finish_query();
void alarm() override;
void start_up() override;
void got_node(adnl::AdnlNodeIdShort id);
void got_next_node(td::BufferSlice data);
void got_next_node_handle(BlockHandle handle);
private:
adnl::AdnlNodeIdShort local_id_;
overlay::OverlayIdShort overlay_id_;
BlockHandle prev_;
BlockIdExt next_block_id_;
td::uint32 priority_;
td::Timestamp timeout_;
td::actor::ActorId<ValidatorManagerInterface> validator_manager_;
td::actor::ActorId<rldp::Rldp> rldp_;
td::actor::ActorId<overlay::Overlays> overlays_;
td::actor::ActorId<adnl::Adnl> adnl_;
td::Promise<ReceivedBlock> promise_;
adnl::AdnlNodeIdShort node_ = adnl::AdnlNodeIdShort::zero();
};
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,205 @@
/*
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 "download-proof.hpp"
#include "ton/ton-tl.hpp"
#include "adnl/utils.hpp"
#include "ton/ton-shard.h"
#include "td/utils/overloaded.h"
#include "ton/ton-io.hpp"
#include "validator/full-node.h"
namespace ton {
namespace validator {
namespace fullnode {
DownloadProof::DownloadProof(BlockIdExt block_id, bool allow_partial_proof, adnl::AdnlNodeIdShort local_id,
overlay::OverlayIdShort overlay_id, adnl::AdnlNodeIdShort download_from,
td::uint32 priority, td::Timestamp timeout,
td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<td::BufferSlice> promise)
: block_id_(block_id)
, allow_partial_proof_(allow_partial_proof)
, local_id_(local_id)
, overlay_id_(overlay_id)
, download_from_(download_from)
, priority_(priority)
, timeout_(timeout)
, validator_manager_(validator_manager)
, rldp_(rldp)
, overlays_(overlays)
, adnl_(adnl)
, promise_(std::move(promise)) {
}
void DownloadProof::abort_query(td::Status reason) {
if (promise_) {
if (reason.code() == ErrorCode::notready || reason.code() == ErrorCode::timeout) {
VLOG(FULL_NODE_DEBUG) << "failed to download proof " << block_id_ << "from " << download_from_ << ": " << reason;
} else {
VLOG(FULL_NODE_NOTICE) << "failed to download proof " << block_id_ << " from " << download_from_ << ": "
<< reason;
}
promise_.set_error(std::move(reason));
}
stop();
}
void DownloadProof::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void DownloadProof::finish_query() {
if (promise_) {
promise_.set_value(std::move(data_));
}
stop();
}
void DownloadProof::start_up() {
alarm_timestamp() = timeout_;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<std::unique_ptr<DownloadToken>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadProof::abort_query,
R.move_as_error_prefix("failed to get download token: "));
} else {
td::actor::send_closure(SelfId, &DownloadProof::got_download_token, R.move_as_ok());
}
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_download_token, 1, priority_, timeout_,
std::move(P));
}
void DownloadProof::got_download_token(std::unique_ptr<DownloadToken> token) {
token_ = std::move(token);
if (download_from_.is_zero()) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<std::vector<adnl::AdnlNodeIdShort>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadProof::abort_query, R.move_as_error());
} else {
auto vec = R.move_as_ok();
if (vec.size() == 0) {
td::actor::send_closure(SelfId, &DownloadProof::abort_query,
td::Status::Error(ErrorCode::notready, "no nodes"));
} else {
td::actor::send_closure(SelfId, &DownloadProof::got_node_to_download, vec[0]);
}
}
});
td::actor::send_closure(overlays_, &overlay::Overlays::get_overlay_random_peers, local_id_, overlay_id_, 1,
std::move(P));
} else {
got_node_to_download(download_from_);
}
}
void DownloadProof::got_node_to_download(adnl::AdnlNodeIdShort node) {
download_from_ = node;
VLOG(FULL_NODE_DEBUG) << "downloading proof for " << block_id_;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) mutable {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadProof::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadProof::got_block_proof_description, R.move_as_ok());
}
});
td::actor::send_closure(overlays_, &overlay::Overlays::send_query, download_from_, local_id_, overlay_id_,
"get_prepare", std::move(P), td::Timestamp::in(1.0),
create_serialize_tl_object<ton_api::tonNode_prepareBlockProof>(create_tl_block_id(block_id_),
allow_partial_proof_));
}
void DownloadProof::got_block_proof_description(td::BufferSlice proof_description) {
VLOG(FULL_NODE_DEBUG) << "downloaded proof description for " << block_id_;
auto F = fetch_tl_object<ton_api::tonNode_PreparedProof>(std::move(proof_description), true);
if (F.is_error()) {
abort_query(F.move_as_error());
return;
}
auto self = this;
ton_api::downcast_call(
*F.move_as_ok().get(),
td::overloaded(
[&](ton_api::tonNode_preparedProof &obj) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(self)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadProof::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadProof::got_block_proof, R.move_as_ok());
}
});
td::actor::send_closure(
overlays_, &overlay::Overlays::send_query_via, download_from_, local_id_, overlay_id_,
"download block proof", std::move(P), td::Timestamp::in(3.0),
create_serialize_tl_object<ton_api::tonNode_downloadBlockProof>(create_tl_block_id(block_id_)),
FullNode::max_proof_size(), rldp_);
},
[&](ton_api::tonNode_preparedProofLink &obj) {
if (!allow_partial_proof_) {
abort_query(td::Status::Error(ErrorCode::protoviolation, "received partial proof, though did not allow"));
return;
}
auto P = td::PromiseCreator::lambda([SelfId = actor_id(self)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadProof::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadProof::got_block_partial_proof, R.move_as_ok());
}
});
td::actor::send_closure(
overlays_, &overlay::Overlays::send_query_via, download_from_, local_id_, overlay_id_,
"download block proof link", std::move(P), td::Timestamp::in(3.0),
create_serialize_tl_object<ton_api::tonNode_downloadBlockProofLink>(create_tl_block_id(block_id_)),
FullNode::max_proof_size(), rldp_);
},
[&](ton_api::tonNode_preparedProofEmpty &obj) {
abort_query(td::Status::Error(ErrorCode::notready, "proof not found"));
}));
}
void DownloadProof::got_block_proof(td::BufferSlice proof) {
VLOG(FULL_NODE_DEBUG) << "downloaded proof for " << block_id_;
data_ = std::move(proof);
finish_query();
}
void DownloadProof::got_block_partial_proof(td::BufferSlice proof) {
VLOG(FULL_NODE_DEBUG) << "downloaded partial proof for " << block_id_;
data_ = std::move(proof);
finish_query();
}
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,76 @@
/*
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 "overlay/overlays.h"
#include "ton/ton-types.h"
#include "validator/validator.h"
#include "rldp/rldp.h"
namespace ton {
namespace validator {
namespace fullnode {
class DownloadProof : public td::actor::Actor {
public:
DownloadProof(BlockIdExt block_id, bool allow_partial_proof, adnl::AdnlNodeIdShort local_id,
overlay::OverlayIdShort overlay_id, adnl::AdnlNodeIdShort download_from, td::uint32 priority,
td::Timestamp timeout, td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<td::BufferSlice> promise);
void abort_query(td::Status reason);
void alarm() override;
void finish_query();
void start_up() override;
void got_download_token(std::unique_ptr<DownloadToken> token);
void got_node_to_download(adnl::AdnlNodeIdShort node);
void got_block_proof_description(td::BufferSlice proof_description);
void got_block_proof(td::BufferSlice data);
void got_block_partial_proof(td::BufferSlice data);
private:
BlockIdExt block_id_;
bool allow_partial_proof_;
adnl::AdnlNodeIdShort local_id_;
overlay::OverlayIdShort overlay_id_;
adnl::AdnlNodeIdShort download_from_ = adnl::AdnlNodeIdShort::zero();
td::uint32 priority_;
td::Timestamp timeout_;
td::actor::ActorId<ValidatorManagerInterface> validator_manager_;
td::actor::ActorId<rldp::Rldp> rldp_;
td::actor::ActorId<overlay::Overlays> overlays_;
td::actor::ActorId<adnl::Adnl> adnl_;
td::Promise<td::BufferSlice> promise_;
td::BufferSlice data_;
std::unique_ptr<DownloadToken> token_;
};
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,181 @@
/*
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 "download-state.hpp"
#include "ton/ton-tl.hpp"
#include "ton/ton-io.hpp"
#include "td/utils/overloaded.h"
#include "full-node.h"
namespace ton {
namespace validator {
namespace fullnode {
DownloadState::DownloadState(BlockIdExt block_id, BlockIdExt masterchain_block_id, adnl::AdnlNodeIdShort local_id,
overlay::OverlayIdShort overlay_id, adnl::AdnlNodeIdShort download_from,
td::uint32 priority, td::Timestamp timeout,
td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<td::BufferSlice> promise)
: block_id_(block_id)
, masterchain_block_id_(masterchain_block_id)
, local_id_(local_id)
, overlay_id_(overlay_id)
, download_from_(download_from)
, priority_(priority)
, timeout_(timeout)
, validator_manager_(validator_manager)
, rldp_(rldp)
, overlays_(overlays)
, adnl_(adnl)
, promise_(std::move(promise)) {
}
void DownloadState::abort_query(td::Status reason) {
if (promise_) {
if (reason.code() == ErrorCode::notready || reason.code() == ErrorCode::timeout) {
VLOG(FULL_NODE_DEBUG) << "failed to download state " << block_id_ << "from " << download_from_ << ": " << reason;
} else {
VLOG(FULL_NODE_NOTICE) << "failed to download state " << block_id_ << " from " << download_from_ << ": "
<< reason;
}
promise_.set_error(std::move(reason));
}
stop();
}
void DownloadState::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void DownloadState::finish_query() {
if (promise_) {
promise_.set_value(std::move(state_));
}
stop();
}
void DownloadState::start_up() {
alarm_timestamp() = timeout_;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadState::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadState::got_block_handle, R.move_as_ok());
}
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_block_handle, block_id_, true,
std::move(P));
}
void DownloadState::got_block_handle(BlockHandle handle) {
handle_ = std::move(handle);
if (!download_from_.is_zero()) {
got_node_to_download(download_from_);
} else {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<std::vector<adnl::AdnlNodeIdShort>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadState::abort_query, R.move_as_error());
} else {
auto vec = R.move_as_ok();
if (vec.size() == 0) {
td::actor::send_closure(SelfId, &DownloadState::abort_query,
td::Status::Error(ErrorCode::notready, "no nodes"));
} else {
td::actor::send_closure(SelfId, &DownloadState::got_node_to_download, vec[0]);
}
}
});
td::actor::send_closure(overlays_, &overlay::Overlays::get_overlay_random_peers, local_id_, overlay_id_, 1,
std::move(P));
}
}
void DownloadState::got_node_to_download(adnl::AdnlNodeIdShort node) {
download_from_ = node;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) mutable {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadState::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadState::got_block_state_description, R.move_as_ok());
}
});
td::BufferSlice query;
if (masterchain_block_id_.is_valid()) {
query = create_serialize_tl_object<ton_api::tonNode_preparePersistentState>(
create_tl_block_id(block_id_), create_tl_block_id(masterchain_block_id_));
} else {
query = create_serialize_tl_object<ton_api::tonNode_prepareZeroState>(create_tl_block_id(block_id_));
}
td::actor::send_closure(overlays_, &overlay::Overlays::send_query, download_from_, local_id_, overlay_id_,
"get_prepare", std::move(P), td::Timestamp::in(1.0), std::move(query));
}
void DownloadState::got_block_state_description(td::BufferSlice data) {
auto F = fetch_tl_object<ton_api::tonNode_PreparedState>(std::move(data), true);
if (F.is_error()) {
abort_query(F.move_as_error());
return;
}
ton_api::downcast_call(
*F.move_as_ok().get(),
td::overloaded(
[&](ton_api::tonNode_notFoundState &f) {
abort_query(td::Status::Error(ErrorCode::notready, "state not found"));
},
[&, self = this](ton_api::tonNode_preparedState &f) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(self)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadState::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadState::got_block_state, R.move_as_ok());
}
});
td::BufferSlice query;
if (masterchain_block_id_.is_valid()) {
query = create_serialize_tl_object<ton_api::tonNode_downloadPersistentState>(
create_tl_block_id(block_id_), create_tl_block_id(masterchain_block_id_));
} else {
query = create_serialize_tl_object<ton_api::tonNode_downloadZeroState>(create_tl_block_id(block_id_));
}
td::actor::send_closure(overlays_, &overlay::Overlays::send_query_via, download_from_, local_id_,
overlay_id_, "download state", std::move(P), timeout_, std::move(query),
FullNode::max_state_size(), rldp_);
}));
}
void DownloadState::got_block_state(td::BufferSlice data) {
state_ = std::move(data);
finish_query();
}
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,75 @@
/*
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 "overlay/overlays.h"
#include "ton/ton-types.h"
#include "validator/validator.h"
#include "rldp/rldp.h"
namespace ton {
namespace validator {
namespace fullnode {
class DownloadState : public td::actor::Actor {
public:
DownloadState(BlockIdExt block_id, BlockIdExt masterchain_block_id, adnl::AdnlNodeIdShort local_id,
overlay::OverlayIdShort overlay_id, adnl::AdnlNodeIdShort download_from, td::uint32 priority,
td::Timestamp timeout, td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<td::BufferSlice> promise);
void abort_query(td::Status reason);
void alarm() override;
void finish_query();
void start_up() override;
void got_block_handle(BlockHandle handle);
void got_node_to_download(adnl::AdnlNodeIdShort node);
void got_block_state_description(td::BufferSlice data_description);
void got_block_state(td::BufferSlice data);
private:
BlockIdExt block_id_;
BlockIdExt masterchain_block_id_;
adnl::AdnlNodeIdShort local_id_;
overlay::OverlayIdShort overlay_id_;
adnl::AdnlNodeIdShort download_from_ = adnl::AdnlNodeIdShort::zero();
td::uint32 priority_;
td::Timestamp timeout_;
td::actor::ActorId<ValidatorManagerInterface> validator_manager_;
td::actor::ActorId<rldp::Rldp> rldp_;
td::actor::ActorId<overlay::Overlays> overlays_;
td::actor::ActorId<adnl::Adnl> adnl_;
td::Promise<td::BufferSlice> promise_;
BlockHandle handle_;
td::BufferSlice state_;
};
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,220 @@
/*
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 "get-next-key-blocks.hpp"
#include "download-proof.hpp"
#include "ton/ton-tl.hpp"
#include "adnl/utils.hpp"
#include "ton/ton-shard.h"
#include "td/utils/overloaded.h"
#include "ton/ton-io.hpp"
#include "full-node.h"
namespace ton {
namespace validator {
namespace fullnode {
GetNextKeyBlocks::GetNextKeyBlocks(BlockIdExt block_id, td::uint32 limit, adnl::AdnlNodeIdShort local_id,
overlay::OverlayIdShort overlay_id, adnl::AdnlNodeIdShort download_from,
td::uint32 priority, td::Timestamp timeout,
td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<std::vector<BlockIdExt>> promise)
: block_id_(block_id)
, limit_(limit)
, local_id_(local_id)
, overlay_id_(overlay_id)
, download_from_(download_from)
, priority_(priority)
, timeout_(timeout)
, validator_manager_(validator_manager)
, rldp_(rldp)
, overlays_(overlays)
, adnl_(adnl)
, promise_(std::move(promise)) {
}
void GetNextKeyBlocks::abort_query(td::Status reason) {
if (promise_) {
if (reason.code() == ErrorCode::notready || reason.code() == ErrorCode::timeout) {
VLOG(FULL_NODE_DEBUG) << "failed to download proof " << block_id_ << "from " << download_from_ << ": " << reason;
} else {
VLOG(FULL_NODE_NOTICE) << "failed to download proof " << block_id_ << " from " << download_from_ << ": "
<< reason;
}
if (res_.size() > 0) {
promise_.set_value(std::move(res_));
} else {
promise_.set_error(std::move(reason));
}
}
stop();
}
void GetNextKeyBlocks::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void GetNextKeyBlocks::finish_query() {
if (promise_) {
promise_.set_value(std::move(res_));
}
stop();
}
void GetNextKeyBlocks::start_up() {
alarm_timestamp() = timeout_;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<std::unique_ptr<DownloadToken>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::abort_query,
R.move_as_error_prefix("failed to get download token: "));
} else {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::got_download_token, R.move_as_ok());
}
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_download_token, 1, priority_, timeout_,
std::move(P));
}
void GetNextKeyBlocks::got_download_token(std::unique_ptr<DownloadToken> token) {
token_ = std::move(token);
if (download_from_.is_zero()) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<std::vector<adnl::AdnlNodeIdShort>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::abort_query, R.move_as_error());
} else {
auto vec = R.move_as_ok();
if (vec.size() == 0) {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::abort_query,
td::Status::Error(ErrorCode::notready, "no nodes"));
} else {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::got_node_to_download, vec[0]);
}
}
});
td::actor::send_closure(overlays_, &overlay::Overlays::get_overlay_random_peers, local_id_, overlay_id_, 1,
std::move(P));
} else {
got_node_to_download(download_from_);
}
}
void GetNextKeyBlocks::got_node_to_download(adnl::AdnlNodeIdShort node) {
download_from_ = node;
VLOG(FULL_NODE_DEBUG) << "downloading proof for " << block_id_;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) mutable {
if (R.is_error()) {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::got_result, R.move_as_ok());
}
});
td::actor::send_closure(
overlays_, &overlay::Overlays::send_query, download_from_, local_id_, overlay_id_, "get_prepare", std::move(P),
td::Timestamp::in(1.0),
create_serialize_tl_object<ton_api::tonNode_getNextKeyBlockIds>(create_tl_block_id(block_id_), limit_));
}
void GetNextKeyBlocks::got_result(td::BufferSlice data) {
auto F = fetch_tl_object<ton_api::tonNode_keyBlocks>(std::move(data), true);
if (F.is_error()) {
abort_query(F.move_as_error_prefix("received bad answer: "));
return;
}
auto f = F.move_as_ok();
if (f->error_) {
abort_query(td::Status::Error(ErrorCode::notready, "received answer with error"));
return;
}
VLOG(FULL_NODE_DEBUG) << "received " << f->blocks_.size() << " key blocks";
for (auto &x : f->blocks_) {
pending_.push_back(create_block_id(x));
}
download_next_proof();
}
void GetNextKeyBlocks::download_next_proof() {
if (!pending_.size()) {
finish_query();
return;
}
auto block_id = pending_[0];
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::got_next_proof, R.move_as_ok());
}
});
td::actor::create_actor<DownloadProof>("downloadproofreq", block_id, false, local_id_, overlay_id_, download_from_,
priority_, timeout_, validator_manager_, rldp_, overlays_, adnl_, std::move(P))
.release();
}
void GetNextKeyBlocks::got_next_proof(td::BufferSlice proof) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Unit> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &GetNextKeyBlocks::checked_next_proof);
}
});
auto block_id = pending_[0];
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::validate_block_proof_rel, block_id,
(res_.size() > 0) ? *res_.rbegin() : block_id_, std::move(proof), std::move(P));
}
void GetNextKeyBlocks::checked_next_proof() {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
R.ensure();
td::actor::send_closure(SelfId, &GetNextKeyBlocks::got_next_block_handle, R.move_as_ok());
});
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_block_handle, pending_[0], false,
std::move(P));
}
void GetNextKeyBlocks::got_next_block_handle(BlockHandle handle) {
CHECK(handle->inited_is_key_block());
if (!handle->is_key_block()) {
abort_query(td::Status::Error(ErrorCode::protoviolation, "got not key block"));
return;
}
auto block_id = pending_[0];
res_.push_back(block_id);
pending_.erase(pending_.begin());
download_next_proof();
}
} // namespace fullnode
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,82 @@
/*
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 "overlay/overlays.h"
#include "ton/ton-types.h"
#include "validator/validator.h"
#include "rldp/rldp.h"
namespace ton {
namespace validator {
namespace fullnode {
class GetNextKeyBlocks : public td::actor::Actor {
public:
GetNextKeyBlocks(BlockIdExt block_id, td::uint32 limit, adnl::AdnlNodeIdShort local_id,
overlay::OverlayIdShort overlay_id, adnl::AdnlNodeIdShort download_from, td::uint32 priority,
td::Timestamp timeout, td::actor::ActorId<ValidatorManagerInterface> validator_manager,
td::actor::ActorId<rldp::Rldp> rldp, td::actor::ActorId<overlay::Overlays> overlays,
td::actor::ActorId<adnl::Adnl> adnl, td::Promise<std::vector<BlockIdExt>> promise);
void abort_query(td::Status reason);
void alarm() override;
void finish_query();
void start_up() override;
void got_download_token(std::unique_ptr<DownloadToken> token);
void got_node_to_download(adnl::AdnlNodeIdShort node);
void send_request();
void got_result(td::BufferSlice res);
void download_next_proof();
void got_next_proof(td::BufferSlice proof);
void checked_next_proof();
void got_next_block_handle(BlockHandle handle);
private:
BlockIdExt block_id_;
td::uint32 limit_;
adnl::AdnlNodeIdShort local_id_;
overlay::OverlayIdShort overlay_id_;
adnl::AdnlNodeIdShort download_from_ = adnl::AdnlNodeIdShort::zero();
td::uint32 priority_;
td::Timestamp timeout_;
td::actor::ActorId<ValidatorManagerInterface> validator_manager_;
td::actor::ActorId<rldp::Rldp> rldp_;
td::actor::ActorId<overlay::Overlays> overlays_;
td::actor::ActorId<adnl::Adnl> adnl_;
td::Promise<std::vector<BlockIdExt>> promise_;
std::vector<BlockIdExt> pending_;
std::vector<BlockIdExt> res_;
std::unique_ptr<DownloadToken> token_;
};
} // namespace fullnode
} // namespace validator
} // namespace ton