From b7cc604488f4d95c48759ab9786454f06cde7512 Mon Sep 17 00:00:00 2001 From: Jay Geng Date: Mon, 27 Feb 2023 15:02:23 -0500 Subject: [PATCH] Adapt storage and snapshot interface to env changes (#873) ### What Adapt to env changes in https://github.com/stellar/rs-soroban-env/pull/700 ### Why [TODO: Why this change is being made. Include any context required to understand the why.] ### Known limitations [TODO or N/A] --- Cargo.lock | 10 ++++---- Cargo.toml | 6 ++--- soroban-ledger-snapshot/src/lib.rs | 14 +++++----- soroban-sdk/src/env.rs | 41 +++++++++++++----------------- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fcbc32884..36760d8ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -838,7 +838,7 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "0.0.14" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7241dbd90048f1a5ce465c39aefa3fc14ec989c5#7241dbd90048f1a5ce465c39aefa3fc14ec989c5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=dc7f13554c9c318fbae7a5ffb043ca43156f79d7#dc7f13554c9c318fbae7a5ffb043ca43156f79d7" dependencies = [ "crate-git-revision", "serde", @@ -851,7 +851,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.14" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7241dbd90048f1a5ce465c39aefa3fc14ec989c5#7241dbd90048f1a5ce465c39aefa3fc14ec989c5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=dc7f13554c9c318fbae7a5ffb043ca43156f79d7#dc7f13554c9c318fbae7a5ffb043ca43156f79d7" dependencies = [ "soroban-env-common", "static_assertions", @@ -860,7 +860,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.14" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7241dbd90048f1a5ce465c39aefa3fc14ec989c5#7241dbd90048f1a5ce465c39aefa3fc14ec989c5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=dc7f13554c9c318fbae7a5ffb043ca43156f79d7#dc7f13554c9c318fbae7a5ffb043ca43156f79d7" dependencies = [ "backtrace", "curve25519-dalek", @@ -882,7 +882,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.14" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7241dbd90048f1a5ce465c39aefa3fc14ec989c5#7241dbd90048f1a5ce465c39aefa3fc14ec989c5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=dc7f13554c9c318fbae7a5ffb043ca43156f79d7#dc7f13554c9c318fbae7a5ffb043ca43156f79d7" dependencies = [ "itertools", "proc-macro2", @@ -908,7 +908,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.14" -source = "git+https://github.com/stellar/rs-soroban-env?rev=7241dbd90048f1a5ce465c39aefa3fc14ec989c5#7241dbd90048f1a5ce465c39aefa3fc14ec989c5" +source = "git+https://github.com/stellar/rs-soroban-env?rev=dc7f13554c9c318fbae7a5ffb043ca43156f79d7#dc7f13554c9c318fbae7a5ffb043ca43156f79d7" dependencies = [ "itertools", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 2227b72da..6cee42397 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/soroban-ledger-snapshot/src/lib.rs b/soroban-ledger-snapshot/src/lib.rs index efa6a21c1..0bea685e1 100644 --- a/soroban-ledger-snapshot/src/lib.rs +++ b/soroban-ledger-snapshot/src/lib.rs @@ -162,24 +162,24 @@ impl Default for LedgerSnapshot { } impl SnapshotSource for &LedgerSnapshot { - fn get(&self, key: &LedgerKey) -> Result { - match self.ledger_entries.iter().find(|(k, _)| k.as_ref() == key) { - Some((_, v)) => Ok(*v.clone()), + fn get(&self, key: &Rc) -> Result, 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 { - Ok(self.ledger_entries.iter().any(|(k, _)| k.as_ref() == key)) + fn has(&self, key: &Rc) -> Result { + Ok(self.ledger_entries.iter().any(|(k, _)| &**k == &**key)) } } impl SnapshotSource for LedgerSnapshot { - fn get(&self, key: &LedgerKey) -> Result { + fn get(&self, key: &Rc) -> Result, HostError> { <_ as SnapshotSource>::get(&self, key) } - fn has(&self, key: &LedgerKey) -> Result { + fn has(&self, key: &Rc) -> Result { <_ as SnapshotSource>::has(&self, key) } } diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index fec40dd8a..f7d09ac59 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -368,15 +368,15 @@ impl Env { impl internal::storage::SnapshotSource for EmptySnapshotSource { fn get( &self, - _key: &xdr::LedgerKey, - ) -> Result { + _key: &Rc, + ) -> Result, 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 { + fn has(&self, _key: &Rc) -> Result { Ok(false) } } @@ -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, @@ -537,7 +537,7 @@ impl Env { }), last_modified_ledger_seq: 0, ext: xdr::LedgerEntryExt::V0, - }; + }); storage.put( &k, &v, @@ -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(); }