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 all commits
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
36 changes: 35 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 @@ -398,6 +419,19 @@ mod tests {
let func_abi = r.resolve_struct(&m, s.as_ident_str()).unwrap();
println!("{}", serde_json::to_string_pretty(&func_abi).unwrap());
}

// test resolve module function index
{
let m = ModuleId::new(genesis_address(), Identifier::new("Dao").unwrap());
let f = r.resolve_module_function_index(&m, 0).unwrap();
assert_eq!(f.name(), "cast_vote");
}

// test resolve module function index overflow
{
let m = ModuleId::new(genesis_address(), Identifier::new("Dao").unwrap());
assert!(r.resolve_module_function_index(&m, 31).is_err())
}
}

#[test]
Expand All @@ -417,7 +451,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
Loading