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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update smoldot connection options
- Loading branch information
Showing
2 changed files
with
157 additions
and
63 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,143 @@ | ||
// TODO | ||
import { deferred } from "../../deps/std/async.ts"; | ||
import { deferred, delay } from "../../deps/std/async.ts"; | ||
import { assertExists, assertNotInstanceOf } from "../../deps/std/testing/asserts.ts"; | ||
import { ProviderListener } from "./base.ts"; | ||
import { smoldotProvider } from "./smoldot.ts"; | ||
|
||
const POLKADOT_CHAIN_SPEC_URL = | ||
"https://raw.githubusercontent.com/paritytech/substrate-connect/main/packages/connect/src/connector/specs/polkadot.json"; | ||
|
||
Deno.test({ | ||
name: "Smoldot Provider", | ||
sanitizeOps: false, | ||
sanitizeResources: false, | ||
async fn() { | ||
const polkadotChainSpec = await (await fetch(POLKADOT_CHAIN_SPEC_URL)).text(); | ||
const pendingSubscriptionId = deferred<string>(); | ||
const initialized = deferred(); | ||
const unsubscribed = deferred(); | ||
const checks: ProviderListener<any, any>[] = [ | ||
// check for chainHead_unstable_follow subscription | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
assertExists(message.result); | ||
pendingSubscriptionId.resolve(message.result); | ||
}, | ||
// check for chainHead_unstable_follow initialized event | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
assertExists(message.params?.result); | ||
if (message.params?.result.event === "initialized") { | ||
initialized.resolve(); | ||
} | ||
}, | ||
// check for chainHead_unstable_unfollow unsubscribe | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
if (message?.result === null) { | ||
unsubscribed.resolve(); | ||
} | ||
async fn(t) { | ||
await t.step({ | ||
name: "relay chain connection", | ||
async fn() { | ||
const relayChainSpec = await ( | ||
await fetch( | ||
"https://raw.githubusercontent.com/paritytech/substrate-connect/main/packages/connect/src/connector/specs/polkadot.json", | ||
) | ||
) | ||
.text(); | ||
const pendingSubscriptionId = deferred<string>(); | ||
const initialized = deferred(); | ||
const unsubscribed = deferred(); | ||
const checks: ProviderListener<any, any>[] = [ | ||
// check for chainHead_unstable_follow subscription | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
assertExists(message.result); | ||
pendingSubscriptionId.resolve(message.result); | ||
}, | ||
// check for chainHead_unstable_follow initialized event | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
assertExists(message.params?.result); | ||
if (message.params?.result.event === "initialized") { | ||
initialized.resolve(); | ||
} | ||
}, | ||
// check for chainHead_unstable_unfollow unsubscribe | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
if (message?.result === null) { | ||
unsubscribed.resolve(); | ||
} | ||
}, | ||
]; | ||
const provider = smoldotProvider(relayChainSpec, (message) => { | ||
if (checks.length > 1) { | ||
checks.shift()!(message); | ||
} else { | ||
checks[0]!(message); | ||
} | ||
}); | ||
provider.send({ | ||
jsonrpc: "2.0", | ||
id: provider.nextId(), | ||
method: "chainHead_unstable_follow", | ||
params: [false], | ||
}); | ||
const subscriptionId = await pendingSubscriptionId; | ||
await initialized; | ||
provider.send({ | ||
jsonrpc: "2.0", | ||
id: provider.nextId(), | ||
method: "chainHead_unstable_unfollow", | ||
params: [subscriptionId], | ||
}); | ||
await unsubscribed; | ||
const providerRelease = await provider.release(); | ||
assertNotInstanceOf(providerRelease, Error); | ||
}, | ||
]; | ||
const provider = smoldotProvider(polkadotChainSpec, (message) => { | ||
if (checks.length > 1) { | ||
checks.shift()!(message); | ||
} else { | ||
checks[0]!(message); | ||
} | ||
}); | ||
provider.send({ | ||
jsonrpc: "2.0", | ||
id: provider.nextId(), | ||
method: "chainHead_unstable_follow", | ||
params: [true], | ||
}); | ||
const subscriptionId = await pendingSubscriptionId; | ||
await initialized; | ||
provider.send({ | ||
jsonrpc: "2.0", | ||
id: provider.nextId(), | ||
method: "chainHead_unstable_unfollow", | ||
params: [subscriptionId], | ||
await t.step({ | ||
name: "parachain connection", | ||
async fn() { | ||
const relayChainSpec = await ( | ||
await fetch( | ||
"https://raw.githubusercontent.com/paritytech/substrate-connect/main/packages/connect/src/connector/specs/westend2.json", | ||
) | ||
) | ||
.text(); | ||
const parachainSpec = await ( | ||
await fetch( | ||
"https://raw.githubusercontent.com/paritytech/substrate-connect/main/projects/demo/src/assets/westend-westmint.json", | ||
) | ||
) | ||
.text(); | ||
const pendingSubscriptionId = deferred<string>(); | ||
const initialized = deferred(); | ||
const unsubscribed = deferred(); | ||
const checks: ProviderListener<any, any>[] = [ | ||
// check for chainHead_unstable_follow subscription | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
assertExists(message.result); | ||
pendingSubscriptionId.resolve(message.result); | ||
}, | ||
// check for chainHead_unstable_follow initialized event | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
assertExists(message.params?.result); | ||
if (message.params?.result.event === "initialized") { | ||
initialized.resolve(); | ||
} | ||
}, | ||
// check for chainHead_unstable_unfollow unsubscribe | ||
(message) => { | ||
assertNotInstanceOf(message, Error); | ||
if (message?.result === null) { | ||
unsubscribed.resolve(); | ||
} | ||
}, | ||
]; | ||
const provider = smoldotProvider( | ||
[parachainSpec, relayChainSpec], | ||
(message) => { | ||
if (checks.length > 1) { | ||
checks.shift()!(message); | ||
} else { | ||
checks[0]!(message); | ||
} | ||
}, | ||
); | ||
provider.send({ | ||
jsonrpc: "2.0", | ||
id: provider.nextId(), | ||
method: "chainHead_unstable_follow", | ||
params: [false], | ||
}); | ||
const subscriptionId = await pendingSubscriptionId; | ||
await initialized; | ||
provider.send({ | ||
jsonrpc: "2.0", | ||
id: provider.nextId(), | ||
method: "chainHead_unstable_unfollow", | ||
params: [subscriptionId], | ||
}); | ||
await unsubscribed; | ||
const providerRelease = await provider.release(); | ||
assertNotInstanceOf(providerRelease, Error); | ||
}, | ||
}); | ||
await unsubscribed; | ||
await provider.release(); | ||
}, | ||
}); |
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