paritytech / jsonrpsee

Rust JSON-RPC library on top of async/await
MIT License
645 stars 172 forks source link

fix(proc-macros): make feature `server-core` compile #1360

Closed niklasad1 closed 6 months ago

niklasad1 commented 6 months ago

Similar to #1359 and kudos to @koushiro for detecting this but I prefer not re-export tokio

koushiro commented 6 months ago

But there are already some reexports in the existing code, such as https://github.com/paritytech/jsonrpsee/blob/0379bc4585cac17b7e3835157ed79ff5bbf340f0/proc-macros/src/render_server.rs#L383 and https://github.com/paritytech/jsonrpsee/blob/0379bc4585cac17b7e3835157ed79ff5bbf340f0/proc-macros/src/render_client.rs#L54

niklasad1 commented 6 months ago

Yes you are correct but we have a couple of re-exports there but these should be as few a possible IMO.

koushiro commented 6 months ago

@niklasad1 There is still a problem when using the subscription attribute

error[E0433]: failed to resolve: could not find `tokio` in `jsonrpsee`
  --> client/rpc/api/src/eth/pubsub.rs:28:1
   |
28 | #[rpc(client, server, namespace = "eth")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `tokio` in `jsonrpsee`
   |
note: found an item that was configured out
  --> /Users/qinxuan/Code/jsonrpsee/jsonrpsee/src/lib.rs:81:10
   |
81 |     pub use tokio;
   |             ^^^^^
   = note: the item is gated behind the `server` feature
   = note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)
koushiro commented 6 months ago

And I found another issue about the params name when using subscription attribute

[package]
name = "rpc-demo"
version = "0.1.0"
edition = "2021"

[dependencies]
jsonrpsee = { version = "0.22.4", features = ["client-core", "server-core", "macros"] }
serde = { version = "1.0", features = ["derive"] }

[patch.crates-io]
jsonrpsee = { path = "../parity/jsonrpsee/jsonrpsee" }
use jsonrpsee::{core::SubscriptionResult, proc_macros::rpc};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub enum PubSubKind {
    A,
    B,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PubSubParams {
    params: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PubSubItem {
    result: String,
}

#[rpc(client, server, namespace = "foo")]
#[async_trait]
pub trait FooPubSubApi {
   // Error 
    #[subscription(name = "subscribe" => "subscription", unsubscribe = "unsubscribe", item = PubSubItem)]
    async fn sub(&self, kind: PubSubKind, params: Option<PubSubParams>) -> SubscriptionResult;

    // Ok
    // #[subscription(name = "subscribe" => "subscription", unsubscribe = "unsubscribe", item = PubSubItem)]
    // async fn sub(&self, kind: PubSubKind, pubsub_params: Option<PubSubParams>) -> SubscriptionResult;
}

Error:

    Checking rpc-demo v0.1.0 (/Users/qinxuan/Code/rpc-demo)
error[E0433]: failed to resolve: could not find `tokio` in `jsonrpsee`
  --> src/lib.rs:20:1
   |
20 | #[rpc(client, server, namespace = "foo")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `tokio` in `jsonrpsee`
   |
note: found an item that was configured out
  --> /Users/qinxuan/Code/parity/jsonrpsee/jsonrpsee/src/lib.rs:81:10
   |
81 |     pub use tokio;
   |             ^^^^^
   = note: the item is gated behind the `server` feature
   = note: this error originates in the attribute macro `rpc` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `ArrayParams: Serialize` is not satisfied
   --> src/lib.rs:24:40
    |
20  | #[rpc(client, server, namespace = "foo")]
    | ----------------------------------------- required by a bound introduced by this call
...
24  |     async fn sub(&self, kind: PubSubKind, params: Option<PubSubParams>) -> Subscript...
    |                                           ^^^^^^ the trait `Serialize` is not implemented for `ArrayParams`
    |
    = help: the following other types implement trait `Serialize`:
              bool
              char
              isize
              i8
              i16
              i32
              i64
              i128
            and 148 others
note: required by a bound in `ArrayParams::insert`
   --> /Users/qinxuan/Code/parity/jsonrpsee/core/src/params.rs:200:19
    |
200 |     pub fn insert<P: Serialize>(&mut self, value: P) -> Result<(), serde_json::Error> {
    |                      ^^^^^^^^^ required by this bound in `ArrayParams::insert`

Some errors have detailed explanations: E0277, E0433.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `rpc-demo` (lib) due to 2 previous errors
niklasad1 commented 6 months ago

You should be unblocked after I merged #1361 but please open issues if something doesn't work

koushiro commented 6 months ago

@niklasad1 there is still a problem about the parameter name, although I can use other names to avoid this issue.

https://github.com/paritytech/jsonrpsee/blob/c908eebc7f00b4ac7fdda4c6cead27af2019f796/proc-macros/src/render_client.rs#L242-L254

error[E0277]: the trait bound `ArrayParams: Serialize` is not satisfied
   --> src/lib.rs:24:40
    |
20  | #[rpc(client, server, namespace = "foo")]
    | ----------------------------------------- required by a bound introduced by this call
...
24  |     async fn sub(&self, kind: PubSubKind, params: Option<PubSubParams>) -> Subscript...
    |                                           ^^^^^^ the trait `Serialize` is not implemented for `ArrayParams`
    |
    = help: the following other types implement trait `Serialize`:
              bool
              char
              isize
              i8
              i16
              i32
              i64
              i128
            and 148 others
note: required by a bound in `ArrayParams::insert`
   --> /Users/qinxuan/Code/parity/jsonrpsee/core/src/params.rs:200:19
    |
200 |     pub fn insert<P: Serialize>(&mut self, value: P) -> Result<(), serde_json::Error> {
    |                      ^^^^^^^^^ required by this bound in `ArrayParams::insert`

Some errors have detailed explanations: E0277, E0433.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `rpc-demo` (lib) due to 2 previous errors
niklasad1 commented 6 months ago

@koushiro I can't reproduce that please open an issue how to reproduce it....

koushiro commented 6 months ago

@koushiro I can't reproduce that please open an issue how to reproduce it....

https://github.com/paritytech/jsonrpsee/issues/1362