mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
updated tonlib, new fullnode queries
This commit is contained in:
parent
87ccb27b70
commit
a1e352d894
40 changed files with 1188 additions and 175 deletions
|
@ -117,6 +117,8 @@ set(FULL_NODE_SOURCE
|
|||
|
||||
net/download-block.hpp
|
||||
net/download-block.cpp
|
||||
net/download-block-new.hpp
|
||||
net/download-block-new.cpp
|
||||
net/download-next-block.hpp
|
||||
net/download-next-block.cpp
|
||||
net/download-state.hpp
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
#include "td/utils/SharedSlice.h"
|
||||
#include "full-node-master.hpp"
|
||||
#include "full-node-shard-queries.hpp"
|
||||
|
||||
#include "ton/ton-shard.h"
|
||||
#include "ton/ton-tl.hpp"
|
||||
|
@ -93,6 +94,20 @@ void FullNodeMasterImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::tonNo
|
|||
create_block_id(query.block_), false, std::move(P));
|
||||
}
|
||||
|
||||
void FullNodeMasterImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadBlockFull &query,
|
||||
td::Promise<td::BufferSlice> promise) {
|
||||
td::actor::create_actor<BlockFullSender>("sender", ton::create_block_id(query.block_), false, validator_manager_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
}
|
||||
|
||||
void FullNodeMasterImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadNextBlockFull &query,
|
||||
td::Promise<td::BufferSlice> promise) {
|
||||
td::actor::create_actor<BlockFullSender>("sender", ton::create_block_id(query.prev_block_), true, validator_manager_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
}
|
||||
|
||||
void FullNodeMasterImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_prepareBlockProof &query,
|
||||
td::Promise<td::BufferSlice> promise) {
|
||||
if (query.block_->seqno_ == 0) {
|
||||
|
|
|
@ -46,6 +46,10 @@ class FullNodeMasterImpl : public FullNodeMaster {
|
|||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadBlock &query,
|
||||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadBlockFull &query,
|
||||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadNextBlockFull &query,
|
||||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_prepareZeroState &query,
|
||||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_preparePersistentState &query,
|
||||
|
|
114
validator/full-node-shard-queries.hpp
Normal file
114
validator/full-node-shard-queries.hpp
Normal file
|
@ -0,0 +1,114 @@
|
|||
#pragma once
|
||||
|
||||
#include "validator/validator.h"
|
||||
#include "ton/ton-tl.hpp"
|
||||
|
||||
namespace ton {
|
||||
|
||||
namespace validator {
|
||||
|
||||
namespace fullnode {
|
||||
|
||||
class BlockFullSender : public td::actor::Actor {
|
||||
public:
|
||||
BlockFullSender(BlockIdExt block_id, bool next, td::actor::ActorId<ValidatorManagerInterface> manager,
|
||||
td::Promise<td::BufferSlice> promise)
|
||||
: block_id_(block_id), next_(next), manager_(manager), promise_(std::move(promise)) {
|
||||
}
|
||||
void abort_query(td::Status error) {
|
||||
promise_.set_value(create_serialize_tl_object<ton_api::tonNode_dataFullEmpty>());
|
||||
stop();
|
||||
}
|
||||
void finish_query() {
|
||||
promise_.set_value(create_serialize_tl_object<ton_api::tonNode_dataFull>(
|
||||
create_tl_block_id(block_id_), std::move(proof_), std::move(data_), is_proof_link_));
|
||||
stop();
|
||||
}
|
||||
void start_up() override {
|
||||
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
|
||||
if (R.is_error()) {
|
||||
td::actor::send_closure(SelfId, &BlockFullSender::abort_query, R.move_as_error());
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &BlockFullSender::got_block_handle, R.move_as_ok());
|
||||
}
|
||||
});
|
||||
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_handle, block_id_, false, std::move(P));
|
||||
}
|
||||
|
||||
void got_block_handle(BlockHandle handle) {
|
||||
if (next_) {
|
||||
if (!handle->inited_next_left()) {
|
||||
return abort_query(td::Status::Error(ErrorCode::notready, "next not known"));
|
||||
}
|
||||
next_ = false;
|
||||
block_id_ = handle->one_next(true);
|
||||
start_up();
|
||||
return;
|
||||
}
|
||||
if (!handle->received() || (!handle->inited_proof() && !handle->inited_proof_link()) || handle->deleted()) {
|
||||
return abort_query(td::Status::Error(ErrorCode::notready, "not in db"));
|
||||
}
|
||||
handle_ = std::move(handle);
|
||||
is_proof_link_ = !handle_->inited_proof();
|
||||
|
||||
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<BlockData>> R) {
|
||||
if (R.is_error()) {
|
||||
td::actor::send_closure(SelfId, &BlockFullSender::abort_query, R.move_as_error());
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &BlockFullSender::got_block_data, R.move_as_ok()->data());
|
||||
}
|
||||
});
|
||||
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_data_from_db, handle_, std::move(P));
|
||||
|
||||
if (!is_proof_link_) {
|
||||
auto Q = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<Proof>> R) {
|
||||
if (R.is_error()) {
|
||||
td::actor::send_closure(SelfId, &BlockFullSender::abort_query, R.move_as_error());
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &BlockFullSender::got_block_proof, R.move_as_ok()->data());
|
||||
}
|
||||
});
|
||||
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_proof_from_db, handle_, std::move(Q));
|
||||
} else {
|
||||
auto Q = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ProofLink>> R) {
|
||||
if (R.is_error()) {
|
||||
td::actor::send_closure(SelfId, &BlockFullSender::abort_query, R.move_as_error());
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &BlockFullSender::got_block_proof, R.move_as_ok()->data());
|
||||
}
|
||||
});
|
||||
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_proof_link_from_db, handle_,
|
||||
std::move(Q));
|
||||
}
|
||||
}
|
||||
|
||||
void got_block_data(td::BufferSlice data) {
|
||||
data_ = std::move(data);
|
||||
if (!proof_.empty()) {
|
||||
finish_query();
|
||||
}
|
||||
}
|
||||
|
||||
void got_block_proof(td::BufferSlice data) {
|
||||
proof_ = std::move(data);
|
||||
if (!data_.empty()) {
|
||||
finish_query();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
BlockIdExt block_id_;
|
||||
bool next_;
|
||||
BlockHandle handle_;
|
||||
bool is_proof_link_;
|
||||
td::BufferSlice proof_;
|
||||
td::BufferSlice data_;
|
||||
td::actor::ActorId<ValidatorManagerInterface> manager_;
|
||||
td::Promise<td::BufferSlice> promise_;
|
||||
};
|
||||
|
||||
} // namespace fullnode
|
||||
|
||||
} // namespace validator
|
||||
|
||||
} // namespace ton
|
|
@ -18,13 +18,16 @@
|
|||
*/
|
||||
#include "td/utils/SharedSlice.h"
|
||||
#include "full-node-shard.hpp"
|
||||
#include "full-node-shard-queries.hpp"
|
||||
|
||||
#include "ton/ton-shard.h"
|
||||
#include "ton/ton-tl.hpp"
|
||||
#include "ton/ton-io.hpp"
|
||||
|
||||
#include "adnl/utils.hpp"
|
||||
#include "net/download-next-block.hpp"
|
||||
#include "net/download-block-new.hpp"
|
||||
#include "net/download-block.hpp"
|
||||
#include "net/download-next-block.hpp"
|
||||
#include "net/download-state.hpp"
|
||||
#include "net/download-proof.hpp"
|
||||
#include "net/get-next-key-blocks.hpp"
|
||||
|
@ -77,15 +80,22 @@ void FullNodeShardImpl::try_get_next_block(td::Timestamp timeout, td::Promise<Re
|
|||
promise.set_error(td::Status::Error(ErrorCode::timeout, "timeout"));
|
||||
return;
|
||||
}
|
||||
td::actor::create_actor<DownloadNextBlock>("downloadnext", adnl_id_, overlay_id_, handle_, download_next_priority(),
|
||||
timeout, validator_manager_, rldp_, overlays_, adnl_, client_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
if (use_new_download()) {
|
||||
td::actor::create_actor<DownloadBlockNew>("downloadnext", adnl_id_, overlay_id_, handle_->id(),
|
||||
ton::adnl::AdnlNodeIdShort::zero(), download_next_priority(), timeout,
|
||||
validator_manager_, rldp_, overlays_, adnl_, client_, std::move(promise))
|
||||
.release();
|
||||
} else {
|
||||
td::actor::create_actor<DownloadNextBlock>("downloadnext", adnl_id_, overlay_id_, handle_, download_next_priority(),
|
||||
timeout, validator_manager_, rldp_, overlays_, adnl_, client_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
}
|
||||
}
|
||||
|
||||
void FullNodeShardImpl::got_next_block(td::Result<BlockHandle> R) {
|
||||
if (R.is_error()) {
|
||||
if (R.error().code() == ErrorCode::timeout) {
|
||||
if (R.error().code() == ErrorCode::timeout || R.error().code() == ErrorCode::notready) {
|
||||
get_next_block();
|
||||
return;
|
||||
}
|
||||
|
@ -199,6 +209,20 @@ void FullNodeShardImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::tonNod
|
|||
create_block_id(query.block_), false, std::move(P));
|
||||
}
|
||||
|
||||
void FullNodeShardImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadBlockFull &query,
|
||||
td::Promise<td::BufferSlice> promise) {
|
||||
td::actor::create_actor<BlockFullSender>("sender", ton::create_block_id(query.block_), false, validator_manager_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
}
|
||||
|
||||
void FullNodeShardImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadNextBlockFull &query,
|
||||
td::Promise<td::BufferSlice> promise) {
|
||||
td::actor::create_actor<BlockFullSender>("sender", ton::create_block_id(query.prev_block_), true, validator_manager_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
}
|
||||
|
||||
void FullNodeShardImpl::process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_prepareBlockProof &query,
|
||||
td::Promise<td::BufferSlice> promise) {
|
||||
if (query.block_->seqno_ == 0) {
|
||||
|
@ -520,10 +544,17 @@ void FullNodeShardImpl::send_broadcast(BlockBroadcast broadcast) {
|
|||
|
||||
void FullNodeShardImpl::download_block(BlockIdExt id, td::uint32 priority, td::Timestamp timeout,
|
||||
td::Promise<ReceivedBlock> promise) {
|
||||
td::actor::create_actor<DownloadBlock>("downloadreq", id, adnl_id_, overlay_id_, adnl::AdnlNodeIdShort::zero(),
|
||||
priority, timeout, validator_manager_, rldp_, overlays_, adnl_, client_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
if (use_new_download()) {
|
||||
td::actor::create_actor<DownloadBlockNew>("downloadreq", id, adnl_id_, overlay_id_, adnl::AdnlNodeIdShort::zero(),
|
||||
priority, timeout, validator_manager_, rldp_, overlays_, adnl_, client_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
} else {
|
||||
td::actor::create_actor<DownloadBlock>("downloadreq", id, adnl_id_, overlay_id_, adnl::AdnlNodeIdShort::zero(),
|
||||
priority, timeout, validator_manager_, rldp_, overlays_, adnl_, client_,
|
||||
std::move(promise))
|
||||
.release();
|
||||
}
|
||||
}
|
||||
|
||||
void FullNodeShardImpl::download_zero_state(BlockIdExt id, td::uint32 priority, td::Timestamp timeout,
|
||||
|
|
|
@ -68,6 +68,10 @@ class FullNodeShardImpl : public FullNodeShard {
|
|||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadBlock &query,
|
||||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadBlockFull &query,
|
||||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_downloadNextBlockFull &query,
|
||||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_prepareZeroState &query,
|
||||
td::Promise<td::BufferSlice> promise);
|
||||
void process_query(adnl::AdnlNodeIdShort src, ton_api::tonNode_preparePersistentState &query,
|
||||
|
@ -126,6 +130,10 @@ class FullNodeShardImpl : public FullNodeShard {
|
|||
td::actor::ActorId<adnl::AdnlExtClient> client);
|
||||
|
||||
private:
|
||||
bool use_new_download() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
ShardIdFull shard_;
|
||||
BlockHandle handle_;
|
||||
td::Promise<td::Unit> promise_;
|
||||
|
|
|
@ -1665,7 +1665,21 @@ void ValidatorManagerImpl::allow_persistent_state_file_gc(BlockIdExt block_id, B
|
|||
promise.set_result(false);
|
||||
return;
|
||||
}
|
||||
promise.set_result(masterchain_block_id.id.seqno + (1 << 17) < gc_masterchain_handle_->id().id.seqno);
|
||||
if (masterchain_block_id.seqno() == 0) {
|
||||
promise.set_result(false);
|
||||
return;
|
||||
}
|
||||
if (masterchain_block_id.seqno() >= gc_masterchain_handle_->id().seqno()) {
|
||||
promise.set_result(false);
|
||||
return;
|
||||
}
|
||||
auto P = td::PromiseCreator::lambda([promise = std::move(promise)](td::Result<BlockHandle> R) mutable {
|
||||
R.ensure();
|
||||
auto handle = R.move_as_ok();
|
||||
CHECK(handle->is_key_block());
|
||||
promise.set_result(ValidatorManager::persistent_state_ttl(handle->unix_time()) < td::Clocks::system());
|
||||
});
|
||||
get_block_handle(masterchain_block_id, false, std::move(P));
|
||||
}
|
||||
|
||||
void ValidatorManagerImpl::allow_archive(BlockIdExt block_id, td::Promise<bool> promise) {
|
||||
|
@ -1814,6 +1828,16 @@ void ValidatorManagerImpl::state_serializer_update(BlockSeqno seqno) {
|
|||
void ValidatorManagerImpl::alarm() {
|
||||
try_advance_gc_masterchain_block();
|
||||
alarm_timestamp() = td::Timestamp::in(1.0);
|
||||
if (log_status_at_.is_in_past()) {
|
||||
if (last_masterchain_block_handle_) {
|
||||
LOG(INFO) << "STATUS: last_masterchain_block_ago="
|
||||
<< td::format::as_time(td::Clocks::system() - last_masterchain_block_handle_->unix_time())
|
||||
<< " last_known_key_block_ago="
|
||||
<< td::format::as_time(td::Clocks::system() - last_known_key_block_handle_->unix_time());
|
||||
}
|
||||
log_status_at_ = td::Timestamp::in(60.0);
|
||||
}
|
||||
alarm_timestamp().relax(log_status_at_);
|
||||
if (resend_shard_blocks_at_ && resend_shard_blocks_at_.is_in_past()) {
|
||||
resend_shard_blocks_at_ = td::Timestamp::never();
|
||||
for (auto &B : out_shard_blocks_) {
|
||||
|
|
|
@ -506,6 +506,7 @@ class ValidatorManagerImpl : public ValidatorManager {
|
|||
td::Timestamp resend_shard_blocks_at_;
|
||||
td::Timestamp check_waiters_at_;
|
||||
td::Timestamp check_shard_clients_;
|
||||
td::Timestamp log_status_at_;
|
||||
void alarm() override;
|
||||
std::map<ShardTopBlockDescriptionId, td::Ref<ShardTopBlockDescription>> out_shard_blocks_;
|
||||
|
||||
|
|
283
validator/net/download-block-new.cpp
Normal file
283
validator/net/download-block-new.cpp
Normal file
|
@ -0,0 +1,283 @@
|
|||
/*
|
||||
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-new.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 {
|
||||
|
||||
DownloadBlockNew::DownloadBlockNew(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::actor::ActorId<adnl::AdnlExtClient> client,
|
||||
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)
|
||||
, client_(client)
|
||||
, promise_(std::move(promise))
|
||||
, block_{block_id_, td::BufferSlice()}
|
||||
, allow_partial_proof_{!block_id_.is_masterchain()} {
|
||||
}
|
||||
|
||||
DownloadBlockNew::DownloadBlockNew(adnl::AdnlNodeIdShort local_id, overlay::OverlayIdShort overlay_id,
|
||||
BlockIdExt prev_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::actor::ActorId<adnl::AdnlExtClient> client,
|
||||
td::Promise<ReceivedBlock> promise)
|
||||
: local_id_(local_id)
|
||||
, overlay_id_(overlay_id)
|
||||
, prev_id_(prev_id)
|
||||
, download_from_(download_from)
|
||||
, priority_(priority)
|
||||
, timeout_(timeout)
|
||||
, validator_manager_(validator_manager)
|
||||
, rldp_(rldp)
|
||||
, overlays_(overlays)
|
||||
, adnl_(adnl)
|
||||
, client_(client)
|
||||
, promise_(std::move(promise))
|
||||
, block_{BlockIdExt{}, td::BufferSlice()} {
|
||||
}
|
||||
|
||||
void DownloadBlockNew::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 DownloadBlockNew::alarm() {
|
||||
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
|
||||
}
|
||||
|
||||
void DownloadBlockNew::finish_query() {
|
||||
if (promise_) {
|
||||
promise_.set_value(std::move(block_));
|
||||
}
|
||||
stop();
|
||||
}
|
||||
|
||||
void DownloadBlockNew::start_up() {
|
||||
alarm_timestamp() = timeout_;
|
||||
|
||||
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
|
||||
R.ensure();
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::got_block_handle, R.move_as_ok());
|
||||
});
|
||||
|
||||
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_block_handle,
|
||||
block_id_.is_valid() ? block_id_ : prev_id_, true, std::move(P));
|
||||
}
|
||||
|
||||
void DownloadBlockNew::got_block_handle(BlockHandle handle) {
|
||||
handle_ = std::move(handle);
|
||||
|
||||
if (!block_id_.is_valid()) {
|
||||
CHECK(prev_id_.is_valid());
|
||||
if (handle_->inited_next_left()) {
|
||||
block_id_ = handle_->one_next(true);
|
||||
block_.id = block_id_;
|
||||
handle_ = nullptr;
|
||||
start_up();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (block_id_.is_valid() &&
|
||||
(handle_->inited_proof() || (handle_->inited_proof_link() && allow_partial_proof_) || skip_proof_) &&
|
||||
handle_->received()) {
|
||||
CHECK(block_.id == block_id_);
|
||||
CHECK(handle_->id() == block_id_);
|
||||
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<BlockData>> R) {
|
||||
if (R.is_error()) {
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::abort_query,
|
||||
R.move_as_error_prefix("failed to get from db: "));
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::got_data_from_db, R.move_as_ok()->data());
|
||||
}
|
||||
});
|
||||
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_block_data_from_db, handle_,
|
||||
std::move(P));
|
||||
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, &DownloadBlockNew::abort_query,
|
||||
R.move_as_error_prefix("failed to get download token: "));
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::got_download_token, R.move_as_ok());
|
||||
}
|
||||
});
|
||||
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::get_download_token, 1, priority_, timeout_,
|
||||
std::move(P));
|
||||
}
|
||||
|
||||
void DownloadBlockNew::got_download_token(std::unique_ptr<DownloadToken> token) {
|
||||
token_ = std::move(token);
|
||||
|
||||
if (download_from_.is_zero() && client_.empty()) {
|
||||
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, &DownloadBlockNew::abort_query, R.move_as_error());
|
||||
} else {
|
||||
auto vec = R.move_as_ok();
|
||||
if (vec.size() == 0) {
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::abort_query,
|
||||
td::Status::Error(ErrorCode::notready, "no nodes"));
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::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 DownloadBlockNew::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, &DownloadBlockNew::abort_query, R.move_as_error());
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::got_data, R.move_as_ok());
|
||||
}
|
||||
});
|
||||
|
||||
td::BufferSlice q;
|
||||
if (block_id_.is_valid()) {
|
||||
q = create_serialize_tl_object<ton_api::tonNode_downloadBlockFull>(create_tl_block_id(block_id_));
|
||||
} else {
|
||||
q = create_serialize_tl_object<ton_api::tonNode_downloadNextBlockFull>(create_tl_block_id(prev_id_));
|
||||
}
|
||||
if (client_.empty()) {
|
||||
td::actor::send_closure(overlays_, &overlay::Overlays::send_query_via, download_from_, local_id_, overlay_id_,
|
||||
"get_proof", std::move(P), td::Timestamp::in(3.0), std::move(q),
|
||||
FullNode::max_proof_size() + FullNode::max_block_size() + 128, rldp_);
|
||||
} else {
|
||||
td::actor::send_closure(client_, &adnl::AdnlExtClient::send_query, "get_prepare",
|
||||
create_serialize_tl_object_suffix<ton_api::tonNode_query>(std::move(q)),
|
||||
td::Timestamp::in(1.0), std::move(P));
|
||||
}
|
||||
}
|
||||
|
||||
void DownloadBlockNew::got_data(td::BufferSlice data) {
|
||||
auto F = fetch_tl_object<ton_api::tonNode_DataFull>(std::move(data), true);
|
||||
|
||||
if (F.is_error()) {
|
||||
abort_query(F.move_as_error_prefix("received invalid answer: "));
|
||||
return;
|
||||
}
|
||||
|
||||
auto f = F.move_as_ok();
|
||||
|
||||
ton_api::downcast_call(
|
||||
*f.get(),
|
||||
td::overloaded(
|
||||
[&](ton_api::tonNode_dataFullEmpty &x) {
|
||||
abort_query(td::Status::Error(ErrorCode::notready, "node doesn't have this block"));
|
||||
},
|
||||
[&](ton_api::tonNode_dataFull &x) {
|
||||
if (!allow_partial_proof_ && x.is_link_) {
|
||||
abort_query(td::Status::Error(ErrorCode::notready, "node doesn't have proof for this block"));
|
||||
return;
|
||||
}
|
||||
auto id = create_block_id(x.id_);
|
||||
if (block_id_.is_valid() && id != block_id_) {
|
||||
abort_query(td::Status::Error(ErrorCode::notready, "received data for wrong block"));
|
||||
return;
|
||||
}
|
||||
block_.id = id;
|
||||
block_.data = std::move(x.block_);
|
||||
if (td::sha256_bits256(block_.data.as_slice()) != id.file_hash) {
|
||||
abort_query(td::Status::Error(ErrorCode::notready, "received data with bad hash"));
|
||||
return;
|
||||
}
|
||||
|
||||
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Unit> R) {
|
||||
if (R.is_error()) {
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::abort_query,
|
||||
R.move_as_error_prefix("received bad proof: "));
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &DownloadBlockNew::checked_block_proof);
|
||||
}
|
||||
});
|
||||
if (block_id_.is_valid()) {
|
||||
if (x.is_link_) {
|
||||
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::validate_block_proof_link,
|
||||
block_id_, std::move(x.proof_), std::move(P));
|
||||
} else {
|
||||
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::validate_block_proof, block_id_,
|
||||
std::move(x.proof_), std::move(P));
|
||||
}
|
||||
} else {
|
||||
CHECK(!x.is_link_);
|
||||
td::actor::send_closure(validator_manager_, &ValidatorManagerInterface::validate_block_is_next_proof,
|
||||
prev_id_, id, std::move(x.proof_), std::move(P));
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
void DownloadBlockNew::got_data_from_db(td::BufferSlice data) {
|
||||
block_.data = std::move(data);
|
||||
finish_query();
|
||||
}
|
||||
|
||||
void DownloadBlockNew::checked_block_proof() {
|
||||
finish_query();
|
||||
}
|
||||
|
||||
} // namespace fullnode
|
||||
|
||||
} // namespace validator
|
||||
|
||||
} // namespace ton
|
89
validator/net/download-block-new.hpp
Normal file
89
validator/net/download-block-new.hpp
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
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 "adnl/adnl-ext-client.h"
|
||||
|
||||
namespace ton {
|
||||
|
||||
namespace validator {
|
||||
|
||||
namespace fullnode {
|
||||
|
||||
class DownloadBlockNew : public td::actor::Actor {
|
||||
public:
|
||||
DownloadBlockNew(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::actor::ActorId<adnl::AdnlExtClient> client, td::Promise<ReceivedBlock> promise);
|
||||
DownloadBlockNew(adnl::AdnlNodeIdShort local_id, overlay::OverlayIdShort overlay_id, BlockIdExt prev_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::actor::ActorId<adnl::AdnlExtClient> client, 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_data(td::BufferSlice data);
|
||||
void got_data_from_db(td::BufferSlice data);
|
||||
void checked_block_proof();
|
||||
|
||||
private:
|
||||
BlockIdExt block_id_;
|
||||
adnl::AdnlNodeIdShort local_id_;
|
||||
overlay::OverlayIdShort overlay_id_;
|
||||
BlockIdExt prev_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::actor::ActorId<adnl::AdnlExtClient> client_;
|
||||
td::Promise<ReceivedBlock> promise_;
|
||||
|
||||
BlockHandle handle_;
|
||||
ReceivedBlock block_;
|
||||
bool skip_proof_ = false;
|
||||
|
||||
bool allow_partial_proof_ = false;
|
||||
|
||||
std::unique_ptr<DownloadToken> token_;
|
||||
};
|
||||
|
||||
} // namespace fullnode
|
||||
|
||||
} // namespace validator
|
||||
|
||||
} // namespace ton
|
Loading…
Add table
Add a link
Reference in a new issue