-
Notifications
You must be signed in to change notification settings - Fork 224
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
light-node: RPC server #363
Conversation
a7b39fb
to
e1b3918
Compare
Supervisor changes have been split out into #394. |
As we start to depend on the surface of the `Handle` we benefit from it being a trait that can be implemented on a per need basis. This will result in less overhead constructing object graphs in places where we wannt to assert behaviour of other types in remote places, i.e. the light-node rpc server. Overall we hope for an increased ease in writing tests on module level. Ref #219
Depends on #401 |
} | ||
} | ||
|
||
const LIGHTBLOCK_JSON: &str = r#" |
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.
Is that needed twice? Here and above (in light-node/examples/rpc.rs)?
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.
The example is temporary to help with the integration work, wouldn't it to leave as is mid-term.
} | ||
} | ||
#[error("i/o error: {0}")] | ||
Io(String), |
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.
Just out of curiosity: why did you decide to wrap the underlying error as a string here (as opposed to using the #[from]
directive)?
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.
Because io::Error
doesn't implement Clone
. Which we mind not need anymore on our Kind
as I refrained from returning our custom errors in the rpc server, as I wasn't able to get mapping to the jsonrpc error to work.
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.
One way to work around the lack of Clone
on io::Error
is to use the anomaly::Context
wrapper (as shown in the docs), which would look something like that:
diff --git a/light-node/src/error.rs b/light-node/src/error.rs
index 094799b..7162c03 100644
--- a/light-node/src/error.rs
+++ b/light-node/src/error.rs
@@ -1,8 +1,9 @@
//! Error types
-use std::io;
use std::net;
+use anomaly::{BoxError, Context};
+
/// Error type
pub type Error = anomaly::Error<Kind>;
@@ -18,12 +19,15 @@ pub enum Kind {
Config,
/// Input/output error
- #[error("i/o error: {0}")]
- Io(String),
+ #[error("i/o error")]
+ Io,
}
-impl From<io::Error> for Kind {
- fn from(err: io::Error) -> Self {
- Self::Io(format!("{}", err))
+impl Kind {
+ /// Add additional context (i.e. include a source error and capture a backtrace).
+ /// You can convert the resulting `Context` into an `Error` by calling `.into()`.
+ pub fn context(self, source: impl Into<BoxError>) -> Context<Self> {
+ Context::new(self, Some(source.into()))
}
}
+
diff --git a/light-node/src/rpc.rs b/light-node/src/rpc.rs
index 544dcb4..f6b190f 100644
--- a/light-node/src/rpc.rs
+++ b/light-node/src/rpc.rs
@@ -25,7 +25,7 @@ where
AccessControlAllowOrigin::Any,
]))
.start_http(&addr.parse().map_err(error::Kind::from)?)
- .map_err(error::Kind::from)?;
+ .map_err(|e| error::Kind::Io.context(e))?;
srv.wait();
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.
PR at #413
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.
Yeah was looking at the Context
as we also use it in the light-client crate. You do loose the From
semantics with it, correct? As in you have to know which variant to construct when mapping.
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.
You do loose the From semantics with it, correct? As in you have to know which variant to construct when mapping.
I would say that you just have to be more explicit, rather than let the compiler pick the appropriate From
instance based on the (static) type of the underlying error, which in this case is a io::Error
. So you don't really lose anything aside from the From
instance, which in this case is not appropriate for io::Error
because of the lack of Clone
bound. If io::Error
was Clone
, then you would use #[from]
like you did for net::AddrParseError
.
@@ -0,0 +1,146 @@ | |||
//! Basic example of running the RPC server. This is a temporary show-case and should be removed | |||
//! once integrated in the light node proper. To test the `/state` endpoint run: |
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.
👍
light-node/src/error.rs
Outdated
ErrorKind::Io.context(err).into() | ||
impl Into<jsonrpc_core::types::Error> for Kind { | ||
fn into(self) -> jsonrpc_core::types::Error { | ||
todo!() |
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.
Did you leave this a todo!()
on purpose?
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.
See https://github.com/informalsystems/tendermint-rs/pull/363/files#r450373699 - I think the whole conversion can go. We don't use it at the moment.
Co-authored-by: Ismail Khoffi <Ismail.Khoffi@gmail.com>
Codecov Report
@@ Coverage Diff @@
## master #363 +/- ##
========================================
+ Coverage 30.4% 30.7% +0.2%
========================================
Files 130 131 +1
Lines 5448 5464 +16
Branches 1686 1695 +9
========================================
+ Hits 1659 1678 +19
+ Misses 2640 2603 -37
- Partials 1149 1183 +34
Continue to review full report at Codecov.
|
@@ -7,11 +7,18 @@ publish = false | |||
|
|||
[dependencies] | |||
abscissa_tokio = "0.5" | |||
anomaly = { version = "0.2", features = [ "serializer" ] } |
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.
@tarcieri @tony-iqlusion Mind chiming in here? It's been a while since I actively used all 3 dependencies: anomaly
, thiserror
, and abscissa
. Does it make sense to mix those here? Doesn't make abscissa using anomaly in the same crate obsolete? I'm a bit lost here.
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.
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.
Good to know thanks!
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.
It looks like there's a decent chance that the changes will get upstreamed back into anyhow
, not sure how long it will be though.
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.
I think It's OK to merge this as is to move forward with the cli / light node tmrw.
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.
👍
Great work @xla! 🥇 |
Adds an RPC server to expose internal state of a light node. Besides adding the general infrastructure it provides the first endpoint under
/state
as described in #219.First part of #219
Depends on #394
Depends on #403