Skip to content

Commit

Permalink
Address review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
khorolets committed Apr 11, 2021
1 parent d6e2250 commit aba2a29
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
28 changes: 4 additions & 24 deletions chain/jsonrpc-primitives/src/types/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use near_primitives::borsh::BorshDeserialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Clone)]
pub struct RpcTransactionRequest {
pub struct RpcBroadcastTransactionRequest {
pub signed_transaction: near_primitives::transaction::SignedTransaction,
}

Expand Down Expand Up @@ -55,17 +54,9 @@ pub struct RpcBroadcastTxSyncResponse {
pub is_routed: bool,
}

impl RpcTransactionRequest {
impl RpcBroadcastTransactionRequest {
pub fn parse(value: Option<Value>) -> Result<Self, crate::errors::RpcParseError> {
// TODO: move this code to crate::utils::parse_signed_transaction
let (encoded,) = crate::utils::parse_params::<(String,)>(value.clone())?;
let bytes = crate::utils::from_base64_or_parse_err(encoded)?;
let signed_transaction = near_primitives::transaction::SignedTransaction::try_from_slice(
&bytes,
)
.map_err(|err| {
crate::errors::RpcParseError(format!("Failed to decode transaction: {}", err))
})?;
let signed_transaction = crate::utils::parse_signed_transaction(value)?;
Ok(Self { signed_transaction })
}
}
Expand All @@ -84,18 +75,7 @@ impl RpcTransactionStatusCommonRequest {
let transaction_info = TransactionInfo::TransactionId { hash, account_id };
Ok(Self { transaction_info })
} else {
// TODO: move this code to crate::utils::parse_signed_transaction
let (encoded,) = crate::utils::parse_params::<(String,)>(value.clone())?;
let bytes = crate::utils::from_base64_or_parse_err(encoded)?;
let signed_transaction =
near_primitives::transaction::SignedTransaction::try_from_slice(&bytes).map_err(
|err| {
crate::errors::RpcParseError(format!(
"Failed to decode transaction: {}",
err
))
},
)?;
let signed_transaction = crate::utils::parse_signed_transaction(value)?;
let transaction_info = TransactionInfo::Transaction(signed_transaction);
Ok(Self { transaction_info })
}
Expand Down
16 changes: 13 additions & 3 deletions chain/jsonrpc-primitives/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::de::DeserializeOwned;
use serde_json::Value;

use near_primitives::borsh::BorshDeserialize;

pub(crate) fn parse_params<T: DeserializeOwned>(
value: Option<Value>,
) -> Result<T, crate::errors::RpcParseError> {
Expand All @@ -12,9 +14,17 @@ pub(crate) fn parse_params<T: DeserializeOwned>(
}
}

pub(crate) fn from_base64_or_parse_err(
encoded: String,
) -> Result<Vec<u8>, crate::errors::RpcParseError> {
fn from_base64_or_parse_err(encoded: String) -> Result<Vec<u8>, crate::errors::RpcParseError> {
near_primitives_core::serialize::from_base64(&encoded)
.map_err(|err| crate::errors::RpcParseError(err.to_string()))
}

pub(crate) fn parse_signed_transaction(
value: Option<Value>,
) -> Result<near_primitives::transaction::SignedTransaction, crate::errors::RpcParseError> {
let (encoded,) = crate::utils::parse_params::<(String,)>(value.clone())?;
let bytes = crate::utils::from_base64_or_parse_err(encoded)?;
Ok(near_primitives::transaction::SignedTransaction::try_from_slice(&bytes).map_err(|err| {
crate::errors::RpcParseError(format!("Failed to decode transaction: {}", err))
})?)
}
16 changes: 8 additions & 8 deletions chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl JsonRpcHandler {
}
"broadcast_tx_async" => {
let rpc_transaction_request =
near_jsonrpc_primitives::types::transactions::RpcTransactionRequest::parse(
near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest::parse(
request.params,
)?;
let transaction_hash = self.send_tx_async(rpc_transaction_request).await;
Expand All @@ -253,7 +253,7 @@ impl JsonRpcHandler {
}
"broadcast_tx_commit" => {
let rpc_transaction_request =
near_jsonrpc_primitives::types::transactions::RpcTransactionRequest::parse(
near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest::parse(
request.params,
)?;
let send_tx_response = self.send_tx_commit(rpc_transaction_request).await?;
Expand All @@ -268,7 +268,7 @@ impl JsonRpcHandler {
}
"EXPERIMENTAL_broadcast_tx_sync" => {
let rpc_transaction_request =
near_jsonrpc_primitives::types::transactions::RpcTransactionRequest::parse(
near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest::parse(
request.params,
)?;
let broadcast_tx_sync_response = self.send_tx_sync(rpc_transaction_request).await?;
Expand All @@ -279,7 +279,7 @@ impl JsonRpcHandler {
"EXPERIMENTAL_changes_in_block" => self.changes_in_block(request.params).await,
"EXPERIMENTAL_check_tx" => {
let rpc_transaction_request =
near_jsonrpc_primitives::types::transactions::RpcTransactionRequest::parse(
near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest::parse(
request.params,
)?;
let broadcast_tx_sync_response = self.check_tx(rpc_transaction_request).await?;
Expand Down Expand Up @@ -365,7 +365,7 @@ impl JsonRpcHandler {

async fn send_tx_async(
&self,
request_data: near_jsonrpc_primitives::types::transactions::RpcTransactionRequest,
request_data: near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest,
) -> CryptoHash {
let tx = request_data.signed_transaction;
let hash = tx.get_hash().clone();
Expand Down Expand Up @@ -553,7 +553,7 @@ impl JsonRpcHandler {

async fn send_tx_sync(
&self,
request_data: near_jsonrpc_primitives::types::transactions::RpcTransactionRequest,
request_data: near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest,
) -> Result<
near_jsonrpc_primitives::types::transactions::RpcBroadcastTxSyncResponse,
near_jsonrpc_primitives::types::transactions::RpcTransactionError,
Expand All @@ -578,7 +578,7 @@ impl JsonRpcHandler {

async fn check_tx(
&self,
request_data: near_jsonrpc_primitives::types::transactions::RpcTransactionRequest,
request_data: near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest,
) -> Result<
near_jsonrpc_primitives::types::transactions::RpcBroadcastTxSyncResponse,
near_jsonrpc_primitives::types::transactions::RpcTransactionError,
Expand All @@ -603,7 +603,7 @@ impl JsonRpcHandler {

async fn send_tx_commit(
&self,
request_data: near_jsonrpc_primitives::types::transactions::RpcTransactionRequest,
request_data: near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest,
) -> Result<
near_jsonrpc_primitives::types::transactions::RpcTransactionResponse,
near_jsonrpc_primitives::types::transactions::RpcTransactionError,
Expand Down

0 comments on commit aba2a29

Please sign in to comment.