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

Snapshot all auths during a test not just last invokes #1168

Merged
merged 8 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 5 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 = "2c49d79b24032e1bda66ca20362de49b230cef60"
rev = "8e593abadd9d3723810c644af01e1124badca244"

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

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

[workspace.dependencies.stellar-strkey]
version = "0.0.8"
Expand Down
58 changes: 39 additions & 19 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct MaybeEnv {
#[cfg(any(test, feature = "testutils"))]
generators: Option<Rc<RefCell<Generators>>>,
#[cfg(any(test, feature = "testutils"))]
auth_snapshot: Option<Rc<RefCell<AuthSnapshot>>>,
#[cfg(any(test, feature = "testutils"))]
snapshot: Option<Rc<LedgerSnapshot>>,
}

Expand Down Expand Up @@ -181,6 +183,8 @@ impl MaybeEnv {
generators: None,
#[cfg(any(test, feature = "testutils"))]
snapshot: None,
#[cfg(any(test, feature = "testutils"))]
auth_snapshot: None,
}
}
}
Expand Down Expand Up @@ -210,6 +214,8 @@ impl TryFrom<MaybeEnv> for Env {
generators: value.generators.unwrap_or_default(),
#[cfg(any(test, feature = "testutils"))]
snapshot: value.snapshot,
#[cfg(any(test, feature = "testutils"))]
auth_snapshot: value.auth_snapshot.unwrap_or_default(),
})
} else {
Err(ConversionError)
Expand All @@ -226,6 +232,8 @@ impl From<Env> for MaybeEnv {
generators: Some(value.generators.clone()),
#[cfg(any(test, feature = "testutils"))]
snapshot: value.snapshot.clone(),
#[cfg(any(test, feature = "testutils"))]
auth_snapshot: Some(value.auth_snapshot.clone()),
}
}
}
Expand All @@ -244,6 +252,8 @@ pub struct Env {
#[cfg(any(test, feature = "testutils"))]
generators: Rc<RefCell<Generators>>,
#[cfg(any(test, feature = "testutils"))]
auth_snapshot: Rc<RefCell<AuthSnapshot>>,
#[cfg(any(test, feature = "testutils"))]
snapshot: Option<Rc<LedgerSnapshot>>,
}

Expand Down Expand Up @@ -430,17 +440,19 @@ impl Env {
}

#[cfg(any(test, feature = "testutils"))]
use crate::auth;
#[cfg(any(test, feature = "testutils"))]
use crate::testutils::{
budget::Budget, Address as _, AuthSnapshot, AuthorizedInvocation, ContractFunctionSet,
EventsSnapshot, Generators, Ledger as _, MockAuth, MockAuthContract, Snapshot,
use crate::{
auth,
testutils::{
budget::Budget, Address as _, AuthSnapshot, AuthorizedInvocation, ContractFunctionSet,
EventsSnapshot, Generators, Ledger as _, MockAuth, MockAuthContract, Snapshot,
},
Bytes, BytesN,
};
#[cfg(any(test, feature = "testutils"))]
use crate::{Bytes, BytesN};
#[cfg(any(test, feature = "testutils"))]
use core::{cell::RefCell, cell::RefMut};
#[cfg(any(test, feature = "testutils"))]
use internal::ContractInvocationEvent;
#[cfg(any(test, feature = "testutils"))]
use soroban_ledger_snapshot::LedgerSnapshot;
#[cfg(any(test, feature = "testutils"))]
use std::{path::Path, rc::Rc};
Expand Down Expand Up @@ -515,10 +527,27 @@ impl Env {
.set_diagnostic_level(internal::DiagnosticLevel::Debug)
.unwrap();
env_impl.set_base_prng_seed([0; 32]).unwrap();

let auth_snapshot = Rc::new(RefCell::new(AuthSnapshot::default()));
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.
leighmcculloch marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or_default();
(*auth_snapshot_in_hook).borrow_mut().0.extend(new_auths);
}
})))
.unwrap();

let env = Env {
env_impl,
generators: generators.unwrap_or_default(),
snapshot,
auth_snapshot,
};

env.ledger().set(ledger_info);
Expand Down Expand Up @@ -1216,7 +1245,7 @@ impl Env {
pub fn to_snapshot(&self) -> Snapshot {
Snapshot {
generators: (*self.generators).borrow().clone(),
auth: self.to_auth_snapshot(),
auth: (*self.auth_snapshot).borrow().clone(),
ledger: self.to_ledger_snapshot(),
events: self.to_events_snapshot(),
}
Expand Down Expand Up @@ -1288,17 +1317,6 @@ impl Env {
)
}

/// Create an auth snapshot from the Env's current state.
pub(crate) fn to_auth_snapshot(&self) -> AuthSnapshot {
AuthSnapshot(
self.env_impl
.get_authenticated_authorizations()
// If an error occurs getting the authenticated authorizations
// it means that no auth has occurred.
.unwrap_or_default(),
)
}

/// Get the budget that tracks the resources consumed for the environment.
pub fn budget(&self) -> Budget {
Budget::new(self.env_impl.budget_cloned())
Expand Down Expand Up @@ -1413,6 +1431,8 @@ impl Env {
generators: Default::default(),
#[cfg(any(test, feature = "testutils"))]
snapshot: None,
#[cfg(any(test, feature = "testutils"))]
auth_snapshot: Default::default(),
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion soroban-sdk/src/tests/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ fn test_budget() {
let b = client.add();
e.budget().print();

assert_eq!(e.budget().tracker(ContractCostType::VisitObject), (4, None));
assert_eq!(
e.budget().tracker(ContractCostType::VisitObject),
(13, None)
);
assert_eq!(b, map![&e, (1, 10), (2, 20)]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,54 @@
"address": 5,
"nonce": 0
},
"auth": [],
"auth": [
[
"GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEGWF",
{
"function": {
"contract_fn": {
"contract_address": "CBEPDNVYXQGWB5YUBXKJWYJA7OXTZW5LFLNO5JRRGE6Z6C5OSUZPCCEL",
"function_name": "set_admin",
"args": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM"
}
]
}
},
"sub_invocations": []
}
],
[
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4",
{
"function": {
"contract_fn": {
"contract_address": "CBEPDNVYXQGWB5YUBXKJWYJA7OXTZW5LFLNO5JRRGE6Z6C5OSUZPCCEL",
"function_name": "approve",
"args": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4"
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK3IM"
},
{
"i128": {
"hi": 0,
"lo": 20
}
},
{
"u32": 200
}
]
}
},
"sub_invocations": []
}
]
],
"ledger": {
"protocol_version": 20,
"sequence_number": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,54 @@
"address": 5,
"nonce": 1
},
"auth": [],
"auth": [
[
"GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEGWF",
{
"function": {
"contract_fn": {
"contract_address": "CBEPDNVYXQGWB5YUBXKJWYJA7OXTZW5LFLNO5JRRGE6Z6C5OSUZPCCEL",
"function_name": "set_admin",
"args": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM"
}
]
}
},
"sub_invocations": []
}
],
[
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4",
{
"function": {
"contract_fn": {
"contract_address": "CBEPDNVYXQGWB5YUBXKJWYJA7OXTZW5LFLNO5JRRGE6Z6C5OSUZPCCEL",
"function_name": "approve",
"args": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4"
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK3IM"
},
{
"i128": {
"hi": 0,
"lo": 20
}
},
{
"u32": 200
}
]
}
},
"sub_invocations": []
}
]
],
"ledger": {
"protocol_version": 20,
"sequence_number": 0,
Expand Down