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

[x/programs] Improve external program function call DX #659

Merged
merged 3 commits into from
Jan 8, 2024
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
3 changes: 1 addition & 2 deletions x/programs/examples/imports/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ func (i *Import) Register(link *host.Link) error {
// callProgramFn makes a call to an entry function of a program in the context of another program's ID.
func (i *Import) callProgramFn(
wasmCaller *wasmtime.Caller,
callerID int64,
programID int64,
maxUnits int64,
function int64,
args int64,
maxUnits int64,
) int64 {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
18 changes: 6 additions & 12 deletions x/programs/rust/examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,9 @@ fn inc(program: Program, to: Address, amount: i64) -> bool {

/// Increments the count at the address by the amount for an external program.
#[public]
fn inc_external(
program: Program,
target: Program,
max_units: i64,
of: Address,
amount: i64,
) -> i64 {
program
.call_program(&target, max_units, "inc", params!(&of, &amount))
fn inc_external(_: Program, target: Program, max_units: i64, of: Address, amount: i64) -> i64 {
target
.call_function("inc", params!(&of, &amount), max_units)
.unwrap()
}

Expand All @@ -63,8 +57,8 @@ fn get_value(program: Program, of: Address) -> i64 {

/// Gets the count at the address for an external program.
#[public]
fn get_value_external(program: Program, target: Program, max_units: i64, of: Address) -> i64 {
program
.call_program(&target, max_units, "get_value", params!(&of))
fn get_value_external(_: Program, target: Program, max_units: i64, of: Address) -> i64 {
target
.call_function("get_value", params!(&of), max_units)
.unwrap()
}
16 changes: 4 additions & 12 deletions x/programs/rust/wasmlanche_sdk/src/host/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,19 @@ use crate::program::Program;
#[link(wasm_import_module = "program")]
extern "C" {
#[link_name = "call_program"]
fn _call_program(
caller_id: i64,
target_id: i64,
max_units: i64,
function: i64,
args_ptr: i64,
) -> i64;
fn _call_program(target_id: i64, function: i64, args_ptr: i64, max_units: i64) -> i64;
}

/// Calls another program `target` and returns the result.
/// Calls a program `target` and returns the result.
pub(crate) fn call(
caller: &Program,
target: &Program,
max_units: i64,
function_name: &str,
args: &[u8],
max_units: i64,
) -> Result<i64, StateError> {
let caller = to_smart_ptr(caller.id())?;
let target = to_smart_ptr(target.id())?;
let function = to_smart_ptr(function_name.as_bytes())?;
let args = to_smart_ptr(args)?;

Ok(unsafe { _call_program(caller, target, max_units, function, args) })
Ok(unsafe { _call_program(target, function, args, max_units) })
}
10 changes: 5 additions & 5 deletions x/programs/rust/wasmlanche_sdk/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ impl Program {
State::new(Program::new(*self.id()))
}

/// Attempts to call another program `target` from this program `caller`.
/// Attempts to call a function `name` with `args` on the given program. This method
/// is used to call functions on external programs.
/// # Errors
/// Returns a `StateError` if the call fails.
/// # Safety
/// The caller must ensure that `function_name` + `args` point to valid memory locations.
pub fn call_program(
pub fn call_function(
&self,
target: &Program,
max_units: i64,
function_name: &str,
args: Vec<Vec<u8>>,
max_units: i64,
) -> Result<i64, StateError> {
// flatten the args into a single byte vector
let args = args.into_iter().flatten().collect::<Vec<u8>>();
call_program(self, target, max_units, function_name, &args)
call_program(self, function_name, &args, max_units)
}
}

Expand Down
Binary file modified x/programs/tests/fixture/counter.wasm
Binary file not shown.
Loading