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 1 commit
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
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
62 changes: 45 additions & 17 deletions soroban-sdk/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::{cmp::Ordering, fmt::Debug};

use crate::{
env::internal::xdr,
env::internal::{Env as _, RawVal, RawValConvertible},
env::internal::{Env as _, EnvBase as _, RawVal, RawValConvertible},
env::EnvObj,
BytesN, ConversionError, Env, IntoVal, Object, TryFromVal, TryIntoVal,
};
Expand Down Expand Up @@ -111,16 +111,30 @@ impl PartialOrd for AccountId {

impl Ord for AccountId {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.0.cmp(&other.0)
self.0.env.check_same_env(&other.0.env);
let v = self
.0
.env
.obj_cmp(self.0.obj.to_raw(), other.0.obj.to_raw());
if v == 0 {
Ordering::Equal
} else if v < 0 {
Ordering::Less
} else {
Ordering::Greater
}
leighmcculloch marked this conversation as resolved.
Show resolved Hide resolved
}
}

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(EnvObj {
env: env.clone(),
obj,
}))
} else {
Err(ConversionError {})
}
Expand Down Expand Up @@ -182,7 +196,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.0.env, v.0.obj.to_raw())
}
}

Expand Down Expand Up @@ -248,28 +262,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(EnvObj { env, obj })
}

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

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

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

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

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

Expand Down Expand Up @@ -421,7 +435,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 +491,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 +528,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 +550,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