starknet-rs icon indicating copy to clipboard operation
starknet-rs copied to clipboard

JSON RPC batching

Open fracek opened this issue 2 years ago • 6 comments

JSON RPC supports batching multiple requests into one (batching). The performance gain is quite high, for example it takes around 80-100 ms to fetch a single block, but ~200ms to fetch 25 with batching.

fracek avatar Jul 12 '22 12:07 fracek

Intersting. I didn't know this before. Will implement soon. Thanks!

xJonathanLEI avatar Jul 12 '22 12:07 xJonathanLEI

That would mean we need to make the method calls return some intermedia representation of the requests though, pretty much like we did with Account::execute().

xJonathanLEI avatar Jul 12 '22 12:07 xJonathanLEI

Yes, that's the best way to add batching.

Also notice a couple of things:

  • The return values can be in any order (the server can process batches in parallel, which is cool), so need to use id to reorder them to the caller. Notice that notifications don't produce responses (I don't think starknet rpc uses them so not an issue).
  • Each individual batch request can fail, so the return value is something like Vec<Result<Response>>.
  • The batch request itself can fail if its malformed.

fracek avatar Jul 12 '22 13:07 fracek

Thanks for that! I guess a downside of such batching is you'd lose strong typing, unless you only allow batching the same request type.

xJonathanLEI avatar Jul 12 '22 13:07 xJonathanLEI

The library would need a response enum that contains all type of response values. An alternative is to return the raw json values then let users deserialize them (all types support deserialization anyway).

fracek avatar Jul 12 '22 13:07 fracek

Yeah I mean then you have to do some kind of unwrap. Lemme have a think about how this can be best implemented. Will update this soon :)

xJonathanLEI avatar Jul 12 '22 13:07 xJonathanLEI