Skip to content

Commit

Permalink
[types] Some useful function for type convert and add `get_state_node…
Browse files Browse the repository at this point in the history
…_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 <chenkaihong>
  • Loading branch information
strawWoo authored Aug 12, 2022
1 parent 95e146e commit a184307
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
83 changes: 83 additions & 0 deletions rpc/api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,36 @@ impl From<BlockHeader> for BlockHeaderView {
}
}

impl From<BlockHeaderView> 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<BlockHeaderView> for Vec<BlockHeader> {
fn from_iter<T: IntoIterator<Item = BlockHeaderView>>(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.
Expand Down Expand Up @@ -496,6 +526,21 @@ impl TryFrom<RawUserTransaction> for RawUserTransactionView {
}
}

impl From<RawUserTransactionView> 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.
Expand Down Expand Up @@ -738,6 +783,25 @@ impl TryFrom<Vec<SignedUserTransaction>> for BlockTransactionsView {
}
}

impl TryFrom<BlockTransactionsView> for Vec<SignedUserTransaction> {
type Error = anyhow::Error;

fn try_from(tx_view: BlockTransactionsView) -> Result<Self, Self::Error> {
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<Vec<HashValue>> for BlockTransactionsView {
fn from(txns: Vec<HashValue>) -> Self {
BlockTransactionsView::Hashes(txns)
Expand Down Expand Up @@ -811,6 +875,25 @@ impl TryFrom<Block> for BlockView {
}
}

impl TryFrom<BlockView> for Block {
type Error = anyhow::Error;

fn try_from(block_view: BlockView) -> Result<Self, Self::Error> {
let block_header: BlockHeader = block_view.header.into();
let uncles: Vec<BlockHeader> = 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,
Expand Down
8 changes: 8 additions & 0 deletions rpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,14 @@ impl RpcClient {
.map_err(map_err)
}

pub fn get_state_node_by_node_hash(
&self,
key_hash: HashValue,
) -> anyhow::Result<Option<Vec<u8>>> {
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<Vec<DecodedMoveValue>> {
self.call_rpc_blocking(|inner| inner.contract_client.call_v2(call))
.map_err(map_err)
Expand Down

0 comments on commit a184307

Please sign in to comment.