Skip to content

Commit

Permalink
(minimal) Adapt to introduction of Env::Error (#837)
Browse files Browse the repository at this point in the history
This is a "minimal" version of
#833, accompanying /
adapting to stellar/rs-soroban-env#638.

If this minimal approach is preferred, this PR and
stellar/rs-soroban-env#638 should be merged
together. Otherwise the more-invasive pair #833 and
stellar/rs-soroban-env#633 should be merged
together.

This one does much less rearranging and makes no difference to the
emitted code at all. It just pushes around where the non-infallible
`Env::Error` cases get escalated: formerly it happened in `impl Env for
CheckedEnv`, now there's a new internal SDK function
`internal::reject_err` that does the work. Infallible errors also need
to be explicitly unwrapped in a bunch of places but this is just a
type-level thing, operationally it's a no-op.

(I should also note: I could do an "even more minimal" version of this
where `soroban_sdk::env::Env` _does not implement_
`soroban_env_common:env::Env`, in which case the "error rejections"
could be buried in the macro-generated code in the SDK and we could
remove all the `.unwrap_infallible` calls in the SDK bodies. I don't
know how important it is for the SDK's Env to implement the common Env.)
  • Loading branch information
graydon authored Jan 23, 2023
1 parent a68335e commit ca34399
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 127 deletions.
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 @@ -37,17 +37,17 @@ soroban-token-spec = { version = "0.4.3", path = "soroban-token-spec" }
[workspace.dependencies.soroban-env-common]
version = "0.0.12"
git = "https://github.com/stellar/rs-soroban-env"
rev = "d803b0922d1d9eb4d3cfde5e6d574c7776c23388"
rev = "96ea6dcbb2a57208b2ba1dc202490b92d4ba537e"

[workspace.dependencies.soroban-env-guest]
version = "0.0.12"
git = "https://github.com/stellar/rs-soroban-env"
rev = "d803b0922d1d9eb4d3cfde5e6d574c7776c23388"
rev = "96ea6dcbb2a57208b2ba1dc202490b92d4ba537e"

[workspace.dependencies.soroban-env-host]
version = "0.0.12"
git = "https://github.com/stellar/rs-soroban-env"
rev = "d803b0922d1d9eb4d3cfde5e6d574c7776c23388"
rev = "96ea6dcbb2a57208b2ba1dc202490b92d4ba537e"

[workspace.dependencies.stellar-strkey]
version = "0.0.6"
Expand Down
32 changes: 25 additions & 7 deletions soroban-sdk/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::{cmp::Ordering, fmt::Debug};
use crate::{
env::internal::xdr,
env::internal::{Env as _, EnvBase as _, RawVal, RawValConvertible},
unwrap::UnwrapInfallible,
BytesN, ConversionError, Env, Object, TryFromVal,
};

Expand Down Expand Up @@ -65,7 +66,11 @@ impl Accounts {
/// Gets the account for the account ID.
pub fn get(&self, id: &AccountId) -> Option<Account> {
let env = id.env();
if env.account_exists(id.to_object()).is_true() {
if env
.account_exists(id.to_object())
.unwrap_infallible()
.is_true()
{
Some(Account(id.clone()))
} else {
None
Expand Down Expand Up @@ -120,7 +125,10 @@ impl PartialOrd for AccountId {
impl Ord for AccountId {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.env.check_same_env(&other.env);
let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw());
let v = self
.env
.obj_cmp(self.obj.to_raw(), other.obj.to_raw())
.unwrap_infallible();
v.cmp(&0)
}
}
Expand Down Expand Up @@ -328,29 +336,37 @@ impl Account {
/// Returns if the account exists.
pub fn exists(id: &AccountId) -> bool {
let env = id.env();
env.account_exists(id.to_object()).is_true()
env.account_exists(id.to_object())
.unwrap_infallible()
.is_true()
}

/// Returns the low threshold for the Stellar account.
pub fn low_threshold(&self) -> u8 {
let env = self.env();
let val = env.account_get_low_threshold(self.to_object());
let val = env
.account_get_low_threshold(self.to_object())
.unwrap_infallible();
let threshold_u32 = unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) };
threshold_u32 as u8
}

/// Returns the medium threshold for the Stellar account.
pub fn medium_threshold(&self) -> u8 {
let env = self.env();
let val = env.account_get_medium_threshold(self.to_object());
let val = env
.account_get_medium_threshold(self.to_object())
.unwrap_infallible();
let threshold_u32 = unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) };
threshold_u32 as u8
}

/// Returns the high threshold for the Stellar account.
pub fn high_threshold(&self) -> u8 {
let env = self.env();
let val = env.account_get_high_threshold(self.to_object());
let val = env
.account_get_high_threshold(self.to_object())
.unwrap_infallible();
let threshold_u32 = unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) };
threshold_u32 as u8
}
Expand All @@ -359,7 +375,9 @@ impl Account {
/// the signer does not exist for the account, returns zero (`0`).
pub fn signer_weight(&self, signer: &BytesN<32>) -> u8 {
let env = self.env();
let val = env.account_get_signer_weight(self.to_object(), signer.to_object());
let val = env
.account_get_signer_weight(self.to_object(), signer.to_object())
.unwrap_infallible();
let weight_u32 = unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) };
weight_u32 as u8
}
Expand Down
55 changes: 38 additions & 17 deletions soroban-sdk/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{
ConversionError, Env, Object, RawVal, TryFromVal,
};

use crate::unwrap::UnwrapOptimized;
use crate::unwrap::{UnwrapInfallible, UnwrapOptimized};
#[cfg(doc)]
use crate::{storage::Storage, Map, Vec};

Expand Down Expand Up @@ -158,7 +158,10 @@ impl PartialOrd for Bytes {
impl Ord for Bytes {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.env.check_same_env(&other.env);
let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw());
let v = self
.env
.obj_cmp(self.obj.to_raw(), other.obj.to_raw())
.unwrap_infallible();
v.cmp(&0)
}
}
Expand Down Expand Up @@ -307,7 +310,7 @@ impl Bytes {
/// Create an empty Bytes.
#[inline(always)]
pub fn new(env: &Env) -> Bytes {
let obj = env.bytes_new();
let obj = env.bytes_new().unwrap_infallible();
unsafe { Self::unchecked_new(env.clone(), obj) }
}

Expand All @@ -328,7 +331,10 @@ impl Bytes {
#[inline(always)]
pub fn set(&mut self, i: u32, v: u8) {
let v32: u32 = v.into();
self.obj = self.env().bytes_put(self.obj, i.into(), v32.into())
self.obj = self
.env()
.bytes_put(self.obj, i.into(), v32.into())
.unwrap_infallible()
}

#[inline(always)]
Expand All @@ -345,6 +351,7 @@ impl Bytes {
let res = self
.env()
.bytes_get(self.obj, i.into())
.unwrap_infallible()
.try_into()
.unwrap_optimized();
let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) };
Expand All @@ -353,12 +360,15 @@ impl Bytes {

#[inline(always)]
pub fn is_empty(&self) -> bool {
self.env().bytes_len(self.obj).is_u32_zero()
self.env()
.bytes_len(self.obj)
.unwrap_infallible()
.is_u32_zero()
}

#[inline(always)]
pub fn len(&self) -> u32 {
let len = self.env().bytes_len(self.obj);
let len = self.env().bytes_len(self.obj).unwrap_infallible();
unsafe { <_ as RawValConvertible>::unchecked_from_val(len) }
}

Expand All @@ -373,7 +383,7 @@ impl Bytes {

#[inline(always)]
pub fn first_unchecked(&self) -> u8 {
let res = self.env().bytes_front(self.obj);
let res = self.env().bytes_front(self.obj).unwrap_infallible();
let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) };
res32 as u8
}
Expand All @@ -389,7 +399,7 @@ impl Bytes {

#[inline(always)]
pub fn last_unchecked(&self) -> u8 {
let res = self.env().bytes_back(self.obj);
let res = self.env().bytes_back(self.obj).unwrap_infallible();
let res32: u32 = unsafe { <_ as RawValConvertible>::unchecked_from_val(res) };
res32 as u8
}
Expand All @@ -406,26 +416,29 @@ impl Bytes {

#[inline(always)]
pub fn remove_unchecked(&mut self, i: u32) {
self.obj = self.env().bytes_del(self.obj, i.into())
self.obj = self.env().bytes_del(self.obj, i.into()).unwrap_infallible()
}

#[inline(always)]
pub fn push(&mut self, x: u8) {
let x32: u32 = x.into();
self.obj = self.env().bytes_push(self.obj, x32.into())
self.obj = self
.env()
.bytes_push(self.obj, x32.into())
.unwrap_infallible()
}

#[inline(always)]
pub fn pop(&mut self) -> Option<u8> {
let last = self.last()?;
self.obj = self.env().bytes_pop(self.obj);
self.obj = self.env().bytes_pop(self.obj).unwrap_infallible();
Some(last)
}

#[inline(always)]
pub fn pop_unchecked(&mut self) -> u8 {
let last = self.last_unchecked();
self.obj = self.env().bytes_pop(self.obj);
self.obj = self.env().bytes_pop(self.obj).unwrap_infallible();
last
}

Expand All @@ -438,7 +451,10 @@ impl Bytes {
#[inline(always)]
pub fn insert(&mut self, i: u32, b: u8) {
let b32: u32 = b.into();
self.obj = self.env().bytes_insert(self.obj, i.into(), b32.into())
self.obj = self
.env()
.bytes_insert(self.obj, i.into(), b32.into())
.unwrap_infallible()
}

/// Insert the bytes in `bytes` into this [Bytes] starting at position
Expand Down Expand Up @@ -482,7 +498,10 @@ impl Bytes {

#[inline(always)]
pub fn append(&mut self, other: &Bytes) {
self.obj = self.env().bytes_append(self.obj, other.obj)
self.obj = self
.env()
.bytes_append(self.obj, other.obj)
.unwrap_infallible()
}

#[inline(always)]
Expand Down Expand Up @@ -534,7 +553,9 @@ impl Bytes {
Bound::Unbounded => self.len(),
};
let env = self.env();
let bin = env.bytes_slice(self.obj, start_bound.into(), end_bound.into());
let bin = env
.bytes_slice(self.obj, start_bound.into(), end_bound.into())
.unwrap_infallible();
unsafe { Self::unchecked_new(env.clone(), bin) }
}

Expand Down Expand Up @@ -568,7 +589,7 @@ impl Iterator for BinIter {
if self.0.is_empty() {
None
} else {
let val = self.0.env().bytes_front(self.0.obj);
let val = self.0.env().bytes_front(self.0.obj).unwrap_infallible();
self.0 = self.0.slice(1..);
let val = unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) } as u8;
Some(val)
Expand All @@ -587,7 +608,7 @@ impl DoubleEndedIterator for BinIter {
if len == 0 {
None
} else {
let val = self.0.env().bytes_back(self.0.obj);
let val = self.0.env().bytes_back(self.0.obj).unwrap_infallible();
self.0 = self.0.slice(..len - 1);
let val = unsafe { <u32 as RawValConvertible>::unchecked_from_val(val) } as u8;
Some(val)
Expand Down
4 changes: 2 additions & 2 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Crypto contains functions for cryptographic functions.
use crate::{env::internal, Bytes, BytesN, Env};
use crate::{env::internal, unwrap::UnwrapInfallible, Bytes, BytesN, Env};

/// Crypto provides access to cryptographic functions.
pub struct Crypto {
Expand All @@ -18,7 +18,7 @@ impl Crypto {
/// Returns the SHA-256 hash of the data.
pub fn sha256(&self, data: &Bytes) -> BytesN<32> {
let env = self.env();
let bin = internal::Env::compute_hash_sha256(env, data.into());
let bin = internal::Env::compute_hash_sha256(env, data.into()).unwrap_infallible();
unsafe { BytesN::unchecked_new(env.clone(), bin) }
}

Expand Down
12 changes: 7 additions & 5 deletions soroban-sdk/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//! # #[cfg(not(feature = "testutils"))]
//! # fn main() { }
//! ```
use crate::{env::internal::Env as _, Bytes, BytesN, Env, IntoVal};
use crate::{env::internal::Env as _, unwrap::UnwrapInfallible, Bytes, BytesN, Env, IntoVal};

/// Deployer provides access to deploying contracts.
pub struct Deployer {
Expand Down Expand Up @@ -105,10 +105,12 @@ impl DeployerWithCurrentContract {
/// Returns the deployed contract's ID.
pub fn deploy(&self, wasm_hash: &impl IntoVal<Env, BytesN<32>>) -> BytesN<32> {
let env = &self.env;
let id = env.create_contract_from_contract(
wasm_hash.into_val(env).to_object(),
self.salt.to_object(),
);
let id = env
.create_contract_from_contract(
wasm_hash.into_val(env).to_object(),
self.salt.to_object(),
)
.unwrap_infallible();
unsafe { BytesN::<32>::unchecked_new(env.clone(), id) }
}
}
Expand Down
Loading

0 comments on commit ca34399

Please sign in to comment.