mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
Limit imported msg queue size
This commit is contained in:
parent
e4e77c16c5
commit
5dd0c15d07
12 changed files with 227 additions and 122 deletions
|
@ -22,94 +22,45 @@
|
|||
#include "interfaces/validator-manager.h"
|
||||
#include "block/block-parse.h"
|
||||
#include "block/block-auto.h"
|
||||
#include "output-queue-merger.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));
|
||||
static td::Status check_no_prunned(const Ref<vm::Cell>& cell) {
|
||||
if (cell.is_null()) {
|
||||
return td::Status::OK();
|
||||
}
|
||||
|
||||
TRY_RESULT(queue_proof, vm::std_boc_deserialize(f.queue_proof_.as_slice()));
|
||||
auto virtual_root = vm::MerkleProof::virtualize(queue_proof, 1);
|
||||
if (virtual_root.is_null()) {
|
||||
return td::Status::Error("invalid queue proof");
|
||||
TRY_RESULT(loaded_cell, cell->load_cell());
|
||||
if (loaded_cell.data_cell->get_level() > 0) {
|
||||
return td::Status::Error("prunned branch");
|
||||
}
|
||||
if (virtual_root->get_hash().as_slice() != state_root_hash.as_slice()) {
|
||||
return td::Status::Error("state root hash mismatch");
|
||||
}
|
||||
|
||||
// Validate proof
|
||||
auto state_root = vm::CellSlice(vm::NoVm(), queue_proof).prefetch_ref(0);
|
||||
TRY_RESULT_PREFIX(state, ShardStateQ::fetch(block_id, {}, state_root), "invalid proof: ");
|
||||
TRY_RESULT_PREFIX(outq_descr, state->message_queue(), "invalid proof: ");
|
||||
|
||||
block::gen::OutMsgQueueInfo::Record qinfo;
|
||||
if (!tlb::unpack_cell(outq_descr->root_cell(), qinfo)) {
|
||||
return td::Status::Error("invalid proof: invalid message queue");
|
||||
}
|
||||
td::Ref<vm::Cell> proc_info = qinfo.proc_info->prefetch_ref(0);
|
||||
if (proc_info.not_null() && proc_info->get_level() != 0) {
|
||||
return td::Status::Error("invalid proof: proc_info has prunned branches");
|
||||
}
|
||||
td::Ref<vm::Cell> ihr_pending = qinfo.ihr_pending->prefetch_ref(0);
|
||||
if (ihr_pending.not_null() && ihr_pending->get_level() != 0) {
|
||||
return td::Status::Error("invalid proof: ihr_pending has prunned branches");
|
||||
}
|
||||
auto queue =
|
||||
std::make_unique<vm::AugmentedDictionary>(qinfo.out_queue->prefetch_ref(0), 352, block::tlb::aug_OutMsgQueue);
|
||||
td::BitArray<96> prefix;
|
||||
td::BitPtr ptr = prefix.bits();
|
||||
ptr.store_int(dst_shard.workchain, 32);
|
||||
ptr.advance(32);
|
||||
ptr.store_uint(dst_shard.shard, 64);
|
||||
if (!queue->cut_prefix_subdict(prefix.bits(), 32 + dst_shard.pfx_len())) {
|
||||
return td::Status::Error("invalid proof: failed to cut queue dict");
|
||||
}
|
||||
if (queue->get_root_cell().not_null() && queue->get_root_cell()->get_level() != 0) {
|
||||
return td::Status::Error("invalid proof: msg queue has prunned branches");
|
||||
}
|
||||
|
||||
return Ref<OutMsgQueueProof>(true, std::move(virtual_root), std::move(block_state_proof));
|
||||
return td::Status::OK();
|
||||
}
|
||||
|
||||
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) {
|
||||
if (!dst_shard.is_valid_ext()) {
|
||||
return td::Status::Error("invalid shard");
|
||||
}
|
||||
vm::MerkleProofBuilder mpb{std::move(state_root)};
|
||||
TRY_RESULT(state, ShardStateQ::fetch(block_id, {}, mpb.root()));
|
||||
TRY_RESULT(outq_descr, state->message_queue());
|
||||
block::gen::OutMsgQueueInfo::Record qinfo;
|
||||
if (!tlb::unpack_cell(outq_descr->root_cell(), qinfo)) {
|
||||
return td::Status::Error("invalid message queue");
|
||||
static td::Status check_no_prunned(const vm::CellSlice& cs) {
|
||||
for (unsigned i = 0; i < cs.size_refs(); ++i) {
|
||||
TRY_STATUS(check_no_prunned(cs.prefetch_ref(i)));
|
||||
}
|
||||
return td::Status::OK();
|
||||
}
|
||||
|
||||
static td::Result<td::int32> process_queue(BlockIdExt block_id, ShardIdFull dst_shard,
|
||||
const block::gen::OutMsgQueueInfo::Record& qinfo) {
|
||||
td::uint64 estimated_proof_size = 0;
|
||||
|
||||
td::HashSet<vm::Cell::Hash> visited;
|
||||
std::function<void(Ref<vm::Cell>)> dfs = [&](Ref<vm::Cell> cell) {
|
||||
std::function<void(const vm::CellSlice&)> dfs_cs;
|
||||
auto dfs = [&](Ref<vm::Cell> cell) {
|
||||
if (cell.is_null() || !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_cs(vm::CellSlice(vm::NoVm(), cell));
|
||||
};
|
||||
auto dfs_cs = [&](const vm::CellSlice &cs) {
|
||||
dfs_cs = [&](const vm::CellSlice& cs) {
|
||||
// Based on BlockLimitStatus::estimate_block_size
|
||||
estimated_proof_size += 12 + (cs.size() + 7) / 8 + cs.size_refs() * 3;
|
||||
for (unsigned i = 0; i < cs.size_refs(); i++) {
|
||||
dfs(cs.prefetch_ref(i));
|
||||
}
|
||||
|
@ -117,17 +68,65 @@ td::Result<tl_object_ptr<ton_api::tonNode_outMsgQueueProof>> OutMsgQueueProof::s
|
|||
dfs_cs(*qinfo.proc_info);
|
||||
dfs_cs(*qinfo.ihr_pending);
|
||||
|
||||
auto queue =
|
||||
std::make_unique<vm::AugmentedDictionary>(qinfo.out_queue->prefetch_ref(0), 352, block::tlb::aug_OutMsgQueue);
|
||||
td::BitArray<96> prefix;
|
||||
td::BitPtr ptr = prefix.bits();
|
||||
ptr.store_int(dst_shard.workchain, 32);
|
||||
ptr.advance(32);
|
||||
ptr.store_uint(dst_shard.shard, 64);
|
||||
if (!queue->cut_prefix_subdict(prefix.bits(), 32 + dst_shard.pfx_len())) {
|
||||
block::OutputQueueMerger queue_merger{
|
||||
dst_shard, {block::OutputQueueMerger::Neighbor{block_id, qinfo.out_queue->prefetch_ref()}}};
|
||||
td::int32 msg_count = 0;
|
||||
bool limit_reached = false;
|
||||
|
||||
while (!queue_merger.is_eof()) {
|
||||
auto kv = queue_merger.extract_cur();
|
||||
queue_merger.next();
|
||||
++msg_count;
|
||||
|
||||
// TODO: Get processed_upto from destination shard (in request?)
|
||||
/*
|
||||
// Parse message to check if it was processed (as in Collator::process_inbound_message)
|
||||
ton::LogicalTime enqueued_lt = kv->msg->prefetch_ulong(64);
|
||||
auto msg_env = kv->msg->prefetch_ref();
|
||||
block::tlb::MsgEnvelope::Record_std env;
|
||||
if (!tlb::unpack_cell(msg_env, env)) {
|
||||
return td::Status::Error("cannot unpack MsgEnvelope of an internal message");
|
||||
}
|
||||
vm::CellSlice cs{vm::NoVmOrd{}, env.msg};
|
||||
block::gen::CommonMsgInfo::Record_int_msg_info info;
|
||||
if (!tlb::unpack(cs, info)) {
|
||||
return td::Status::Error("cannot unpack CommonMsgInfo of an internal message");
|
||||
}
|
||||
auto src_prefix = block::tlb::MsgAddressInt::get_prefix(info.src);
|
||||
auto dest_prefix = block::tlb::MsgAddressInt::get_prefix(info.dest);
|
||||
auto cur_prefix = block::interpolate_addr(src_prefix, dest_prefix, env.cur_addr);
|
||||
auto next_prefix = block::interpolate_addr(src_prefix, dest_prefix, env.next_addr);
|
||||
block::EnqueuedMsgDescr descr{cur_prefix, next_prefix, kv->lt, enqueued_lt, env.msg->get_hash().bits()};
|
||||
if (dst_processed_upto->already_processed(descr)) {
|
||||
} else {
|
||||
}*/
|
||||
|
||||
dfs_cs(*kv->msg);
|
||||
TRY_STATUS_PREFIX(check_no_prunned(*kv->msg), "invalid message proof: ")
|
||||
if (estimated_proof_size > OutMsgQueueProof::QUEUE_SIZE_THRESHOLD) {
|
||||
limit_reached = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return limit_reached ? msg_count : -1;
|
||||
}
|
||||
|
||||
td::Result<tl_object_ptr<ton_api::tonNode_outMsgQueueProof>> OutMsgQueueProof::build(BlockIdExt block_id,
|
||||
ShardIdFull dst_shard,
|
||||
Ref<vm::Cell> state_root,
|
||||
Ref<vm::Cell> block_root) {
|
||||
if (!dst_shard.is_valid_ext()) {
|
||||
return td::Status::Error("invalid shard");
|
||||
}
|
||||
|
||||
vm::MerkleProofBuilder mpb{std::move(state_root)};
|
||||
TRY_RESULT(state, ShardStateQ::fetch(block_id, {}, mpb.root()));
|
||||
TRY_RESULT(outq_descr, state->message_queue());
|
||||
block::gen::OutMsgQueueInfo::Record qinfo;
|
||||
if (!tlb::unpack_cell(outq_descr->root_cell(), qinfo)) {
|
||||
return td::Status::Error("invalid message queue");
|
||||
}
|
||||
dfs(queue->get_root_cell());
|
||||
TRY_RESULT(cnt, process_queue(block_id, dst_shard, qinfo));
|
||||
|
||||
TRY_RESULT(queue_proof, mpb.extract_proof_boc());
|
||||
td::BufferSlice block_state_proof;
|
||||
|
@ -135,7 +134,52 @@ td::Result<tl_object_ptr<ton_api::tonNode_outMsgQueueProof>> OutMsgQueueProof::s
|
|||
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));
|
||||
return create_tl_object<ton_api::tonNode_outMsgQueueProof>(std::move(queue_proof), std::move(block_state_proof),
|
||||
cnt);
|
||||
}
|
||||
|
||||
td::Result<td::Ref<OutMsgQueueProof>> OutMsgQueueProof::fetch(BlockIdExt block_id, ShardIdFull dst_shard,
|
||||
const ton_api::tonNode_outMsgQueueProof& f) {
|
||||
try {
|
||||
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 virtual_root = vm::MerkleProof::virtualize(queue_proof, 1);
|
||||
if (virtual_root.is_null()) {
|
||||
return td::Status::Error("invalid queue proof");
|
||||
}
|
||||
if (virtual_root->get_hash().as_slice() != state_root_hash.as_slice()) {
|
||||
return td::Status::Error("state root hash mismatch");
|
||||
}
|
||||
|
||||
// Validate proof
|
||||
TRY_RESULT_PREFIX(state, ShardStateQ::fetch(block_id, {}, virtual_root), "invalid proof: ");
|
||||
TRY_RESULT_PREFIX(outq_descr, state->message_queue(), "invalid proof: ");
|
||||
|
||||
block::gen::OutMsgQueueInfo::Record qinfo;
|
||||
if (!tlb::unpack_cell(outq_descr->root_cell(), qinfo)) {
|
||||
return td::Status::Error("invalid proof: invalid message queue");
|
||||
}
|
||||
TRY_STATUS_PREFIX(check_no_prunned(qinfo.proc_info->prefetch_ref(0)), "invalid proc_info: ")
|
||||
TRY_STATUS_PREFIX(check_no_prunned(qinfo.ihr_pending->prefetch_ref(0)), "invalid ihr_pending: ")
|
||||
TRY_RESULT(cnt, process_queue(block_id, dst_shard, qinfo));
|
||||
if (cnt != f.msg_count_) {
|
||||
return td::Status::Error(PSTRING() << "invalid msg_count: expected=" << f.msg_count_ << ", found=" << cnt);
|
||||
}
|
||||
return Ref<OutMsgQueueProof>(true, std::move(virtual_root), std::move(block_state_proof), cnt);
|
||||
} catch (vm::VmVirtError& err) {
|
||||
return td::Status::Error(PSTRING() << "invalid proof: " << err.get_msg());
|
||||
}
|
||||
}
|
||||
|
||||
void WaitOutMsgQueueProof::alarm() {
|
||||
|
@ -296,7 +340,7 @@ void BuildOutMsgQueueProof::got_block_root(Ref<vm::Cell> root) {
|
|||
}
|
||||
|
||||
void BuildOutMsgQueueProof::build_proof() {
|
||||
auto result = OutMsgQueueProof::serialize(block_id_, dst_shard_, std::move(state_root_), std::move(block_root_));
|
||||
auto result = OutMsgQueueProof::build(block_id_, dst_shard_, std::move(state_root_), std::move(block_root_));
|
||||
if (result.is_error()) {
|
||||
LOG(ERROR) << "Failed to build msg queue proof: " << result.error();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue