Skip to content

Commit

Permalink
Adapt storage and snapshot interface to env changes (#873)
Browse files Browse the repository at this point in the history
### What

Adapt to env changes in
stellar/rs-soroban-env#700

### Why

[TODO: Why this change is being made. Include any context required to
understand the why.]

### Known limitations

[TODO or N/A]
  • Loading branch information
jayz22 authored Feb 27, 2023
1 parent ea3a44d commit b7cc604
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 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.6.0", path = "soroban-token-spec" }
[workspace.dependencies.soroban-env-common]
version = "0.0.14"
git = "https://github.com/stellar/rs-soroban-env"
rev = "7241dbd90048f1a5ce465c39aefa3fc14ec989c5"
rev = "dc7f13554c9c318fbae7a5ffb043ca43156f79d7"

[workspace.dependencies.soroban-env-guest]
version = "0.0.14"
git = "https://github.com/stellar/rs-soroban-env"
rev = "7241dbd90048f1a5ce465c39aefa3fc14ec989c5"
rev = "dc7f13554c9c318fbae7a5ffb043ca43156f79d7"

[workspace.dependencies.soroban-env-host]
version = "0.0.14"
git = "https://github.com/stellar/rs-soroban-env"
rev = "7241dbd90048f1a5ce465c39aefa3fc14ec989c5"
rev = "dc7f13554c9c318fbae7a5ffb043ca43156f79d7"

[workspace.dependencies.stellar-strkey]
version = "0.0.7"
Expand Down
14 changes: 7 additions & 7 deletions soroban-ledger-snapshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,24 @@ impl Default for LedgerSnapshot {
}

impl SnapshotSource for &LedgerSnapshot {
fn get(&self, key: &LedgerKey) -> Result<LedgerEntry, HostError> {
match self.ledger_entries.iter().find(|(k, _)| k.as_ref() == key) {
Some((_, v)) => Ok(*v.clone()),
fn get(&self, key: &Rc<LedgerKey>) -> Result<Rc<LedgerEntry>, HostError> {
match self.ledger_entries.iter().find(|(k, _)| &**k == &**key) {
Some((_, v)) => Ok(Rc::new(*v.clone())),
None => {
Err(ScStatus::HostStorageError(ScHostStorageErrorCode::AccessToUnknownEntry).into())
}
}
}
fn has(&self, key: &LedgerKey) -> Result<bool, HostError> {
Ok(self.ledger_entries.iter().any(|(k, _)| k.as_ref() == key))
fn has(&self, key: &Rc<LedgerKey>) -> Result<bool, HostError> {
Ok(self.ledger_entries.iter().any(|(k, _)| &**k == &**key))
}
}

impl SnapshotSource for LedgerSnapshot {
fn get(&self, key: &LedgerKey) -> Result<LedgerEntry, HostError> {
fn get(&self, key: &Rc<LedgerKey>) -> Result<Rc<LedgerEntry>, HostError> {
<_ as SnapshotSource>::get(&self, key)
}
fn has(&self, key: &LedgerKey) -> Result<bool, HostError> {
fn has(&self, key: &Rc<LedgerKey>) -> Result<bool, HostError> {
<_ as SnapshotSource>::has(&self, key)
}
}
41 changes: 18 additions & 23 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ impl Env {
impl internal::storage::SnapshotSource for EmptySnapshotSource {
fn get(
&self,
_key: &xdr::LedgerKey,
) -> Result<xdr::LedgerEntry, soroban_env_host::HostError> {
_key: &Rc<xdr::LedgerKey>,
) -> Result<Rc<xdr::LedgerEntry>, soroban_env_host::HostError> {
use xdr::{ScHostStorageErrorCode, ScStatus};
let status: internal::Status =
ScStatus::HostStorageError(ScHostStorageErrorCode::MissingKeyInGet).into();
Err(status.into())
}

fn has(&self, _key: &xdr::LedgerKey) -> Result<bool, soroban_env_host::HostError> {
fn has(&self, _key: &Rc<xdr::LedgerKey>) -> Result<bool, soroban_env_host::HostError> {
Ok(false)
}
}
Expand Down Expand Up @@ -514,15 +514,15 @@ impl Env {

self.host()
.with_mut_storage(|storage| {
let k = xdr::LedgerKey::Account(xdr::LedgerKeyAccount {
let k = Rc::new(xdr::LedgerKey::Account(xdr::LedgerKeyAccount {
account_id: issuer_id.clone(),
});
}));

if !storage.has(
&k,
soroban_env_host::budget::AsBudget::as_budget(self.host()),
)? {
let v = xdr::LedgerEntry {
let v = Rc::new(xdr::LedgerEntry {
data: xdr::LedgerEntryData::Account(xdr::AccountEntry {
account_id: issuer_id.clone(),
balance: 0,
Expand All @@ -537,7 +537,7 @@ impl Env {
}),
last_modified_ledger_seq: 0,
ext: xdr::LedgerEntryExt::V0,
};
});
storage.put(
&k,
&v,
Expand Down Expand Up @@ -736,26 +736,21 @@ impl Env {
) {
let contract_id_hash = Hash(contract_id.into());
let data_key = xdr::ScVal::Static(xdr::ScStatic::LedgerKeyContractCode);
let key = LedgerKey::ContractData(LedgerKeyContractData {
let key = Rc::new(LedgerKey::ContractData(LedgerKeyContractData {
contract_id: contract_id_hash.clone(),
key: data_key.clone(),
}));
let entry = Rc::new(LedgerEntry {
ext: xdr::LedgerEntryExt::V0,
last_modified_ledger_seq: 0,
data: xdr::LedgerEntryData::ContractData(xdr::ContractDataEntry {
contract_id: contract_id_hash.clone(),
key: data_key,
val: xdr::ScVal::Object(Some(xdr::ScObject::ContractCode(source))),
}),
});
self.env_impl
.with_mut_storage(|storage| {
storage.put(
&key,
&LedgerEntry {
ext: xdr::LedgerEntryExt::V0,
last_modified_ledger_seq: 0,
data: xdr::LedgerEntryData::ContractData(xdr::ContractDataEntry {
contract_id: contract_id_hash.clone(),
key: data_key,
val: xdr::ScVal::Object(Some(xdr::ScObject::ContractCode(source))),
}),
},
&self.env_impl.budget_cloned(),
)
})
.with_mut_storage(|storage| storage.put(&key, &entry, &self.env_impl.budget_cloned()))
.unwrap();
}

Expand Down

0 comments on commit b7cc604

Please sign in to comment.