Skip to content

Commit

Permalink
Add test to check that broadcast_tx_sync returns transaction hash in …
Browse files Browse the repository at this point in the history
…String. Move all experimental json rpc methods to the end of the list
  • Loading branch information
khorolets committed Apr 19, 2021
1 parent 1c5fda7 commit afe80d2
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 62 deletions.
124 changes: 62 additions & 62 deletions chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,68 @@ impl JsonRpcHandler {
serde_json::to_value(chunk)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"gas_price" => {
let rpc_gas_price_request =
near_jsonrpc_primitives::types::gas_price::RpcGasPriceRequest::parse(
request.params,
)?;
let gas_price = self.gas_price(rpc_gas_price_request).await?;
serde_json::to_value(gas_price)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"health" => {
let health_response = self.health().await?;
serde_json::to_value(health_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"light_client_proof" => {
let rpc_light_client_execution_proof_request = near_jsonrpc_primitives::types::light_client::RpcLightClientExecutionProofRequest::parse(request.params)?;
let rpc_light_client_execution_proof_response = self
.light_client_execution_outcome_proof(rpc_light_client_execution_proof_request)
.await?;
serde_json::to_value(rpc_light_client_execution_proof_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"next_light_client_block" => {
let rpc_light_client_next_block_request = near_jsonrpc_primitives::types::light_client::RpcLightClientNextBlockRequest::parse(request.params)?;
let next_light_client_block =
self.next_light_client_block(rpc_light_client_next_block_request).await?;
serde_json::to_value(next_light_client_block)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"network_info" => {
let network_info_response = self.network_info().await?;
serde_json::to_value(network_info_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"query" => {
let rpc_query_request =
near_jsonrpc_primitives::types::query::RpcQueryRequest::parse(request.params)?;
let query_response = self.query(rpc_query_request).await;
process_query_response(query_response)
}
"status" => {
let status_response = self.status().await?;
serde_json::to_value(status_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"tx" => {
let rpc_transaction_status_common_request =
near_jsonrpc_primitives::types::transactions::RpcTransactionStatusCommonRequest::parse(request.params)?;
let rpc_transaction_response =
self.tx_status_common(rpc_transaction_status_common_request, false).await?;
serde_json::to_value(rpc_transaction_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"validators" => {
let rpc_validator_request =
near_jsonrpc_primitives::types::validator::RpcValidatorRequest::parse(
request.params,
)?;
let validator_info = self.validators(rpc_validator_request).await?;
serde_json::to_value(validator_info)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"EXPERIMENTAL_broadcast_tx_sync" => {
let rpc_transaction_request =
near_jsonrpc_primitives::types::transactions::RpcBroadcastTransactionRequest::parse(
Expand Down Expand Up @@ -348,68 +410,6 @@ impl JsonRpcHandler {
serde_json::to_value(validators)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"gas_price" => {
let rpc_gas_price_request =
near_jsonrpc_primitives::types::gas_price::RpcGasPriceRequest::parse(
request.params,
)?;
let gas_price = self.gas_price(rpc_gas_price_request).await?;
serde_json::to_value(gas_price)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"health" => {
let health_response = self.health().await?;
serde_json::to_value(health_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"light_client_proof" => {
let rpc_light_client_execution_proof_request = near_jsonrpc_primitives::types::light_client::RpcLightClientExecutionProofRequest::parse(request.params)?;
let rpc_light_client_execution_proof_response = self
.light_client_execution_outcome_proof(rpc_light_client_execution_proof_request)
.await?;
serde_json::to_value(rpc_light_client_execution_proof_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"next_light_client_block" => {
let rpc_light_client_next_block_request = near_jsonrpc_primitives::types::light_client::RpcLightClientNextBlockRequest::parse(request.params)?;
let next_light_client_block =
self.next_light_client_block(rpc_light_client_next_block_request).await?;
serde_json::to_value(next_light_client_block)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"network_info" => {
let network_info_response = self.network_info().await?;
serde_json::to_value(network_info_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"query" => {
let rpc_query_request =
near_jsonrpc_primitives::types::query::RpcQueryRequest::parse(request.params)?;
let query_response = self.query(rpc_query_request).await;
process_query_response(query_response)
}
"status" => {
let status_response = self.status().await?;
serde_json::to_value(status_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"tx" => {
let rpc_transaction_status_common_request =
near_jsonrpc_primitives::types::transactions::RpcTransactionStatusCommonRequest::parse(request.params)?;
let rpc_transaction_response =
self.tx_status_common(rpc_transaction_status_common_request, false).await?;
serde_json::to_value(rpc_transaction_response)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
"validators" => {
let rpc_validator_request =
near_jsonrpc_primitives::types::validator::RpcValidatorRequest::parse(
request.params,
)?;
let validator_info = self.validators(rpc_validator_request).await?;
serde_json::to_value(validator_info)
.map_err(|err| RpcError::serialization_error(err.to_string()))
}
_ => Err(RpcError::method_not_found(request.method.clone())),
};

Expand Down
51 changes: 51 additions & 0 deletions neard/tests/rpc_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,57 @@ fn test_tx_not_enough_balance_must_return_error() {
});
}

#[test]
fn test_send_tx_sync_returns_transaction_hash() {
init_integration_logger();
heavy_test(|| {
run_actix_until_stop(async move {
let num_nodes = 1;
let dirs = (0..num_nodes)
.map(|i| {
tempfile::Builder::new().prefix(&format!("tx_routed{}", i)).tempdir().unwrap()
})
.collect::<Vec<_>>();
let (genesis, rpc_addrs, clients) = start_nodes(1, &dirs, 1, 0, 10, 0);
let view_client = clients[0].1.clone();

let genesis_hash = *genesis_block(&genesis).hash();
let signer = InMemorySigner::from_seed("near.0", KeyType::ED25519, "near.0");
let transaction = SignedTransaction::send_money(
1,
"near.0".to_string(),
"near.0".to_string(),
&signer,
10000,
genesis_hash,
);

let client = new_client(&format!("http://{}", rpc_addrs[0]));
let tx_hash = transaction.get_hash();
let bytes = transaction.try_to_vec().unwrap();

actix::spawn(async move {
loop {
let res = view_client.send(GetBlock::latest()).await;
if let Ok(Ok(block)) = res {
if block.header.height > 10 {
let response = client
.EXPERIMENTAL_broadcast_tx_sync(to_base64(&bytes))
.map_err(|err| panic_on_rpc_error!(err))
.await
.unwrap();
assert_eq!(response["transaction_hash"], tx_hash.to_string());
actix::System::current().stop();
break;
}
}
sleep(std::time::Duration::from_millis(500)).await;
}
});
});
});
}

#[test]
fn test_send_tx_sync_to_lightclient_must_be_routed() {
init_integration_logger();
Expand Down

0 comments on commit afe80d2

Please sign in to comment.