Skip to content

Conversation

@niklasad1
Copy link
Contributor

@niklasad1 niklasad1 commented Apr 12, 2021

Migrate to jsonrpsee_types::v2 which causes lots of related changes.

The major change is the client traits and related JsonRpcParam type,

enum JsonRpcParams<'a> {
    NoParams,
    Array(Vec<JsonValue>]),
    ArrayRef(&'a [JsonValue]),
    Map(BTreeMap<&'a str, JsonValue>),
}

fn request<'a>(method: &'str, params: JsonRpcParams<'a>) { .... } 

The changes were easy to port to http client but with websocket client I had to major refactoring and decided to make the transport abstraction clean i.e, doesn't know anything about jsonrpc it just sends messages. Thus, the clients are responsible for constructing the messages.

  • re-org types::v2 module perhaps request/response or serialize/deserial or something.-

Follow up:

Closing #249 #219 #225

@niklasad1 niklasad1 changed the title [WIP]: client use types v2 [client] use types v2 (less alloc) Apr 13, 2021
@niklasad1 niklasad1 requested a review from maciejhirsz April 13, 2021 16:11
pub fn jsonrpsee_types_v2(crit: &mut Criterion) {
crit.bench_function("jsonrpsee_types_v2", |b| {
b.iter(|| {
let params: JsonRpcParams<_> = vec![&1, &2].into();
Copy link
Contributor Author

@niklasad1 niklasad1 Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

provide a macro for this:...

maybe: params_postional![1, 2, 3], params_by_key!["key" => val]?

Ok(quote!(#ident::types))
}
Ok(FoundCrate::Itself) => panic!("Deriving RPC methods in any of the `jsonrpsee crates` is not supported"),
Err(_) => match (crate_name("jsonrpsee-http-client"), crate_name("jsonrpsee-ws-client")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug before we searched only for the clients crates in Cargo.toml.

So now:

  1. Search for jsonrpsee crate
  2. if not found search for client crates


/// JSONRPC error code
#[derive(Error, Debug, PartialEq, Copy, Clone)]
pub enum ErrorCode {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy-pasted from old jsonrpc types with some minor tweaks.

Str(String),
}

impl From<SubscriptionId> for JsonValue {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid to call serde_json::to_value and it's infallible

}

impl<'a> serde::Deserialize<'a> for ErrorCode {
fn deserialize<D>(deserializer: D) -> Result<ErrorCode, D::Error>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are ser/deser is a little bit weird (not symmetrical) but essentially the error message can be deduced from the error code so no need to store the message as allocated string.


pub use beef::Cow;
pub use client::*;
pub use error::Error;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could revisit this error type but it's quite hard to understand the different error types.

Technically we could kill the huge enum and have each client/server to have their own error type instead.

/// JSON-RPC version.
pub jsonrpc: TwoPointZero,
/// Error.
pub error: ErrorCode,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maciejhirsz I removed your type https://github.com/paritytech/jsonrpsee/blob/master/types/src/v2/mod.rs#L85-#L90. ErrorCode should be as cheap I think and more readable

#[derive(Debug, PartialEq, Clone, Hash, Eq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
#[serde(untagged)]
pub enum Id {
Copy link
Contributor Author

@niklasad1 niklasad1 Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decided to bring this back, currently we just use Number in the clients and the servers is using RawValue

but probably worth to comply with the spec

Copy link
Contributor

@maciejhirsz maciejhirsz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +151 to +153
let curr = self.current_pending.load(Ordering::Relaxed);
if curr > 0 {
self.current_pending.store(curr - 1, Ordering::Relaxed);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a pretty nasty race condition here :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants