From 627f990166fb1190c5e2d7168eeb3ad04a3ec218 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 3 Nov 2022 09:23:12 +0200 Subject: [PATCH] Align ScProvider code structure with other providers (#5313) * Align ScProvider code structure with other providers * Additional moves * Final adjustment * Remove old * Health adjustments --- CHANGELOG.md | 1 + packages/rpc-provider/src/bundle.ts | 2 +- .../src/substrate-connect/Health.ts | 40 +- .../src/substrate-connect/ScProvider.ts | 394 ------------------ .../{ScProvider.spec.ts => index.spec.ts} | 4 +- .../src/substrate-connect/index.ts | 393 ++++++++++++++++- 6 files changed, 413 insertions(+), 421 deletions(-) delete mode 100644 packages/rpc-provider/src/substrate-connect/ScProvider.ts rename packages/rpc-provider/src/substrate-connect/{ScProvider.spec.ts => index.spec.ts} (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c985be2ae..8307cc1def 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Contributed: Changes: +- Align `ScProvider` code structure with other providers - Update to latest Substrate metadata diff --git a/packages/rpc-provider/src/bundle.ts b/packages/rpc-provider/src/bundle.ts index 371e66b501..dc8f9c5cb5 100644 --- a/packages/rpc-provider/src/bundle.ts +++ b/packages/rpc-provider/src/bundle.ts @@ -3,5 +3,5 @@ export { HttpProvider } from './http'; export { packageInfo } from './packageInfo'; -export { WsProvider } from './ws'; export { ScProvider } from './substrate-connect'; +export { WsProvider } from './ws'; diff --git a/packages/rpc-provider/src/substrate-connect/Health.ts b/packages/rpc-provider/src/substrate-connect/Health.ts index d77263dc18..5ad64f59b7 100644 --- a/packages/rpc-provider/src/substrate-connect/Health.ts +++ b/packages/rpc-provider/src/substrate-connect/Health.ts @@ -1,6 +1,8 @@ // Copyright 2017-2022 @polkadot/rpc-provider authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { stringify } from '@polkadot/util'; + export interface SmoldotHealth { isSyncing: boolean peers: number @@ -105,10 +107,7 @@ class InnerChecker { #isSyncing = false; #nextRequestId = 0; - constructor ( - healthCallback: (health: SmoldotHealth) => void, - requestToSmoldot: (request: string) => void - ) { + constructor (healthCallback: (health: SmoldotHealth) => void, requestToSmoldot: (request: string) => void) { this.#healthCallback = healthCallback; this.#requestToSmoldot = requestToSmoldot; } @@ -124,12 +123,12 @@ class InnerChecker { } if (parsedRequest.id) { - const newId = 'extern:' + JSON.stringify(parsedRequest.id); + const newId = 'extern:' + stringify(parsedRequest.id); parsedRequest.id = newId; } - this.#requestToSmoldot(JSON.stringify(parsedRequest)); + this.#requestToSmoldot(stringify(parsedRequest)); }; responsePassThrough = (jsonRpcResponse: string): string | null => { @@ -220,7 +219,7 @@ class InnerChecker { parsedResponse.id = newId; } - return JSON.stringify(parsedResponse); + return stringify(parsedResponse); }; update = (startNow: boolean): void => { @@ -241,12 +240,11 @@ class InnerChecker { } // Actual request starting. - this.#currentHealthCheckId = 'health-checker:'.concat( - this.#nextRequestId.toString() - ); + this.#currentHealthCheckId = `health-checker:${this.#nextRequestId}`; this.#nextRequestId += 1; + this.#requestToSmoldot( - JSON.stringify({ + stringify({ id: this.#currentHealthCheckId, jsonrpc: '2.0', method: 'system_health', @@ -284,12 +282,11 @@ class InnerChecker { throw new Error('Internal error in health checker'); } - this.#currentSubunsubRequestId = 'health-checker:'.concat( - this.#nextRequestId.toString() - ); + this.#currentSubunsubRequestId = `health-checker:${this.#nextRequestId}`; this.#nextRequestId += 1; + this.#requestToSmoldot( - JSON.stringify({ + stringify({ id: this.#currentSubunsubRequestId, jsonrpc: '2.0', method: 'chain_subscribeNewHeads', @@ -303,12 +300,11 @@ class InnerChecker { throw new Error('Internal error in health checker'); } - this.#currentSubunsubRequestId = 'health-checker:'.concat( - this.#nextRequestId.toString() - ); + this.#currentSubunsubRequestId = `health-checker:${this.#nextRequestId}`; this.#nextRequestId += 1; + this.#requestToSmoldot( - JSON.stringify({ + stringify({ id: this.#currentSubunsubRequestId, jsonrpc: '2.0', method: 'chain_unsubscribeNewHeads', @@ -332,11 +328,9 @@ export class HealthCheckError extends Error { return this.#cause; } - constructor ( - response: unknown, - message = 'Got error response asking for system health' - ) { + constructor (response: unknown, message = 'Got error response asking for system health') { super(message); + this.#cause = response; } } diff --git a/packages/rpc-provider/src/substrate-connect/ScProvider.ts b/packages/rpc-provider/src/substrate-connect/ScProvider.ts deleted file mode 100644 index 0070c37201..0000000000 --- a/packages/rpc-provider/src/substrate-connect/ScProvider.ts +++ /dev/null @@ -1,394 +0,0 @@ -// Copyright 2017-2022 @polkadot/rpc-provider authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -import type { Config as ScConfig } from '@substrate/connect'; -import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '../types'; - -import { Chain, createScClient, ScClient, WellKnownChain } from '@substrate/connect'; -import EventEmitter from 'eventemitter3'; - -import { isError, logger, objectSpread } from '@polkadot/util'; - -import { RpcCoder } from '../coder'; -import { healthChecker } from './Health'; - -type ResponseCallback = (response: string | Error) => void - -const l = logger('api-substrate-connect'); - -// These methods have been taken from: -// https://github.com/paritytech/smoldot/blob/17425040ddda47d539556eeaf62b88c4240d1d42/src/json_rpc/methods.rs#L338-L462 -// It's important to take into account that smoldot is adding support to the new -// json-rpc-interface https://paritytech.github.io/json-rpc-interface-spec/ -// However, at the moment this list only includes methods that belong to the "old" API -const subscriptionUnsubscriptionMethods = new Map([ - ['author_submitAndWatchExtrinsic', 'author_unwatchExtrinsic'], - ['chain_subscribeAllHeads', 'chain_unsubscribeAllHeads'], - ['chain_subscribeFinalizedHeads', 'chain_unsubscribeFinalizedHeads'], - ['chain_subscribeFinalisedHeads', 'chain_subscribeFinalisedHeads'], - ['chain_subscribeNewHeads', 'chain_unsubscribeNewHeads'], - ['chain_subscribeNewHead', 'chain_unsubscribeNewHead'], - ['chain_subscribeRuntimeVersion', 'chain_unsubscribeRuntimeVersion'], - ['subscribe_newHead', 'unsubscribe_newHead'], - ['state_subscribeRuntimeVersion', 'state_unsubscribeRuntimeVersion'], - ['state_subscribeStorage', 'state_unsubscribeStorage'] -]); - -const wellKnownChains = new Set(Object.values(WellKnownChain)); -const scClients = new WeakMap(); - -export { WellKnownChain }; - -interface ActiveSubs { - type: string, - method: string, - params: any[], - callback: ProviderInterfaceCallback -} - -export class ScProvider implements ProviderInterface { - readonly #coder: RpcCoder = new RpcCoder(); - readonly #spec: string | WellKnownChain; - readonly #sharedSandbox?: ScProvider; - readonly #subscriptions: Map = new Map(); - readonly #resubscribeMethods: Map = new Map(); - readonly #requests: Map = new Map(); - readonly #eventemitter: EventEmitter = new EventEmitter(); - #chain: Promise | null = null; - #isChainReady = false; - - static WellKnownChain = WellKnownChain; - - public constructor (spec: string | WellKnownChain, sharedSandbox?: ScProvider) { - this.#spec = spec; - this.#sharedSandbox = sharedSandbox; - } - - public get hasSubscriptions (): boolean { - // Indicates that subscriptions are supported - return true; - } - - public get isConnected (): boolean { - return !!this.#chain && this.#isChainReady; - } - - public clone (): ProviderInterface { - throw new Error('clone() is not supported.'); - } - - // Config details can be found in @substrate/connect repo following the link: - // https://github.com/paritytech/substrate-connect/blob/main/packages/connect/src/connector/index.ts - async connect (config?: ScConfig, checkerFactory = healthChecker): Promise { - if (this.isConnected) { - throw new Error('Already connected!'); - } - - // it could happen that after emitting `disconnected` due to the fact taht - // smoldot is syncing, the consumer tries to reconnect after a certain amount - // of time... In which case we want to make sure that we don't create a new - // chain. - if (this.#chain) { - await this.#chain; - - return; - } - - if (this.#sharedSandbox && !this.#sharedSandbox.isConnected) { - await this.#sharedSandbox.connect(); - } - - const client = this.#sharedSandbox - ? scClients.get(this.#sharedSandbox) - : createScClient(config); - - if (!client) { - throw new Error('Unkown ScProvider!'); - } - - scClients.set(this, client); - - const hc = checkerFactory(); - - const onResponse = (res: string): void => { - const hcRes = hc.responsePassThrough(res); - - if (!hcRes) { - return; - } - - const response = JSON.parse(hcRes) as JsonRpcResponse; - let decodedResponse: string | Error; - - try { - decodedResponse = this.#coder.decodeResponse(response) as string; - } catch (e) { - decodedResponse = e as Error; - } - - // It's not a subscription message, but rather a standar RPC response - if (response.params?.subscription === undefined || !response.method) { - return this.#requests.get(response.id)?.(decodedResponse); - } - - // We are dealing with a subscription message - const subscriptionId = `${response.method}::${response.params.subscription}`; - - const callback = this.#subscriptions.get(subscriptionId)?.[0]; - - callback?.(decodedResponse); - }; - - const addChain = wellKnownChains.has(this.#spec as WellKnownChain) - ? client.addWellKnownChain - : client.addChain; - - this.#chain = addChain(this.#spec as WellKnownChain, onResponse).then((chain) => { - hc.setSendJsonRpc(chain.sendJsonRpc); - - this.#isChainReady = false; - - const cleanup = () => { - // If there are any callbacks left, we have to reject/error them. - // Otherwise, that would cause a memory leak. - const disconnectionError = new Error('Disconnected'); - - this.#requests.forEach((cb) => cb(disconnectionError)); - this.#subscriptions.forEach(([cb]) => cb(disconnectionError)); - this.#subscriptions.clear(); - }; - - const staleSubscriptions: { - unsubscribeMethod: string - id: number | string - }[] = []; - - const killStaleSubscriptions = () => { - if (staleSubscriptions.length === 0) { - return; - } - - const stale = staleSubscriptions.pop(); - - if (!stale) { - throw new Error('Unable to get stale subscription'); - } - - const { id, unsubscribeMethod } = stale; - - Promise - .race([ - this.send(unsubscribeMethod, [id]).catch(() => undefined), - new Promise((resolve) => setTimeout(resolve, 500)) - ]) - .then(killStaleSubscriptions) - .catch(() => undefined); - }; - - hc.start((health) => { - const isReady = - !health.isSyncing && (health.peers > 0 || !health.shouldHavePeers); - - // if it's the same as before, then nothing has changed and we are done - if (this.#isChainReady === isReady) { - return; - } - - this.#isChainReady = isReady; - - if (!isReady) { - // If we've reached this point, that means that the chain used to be "ready" - // and now we are about to emit `disconnected`. - // - // This will cause the PolkadotJs API think that the connection is - // actually dead. In reality the smoldot chain is not dead, of course. - // However, we have to cleanup all the existing callbacks because when - // the smoldot chain stops syncing, then we will emit `connected` and - // the PolkadotJs API will try to re-create the previous - // subscriptions and requests. Although, now is not a good moment - // to be sending unsubscription messages to the smoldot chain, we - // should wait until is no longer syncing to send the unsubscription - // messages from the stale subscriptions of the previous connection. - // - // That's why -before we perform the cleanup of `this.#subscriptions`- - // we keep the necessary information that we will need later on to - // kill the stale subscriptions. - [...this.#subscriptions.values()].forEach((s) => { - staleSubscriptions.push(s[1]); - }); - cleanup(); - - this.#eventemitter.emit('disconnected'); - } else { - killStaleSubscriptions(); - - this.#eventemitter.emit('connected'); - - if (this.#resubscribeMethods.size) { - this.#resubscribe(); - } - } - }); - - return objectSpread({}, chain, { - remove: () => { - hc.stop(); - chain.remove(); - cleanup(); - }, - sendJsonRpc: hc.sendJsonRpc.bind(hc) - }); - }); - - try { - await this.#chain; - } catch (e) { - this.#chain = null; - this.#eventemitter.emit('error', e); - throw e; - } - } - - #resubscribe = (): void => { - const promises: any[] = []; - - this.#resubscribeMethods.forEach((subDetails: ActiveSubs): void => { - // only re-create subscriptions which are not in author (only area where - // transactions are created, i.e. submissions such as 'author_submitAndWatchExtrinsic' - // are not included (and will not be re-broadcast) - if (subDetails.type.startsWith('author_')) { - return; - } - - try { - const promise: Promise = new Promise((resolve) => { - this.subscribe(subDetails.type, subDetails.method, subDetails.params, subDetails.callback).catch((error) => console.log(error)); - resolve(); - }); - - promises.push(promise); - } catch (error) { - l.error(error); - } - }); - - Promise.all(promises).catch((err) => l.log(err)); - }; - - async disconnect (): Promise { - if (!this.#chain) { - return; - } - - const chain = await this.#chain; - - this.#chain = null; - this.#isChainReady = false; - - try { - chain.remove(); - } catch (_) {} - - this.#eventemitter.emit('disconnected'); - } - - public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void { - // It's possible. Although, quite unlikely, that by the time that polkadot - // subscribes to the `connected` event, the Provider is already connected. - // In that case, we must emit to let the consumer know that we are connected. - if (type === 'connected' && this.isConnected) { - sub(); - } - - this.#eventemitter.on(type, sub); - - return (): void => { - this.#eventemitter.removeListener(type, sub); - }; - } - - public async send (method: string, params: unknown[]): Promise { - if (!this.isConnected || !this.#chain) { - throw new Error('Provider is not connected'); - } - - const chain = await this.#chain; - const [id, json] = this.#coder.encodeJson(method, params); - - const result = new Promise((resolve, reject): void => { - this.#requests.set(id, (response) => { - (isError(response) ? reject : resolve)(response as unknown as T); - }); - - try { - chain.sendJsonRpc(json); - } catch (e) { - this.#chain = null; - - try { - chain.remove(); - } catch (_) {} - - this.#eventemitter.emit('error', e); - } - }); - - try { - return await result; - } finally { - // let's ensure that once the Promise is resolved/rejected, then we remove - // remove its entry from the internal #requests - this.#requests.delete(id); - } - } - - public async subscribe (type: string, method: string, params: any[], callback: ProviderInterfaceCallback): Promise { - if (!subscriptionUnsubscriptionMethods.has(method)) { - throw new Error(`Unsupported subscribe method: ${method}`); - } - - const id = await this.send(method, params); - const subscriptionId = `${type}::${id}`; - - const cb = (response: Error | string) => { - if (response instanceof Error) { - callback(response, undefined); - } else { - callback(null, response); - } - }; - - const unsubscribeMethod = subscriptionUnsubscriptionMethods.get(method); - - if (!unsubscribeMethod) { - throw new Error('Invalid unsubscribe method found'); - } - - this.#resubscribeMethods.set(subscriptionId, { callback, method, params, type }); - - this.#subscriptions.set(subscriptionId, [cb, { id, unsubscribeMethod }]); - - return id; - } - - public unsubscribe (type: string, method: string, id: number | string): Promise { - if (!this.isConnected) { - throw new Error('Provider is not connected'); - } - - const subscriptionId = `${type}::${id}`; - - if (!this.#subscriptions.has(subscriptionId)) { - return Promise.reject( - new Error(`Unable to find active subscription=${subscriptionId}`) - ); - } - - this.#resubscribeMethods.delete(subscriptionId); - this.#subscriptions.delete(subscriptionId); - - return this.send(method, [id]); - } -} -export type ScProviderClass = typeof ScProvider -export type Config = ScConfig diff --git a/packages/rpc-provider/src/substrate-connect/ScProvider.spec.ts b/packages/rpc-provider/src/substrate-connect/index.spec.ts similarity index 99% rename from packages/rpc-provider/src/substrate-connect/ScProvider.spec.ts rename to packages/rpc-provider/src/substrate-connect/index.spec.ts index 45cfa78bb0..115cef1f89 100644 --- a/packages/rpc-provider/src/substrate-connect/ScProvider.spec.ts +++ b/packages/rpc-provider/src/substrate-connect/index.spec.ts @@ -19,8 +19,8 @@ // Jest has "some" issues with `await import` - we don't transform these import type { Chain, JsonRpcCallback } from '@substrate/connect'; +import type { ScProviderClass } from '.'; import type { HealthChecker, SmoldotHealth } from './Health'; -import type { ScProviderClass } from './ScProvider'; import { jest } from '@jest/globals'; @@ -198,7 +198,7 @@ const setChainSyncyingStatus = (isSyncing: boolean) => { }; beforeAll(async () => { - ({ ScProvider } = await import('./ScProvider')); + ({ ScProvider } = await import('.')); mockedConnector = (await import( '@substrate/connect' )) as unknown as ReturnType; diff --git a/packages/rpc-provider/src/substrate-connect/index.ts b/packages/rpc-provider/src/substrate-connect/index.ts index 5a1448de0a..d94ba26823 100644 --- a/packages/rpc-provider/src/substrate-connect/index.ts +++ b/packages/rpc-provider/src/substrate-connect/index.ts @@ -1,4 +1,395 @@ // Copyright 2017-2022 @polkadot/rpc-provider authors & contributors // SPDX-License-Identifier: Apache-2.0 -export * from './ScProvider'; +import type { Config as ScConfig } from '@substrate/connect'; +import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '../types'; + +import { Chain, createScClient, ScClient, WellKnownChain } from '@substrate/connect'; +import EventEmitter from 'eventemitter3'; + +import { isError, logger, objectSpread } from '@polkadot/util'; + +import { RpcCoder } from '../coder'; +import { healthChecker } from './Health'; + +type ResponseCallback = (response: string | Error) => void + +const l = logger('api-substrate-connect'); + +// These methods have been taken from: +// https://github.com/paritytech/smoldot/blob/17425040ddda47d539556eeaf62b88c4240d1d42/src/json_rpc/methods.rs#L338-L462 +// It's important to take into account that smoldot is adding support to the new +// json-rpc-interface https://paritytech.github.io/json-rpc-interface-spec/ +// However, at the moment this list only includes methods that belong to the "old" API +const subscriptionUnsubscriptionMethods = new Map([ + ['author_submitAndWatchExtrinsic', 'author_unwatchExtrinsic'], + ['chain_subscribeAllHeads', 'chain_unsubscribeAllHeads'], + ['chain_subscribeFinalizedHeads', 'chain_unsubscribeFinalizedHeads'], + ['chain_subscribeFinalisedHeads', 'chain_subscribeFinalisedHeads'], + ['chain_subscribeNewHeads', 'chain_unsubscribeNewHeads'], + ['chain_subscribeNewHead', 'chain_unsubscribeNewHead'], + ['chain_subscribeRuntimeVersion', 'chain_unsubscribeRuntimeVersion'], + ['subscribe_newHead', 'unsubscribe_newHead'], + ['state_subscribeRuntimeVersion', 'state_unsubscribeRuntimeVersion'], + ['state_subscribeStorage', 'state_unsubscribeStorage'] +]); + +const wellKnownChains = new Set(Object.values(WellKnownChain)); +const scClients = new WeakMap(); + +export { WellKnownChain }; + +interface ActiveSubs { + type: string, + method: string, + params: any[], + callback: ProviderInterfaceCallback +} + +export class ScProvider implements ProviderInterface { + readonly #coder: RpcCoder = new RpcCoder(); + readonly #spec: string | WellKnownChain; + readonly #sharedSandbox?: ScProvider; + readonly #subscriptions: Map = new Map(); + readonly #resubscribeMethods: Map = new Map(); + readonly #requests: Map = new Map(); + readonly #eventemitter: EventEmitter = new EventEmitter(); + #chain: Promise | null = null; + #isChainReady = false; + + static WellKnownChain = WellKnownChain; + + public constructor (spec: string | WellKnownChain, sharedSandbox?: ScProvider) { + this.#spec = spec; + this.#sharedSandbox = sharedSandbox; + } + + public get hasSubscriptions (): boolean { + // Indicates that subscriptions are supported + return true; + } + + public get isConnected (): boolean { + return !!this.#chain && this.#isChainReady; + } + + public clone (): ProviderInterface { + throw new Error('clone() is not supported.'); + } + + // Config details can be found in @substrate/connect repo following the link: + // https://github.com/paritytech/substrate-connect/blob/main/packages/connect/src/connector/index.ts + async connect (config?: ScConfig, checkerFactory = healthChecker): Promise { + if (this.isConnected) { + throw new Error('Already connected!'); + } + + // it could happen that after emitting `disconnected` due to the fact taht + // smoldot is syncing, the consumer tries to reconnect after a certain amount + // of time... In which case we want to make sure that we don't create a new + // chain. + if (this.#chain) { + await this.#chain; + + return; + } + + if (this.#sharedSandbox && !this.#sharedSandbox.isConnected) { + await this.#sharedSandbox.connect(); + } + + const client = this.#sharedSandbox + ? scClients.get(this.#sharedSandbox) + : createScClient(config); + + if (!client) { + throw new Error('Unkown ScProvider!'); + } + + scClients.set(this, client); + + const hc = checkerFactory(); + + const onResponse = (res: string): void => { + const hcRes = hc.responsePassThrough(res); + + if (!hcRes) { + return; + } + + const response = JSON.parse(hcRes) as JsonRpcResponse; + let decodedResponse: string | Error; + + try { + decodedResponse = this.#coder.decodeResponse(response) as string; + } catch (e) { + decodedResponse = e as Error; + } + + // It's not a subscription message, but rather a standar RPC response + if (response.params?.subscription === undefined || !response.method) { + return this.#requests.get(response.id)?.(decodedResponse); + } + + // We are dealing with a subscription message + const subscriptionId = `${response.method}::${response.params.subscription}`; + + const callback = this.#subscriptions.get(subscriptionId)?.[0]; + + callback?.(decodedResponse); + }; + + const addChain = wellKnownChains.has(this.#spec as WellKnownChain) + ? client.addWellKnownChain + : client.addChain; + + this.#chain = addChain(this.#spec as WellKnownChain, onResponse).then((chain) => { + hc.setSendJsonRpc(chain.sendJsonRpc); + + this.#isChainReady = false; + + const cleanup = () => { + // If there are any callbacks left, we have to reject/error them. + // Otherwise, that would cause a memory leak. + const disconnectionError = new Error('Disconnected'); + + this.#requests.forEach((cb) => cb(disconnectionError)); + this.#subscriptions.forEach(([cb]) => cb(disconnectionError)); + this.#subscriptions.clear(); + }; + + const staleSubscriptions: { + unsubscribeMethod: string + id: number | string + }[] = []; + + const killStaleSubscriptions = () => { + if (staleSubscriptions.length === 0) { + return; + } + + const stale = staleSubscriptions.pop(); + + if (!stale) { + throw new Error('Unable to get stale subscription'); + } + + const { id, unsubscribeMethod } = stale; + + Promise + .race([ + this.send(unsubscribeMethod, [id]).catch(() => undefined), + new Promise((resolve) => setTimeout(resolve, 500)) + ]) + .then(killStaleSubscriptions) + .catch(() => undefined); + }; + + hc.start((health) => { + const isReady = + !health.isSyncing && (health.peers > 0 || !health.shouldHavePeers); + + // if it's the same as before, then nothing has changed and we are done + if (this.#isChainReady === isReady) { + return; + } + + this.#isChainReady = isReady; + + if (!isReady) { + // If we've reached this point, that means that the chain used to be "ready" + // and now we are about to emit `disconnected`. + // + // This will cause the PolkadotJs API think that the connection is + // actually dead. In reality the smoldot chain is not dead, of course. + // However, we have to cleanup all the existing callbacks because when + // the smoldot chain stops syncing, then we will emit `connected` and + // the PolkadotJs API will try to re-create the previous + // subscriptions and requests. Although, now is not a good moment + // to be sending unsubscription messages to the smoldot chain, we + // should wait until is no longer syncing to send the unsubscription + // messages from the stale subscriptions of the previous connection. + // + // That's why -before we perform the cleanup of `this.#subscriptions`- + // we keep the necessary information that we will need later on to + // kill the stale subscriptions. + [...this.#subscriptions.values()].forEach((s) => { + staleSubscriptions.push(s[1]); + }); + cleanup(); + + this.#eventemitter.emit('disconnected'); + } else { + killStaleSubscriptions(); + + this.#eventemitter.emit('connected'); + + if (this.#resubscribeMethods.size) { + this.#resubscribe(); + } + } + }); + + return objectSpread({}, chain, { + remove: () => { + hc.stop(); + chain.remove(); + cleanup(); + }, + sendJsonRpc: hc.sendJsonRpc.bind(hc) + }); + }); + + try { + await this.#chain; + } catch (e) { + this.#chain = null; + this.#eventemitter.emit('error', e); + throw e; + } + } + + #resubscribe = (): void => { + const promises: any[] = []; + + this.#resubscribeMethods.forEach((subDetails: ActiveSubs): void => { + // only re-create subscriptions which are not in author (only area where + // transactions are created, i.e. submissions such as 'author_submitAndWatchExtrinsic' + // are not included (and will not be re-broadcast) + if (subDetails.type.startsWith('author_')) { + return; + } + + try { + const promise: Promise = new Promise((resolve) => { + this.subscribe(subDetails.type, subDetails.method, subDetails.params, subDetails.callback).catch((error) => console.log(error)); + resolve(); + }); + + promises.push(promise); + } catch (error) { + l.error(error); + } + }); + + Promise.all(promises).catch((err) => l.log(err)); + }; + + async disconnect (): Promise { + if (!this.#chain) { + return; + } + + const chain = await this.#chain; + + this.#chain = null; + this.#isChainReady = false; + + try { + chain.remove(); + } catch (_) {} + + this.#eventemitter.emit('disconnected'); + } + + public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void { + // It's possible. Although, quite unlikely, that by the time that polkadot + // subscribes to the `connected` event, the Provider is already connected. + // In that case, we must emit to let the consumer know that we are connected. + if (type === 'connected' && this.isConnected) { + sub(); + } + + this.#eventemitter.on(type, sub); + + return (): void => { + this.#eventemitter.removeListener(type, sub); + }; + } + + public async send (method: string, params: unknown[]): Promise { + if (!this.isConnected || !this.#chain) { + throw new Error('Provider is not connected'); + } + + const chain = await this.#chain; + const [id, json] = this.#coder.encodeJson(method, params); + + const result = new Promise((resolve, reject): void => { + this.#requests.set(id, (response) => { + (isError(response) ? reject : resolve)(response as unknown as T); + }); + + try { + chain.sendJsonRpc(json); + } catch (e) { + this.#chain = null; + + try { + chain.remove(); + } catch (_) {} + + this.#eventemitter.emit('error', e); + } + }); + + try { + return await result; + } finally { + // let's ensure that once the Promise is resolved/rejected, then we remove + // remove its entry from the internal #requests + this.#requests.delete(id); + } + } + + public async subscribe (type: string, method: string, params: any[], callback: ProviderInterfaceCallback): Promise { + if (!subscriptionUnsubscriptionMethods.has(method)) { + throw new Error(`Unsupported subscribe method: ${method}`); + } + + const id = await this.send(method, params); + const subscriptionId = `${type}::${id}`; + + const cb = (response: Error | string) => { + if (response instanceof Error) { + callback(response, undefined); + } else { + callback(null, response); + } + }; + + const unsubscribeMethod = subscriptionUnsubscriptionMethods.get(method); + + if (!unsubscribeMethod) { + throw new Error('Invalid unsubscribe method found'); + } + + this.#resubscribeMethods.set(subscriptionId, { callback, method, params, type }); + + this.#subscriptions.set(subscriptionId, [cb, { id, unsubscribeMethod }]); + + return id; + } + + public unsubscribe (type: string, method: string, id: number | string): Promise { + if (!this.isConnected) { + throw new Error('Provider is not connected'); + } + + const subscriptionId = `${type}::${id}`; + + if (!this.#subscriptions.has(subscriptionId)) { + return Promise.reject( + new Error(`Unable to find active subscription=${subscriptionId}`) + ); + } + + this.#resubscribeMethods.delete(subscriptionId); + this.#subscriptions.delete(subscriptionId); + + return this.send(method, [id]); + } +} + +export type ScProviderClass = typeof ScProvider +export type Config = ScConfig