Skip to content

Commit

Permalink
Restore previous auth state in client (#1174)
Browse files Browse the repository at this point in the history
### What
Restore previous auth state in client after client finishes invoke.

### Why
The client is presented in such a way that it looks like invocations
with a client that has been configured with certain auth will apply the
auth only during invocations with it.

It's unintuitive that the auths from the client apply forever after the
invocation.

Close #1084

Dependent on:
- stellar/rs-soroban-env#1244
  • Loading branch information
leighmcculloch authored Nov 22, 2023
1 parent 8a6ea7e commit 8c5eba9
Show file tree
Hide file tree
Showing 80 changed files with 819 additions and 598 deletions.
18 changes: 13 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ soroban-token-sdk = { version = "20.0.0-rc2", path = "soroban-token-sdk" }
[workspace.dependencies.soroban-env-common]
version = "20.0.0-rc2"
git = "https://github.com/stellar/rs-soroban-env"
rev = "8e593abadd9d3723810c644af01e1124badca244"
rev = "c26e8a9224c004e3931ae15cc2e29a4facf3a856"

[workspace.dependencies.soroban-env-guest]
version = "20.0.0-rc2"
git = "https://github.com/stellar/rs-soroban-env"
rev = "8e593abadd9d3723810c644af01e1124badca244"
rev = "c26e8a9224c004e3931ae15cc2e29a4facf3a856"

[workspace.dependencies.soroban-env-host]
version = "20.0.0-rc2"
git = "https://github.com/stellar/rs-soroban-env"
rev = "8e593abadd9d3723810c644af01e1124badca244"
rev = "c26e8a9224c004e3931ae15cc2e29a4facf3a856"

[workspace.dependencies.stellar-strkey]
version = "0.0.8"
Expand Down
34 changes: 24 additions & 10 deletions soroban-sdk-macros/src/derive_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ pub fn derive_client_impl(crate_path: &Path, name: &str, fns: &[syn_ext::Fn]) ->
quote! {
#(#fn_attrs)*
pub fn #fn_ident(&self, #(#fn_input_types),*) -> #fn_output {
// TODO: Undo the mock and restore previous auth state after
// https://github.com/stellar/rs-soroban-env/issues/785 is
// implemented.
use core::ops::Not;
#[cfg(any(test, feature = "testutils"))]
let old_auth_manager = self.env.in_contract().not().then(||
self.env.host().snapshot_auth_manager().unwrap()
);
#[cfg(any(test, feature = "testutils"))]
{
if let Some(set_auths) = self.set_auths {
Expand All @@ -201,19 +203,26 @@ pub fn derive_client_impl(crate_path: &Path, name: &str, fns: &[syn_ext::Fn]) ->
}
}
use #crate_path::{IntoVal,FromVal};
self.env.invoke_contract(
let res = self.env.invoke_contract(
&self.address,
&#crate_path::Symbol::new(&self.env, &#fn_name),
#crate_path::vec![&self.env, #(#fn_input_names.into_val(&self.env)),*],
)
);
#[cfg(any(test, feature = "testutils"))]
if let Some(old_auth_manager) = old_auth_manager {
self.env.host().set_auth_manager(old_auth_manager).unwrap();
}
res
}

#(#fn_attrs)*
pub fn #fn_try_ident(&self, #(#fn_input_types),*) -> #fn_try_output {
#[cfg(any(test, feature = "testutils"))]
// TODO: Undo the mock and restore previous auth state after
// https://github.com/stellar/rs-soroban-env/issues/785 is
// implemented.
use core::ops::Not;
#[cfg(any(test, feature = "testutils"))]
let old_auth_manager = self.env.in_contract().not().then(||
self.env.host().snapshot_auth_manager().unwrap()
);
#[cfg(any(test, feature = "testutils"))]
{
if let Some(set_auths) = self.set_auths {
Expand All @@ -227,11 +236,16 @@ pub fn derive_client_impl(crate_path: &Path, name: &str, fns: &[syn_ext::Fn]) ->
}
}
use #crate_path::{IntoVal,FromVal};
self.env.try_invoke_contract(
let res = self.env.try_invoke_contract(
&self.address,
&#crate_path::Symbol::new(&self.env, &#fn_name),
#crate_path::vec![&self.env, #(#fn_input_names.into_val(&self.env)),*],
)
);
#[cfg(any(test, feature = "testutils"))]
if let Some(old_auth_manager) = old_auth_manager {
self.env.host().set_auth_manager(old_auth_manager).unwrap();
}
res
}
}
})
Expand Down
52 changes: 27 additions & 25 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,11 @@ use xdr::{
#[cfg(any(test, feature = "testutils"))]
#[cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))]
impl Env {
#[doc(hidden)]
pub fn in_contract(&self) -> bool {
self.env_impl.has_frame().unwrap()
}

#[doc(hidden)]
pub fn host(&self) -> &internal::Host {
&self.env_impl
Expand Down Expand Up @@ -532,13 +537,16 @@ impl Env {
let auth_snapshot_in_hook = auth_snapshot.clone();
env_impl
.set_top_contract_invocation_hook(Some(Rc::new(move |host, event| {
if let ContractInvocationEvent::Finish = event {
let new_auths = host
.get_authenticated_authorizations()
// If an error occurs getting the authenticated authorizations
// it means that no auth has occurred.
.unwrap_or_default();
(*auth_snapshot_in_hook).borrow_mut().0.extend(new_auths);
match event {
ContractInvocationEvent::Start => {}
ContractInvocationEvent::Finish => {
let new_auths = host
.get_authenticated_authorizations()
// If an error occurs getting the authenticated authorizations
// it means that no auth has occurred.
.unwrap();
(*auth_snapshot_in_hook).borrow_mut().0.push(new_auths);
}
}
})))
.unwrap();
Expand Down Expand Up @@ -600,7 +608,12 @@ impl Env {
env_impl: &internal::EnvImpl,
args: &[Val],
) -> Option<Val> {
let env = Env::with_impl(env_impl.clone());
let env = Env {
env_impl: env_impl.clone(),
generators: Default::default(),
auth_snapshot: Default::default(),
snapshot: None,
};
self.0.call(
crate::Symbol::try_from_val(&env, func)
.unwrap_infallible()
Expand Down Expand Up @@ -1050,8 +1063,12 @@ impl Env {
/// # fn main() { }
/// ```
pub fn auths(&self) -> std::vec::Vec<(Address, AuthorizedInvocation)> {
let authorizations = self.env_impl.get_authenticated_authorizations().unwrap();
authorizations
(*self.auth_snapshot)
.borrow()
.0
.last()
.cloned()
.unwrap_or_default()
.into_iter()
.map(|(sc_addr, invocation)| {
(
Expand Down Expand Up @@ -1422,21 +1439,6 @@ impl Env {
}
}

#[doc(hidden)]
impl Env {
pub fn with_impl(env_impl: internal::EnvImpl) -> Env {
Env {
env_impl,
#[cfg(any(test, feature = "testutils"))]
generators: Default::default(),
#[cfg(any(test, feature = "testutils"))]
auth_snapshot: Default::default(),
#[cfg(any(test, feature = "testutils"))]
snapshot: None,
}
}
}

#[doc(hidden)]
impl internal::EnvBase for Env {
type Error = Infallible;
Expand Down
23 changes: 13 additions & 10 deletions soroban-sdk/src/prng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Prng {
/// # env.prng().seed(Bytes::from_array(&env, &[1; 32]));
/// let mut value: u64 = 0;
/// env.prng().fill(&mut value);
/// assert_eq!(value, 14156542310752927490);
/// assert_eq!(value, 8478755077819529274);
/// # })
/// # }
/// # #[cfg(not(feature = "testutils"))]
Expand All @@ -150,8 +150,9 @@ impl Prng {
/// assert_eq!(
/// value,
/// [
/// 2, 63, 55, 32, 58, 36, 118, 196, 37, 102, 166, 28, 197, 92, 60, 168, 117, 219,
/// 180, 204, 65, 192, 222, 183, 137, 248, 231, 191, 136, 24, 54, 56
/// 58, 248, 248, 38, 210, 150, 170, 117, 122, 110, 9, 101, 244, 57,
/// 221, 102, 164, 48, 43, 104, 222, 229, 242, 29, 25, 148, 88, 204,
/// 130, 148, 2, 66
/// ],
/// );
/// # })
Expand Down Expand Up @@ -190,7 +191,7 @@ impl Prng {
/// # env.as_contract(&contract_id, || {
/// # env.prng().seed(Bytes::from_array(&env, &[1; 32]));
/// let value: u64 = env.prng().gen();
/// assert_eq!(value, 14156542310752927490);
/// assert_eq!(value, 8478755077819529274);
/// # })
/// # }
/// # #[cfg(not(feature = "testutils"))]
Expand All @@ -215,8 +216,9 @@ impl Prng {
/// assert_eq!(
/// value,
/// [
/// 2, 63, 55, 32, 58, 36, 118, 196, 37, 102, 166, 28, 197, 92, 60, 168, 117, 219,
/// 180, 204, 65, 192, 222, 183, 137, 248, 231, 191, 136, 24, 54, 56
/// 58, 248, 248, 38, 210, 150, 170, 117, 122, 110, 9, 101, 244, 57,
/// 221, 102, 164, 48, 43, 104, 222, 229, 242, 29, 25, 148, 88, 204,
/// 130, 148, 2, 66
/// ],
/// );
/// # })
Expand Down Expand Up @@ -263,8 +265,9 @@ impl Prng {
/// assert_eq!(value, Bytes::from_slice(
/// &env,
/// &[
/// 2, 63, 55, 32, 58, 36, 118, 196, 37, 102, 166, 28, 197, 92, 60, 168, 117, 219,
/// 180, 204, 65, 192, 222, 183, 137, 248, 231, 191, 136, 24, 54, 56
/// 58, 248, 248, 38, 210, 150, 170, 117, 122, 110, 9, 101, 244, 57,
/// 221, 102, 164, 48, 43, 104, 222, 229, 242, 29, 25, 148, 88, 204,
/// 130, 148, 2, 66
/// ],
/// ));
/// # })
Expand Down Expand Up @@ -308,7 +311,7 @@ impl Prng {
/// # env.prng().seed(Bytes::from_array(&env, &[1; 32]));
/// // Get a value in the range of 1 to 100, inclusive.
/// let value: u64 = env.prng().gen_range(1..=100);
/// assert_eq!(value, 77);
/// assert_eq!(value, 46);
/// # })
/// # }
/// # #[cfg(not(feature = "testutils"))]
Expand Down Expand Up @@ -348,7 +351,7 @@ impl Prng {
/// # env.prng().seed(Bytes::from_array(&env, &[1; 32]));
/// // Get a value in the range of 1 to 100, inclusive.
/// let value = env.prng().u64_in_range(1..=100);
/// assert_eq!(value, 77);
/// assert_eq!(value, 46);
/// # })
/// # }
/// # #[cfg(not(feature = "testutils"))]
Expand Down
Loading

0 comments on commit 8c5eba9

Please sign in to comment.