-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get_current_call_stack method (#574)
* Add get_current_call_stack method * update tests * add example
- Loading branch information
Showing
5 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "example_call_stack" | ||
version = "0.0.4" | ||
authors = ["Stellar Development Foundation <[email protected]>"] | ||
license = "Apache-2.0" | ||
edition = "2021" | ||
publish = false | ||
rust-version = "1.63" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
doctest = false | ||
|
||
[dependencies] | ||
soroban-sdk = {path = "../../soroban-sdk"} | ||
|
||
[dev-dependencies] | ||
soroban-sdk = {path = "../../soroban-sdk", features = ["testutils"]} | ||
|
||
[features] | ||
testutils = ["soroban-sdk/testutils"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#![no_std] | ||
use soroban_sdk::{contractimpl, symbol, BytesN, Env}; | ||
|
||
pub struct OuterContract; | ||
|
||
#[contractimpl] | ||
impl OuterContract { | ||
pub fn outer(env: Env, contract_id: BytesN<32>) { | ||
let check_call_stack = || { | ||
let stack = env.get_current_call_stack(); | ||
assert_eq!(stack.len(), 1); | ||
let outer = stack.get(0).unwrap().unwrap(); | ||
assert_eq!(outer.0, BytesN::from_array(&env, &[1u8; 32])); | ||
assert_eq!(outer.1, symbol!("outer")); | ||
}; | ||
|
||
// Check before the inner call | ||
check_call_stack(); | ||
|
||
let client = InnerContractClient::new(&env, &contract_id); | ||
client.inner(); | ||
|
||
// Check after the inner call | ||
check_call_stack(); | ||
} | ||
} | ||
|
||
pub struct InnerContract; | ||
|
||
#[contractimpl] | ||
impl InnerContract { | ||
pub fn inner(env: Env) { | ||
let stack = env.get_current_call_stack(); | ||
assert_eq!(stack.len(), 2); | ||
|
||
let outer = stack.get(0).unwrap().unwrap(); | ||
assert_eq!(outer.0, BytesN::from_array(&env, &[1u8; 32])); | ||
assert_eq!(outer.1, symbol!("outer")); | ||
|
||
let inner = stack.get(1).unwrap().unwrap(); | ||
assert_eq!(inner.0, BytesN::from_array(&env, &[0u8; 32])); | ||
assert_eq!(inner.1, symbol!("inner")); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use soroban_sdk::{BytesN, Env}; | ||
|
||
use crate::{InnerContract, OuterContract, OuterContractClient}; | ||
|
||
#[test] | ||
fn test() { | ||
let e = Env::default(); | ||
|
||
let inner_contract_id = BytesN::from_array(&e, &[0; 32]); | ||
e.register_contract(&inner_contract_id, InnerContract); | ||
|
||
let contract_id = BytesN::from_array(&e, &[1; 32]); | ||
e.register_contract(&contract_id, OuterContract); | ||
let client = OuterContractClient::new(&e, &contract_id); | ||
|
||
client.outer(&inner_contract_id); | ||
} | ||
} |