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 11, 2023
1 parent c208642 commit 2b7de2f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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.

48 changes: 48 additions & 0 deletions chain/jsonrpc/src/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,51 @@ fn decode_signed_transaction(value: Value) -> Result<SignedTransaction, RpcParse
SignedTransaction::try_from_slice(&bytes)
.map_err(|err| RpcParseError(format!("Failed to decode transaction: {}", err)))
}

#[cfg(test)]
mod tests {
use crate::api::RpcRequest;
use near_jsonrpc_primitives::types::transactions::{
RpcBroadcastTransactionRequest, RpcTransactionStatusCommonRequest,
};
use near_primitives::borsh::BorshSerialize;
use near_primitives::hash::CryptoHash;
use near_primitives::serialize::to_base64;
use near_primitives::transaction::SignedTransaction;

#[test]
fn test_serialize_tx_status_params_as_vec() {
let tx_hash = CryptoHash::new().to_string();
let account_id = "sender.testnet";
let params = serde_json::json!([tx_hash, 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 = 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());
}

// The params are invalid because sender_account_id is missing
#[test]
fn test_serialize_invalid_tx_status_params() {
let tx_hash = CryptoHash::new().to_string();
let params = serde_json::json!([tx_hash]);
assert!(RpcTransactionStatusCommonRequest::parse(params).is_err());
}

#[test]
fn test_serialize_send_tx_params_as_binary_signed_tx() {
let tx_hash = CryptoHash::new();
let tx = SignedTransaction::empty(tx_hash);
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());
}
}

0 comments on commit 2b7de2f

Please sign in to comment.