diff --git a/crates/mem-pool/src/sync/mq/gw_kafka.rs b/crates/mem-pool/src/sync/mq/gw_kafka.rs index 84e4f0e2c..24ac8a1d0 100644 --- a/crates/mem-pool/src/sync/mq/gw_kafka.rs +++ b/crates/mem-pool/src/sync/mq/gw_kafka.rs @@ -3,7 +3,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use anyhow::Result; use async_trait::async_trait; use gw_types::{ - packed::{RefreshMemBlockMessage, RefreshMemBlockMessageUnion}, + packed::{RefreshMemBlockMessage, RefreshMemBlockMessageReader, RefreshMemBlockMessageUnion}, prelude::{Builder, Entity, Reader}, }; use rdkafka::{ @@ -134,8 +134,7 @@ impl Consume for Consumer { ); if let Some(payload) = payload { - let refresh_msg = RefreshMemBlockMessage::from_slice(payload)?; - let reader = refresh_msg.as_reader(); + let reader = RefreshMemBlockMessageReader::from_slice(payload)?; let refresh_msg = reader.to_enum(); match &refresh_msg { gw_types::packed::RefreshMemBlockMessageUnionReader::NextL2Transaction( diff --git a/crates/store/src/mem_pool_state.rs b/crates/store/src/mem_pool_state.rs index 823ebb5fe..265dab0b1 100644 --- a/crates/store/src/mem_pool_state.rs +++ b/crates/store/src/mem_pool_state.rs @@ -14,8 +14,8 @@ use gw_db::{ schema::{Col, COLUMNS, COLUMN_ACCOUNT_SMT_BRANCH, COLUMN_ACCOUNT_SMT_LEAF, COLUMN_META}, }; use gw_types::{ - packed, - prelude::{Entity, FromSliceShouldBeOk, Pack, Reader, Unpack}, + from_box_should_be_ok, packed, + prelude::{Entity, FromSliceShouldBeOk, Pack, Unpack}, }; use crate::{ @@ -115,11 +115,9 @@ impl MemStore { pub fn get_mem_block_account_count(&self) -> Result, Error> { match self.get(COLUMN_META, META_MEM_SMT_COUNT_KEY) { - Some(slice) => { - let count = - packed::Uint32Reader::from_slice_should_be_ok(slice.as_ref()).to_entity(); - Ok(Some(count.unpack())) - } + Some(slice) => Ok(Some( + packed::Uint32Reader::from_slice_should_be_ok(&slice).unpack(), + )), None => Ok(None), } } @@ -144,9 +142,7 @@ impl MemStore { pub fn get_mem_pool_block_info(&self) -> Result, Error> { match self.get(COLUMN_META, META_MEM_BLOCK_INFO) { - Some(slice) => Ok(Some( - packed::BlockInfoReader::from_slice_should_be_ok(slice.as_ref()).to_entity(), - )), + Some(slice) => Ok(Some(from_box_should_be_ok!(packed::BlockInfoReader, slice))), None => Ok(None), } } diff --git a/crates/store/src/state/mem_state_db.rs b/crates/store/src/state/mem_state_db.rs index 98091ceb1..fe9e23f48 100644 --- a/crates/store/src/state/mem_state_db.rs +++ b/crates/store/src/state/mem_state_db.rs @@ -7,6 +7,7 @@ use anyhow::Result; use gw_common::{error::Error as StateError, smt::SMT, state::State, H256}; use gw_db::schema::{COLUMN_DATA, COLUMN_SCRIPT, COLUMN_SCRIPT_PREFIX}; use gw_traits::CodeStore; +use gw_types::from_box_should_be_ok; use gw_types::{ bytes::Bytes, packed::{self, AccountMerkleState}, @@ -109,7 +110,7 @@ impl<'a> CodeStore for MemStateTree<'a> { self.db() .get(COLUMN_SCRIPT, script_hash.as_slice()) .or_else(|| self.db().get(COLUMN_SCRIPT, script_hash.as_slice())) - .map(|slice| packed::ScriptReader::from_slice_should_be_ok(slice.as_ref()).to_entity()) + .map(|slice| from_box_should_be_ok!(packed::ScriptReader, slice)) } fn get_script_hash_by_short_script_hash(&self, script_hash_prefix: &[u8]) -> Option { diff --git a/crates/store/src/state/state_db.rs b/crates/store/src/state/state_db.rs index fd813f66f..a384b2ef6 100644 --- a/crates/store/src/state/state_db.rs +++ b/crates/store/src/state/state_db.rs @@ -11,6 +11,7 @@ use gw_db::schema::{COLUMN_DATA, COLUMN_SCRIPT, COLUMN_SCRIPT_PREFIX}; use gw_traits::CodeStore; use gw_types::{ bytes::Bytes, + from_box_should_be_ok, packed::{self, AccountMerkleState, Byte32}, prelude::*, }; @@ -236,7 +237,7 @@ impl<'a> CodeStore for StateTree<'a> { fn get_script(&self, script_hash: &H256) -> Option { self.db() .get(COLUMN_SCRIPT, script_hash.as_slice()) - .map(|slice| packed::ScriptReader::from_slice_should_be_ok(slice.as_ref()).to_entity()) + .map(|slice| from_box_should_be_ok!(packed::ScriptReader, slice)) } fn get_script_hash_by_short_script_hash(&self, script_hash_prefix: &[u8]) -> Option { diff --git a/crates/store/src/traits/chain_store.rs b/crates/store/src/traits/chain_store.rs index ba913949f..c63abebe3 100644 --- a/crates/store/src/traits/chain_store.rs +++ b/crates/store/src/traits/chain_store.rs @@ -17,6 +17,7 @@ use gw_db::schema::{ use gw_types::offchain::global_state_from_slice; use gw_types::packed::{Script, WithdrawalKey}; use gw_types::{ + from_box_should_be_ok, packed::{self, ChallengeTarget, TransactionKey}, prelude::*, }; @@ -70,7 +71,7 @@ pub trait ChainStore: KVStoreRead { .get(COLUMN_META, META_LAST_VALID_TIP_BLOCK_HASH_KEY) .expect("get last valid tip block hash"); - let byte32 = packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()).to_entity(); + let byte32 = packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()); Ok(byte32.unpack()) } @@ -78,11 +79,7 @@ pub trait ChainStore: KVStoreRead { let slice = self .get(COLUMN_META, META_TIP_BLOCK_HASH_KEY) .expect("get tip block hash"); - Ok( - packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()) - .to_entity() - .unpack(), - ) + Ok(packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()).unpack()) } fn get_tip_block(&self) -> Result { @@ -94,9 +91,7 @@ pub trait ChainStore: KVStoreRead { let block_number: packed::Uint64 = number.pack(); match self.get(COLUMN_INDEX, block_number.as_slice()) { Some(slice) => Ok(Some( - packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()) - .to_entity() - .unpack(), + packed::Byte32Reader::from_slice_should_be_ok(slice.as_ref()).unpack(), )), None => Ok(None), } @@ -105,9 +100,7 @@ pub trait ChainStore: KVStoreRead { fn get_block_number(&self, block_hash: &H256) -> Result, Error> { match self.get(COLUMN_INDEX, block_hash.as_slice()) { Some(slice) => Ok(Some( - packed::Uint64Reader::from_slice_should_be_ok(slice.as_ref()) - .to_entity() - .unpack(), + packed::Uint64Reader::from_slice_should_be_ok(slice.as_ref()).unpack(), )), None => Ok(None), } @@ -115,9 +108,7 @@ pub trait ChainStore: KVStoreRead { fn get_block(&self, block_hash: &H256) -> Result, Error> { match self.get(COLUMN_BLOCK, block_hash.as_slice()) { - Some(slice) => Ok(Some( - packed::L2BlockReader::from_slice_should_be_ok(slice.as_ref()).to_entity(), - )), + Some(slice) => Ok(Some(from_box_should_be_ok!(packed::L2BlockReader, slice))), None => Ok(None), } } @@ -135,9 +126,7 @@ pub trait ChainStore: KVStoreRead { ) -> Result, Error> { let tx_info_opt = self .get(COLUMN_TRANSACTION_INFO, tx_hash.as_slice()) - .map(|slice| { - packed::TransactionInfoReader::from_slice_should_be_ok(slice.as_ref()).to_entity() - }); + .map(|slice| from_box_should_be_ok!(packed::TransactionInfoReader, slice)); Ok(tx_info_opt) } @@ -147,15 +136,12 @@ pub trait ChainStore: KVStoreRead { ) -> Result, Error> { Ok(self .get(COLUMN_TRANSACTION, tx_key.as_slice()) - .map(|slice| { - packed::L2TransactionReader::from_slice_should_be_ok(slice.as_ref()).to_entity() - })) + .map(|slice| from_box_should_be_ok!(packed::L2TransactionReader, slice))) } fn get_transaction_receipt(&self, tx_hash: &H256) -> Result, Error> { if let Some(slice) = self.get(COLUMN_TRANSACTION_INFO, tx_hash.as_slice()) { - let info = - packed::TransactionInfoReader::from_slice_should_be_ok(slice.as_ref()).to_entity(); + let info = from_box_should_be_ok!(packed::TransactionInfoReader, slice); let tx_key = info.key(); self.get_transaction_receipt_by_key(&tx_key) } else { @@ -169,9 +155,7 @@ pub trait ChainStore: KVStoreRead { ) -> Result, Error> { Ok(self .get(COLUMN_TRANSACTION_RECEIPT, key.as_slice()) - .map(|slice| { - packed::TxReceiptReader::from_slice_should_be_ok(slice.as_ref()).to_entity() - })) + .map(|slice| from_box_should_be_ok!(packed::TxReceiptReader, slice))) } fn get_withdrawal( @@ -190,9 +174,7 @@ pub trait ChainStore: KVStoreRead { ) -> Result, Error> { let withdrawal_info_opt = self .get(COLUMN_WITHDRAWAL_INFO, withdrawal_hash.as_slice()) - .map(|slice| { - packed::WithdrawalInfoReader::from_slice_should_be_ok(slice.as_ref()).to_entity() - }); + .map(|slice| from_box_should_be_ok!(packed::WithdrawalInfoReader, slice)); Ok(withdrawal_info_opt) } @@ -202,10 +184,7 @@ pub trait ChainStore: KVStoreRead { ) -> Result, Error> { Ok(self .get(COLUMN_WITHDRAWAL, withdrawal_key.as_slice()) - .map(|slice| { - packed::WithdrawalRequestExtraReader::from_slice_should_be_ok(slice.as_ref()) - .to_entity() - })) + .map(|slice| from_box_should_be_ok!(packed::WithdrawalRequestExtraReader, slice))) } fn get_l2block_committed_info( @@ -213,10 +192,10 @@ pub trait ChainStore: KVStoreRead { block_hash: &H256, ) -> Result, Error> { match self.get(COLUMN_L2BLOCK_COMMITTED_INFO, block_hash.as_slice()) { - Some(slice) => Ok(Some( - packed::L2BlockCommittedInfoReader::from_slice_should_be_ok(slice.as_ref()) - .to_entity(), - )), + Some(slice) => Ok(Some(from_box_should_be_ok!( + packed::L2BlockCommittedInfoReader, + slice + ))), None => Ok(None), } } @@ -227,8 +206,7 @@ pub trait ChainStore: KVStoreRead { ) -> Result>, Error> { match self.get(COLUMN_BLOCK_DEPOSIT_REQUESTS, block_hash.as_slice()) { Some(slice) => Ok(Some( - packed::DepositRequestVecReader::from_slice_should_be_ok(slice.as_ref()) - .to_entity() + from_box_should_be_ok!(packed::DepositRequestVecReader, slice) .into_iter() .collect(), )), @@ -271,7 +249,7 @@ pub trait ChainStore: KVStoreRead { ) { Some(slice) => { let block_hash = packed::Byte32VecReader::from_slice_should_be_ok(slice.as_ref()); - Ok(Some(block_hash.to_entity().unpack())) + Ok(Some(block_hash.unpack())) } None => Ok(None), } @@ -279,9 +257,7 @@ pub trait ChainStore: KVStoreRead { fn get_asset_script(&self, script_hash: &H256) -> Result, Error> { match self.get(COLUMN_ASSET_SCRIPT, script_hash.as_slice()) { - Some(slice) => Ok(Some( - packed::ScriptReader::from_slice_should_be_ok(slice.as_ref()).to_entity(), - )), + Some(slice) => Ok(Some(from_box_should_be_ok!(packed::ScriptReader, slice))), None => Ok(None), } } @@ -292,9 +268,7 @@ pub trait ChainStore: KVStoreRead { ) -> Result, Error> { Ok(self .get(COLUMN_MEM_POOL_TRANSACTION, tx_hash.as_slice()) - .map(|slice| { - packed::L2TransactionReader::from_slice_should_be_ok(slice.as_ref()).to_entity() - })) + .map(|slice| from_box_should_be_ok!(packed::L2TransactionReader, slice))) } fn get_mem_pool_transaction_receipt( @@ -303,9 +277,7 @@ pub trait ChainStore: KVStoreRead { ) -> Result, Error> { Ok(self .get(COLUMN_MEM_POOL_TRANSACTION_RECEIPT, tx_hash.as_slice()) - .map(|slice| { - packed::TxReceiptReader::from_slice_should_be_ok(slice.as_ref()).to_entity() - })) + .map(|slice| from_box_should_be_ok!(packed::TxReceiptReader, slice))) } fn get_mem_pool_withdrawal( diff --git a/crates/store/src/transaction/store_transaction.rs b/crates/store/src/transaction/store_transaction.rs index 1da24c1ad..dfc24ec60 100644 --- a/crates/store/src/transaction/store_transaction.rs +++ b/crates/store/src/transaction/store_transaction.rs @@ -240,10 +240,9 @@ impl StoreTransaction { // TODO: prune db state pub fn get_reverted_block_hashes(&self) -> Result, Error> { let iter = self.get_iter(COLUMN_REVERTED_BLOCK_SMT_LEAF, IteratorMode::End); - let to_byte32 = iter.map(|(key, _value)| { - packed::Byte32Reader::from_slice_should_be_ok(key.as_ref()).to_entity() + let to_h256 = iter.map(|(key, _value)| { + packed::Byte32Reader::from_slice_should_be_ok(key.as_ref()).unpack() }); - let to_h256 = to_byte32.map(|byte32| byte32.unpack()); Ok(to_h256.collect()) } diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 73ea1da3c..c82bfebb7 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -29,3 +29,11 @@ cfg_if::cfg_if! { use alloc::string; } } + +#[macro_export] +macro_rules! from_box_should_be_ok { + ($r:ty, $b:ident) => {{ + <$r>::from_slice_should_be_ok(&$b); + <$r as gw_types::prelude::Reader>::Entity::new_unchecked($b.into()) + }}; +}