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,233 @@
/*
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 "validator/fabric.h"
#include "common/checksum.h"
#include "common/delay.h"
#include "ton/ton-io.hpp"
namespace ton {
namespace validator {
DownloadShardState::DownloadShardState(BlockIdExt block_id, BlockIdExt masterchain_block_id, td::uint32 priority,
td::actor::ActorId<ValidatorManager> manager, td::Timestamp timeout,
td::Promise<td::Ref<ShardState>> promise)
: block_id_(block_id)
, masterchain_block_id_(masterchain_block_id)
, priority_(priority)
, manager_(manager)
, timeout_(timeout)
, promise_(std::move(promise)) {
}
void DownloadShardState::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, &DownloadShardState::got_block_handle, R.move_as_ok());
});
td::actor::send_closure(manager_, &ValidatorManager::get_block_handle, block_id_, true, std::move(P));
}
void DownloadShardState::got_block_handle(BlockHandle handle) {
handle_ = std::move(handle);
download_state();
}
void DownloadShardState::retry() {
download_state();
}
void DownloadShardState::download_state() {
if (handle_->id().seqno() == 0 || handle_->inited_proof() || handle_->inited_proof_link()) {
checked_proof_link();
return;
}
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
fail_handler(SelfId, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadShardState::downloaded_proof_link, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::send_get_block_proof_link_request, block_id_, priority_,
std::move(P));
}
void DownloadShardState::downloaded_proof_link(td::BufferSlice data) {
auto pp = create_proof_link(block_id_, std::move(data));
if (pp.is_error()) {
fail_handler(actor_id(this), pp.move_as_error());
return;
}
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<BlockHandle> R) {
if (R.is_error()) {
fail_handler(SelfId, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadShardState::checked_proof_link);
}
});
run_check_proof_link_query(block_id_, pp.move_as_ok(), manager_, td::Timestamp::in(60.0), std::move(P));
}
void DownloadShardState::checked_proof_link() {
if (block_id_.seqno() == 0) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &DownloadShardState::download_zero_state);
} else {
td::actor::send_closure(SelfId, &DownloadShardState::downloaded_zero_state, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::try_get_static_file, block_id_.file_hash, std::move(P));
} else {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
fail_handler(SelfId, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadShardState::downloaded_shard_state, R.move_as_ok());
}
});
CHECK(masterchain_block_id_.is_valid());
CHECK(masterchain_block_id_.is_masterchain());
td::actor::send_closure(manager_, &ValidatorManager::send_get_persistent_state_request, block_id_,
masterchain_block_id_, priority_, std::move(P));
}
}
void DownloadShardState::download_zero_state() {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
fail_handler(SelfId, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &DownloadShardState::downloaded_zero_state, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::send_get_zero_state_request, block_id_, priority_, std::move(P));
}
void DownloadShardState::downloaded_zero_state(td::BufferSlice data) {
if (sha256_bits256(data.as_slice()) != block_id_.file_hash) {
fail_handler(actor_id(this), td::Status::Error(ErrorCode::protoviolation, "bad zero state: file hash mismatch"));
return;
}
data_ = std::move(data);
auto S = create_shard_state(block_id_, data_.clone());
S.ensure();
state_ = S.move_as_ok();
CHECK(state_->root_hash() == block_id_.root_hash);
checked_shard_state();
}
void DownloadShardState::downloaded_shard_state(td::BufferSlice data) {
auto S = create_shard_state(block_id_, data.clone());
if (S.is_error()) {
fail_handler(actor_id(this), S.move_as_error());
return;
}
auto state = S.move_as_ok();
if (state->root_hash() != handle_->state()) {
fail_handler(actor_id(this),
td::Status::Error(ErrorCode::protoviolation, "bad persistent state: root hash mismatch"));
return;
}
auto St = state->validate_deep();
if (St.is_error()) {
fail_handler(actor_id(this), St.move_as_error());
return;
}
state_ = std::move(state);
data_ = data.clone();
checked_shard_state();
}
void DownloadShardState::checked_shard_state() {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Unit> R) {
R.ensure();
td::actor::send_closure(SelfId, &DownloadShardState::written_shard_state_file);
});
if (block_id_.seqno() == 0) {
td::actor::send_closure(manager_, &ValidatorManager::store_zero_state_file, block_id_, std::move(data_),
std::move(P));
} else {
td::actor::send_closure(manager_, &ValidatorManager::store_persistent_state_file, block_id_, masterchain_block_id_,
std::move(data_), std::move(P));
}
}
void DownloadShardState::written_shard_state_file() {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
R.ensure();
td::actor::send_closure(SelfId, &DownloadShardState::written_shard_state, R.move_as_ok());
});
td::actor::send_closure(manager_, &ValidatorManager::set_block_state, handle_, std::move(state_), std::move(P));
}
void DownloadShardState::written_shard_state(td::Ref<ShardState> state) {
state_ = std::move(state);
handle_->set_unix_time(state_->get_unix_time());
handle_->set_is_key_block(block_id_.is_masterchain());
handle_->set_logical_time(state_->get_logical_time());
handle_->set_applied();
handle_->set_split(state_->before_split());
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Unit> R) {
R.ensure();
td::actor::send_closure(SelfId, &DownloadShardState::written_block_handle);
});
handle_->flush(manager_, handle_, std::move(P));
}
void DownloadShardState::written_block_handle() {
finish_query();
}
void DownloadShardState::finish_query() {
if (promise_) {
promise_.set_value(std::move(state_));
}
stop();
}
void DownloadShardState::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void DownloadShardState::abort_query(td::Status reason) {
if (promise_) {
promise_.set_error(std::move(reason));
}
stop();
}
void DownloadShardState::fail_handler(td::actor::ActorId<DownloadShardState> SelfId, td::Status error) {
LOG(WARNING) << "failed to download state : " << error;
delay_action([=]() { td::actor::send_closure(SelfId, &DownloadShardState::retry); }, td::Timestamp::in(1.0));
}
} // 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 "validator/interfaces/validator-manager.h"
namespace ton {
namespace validator {
class DownloadShardState : public td::actor::Actor {
public:
DownloadShardState(BlockIdExt block_id, BlockIdExt masterchain_block_id, td::uint32 priority,
td::actor::ActorId<ValidatorManager> manager, td::Timestamp timeout,
td::Promise<td::Ref<ShardState>> promise);
void start_up() override;
void got_block_handle(BlockHandle handle);
void retry();
void downloaded_proof_link(td::BufferSlice data);
void checked_proof_link();
void download_state();
void download_zero_state();
void downloaded_zero_state(td::BufferSlice data);
void downloaded_shard_state(td::BufferSlice data);
void checked_shard_state();
void written_shard_state_file();
void written_shard_state(td::Ref<ShardState> state);
void written_block_handle();
void finish_query();
void alarm() override;
void abort_query(td::Status reason);
static void fail_handler(td::actor::ActorId<DownloadShardState> SelfId, td::Status error);
private:
BlockIdExt block_id_;
BlockIdExt masterchain_block_id_;
BlockHandle handle_;
td::uint32 priority_;
td::actor::ActorId<ValidatorManager> manager_;
td::Timestamp timeout_;
td::Promise<td::Ref<ShardState>> promise_;
td::BufferSlice data_;
td::Ref<ShardState> state_;
};
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,79 @@
/*
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 "wait-block-data-disk.hpp"
#include "fabric.h"
#include "adnl/utils.hpp"
#include "ton/ton-io.hpp"
namespace ton {
namespace validator {
void WaitBlockDataDisk::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void WaitBlockDataDisk::abort_query(td::Status reason) {
if (promise_) {
LOG(WARNING) << "aborting wait block data (disk) query for block " << handle_->id() << ": " << reason;
promise_.set_error(reason.move_as_error_prefix(PSTRING() << "failed to download (disk) " << handle_->id() << ": "));
}
stop();
}
void WaitBlockDataDisk::finish_query() {
CHECK(handle_->received());
if (promise_) {
promise_.set_result(data_);
}
stop();
}
void WaitBlockDataDisk::start_up() {
alarm_timestamp() = timeout_;
CHECK(handle_);
start();
}
void WaitBlockDataDisk::start() {
if (handle_->received() && (handle_->id().is_masterchain() ? handle_->inited_proof() : handle_->inited_proof_link())) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<BlockData>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockDataDisk::abort_query, R.move_as_error_prefix("db error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockDataDisk::got_block_data_from_db, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::get_block_data_from_db, handle_, std::move(P));
} else {
abort_query(td::Status::Error(ErrorCode::notready, "not in db"));
}
}
void WaitBlockDataDisk::got_block_data_from_db(td::Ref<BlockData> data) {
data_ = std::move(data);
finish_query();
}
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,65 @@
/*
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 "interfaces/block-handle.h"
#include "interfaces/validator-manager.h"
namespace ton {
namespace validator {
class ValidatorManager;
class WaitBlockDataDisk : public td::actor::Actor {
public:
WaitBlockDataDisk(BlockHandle handle, td::actor::ActorId<ValidatorManager> manager, td::Timestamp timeout,
td::Promise<td::Ref<BlockData>> promise)
: handle_(std::move(handle)), manager_(manager), timeout_(timeout), promise_(std::move(promise)) {
}
void update_timeout(td::Timestamp timeout) {
if (timeout.at() > timeout_.at()) {
timeout_ = timeout;
alarm_timestamp() = timeout_;
}
}
void abort_query(td::Status reason);
void finish_query();
void alarm() override;
void start_up() override;
void got_block_handle(BlockHandle handle);
void start();
void got_block_data_from_db(td::Ref<BlockData> data);
private:
BlockHandle handle_;
td::actor::ActorId<ValidatorManager> manager_;
td::Timestamp timeout_;
td::Promise<td::Ref<BlockData>> promise_;
td::Ref<BlockData> data_;
};
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,154 @@
/*
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 "wait-block-data.hpp"
#include "fabric.h"
#include "adnl/utils.hpp"
#include "ton/ton-io.hpp"
#include "common/delay.h"
namespace ton {
namespace validator {
void WaitBlockData::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void WaitBlockData::abort_query(td::Status reason) {
if (promise_) {
if (priority_ > 0 || (reason.code() != ErrorCode::timeout && reason.code() != ErrorCode::notready)) {
LOG(WARNING) << "aborting wait block data query for " << handle_->id() << " priority=" << priority_ << ": "
<< reason;
} else {
LOG(DEBUG) << "aborting wait block data query for " << handle_->id() << " priority=" << priority_ << ": "
<< reason;
}
promise_.set_error(reason.move_as_error_prefix(PSTRING() << "failed to download " << handle_->id() << ": "));
}
stop();
}
void WaitBlockData::finish_query() {
CHECK(handle_->received());
if (promise_) {
promise_.set_result(data_);
}
stop();
}
void WaitBlockData::start_up() {
alarm_timestamp() = timeout_;
CHECK(handle_);
start();
}
void WaitBlockData::start() {
if (reading_from_db_) {
return;
}
if (handle_->received() &&
(handle_->id().is_masterchain() ? handle_->inited_proof() : handle_->inited_proof_link())) {
reading_from_db_ = true;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<BlockData>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockData::abort_query, R.move_as_error_prefix("db get error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockData::got_block_data_from_db, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::get_block_data_from_db, handle_, std::move(P));
} else {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<ReceivedBlock> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockData::failed_to_get_block_data_from_net,
R.move_as_error_prefix("net error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockData::got_block_data_from_net, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::send_get_block_request, handle_->id(), priority_,
std::move(P));
}
}
void WaitBlockData::got_block_data_from_db(td::Ref<BlockData> data) {
data_ = std::move(data);
finish_query();
}
void WaitBlockData::failed_to_get_block_data_from_net(td::Status reason) {
if (reason.code() == ErrorCode::notready) {
LOG(DEBUG) << "failed to get block " << handle_->id() << " data from net: " << reason;
} else {
LOG(WARNING) << "failed to get block " << handle_->id() << " data from net: " << reason;
}
delay_action([SelfId = actor_id(this)]() mutable { td::actor::send_closure(SelfId, &WaitBlockData::start); },
td::Timestamp::in(0.1));
}
void WaitBlockData::got_block_data_from_net(ReceivedBlock block) {
auto X = create_block(std::move(block));
if (X.is_error()) {
failed_to_get_block_data_from_net(X.move_as_error_prefix("bad block from net: "));
return;
}
data_ = X.move_as_ok();
CHECK(handle_->id().is_masterchain() ? handle_->inited_proof() : handle_->inited_proof_link());
if (!handle_->received()) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Unit> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockData::abort_query, R.move_as_error_prefix("db set error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockData::finish_query);
}
});
td::actor::send_closure(manager_, &ValidatorManager::set_block_data, handle_, data_, std::move(P));
} else {
finish_query();
}
}
void WaitBlockData::force_read_from_db() {
if (reading_from_db_) {
return;
}
CHECK(handle_->id().is_masterchain() ? handle_->inited_proof() : handle_->inited_proof_link());
CHECK(handle_->received());
reading_from_db_ = true;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<BlockData>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockData::abort_query, R.move_as_error_prefix("db read error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockData::got_block_data_from_db, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::get_block_data_from_db, handle_, std::move(P));
}
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,78 @@
/*
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 "interfaces/block-handle.h"
#include "interfaces/validator-manager.h"
namespace ton {
namespace validator {
class ValidatorManager;
class WaitBlockData : public td::actor::Actor {
public:
WaitBlockData(BlockHandle handle, td::uint32 priority, td::actor::ActorId<ValidatorManager> manager,
td::Timestamp timeout, td::Promise<td::Ref<BlockData>> promise)
: handle_(std::move(handle))
, priority_(priority)
, manager_(manager)
, timeout_(timeout)
, promise_(std::move(promise)) {
}
void update_timeout(td::Timestamp timeout, td::uint32 priority) {
timeout_ = timeout;
alarm_timestamp() = timeout_;
priority_ = priority;
}
void abort_query(td::Status reason);
void finish_query();
void alarm() override;
void force_read_from_db();
void start_up() override;
void got_block_handle(BlockHandle handle);
void start();
void got_block_data_from_db(td::Ref<BlockData> data);
void got_block_data_from_net(ReceivedBlock data);
void failed_to_get_block_data_from_net(td::Status reason);
private:
BlockHandle handle_;
td::uint32 priority_;
td::actor::ActorId<ValidatorManager> manager_;
td::Timestamp timeout_;
td::Promise<td::Ref<BlockData>> promise_;
td::Ref<BlockData> data_;
bool reading_from_db_ = false;
//td::PerfWarningTimer perf_timer_{"waitdata", 1.0};
};
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,87 @@
/*
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 "wait-block-state-merge.hpp"
#include "wait-block-state.hpp"
#include "ton/ton-io.hpp"
namespace ton {
namespace validator {
void WaitBlockStateMerge::abort_query(td::Status reason) {
if (promise_) {
LOG(WARNING) << "aborting wait block state merge query for " << left_ << " and " << right_ << ": " << reason;
promise_.set_error(
reason.move_as_error_prefix(PSTRING() << "failed to download merge " << left_ << " and " << right_ << ": "));
}
stop();
}
void WaitBlockStateMerge::finish_query(td::Ref<ShardState> result) {
if (promise_) {
promise_.set_value(std::move(result));
}
stop();
}
void WaitBlockStateMerge::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void WaitBlockStateMerge::start_up() {
alarm_timestamp() = timeout_;
auto P_l = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockStateMerge::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &WaitBlockStateMerge::got_answer, true, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::wait_block_state_short, left_, priority_, timeout_,
std::move(P_l));
auto P_r = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockStateMerge::abort_query, R.move_as_error());
} else {
td::actor::send_closure(SelfId, &WaitBlockStateMerge::got_answer, false, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::wait_block_state_short, right_, priority_, timeout_,
std::move(P_r));
}
void WaitBlockStateMerge::got_answer(bool left, td::Ref<ShardState> state) {
(left ? left_state_ : right_state_) = std::move(state);
if (left_state_.not_null() && right_state_.not_null()) {
auto R = left_state_->merge_with(*right_state_.get());
if (R.is_error()) {
abort_query(R.move_as_error_prefix("failed to merge states: "));
} else {
finish_query(R.move_as_ok());
}
}
}
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,70 @@
/*
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 "interfaces/validator-manager.h"
namespace ton {
namespace validator {
class ValidatorManager;
class WaitBlockState;
class WaitBlockStateMerge : public td::actor::Actor {
public:
WaitBlockStateMerge(BlockIdExt left, BlockIdExt right, td::uint32 priority,
td::actor::ActorId<ValidatorManager> manager, td::Timestamp timeout,
td::Promise<td::Ref<ShardState>> promise)
: left_(left)
, right_(right)
, priority_(priority)
, manager_(manager)
, timeout_(timeout)
, promise_(std::move(promise)) {
}
void abort_query(td::Status reason);
void finish_query(td::Ref<ShardState> result);
void alarm() override;
void start_up() override;
void got_answer(bool left, td::Ref<ShardState> state);
private:
BlockIdExt left_;
BlockIdExt right_;
td::uint32 priority_;
td::actor::ActorId<ValidatorManager> manager_;
td::Timestamp timeout_;
td::Promise<td::Ref<ShardState>> promise_;
//td::actor::ActorOwn<WaitBlockState> left_query_;
//td::actor::ActorOwn<WaitBlockState> right_query_;
td::Ref<ShardState> left_state_;
td::Ref<ShardState> right_state_;
};
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,322 @@
/*
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 "wait-block-state.hpp"
#include "validator/fabric.h"
#include "ton/ton-io.hpp"
#include "common/checksum.h"
namespace ton {
namespace validator {
void WaitBlockState::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void WaitBlockState::abort_query(td::Status reason) {
if (promise_) {
if (priority_ > 0 || (reason.code() != ErrorCode::timeout && reason.code() != ErrorCode::notready)) {
LOG(WARNING) << "aborting wait block state query for " << handle_->id() << " priority=" << priority_ << ": "
<< reason;
} else {
LOG(DEBUG) << "aborting wait block state query for " << handle_->id() << " priority=" << priority_ << ": "
<< reason;
}
promise_.set_error(reason.move_as_error_prefix(PSTRING() << "failed to download state " << handle_->id() << ": "));
}
stop();
}
void WaitBlockState::finish_query() {
CHECK(handle_->received_state());
/*if (handle_->id().is_masterchain() && handle_->inited_proof()) {
td::actor::send_closure(manager_, &ValidatorManager::new_block, handle_, prev_state_, [](td::Unit) {});
}*/
if (promise_) {
promise_.set_result(prev_state_);
}
stop();
}
void WaitBlockState::start_up() {
alarm_timestamp() = timeout_;
CHECK(handle_);
start();
}
void WaitBlockState::start() {
if (reading_from_db_) {
return;
}
if (handle_->received_state()) {
reading_from_db_ = true;
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::abort_query, R.move_as_error_prefix("db error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::got_state_from_db, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::get_shard_state_from_db, handle_, std::move(P));
} else if (handle_->id().id.seqno == 0 && next_static_file_attempt_.is_in_past()) {
next_static_file_attempt_ = td::Timestamp::in(60.0);
// id.file_hash contrains correct file hash of zero state
// => if file with this sha256 is found it is garanteed to be correct
// => if it is not, this error is permanent
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), id = handle_->id()](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
if (R.error().code() == ErrorCode::notready) {
td::actor::send_closure(SelfId, &WaitBlockState::start);
} else {
td::actor::send_closure(SelfId, &WaitBlockState::abort_query, R.move_as_error_prefix("static db error: "));
}
} else {
auto data = R.move_as_ok();
td::actor::send_closure(SelfId, &WaitBlockState::got_state_from_net, std::move(data));
}
});
td::actor::send_closure(manager_, &ValidatorManager::try_get_static_file, handle_->id().file_hash, std::move(P));
} else if (handle_->id().id.seqno == 0) {
// do not try to download full chain
// download state + proof_link only
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::failed_to_get_state_from_net,
R.move_as_error_prefix("net error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::got_state_from_net, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::send_get_zero_state_request, handle_->id(), priority_,
std::move(P));
} else if (block_.is_null()) {
// download block and then prev state, not in reverse only
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<BlockData>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::failed_to_get_block_data,
R.move_as_error_prefix("block wait error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::got_block_data, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::wait_block_data, handle_, priority_, timeout_, std::move(P));
} else if (prev_state_.is_null()) {
CHECK(handle_->inited_proof() || handle_->inited_proof_link());
CHECK(handle_->received());
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::failed_to_get_prev_state,
R.move_as_error_prefix("prev state wait error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::got_prev_state, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::wait_prev_block_state, handle_, priority_, timeout_,
std::move(P));
} else {
apply();
}
}
void WaitBlockState::failed_to_get_prev_state(td::Status reason) {
if (reason.code() == ErrorCode::notready) {
start();
} else {
abort_query(std::move(reason));
}
}
void WaitBlockState::got_prev_state(td::Ref<ShardState> state) {
prev_state_ = std::move(state);
start();
}
void WaitBlockState::failed_to_get_block_data(td::Status reason) {
if (reason.code() == ErrorCode::notready) {
start();
} else {
abort_query(std::move(reason));
}
}
void WaitBlockState::got_block_data(td::Ref<BlockData> data) {
block_ = std::move(data);
start();
}
void WaitBlockState::apply() {
td::PerfWarningTimer t{"applyblocktostate", 0.1};
auto S = prev_state_.write().apply_block(handle_->id(), block_);
if (S.is_error()) {
abort_query(S.move_as_error_prefix("apply error: "));
return;
}
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::abort_query, R.move_as_error_prefix("db set error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::written_state, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::set_block_state, handle_, prev_state_, std::move(P));
}
void WaitBlockState::written_state(td::Ref<ShardState> upd_state) {
prev_state_ = std::move(upd_state);
finish_query();
}
void WaitBlockState::got_state_from_db(td::Ref<ShardState> state) {
prev_state_ = state;
if (!handle_->received_state()) {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::abort_query, R.move_as_error_prefix("db set error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::written_state, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::set_block_state, handle_, prev_state_, std::move(P));
} else {
finish_query();
}
}
void WaitBlockState::got_state_from_static_file(td::Ref<ShardState> state, td::BufferSlice data) {
auto P =
td::PromiseCreator::lambda([SelfId = actor_id(this), state = std::move(state)](td::Result<td::Unit> R) mutable {
R.ensure();
td::actor::send_closure(SelfId, &WaitBlockState::got_state_from_db, std::move(state));
});
td::actor::send_closure(manager_, &ValidatorManager::store_zero_state_file, handle_->id(), std::move(data),
std::move(P));
}
void WaitBlockState::force_read_from_db() {
if (!handle_ || reading_from_db_) {
return;
}
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::abort_query, R.move_as_error_prefix("db get error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::got_state_from_db, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::get_shard_state_from_db, handle_, std::move(P));
}
void WaitBlockState::got_state_from_net(td::BufferSlice data) {
auto R = create_shard_state(handle_->id(), data.clone());
if (R.is_error()) {
LOG(WARNING) << "received bad state from net: " << R.move_as_error();
start();
return;
}
auto state = R.move_as_ok();
if (handle_->id().id.seqno == 0) {
handle_->set_state_root_hash(handle_->id().root_hash);
}
if (state->root_hash() != handle_->state()) {
LOG(WARNING) << "received state have bad root hash";
start();
return;
}
if (handle_->id().id.seqno != 0) {
auto S = state->validate_deep();
if (S.is_error()) {
LOG(WARNING) << "received bad state from net: " << S;
start();
return;
}
} else {
if (sha256_bits256(data.as_slice()) != handle_->id().file_hash) {
LOG(WARNING) << "received bad state from net: file hash mismatch";
start();
return;
}
}
handle_->set_logical_time(state->get_logical_time());
handle_->set_unix_time(state->get_unix_time());
handle_->set_is_key_block(handle_->id().is_masterchain() && handle_->id().id.seqno == 0);
handle_->set_split(state->before_split());
prev_state_ = std::move(state);
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Unit> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::abort_query, R.move_as_error_prefix("db set error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::written_state_file);
}
});
td::actor::send_closure(manager_, &ValidatorManager::store_zero_state_file, handle_->id(), std::move(data),
std::move(P));
}
void WaitBlockState::written_state_file() {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::abort_query, R.move_as_error_prefix("db set error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::written_state, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::set_block_state, handle_, prev_state_, std::move(P));
}
void WaitBlockState::failed_to_get_zero_state() {
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this)](td::Result<td::BufferSlice> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitBlockState::failed_to_get_state_from_net,
R.move_as_error_prefix("net error: "));
} else {
td::actor::send_closure(SelfId, &WaitBlockState::got_state_from_net, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::send_get_zero_state_request, handle_->id(), priority_,
std::move(P));
}
void WaitBlockState::failed_to_get_state_from_net(td::Status reason) {
if (reason.code() == ErrorCode::notready) {
LOG(DEBUG) << "failed to download state for " << handle_->id() << " from net: " << reason;
} else {
LOG(WARNING) << "failed to download state for " << handle_->id() << " from net: " << reason;
}
start();
}
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,86 @@
/*
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 "interfaces/validator-manager.h"
namespace ton {
namespace validator {
class WaitBlockState : public td::actor::Actor {
public:
WaitBlockState(BlockHandle handle, td::uint32 priority, td::actor::ActorId<ValidatorManager> manager,
td::Timestamp timeout, td::Promise<td::Ref<ShardState>> promise)
: handle_(std::move(handle))
, priority_(priority)
, manager_(manager)
, timeout_(timeout)
, promise_(std::move(promise)) {
}
void abort_query(td::Status reason);
void finish_query();
void alarm() override;
void force_read_from_db();
void start_up() override;
void got_block_handle(BlockHandle handle);
void start();
void got_state_from_db(td::Ref<ShardState> data);
void got_state_from_static_file(td::Ref<ShardState> state, td::BufferSlice data);
void failed_to_get_state_from_db(td::Status reason);
void got_prev_state(td::Ref<ShardState> state);
void failed_to_get_prev_state(td::Status reason);
void got_block_data(td::Ref<BlockData> data);
void failed_to_get_block_data(td::Status reason);
void got_state_from_net(td::BufferSlice data);
void failed_to_get_zero_state();
void failed_to_get_state_from_net(td::Status reason);
void apply();
void written_state(td::Ref<ShardState> upd_state);
void written_state_file();
void update_timeout(td::Timestamp timeout, td::uint32 priority) {
timeout_ = timeout;
alarm_timestamp() = timeout_;
priority_ = priority;
}
private:
BlockHandle handle_;
td::uint32 priority_;
td::actor::ActorId<ValidatorManager> manager_;
td::Timestamp timeout_;
td::Promise<td::Ref<ShardState>> promise_;
td::Ref<ShardState> prev_state_;
td::Ref<BlockData> block_;
bool reading_from_db_ = false;
td::Timestamp next_static_file_attempt_;
//td::PerfWarningTimer perf_timer_{"waitstate", 1.0};
};
} // namespace validator
} // namespace ton