From 965749b2bc37d0f309a13d0ab606d6ea3d5f4117 Mon Sep 17 00:00:00 2001 From: ms Date: Sun, 14 Jan 2024 03:30:11 +0100 Subject: [PATCH] add first 16 publishers in lib proof --- tl/generate/scheme/lite_api.tl | 4 +-- tl/generate/scheme/lite_api.tlo | Bin 15296 -> 15368 bytes tonlib/tonlib/TonlibClient.cpp | 2 +- validator/impl/liteserver.cpp | 61 +++++++++++++++++--------------- validator/impl/liteserver.hpp | 4 +-- 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/tl/generate/scheme/lite_api.tl b/tl/generate/scheme/lite_api.tl index 86d6ac549..e1e28b9f6 100644 --- a/tl/generate/scheme/lite_api.tl +++ b/tl/generate/scheme/lite_api.tl @@ -53,7 +53,7 @@ liteServer.partialBlockProof complete:Bool from:tonNode.blockIdExt to:tonNode.bl liteServer.configInfo mode:# id:tonNode.blockIdExt state_proof:bytes config_proof:bytes = liteServer.ConfigInfo; liteServer.validatorStats mode:# id:tonNode.blockIdExt count:int complete:Bool state_proof:bytes data_proof:bytes = liteServer.ValidatorStats; liteServer.libraryResult result:(vector liteServer.libraryEntry) = liteServer.LibraryResult; -liteServer.libraryResultWithProof id:tonNode.blockIdExt result:(vector liteServer.libraryEntry) state_proof:bytes data_proof:bytes = liteServer.LibraryResultWithProof; +liteServer.libraryResultWithProof id:tonNode.blockIdExt mode:# result:(vector liteServer.libraryEntry) state_proof:bytes data_proof:bytes = liteServer.LibraryResultWithProof; liteServer.shardBlockLink id:tonNode.blockIdExt proof:bytes = liteServer.ShardBlockLink; liteServer.shardBlockProof masterchain_id:tonNode.blockIdExt links:(vector liteServer.shardBlockLink) = liteServer.ShardBlockProof; liteServer.lookupBlockResult id:tonNode.blockIdExt mode:# mc_block_id:tonNode.blockIdExt client_mc_state_proof:bytes mc_block_proof:bytes shard_links:(vector liteServer.shardBlockLink) header:bytes prev_header:bytes = liteServer.LookupBlockResult; @@ -86,7 +86,7 @@ liteServer.getConfigAll mode:# id:tonNode.blockIdExt = liteServer.ConfigInfo; liteServer.getConfigParams mode:# id:tonNode.blockIdExt param_list:(vector int) = liteServer.ConfigInfo; liteServer.getValidatorStats#091a58bc mode:# id:tonNode.blockIdExt limit:int start_after:mode.0?int256 modified_after:mode.2?int = liteServer.ValidatorStats; liteServer.getLibraries library_list:(vector int256) = liteServer.LibraryResult; -liteServer.getLibrariesWithProof id:tonNode.blockIdExt library_list:(vector int256) = liteServer.LibraryResultWithProof; +liteServer.getLibrariesWithProof id:tonNode.blockIdExt mode:# library_list:(vector int256) = liteServer.LibraryResultWithProof; liteServer.getShardBlockProof id:tonNode.blockIdExt = liteServer.ShardBlockProof; liteServer.queryPrefix = Object; diff --git a/tl/generate/scheme/lite_api.tlo b/tl/generate/scheme/lite_api.tlo index 723b5f288f0f888ae67858551f4b97b5a475b77c..06c082586eff2976ffc6b0b7cb44300126bffab9 100644 GIT binary patch delta 97 zcmX?5-chkZft7W?`bq)C$!}RCHrul97iIx*CrPS+>B$pBG$tEJsZ8D?VKBKtG5{>N h`HJKmQI@@v%Wh6C(TA!tP=%;FqVE7v!(p(48vw8%B5?o! delta 94 zcmeCEI8eSpft9t+>a~jE return; } - client_.send_query(ton::lite_api::liteServer_getLibrariesWithProof(ton::create_tl_lite_block_id(blkid), std::move(not_cached_hashes)), + client_.send_query(ton::lite_api::liteServer_getLibrariesWithProof(ton::create_tl_lite_block_id(blkid), 0, std::move(not_cached_hashes)), promise.wrap([self=this, blkid, result_entries = std::move(result_entries), not_cached_hashes] (td::Result> r_libraries) mutable -> td::Result> { diff --git a/validator/impl/liteserver.cpp b/validator/impl/liteserver.cpp index ef30b565a..0f8a83604 100644 --- a/validator/impl/liteserver.cpp +++ b/validator/impl/liteserver.cpp @@ -214,7 +214,7 @@ void LiteQuery::start_up() { this->perform_getLibraries(q.library_list_); }, [&](lite_api::liteServer_getLibrariesWithProof& q) { - this->perform_getLibrariesWithProof(ton::create_block_id(q.id_), q.library_list_); + this->perform_getLibrariesWithProof(ton::create_block_id(q.id_), q.mode_, q.library_list_); }, [&](lite_api::liteServer_getShardBlockProof& q) { this->perform_getShardBlockProof(create_block_id(q.id_)); @@ -924,7 +924,7 @@ void LiteQuery::continue_getLibraries(Ref mc_s finish_query(std::move(b)); } -void LiteQuery::perform_getLibrariesWithProof(BlockIdExt blkid, std::vector library_list) { +void LiteQuery::perform_getLibrariesWithProof(BlockIdExt blkid, int mode, std::vector library_list) { LOG(INFO) << "started a getLibrariesWithProof() liteserver query"; if (library_list.size() > 16) { LOG(INFO) << "too many libraries requested, returning only first 16"; @@ -933,11 +933,11 @@ void LiteQuery::perform_getLibrariesWithProof(BlockIdExt blkid, std::vector void { continue_getLibrariesWithProof(library_list); }); + set_continuation([this, library_list, mode]() -> void { continue_getLibrariesWithProof(library_list, mode); }); request_mc_block_data_state(blkid); } -void LiteQuery::continue_getLibrariesWithProof(std::vector library_list) { +void LiteQuery::continue_getLibrariesWithProof(std::vector library_list, int mode) { LOG(INFO) << "obtained masterchain block = " << base_blk_id_.to_str(); CHECK(mc_state_.not_null()); @@ -947,40 +947,45 @@ void LiteQuery::continue_getLibrariesWithProof(std::vector library_ } vm::MerkleProofBuilder pb{mc_state_->root_cell()}; - - auto rconfig = block::ConfigInfo::extract_config(pb.root(), block::ConfigInfo::needLibraries); - if (rconfig.is_error()) { - fatal_error("cannot extract library list block configuration from masterchain state"); - return; - } - auto config = rconfig.move_as_ok(); - - if (false) { - std::ostringstream os; - vm::load_cell_slice(config->get_libraries_root()).print_rec(os); - LOG(INFO) << "\n" << os.str(); - - auto lib_dict = std::make_unique(config->get_libraries_root(), 256); - for (auto k: *lib_dict) { - std::ostringstream oss; - k.second->print_rec(oss); - LOG(INFO) << "library " << k.first.to_hex(256) << ": \n" << oss.str(); - } + block::gen::ShardStateUnsplit::Record state; + if (!tlb::unpack_cell(pb.root(), state)) { + fatal_error("cannot unpack header of shardchain state "s + base_blk_id_.to_str()); } + auto libraries_dict = vm::Dictionary(state.r1.libraries->prefetch_ref(), 256); std::vector> result; for (const auto& hash : library_list) { LOG(INFO) << "looking for library " << hash.to_hex(); - auto libres = config->lookup_library(hash); - if (libres.is_null()) { - LOG(INFO) << "library lookup result is null"; + + auto csr = libraries_dict.lookup(hash.bits(), 256); + if (csr.is_null() || csr->prefetch_ulong(2) != 0 || !csr->have_refs()) { // shared_lib_descr$00 lib:^Cell continue; } - auto data = vm::std_boc_serialize(libres); + block::gen::LibDescr::Record libdescr; + if (!tlb::csr_unpack(csr, libdescr)) { + fatal_error("cannot unpack LibDescr record "s + hash.to_hex()); + return; + } + if (!libdescr.lib->get_hash().bits().equals(hash.bits(), 256)) { + LOG(ERROR) << "public library hash mismatch: expected " << hash.to_hex() << " , found " + << libdescr.lib->get_hash().to_hex(); + continue; + } + + auto data = vm::std_boc_serialize(libdescr.lib); if (data.is_error()) { LOG(WARNING) << "library serialization failed: " << data.move_as_error().to_string(); continue; } + if (mode & 1) { + // include first 16 publishers in the proof + auto publishers_dict = vm::Dictionary(libdescr.publishers, 256); + int publishers_max_count = 16; + auto iter = publishers_dict.begin(); + while (publishers_max_count-- && iter != publishers_dict.end()) { + ++iter; + } + } result.push_back(ton::create_tl_object(hash, data.move_as_ok())); } @@ -999,7 +1004,7 @@ void LiteQuery::continue_getLibrariesWithProof(std::vector library_ return; } - auto b = ton::create_serialize_tl_object(ton::create_tl_lite_block_id(base_blk_id_), std::move(result), + auto b = ton::create_serialize_tl_object(ton::create_tl_lite_block_id(base_blk_id_), mode, std::move(result), state_proof_boc.move_as_ok(), data_proof_boc.move_as_ok()); finish_query(std::move(b)); } diff --git a/validator/impl/liteserver.hpp b/validator/impl/liteserver.hpp index c153e94c9..039888fc4 100644 --- a/validator/impl/liteserver.hpp +++ b/validator/impl/liteserver.hpp @@ -120,8 +120,8 @@ class LiteQuery : public td::actor::Actor { UnixTime gen_utime, LogicalTime gen_lt); void perform_getLibraries(std::vector library_list); void continue_getLibraries(Ref mc_state, BlockIdExt blkid, std::vector library_list); - void perform_getLibrariesWithProof(BlockIdExt blkid, std::vector library_list); - void continue_getLibrariesWithProof(std::vector library_list); + void perform_getLibrariesWithProof(BlockIdExt blkid, int mode, std::vector library_list); + void continue_getLibrariesWithProof(std::vector library_list, int mode); void perform_getOneTransaction(BlockIdExt blkid, WorkchainId workchain, StdSmcAddress addr, LogicalTime lt); void continue_getOneTransaction(); void perform_getTransactions(WorkchainId workchain, StdSmcAddress addr, LogicalTime lt, Bits256 hash, unsigned count);