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

Get neighbors' msg queues from other nodes

This commit is contained in:
SpyCheese 2022-08-01 17:48:22 +03:00
parent 1869a25062
commit e43e235143
22 changed files with 658 additions and 68 deletions

View file

@ -15,6 +15,7 @@ set(TON_VALIDATOR_SOURCE
ihr-message.cpp
liteserver.cpp
message-queue.cpp
out-msg-queue-proof.cpp
proof.cpp
shard.cpp
signature-set.cpp
@ -32,13 +33,13 @@ set(TON_VALIDATOR_SOURCE
ihr-message.hpp
liteserver.hpp
message-queue.hpp
out-msg-queue-proof.hpp
proof.hpp
shard.hpp
signature-set.hpp
top-shard-descr.hpp
validate-query.hpp
validator-set.hpp
)
validator-set.hpp)
add_library(ton_validator STATIC ${TON_VALIDATOR_SOURCE})

View file

@ -205,7 +205,7 @@ class Collator final : public td::actor::Actor {
std::vector<ExtMessage::Hash> bad_ext_msgs_, delay_ext_msgs_;
Ref<vm::Cell> shard_account_blocks_; // ShardAccountBlocks
std::map<td::Bits256, Ref<BlockData>> blocks_with_state_proofs_;
std::map<td::Bits256, Ref<vm::Cell>> block_state_proofs_;
std::vector<vm::MerkleProofBuilder> neighbor_proof_builders_;
std::vector<Ref<vm::Cell>> collated_roots_;
@ -238,8 +238,7 @@ class Collator final : public td::actor::Actor {
void after_get_aux_shard_state(ton::BlockIdExt blkid, td::Result<Ref<ShardState>> res);
bool fix_one_processed_upto(block::MsgProcessedUpto& proc, const ton::ShardIdFull& owner);
bool fix_processed_upto(block::MsgProcessedUptoCollection& upto);
void got_neighbor_block_data(td::Result<Ref<BlockData>> res);
void got_neighbor_block_state(int i, td::Result<Ref<ShardState>> res);
void got_neighbor_msg_queue(unsigned i, td::Result<Ref<OutMsgQueueProof>> R);
bool adjust_shard_config();
bool store_shard_fees(ShardIdFull shard, const block::CurrencyCollection& fees,
const block::CurrencyCollection& created);

View file

@ -501,7 +501,13 @@ void Collator::after_get_block_data(int idx, td::Result<Ref<BlockData>> res) {
prev_mc_block = prev_block_data[0];
mc_block_root = prev_mc_block->root_cell();
}
blocks_with_state_proofs_[prev_block_data[idx]->root_cell()->get_hash().bits()] = prev_block_data[idx];
Ref<vm::Cell> root = prev_block_data[idx]->root_cell();
auto proof = create_block_state_proof(root);
if (proof.is_error()) {
fatal_error(proof.move_as_error());
return;
}
block_state_proofs_.emplace(root->get_hash().bits(), proof.move_as_ok());
}
check_pending();
}
@ -613,54 +619,39 @@ bool Collator::request_neighbor_msg_queues() {
}
neighbors_.emplace_back(*shard_ptr);
}
int i = 0;
unsigned i = 0;
neighbor_proof_builders_.resize(neighbors_.size());
for (block::McShardDescr& descr : neighbors_) {
LOG(DEBUG) << "neighbor #" << i << " : " << descr.blk_.to_str();
if (descr.blk_.seqno() != 0) {
++pending;
send_closure_later(manager, &ValidatorManager::wait_block_data_short, descr.blk_, priority(), timeout,
[self = get_self(), i](td::Result<Ref<BlockData>> res) {
LOG(DEBUG) << "got answer to wait_block_data for neighbor #" << i;
send_closure_later(std::move(self), &Collator::got_neighbor_block_data, std::move(res));
});
}
++pending;
send_closure_later(manager, &ValidatorManager::wait_block_state_short, descr.blk_, priority(), timeout,
[self = get_self(), i](td::Result<Ref<ShardState>> res) {
LOG(DEBUG) << "got answer to wait_block_state for neighbor #" << i;
send_closure_later(std::move(self), &Collator::got_neighbor_block_state, i, std::move(res));
send_closure_later(manager, &ValidatorManager::wait_out_msg_queue_proof, descr.blk_, shard_, priority(), timeout,
[self = get_self(), i](td::Result<Ref<OutMsgQueueProof>> res) {
LOG(DEBUG) << "got msg queue for neighbor #" << i;
send_closure_later(std::move(self), &Collator::got_neighbor_msg_queue, i, std::move(res));
});
++i;
}
return true;
}
void Collator::got_neighbor_block_data(td::Result<Ref<BlockData>> res) {
void Collator::got_neighbor_msg_queue(unsigned i, td::Result<Ref<OutMsgQueueProof>> R) {
--pending;
if (res.is_error()) {
fatal_error(res.move_as_error());
if (R.is_error()) {
fatal_error(R.move_as_error());
return;
}
auto block_data = res.move_as_ok();
blocks_with_state_proofs_[block_data->root_cell()->get_hash().bits()] = block_data;
check_pending();
}
void Collator::got_neighbor_block_state(int i, td::Result<Ref<ShardState>> res) {
--pending;
if (res.is_error()) {
fatal_error(res.move_as_error());
auto res = R.move_as_ok();
BlockIdExt block_id = neighbors_.at(i).blk_;
if (res->block_state_proof_.not_null()) {
block_state_proofs_.emplace(block_id.root_hash, res->block_state_proof_);
}
neighbor_proof_builders_.at(i) = vm::MerkleProofBuilder{res->state_root_};
auto state = ShardStateQ::fetch(block_id, {}, neighbor_proof_builders_.at(i).root());
if (state.is_error()) {
fatal_error(state.move_as_error());
return;
}
Ref<ShardState> state = res.move_as_ok();
neighbor_proof_builders_.at(i) = vm::MerkleProofBuilder{state->root_cell()};
auto new_state = ShardStateQ::fetch(state->get_block_id(), {}, neighbor_proof_builders_.at(i).root());
if (new_state.is_error()) {
fatal_error(new_state.move_as_error());
return;
}
auto outq_descr_res = new_state.move_as_ok()->message_queue();
auto outq_descr_res = state.move_as_ok()->message_queue();
if (outq_descr_res.is_error()) {
fatal_error(outq_descr_res.move_as_error());
return;
@ -3992,17 +3983,8 @@ bool Collator::create_collated_data() {
collated_roots_.push_back(std::move(cell));
}
// 2. Proofs for hashes of states: previous states + neighbors
for (const auto& p : blocks_with_state_proofs_) {
vm::MerkleProofBuilder mpb{p.second->root_cell()};
block::gen::Block::Record block;
if (!tlb::unpack_cell(mpb.root(), block) || block.state_update->load_cell().is_error()) {
return fatal_error("cannot generate Merkle proof for previous block");
}
Ref<vm::Cell> proof = mpb.extract_proof();
if (proof.is_null()) {
return fatal_error("cannot generate Merkle proof for previous block");
}
collated_roots_.push_back(std::move(proof));
for (const auto& p : block_state_proofs_) {
collated_roots_.push_back(p.second);
}
// 3. Previous state proof (only shadchains)
std::map<td::Bits256, Ref<vm::Cell>> proofs;

View file

@ -0,0 +1,245 @@
/*
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/>.
*/
#include "out-msg-queue-proof.hpp"
#include "interfaces/proof.h"
#include "shard.hpp"
#include "vm/cells/MerkleProof.h"
#include "common/delay.h"
#include "interfaces/validator-manager.h"
namespace ton {
namespace validator {
td::Result<td::Ref<OutMsgQueueProof>> OutMsgQueueProof::fetch(BlockIdExt block_id, ShardIdFull dst_shard,
const ton_api::tonNode_outMsgQueueProof &f) {
Ref<vm::Cell> block_state_proof;
td::Bits256 state_root_hash;
if (block_id.seqno() == 0) {
if (!f.block_state_proof_.empty()) {
return td::Status::Error("expected empty block state proof");
}
state_root_hash = block_id.root_hash;
} else {
TRY_RESULT_ASSIGN(block_state_proof, vm::std_boc_deserialize(f.block_state_proof_.as_slice()));
TRY_RESULT_ASSIGN(state_root_hash, unpack_block_state_proof(block_id, block_state_proof));
}
TRY_RESULT(queue_proof, vm::std_boc_deserialize(f.queue_proof_.as_slice()));
auto state_root = vm::MerkleProof::virtualize(queue_proof, 1);
if (state_root.is_null()) {
return td::Status::Error("invalid queue proof");
}
if (state_root->get_hash().as_slice() != state_root_hash.as_slice()) {
return td::Status::Error("state root hash mismatch");
}
// TODO: validate
return Ref<OutMsgQueueProof>(true, std::move(state_root), std::move(block_state_proof));
}
td::Result<tl_object_ptr<ton_api::tonNode_outMsgQueueProof>> OutMsgQueueProof::serialize(
BlockIdExt block_id, ShardIdFull dst_shard, Ref<vm::Cell> state_root, Ref<vm::Cell> block_root) {
vm::MerkleProofBuilder mpb{std::move(state_root)};
TRY_RESULT(state, ShardStateQ::fetch(block_id, {}, mpb.root()));
TRY_RESULT(outq_descr, state->message_queue());
// TODO: add only required part of msg queue
td::HashSet<vm::Cell::Hash> visited;
std::function<void(Ref<vm::Cell>)> dfs = [&](Ref<vm::Cell> cell) {
if (!visited.insert(cell->get_hash()).second) {
return;
}
vm::CellSlice cs(vm::NoVm(), cell);
for (unsigned i = 0; i < cs.size_refs(); i++) {
dfs(cs.prefetch_ref(i));
}
};
dfs(outq_descr->root_cell());
TRY_RESULT(queue_proof, vm::std_boc_serialize(mpb.extract_proof()));
td::BufferSlice block_state_proof;
if (block_id.seqno() != 0) {
TRY_RESULT(proof, create_block_state_proof(std::move(block_root)));
TRY_RESULT_ASSIGN(block_state_proof, vm::std_boc_serialize(std::move(proof), 31));
}
return create_tl_object<ton_api::tonNode_outMsgQueueProof>(std::move(queue_proof), std::move(block_state_proof));
}
void WaitOutMsgQueueProof::alarm() {
abort_query(td::Status::Error(ErrorCode::timeout, "timeout"));
}
void WaitOutMsgQueueProof::abort_query(td::Status reason) {
if (promise_) {
if (priority_ > 0 || (reason.code() != ErrorCode::timeout && reason.code() != ErrorCode::notready)) {
LOG(WARNING) << "aborting wait msg queue query for " << block_id_.to_str() << " priority=" << priority_ << ": "
<< reason;
} else {
LOG(DEBUG) << "aborting wait msg queue query for " << block_id_.to_str() << " priority=" << priority_ << ": "
<< reason;
}
promise_.set_error(
reason.move_as_error_prefix(PSTRING() << "failed to get msg queue for " << block_id_.to_str() << ": "));
}
stop();
}
void WaitOutMsgQueueProof::finish_query(Ref<OutMsgQueueProof> result) {
promise_.set_result(std::move(result));
stop();
}
void WaitOutMsgQueueProof::start_up() {
alarm_timestamp() = timeout_;
if (local_) {
run_local();
} else {
run_net();
}
}
void WaitOutMsgQueueProof::run_local() {
++pending;
td::actor::send_closure(manager_, &ValidatorManager::wait_block_state_short, block_id_, priority_, timeout_,
[SelfId = actor_id(this)](td::Result<Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitOutMsgQueueProof::abort_query,
R.move_as_error_prefix("failed to get shard state"));
} else {
td::actor::send_closure(SelfId, &WaitOutMsgQueueProof::got_state_root,
R.move_as_ok()->root_cell());
}
});
if (block_id_.seqno() != 0) {
++pending;
td::actor::send_closure(manager_, &ValidatorManager::wait_block_data_short, block_id_, priority_, timeout_,
[SelfId = actor_id(this)](td::Result<Ref<BlockData>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &WaitOutMsgQueueProof::abort_query,
R.move_as_error_prefix("failed to get block data"));
} else {
td::actor::send_closure(SelfId, &WaitOutMsgQueueProof::got_block_root,
R.move_as_ok()->root_cell());
}
});
}
}
void WaitOutMsgQueueProof::got_state_root(Ref<vm::Cell> root) {
state_root_ = std::move(root);
if (--pending == 0) {
run_local_cont();
}
}
void WaitOutMsgQueueProof::got_block_root(Ref<vm::Cell> root) {
block_root_ = std::move(root);
if (--pending == 0) {
run_local_cont();
}
}
void WaitOutMsgQueueProof::run_local_cont() {
Ref<vm::Cell> block_state_proof;
if (block_id_.seqno() != 0) {
auto R = create_block_state_proof(std::move(block_root_));
if (R.is_error()) {
abort_query(R.move_as_error_prefix("failed to create block state proof"));
return;
}
block_state_proof = R.move_as_ok();
}
finish_query(td::Ref<OutMsgQueueProof>(true, std::move(state_root_), std::move(block_state_proof)));
}
void WaitOutMsgQueueProof::run_net() {
auto P =
td::PromiseCreator::lambda([SelfId = actor_id(this), block_id = block_id_](td::Result<Ref<OutMsgQueueProof>> R) {
if (R.is_error()) {
LOG(DEBUG) << "failed to get msg queue for " << block_id.to_str() << " from net: " << R.move_as_error();
delay_action([SelfId]() mutable { td::actor::send_closure(SelfId, &WaitOutMsgQueueProof::run_net); },
td::Timestamp::in(0.1));
} else {
td::actor::send_closure(SelfId, &WaitOutMsgQueueProof::finish_query, R.move_as_ok());
}
});
td::actor::send_closure(manager_, &ValidatorManager::send_get_out_msg_queue_proof_request, block_id_, dst_shard_,
priority_, std::move(P));
}
void BuildOutMsgQueueProof::abort_query(td::Status reason) {
if (promise_) {
LOG(WARNING) << "failed to build msg queue proof for " << block_id_.to_str() << ": " << reason;
promise_.set_error(
reason.move_as_error_prefix(PSTRING() << "failed to build msg queue proof for " << block_id_.to_str() << ": "));
}
stop();
}
void BuildOutMsgQueueProof::start_up() {
++pending;
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_shard_state_from_db_short, block_id_,
[SelfId = actor_id(this)](td::Result<Ref<ShardState>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &BuildOutMsgQueueProof::abort_query,
R.move_as_error_prefix("failed to get shard state"));
} else {
td::actor::send_closure(SelfId, &BuildOutMsgQueueProof::got_state_root,
R.move_as_ok()->root_cell());
}
});
if (block_id_.seqno() != 0) {
++pending;
td::actor::send_closure(manager_, &ValidatorManagerInterface::get_block_data_from_db_short, block_id_,
[SelfId = actor_id(this)](td::Result<Ref<BlockData>> R) {
if (R.is_error()) {
td::actor::send_closure(SelfId, &BuildOutMsgQueueProof::abort_query,
R.move_as_error_prefix("failed to get block data"));
} else {
td::actor::send_closure(SelfId, &BuildOutMsgQueueProof::got_block_root,
R.move_as_ok()->root_cell());
}
});
}
}
void BuildOutMsgQueueProof::got_state_root(Ref<vm::Cell> root) {
state_root_ = std::move(root);
if (--pending == 0) {
build_proof();
}
}
void BuildOutMsgQueueProof::got_block_root(Ref<vm::Cell> root) {
block_root_ = std::move(root);
if (--pending == 0) {
build_proof();
}
}
void BuildOutMsgQueueProof::build_proof() {
promise_.set_result(
OutMsgQueueProof::serialize(block_id_, dst_shard_, std::move(state_root_), std::move(block_root_)));
stop();
}
} // namespace validator
} // namespace ton

View file

@ -0,0 +1,109 @@
/*
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/>.
*/
#pragma once
#include "vm/cells.h"
#include "ton/ton-types.h"
#include "auto/tl/ton_api.h"
#include "interfaces/out-msg-queue-proof.h"
#include "td/actor/actor.h"
namespace ton {
namespace validator {
using td::Ref;
class ValidatorManager;
class ValidatorManagerInterface;
class WaitOutMsgQueueProof : public td::actor::Actor {
public:
WaitOutMsgQueueProof(BlockIdExt block_id, ShardIdFull dst_shard, bool local, td::uint32 priority,
td::actor::ActorId<ValidatorManager> manager, td::Timestamp timeout,
td::Promise<Ref<OutMsgQueueProof>> promise)
: block_id_(std::move(block_id))
, dst_shard_(dst_shard)
, local_(local)
, 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(Ref<OutMsgQueueProof> result);
void alarm() override;
void start_up() override;
void run_local();
void got_state_root(Ref<vm::Cell> root);
void got_block_root(Ref<vm::Cell> root);
void run_local_cont();
void run_net();
private:
BlockIdExt block_id_;
ShardIdFull dst_shard_;
bool local_;
td::uint32 priority_;
td::actor::ActorId<ValidatorManager> manager_;
td::Timestamp timeout_;
td::Promise<Ref<OutMsgQueueProof>> promise_;
Ref<vm::Cell> state_root_, block_root_;
unsigned pending = 0;
};
class BuildOutMsgQueueProof : public td::actor::Actor {
public:
BuildOutMsgQueueProof(BlockIdExt block_id, ShardIdFull dst_shard,
td::actor::ActorId<ValidatorManagerInterface> manager,
td::Promise<tl_object_ptr<ton_api::tonNode_outMsgQueueProof>> promise)
: block_id_(std::move(block_id))
, dst_shard_(dst_shard)
, manager_(manager)
, promise_(std::move(promise)) {
}
void abort_query(td::Status reason);
void start_up() override;
void got_state_root(Ref<vm::Cell> root);
void got_block_root(Ref<vm::Cell> root);
void build_proof();
private:
BlockIdExt block_id_;
ShardIdFull dst_shard_;
td::actor::ActorId<ValidatorManagerInterface> manager_;
td::Promise<tl_object_ptr<ton_api::tonNode_outMsgQueueProof>> promise_;
Ref<vm::Cell> state_root_, block_root_;
unsigned pending = 0;
};
} // namespace validator
} // namespace ton

View file

@ -162,5 +162,40 @@ td::Result<Ref<vm::Cell>> ProofQ::get_signatures_root() const {
return proof.signatures->prefetch_ref();
}
td::Result<td::Ref<vm::Cell>> create_block_state_proof(td::Ref<vm::Cell> root) {
if (root.is_null()) {
return td::Status::Error("root is null");
}
vm::MerkleProofBuilder mpb{std::move(root)};
block::gen::Block::Record block;
if (!tlb::unpack_cell(mpb.root(), block) || block.state_update->load_cell().is_error()) {
return td::Status::Error("invalid block");
}
Ref<vm::Cell> proof = mpb.extract_proof();
if (proof.is_null()) {
return td::Status::Error("failed to create proof");
}
return proof;
}
td::Result<RootHash> unpack_block_state_proof(BlockIdExt block_id, td::Ref<vm::Cell> proof) {
auto virt_root = vm::MerkleProof::virtualize(proof, 1);
if (virt_root.is_null()) {
return td::Status::Error("invalid Merkle proof");
}
if (virt_root->get_hash().as_slice() != block_id.root_hash.as_slice()) {
return td::Status::Error("hash mismatch");
}
block::gen::Block::Record block;
if (!tlb::unpack_cell(virt_root, block)) {
return td::Status::Error("invalid block");
}
vm::CellSlice upd_cs{vm::NoVmSpec(), block.state_update};
if (!(upd_cs.is_special() && upd_cs.prefetch_long(8) == 4 && upd_cs.size_ext() == 0x20228)) {
return td::Status::Error("invalid Merkle update");
}
return upd_cs.prefetch_ref(1)->get_hash(0).bits();
}
} // namespace validator
} // namespace ton