Skip to content

Commit

Permalink
storage_api: add default borsh encoded read/write impl and handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Aug 21, 2022
1 parent b92edd4 commit 9021d7e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 84 deletions.
14 changes: 0 additions & 14 deletions shared/src/ledger/native_vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ where
{
type PrefixIter = <DB as storage::DBIter<'a>>::PrefixIter;

fn read<T: borsh::BorshDeserialize>(
&self,
key: &crate::types::storage::Key,
) -> Result<Option<T>, storage_api::Error> {
self.ctx.read_pre(key).into_storage_result()
}

fn read_bytes(
&self,
key: &crate::types::storage::Key,
Expand Down Expand Up @@ -219,13 +212,6 @@ where
{
type PrefixIter = <DB as storage::DBIter<'a>>::PrefixIter;

fn read<T: borsh::BorshDeserialize>(
&self,
key: &crate::types::storage::Key,
) -> Result<Option<T>, storage_api::Error> {
self.ctx.read_post(key).into_storage_result()
}

fn read_bytes(
&self,
key: &crate::types::storage::Key,
Expand Down
20 changes: 0 additions & 20 deletions shared/src/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,17 +697,6 @@ where
{
type PrefixIter = <D as DBIter<'iter>>::PrefixIter;

fn read<T: borsh::BorshDeserialize>(
&self,
key: &crate::types::storage::Key,
) -> std::result::Result<Option<T>, storage_api::Error> {
self.read_bytes(key)
.map(|maybe_value| {
maybe_value.and_then(|t| T::try_from_slice(&t[..]).ok())
})
.into_storage_result()
}

fn read_bytes(
&self,
key: &crate::types::storage::Key,
Expand Down Expand Up @@ -765,15 +754,6 @@ where
D: DB + for<'iter> DBIter<'iter>,
H: StorageHasher,
{
fn write<T: borsh::BorshSerialize>(
&mut self,
key: &crate::types::storage::Key,
val: T,
) -> storage_api::Result<()> {
let val = val.try_to_vec().into_storage_result()?;
self.write_bytes(key, val)
}

fn write_bytes(
&mut self,
key: &crate::types::storage::Key,
Expand Down
16 changes: 14 additions & 2 deletions shared/src/ledger/storage_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ pub trait StorageRead<'iter> {
fn read<T: BorshDeserialize>(
&self,
key: &storage::Key,
) -> Result<Option<T>>;
) -> Result<Option<T>> {
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<Option<Vec<u8>>>;
Expand Down Expand Up @@ -84,7 +93,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(
Expand Down
20 changes: 0 additions & 20 deletions tx_prelude/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,6 @@ pub struct KeyValIterator<T>(pub u64, pub PhantomData<T>);
impl StorageRead<'_> for Ctx {
type PrefixIter = KeyValIterator<(String, Vec<u8>)>;

fn read<T: BorshDeserialize>(
&self,
key: &namada::types::storage::Key,
) -> Result<Option<T>, 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,
Expand Down Expand Up @@ -189,15 +178,6 @@ impl StorageRead<'_> for Ctx {
}

impl StorageWrite for Ctx {
fn write<T: BorshSerialize>(
&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,
Expand Down
28 changes: 0 additions & 28 deletions vp_prelude/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,6 @@ impl VpEnv for Ctx {
impl StorageRead<'_> for CtxPreStorageRead<'_> {
type PrefixIter = KeyValIterator<(String, Vec<u8>)>;

fn read<T: BorshDeserialize>(
&self,
key: &storage::Key,
) -> Result<Option<T>, 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,
Expand Down Expand Up @@ -375,20 +361,6 @@ impl StorageRead<'_> for CtxPreStorageRead<'_> {
impl StorageRead<'_> for CtxPostStorageRead<'_> {
type PrefixIter = KeyValIterator<(String, Vec<u8>)>;

fn read<T: BorshDeserialize>(
&self,
key: &storage::Key,
) -> Result<Option<T>, 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,
Expand Down

0 comments on commit 9021d7e

Please sign in to comment.