Skip to content

Commit

Permalink
Revive read-tickets WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon committed Jul 5, 2023
1 parent 8b423d0 commit 4b51de4
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 34 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions soroban-env-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ k256 = {version = "0.13.1", features=["ecdsa", "arithmetic"]}
# is needed to build the host for wasm (a rare but supported config).
getrandom = { version = "0.2", features=["js"] }
sha3 = "0.10.8"
bs = "0.1.0"

[dev-dependencies]
env_logger = "0.9.0"
Expand Down
10 changes: 9 additions & 1 deletion soroban-env-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
err,
events::{diagnostic::DiagnosticLevel, Events, InternalEventsBuffer},
expiration_ledger_bumps::{ExpirationLedgerBumps, LedgerBump},
host_object::{HostMap, HostObject, HostObjectType, HostVec},
host_object::{HostMap, HostObject, HostObjectType, HostVec, Ticket},
impl_bignum_host_fns_rhs_u32, impl_wrapping_obj_from_num, impl_wrapping_obj_to_num,
num::*,
storage::{InstanceStorageMap, Storage},
Expand Down Expand Up @@ -94,6 +94,7 @@ pub(crate) struct HostImpl {
source_account: RefCell<Option<AccountId>>,
ledger: RefCell<Option<LedgerInfo>>,
pub(crate) objects: RefCell<Vec<HostObject>>,
pub(crate) ticket: RefCell<Ticket>,
storage: RefCell<Storage>,
pub(crate) context: RefCell<Vec<Context>>,
// Note: budget is refcounted and is _not_ deep-cloned when you call HostImpl::deep_clone,
Expand Down Expand Up @@ -164,6 +165,12 @@ impl_checked_borrow_helpers!(
try_borrow_objects,
try_borrow_objects_mut
);
impl_checked_borrow_helpers!(
ticket,
Ticket,
try_borrow_ticket,
try_borrow_ticket_mut
);
impl_checked_borrow_helpers!(storage, Storage, try_borrow_storage, try_borrow_storage_mut);
impl_checked_borrow_helpers!(
context,
Expand Down Expand Up @@ -234,6 +241,7 @@ impl Host {
source_account: RefCell::new(None),
ledger: RefCell::new(None),
objects: Default::default(),
ticket: Default::default(),
storage: RefCell::new(storage),
context: Default::default(),
budget,
Expand Down
29 changes: 28 additions & 1 deletion soroban-env-host/src/host/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
err,
storage::{InstanceStorageMap, StorageMap},
xdr::{ContractCostType, ContractExecutable, Hash, HostFunction, HostFunctionType, ScVal},
Error, Host, HostError, Symbol, SymbolStr, TryFromVal, TryIntoVal, Val,
Error, Host, HostError, Symbol, SymbolStr, TryFromVal, TryIntoVal, Val, host_object::Ticket,
};

#[cfg(any(test, feature = "testutils"))]
Expand Down Expand Up @@ -89,6 +89,7 @@ pub(crate) struct Context {
pub(crate) frame: Frame,
prng: Option<Prng>,
pub(crate) storage: Option<InstanceStorageMap>,
ticket: Ticket
}

/// Holds contextual information about a single invocation, either
Expand Down Expand Up @@ -126,10 +127,12 @@ impl Host {
auth_manager.push_frame(self, &frame)?;
auth_snapshot = Some(auth_manager.snapshot());
}
let ticket = self.try_borrow_ticket_mut()?.bump()?;
let ctx = Context {
frame,
prng: None,
storage: None,
ticket
};
self.try_borrow_context_mut()?.push(ctx);
Ok(RollbackPoint {
Expand Down Expand Up @@ -242,6 +245,30 @@ impl Host {
}
}

pub(super) fn with_current_context<F, U>(&self, f: F) -> Result<U, HostError>
where
F: FnOnce(&Context) -> Result<U, HostError>,
{
let Ok(context_guard) = self.0.context.try_borrow() else {
return Err(self.err(ScErrorType::Context, ScErrorCode::InternalError, "context is already borrowed", &[]));
};
if let Some(context) = context_guard.last() {
f(context)
} else {
drop(context_guard);
Err(self.err(
ScErrorType::Context,
ScErrorCode::MissingValue,
"no contract running",
&[],
))
}
}

pub(crate) fn get_current_ticket(&self) -> Result<Ticket, HostError> {
self.with_current_context(|ctx| Ok(ctx.ticket))
}

/// Same as [`Self::with_current_frame`] but passes `None` when there is no current
/// frame, rather than logging an error.
pub(crate) fn with_current_frame_opt<F, U>(&self, f: F) -> Result<U, HostError>
Expand Down
Loading

0 comments on commit 4b51de4

Please sign in to comment.