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 Sep 14, 2022
2 parents 3fa9d01 + 6f0be8f commit 7ddeae4
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 @@ -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 @@ -100,17 +100,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 @@ -191,15 +180,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 @@ -300,20 +300,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 @@ -377,20 +363,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.ee44010745c50f798d9c838b0121e5713ec4dc2ecfae093c3f3b332ed8af85d2.wasm",
"tx_from_intent.wasm": "tx_from_intent.578f429e933f5a6530628a2fa9c1d52d78fb652fe2acd796cb956fddf842acaa.wasm",
"tx_ibc.wasm": "tx_ibc.0b61d351e12fe8afc13198fcfa40a206904d027388707b947825255233c3af04.wasm",
"tx_init_account.wasm": "tx_init_account.dfb26afc4434f3687b13238d9cfed93f03ee7ac21e6ec7755851ef9dafea8f08.wasm",
"tx_init_nft.wasm": "tx_init_nft.7382e9515ec538ef94fbc60a369cb2c793306ee7cef54a28858425395bf4eca4.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.7d745b1e8dba5d4a7a0e1a8b8fae93ee5af946dda71462af9038bc201058edf2.wasm",
"tx_init_validator.wasm": "tx_init_validator.8ab24cab601f1c976f2ce0f84d8f4e5de39fbebf7e4239c0c941668780d6b86b.wasm",
"tx_mint_nft.wasm": "tx_mint_nft.e3dc0937b49f0924ee73d452c492cc6f53bfe8d629635195496b0b49c08d6615.wasm",
"tx_transfer.wasm": "tx_transfer.34c106e507d103089a4247225e87d966df20b13781a340303a3ccff50c1bc9e2.wasm",
"tx_unbond.wasm": "tx_unbond.81ed86fd48ef0b71f3494bf704b51b9eea7488aacd02313b69cb5512521af3ab.wasm",
"tx_bond.wasm": "tx_bond.f15a8858b528744c297bab32c8abeb5994b5192744c82fc530d5106b4c869b94.wasm",
"tx_from_intent.wasm": "tx_from_intent.31ce3341fbf69c6dc44419894da9e56fb8a4dff11aee6b097a15930cac74ba1e.wasm",
"tx_ibc.wasm": "tx_ibc.0fd4598671af66ed1febad48739fa524734f1d83942420962926d38c63764107.wasm",
"tx_init_account.wasm": "tx_init_account.e44d4d11f7e5a29162fe36741ee1a86f82707c7734c3ff15e890b225a4848647.wasm",
"tx_init_nft.wasm": "tx_init_nft.307b2b7b41f701788ea0f879e392796987d576b6fc76ca90780f474c2d266e5b.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.3541a117a422a9b9c887bec161b27a5b8ee22331a5e3a79c57b24a31a84ebabb.wasm",
"tx_init_validator.wasm": "tx_init_validator.a14e6172837dfeae355d4906955506998c0b89f1c608434151458f71e71ea9d4.wasm",
"tx_mint_nft.wasm": "tx_mint_nft.a7a3b6a31da00fa67cab37b01bfc6e4a8501d1967481696888a6bf0d64122872.wasm",
"tx_transfer.wasm": "tx_transfer.9459b116081cda57047909414a993e98a4d4acd7f2a71b301d910bd3da6a8372.wasm",
"tx_unbond.wasm": "tx_unbond.8c4ae7092ada34acba3c37db4be672722ce4c1d9e4819f44043509a7925030a3.wasm",
"tx_update_vp.wasm": "tx_update_vp.f32dd6d4225d426d60bf82cda4e6d6432f869a201b65ff135a9fd589679a9f0a.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.829b383d2db6b077b5a6112cacb75af63cc152bf2fb66e2df45f42d55c251b41.wasm",
"tx_withdraw.wasm": "tx_withdraw.9fc2aca6243a167ba11689410c0373afe1cb7ecbdc9891af1c01bd7052849ea1.wasm",
"vp_nft.wasm": "vp_nft.ded411ba44c11b9fc6a3626c6858c760b0fae6d3cdef4664ec5ce0a938149edc.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.4a13e20a3d263d74d7e325ab86a874bbcd60d4562f23d8a483919676f48d80aa.wasm",
"vp_token.wasm": "vp_token.9799a1d3e276c19b58ad6e78f0f1c6f8d4b17da72ff7fee582658d33bbd67583.wasm",
"vp_user.wasm": "vp_user.aadcf526d1cbe2c469a6a8eaacac13601400557ab3e87382ded16c44e8cc59be.wasm"
"tx_vote_proposal.wasm": "tx_vote_proposal.8f7dda2935faa0de3c61e867290aef1d8384301036d11d15e4afc0bd2182f4db.wasm",
"tx_withdraw.wasm": "tx_withdraw.290ad129267b7877a8d68ee14bcba8e80f34f514c4fadeb328d89f8879a6c147.wasm",
"vp_nft.wasm": "vp_nft.9771b8c2853d00447068193b10ae2d4048f560539cfb545359df9e3110748250.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.810fe25034654bcba63d1a3e4d670d9b97bbf8424fd65f739e7e2aa6b2e54a73.wasm",
"vp_token.wasm": "vp_token.3748713d602e3d0f28ebceee1fde8033c14608a97b0255b7705ea756062d5f27.wasm",
"vp_user.wasm": "vp_user.c19de653bb91dd30c555f16bf6c35dfc7d48526f0ac5b683d9a3ca2427c6f554.wasm"
}

0 comments on commit 7ddeae4

Please sign in to comment.