diff --git a/.changelog/unreleased/improvements/334-refactor-storage-read-write.md b/.changelog/unreleased/improvements/334-refactor-storage-read-write.md new file mode 100644 index 0000000000..0642596268 --- /dev/null +++ b/.changelog/unreleased/improvements/334-refactor-storage-read-write.md @@ -0,0 +1,2 @@ +- Re-use encoding/decoding storage write/read and handle any errors + ([#334](https://github.com/anoma/namada/pull/334)) \ No newline at end of file diff --git a/shared/src/ledger/native_vp.rs b/shared/src/ledger/native_vp.rs index 5cbab7d639..41fbc1cda3 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, @@ -219,13 +212,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 355a2ea0b7..8144e17439 100644 --- a/shared/src/ledger/storage/mod.rs +++ b/shared/src/ledger/storage/mod.rs @@ -699,17 +699,6 @@ where { type PrefixIter = >::PrefixIter; - fn read( - &self, - key: &crate::types::storage::Key, - ) -> std::result::Result, 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, @@ -767,15 +756,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().into_storage_result()?; - 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 0e7e299970..873c14ef9b 100644 --- a/shared/src/ledger/storage_api/mod.rs +++ b/shared/src/ledger/storage_api/mod.rs @@ -37,7 +37,16 @@ pub trait StorageRead<'iter> { 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>>; @@ -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( diff --git a/tx_prelude/src/lib.rs b/tx_prelude/src/lib.rs index 0f2f4dba39..5ba371a9de 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 30bf99f5f3..688c69eaee 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, diff --git a/wasm/checksums.json b/wasm/checksums.json index d5649ffd81..bba53aaaee 100644 --- a/wasm/checksums.json +++ b/wasm/checksums.json @@ -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" } \ No newline at end of file