From 310dd6dec1c7a79b781d9b2a1881a3388841c141 Mon Sep 17 00:00:00 2001 From: EmelyanenkoK Date: Wed, 21 Feb 2024 16:13:55 +0300 Subject: [PATCH] Fix fetching mc config for runSmcMethod, fix caching in LS (#915) Co-authored-by: SpyCheese --- validator/impl/liteserver.cpp | 38 ++++++++++++++++++++++++++++++++--- validator/impl/liteserver.hpp | 3 ++- validator/impl/shard.cpp | 9 +++++++++ validator/impl/shard.hpp | 4 ++++ validator/interfaces/shard.h | 1 + 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/validator/impl/liteserver.cpp b/validator/impl/liteserver.cpp index 139c23471..9b8cb300c 100644 --- a/validator/impl/liteserver.cpp +++ b/validator/impl/liteserver.cpp @@ -157,7 +157,7 @@ void LiteQuery::start_up() { }); return; } - use_cache_ = !cache_.empty() && query_obj_->get_id() == lite_api::liteServer_runSmcMethod::ID; + use_cache_ = use_cache(); if (use_cache_) { cache_key_ = td::sha256_bits256(query_); td::actor::send_closure( @@ -173,6 +173,22 @@ void LiteQuery::start_up() { } } +bool LiteQuery::use_cache() { + if (cache_.empty()) { + return false; + } + bool use = false; + lite_api::downcast_call( + *query_obj_, + td::overloaded( + [&](lite_api::liteServer_runSmcMethod& q) { + // wc=-1, seqno=-1 means "use latest mc block" + use = q.id_->workchain_ != masterchainId || q.id_->seqno_ != -1; + }, + [&](auto& obj) { use = false; })); + return use; +} + void LiteQuery::perform() { td::actor::send_closure(manager_, &ValidatorManager::add_lite_query_stats, query_obj_->get_id()); lite_api::downcast_call( @@ -1245,8 +1261,24 @@ void LiteQuery::finish_getAccountState(td::BufferSlice shard_proof) { return; } if (mode_ & 0x10000) { - finish_runSmcMethod(std::move(shard_proof), proof.move_as_ok(), std::move(acc_root), sstate.gen_utime, - sstate.gen_lt); + if (!mc_state_.is_null()) { + finish_runSmcMethod(std::move(shard_proof), proof.move_as_ok(), std::move(acc_root), sstate.gen_utime, + sstate.gen_lt); + return; + } + shard_proof_ = std::move(shard_proof); + proof_ = proof.move_as_ok(); + set_continuation( + [&, base_blk_id = base_blk_id_, acc_root, utime = sstate.gen_utime, lt = sstate.gen_lt]() mutable -> void { + base_blk_id_ = base_blk_id; // It gets overridden by request_mc_block_data_state + finish_runSmcMethod(std::move(shard_proof_), std::move(proof_), std::move(acc_root), utime, lt); + }); + td::optional master_ref = state_->get_master_ref(); + if (!master_ref) { + fatal_error("masterchain ref block is not available"); + return; + } + request_mc_block_data_state(master_ref.value()); return; } td::BufferSlice data; diff --git a/validator/impl/liteserver.hpp b/validator/impl/liteserver.hpp index 57ec7c3ce..145fff3da 100644 --- a/validator/impl/liteserver.hpp +++ b/validator/impl/liteserver.hpp @@ -62,7 +62,7 @@ class LiteQuery : public td::actor::Actor { td::BufferSlice buffer_; std::function continuation_; bool cont_set_{false}; - td::BufferSlice shard_proof_; + td::BufferSlice shard_proof_, proof_; std::vector> roots_; std::vector> aux_objs_; std::vector blk_ids_; @@ -98,6 +98,7 @@ class LiteQuery : public td::actor::Actor { bool finish_query(td::BufferSlice result, bool skip_cache_update = false); void alarm() override; void start_up() override; + bool use_cache(); void perform(); void perform_getTime(); void perform_getVersion(); diff --git a/validator/impl/shard.cpp b/validator/impl/shard.cpp index a96f1a819..e899926a0 100644 --- a/validator/impl/shard.cpp +++ b/validator/impl/shard.cpp @@ -129,6 +129,15 @@ td::Status ShardStateQ::init() { " contains BlockId " + hdr_id.to_str() + " different from the one originally required"); } + if (info.r1.master_ref.write().fetch_long(1)) { + BlockIdExt mc_id; + if (!block::tlb::t_ExtBlkRef.unpack(info.r1.master_ref, mc_id, nullptr)) { + return td::Status::Error(-668, "cannot unpack master_ref in shardchain state of "s + blkid.to_str()); + } + master_ref = mc_id; + } else { + master_ref = {}; + } return td::Status::OK(); } diff --git a/validator/impl/shard.hpp b/validator/impl/shard.hpp index 1c511ab5b..99a9e8b08 100644 --- a/validator/impl/shard.hpp +++ b/validator/impl/shard.hpp @@ -41,6 +41,7 @@ class ShardStateQ : virtual public ShardState { bool before_split_{false}; bool fake_split_{false}; bool fake_merge_{false}; + td::optional master_ref; protected: friend class Ref; @@ -80,6 +81,9 @@ class ShardStateQ : virtual public ShardState { LogicalTime get_logical_time() const override { return lt; } + td::optional get_master_ref() const override { + return master_ref; + } td::Status validate_deep() const override; ShardStateQ* make_copy() const override; td::Result> message_queue() const override; diff --git a/validator/interfaces/shard.h b/validator/interfaces/shard.h index 4a0e42fe1..35fe4bc9a 100644 --- a/validator/interfaces/shard.h +++ b/validator/interfaces/shard.h @@ -44,6 +44,7 @@ class ShardState : public td::CntObject { virtual BlockIdExt get_block_id() const = 0; virtual RootHash root_hash() const = 0; virtual td::Ref root_cell() const = 0; + virtual td::optional get_master_ref() const = 0; virtual td::Status validate_deep() const = 0;