Skip to content

Commit

Permalink
[rpc] add RPC API contract.resolve_module_function_index
Browse files Browse the repository at this point in the history
Related #3450.
  • Loading branch information
coldnight committed Jun 14, 2022
1 parent 2d55f96 commit 172a6ae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
23 changes: 22 additions & 1 deletion abi/resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@ impl<'a> ABIResolver<'a> {
}
})
}

pub fn resolve_module_function_index(
&self,
module_id: &ModuleId,
function_idx: u16,
) -> Result<FunctionABI> {
let module = self
.resolver
.get_module(module_id.address(), module_id.name())?;
if function_idx as usize >= module.function_defs.len() {
return Err(anyhow!(
"Function index {} out of range in {:?}",
function_idx,
module.self_id(),
));
}
let function_def = module.function_def_at(FunctionDefinitionIndex::new(function_idx));
let (func_name, func) = Function::new(&module, function_def);
self.function_to_abi(module_id, &func_name, &func)
}

pub fn resolve_function(
&self,
module_id: &ModuleId,
Expand Down Expand Up @@ -417,7 +438,7 @@ mod tests {
let test_source = r#"
module {{sender}}::TestModule {
struct A has copy, store{
}
}
struct B has key{
a: vector<A>,
}
Expand Down
6 changes: 6 additions & 0 deletions rpc/api/src/contract_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub trait ContractApi {

#[rpc(name = "contract.resolve_function")]
fn resolve_function(&self, function_id: FunctionIdView) -> FutureResult<FunctionABI>;
#[rpc(name = "contract.resolve_module_function_index")]
fn resolve_module_function_index(
&self,
module_id: ModuleIdView,
function_index: u16,
) -> FutureResult<FunctionABI>;
#[rpc(name = "contract.resolve_struct")]
fn resolve_struct(&self, struct_tag: StructTagView) -> FutureResult<StructInstantiation>;
#[rpc(name = "contract.resolve_module")]
Expand Down
15 changes: 15 additions & 0 deletions rpc/server/src/module/contract_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ where
Box::pin(fut.boxed())
}

fn resolve_module_function_index(
&self,
module_id: ModuleIdView,
function_idx: u16,
) -> FutureResult<FunctionABI> {
let service = self.chain_state.clone();
let storage = self.storage.clone();
let fut = async move {
let state = ChainStateDB::new(storage, Some(service.state_root().await?));
ABIResolver::new(&state).resolve_module_function_index(&module_id.0, function_idx)
}
.map_err(map_err);
Box::pin(fut.boxed())
}

fn resolve_struct(&self, struct_tag: StructTagView) -> FutureResult<StructInstantiation> {
let service = self.chain_state.clone();
let storage = self.storage.clone();
Expand Down

0 comments on commit 172a6ae

Please sign in to comment.