Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

parity-clib: async C bindings to RPC requests + subscribe/unsubscribe to websocket events #9920

Merged
merged 24 commits into from
Jan 2, 2019

Conversation

niklasad1
Copy link
Collaborator

@niklasad1 niklasad1 commented Nov 14, 2018

Attempt to close #9271

This PR changes to following in parity-clib (C and Java bindings):

  • asynchronous RPC queries (allocates a background thread that runs for at most X seconds)
  • subscribe to WebSocket events (runs in a background thread until it is cancled)
  • unsubscribe from WebSocket events

Additional changes:

  • modify party-cpp-example
  • add Java example application

TODOs:

  • Update Java bindings (Parity.java) and test it with a Java application
  • Change callback structure

Pitfalls/suggested improvements, should be fixed in this PR?!

  • Dropping a GlobalRef in a detached thread (JNI related https://docs.rs/jni/0.10.2/jni/objects/struct.GlobalRef.html)
  • Use a shared thread for WebSocket subscriptions (really hard without major refactoring because the parity-clib doesn't keep state)
  • Use of deprecated finalize() in Parity.Java (I have looked into it very briefly and the way to go is to use PhantomReference instead but requires more boilerplate code and I like currently how simple the Parity class is)

logger/src/lib.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 added A0-pleasereview 🤓 Pull request needs code review. M4-core ⛓ Core client code / Rust. labels Nov 14, 2018
parity-clib/src/lib.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 changed the title parity-clib: async C bindings to RPC queries [WIP] parity-clib: async C bindings to RPC queries Nov 14, 2018
@niklasad1 niklasad1 added A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. and removed A0-pleasereview 🤓 Pull request needs code review. labels Nov 14, 2018
@niklasad1 niklasad1 force-pushed the parity-clib/jsonrpc-callback branch 4 times, most recently from 5f7f1ae to 350b13a Compare November 17, 2018 00:30
@niklasad1 niklasad1 changed the title [WIP] parity-clib: async C bindings to RPC queries parity-clib: async C bindings to RPC query + subscribe/unsubscribe to websocket events Nov 17, 2018
@niklasad1 niklasad1 changed the title parity-clib: async C bindings to RPC query + subscribe/unsubscribe to websocket events parity-clib: async C bindings to RPC requests + subscribe/unsubscribe to websocket events Nov 17, 2018
@niklasad1 niklasad1 added A0-pleasereview 🤓 Pull request needs code review. M6-rpcapi 📣 RPC API. and removed A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. labels Nov 17, 2018
@niklasad1
Copy link
Collaborator Author

The PR should be in reviewable stable state now seem to work both for the light and full client.

The easiest way if you want to test it, grab the update cpp example and then make sure that you are fully-synced otherwise ChainNotify doesn't notify the PubSub Client

@niklasad1 niklasad1 force-pushed the parity-clib/jsonrpc-callback branch 3 times, most recently from 3e4c677 to c1a8cc9 Compare November 18, 2018 23:21
parity-clib-examples/cpp/main.cpp Outdated Show resolved Hide resolved
parity-clib/parity.h Outdated Show resolved Hide resolved
parity-clib/parity.h Outdated Show resolved Hide resolved
parity-clib/parity.h Outdated Show resolved Hide resolved
parity-clib/parity.h Outdated Show resolved Hide resolved
parity-clib/parity.h Outdated Show resolved Hide resolved
parity-clib/parity.h Outdated Show resolved Hide resolved
@niklasad1 niklasad1 force-pushed the parity-clib/jsonrpc-callback branch 2 times, most recently from b897ac4 to 55a0d2e Compare November 20, 2018 12:41
@niklasad1
Copy link
Collaborator Author

@tomaka

I have started to look at the Java bindings and the synchronous part works fine. However, the asynchronous part is a different story in order to invoke a java method I have to do by using the JNI/JVM environment AFAIU

Something like

    env.call_method(
        callback, 
        "callback", 
        "(JLjava/lang/Object;Ljava/lang/Object;J)V", 
        &[JValue::Long(0), JValue::Object(null), JValue::Object(null), JValue::Long(0)]
  ).expect("Java method call failed");

Thus, using the C API "out-of-the-box" won't work. So as I see it I can either:

  1. Introduce CCallback and JCallback in the the C-API
  2. Re-implement the functionality in the java module

Thoughts?

impl CallbackStr {
fn call(&self, msg: &[u8]) {
if let Some(ref cb) = self.f {
let cstr = CString::new(msg).expect("valid string with no null bytes in the middle; qed").into_raw();
Copy link
Collaborator Author

@niklasad1 niklasad1 Nov 22, 2018

Choose a reason for hiding this comment

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

This may be a performance bottleneck, it is O(n) + additional allocation but nice to have from a C/C++ perspective but for other languages such as Java, I expect that it still requires to copy these raw bytes into respective String type!

niklasad1 and others added 21 commits January 2, 2019 14:02
* Subscribing to websockets for the full-client works
* Use it the example to avoid using global variables
Forgot to pass the JNIEnv environment since it is an instance method
* Remove Java dependency by constructing a valid Java String in the callback
* `cpp` example pass in a struct instead to determines `callback kind`
* `java` add a instance variable the class `Callback` to determine `callback kind`
@niklasad1
Copy link
Collaborator Author

@5chdn The parity-clib/example was moved so the source path didn't work. I will fix this soon

@5chdn 5chdn merged commit b4f8bba into master Jan 2, 2019
@5chdn 5chdn deleted the parity-clib/jsonrpc-callback branch January 2, 2019 15:49
5chdn pushed a commit that referenced this pull request Feb 11, 2019
* fix(remove needless unsafe blocks)

* style(nits)

* fix(parity-clib): eliminate repetitive event loops

* revert(java bindings): safe rust -> unsafe rust

These functions can still end up with `UB` thus should be unsafe

* fix(grumbles): make Callback trait `pub (crate)`
ordian added a commit that referenced this pull request Apr 5, 2019
* master:
  no volumes are needed, just run -v volume:/path/in/the/container (#10345)
  Fixed misstype (#10351)
  snap: prefix version and populate candidate channel (#10343)
  Bundle protocol and packet_id together in chain sync (#10315)
  role back docker build image and docker deploy image to ubuntu:xenial based (#10338)
  change docker image based on debian instead of ubuntu due to the chan… (#10336)
  Don't add discovery initiators to the node table (#10305)
  fix(docker): fix not receives SIGINT (#10059)
  snap: official image / test (#10168)
  fix(add helper for timestamp overflows) (#10330)
  Additional error for invalid gas (#10327)
  Revive parity_setMinGasPrice RPC call (#10294)
  Add Statetest support for Constantinople Fix (#10323)
  fix(parity-clib): grumbles that were not addressed in #9920 (#10154)
  fix(light-rpc): Make `light_sync` generic (#10238)
  fix publish job (#10317)
  Secure WS-RPC: grant access to all apis (#10246)
  Make specification of protocol in SyncRequester::send_request explicit (#10295)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A6-mustntgrumble 💦 Pull request has areas for improvement. The author need not address them before merging. A7-looksgoodcantmerge 🙄 Pull request is reviewed well, but cannot be merged due to conflicts. M4-core ⛓ Core client code / Rust. M6-rpcapi 📣 RPC API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a method in the C bindings to replace the websocket API
6 participants