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

WebSocket subscription query not found #1426

Open
patternmachine opened this issue May 22, 2024 · 2 comments · May be fixed by #1433
Open

WebSocket subscription query not found #1426

patternmachine opened this issue May 22, 2024 · 2 comments · May be fixed by #1433
Labels
bug Something isn't working

Comments

@patternmachine
Copy link

What went wrong?

When subscribing to NewBlock events (possibly also others, which I have not tested) I expect my code to receive events via the Subscription returned by the subscribe() call which is not the case.

After spending way too much time going through the code I discovered that the SubscriptionRouter stores the subscriptions internally in a HashMap<SubscriptionQuery, HashMap<SubscriptionId, SubscriptionTx>>, where SubscriptionQuery is just a type alias for a String. For the NewBlock event that then means the key ends up being "tm.event = 'NewBlock'".
Now when the client receives a message, it takes the query field inside that message to look into the hashmap. Unfortunately, I keep getting back "tm.event='NewBlock'" which is obviously not the same as "tm.event = 'NewBlock'" (missing spaces around the =).

Steps to reproduce

Take the example code from the WebSocketClient

use tendermint_rpc::{WebSocketClient, SubscriptionClient, Client};
use tendermint_rpc::query::EventType;
use futures::StreamExt;

#[tokio::main]
async fn main() {
    let (client, driver) = WebSocketClient::new("wss://osmosis-rpc.publicnode.com:443/websocket")
        .await
        .unwrap();
    let driver_handle = tokio::spawn(async move { driver.run().await });

    // Subscription functionality
    let mut subs = client.subscribe(EventType::NewBlock.into())
        .await
        .unwrap();

    // Grab 5 NewBlock events
    let mut ev_count = 5_i32;

    while let Some(res) = subs.next().await {
        let ev = res.unwrap();
        println!("Got event: {:?}", ev);
        ev_count -= 1;
        if ev_count < 0 {
            break;
        }
    }

    // Signal to the driver to terminate.
    client.close().unwrap();
    // Await the driver's termination to ensure proper connection closure.
    let _ = driver_handle.await.unwrap();
}

Definition of "done"

The above should print out the events.

@patternmachine patternmachine added the bug Something isn't working label May 22, 2024
@romac
Copy link
Member

romac commented May 22, 2024

Thanks for taking the time to figure this out, and sorry you had to run into this!

We should definitely index the subscriptions by the corresponding parsed and normalized Query instead of its string representation.

Would you be open to create a PR for this? Otherwise I am happy to do it myself but it'll have to wait until next week or the one after that.

@romac
Copy link
Member

romac commented May 22, 2024

@patternmachine I just put a WIP branch with a tentative fix, let me know if that works for you until I have time to get back to it, or feel free to improve on it and open a PR.

Note that I haven't had a chance to test this so it may very well be broken in more than one way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants