-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(jsonrpc): Refactor broadcast_tx jsonrpc handler to add struc…
…tured errors (#4180) * Refactor broadcast_tx jsonrpc handler to add structurred errors * Remove wip comments * Refactor tx_status_common, move impl From<NetworkClientResponses> to jsonrpc to avoid near_network dependency in jsonrpc-primitives * Return error in case of NetworkClientResponses::RequestRouted, return same result on check_tx and send_tx_sync * Make backward compatible error messages to refactored methods. Make corresponding tests pass * Move backward compatibility to jsonrpc-primitives from jsonrpc, move ServerError from jsonrpc to jsonrpc-primitives, fix dependencies * Address review suggestions * Address review suggestions. Update jsonrpc changelog, bump version * Address review suggestions
- Loading branch information
Showing
20 changed files
with
438 additions
and
204 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ pub mod config; | |
pub mod gas_price; | ||
pub mod query; | ||
pub mod receipts; | ||
pub mod transactions; | ||
pub mod validator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct RpcBroadcastTransactionRequest { | ||
pub signed_transaction: near_primitives::transaction::SignedTransaction, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct RpcTransactionStatusCommonRequest { | ||
pub transaction_info: TransactionInfo, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum TransactionInfo { | ||
Transaction(near_primitives::transaction::SignedTransaction), | ||
TransactionId { | ||
hash: near_primitives::hash::CryptoHash, | ||
account_id: near_primitives::types::AccountId, | ||
}, | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum RpcTransactionError { | ||
#[error("An error happened during transaction execution: {context:?}")] | ||
InvalidTransaction { context: near_primitives::errors::InvalidTxError }, | ||
#[error("Node doesn't track this shard. Cannot determine whether the transaction is valid")] | ||
DoesNotTrackShard, | ||
#[error("Transaction with hash {transaction_hash} was routed")] | ||
RequestRouted { transaction_hash: String }, | ||
#[error("Transaction {requested_transaction_hash} doesn't exist")] | ||
UnknownTransaction { requested_transaction_hash: near_primitives::hash::CryptoHash }, | ||
#[error("The node reached its limits. Try again later. More details: {debug_info}")] | ||
InternalError { debug_info: String }, | ||
#[error("Timeout")] | ||
TimeoutError, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct RpcTransactionResponse { | ||
#[serde(flatten)] | ||
pub final_execution_outcome: near_primitives::views::FinalExecutionOutcomeViewEnum, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct RpcBroadcastTxSyncResponse { | ||
pub transaction_hash: String, | ||
} | ||
|
||
impl RpcBroadcastTransactionRequest { | ||
pub fn parse(value: Option<Value>) -> Result<Self, crate::errors::RpcParseError> { | ||
let signed_transaction = crate::utils::parse_signed_transaction(value)?; | ||
Ok(Self { signed_transaction }) | ||
} | ||
} | ||
|
||
impl RpcTransactionStatusCommonRequest { | ||
pub fn parse(value: Option<Value>) -> Result<Self, crate::errors::RpcParseError> { | ||
if let Ok((hash, account_id)) = | ||
crate::utils::parse_params::<(near_primitives::hash::CryptoHash, String)>(value.clone()) | ||
{ | ||
if !near_runtime_utils::is_valid_account_id(&account_id) { | ||
return Err(crate::errors::RpcParseError(format!( | ||
"Invalid account id: {}", | ||
account_id | ||
))); | ||
} | ||
let transaction_info = TransactionInfo::TransactionId { hash, account_id }; | ||
Ok(Self { transaction_info }) | ||
} else { | ||
let signed_transaction = crate::utils::parse_signed_transaction(value)?; | ||
let transaction_info = TransactionInfo::Transaction(signed_transaction); | ||
Ok(Self { transaction_info }) | ||
} | ||
} | ||
} | ||
|
||
impl From<near_client_primitives::types::TxStatusError> for RpcTransactionError { | ||
fn from(error: near_client_primitives::types::TxStatusError) -> Self { | ||
match error { | ||
near_client_primitives::types::TxStatusError::ChainError(err) => { | ||
Self::InternalError { debug_info: format!("{:?}", err) } | ||
} | ||
near_client_primitives::types::TxStatusError::MissingTransaction( | ||
requested_transaction_hash, | ||
) => Self::UnknownTransaction { requested_transaction_hash }, | ||
near_client_primitives::types::TxStatusError::InvalidTx(context) => { | ||
Self::InvalidTransaction { context } | ||
} | ||
near_client_primitives::types::TxStatusError::InternalError(debug_info) => { | ||
Self::InternalError { debug_info } | ||
} | ||
near_client_primitives::types::TxStatusError::TimeoutError => Self::TimeoutError, | ||
} | ||
} | ||
} | ||
|
||
impl From<near_primitives::views::FinalExecutionOutcomeViewEnum> for RpcTransactionResponse { | ||
fn from( | ||
final_execution_outcome: near_primitives::views::FinalExecutionOutcomeViewEnum, | ||
) -> Self { | ||
Self { final_execution_outcome } | ||
} | ||
} | ||
|
||
impl From<RpcTransactionError> for crate::errors::RpcError { | ||
fn from(error: RpcTransactionError) -> Self { | ||
let error_data = match error { | ||
RpcTransactionError::InvalidTransaction { ref context } => Some( | ||
serde_json::to_value(crate::errors::ServerError::TxExecutionError( | ||
near_primitives::errors::TxExecutionError::InvalidTxError(context.clone()), | ||
)) | ||
.unwrap_or(Value::String(error.to_string())), | ||
), | ||
_ => Some(Value::String(error.to_string())), | ||
}; | ||
Self::new(-32_000, "Server error".to_string(), error_data) | ||
} | ||
} | ||
|
||
impl From<actix::MailboxError> for RpcTransactionError { | ||
fn from(error: actix::MailboxError) -> Self { | ||
Self::InternalError { debug_info: error.to_string() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
[package] | ||
name = "near-jsonrpc" | ||
version = "0.2.0" | ||
version = "0.2.1" | ||
authors = ["Near Inc <[email protected]>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
actix = "0.11.0-beta.2" | ||
actix-web = "4.0.0-beta.3" | ||
actix-cors = { git = "https://github.com/near/actix-extras.git", branch="actix-web-4-beta.3" } | ||
easy-ext = "0.2" | ||
tokio = { version = "1.1", features = ["full"] } | ||
futures = "0.3" | ||
lazy_static = "1.4" | ||
|
@@ -27,7 +28,6 @@ near-network = { path = "../network" } | |
near-jsonrpc-client = { path = "client" } | ||
near-jsonrpc-primitives = { path = "../jsonrpc-primitives" } | ||
near-rpc-error-macro = { path = "../../tools/rpctypegen/macro" } | ||
near-runtime-utils = { path = "../../runtime/near-runtime-utils" } | ||
near-performance-metrics = { path = "../../utils/near-performance-metrics" } | ||
|
||
[dev-dependencies] | ||
|
Oops, something went wrong.