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 15, 2022
1 parent 22c5956 commit 830be45
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 73 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 @@ -218,13 +211,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
9 changes: 0 additions & 9 deletions shared/src/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,15 +763,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().unwrap();
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 @@ -18,7 +18,16 @@ pub trait StorageRead {
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 @@ -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(
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 @@ -110,17 +110,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 @@ -201,15 +190,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 @@ -310,20 +310,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 @@ -387,20 +373,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 830be45

Please sign in to comment.