Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rpc] add RPC API contract.resolve_module_function_index #3464

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[rpc] add RPC API contract.resolve_module_function_index
Related #3450.
  • Loading branch information
coldnight committed Jun 14, 2022
commit 172a6aef6132a94ada90276335bd368c332e5a27
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