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

Overhaul "free budget" and migrate non-metered code #1123

Merged
merged 21 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
03e5889
Add internal mode to `Budget` and route diagnostics to it
jayz22 Oct 22, 2023
61f1b30
Refactoring: split `budget.rs` into several files
jayz22 Oct 22, 2023
7b3e2f8
Tighten up metering for diagnostic workflows
jayz22 Oct 24, 2023
d25fce6
fixup! Tighten up metering for diagnostic workflows
jayz22 Oct 24, 2023
007ab33
Remove _non_metered code and add `with_internal_mode_fallible`
jayz22 Oct 24, 2023
95551ab
fixup! Remove _non_metered code and add `with_internal_mode_fallible`
jayz22 Oct 24, 2023
2f7c56d
Merge branch 'main' of https://github.com/stellar/rs-soroban-env into…
jayz22 Oct 24, 2023
4fba12a
Add a test for linear memory logging
jayz22 Oct 25, 2023
ad45dc6
fixup! Add a test for linear memory logging
jayz22 Oct 25, 2023
6ffed01
Some fixes, addressing review comments
jayz22 Oct 25, 2023
0c996e5
fixup! Some fixes, addressing review comments
jayz22 Oct 25, 2023
c7b5ee1
fixup! Some fixes, addressing review comments
jayz22 Oct 25, 2023
00068d0
Rename
jayz22 Oct 25, 2023
c187c89
Add a public shadow budget setter
jayz22 Oct 25, 2023
a8466e3
Some further cleanup on internal event
jayz22 Oct 26, 2023
5c5530d
Cleanup `get_contract_id`
jayz22 Oct 26, 2023
2ad537e
Clean up `with_shadow_mode`
jayz22 Oct 26, 2023
8539550
Merge branch 'main' of https://github.com/stellar/rs-soroban-env into…
jayz22 Oct 26, 2023
1635e6e
Tighten up debug vs shadow mode check for internal event
jayz22 Oct 26, 2023
6104fcf
Merge branch 'main' of https://github.com/stellar/rs-soroban-env into…
jayz22 Oct 26, 2023
e0a2d11
Simplify get_current_contract_id_opt_internal
graydon Oct 26, 2023
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
111 changes: 42 additions & 69 deletions soroban-env-host/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,27 +412,6 @@ impl AuthorizedFunction {
}
}
}

// metering: free
#[cfg(any(test, feature = "recording_auth"))]
fn to_xdr_non_metered(&self, host: &Host) -> Result<xdr::SorobanAuthorizedFunction, HostError> {
match self {
AuthorizedFunction::ContractFn(contract_fn) => {
Ok(SorobanAuthorizedFunction::ContractFn(InvokeContractArgs {
contract_address: host
.visit_obj(contract_fn.contract_address, |addr: &ScAddress| {
Ok(addr.clone())
})?,
function_name: contract_fn.function_name.try_into_val(host)?,
// why is this intentionally non-metered? the visit_obj above is metered.
args: host.vals_to_scval_vec_non_metered(contract_fn.args.as_slice())?,
}))
}
AuthorizedFunction::CreateContractHostFn(create_contract_args) => Ok(
SorobanAuthorizedFunction::CreateContractHostFn(create_contract_args.clone()),
),
}
}
}

impl AuthorizedInvocation {
Expand Down Expand Up @@ -476,36 +455,19 @@ impl AuthorizedInvocation {
}

// metering: covered
fn to_xdr(&self, host: &Host) -> Result<xdr::SorobanAuthorizedInvocation, HostError> {
Ok(xdr::SorobanAuthorizedInvocation {
function: self.function.to_xdr(host)?,
sub_invocations: self
.sub_invocations
.iter()
.map(|i| i.to_xdr(host))
.metered_collect::<Result<Vec<xdr::SorobanAuthorizedInvocation>, HostError>>(host)??
.try_into()
.map_err(|_| HostError::from((ScErrorType::Auth, ScErrorCode::InternalError)))?,
})
}

// Non-metered conversion should only be used for the recording preflight
// runs or testing.
// metering: free
#[cfg(any(test, feature = "recording_auth"))]
fn to_xdr_non_metered(
fn to_xdr(
&self,
host: &Host,
exhausted_sub_invocations_only: bool,
) -> Result<xdr::SorobanAuthorizedInvocation, HostError> {
Ok(xdr::SorobanAuthorizedInvocation {
function: self.function.to_xdr_non_metered(host)?,
function: self.function.to_xdr(host)?,
sub_invocations: self
.sub_invocations
.iter()
.filter(|i| i.is_exhausted || !exhausted_sub_invocations_only)
.map(|i| i.to_xdr_non_metered(host, exhausted_sub_invocations_only))
.collect::<Result<Vec<xdr::SorobanAuthorizedInvocation>, HostError>>()?
.map(|i| i.to_xdr(host, exhausted_sub_invocations_only))
.metered_collect::<Result<Vec<xdr::SorobanAuthorizedInvocation>, HostError>>(host)??
.try_into()
.map_err(|_| HostError::from((ScErrorType::Auth, ScErrorCode::InternalError)))?,
})
Expand Down Expand Up @@ -1195,21 +1157,30 @@ impl AuthorizationManager {
&self,
host: &Host,
) -> Vec<(ScAddress, xdr::SorobanAuthorizedInvocation)> {
self.account_trackers
.borrow()
.iter()
.filter(|t| t.borrow().verified)
.map(|t| {
(
host.scaddress_from_address(t.borrow().address).unwrap(),
t.borrow()
.invocation_tracker
.root_authorized_invocation
.to_xdr_non_metered(host, true)
.unwrap(),
)
})
.collect()
let inv: Option<Vec<(ScAddress, xdr::SorobanAuthorizedInvocation)>> =
host.as_budget().with_shadow_mode(
|| {
let inv = self
.account_trackers
.borrow()
.iter()
.filter(|t| t.borrow().verified)
.map(|t| {
(
host.scaddress_from_address(t.borrow().address).unwrap(),
t.borrow()
.invocation_tracker
.root_authorized_invocation
.to_xdr(host, true)
.unwrap(),
)
})
.metered_collect(host)?;
Ok(Some(inv))
},
|| None,
);
inv.unwrap()
}
}

Expand Down Expand Up @@ -1571,17 +1542,19 @@ impl AccountAuthorizationTracker {
// metering: free for recording
#[cfg(any(test, feature = "recording_auth"))]
fn get_recorded_auth_payload(&self, host: &Host) -> Result<RecordedAuthPayload, HostError> {
Ok(RecordedAuthPayload {
address: if !self.is_invoker {
Some(host.visit_obj(self.address, |a: &ScAddress| Ok(a.clone()))?)
} else {
None
},
invocation: self
.invocation_tracker
.root_authorized_invocation
.to_xdr_non_metered(host, false)?,
nonce: self.nonce.map(|(nonce, _)| nonce),
host.as_budget().with_shadow_mode_fallible(|| {
Ok(RecordedAuthPayload {
address: if !self.is_invoker {
Some(host.visit_obj(self.address, |a: &ScAddress| a.metered_clone(host))?)
} else {
None
},
invocation: self
.invocation_tracker
.root_authorized_invocation
.to_xdr(host, false)?,
nonce: self.nonce.map(|(nonce, _)| nonce),
})
})
}

Expand All @@ -1600,7 +1573,7 @@ impl AccountAuthorizationTracker {
) -> Result<xdr::SorobanAuthorizedInvocation, HostError> {
self.invocation_tracker
.root_authorized_invocation
.to_xdr(host)
.to_xdr(host, false)
}

// metering: covered
Expand Down
Loading