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

Update env to include delete-im, remove-EnvVal changes #794

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 5 additions & 48 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 @@ -35,17 +35,17 @@ soroban-token-spec = { version = "0.3.2", path = "soroban-token-spec" }
[workspace.dependencies.soroban-env-common]
version = "0.0.11"
git = "https://github.com/stellar/rs-soroban-env"
rev = "c551daf"
rev = "995386a"

[workspace.dependencies.soroban-env-guest]
version = "0.0.11"
git = "https://github.com/stellar/rs-soroban-env"
rev = "c551daf"
rev = "995386a"

[workspace.dependencies.soroban-env-host]
version = "0.0.11"
git = "https://github.com/stellar/rs-soroban-env"
rev = "c551daf"
rev = "995386a"

[workspace.dependencies.stellar-strkey]
version = "0.0.6"
Expand Down
7 changes: 4 additions & 3 deletions soroban-ledger-snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
fs::File,
io::{Read, Write},
path::Path,
rc::Rc,
};

use soroban_env_host::{
Expand Down Expand Up @@ -79,12 +80,12 @@ impl LedgerSnapshot {
// entry.
pub fn update_entries<'a>(
&mut self,
entries: impl IntoIterator<Item = (&'a Box<LedgerKey>, &'a Option<Box<LedgerEntry>>)>,
entries: impl IntoIterator<Item = &'a (Rc<LedgerKey>, Option<Rc<LedgerEntry>>)>,
) {
for (k, e) in entries {
let i = self.ledger_entries.iter().position(|(ik, _)| ik == k);
let i = self.ledger_entries.iter().position(|(ik, _)| **ik == **k);
if let Some(e) = e {
let new = (k.clone(), e.clone());
let new = (Box::new((**k).clone()), Box::new((**e).clone()));
if let Some(i) = i {
self.ledger_entries[i] = new;
} else {
Expand Down
59 changes: 40 additions & 19 deletions soroban-sdk/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use core::{cmp::Ordering, fmt::Debug};

use crate::{
env::internal::xdr,
env::internal::{Env as _, RawVal, RawValConvertible},
env::EnvObj,
env::internal::{Env as _, EnvBase as _, RawVal, RawValConvertible},
BytesN, ConversionError, Env, IntoVal, Object, TryFromVal, TryIntoVal,
};

Expand Down Expand Up @@ -76,7 +75,10 @@ impl Accounts {
///
/// In tests account identifiers can be generated using [`Accounts`].
#[derive(Clone)]
pub struct AccountId(EnvObj);
pub struct AccountId {
env: Env,
obj: Object,
}

impl Debug for AccountId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Expand Down Expand Up @@ -111,16 +113,21 @@ impl PartialOrd for AccountId {

impl Ord for AccountId {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.0.cmp(&other.0)
self.env.check_same_env(&other.env);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call to add this check.

let v = self.env.obj_cmp(self.obj.to_raw(), other.obj.to_raw());
v.cmp(&0)
}
}

impl TryFromVal<Env, Object> for AccountId {
type Error = ConversionError;

fn try_from_val(env: &Env, val: Object) -> Result<Self, Self::Error> {
if val.is_obj_type(xdr::ScObjectType::AccountId) {
Ok(AccountId(val.in_env(env)))
fn try_from_val(env: &Env, obj: Object) -> Result<Self, Self::Error> {
if obj.is_obj_type(xdr::ScObjectType::AccountId) {
Ok(AccountId {
env: env.clone(),
obj,
})
} else {
Err(ConversionError {})
}
Expand Down Expand Up @@ -182,7 +189,7 @@ use super::xdr::ScVal;
impl TryFrom<&AccountId> for ScVal {
type Error = ConversionError;
fn try_from(v: &AccountId) -> Result<Self, Self::Error> {
ScVal::try_from_val(&v.0.env, v.0.val.to_raw())
ScVal::try_from_val(&v.env, v.obj.to_raw())
}
}

Expand Down Expand Up @@ -248,28 +255,28 @@ impl TryIntoVal<Env, AccountId> for super::xdr::AccountId {
}

impl AccountId {
pub(crate) unsafe fn unchecked_new(obj: EnvObj) -> Self {
Self(obj)
pub(crate) unsafe fn unchecked_new(env: Env, obj: Object) -> Self {
Self { env, obj }
}

pub fn env(&self) -> &Env {
self.0.env()
&self.env
}

pub fn as_raw(&self) -> &RawVal {
self.0.as_raw()
self.obj.as_raw()
}

pub fn as_object(&self) -> &Object {
self.0.as_object()
&self.obj
}

pub fn to_raw(&self) -> RawVal {
self.0.to_raw()
self.obj.to_raw()
}

pub fn to_object(&self) -> Object {
self.0.to_object()
self.obj
}
}

Expand Down Expand Up @@ -421,7 +428,11 @@ impl testutils::Accounts for Accounts {
last_modified_ledger_seq: 0,
ext: xdr::LedgerEntryExt::V0,
};
storage.put(&k, &v)
storage.put(
&k,
&v,
soroban_env_host::budget::AsBudget::as_budget(env.host()),
leighmcculloch marked this conversation as resolved.
Show resolved Hide resolved
)
})
.unwrap();
}
Expand Down Expand Up @@ -473,7 +484,10 @@ impl testutils::Accounts for Accounts {
env.host()
.with_mut_storage(|storage| {
let k = xdr::LedgerKey::Account(xdr::LedgerKeyAccount { account_id: id });
storage.del(&k)
storage.del(
&k,
soroban_env_host::budget::AsBudget::as_budget(env.host()),
)
})
.unwrap();
}
Expand Down Expand Up @@ -507,7 +521,10 @@ impl Accounts {
account_id: id.clone(),
});
let mut v = storage
.get(&k)
.get(
&k,
soroban_env_host::budget::AsBudget::as_budget(env.host()),
)
.ok()
.and_then(|v| {
if let xdr::LedgerEntryData::Account(_) = v.data {
Expand All @@ -526,7 +543,11 @@ impl Accounts {
} else {
panic!("ledger entry is not an account");
}
storage.put(&k, &v)
storage.put(
&k,
&v,
soroban_env_host::budget::AsBudget::as_budget(env.host()),
)
})
.unwrap();
}
Expand Down
Loading