Skip to content

Commit

Permalink
Merge branch 'main' into correct-string-comment
Browse files Browse the repository at this point in the history
  • Loading branch information
graydon authored Aug 24, 2023
2 parents 45c72fb + 67d95ad commit 613d678
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 109 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

8 changes: 4 additions & 4 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 = "d92944576e2301c9866215efcdc4bbd24a5f3981"
rev = "a4fe7a118131e11e1d78ba0e6627cfe72d7de2ad"

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

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

[workspace.dependencies.stellar-strkey]
version = "0.0.7"
Expand All @@ -60,7 +60,7 @@ rev = "e6ba45c60c16de28c7522586b80ed0150157df73"
[workspace.dependencies.stellar-xdr]
version = "0.0.17"
git = "https://github.com/stellar/rs-stellar-xdr"
rev = "4876e5eb20016caebbd13bcf6401626dc6073b8e"
rev = "936273b737b99d79eee7a28dd4823436d0bd0abb"
default-features = false

#[patch."https://github.com/stellar/rs-soroban-env"]
Expand Down
31 changes: 16 additions & 15 deletions soroban-ledger-snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ pub struct LedgerSnapshot {
pub min_persistent_entry_expiration: u32,
pub min_temp_entry_expiration: u32,
pub max_entry_expiration: u32,
pub ledger_entries: Vec<(Box<LedgerKey>, Box<LedgerEntry>)>,
pub autobump_ledgers: u32,
pub ledger_entries: Vec<(Box<LedgerKey>, (Box<LedgerEntry>, Option<u32>))>,
}

impl LedgerSnapshot {
// Create a ledger snapshot from ledger info and a set of entries.
pub fn from<'a>(
info: LedgerInfo,
entries: impl IntoIterator<Item = (&'a Box<LedgerKey>, &'a Box<LedgerEntry>)>,
entries: impl IntoIterator<Item = (&'a Box<LedgerKey>, (&'a Box<LedgerEntry>, Option<u32>))>,
) -> Self {
let mut s = Self::default();
s.set_ledger_info(info);
Expand Down Expand Up @@ -76,7 +75,6 @@ impl LedgerSnapshot {
min_persistent_entry_expiration: self.min_persistent_entry_expiration,
min_temp_entry_expiration: self.min_temp_entry_expiration,
max_entry_expiration: self.max_entry_expiration,
autobump_ledgers: self.autobump_ledgers,
}
}

Expand All @@ -87,22 +85,23 @@ impl LedgerSnapshot {
self.timestamp = info.timestamp;
self.network_id = info.network_id;
self.base_reserve = info.base_reserve;
self.autobump_ledgers = info.autobump_ledgers;
}

/// Get the entries in the snapshot.
pub fn entries(&self) -> impl IntoIterator<Item = (&Box<LedgerKey>, &Box<LedgerEntry>)> {
pub fn entries(
&self,
) -> impl IntoIterator<Item = (&Box<LedgerKey>, &(Box<LedgerEntry>, Option<u32>))> {
self.ledger_entries.iter().map(|(k, v)| (k, v))
}

/// Replace the entries in the snapshot with the entries in the iterator.
pub fn set_entries<'a>(
&mut self,
entries: impl IntoIterator<Item = (&'a Box<LedgerKey>, &'a Box<LedgerEntry>)>,
entries: impl IntoIterator<Item = (&'a Box<LedgerKey>, (&'a Box<LedgerEntry>, Option<u32>))>,
) {
self.ledger_entries.clear();
for (k, e) in entries {
self.ledger_entries.push((k.clone(), e.clone()));
self.ledger_entries.push((k.clone(), (e.0.clone(), e.1)));
}
}

Expand All @@ -111,12 +110,15 @@ impl LedgerSnapshot {
/// entry.
pub fn update_entries<'a>(
&mut self,
entries: impl IntoIterator<Item = &'a (Rc<LedgerKey>, Option<Rc<LedgerEntry>>)>,
entries: impl IntoIterator<Item = &'a (Rc<LedgerKey>, Option<(Rc<LedgerEntry>, Option<u32>)>)>,
) {
for (k, e) in entries {
let i = self.ledger_entries.iter().position(|(ik, _)| **ik == **k);
if let Some(e) = e {
let new = (Box::new((**k).clone()), Box::new((**e).clone()));
if let Some((entry, expiration)) = e {
let new = (
Box::new((**k).clone()),
(Box::new((**entry).clone()), *expiration),
);
if let Some(i) = i {
self.ledger_entries[i] = new;
} else {
Expand Down Expand Up @@ -169,15 +171,14 @@ impl Default for LedgerSnapshot {
min_persistent_entry_expiration: Default::default(),
min_temp_entry_expiration: Default::default(),
max_entry_expiration: Default::default(),
autobump_ledgers: Default::default(),
}
}
}

impl SnapshotSource for &LedgerSnapshot {
fn get(&self, key: &Rc<LedgerKey>) -> Result<Rc<LedgerEntry>, HostError> {
fn get(&self, key: &Rc<LedgerKey>) -> Result<(Rc<LedgerEntry>, Option<u32>), HostError> {
match self.ledger_entries.iter().find(|(k, _)| **k == **key) {
Some((_, v)) => Ok(Rc::new(*v.clone())),
Some((_, v)) => Ok((Rc::new(*v.0.clone()), v.1)),
None => Err(ScError::Storage(ScErrorCode::MissingValue).into()),
}
}
Expand All @@ -187,7 +188,7 @@ impl SnapshotSource for &LedgerSnapshot {
}

impl SnapshotSource for LedgerSnapshot {
fn get(&self, key: &Rc<LedgerKey>) -> Result<Rc<LedgerEntry>, HostError> {
fn get(&self, key: &Rc<LedgerKey>) -> Result<(Rc<LedgerEntry>, Option<u32>), HostError> {
<_ as SnapshotSource>::get(&self, key)
}
fn has(&self, key: &Rc<LedgerKey>) -> Result<bool, HostError> {
Expand Down
38 changes: 36 additions & 2 deletions soroban-sdk-macros/src/derive_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub fn derive_client_type(crate_path: &Path, ty: &str, name: &str) -> TokenStrea
#[doc(hidden)]
#[cfg(any(test, feature = "testutils"))]
mock_all_auths: bool,
#[doc(hidden)]
#[cfg(any(test, feature = "testutils"))]
allow_non_root_auth: bool,
}

impl<'a> #client_ident<'a> {
Expand All @@ -41,6 +44,8 @@ pub fn derive_client_type(crate_path: &Path, ty: &str, name: &str) -> TokenStrea
mock_auths: None,
#[cfg(any(test, feature = "testutils"))]
mock_all_auths: false,
#[cfg(any(test, feature = "testutils"))]
allow_non_root_auth: false,
}
}

Expand All @@ -60,6 +65,7 @@ pub fn derive_client_type(crate_path: &Path, ty: &str, name: &str) -> TokenStrea
set_auths: Some(auths),
mock_auths: self.mock_auths.clone(),
mock_all_auths: false,
allow_non_root_auth: false,
}
}

Expand All @@ -76,14 +82,16 @@ pub fn derive_client_type(crate_path: &Path, ty: &str, name: &str) -> TokenStrea
set_auths: self.set_auths.clone(),
mock_auths: Some(mock_auths),
mock_all_auths: false,
allow_non_root_auth: false,
}
}

/// Mock all calls to the `Address::require_auth` and
/// `Address::require_auth_for_args` functions in invoked contracts,
/// having them succeed as if authorization was provided.
///
/// See `soroban_sdk::Env::set_auths` for more details and examples.
/// See `soroban_sdk::Env::mock_all_auths` for more details and
/// examples.
#[cfg(any(test, feature = "testutils"))]
pub fn mock_all_auths(&self) -> Self {
Self {
Expand All @@ -92,6 +100,28 @@ pub fn derive_client_type(crate_path: &Path, ty: &str, name: &str) -> TokenStrea
set_auths: None,
mock_auths: None,
mock_all_auths: true,
allow_non_root_auth: false,
}
}

/// A version of `mock_all_auths` that allows authorizations that
/// are not present in the root invocation.
///
/// Refer to `mock_all_auths` documentation for details and
/// prefer using `mock_all_auths` unless non-root authorization is
/// required.
///
/// See `soroban_sdk::Env::mock_all_auths_allowing_non_root_auth`
/// for more details and examples.
#[cfg(any(test, feature = "testutils"))]
pub fn mock_all_auths_allowing_non_root_auth(&self) -> Self {
Self {
env: self.env.clone(),
address: self.address.clone(),
set_auths: None,
mock_auths: None,
mock_all_auths: true,
allow_non_root_auth: true,
}
}
}
Expand Down Expand Up @@ -163,7 +193,11 @@ pub fn derive_client_impl(crate_path: &Path, name: &str, fns: &[syn_ext::Fn]) ->
self.env.mock_auths(mock_auths);
}
if self.mock_all_auths {
self.env.mock_all_auths();
if self.allow_non_root_auth {
self.env.mock_all_auths_allowing_non_root_auth();
} else {
self.env.mock_all_auths();
}
}
}
use #crate_path::{IntoVal,FromVal};
Expand Down
14 changes: 1 addition & 13 deletions soroban-sdk-macros/src/map_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use stellar_xdr::{
ScSpecTypeBytesN, ScSpecTypeDef, ScSpecTypeMap, ScSpecTypeOption, ScSpecTypeResult,
ScSpecTypeSet, ScSpecTypeTuple, ScSpecTypeUdt, ScSpecTypeVec,
ScSpecTypeTuple, ScSpecTypeUdt, ScSpecTypeVec,
};
use syn::{
spanned::Spanned, Error, Expr, ExprLit, GenericArgument, Lit, Path, PathArguments, PathSegment,
Expand Down Expand Up @@ -86,18 +86,6 @@ pub fn map_type(t: &Type) -> Result<ScSpecTypeDef, Error> {
element_type: Box::new(map_type(t)?),
})))
}
"Set" => {
let t = match args.as_slice() {
[GenericArgument::Type(t)] => t,
[..] => Err(Error::new(
t.span(),
"incorrect number of generic arguments, expect one for Set<T>",
))?,
};
Ok(ScSpecTypeDef::Set(Box::new(ScSpecTypeSet {
element_type: Box::new(map_type(t)?),
})))
}
"Map" => {
let (k, v) = match args.as_slice() {
[GenericArgument::Type(k), GenericArgument::Type(v)] => (k, v),
Expand Down
Loading

0 comments on commit 613d678

Please sign in to comment.