From a184307e4ae3c0f9942935a6049f03b22819890d Mon Sep 17 00:00:00 2001 From: strawWoo Date: Fri, 12 Aug 2022 11:06:18 +0800 Subject: [PATCH] [types] Some useful function for type convert and add `get_state_node_by_node_hash` to RPCClient (#3643) * [api] add state node getter for rpc-client * [types] provide function from blockView to block * [api] add state node getter for rpc-client * [types] provide function from blockView to block Co-authored-by: chenkaihong --- rpc/api/src/types.rs | 83 +++++++++++++++++++++++++++++++++++++++++++ rpc/client/src/lib.rs | 8 +++++ 2 files changed, 91 insertions(+) diff --git a/rpc/api/src/types.rs b/rpc/api/src/types.rs index 81b70a058c..1da8490315 100644 --- a/rpc/api/src/types.rs +++ b/rpc/api/src/types.rs @@ -449,6 +449,36 @@ impl From for BlockHeaderView { } } +impl From for BlockHeader { + fn from(header_view: BlockHeaderView) -> Self { + BlockHeader::new( + header_view.parent_hash, + header_view.timestamp.0, + header_view.number.0, + header_view.author, + header_view.txn_accumulator_root, + header_view.block_accumulator_root, + header_view.state_root, + header_view.gas_used.0, + header_view.difficulty, + header_view.body_hash, + genesis_config::ChainId::new(header_view.chain_id), + header_view.nonce, + header_view.extra, + ) + } +} + +impl FromIterator for Vec { + fn from_iter>(views: T) -> Self { + let mut blocks = vec![]; + for view in views { + blocks.push(view.into()) + } + blocks + } +} + #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct RawUserTransactionView { /// Sender's address. @@ -496,6 +526,21 @@ impl TryFrom for RawUserTransactionView { } } +impl From for RawUserTransaction { + fn from(transaction_view: RawUserTransactionView) -> Self { + RawUserTransaction::new( + transaction_view.sender, + transaction_view.sequence_number.0, + TransactionPayload::decode(transaction_view.payload.0.as_slice()).unwrap(), + transaction_view.max_gas_amount.0, + transaction_view.gas_unit_price.0, + transaction_view.expiration_timestamp_secs.0, + genesis_config::ChainId::new(transaction_view.chain_id), + transaction_view.gas_token_code.clone(), + ) + } +} + #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)] pub enum TransactionPayloadView { /// A transaction that executes code. @@ -738,6 +783,25 @@ impl TryFrom> for BlockTransactionsView { } } +impl TryFrom for Vec { + type Error = anyhow::Error; + + fn try_from(tx_view: BlockTransactionsView) -> Result { + match tx_view { + BlockTransactionsView::Full(full) => Ok(full + .into_iter() + .map(|transaction_view| { + SignedUserTransaction::new( + transaction_view.raw_txn.into(), + transaction_view.authenticator, + ) + }) + .collect()), + _ => Err(anyhow::Error::msg("not support")), + } + } +} + impl From> for BlockTransactionsView { fn from(txns: Vec) -> Self { BlockTransactionsView::Hashes(txns) @@ -811,6 +875,25 @@ impl TryFrom for BlockView { } } +impl TryFrom for Block { + type Error = anyhow::Error; + + fn try_from(block_view: BlockView) -> Result { + let block_header: BlockHeader = block_view.header.into(); + let uncles: Vec = block_view + .uncles + .into_iter() + .map(BlockHeader::from) + .collect(); + let transactions = block_view.body.try_into()?; + + Ok(Block { + header: block_header, + body: BlockBody::new(transactions, Some(uncles)), + }) + } +} + #[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize, JsonSchema)] pub struct TransactionInfoView { pub block_hash: HashValue, diff --git a/rpc/client/src/lib.rs b/rpc/client/src/lib.rs index f1c8865eaf..599e0fa12c 100644 --- a/rpc/client/src/lib.rs +++ b/rpc/client/src/lib.rs @@ -638,6 +638,14 @@ impl RpcClient { .map_err(map_err) } + pub fn get_state_node_by_node_hash( + &self, + key_hash: HashValue, + ) -> anyhow::Result>> { + self.call_rpc_blocking(|inner| inner.state_client.get_state_node_by_node_hash(key_hash)) + .map_err(map_err) + } + pub fn contract_call(&self, call: ContractCall) -> anyhow::Result> { self.call_rpc_blocking(|inner| inner.contract_client.call_v2(call)) .map_err(map_err)