Skip to content

Commit

Permalink
Merge branch 'main' into hexacolic-britishness
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon authored Aug 25, 2023
2 parents e74ad0e + f9969b4 commit 139bd9b
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 31 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 @@ -40,17 +40,17 @@ soroban-token-sdk = { version = "0.9.2", path = "soroban-token-sdk" }
[workspace.dependencies.soroban-env-common]
version = "0.0.17"
git = "https://github.com/stellar/rs-soroban-env"
rev = "a4fe7a118131e11e1d78ba0e6627cfe72d7de2ad"
rev = "3be35df3061d08cf3e207b3eb546aa29adf32c01"

[workspace.dependencies.soroban-env-guest]
version = "0.0.17"
git = "https://github.com/stellar/rs-soroban-env"
rev = "a4fe7a118131e11e1d78ba0e6627cfe72d7de2ad"
rev = "3be35df3061d08cf3e207b3eb546aa29adf32c01"

[workspace.dependencies.soroban-env-host]
version = "0.0.17"
git = "https://github.com/stellar/rs-soroban-env"
rev = "a4fe7a118131e11e1d78ba0e6627cfe72d7de2ad"
rev = "3be35df3061d08cf3e207b3eb546aa29adf32c01"

[workspace.dependencies.stellar-strkey]
version = "0.0.7"
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl Address {
pub(crate) fn contract_id(&self) -> BytesN<32> {
match self.try_contract_id() {
Some(contract_id) => contract_id,
None => panic!("Address is not a Contract ID"),
None => sdk_panic!("Address is not a Contract ID"),
}
}

Expand Down
31 changes: 25 additions & 6 deletions soroban-sdk/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,15 @@ impl Bytes {

/// Copy the bytes into the given slice.
///
/// The minimum number of bytes are copied to either exhaust [Bytes] or fill
/// slice.
/// ### Panics
///
/// If the output slice and bytes are of different lengths.
#[inline(always)]
pub fn copy_into_slice(&self, slice: &mut [u8]) {
let env = self.env();
if self.len() as usize != slice.len() {
sdk_panic!("Bytes::copy_into_slice with mismatched slice length")
}
env.bytes_copy_to_slice(self.to_object(), Val::U32_ZERO, slice)
.unwrap_optimized();
}
Expand Down Expand Up @@ -1013,11 +1017,8 @@ impl<const N: usize> BytesN<N> {
}

/// Copy the bytes into the given slice.
///
/// The minimum number of bytes are copied to either exhaust [BytesN] or fill
/// slice.
#[inline(always)]
pub fn copy_into_slice(&self, slice: &mut [u8]) {
pub fn copy_into_slice(&self, slice: &mut [u8; N]) {
let env = self.env();
env.bytes_copy_to_slice(self.to_object(), Val::U32_ZERO, slice)
.unwrap_optimized();
Expand Down Expand Up @@ -1127,6 +1128,24 @@ mod test {
assert_eq!([1, 2, 3, 4], out);
}

#[test]
#[should_panic]
fn bytes_to_short_slice() {
let env = Env::default();
let b = Bytes::from_slice(&env, &[1, 2, 3, 4]);
let mut out = [0u8; 3];
b.copy_into_slice(&mut out);
}

#[test]
#[should_panic]
fn bytes_to_long_slice() {
let env = Env::default();
let b = Bytes::from_slice(&env, &[1, 2, 3, 4]);
let mut out = [0u8; 5];
b.copy_into_slice(&mut out);
}

#[test]
fn macro_bytes() {
let env = Env::default();
Expand Down
21 changes: 21 additions & 0 deletions soroban-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,27 @@ macro_rules! panic_error {
}};
}

/// An internal panic! variant that avoids including the string
/// when building for wasm (since it's just pointless baggage).
#[cfg(target_family = "wasm")]
macro_rules! sdk_panic {
($_msg:literal) => {
panic!()
};
() => {
panic!()
};
}
#[cfg(not(target_family = "wasm"))]
macro_rules! sdk_panic {
($msg:literal) => {
panic!($msg)
};
() => {
panic!()
};
}

/// Assert a condition and panic with the given error if it is false.
///
/// The first argument in the list must be a reference to an [Env].
Expand Down
37 changes: 26 additions & 11 deletions soroban-sdk/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,22 @@ impl Storage {
.unwrap_infallible();
}

pub(crate) fn bump<K>(&self, key: &K, storage_type: StorageType, min_ledgers_to_live: u32)
where
pub(crate) fn bump<K>(
&self,
key: &K,
storage_type: StorageType,
low_expiration_watermark: u32,
high_expiration_watermark: u32,
) where
K: IntoVal<Env, Val>,
{
let env = &self.env;
internal::Env::bump_contract_data(
env,
key.into_val(env),
storage_type,
min_ledgers_to_live.into(),
low_expiration_watermark.into(),
high_expiration_watermark.into(),
)
.unwrap_infallible();
}
Expand Down Expand Up @@ -253,12 +259,16 @@ impl Persistent {
self.storage.set(key, val, StorageType::Persistent)
}

pub fn bump<K>(&self, key: &K, min_ledgers_to_live: u32)
pub fn bump<K>(&self, key: &K, low_expiration_watermark: u32, high_expiration_watermark: u32)
where
K: IntoVal<Env, Val>,
{
self.storage
.bump(key, StorageType::Persistent, min_ledgers_to_live)
self.storage.bump(
key,
StorageType::Persistent,
low_expiration_watermark,
high_expiration_watermark,
)
}

#[inline(always)]
Expand Down Expand Up @@ -299,12 +309,16 @@ impl Temporary {
self.storage.set(key, val, StorageType::Temporary)
}

pub fn bump<K>(&self, key: &K, min_ledgers_to_live: u32)
pub fn bump<K>(&self, key: &K, low_expiration_watermark: u32, high_expiration_watermark: u32)
where
K: IntoVal<Env, Val>,
{
self.storage
.bump(key, StorageType::Temporary, min_ledgers_to_live)
self.storage.bump(
key,
StorageType::Temporary,
low_expiration_watermark,
high_expiration_watermark,
)
}

#[inline(always)]
Expand Down Expand Up @@ -353,10 +367,11 @@ impl Instance {
self.storage.remove(key, StorageType::Instance)
}

pub fn bump(&self, min_ledgers_to_live: u32) {
pub fn bump(&self, low_expiration_watermark: u32, high_expiration_watermark: u32) {
internal::Env::bump_current_contract_instance_and_code(
&self.storage.env,
min_ledgers_to_live.into(),
low_expiration_watermark.into(),
high_expiration_watermark.into(),
)
.unwrap_infallible();
}
Expand Down
28 changes: 26 additions & 2 deletions soroban-sdk/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,15 @@ impl String {

/// Copy the bytes in [String] into the given slice.
///
/// The minimum number of bytes are copied to either exhaust [String] or fill
/// slice.
/// ### Panics
///
/// If the output slice and string are of different lengths.
#[inline(always)]
pub fn copy_into_slice(&self, slice: &mut [u8]) {
let env = self.env();
if self.len() as usize != slice.len() {
sdk_panic!("String::copy_into_slice with mismatched slice length")
}
env.string_copy_to_slice(self.to_object(), Val::U32_ZERO, slice)
.unwrap_optimized();
}
Expand All @@ -235,4 +239,24 @@ mod test {
s.copy_into_slice(&mut out);
assert_eq!(msg.as_bytes(), out)
}

#[test]
#[should_panic]
fn string_to_short_slice() {
let env = Env::default();
let msg = "a message";
let s = String::from_slice(&env, msg);
let mut out = [0u8; 8];
s.copy_into_slice(&mut out);
}

#[test]
#[should_panic]
fn string_to_long_slice() {
let env = Env::default();
let msg = "a message";
let s = String::from_slice(&env, msg);
let mut out = [0u8; 10];
s.copy_into_slice(&mut out);
}
}
6 changes: 3 additions & 3 deletions soroban-sdk/src/tests/contract_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ impl Contract {
}

pub fn bump_persistent(env: Env, k: i32) {
env.storage().persistent().bump(&DataKey::Key(k), 100);
env.storage().persistent().bump(&DataKey::Key(k), 100, 100);
}

pub fn bump_temporary(env: Env, k: i32) {
env.storage().temporary().bump(&DataKey::Key(k), 100);
env.storage().temporary().bump(&DataKey::Key(k), 100, 100);
}

pub fn bump_instance(env: Env) {
env.storage().instance().bump(100);
env.storage().instance().bump(100, 100);
}
}

Expand Down

0 comments on commit 139bd9b

Please sign in to comment.