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

Fix testutils invoke with incorrect arg count #1336

Merged
merged 2 commits into from
Sep 16, 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
5 changes: 5 additions & 0 deletions soroban-sdk-macros/src/derive_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub fn derive_pub_fn(
quote! {}
};
let slice_args: Vec<TokenStream2> = (0..wrap_args.len()).map(|n| quote! { args[#n] }).collect();
let arg_count = slice_args.len();
let use_trait = if let Some(t) = trait_ident {
quote! { use super::#t }
} else {
Expand Down Expand Up @@ -145,11 +146,15 @@ pub fn derive_pub_fn(
)
}

#[cfg(any(test, feature = "testutils"))]
#[deprecated(note = #deprecated_note)]
pub fn invoke_raw_slice(
env: #crate_path::Env,
args: &[#crate_path::Val],
) -> #crate_path::Val {
if args.len() != #arg_count {
panic!("invalid number of input arguments: {} expected, got {}", #arg_count, args.len());
}
#[allow(deprecated)]
invoke_raw(env, #(#slice_args),*)
}
Expand Down
1 change: 1 addition & 0 deletions soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod contract_docs;
mod contract_duration;
mod contract_fn;
mod contract_invoke;
mod contract_invoke_arg_count;
mod contract_overlapping_type_fn_names;
mod contract_snapshot;
mod contract_store;
Expand Down
73 changes: 73 additions & 0 deletions soroban-sdk/src/tests/contract_invoke_arg_count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate as soroban_sdk;
use soroban_sdk::{contract, contractimpl, symbol_short, vec, Address, Env, IntoVal as _};

#[contract]
pub struct Contract;

#[contractimpl]
impl Contract {
pub fn add_with(env: Env, x: i32, y: Option<i32>, z: Option<i32>, contract_id: Address) -> i32 {
let mut args = vec![&env, x.into_val(&env)];
if let Some(y) = y {
args.push_back(y.into_val(&env));
}
if let Some(z) = z {
args.push_back(z.into_val(&env));
}
env.invoke_contract(&contract_id, &symbol_short!("add"), args)
}
}

#[contract]
pub struct AddContract;

#[contractimpl]
impl AddContract {
pub fn add(_: Env, a: i32, b: i32) -> i32 {
a + b
}
}

#[test]
fn test_correct_arg_count() {
let e = Env::default();

let add_contract_id = e.register_contract(None, AddContract);

let contract_id = e.register_contract(None, Contract);
let client = ContractClient::new(&e, &contract_id);

let x = 10i32;
let y = 12i32;
let z = client.add_with(&x, &Some(y), &None, &add_contract_id);
assert!(z == 22);
}

#[test]
#[should_panic(expected = "invalid number of input arguments: 2 expected, got 1")]
fn test_too_few_args() {
let e = Env::default();

let add_contract_id = e.register_contract(None, AddContract);

let contract_id = e.register_contract(None, Contract);
let client = ContractClient::new(&e, &contract_id);

let x = 10i32;
let _ = client.add_with(&x, &None, &None, &add_contract_id);
}

#[test]
#[should_panic(expected = "invalid number of input arguments: 2 expected, got 3")]
fn test_too_many_args() {
let e = Env::default();

let add_contract_id = e.register_contract(None, AddContract);

let contract_id = e.register_contract(None, Contract);
let client = ContractClient::new(&e, &contract_id);

let x = 10i32;
let y = 12i32;
let _ = client.add_with(&x, &Some(y), &Some(1), &add_contract_id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
{
"generators": {
"address": 2,
"nonce": 0
},
"auth": [
[]
],
"ledger": {
"protocol_version": 21,
"sequence_number": 0,
"timestamp": 0,
"network_id": "0000000000000000000000000000000000000000000000000000000000000000",
"base_reserve": 0,
"min_persistent_entry_ttl": 4096,
"min_temp_entry_ttl": 16,
"max_entry_ttl": 6312000,
"ledger_entries": [
[
{
"contract_data": {
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": "ledger_key_contract_instance",
"durability": "persistent"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": "ledger_key_contract_instance",
"durability": "persistent",
"val": {
"contract_instance": {
"executable": {
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"storage": null
}
}
}
},
"ext": "v0"
},
4095
]
],
[
{
"contract_data": {
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
"key": "ledger_key_contract_instance",
"durability": "persistent"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
"key": "ledger_key_contract_instance",
"durability": "persistent",
"val": {
"contract_instance": {
"executable": {
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"storage": null
}
}
}
},
"ext": "v0"
},
4095
]
],
[
{
"contract_code": {
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_code": {
"ext": "v0",
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"code": ""
}
},
"ext": "v0"
},
4095
]
]
]
},
"events": [
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000002"
},
{
"symbol": "add_with"
}
],
"data": {
"vec": [
{
"i32": 10
},
{
"i32": 12
},
"void",
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM"
}
]
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000002",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000001"
},
{
"symbol": "add"
}
],
"data": {
"vec": [
{
"i32": 10
},
{
"i32": 12
}
]
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "add"
}
],
"data": {
"i32": 22
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000002",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "add_with"
}
],
"data": {
"i32": 22
}
}
}
},
"failed_call": false
}
]
}
Loading
Loading