Skip to content
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

[rpc module] test helper for calling and converting types to JSON-RPC params #458

Merged
merged 12 commits into from Sep 13, 2021

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Sep 9, 2021

Follow-up on #443

A little bit hacky to do like this but I think it will work in order to avoid users to manually serializing their parameters to serde::RawValue and manually add it to be serialized to a sequence "[ .... ]" to work properly if it's not already the case.

See examples in the introduced tests

utils/src/server/rpc_module.rs Outdated Show resolved Hide resolved
utils/src/server/rpc_module.rs Outdated Show resolved Hide resolved
niklasad1 and others added 2 commits September 10, 2021 16:36
/// Converts the params to a stringified array for you if it's not already serialized to a sequence.
pub async fn test_call<T: Serialize>(&self, method: &str, params: T) -> Option<String> {
let params = serde_json::to_string(&params).ok().map(|json| {
let json = if json.starts_with('[') && json.ends_with(']') { json } else { format!("[{}]", json) };
Copy link
Collaborator

@jsdw jsdw Sep 13, 2021

Choose a reason for hiding this comment

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

What if there's only one param provided to the call, and it's something that already serializes to an array like Vec<u8>?

Copy link
Member Author

@niklasad1 niklasad1 Sep 13, 2021

Choose a reason for hiding this comment

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

It would already be serialized to an array then we won' insert [ json ], see if expression above :)

However, it could be weird if you put in a Vec<_> and expect it to be serialized as [[my vec]], lemme double check I forgot if I added a test for this.

Copy link
Collaborator

@jsdw jsdw Sep 13, 2021

Choose a reason for hiding this comment

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

That's the case I was picturing; if there's an RPC call that accepts 1 param, a Vec<u8>, then I guess I couldn't use test_call, because using it would lead to params like [1,2,3] being passed in instead of a single param like [[1,2,3]]

Copy link
Member Author

Choose a reason for hiding this comment

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

yepp, you are right.

I did it this way to work with both RpcParams::parse and RpcParams::Sequence::next(), but it won't work with doing weird things or that. I guess I was fooled because I used JsonValue in the test....

Don't remember if I had an issue with the parser to get this work.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is an chicken and egg problem it will always be an edge-case for this; so no good solution AFAIU.

Perhaps, I should revisit to try to add into_rpc_test_module or something from because this essentially becomes a footgun unless we do if params == edge_case then panic!("not supported")

Copy link
Collaborator

Choose a reason for hiding this comment

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

A call like test_call could accept something like T: IntoRpcParams, which is implemented for a bunch of tuple sizes (and maybe even slices and arrays) like:

impl <A: Serialize> IntoRpcParams for (A,) { .. }
impl <A: Serialize, B: Serialize> IntoRpcParams for (A,B) { .. }
impl <const N: usize, A: Serialize> IntoRpcParams for [A;N] { .. }

That way, You could call eg .test_call("foo", (a, b, c)) or .test_call("foo", [a]) or .test_call("foo", (a,)) with low overhead?

(perhaps something like that exists in JsonRpsee already and I'm not being very helpful; I haven't checked! :))

Copy link
Member Author

Choose a reason for hiding this comment

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

that's good idea, I think that's better

Copy link
Contributor

Choose a reason for hiding this comment

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

I played around with tuples on a branch on saturday and it worked fine up to a point and then didn't. I ended up having to call some permutations with a string and some with a tuple, in the end I abandoned it because it felt like the user would have a deeper understanding of what's going on than I felt was warranted.

That said, it might work out better for you. :)


impl<P: Serialize> ToRpcParams for Vec<P> {}

macro_rules! array_impls {
Copy link
Member Author

Choose a reason for hiding this comment

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

const generics doesn't work because of serde I think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, came here to ask about this.

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

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

LGTM! :)

@niklasad1
Copy link
Member Author

I'll merge it so @dvdplm can continue with his work :)

@niklasad1 niklasad1 merged commit 932304c into master Sep 13, 2021
@niklasad1 niklasad1 deleted the na-rpc-module-test-helper branch September 13, 2021 14:04
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.

None yet

3 participants