smol-dot / smoldot

Lightweight client for Substrate-based chains, such as Polkadot and Kusama.
GNU General Public License v3.0
176 stars 47 forks source link

Testing smoldot with substrate WebRTC #1806

Closed lexnv closed 3 months ago

lexnv commented 3 months ago

I would like to test the litep2p WebRTC with smoldot.

I've created this branch in substrate to configure webRtc with litep2p:

I'm running the local polkadot node as:

RUST_LOG="litep2p=trace" ./target/release/polkadot --dev --node-key 0000000000000000000000000000000000000000000000000000000000000002 --network-backend litep2p --listen-addr /ip4/127.0.0.1/udp/8888/webrtc-direct --rpc-port 9999

However, I'm not able to connect subxt's lightclient to the node

 cd testing/integration-tests/

 SUBSTRATE_NODE_PATH=/home/ubuntu/workspace/polka-work/target/release/substrate-node RUST_LOG=trace cargo test --release --features unstable-light-client

Currently, the peer is banned every 10 seconds:

2024-05-01T12:11:42.808763Z DEBUG async_io::block_on: network: slot-assigned; chain=rococo_dev, peer_id=12D3KooWHdiAxVd8uMQR1hGWXccidmfCwLqcMpGwR6QcTP6QRMuD
2024-05-01T12:11:42.808927Z DEBUG async_io::block_on: network: slot-unassigned; chain=rococo_dev, peer_id=12D3KooWHdiAxVd8uMQR1hGWXccidmfCwLqcMpGwR6QcTP6QRMuD, ban_duration=10s, reason=no-address

Would you happen to know if I need a custom branch for smoldot to work with webrtc? It might be possible that the default PlatformRef we use in subxt only supports websockets and TCP? 🤔 Wanted to double check, is this up-to date with latest smoldot: https://tomaka.github.io/test-browser-node/index.html? Thanks 🙏

tomaka commented 3 months ago

I'm running the local polkadot node as:

https://tomaka.github.io/test-browser-node/index.html uses Westend, so if your Polkadot node doesn't use Westend it will disconnect smoldot.

It might be possible that the default PlatformRef we use in subxt only supports websockets and TCP? 🤔

Yes, only the wasm node and its custom PlatformRef implementation supports WebRTC.

is this up-to date with latest smoldot: https://tomaka.github.io/test-browser-node/index.html?

No, but nothing has fundamentally changed since it has last been updated.

tomaka commented 3 months ago

I have updated https://tomaka.github.io/test-browser-node/index.html with the latest version of smoldot.

Also note that Firefox has a long-standing bug (https://bugzilla.mozilla.org/show_bug.cgi?id=1659672) that makes WebRTC not work when connecting localhost. You should either use Chrome, or deploy your node on a different machine.

lexnv commented 3 months ago

Coming back to this, I have 2 computers connected:

A quick port scanning from macOs -> linux resulted in polkadot log-lines coming from webrtc component nmap -p 33999 -sU my-ip.

I've fetched the chainSpec from the running node and replaced the westend.json for the test-browser-node app.

[smoldot] Smoldot v2.0.18
[smoldot] Chain initialization complete for rococo_dev. Name: "Development". Genesis hash: 0xcc64…7ffe. Chain specification starting at: 0xcc64…7ffe (#0)
[smoldot] Chain specification of rococo_dev contains a `genesis.raw` item. It is possible to significantly improve the initialization time by replacing the `"raw": ...` field with `"stateRootHash": "0x9d4f2d3841854235149fb3d91ce991fa4f389ce7a02b4b483aa5b19385116643"`
[smoldot] Chain specification of rococo_dev contains a `protocolId` field. This field is deprecated and its value is no longer used. It can be safely removed from the JSON document.
[network] chain-added; id=rococo_dev
[network] slot-assigned; chain=rococo_dev, peer_id=12D3KooWHdiAxVd8uMQR1hGWXccidmfCwLqcMpGwR6QcTP6QRMuD
[network] slot-unassigned; chain=rococo_dev, peer_id=12D3KooWHdiAxVd8uMQR1hGWXccidmfCwLqcMpGwR6QcTP6QRMuD, ban_duration=10s, reason=no-address
[network] discovery-skipped-no-peer; chain=rococo_dev
[runtime-rococo_dev] sync-service-subscription-started; finalized_block_hash=0xcc64…7ffe, finalized_block_runtime_known=false
[runtime-rococo_dev] block-runtime-download-started; block_hash=0xcc64…7ffe
[runtime-rococo_dev] runtime-download-error; block_hashes=0xcc64…7ffe, error=StorageQuery(StorageQueryError { errors: [] })
[network] discovery-skipped-no-peer; chain=rococo_dev
[network] slot-assigned; chain=rococo_dev, peer_id=12D3KooWHdiAxVd8uMQR1hGWXccidmfCwLqcMpGwR6QcTP6QRMuD
[network] slot-unassigned; chain=rococo_dev, peer_id=12D3KooWHdiAxVd8uMQR1hGWXccidmfCwLqcMpGwR6QcTP6QRMuD, ban_duration=10s, reason=no-address
[runtime-rococo_dev] block-runtime-download-started; block_hash=0xcc64…7ffe
[runtime-rococo_dev] runtime-download-error; block_hashes=0xcc64…7ffe, error=StorageQuery(StorageQueryError { errors: [] })
[network] discovery-skipped-no-peer; chain=rococo_dev

The polkadot node did not produce any traces for the webrtc during the connection attempt. Will keep looking into this, it seems like a connectivity issue 🙏

tomaka commented 3 months ago

I don't log this properly, but the multiaddress that you pass to the test-browser-node is somehow in an invalid format.

See https://github.com/libp2p/specs/blob/master/webrtc/webrtc-direct.md#addressing for the format. Are you maybe missing the certhash? This is the hash of a certificate created by the WebRTC server, that the Polkadot node should be able to print somehow.

lexnv commented 3 months ago

That did the trick! Thanks!

I was unable to sync smoldot to the head of the chain for my local node, I believe atm there's something wrong on our side. Will close this and come back later, thanks again 🙏