Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Mar 14, 2024
1 parent bee53cd commit ca58f2c
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 168 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ We also provide some built-in functions:
* `add/sub/mul/div(e1, e2)`: addition/subtraction/multiplication/division of two integer/float numbers. If one of the arguments is float32/float64, the result is float64; otherwise, the result is integer. You can use type annotation to get the integer part of the float number. For example `div((mul(div(1, 3.0), 1000) : nat), 100.0)` returns `3.33`.

The following functions are only available in non-offline mode:
* `read_state([effective_canister_id,] prefix, id, paths)`: fetch the state tree path of `<prefix>/<id>/<paths>`. Some useful examples,
* `read_state([effective_id,] prefix, id, paths)`: fetch the state tree path of `<prefix>/<id>/<paths>`. Some useful examples,
+ candid metadata: `read_state("canister", principal "canister_id", "metadata/candid:service")`
+ list all subnet ids: `read_state("subnet")`
+ subnet ranges: `read_state("subnet", principal "subnet_id", "canister_ranges")`
Expand Down
46 changes: 20 additions & 26 deletions src/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use super::helper::{find_init_args, MyHelper, OfflineOutput};
use super::selector::{project, Selector};
use super::token::{ParserError, Tokenizer};
use super::utils::{
args_to_value, as_u32, cast_type, fetch_state_tree_path, get_effective_canister_id, get_field,
resolve_path, str_to_principal,
args_to_value, as_u32, cast_type, get_effective_canister_id, get_field, resolve_path,
str_to_principal,
};
use anyhow::{anyhow, Context, Result};
use candid::{
Expand Down Expand Up @@ -123,31 +123,25 @@ impl Exp {
}
_ => return Err(anyhow!("neuron_account expects (principal, nonce)")),
},
"read_state" if helper.offline.is_none() => match args.as_slice() {
[IDLValue::Text(prefix), IDLValue::Principal(id), IDLValue::Text(path)] => {
let effective = if matches!(prefix.as_str(), "subnet") {
// This is a hack. Should be removed after boundary nodes can route subnet ids
Some(Principal::from_text("ryjl3-tyaaa-aaaaa-aaaba-cai")?)
} else {
None
};
fetch_state_tree_path(&helper.agent, prefix, Some(*id), path, effective)?
}
[IDLValue::Principal(effective), IDLValue::Text(prefix), IDLValue::Principal(id), IDLValue::Text(path)] => {
fetch_state_tree_path(&helper.agent, prefix, Some(*id), path, Some(*effective))?
}
[IDLValue::Principal(effective), IDLValue::Text(prefix)] => {
fetch_state_tree_path(&helper.agent, prefix, None, "", Some(*effective))?
}
[IDLValue::Text(prefix)] => {
fetch_state_tree_path(&helper.agent, prefix, None, "", Some(Principal::from_text("ryjl3-tyaaa-aaaaa-aaaba-cai")?))?
}
_ => {
return Err(anyhow!(
"state_tree_path expects ([effective_canister_id,] prefix, principal, path)"
))
"read_state" if helper.offline.is_none() => {
use crate::utils::{fetch_state_path, parse_state_path};
match args.as_slice() {
[IDLValue::Text(_), ..] => {
let path = parse_state_path(args.as_slice())?;
fetch_state_path(&helper.agent, path)?
}
[IDLValue::Principal(effective), IDLValue::Text(_), ..] => {
let mut path = parse_state_path(&args[1..])?;
path.effective_id = Some(*effective);
fetch_state_path(&helper.agent, path)?
}
_ => {
return Err(anyhow!(
"read_state expects ([effective_id,] prefix, principal, path)"
))
}
}
},
}
"file" => match args.as_slice() {
[IDLValue::Text(file)] => {
let path = resolve_path(&helper.base_path, file);
Expand Down
22 changes: 7 additions & 15 deletions src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::exp::Exp;
use crate::token::{Token, Tokenizer};
use crate::utils::{fetch_state_tree_path_raw, random_value, str_to_principal};
use crate::utils::{fetch_metadata, random_value, str_to_principal};
use candid::{
types::value::{IDLField, IDLValue, VariantValue},
types::{Function, Label, Type, TypeInner},
Expand Down Expand Up @@ -472,20 +472,12 @@ impl Validator for MyHelper {

#[tokio::main]
async fn fetch_actor(agent: &Agent, canister_id: Principal) -> anyhow::Result<CanisterInfo> {
let response = fetch_state_tree_path_raw(
agent,
"canister",
Some(canister_id),
"metadata/candid:service",
None,
)
.await;
let profiling =
fetch_state_tree_path_raw(agent, "canister", Some(canister_id), "metadata/name", None)
.await
.ok()
.as_ref()
.and_then(|bytes| Decode!(bytes, BTreeMap<u16, String>).ok());
let response = fetch_metadata(agent, canister_id, "metadata/candid:service").await;
let profiling = fetch_metadata(agent, canister_id, "metadata/name")
.await
.ok()
.as_ref()
.and_then(|bytes| Decode!(bytes, BTreeMap<u16, String>).ok());
let candid = match response {
Ok(blob) => std::str::from_utf8(&blob)?.to_owned(),
Err(_) => {
Expand Down
Loading

0 comments on commit ca58f2c

Please sign in to comment.