Skip to content

Commit

Permalink
jsonrpc: add tx serialisation tests (#9653)
Browse files Browse the repository at this point in the history
I mixed up adding tests and introducing new functionality here
#9648
Please have a look just at tests separately
  • Loading branch information
telezhnaya authored and nikurt committed Oct 23, 2023
1 parent 29cbdfa commit 33b0a7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 18 additions & 22 deletions chain/jsonrpc/src/api/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use serde_json::Value;
use serde_with::base64::Base64;
use serde_with::serde_as;

use near_client_primitives::types::TxStatusError;
use near_jsonrpc_primitives::errors::RpcParseError;
Expand All @@ -13,20 +15,19 @@ use super::{Params, RpcFrom, RpcRequest};

impl RpcRequest for RpcBroadcastTransactionRequest {
fn parse(value: Value) -> Result<Self, RpcParseError> {
let signed_transaction =
Params::new(value).try_singleton(|value| decode_signed_transaction(value)).unwrap()?;
let signed_transaction = decode_signed_transaction(value)?;
Ok(Self { signed_transaction })
}
}

impl RpcRequest for RpcTransactionStatusCommonRequest {
fn parse(value: Value) -> Result<Self, RpcParseError> {
Ok(Params::new(value)
.try_singleton(|signed_tx| decode_signed_transaction(signed_tx).map(|x| x.into()))
.try_pair(|tx_hash, sender_account_id| {
Ok(TransactionInfo::TransactionId { tx_hash, sender_account_id }.into())
})
.unwrap_or_parse()?)
let transaction_info = Params::<TransactionInfo>::new(value)
.try_pair(|hash, account_id| Ok(TransactionInfo::TransactionId { hash, account_id }))
.unwrap_or_else(|value| {
decode_signed_transaction(value).map(TransactionInfo::Transaction)
})?;
Ok(Self { transaction_info })
}
}

Expand All @@ -51,9 +52,12 @@ impl RpcFrom<TxStatusError> for RpcTransactionError {
}
}

fn decode_signed_transaction(value: String) -> Result<SignedTransaction, RpcParseError> {
let bytes = near_primitives::serialize::from_base64(&value)
.map_err(|err| RpcParseError(format!("Failed to decode transaction: {}", err)))?;
fn decode_signed_transaction(value: Value) -> Result<SignedTransaction, RpcParseError> {
#[serde_as]
#[derive(serde::Deserialize)]
struct Payload(#[serde_as(as = "(Base64,)")] (Vec<u8>,));

let Payload((bytes,)) = Params::<Payload>::parse(value)?;
SignedTransaction::try_from_slice(&bytes)
.map_err(|err| RpcParseError(format!("Failed to decode transaction: {}", err)))
}
Expand All @@ -64,7 +68,7 @@ mod tests {
use near_jsonrpc_primitives::types::transactions::{
RpcBroadcastTransactionRequest, RpcTransactionStatusCommonRequest,
};
use near_primitives::borsh;
use near_primitives::borsh::BorshSerialize;
use near_primitives::hash::CryptoHash;
use near_primitives::serialize::to_base64;
use near_primitives::transaction::SignedTransaction;
Expand All @@ -77,19 +81,11 @@ mod tests {
assert!(RpcTransactionStatusCommonRequest::parse(params).is_ok());
}

#[test]
fn test_serialize_tx_status_params_as_object() {
let tx_hash = CryptoHash::new().to_string();
let account_id = "sender.testnet";
let params = serde_json::json!({"tx_hash": tx_hash, "sender_account_id": account_id});
assert!(RpcTransactionStatusCommonRequest::parse(params).is_ok());
}

#[test]
fn test_serialize_tx_status_params_as_binary_signed_tx() {
let tx_hash = CryptoHash::new();
let tx = SignedTransaction::empty(tx_hash);
let bytes_tx = borsh::to_vec(&tx).unwrap();
let bytes_tx = tx.try_to_vec().unwrap();
let str_tx = to_base64(&bytes_tx);
let params = serde_json::json!([str_tx]);
assert!(RpcTransactionStatusCommonRequest::parse(params).is_ok());
Expand All @@ -107,7 +103,7 @@ mod tests {
fn test_serialize_send_tx_params_as_binary_signed_tx() {
let tx_hash = CryptoHash::new();
let tx = SignedTransaction::empty(tx_hash);
let bytes_tx = borsh::to_vec(&tx).unwrap();
let bytes_tx = tx.try_to_vec().unwrap();
let str_tx = to_base64(&bytes_tx);
let params = serde_json::json!([str_tx]);
assert!(RpcBroadcastTransactionRequest::parse(params).is_ok());
Expand Down

0 comments on commit 33b0a7d

Please sign in to comment.