From 830be45146a636ddb8c17a38a2f1ca43a7fb6ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Mon, 15 Aug 2022 15:54:35 +0200 Subject: [PATCH] storage_api: add default borsh encoded read/write impl and handle errors --- shared/src/ledger/native_vp.rs | 14 -------------- shared/src/ledger/storage/mod.rs | 9 --------- shared/src/ledger/storage_api/mod.rs | 16 ++++++++++++++-- tx_prelude/src/lib.rs | 20 -------------------- vp_prelude/src/lib.rs | 28 ---------------------------- 5 files changed, 14 insertions(+), 73 deletions(-) diff --git a/shared/src/ledger/native_vp.rs b/shared/src/ledger/native_vp.rs index fa30efb7eae..ae339daddf1 100644 --- a/shared/src/ledger/native_vp.rs +++ b/shared/src/ledger/native_vp.rs @@ -158,13 +158,6 @@ where { type PrefixIter = >::PrefixIter; - fn read( - &self, - key: &crate::types::storage::Key, - ) -> Result, storage_api::Error> { - self.ctx.read_pre(key).into_storage_result() - } - fn read_bytes( &self, key: &crate::types::storage::Key, @@ -218,13 +211,6 @@ where { type PrefixIter = >::PrefixIter; - fn read( - &self, - key: &crate::types::storage::Key, - ) -> Result, storage_api::Error> { - self.ctx.read_post(key).into_storage_result() - } - fn read_bytes( &self, key: &crate::types::storage::Key, diff --git a/shared/src/ledger/storage/mod.rs b/shared/src/ledger/storage/mod.rs index 28b160ddb4c..0b0207c6af6 100644 --- a/shared/src/ledger/storage/mod.rs +++ b/shared/src/ledger/storage/mod.rs @@ -763,15 +763,6 @@ where D: DB + for<'iter> DBIter<'iter>, H: StorageHasher, { - fn write( - &mut self, - key: &crate::types::storage::Key, - val: T, - ) -> storage_api::Result<()> { - let val = val.try_to_vec().unwrap(); - self.write_bytes(key, val) - } - fn write_bytes( &mut self, key: &crate::types::storage::Key, diff --git a/shared/src/ledger/storage_api/mod.rs b/shared/src/ledger/storage_api/mod.rs index 36e89a81716..3e1e7317999 100644 --- a/shared/src/ledger/storage_api/mod.rs +++ b/shared/src/ledger/storage_api/mod.rs @@ -18,7 +18,16 @@ pub trait StorageRead { fn read( &self, key: &storage::Key, - ) -> Result>; + ) -> Result> { + let bytes = self.read_bytes(key)?; + match bytes { + Some(bytes) => { + let val = T::try_from_slice(&bytes).into_storage_result()?; + Ok(Some(val)) + } + None => Ok(None), + } + } /// Storage read raw bytes. It will try to read from the storage. fn read_bytes(&self, key: &storage::Key) -> Result>>; @@ -59,7 +68,10 @@ pub trait StorageWrite { &mut self, key: &storage::Key, val: T, - ) -> Result<()>; + ) -> Result<()> { + let bytes = val.try_to_vec().into_storage_result()?; + self.write_bytes(key, bytes) + } /// Write a value as bytes at the given key to storage. fn write_bytes( diff --git a/tx_prelude/src/lib.rs b/tx_prelude/src/lib.rs index ac761b610a9..28463533c33 100644 --- a/tx_prelude/src/lib.rs +++ b/tx_prelude/src/lib.rs @@ -110,17 +110,6 @@ pub struct KeyValIterator(pub u64, pub PhantomData); impl StorageRead for Ctx { type PrefixIter = KeyValIterator<(String, Vec)>; - fn read( - &self, - key: &namada::types::storage::Key, - ) -> Result, storage_api::Error> { - let key = key.to_string(); - let read_result = - unsafe { anoma_tx_read(key.as_ptr() as _, key.len() as _) }; - Ok(read_from_buffer(read_result, anoma_tx_result_buffer) - .and_then(|t| T::try_from_slice(&t[..]).ok())) - } - fn read_bytes( &self, key: &namada::types::storage::Key, @@ -201,15 +190,6 @@ impl StorageRead for Ctx { } impl StorageWrite for Ctx { - fn write( - &mut self, - key: &namada::types::storage::Key, - val: T, - ) -> storage_api::Result<()> { - let buf = val.try_to_vec().unwrap(); - self.write_bytes(key, buf) - } - fn write_bytes( &mut self, key: &namada::types::storage::Key, diff --git a/vp_prelude/src/lib.rs b/vp_prelude/src/lib.rs index b6d7c90171f..b90106c3079 100644 --- a/vp_prelude/src/lib.rs +++ b/vp_prelude/src/lib.rs @@ -310,20 +310,6 @@ impl VpEnv for Ctx { impl StorageRead for CtxPreStorageRead<'_> { type PrefixIter = KeyValIterator<(String, Vec)>; - fn read( - &self, - key: &storage::Key, - ) -> Result, storage_api::Error> { - let bytes = self.read_bytes(key)?; - match bytes { - Some(bytes) => match T::try_from_slice(&bytes[..]) { - Ok(val) => Ok(Some(val)), - Err(err) => Err(storage_api::Error::new(err)), - }, - None => Ok(None), - } - } - fn read_bytes( &self, key: &storage::Key, @@ -387,20 +373,6 @@ impl StorageRead for CtxPreStorageRead<'_> { impl StorageRead for CtxPostStorageRead<'_> { type PrefixIter = KeyValIterator<(String, Vec)>; - fn read( - &self, - key: &storage::Key, - ) -> Result, storage_api::Error> { - let bytes = self.read_bytes(key)?; - match bytes { - Some(bytes) => match T::try_from_slice(&bytes[..]) { - Ok(val) => Ok(Some(val)), - Err(err) => Err(storage_api::Error::new(err)), - }, - None => Ok(None), - } - } - fn read_bytes( &self, key: &storage::Key,