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

Fix unstable-light-client + ChainHeadBackend tx events #1865

Merged
merged 4 commits into from
Nov 13, 2024

Conversation

jsdw
Copy link
Collaborator

@jsdw jsdw commented Nov 12, 2024

Both of these changes should be backward compatible: it doesn't matter if the JSON returned contains the removed fields.

Oddly, when encountering the error, I was noticing that Smoldot was returning the JSON we expected ie {"event":"broadcasted","numPeers":1}. I'm not sure why this wasn't being properly deserialized by us in the first place (we expect num_peers but also rename_all = "camelCase" so it should line up).

This can be tested by running the following example, which fails prior to this change and passes after it:

#![allow(missing_docs)]
use subxt::utils::fetch_chainspec_from_rpc_node;
use subxt::{
    client::OnlineClient,
    lightclient::{ChainConfig, LightClient},
    PolkadotConfig,
};
use subxt_signer::sr25519::dev;
use subxt::backend::chain_head::{ChainHeadBackend, ChainHeadBackendBuilder};

// Generate an interface that we can use from the node's metadata.
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
pub mod polkadot {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // The smoldot logs are informative:
    tracing_subscriber::fmt::init();

    // Use a utility function to obtain a chain spec from a locally running node:
    let chain_spec = fetch_chainspec_from_rpc_node("ws://127.0.0.1:9944").await?;

    // Configure the bootnodes of this chain spec. In this case, because we start one
    // single node, the bootnodes must be overwritten for the light client to connect
    // to the local node.
    //
    // The `12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp` is the P2P address
    // from a local polkadot node starting with
    // `--node-key 0000000000000000000000000000000000000000000000000000000000000001`
    let chain_config = ChainConfig::chain_spec(chain_spec.get()).set_bootnodes([
        "/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp",
    ])?;

    // Start the light client up, establishing a connection to the local node.
    let (_light_client, chain_rpc) = LightClient::relay_chain(chain_config)?;

    let backend: ChainHeadBackend<PolkadotConfig> =
        ChainHeadBackendBuilder::default().build_with_background_driver(chain_rpc.clone());

    let api = OnlineClient::<PolkadotConfig>::from_backend(backend.into()).await?;

    // Build a balance transfer extrinsic.
    let dest = dev::bob().public_key().into();
    let balance_transfer_tx = polkadot::tx().balances().transfer_allow_death(dest, 10_000);

    // Submit the balance transfer extrinsic from Alice, and wait for it to be successful
    // and in a finalized block. We get back the extrinsic events if all is well.
    let from = dev::alice();
    let events = api
        .tx()
        .sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
        .await?
        .wait_for_finalized_success()
        .await?;

    // Find a Transfer event and print it.
    let transfer_event = events.find_first::<polkadot::balances::events::Transfer>()?;
    if let Some(event) = transfer_event {
        println!("Balance transfer success: {event:?}");
    }

    Ok(())
}

Supercedes #1863

Copy link
Collaborator

@lexnv lexnv left a comment

Choose a reason for hiding this comment

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

Nice! 🙏

@niklasad1 niklasad1 merged commit 057c847 into master Nov 13, 2024
13 checks passed
@niklasad1 niklasad1 deleted the jsdw-lightclient-chainhead-event-fix branch November 13, 2024 14:26
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.

3 participants