-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(jsonrpc): move nearcore
type conversions to server
#6902
Conversation
I think this can be handled in a simpler way, without adding our own conversion trait/macro: diff --git a/chain/jsonrpc/src/errors.rs b/chain/jsonrpc/src/errors.rs
index f71d3217b..9082dc5cd 100644
--- a/chain/jsonrpc/src/errors.rs
+++ b/chain/jsonrpc/src/errors.rs
@@ -11,6 +11,10 @@ macro_rules! _rpc_try {
pub(crate) use _rpc_try as rpc_try;
+pub(crate) struct InternalServerError {
+ pub(crate) message: String,
+}
+
pub trait RpcFrom<T> {
fn rpc_from(_: T) -> Self;
}
@@ -36,9 +40,9 @@ impl RpcFrom<actix::MailboxError> for ServerError {
}
}
-impl RpcFrom<actix::MailboxError> for near_jsonrpc_primitives::types::blocks::RpcBlockError {
- fn rpc_from(error: actix::MailboxError) -> Self {
- Self::InternalError { error_message: error.to_string() }
+impl From<InternalServerError> for near_jsonrpc_primitives::types::blocks::RpcBlockError {
+ fn from(error: InternalServerError) -> Self {
+ Self::InternalError { error_message: error.message }
}
}
diff --git a/chain/jsonrpc/src/lib.rs b/chain/jsonrpc/src/lib.rs
index e76d9cbbc..7dac68f5d 100644
--- a/chain/jsonrpc/src/lib.rs
+++ b/chain/jsonrpc/src/lib.rs
@@ -5,6 +5,7 @@ use std::time::{Duration, Instant};
use actix::Addr;
use actix_cors::Cors;
use actix_web::{get, http, middleware, web, App, Error as HttpError, HttpResponse, HttpServer};
+use errors::InternalServerError;
use futures::Future;
use futures::FutureExt;
use prometheus;
@@ -984,12 +985,7 @@ impl JsonRpcHandler {
near_jsonrpc_primitives::types::blocks::RpcBlockResponse,
near_jsonrpc_primitives::types::blocks::RpcBlockError,
> {
- let block_view = rpc_try! {
- self
- .view_client_addr
- .send(GetBlock(request_data.block_reference))
- .await
- }?;
+ let block_view = self.message_view_client(GetBlock(request_data.block_reference)).await??;
Ok(near_jsonrpc_primitives::types::blocks::RpcBlockResponse { block_view })
}
@@ -1188,6 +1184,18 @@ impl JsonRpcHandler {
}?;
Ok(validators)
}
+
+ async fn message_view_client<M>(&self, msg: M) -> Result<M::Result, InternalServerError>
+ where
+ M: actix::Message + Send + 'static,
+ M::Result: Send,
+ ViewClientActor: actix::Handler<M>,
+ {
+ self.view_client_addr
+ .send(msg)
+ .await
+ .map_err(|it| InternalServerError { message: it.to_string() })
+ }
}
#[cfg(feature = "sandbox")] There's a bit of logical copy-paste with handling actix messages. So we can extract that bit and do conversion once, rather than doing it on every call-site. |
actix
error conversions to servernearcore
type conversions to server
671f70f
to
f516a37
Compare
near-chain-configs = { path = "../../core/chain-configs" } | ||
near-rpc-error-macro = { path = "../../tools/rpctypegen/macro" } | ||
near-client-primitives = { path = "../client-primitives" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the initial read, I thought we introduced new cruel dependencies chain-configs
and client-primitives
, but in fact, they just got shuffled a bit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few more PRs and they'll be gone for good.
Tagged: https://github.com/near/nearcore/releases/tag/crates-0.14.0 Notable changes: - #6844 - #6853 - #6902 - #6917 - #6916 Check here for a full changelog: crates-0.13.0...crates-0.14.0 Tested `cargo package` on each public crate locally, they all compile independently of the workspace. We should be good to go.
Tracking issue: #6850
Move all conversions between
near-primitives
andnear-jsonrpc-primitives
to the server side. Freeing up a bunch of dependencies onnear-jsonrpc-primitives
.