From 9e3bedcb9fdf4d3b8bd0d1e6c1defa99ba79d697 Mon Sep 17 00:00:00 2001 From: Vladimir Lebedev Date: Tue, 25 Jun 2024 17:29:17 +0700 Subject: [PATCH] work on GetConfigParams --- Cargo.lock | 74 +++++++++++++++++++++++------------------------ Cargo.toml | 2 +- src/liteserver.rs | 65 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 102 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index af5c17e..adcdb0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,7 +117,7 @@ dependencies = [ "argh_shared", "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -137,7 +137,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -246,7 +246,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -257,9 +257,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "block-buffer" @@ -344,9 +344,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.99" +version = "1.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" +checksum = "c891175c3fb232128f48de6590095e59198bbeb8620c310be349bfc3afd12c7b" dependencies = [ "jobserver", "libc", @@ -559,7 +559,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -929,7 +929,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -1288,9 +1288,9 @@ dependencies = [ [[package]] name = "lazy_static" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "lazycell" @@ -1306,9 +1306,9 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libloading" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" +checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" dependencies = [ "cfg-if", "windows-targets 0.52.5", @@ -1810,7 +1810,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -1841,7 +1841,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -1887,14 +1887,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" dependencies = [ "proc-macro2", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] name = "proc-macro2" -version = "1.0.85" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] @@ -1905,7 +1905,7 @@ version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "731e0d9356b0c25f16f33b5be79b1c57b562f141ebfcdb0ad8ac2c13a24293b4" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "hex", "lazy_static", "procfs-core", @@ -1918,7 +1918,7 @@ version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d3554923a69f4ce04c4a754260c338f505ce22642d3830e049a399fc2059a29" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "hex", ] @@ -2048,7 +2048,7 @@ version = "11.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e29830cbb1290e404f24c73af91c5d8d631ce7e128691e9477556b540cd01ecd" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] @@ -2057,7 +2057,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] @@ -2165,7 +2165,7 @@ version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "errno", "libc", "linux-raw-sys", @@ -2254,7 +2254,7 @@ checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -2415,9 +2415,9 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" [[package]] name = "subtle" -version = "2.5.0" +version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "subtle-ng" @@ -2438,9 +2438,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.66" +version = "2.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" +checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" dependencies = [ "proc-macro2", "quote", @@ -2490,7 +2490,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -2612,7 +2612,7 @@ dependencies = [ "proc-macro2", "quote", "rustc-hash", - "syn 2.0.66", + "syn 2.0.68", "tl-scheme", ] @@ -2656,7 +2656,7 @@ checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -2829,7 +2829,7 @@ dependencies = [ [[package]] name = "ton_liteapi" version = "0.1.0" -source = "git+https://github.com/tonstack/lite-client?rev=86a7cfa#86a7cfa5228fd967e308be256a392f5a65ecd441" +source = "git+https://github.com/tonstack/lite-client?branch=async#8289d25280eb8bbdf855f054ff9126e16b4f3015" dependencies = [ "adnl", "derivative", @@ -2917,7 +2917,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -3192,7 +3192,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", "wasm-bindgen-shared", ] @@ -3214,7 +3214,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3464,7 +3464,7 @@ checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] @@ -3484,7 +3484,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.66", + "syn 2.0.68", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 33877f3..eb87dcd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ futures-util = "0.3" axum = "0.7.2" ton_block = { git = "https://github.com/broxus/ton-labs-block.git", features = ["ton"] } prometheus = { version = "0.13.3", features = ["process"] } -ton_liteapi = { git = "https://github.com/tonstack/lite-client", rev = "86a7cfa" } +ton_liteapi = { git = "https://github.com/tonstack/lite-client", branch = "async" } ton_types = { git = "https://github.com/broxus/ton-labs-types.git" } tower = { version = "0.4.13" } hex = "0.4" diff --git a/src/liteserver.rs b/src/liteserver.rs index 2b0f0bc..8c9f4aa 100644 --- a/src/liteserver.rs +++ b/src/liteserver.rs @@ -11,7 +11,7 @@ use ton_liteapi::{ tl::{ common::{AccountId, BlockIdExt, Int256, ZeroStateIdExt}, request::{ - GetAccountState, GetAllShardsInfo, GetBlock, GetBlockHeader, GetConfigAll, GetMasterchainInfoExt, GetTransactions, ListBlockTransactions, LookupBlock, Request, WaitMasterchainSeqno, WrappedRequest + GetAccountState, GetAllShardsInfo, GetBlock, GetBlockHeader, GetConfigAll, GetConfigParams, GetMasterchainInfoExt, GetTransactions, ListBlockTransactions, LookupBlock, Request, WaitMasterchainSeqno, WrappedRequest }, response::{AccountState, AllShardsInfo, BlockData, BlockHeader, BlockTransactions, ConfigInfo, MasterchainInfo, MasterchainInfoExt, Response, TransactionId, TransactionList}, }, @@ -383,6 +383,66 @@ impl LiteServer { }) } + fn make_state_proof(state_root: Cell, with_accounts: bool, with_prev_blocks: bool) -> Result { + let usage_tree = UsageTree::with_root(state_root.clone()); + let state = ShardStateUnsplit::construct_from_cell(usage_tree.root_cell())?; + if with_accounts { + state.read_accounts()?; + } + let custom = state.read_custom()?.ok_or(anyhow!("no custom data in state"))?; + let mut counter = 0; + let _ = custom.prev_blocks.iterate_ext(true, None, |_, _| { counter += 1; Ok(counter < 16) })?; + Ok(MerkleProof::create_by_usage_tree(&state_root, usage_tree)?) + } + + pub async fn get_config_params(&self, req: GetConfigParams) -> Result { + if req.id.workchain != -1 { + return Err(anyhow!("requested block is not in masterchain")) + } + let block_root = self.load_block_by_tonlabs_id(&req.id.as_tonlabs()?).await?.ok_or(anyhow!("no such block in db"))?; + let block = Block::construct_from_cell(block_root.clone())?; + if req.extract_from_key_block.is_some() { + let key_seqno = block.read_info()?.prev_key_block_seqno(); + let key_id = self.search_mc_block_by_seqno(key_seqno).await?.ok_or(anyhow!("no such key block in masterchain state"))?; + let key_root = self.load_block_by_tonlabs_id(&key_id).await?.ok_or(anyhow!("no such key block in db"))?; + let usage_tree = UsageTree::with_root(key_root.clone()); + let key_block = Block::construct_from_cell(usage_tree.root_cell())?; + let config = key_block.read_extra()?.read_custom()?.ok_or(anyhow!("no custom data in key block"))?.config().ok_or(anyhow!("no config in key block"))?; + Ok(ConfigInfo { + mode: (), + id: key_id.as_liteapi(), + state_proof: Vec::new(), + config_proof: todo!(), + with_state_root: req.with_state_root, + with_libraries: req.with_libraries, + with_state_extra_root: req.with_state_extra_root, + with_shard_hashes: req.with_shard_hashes, + with_accounts_root: req.with_accounts_root, + with_prev_blocks: req.with_prev_blocks, + extract_from_key_block: req.extract_from_key_block, + }) + } else { + let state_root = self.load_state(&req.id).await?.root_cell().clone(); + let usage_tree = UsageTree::with_root(state_root.clone()); + let state = ShardStateUnsplit::construct_from_cell(usage_tree.root_cell())?; + let config = state.read_custom()?.ok_or(anyhow!("no custom data in state"))?.config(); + Ok(ConfigInfo { + mode: (), + id: req.id, + state_proof: Self::make_block_proof(block_root, true, false, false)?.write_to_bytes()?, + config_proof: Self::make_state_proof(state_root, req.with_accounts_root.is_some(), req.with_prev_blocks.is_some())?.write_to_bytes()?, + with_state_root: req.with_state_root, + with_libraries: req.with_libraries, + with_state_extra_root: req.with_state_extra_root, + with_shard_hashes: req.with_shard_hashes, + with_accounts_root: req.with_accounts_root, + with_prev_blocks: req.with_accounts_root, + extract_from_key_block: req.extract_from_key_block, + }) + } + + } + pub async fn wait_masterchain_seqno(&self, req: WaitMasterchainSeqno) -> Result<()> { tokio::select! { _ = tokio::time::sleep(Duration::from_millis(req.timeout_ms as u64)) => Err(anyhow!("Timeout")), @@ -432,6 +492,9 @@ impl LiteServer { Request::LookupBlock(req) => Ok(Response::BlockHeader( self.lookup_block(req).await?, )), + Request::GetConfigParams(req) => Ok(Response::ConfigInfo( + self.get_config_params(req).await?, + )), _ => Err(anyhow!("unimplemented method: {:?}", req.request)), } }