Skip to content

Commit

Permalink
work on GetConfigParams
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker-volodya committed Jun 25, 2024
1 parent 24a4570 commit 9e3bedc
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 39 deletions.
74 changes: 37 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
65 changes: 64 additions & 1 deletion src/liteserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand Down Expand Up @@ -383,6 +383,66 @@ impl LiteServer {
})
}

fn make_state_proof(state_root: Cell, with_accounts: bool, with_prev_blocks: bool) -> Result<MerkleProof> {
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<ConfigInfo> {
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")),
Expand Down Expand Up @@ -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)),
}
}
Expand Down

0 comments on commit 9e3bedc

Please sign in to comment.