Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[types] Some useful function for type convert and add get_state_node_by_node_hash to RPCClient #3643

Merged
merged 6 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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