Skip to content

Commit

Permalink
chore(jsonrpc): move nearcore type conversions to server (#6902)
Browse files Browse the repository at this point in the history
Tracking issue: #6850

Move all conversions between `near-primitives` and `near-jsonrpc-primitives` to the server side. Freeing up a bunch of dependencies on `near-jsonrpc-primitives`.
  • Loading branch information
miraclx authored May 31, 2022
1 parent 0042d05 commit ac19a77
Show file tree
Hide file tree
Showing 23 changed files with 893 additions and 737 deletions.
7 changes: 1 addition & 6 deletions Cargo.lock

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

10 changes: 2 additions & 8 deletions chain/jsonrpc-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@ description = "This crate hosts structures for the NEAR JSON RPC Requests, Respo
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix = "0.13.0"
once_cell = "1.5.2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "1.0"
tracing = "0.1.13"
uuid = { version = "~0.8", features = ["v4"] }

near-chain-configs = { path = "../../core/chain-configs" }
near-client-primitives = { path = "../client-primitives" }
near-crypto = { path = "../../core/crypto" }
near-metrics = { path = "../../core/metrics" }
near-primitives = { path = "../../core/primitives" }
near-network-primitives = { path = "../../chain/network-primitives" }
near-primitives-core = { path = "../../core/primitives-core" }
near-chain-configs = { path = "../../core/chain-configs" }
near-rpc-error-macro = { path = "../../tools/rpctypegen/macro" }
near-client-primitives = { path = "../client-primitives" }

[features]
test_features = []
27 changes: 3 additions & 24 deletions chain/jsonrpc-primitives/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;
use serde::{Deserialize, Serialize};
use serde_json::{to_value, Value};

use near_primitives::errors::{InvalidTxError, TxExecutionError};
use near_primitives::errors::TxExecutionError;

#[derive(Serialize)]
pub struct RpcParseError(pub String);
Expand Down Expand Up @@ -151,14 +151,8 @@ impl fmt::Display for RpcError {
}
}

impl From<actix::MailboxError> for RpcError {
fn from(error: actix::MailboxError) -> Self {
Self::new(-32_000, "Server error".to_string(), Some(Value::String(error.to_string())))
}
}

impl From<crate::errors::RpcParseError> for RpcError {
fn from(parse_error: crate::errors::RpcParseError) -> Self {
impl From<RpcParseError> for RpcError {
fn from(parse_error: RpcParseError) -> Self {
Self::parse_error(parse_error.0)
}
}
Expand All @@ -173,21 +167,6 @@ impl fmt::Display for ServerError {
}
}

impl From<InvalidTxError> for ServerError {
fn from(e: InvalidTxError) -> ServerError {
ServerError::TxExecutionError(TxExecutionError::InvalidTxError(e))
}
}

impl From<actix::MailboxError> for ServerError {
fn from(e: actix::MailboxError) -> Self {
match e {
actix::MailboxError::Closed => ServerError::Closed,
actix::MailboxError::Timeout => ServerError::Timeout,
}
}
}

impl From<ServerError> for RpcError {
fn from(e: ServerError) -> RpcError {
let error_data = match to_value(&e) {
Expand Down
1 change: 0 additions & 1 deletion chain/jsonrpc-primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod errors;
pub mod message;
pub(crate) mod metrics;
pub mod types;
11 changes: 0 additions & 11 deletions chain/jsonrpc-primitives/src/metrics.rs

This file was deleted.

27 changes: 0 additions & 27 deletions chain/jsonrpc-primitives/src/types/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,6 @@ pub struct RpcBlockResponse {
pub block_view: near_primitives::views::BlockView,
}

impl From<near_client_primitives::types::GetBlockError> for RpcBlockError {
fn from(error: near_client_primitives::types::GetBlockError) -> Self {
match error {
near_client_primitives::types::GetBlockError::UnknownBlock { error_message } => {
Self::UnknownBlock { error_message }
}
near_client_primitives::types::GetBlockError::NotSyncedYet => Self::NotSyncedYet,
near_client_primitives::types::GetBlockError::IOError { error_message } => {
Self::InternalError { error_message }
}
near_client_primitives::types::GetBlockError::Unreachable { ref error_message } => {
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
crate::metrics::RPC_UNREACHABLE_ERROR_COUNT
.with_label_values(&["RpcBlockError"])
.inc();
Self::InternalError { error_message: error.to_string() }
}
}
}
}

impl From<actix::MailboxError> for RpcBlockError {
fn from(error: actix::MailboxError) -> Self {
Self::InternalError { error_message: error.to_string() }
}
}

impl From<RpcBlockError> for crate::errors::RpcError {
fn from(error: RpcBlockError) -> Self {
let error_data = match &error {
Expand Down
50 changes: 0 additions & 50 deletions chain/jsonrpc-primitives/src/types/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,50 +40,6 @@ pub enum RpcStateChangesError {
InternalError { error_message: String },
}

impl From<near_client_primitives::types::GetBlockError> for RpcStateChangesError {
fn from(error: near_client_primitives::types::GetBlockError) -> Self {
match error {
near_client_primitives::types::GetBlockError::UnknownBlock { error_message } => {
Self::UnknownBlock { error_message }
}
near_client_primitives::types::GetBlockError::NotSyncedYet => Self::NotSyncedYet,
near_client_primitives::types::GetBlockError::IOError { error_message } => {
Self::InternalError { error_message }
}
near_client_primitives::types::GetBlockError::Unreachable { ref error_message } => {
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
crate::metrics::RPC_UNREACHABLE_ERROR_COUNT
.with_label_values(&["RpcStateChangesError"])
.inc();
Self::InternalError { error_message: error.to_string() }
}
}
}
}

impl From<near_client_primitives::types::GetStateChangesError> for RpcStateChangesError {
fn from(error: near_client_primitives::types::GetStateChangesError) -> Self {
match error {
near_client_primitives::types::GetStateChangesError::IOError { error_message } => {
Self::InternalError { error_message }
}
near_client_primitives::types::GetStateChangesError::UnknownBlock { error_message } => {
Self::UnknownBlock { error_message }
}
near_client_primitives::types::GetStateChangesError::NotSyncedYet => Self::NotSyncedYet,
near_client_primitives::types::GetStateChangesError::Unreachable {
ref error_message,
} => {
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
crate::metrics::RPC_UNREACHABLE_ERROR_COUNT
.with_label_values(&["RpcStateChangesError"])
.inc();
Self::InternalError { error_message: error.to_string() }
}
}
}
}

impl From<RpcStateChangesError> for crate::errors::RpcError {
fn from(error: RpcStateChangesError) -> Self {
let error_data = match serde_json::to_value(error) {
Expand All @@ -98,9 +54,3 @@ impl From<RpcStateChangesError> for crate::errors::RpcError {
Self::new_internal_or_handler_error(Some(error_data.clone()), error_data)
}
}

impl From<actix::MailboxError> for RpcStateChangesError {
fn from(error: actix::MailboxError) -> Self {
Self::InternalError { error_message: error.to_string() }
}
}
46 changes: 0 additions & 46 deletions chain/jsonrpc-primitives/src/types/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,52 +41,6 @@ pub enum RpcChunkError {
UnknownChunk { chunk_hash: near_primitives::sharding::ChunkHash },
}

impl From<ChunkReference> for near_client_primitives::types::GetChunk {
fn from(chunk_reference: ChunkReference) -> Self {
match chunk_reference {
ChunkReference::BlockShardId { block_id, shard_id } => match block_id {
near_primitives::types::BlockId::Height(height) => Self::Height(height, shard_id),
near_primitives::types::BlockId::Hash(block_hash) => {
Self::BlockHash(block_hash, shard_id)
}
},
ChunkReference::ChunkHash { chunk_id } => Self::ChunkHash(chunk_id.into()),
}
}
}

impl From<near_client_primitives::types::GetChunkError> for RpcChunkError {
fn from(error: near_client_primitives::types::GetChunkError) -> Self {
match error {
near_client_primitives::types::GetChunkError::IOError { error_message } => {
Self::InternalError { error_message }
}
near_client_primitives::types::GetChunkError::UnknownBlock { error_message } => {
Self::UnknownBlock { error_message }
}
near_client_primitives::types::GetChunkError::InvalidShardId { shard_id } => {
Self::InvalidShardId { shard_id }
}
near_client_primitives::types::GetChunkError::UnknownChunk { chunk_hash } => {
Self::UnknownChunk { chunk_hash }
}
near_client_primitives::types::GetChunkError::Unreachable { ref error_message } => {
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
crate::metrics::RPC_UNREACHABLE_ERROR_COUNT
.with_label_values(&["RpcChunkError"])
.inc();
Self::InternalError { error_message: error.to_string() }
}
}
}
}

impl From<actix::MailboxError> for RpcChunkError {
fn from(error: actix::MailboxError) -> Self {
Self::InternalError { error_message: error.to_string() }
}
}

impl From<RpcChunkError> for crate::errors::RpcError {
fn from(error: RpcChunkError) -> Self {
let error_data = match &error {
Expand Down
28 changes: 0 additions & 28 deletions chain/jsonrpc-primitives/src/types/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,6 @@ pub enum RpcProtocolConfigError {
InternalError { error_message: String },
}

impl From<near_client_primitives::types::GetProtocolConfigError> for RpcProtocolConfigError {
fn from(error: near_client_primitives::types::GetProtocolConfigError) -> Self {
match error {
near_client_primitives::types::GetProtocolConfigError::UnknownBlock(error_message) => {
Self::UnknownBlock { error_message }
}
near_client_primitives::types::GetProtocolConfigError::IOError(error_message) => {
Self::InternalError { error_message }
}
near_client_primitives::types::GetProtocolConfigError::Unreachable(
ref error_message,
) => {
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
crate::metrics::RPC_UNREACHABLE_ERROR_COUNT
.with_label_values(&["RpcProtocolConfigError"])
.inc();
Self::InternalError { error_message: error.to_string() }
}
}
}
}

impl From<actix::MailboxError> for RpcProtocolConfigError {
fn from(error: actix::MailboxError) -> Self {
Self::InternalError { error_message: error.to_string() }
}
}

impl From<RpcProtocolConfigError> for crate::errors::RpcError {
fn from(error: RpcProtocolConfigError) -> Self {
let error_data = match &error {
Expand Down
27 changes: 0 additions & 27 deletions chain/jsonrpc-primitives/src/types/gas_price.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use near_client_primitives::types::GetGasPriceError;
use near_primitives::types::MaybeBlockId;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -26,32 +25,6 @@ pub enum RpcGasPriceError {
},
}

impl From<near_client_primitives::types::GetGasPriceError> for RpcGasPriceError {
fn from(error: near_client_primitives::types::GetGasPriceError) -> Self {
match error {
GetGasPriceError::UnknownBlock { error_message } => {
Self::UnknownBlock { error_message }
}
GetGasPriceError::InternalError { error_message } => {
Self::InternalError { error_message }
}
GetGasPriceError::Unreachable { ref error_message } => {
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
crate::metrics::RPC_UNREACHABLE_ERROR_COUNT
.with_label_values(&["RpcGasPriceError"])
.inc();
Self::InternalError { error_message: error.to_string() }
}
}
}
}

impl From<actix::MailboxError> for RpcGasPriceError {
fn from(error: actix::MailboxError) -> Self {
Self::InternalError { error_message: error.to_string() }
}
}

impl From<RpcGasPriceError> for crate::errors::RpcError {
fn from(error: RpcGasPriceError) -> Self {
let error_data = match &error {
Expand Down
Loading

0 comments on commit ac19a77

Please sign in to comment.