Skip to content

Commit

Permalink
Merge branch 'tomas/refactor-read-write' (#334)
Browse files Browse the repository at this point in the history
* tomas/refactor-read-write:
  changelog: add #334
  wasm checksums update
  storage_api: add default borsh encoded read/write impl and handle errors
  • Loading branch information
tzemanovic committed Aug 22, 2022
2 parents 6eb19ad + 6f0be8f commit d339f19
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Re-use encoding/decoding storage write/read and handle any errors
([#334](https://github.com/anoma/namada/pull/334))
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 @@ -699,17 +699,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 @@ -767,15 +756,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 @@ -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
32 changes: 16 additions & 16 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"tx_bond.wasm": "tx_bond.3999d6dc7cdadb58d3e55aadf5099b42ff4f07d70f68464cb514d0bbdb753f0b.wasm",
"tx_from_intent.wasm": "tx_from_intent.43ab4d55fa555a7f93366f11625d05bb9feb5fe08102185417235b3a9cb1a632.wasm",
"tx_ibc.wasm": "tx_ibc.d499da43e2b9662ee9448b9691089132bbba31c8cc2c60e40650f492f6ab7c45.wasm",
"tx_init_account.wasm": "tx_init_account.ce6092a2baae2a57047605fa649b2052ec33ba33e000b5d191bdea09ac0ffb23.wasm",
"tx_init_nft.wasm": "tx_init_nft.bd8687a6223daf14a23f82c4cd5bbf9c1dbacbf598824b78f5e5f7f92f5d0373.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.f01c0dbedccc840a720e7f2ab0d7e2f719fad90985c370e29bf49ba2e400fbbc.wasm",
"tx_init_validator.wasm": "tx_init_validator.1ec27c79fd902eb9f2f42a1852295a617610a3cc4f99641e0fb160c70bbf8453.wasm",
"tx_mint_nft.wasm": "tx_mint_nft.c3d3db069ab09d2d13487c2c8f4d04fe2569314fe9b8eabc54a4be31dab746b0.wasm",
"tx_transfer.wasm": "tx_transfer.794b1d0667b14582b592d16802fe10edb2a9d1e63fdf5096b6216ee3e943f7ce.wasm",
"tx_unbond.wasm": "tx_unbond.11fbf206fea14e56d801564b59b08999617a45a31aa67b00107cfd5830d0e267.wasm",
"tx_bond.wasm": "tx_bond.1954b4909bf4e43f09b8a0da2fceb1aa0e2313d0596fadd5151b1b28e369ca0e.wasm",
"tx_from_intent.wasm": "tx_from_intent.e065a661f424cf205a412f68762543f558300d14edd08774109b41edcddba13b.wasm",
"tx_ibc.wasm": "tx_ibc.6ca598ab75afa183d399f74db60dc39a843ae7583ba4cbbfbf6e1b0be89010a6.wasm",
"tx_init_account.wasm": "tx_init_account.c8bae19a17172e8843ac40b343c929e50f85fc56c351d02914ee42d81e6053bc.wasm",
"tx_init_nft.wasm": "tx_init_nft.8d72699664afd7fc7801dcf09e3d1fec04dd55b98a3ed1dc30aae8010898bc2c.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.13efe62a687d0688b615a9cc869f35ee455a52e7c175ed594ab23cf3ae609050.wasm",
"tx_init_validator.wasm": "tx_init_validator.3ba02ac038acfe50ce07be1786d6ba5ab334b31384b1466a4c00b4cb4044df7d.wasm",
"tx_mint_nft.wasm": "tx_mint_nft.a696a518c3a3cfe2a9a611dc1e35ab8907a46fca35deea977c810aa315eda8c2.wasm",
"tx_transfer.wasm": "tx_transfer.33206497969f293a4ece5f816b43a4794b2bef2ba792300d89c25d38f4fb7eea.wasm",
"tx_unbond.wasm": "tx_unbond.89b65c1eb0bdefadfcb900820514a0d740f8debb2c9f9d5572ed50d774a064ea.wasm",
"tx_update_vp.wasm": "tx_update_vp.a74c98cb8d5230875e212807a86c9c7756ab121afe9332a91544c2ad08d9cdab.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.06bb9ba743b175a0ed0ea8eb782624d3b9a7420c03178ac5325de1b772dbdc9a.wasm",
"tx_withdraw.wasm": "tx_withdraw.cf788e62bbdeb9af43c0353ddaf2536ae79c389625e17d9207de8f1046548a01.wasm",
"vp_nft.wasm": "vp_nft.0e64dd5846dba1a34bbf796036bcc0c07b8c2c1f168af438734a4ac250a3be87.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.3e7bf06437e8ad1166ef987f831e26fccd4ae77d6122f9b3ea1d4f653614595a.wasm",
"vp_token.wasm": "vp_token.f3dd9b74572b76a20b2becfb45f66fac130c14121cbfb67d5de6c98f715e8c06.wasm",
"vp_user.wasm": "vp_user.39be129c4b577c4ac3a1f4147e1f6f5f8fd4d8acddc74eff85f6e4bb281ebb63.wasm"
"tx_vote_proposal.wasm": "tx_vote_proposal.f34d1ed3bc6f9a7b0ee8f7a209a77f63149b9a166fb433dedb305d5adb5fad05.wasm",
"tx_withdraw.wasm": "tx_withdraw.0bb13fdf61f6ab635e15e5abba3b72ab833e9e33d10794d9331acb33d777c8d5.wasm",
"vp_nft.wasm": "vp_nft.7f524b462d5d23a08bebbae517a154bc3d44436a435ed37b44aa816f20ae86ff.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.cdd0e15059c0b4194ae1144fa6307d28bb2c7b96cbdc5f9c6f08c16059a7b7db.wasm",
"vp_token.wasm": "vp_token.1f415edb5a4d15feea249271c8efc9ea7a0b9873b6cfe1e2774ac5db58254700.wasm",
"vp_user.wasm": "vp_user.165ec95f97d5e9e6931a9b32a18b8c02e8f89028c52e67d0a6fb09639512ce53.wasm"
}

0 comments on commit d339f19

Please sign in to comment.