Skip to content

Commit

Permalink
Add GetLibraries and custom liteserver opts
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker-volodya committed Oct 3, 2024
1 parent 408686a commit 4ede2cd
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 43 deletions.
23 changes: 1 addition & 22 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 cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ hex = "0.4.3"
ureq = "2.4.0"
regex = "1"
ton_liteapi = { path = "../liteapi" }
ton_networkconfig = { path = "../network-config", features = ["adnl"] }
ton_networkconfig = { path = "../network-config" }
rand = "0.8.5"
tokio = { version = "1.36", features = ["full"] }
8 changes: 6 additions & 2 deletions cli/src/arg_parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ fn parse_account_base64(s: &str) -> std::result::Result<AccountId, Box<dyn Error

fn parse_account_raw(s: &str) -> std::result::Result<AccountId, Box<dyn Error>> {
let (workchain, account) = s.split_once(":").ok_or_else(|| format!("can't parse {}: wrong address format, must be <workchain>:<account>", s))?;
let workchain = workchain.parse::<i32>().map_err(|e| format!("wrong workchain {}", workchain))?;
let id = account.parse::<Int256>().map_err(|e| format!("wrong account id {}", account))?;
let workchain = workchain.parse::<i32>().map_err(|_e| format!("wrong workchain {}", workchain))?;
let id = account.parse::<Int256>().map_err(|_e| format!("wrong account id {}", account))?;
Ok(AccountId { workchain, id })
}

pub fn parse_account_id(s: &str) -> std::result::Result<AccountId, String> {
parse_account_base64(s).or_else(|e| parse_account_raw(s).map_err(|e2| format!("Can't parse account as base64 ({}) or as raw ({}))", e, e2)))
}

pub fn parse_key(s: &str) -> std::result::Result<[u8; 32], Box<dyn Error + Send + Sync>> {
Ok(base64::decode(s).or_else(|_e| hex::decode(s)).map_err(|_e| "can't parse key")?.as_slice().try_into()?)
}
37 changes: 29 additions & 8 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use ton_networkconfig::ConfigGlobal;
use std::error::Error;
use std::fs::{read_to_string, File};
use std::io::{stdin, Read};
use std::net::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::{Duration, UNIX_EPOCH};

use crate::arg_parsers::{parse_account_id, parse_block_id_ext};
use crate::arg_parsers::{parse_account_id, parse_block_id_ext, parse_key};

type Result<T> = std::result::Result<T, Box<dyn Error>>;

Expand All @@ -27,6 +28,12 @@ struct Args {
/// Use testnet config, if not provided use mainnet config
#[clap(short, long, parse(from_flag), group = "config-group")]
testnet: bool,
/// Liteserver address (IP:PORT)
#[clap(long, group = "config-group")]
address: Option<SocketAddr>,
/// Liteserver public key (hex-encoded)
#[clap(long, value_parser = parse_key, requires = "address")]
public_key: Option<[u8; 32]>,
#[clap(subcommand)]
command: Commands,
}
Expand Down Expand Up @@ -215,21 +222,31 @@ enum Commands {
start_after: Option<Int256>,
modified_after: Option<u32>,
},
GetLibraries {
library_list: Vec<Int256>,
},
}

#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let args = Args::parse();
let config_json = if let Some(config) = args.config {
read_to_string(config)?

let client = if let (Some(address), Some(public_key)) = (&args.address, &args.public_key) {
LiteClient::connect(address, public_key).await?
} else {
download_config(args.testnet).await?
let config_json = if let Some(config) = args.config {
read_to_string(config)?
} else {
download_config(args.testnet).await?
};
let config: ConfigGlobal = ConfigGlobal::from_str(&config_json)?;
let ls = config.liteservers.choose(&mut rand::thread_rng()).unwrap();
let public_key: [u8; 32] = ls.id.clone().into();
LiteClient::connect(ls.socket_addr(), public_key).await?
};
let config: ConfigGlobal = ConfigGlobal::from_str(&config_json)?;
let ls = config.liteservers.choose(&mut rand::thread_rng()).unwrap();
let public_key: [u8; 32] = ls.id.clone().into();
let mut client = LiteClient::connect(ls.socket_addr(), public_key).await?;

let mut client = client;

if let Err(e) = execute_command(&mut client, &args.command).await {
println!("[ERROR] {:?}", e);
Expand Down Expand Up @@ -393,6 +410,10 @@ async fn execute_command(client: &mut LiteClient, command: &Commands) -> Result<
).await?;
println!("{:#?}", result);
}
Commands::GetLibraries { library_list } => {
let result = client.get_libraries(library_list.clone()).await?;
println!("{:#?}", result);
}
};
Ok(())
}
Expand Down
7 changes: 7 additions & 0 deletions liteapi/src/tl/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub struct TransactionId3 {
// #[tl(boxed, id = "liteServer.signature", scheme_inline = r##"liteServer.signature node_id_short:int256 signature:bytes = liteServer.Signature;"##)]
pub struct Signature {
pub node_id_short: Int256,
#[derivative(Debug(format_with = "fmt_bytes"))]
pub signature: Vec<u8>,
}

Expand Down Expand Up @@ -150,8 +151,11 @@ pub enum BlockLink {
to_key_block: bool,
from: BlockIdExt,
to: BlockIdExt,
#[derivative(Debug(format_with = "fmt_bytes"))]
dest_proof: Vec<u8>,
#[derivative(Debug(format_with = "fmt_bytes"))]
proof: Vec<u8>,
#[derivative(Debug(format_with = "fmt_bytes"))]
state_proof: Vec<u8>,
},
/// liteServer.blockLinkForward to_key_block:Bool from:tonNode.blockIdExt to:tonNode.blockIdExt dest_proof:bytes config_proof:bytes signatures:liteServer.SignatureSet = liteServer.BlockLink;
Expand All @@ -160,7 +164,9 @@ pub enum BlockLink {
to_key_block: bool,
from: BlockIdExt,
to: BlockIdExt,
#[derivative(Debug(format_with = "fmt_bytes"))]
dest_proof: Vec<u8>,
#[derivative(Debug(format_with = "fmt_bytes"))]
config_proof: Vec<u8>,
signatures: SignatureSet,
},
Expand Down Expand Up @@ -196,5 +202,6 @@ pub struct TransactionId {
#[derivative(Debug, Clone, PartialEq)]
pub struct LibraryEntry {
pub hash: Int256,
#[derivative(Debug(format_with = "fmt_bytes"))]
pub data: Vec<u8>,
}
7 changes: 1 addition & 6 deletions network-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ edition = "2021"
[dependencies]
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
serde_with = { version = "1.12.0", features = ["base64"] }
adnl = { version = "0.1.0", optional = true }

[features]
default = []
adnl = ["dep:adnl"]
serde_with = { version = "1.12.0", features = ["base64"] }
4 changes: 0 additions & 4 deletions network-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ use serde::{Deserialize, Serialize};
use std::net::{Ipv4Addr, SocketAddrV4};
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
#[cfg(feature = "dalek")]
use x25519_dalek::PublicKey;
#[cfg(feature = "adnl")]
use adnl::AdnlPublicKey;

#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down

0 comments on commit 4ede2cd

Please sign in to comment.