Skip to content

Commit

Permalink
[rpc][state.list_resource] add resource type filter (#3586)
Browse files Browse the repository at this point in the history
* draft

* save

* clean up

* pass scripts/check_commit.sh

* clean up
  • Loading branch information
jiangying000 authored Jul 30, 2022
1 parent 18a055f commit 3560a61
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/starcoin/src/account/nft_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl CommandAction for NFTCommand {
None,
0,
std::usize::MAX,
None,
)?;
let galleries: Result<Vec<NFTGallery>> = all_resources
.resources
Expand Down Expand Up @@ -128,6 +129,7 @@ impl CommandAction for NFTCommand {
None,
0,
std::usize::MAX,
None,
)?;
let ident_nfts: Result<Vec<IdentifierNFT>> = all_resources
.resources
Expand Down
1 change: 1 addition & 0 deletions cmd/starcoin/src/account/show_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl CommandAction for ShowCommand {
Some(chain_state_reader.state_root()),
0,
std::usize::MAX,
None,
)?;
let balances: HashMap<TokenCode, u128> = resources
.resources
Expand Down
1 change: 1 addition & 0 deletions cmd/starcoin/src/state/list_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl CommandAction for ListCmd {
state_root,
0,
std::usize::MAX,
None,
)?,
))
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/api/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub struct ListResourceOption {
pub state_root: Option<HashValue>,
pub start_index: usize,
pub max_size: usize,
//TODO support filter by type
pub resource_types: Option<Vec<String>>,
}

impl Default for ListResourceOption {
Expand All @@ -124,6 +124,7 @@ impl Default for ListResourceOption {
state_root: None,
start_index: 0,
max_size: std::usize::MAX,
resource_types: None,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions rpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ impl RpcClient {
state_root: Option<HashValue>,
start_index: usize,
max_size: usize,
resource_types: Option<Vec<String>>,
) -> anyhow::Result<ListResourceView> {
self.call_rpc_blocking(|inner| {
inner.state_client.list_resource(
Expand All @@ -594,6 +595,7 @@ impl RpcClient {
state_root,
start_index,
max_size,
resource_types,
}),
)
})
Expand Down
10 changes: 10 additions & 0 deletions rpc/generated_rpc_schema/state.json
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,16 @@
"format": "uint",
"minimum": 0.0
},
"resource_types": {
"default": null,
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
},
"start_index": {
"default": 0,
"type": "integer",
Expand Down
20 changes: 19 additions & 1 deletion rpc/server/src/module/state_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use starcoin_types::{
};
use starcoin_vm_types::identifier::Identifier;
use starcoin_vm_types::language_storage::StructTag;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashSet};
use std::sync::Arc;

pub struct StateRpcImpl<S>
Expand Down Expand Up @@ -260,6 +260,10 @@ where
let statedb = ChainStateDB::new(db, Some(state_root));
//TODO implement list state by iter, and pagination
let state = statedb.get_account_state_set(&addr)?;
let resource_types_set: Option<HashSet<String>> =
option.resource_types.map(|resource_types_value| {
HashSet::from_iter(resource_types_value.iter().cloned())
});
match state {
None => Ok(ListResourceView::default()),
Some(s) => {
Expand All @@ -268,6 +272,20 @@ where
.cloned()
.unwrap_or_default()
.iter()
.filter(|(k, _)| {
if resource_types_set.is_none() {
return true;
}
let struct_tag = StructTag::decode(k.as_slice()).unwrap();
let resource_type_address_module_name_str = format!(
"{}::{}::{}",
struct_tag.address, struct_tag.module, struct_tag.name
);
resource_types_set
.as_ref()
.unwrap()
.contains(&resource_type_address_module_name_str)
})
.skip(option.start_index)
.take(option.max_size)
.map(|(k, v)| {
Expand Down
2 changes: 1 addition & 1 deletion state/statedb/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn test_state_db_dump_iter() -> Result<()> {
assert_eq!(
global_state1.state_sets().len(),
2,
"unexpect state_set length."
"unexpected state_set length."
);
let mut kv1 = HashMap::new();
for item in global_state1.into_inner() {
Expand Down

0 comments on commit 3560a61

Please sign in to comment.