Skip to content

Commit

Permalink
[cmd] state list command decode resource at local with remove state…
Browse files Browse the repository at this point in the history
… reader. (#3200)
  • Loading branch information
jolestar authored Feb 10, 2022
1 parent 031cea9 commit afffc4d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cmd/starcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ network-api = {path = "../../network/api", package="network-api"}
starcoin-network-rpc-api = {path = "../../network-rpc/api"}
short-hex-str = { git = "https://github.com/starcoinorg/diem", rev="94cad072661257a7d55713d6a6df81638a9580ae" }
starcoin-abi-decoder = {path = "../../abi/decoder"}
starcoin-abi-resolver = {path = "../../abi/resolver"}
starcoin-abi-types = {path = "../../abi/types"}
move-command-line-common = {git = "https://github.com/starcoinorg/diem", rev="94cad072661257a7d55713d6a6df81638a9580ae" }

Expand Down
69 changes: 63 additions & 6 deletions cmd/starcoin/src/state/list_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ use crate::StarcoinOpt;
use anyhow::Result;
use scmd::{CommandAction, ExecContext};
use serde::{Serialize, Serializer};
use starcoin_abi_resolver::ABIResolver;
use starcoin_rpc_api::types::{ListCodeView, ListResourceView};
use starcoin_rpc_client::StateRootOption;
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::language_storage::ModuleId;
use starcoin_vm_types::state_view::StateView;
use structopt::StructOpt;

/// List state data command
Expand Down Expand Up @@ -80,11 +84,16 @@ impl CommandAction for ListCmd {
.map(|block_view| block_view.header.state_root),
None => None,
};
ListDataResult::Code(
let state_reader = ctx.state().client().state_reader(
state_root.map_or(StateRootOption::Latest, StateRootOption::BlockHash),
)?;
ListDataResult::Code(resolve_code(
&state_reader,
*address,
ctx.state()
.client()
.state_list_code(*address, true, state_root)?,
)
.state_list_code(*address, false, state_root)?,
))
}
ListDataOpt::Resource {
address,
Expand All @@ -98,13 +107,61 @@ impl CommandAction for ListCmd {
.map(|block_view| block_view.header.state_root),
None => None,
};
ListDataResult::Resource(
let state_reader = ctx.state().client().state_reader(
state_root.map_or(StateRootOption::Latest, StateRootOption::BlockHash),
)?;
ListDataResult::Resource(decode_resource(
&state_reader,
ctx.state()
.client()
.state_list_resource(*address, true, state_root)?,
)
.state_list_resource(*address, false, state_root)?,
))
}
};
Ok(result)
}
}

fn decode_resource(state_view: &dyn StateView, mut list: ListResourceView) -> ListResourceView {
list.resources
.iter_mut()
.for_each(|(tag_view, resource_view)| {
match starcoin_dev::playground::view_resource(
state_view,
tag_view.0.clone(),
resource_view.raw.0.as_slice(),
) {
Err(e) => {
eprintln!(
"Warn: decode resource {:?} failed, error:{:?}, hex:{}",
tag_view.0, e, resource_view.raw
);
}
Ok(decoded) => resource_view.json = Some(decoded.into()),
}
});
list
}

fn resolve_code(
state_view: &dyn StateView,
address: AccountAddress,
mut list: ListCodeView,
) -> ListCodeView {
let resolver = ABIResolver::new(state_view);
list.codes.iter_mut().for_each(|(id, code_view)| {
let module_id = ModuleId::new(address, id.clone());
match resolver.resolve_module(&module_id) {
Err(e) => {
eprintln!(
"Warn: resolve module {:?} failed, error:{:?}, hex:{}",
module_id, e, code_view.code
);
}
Ok(abi) => {
code_view.abi = Some(abi);
}
}
});
list
}

0 comments on commit afffc4d

Please sign in to comment.