# All files / SmoldotProvider SmoldotProvider.ts

89.17% Statements 107/120
86.36% Branches 38/44
68% Functions 17/25
93.04% Lines 107/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420

    1x

    1x

    1x

    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x
    16x

    16x

    16x

    16x

    16x
    16x

    16x
    45x

    45x

    45x

    16x
    39x

    39x

    39x
    39x
    39x

    37x

    37x
    5x

    5x

    5x
    1x

    2x

    39x

    16x
    7x
    7x
    7x

    7x

    3x

    3x

    3x

    4x

    4x
    4x

    4x

    16x

    26x

    19x
    1x
    1x
    1x
    1x

    18x

    7x
    7x

    7x
    7x

    7x

    7x
    2x
    2x
    2x
    2x

    5x
    4x
    4x
    4x
    4x

    16x
    27x

    1x

    16x
    16x
    16x
    16x

    45x

    15x

    1x

    16x
    16x

    16x

    16x
    16x

    16x
    16x

    12x

    12x
    3x

    39x
    39x
    39x
    39x

    39x
    39x

    39x

    39x

    39x

    5x

    2x

    2x
    1x

    1x

    1x

    1x

    25x

     import {RpcCoder} from '@polkadot/rpc-provider/coder';
    import {
      JsonRpcResponse,
      ProviderInterface,
      ProviderInterfaceCallback,
      ProviderInterfaceEmitCb,
      ProviderInterfaceEmitted,
    } from '@polkadot/rpc-provider/types';
    import { assert, logger } from '@polkadot/util';
    import EventEmitter from 'eventemitter3';
    import * as smoldot from 'smoldot';
    import { HealthCheckError } from './errors.js';
    import { isUndefined } from '../utils/index.js';

    const l = logger('smoldot-provider');

    interface RpcStateAwaiting {
      callback: ProviderInterfaceCallback;
      method: string;
      subscription?: SubscriptionHandler;
    }

    interface SubscriptionHandler {
      callback: ProviderInterfaceCallback;
      // type is the value of the method property in the JSON responses to this
      // subscription
      type: string;
    }

    interface StateSubscription extends SubscriptionHandler {
        method: string;
    }

    interface HealthResponse {
      isSyncing: boolean;
      peers: number;
      shouldHavePeers: boolean;
    }

    const ANGLICISMS: { [index: string]: string } = {
      chain_finalisedHead: 'chain_finalizedHead',
      chain_subscribeFinalisedHeads: 'chain_subscribeFinalizedHeads',
      chain_unsubscribeFinalisedHeads: 'chain_unsubscribeFinalizedHeads'
    };

    /*
     * Number of milliseconds to wait between checks to see if we have any
     * connected peers in the smoldot client
     */
    const CONNECTION_STATE_PINGER_INTERVAL = 2000;

    /**
     * SmoldotProvider
     *
     * The SmoldotProvider allows interacting with a smoldot-based
     * WASM light client.  I.e. without doing RPC to a remote node over HTTP
     * or websockets
     *
     * @example
     * ```javascript
     * import readFileSync from 'fs';
     * import Api from '@polkadot/api/promise';
     * import { SmoldotProvider } from '../';
     *
     * const chainSpec = readFileSync('./path/to/chainSpec.json');
     * const provider = new SmoldotProvider(chainSpec);
     * const api = new Api(provider);
     * ```
     * @example
     * ```javascript
     * import readFileSync from 'fs';
     * import Api from '@polkadot/api/promise';
     * import { SmoldotProvider } from '../';
     *
     * const chainSpec = readFileSync('./path/to/polkadot.json');
     * const pp = new SmoldotProvider(chainSpec);
     * const polkadotApi = new Api(pp);
     *
     * const chainSpec = readFileSync('./path/to/kusama.json');
     * const kp = new SmoldotProvider(chainSpec);
     * const kusamaApi = new Api(pp);
     * ```
     */
    export class SmoldotProvider implements ProviderInterface {
      #chainSpec: string;
      readonly #coder: RpcCoder = new RpcCoder();
      readonly #eventemitter: EventEmitter = new EventEmitter();
      readonly #handlers: Record<string, RpcStateAwaiting> = {};
      readonly #subscriptions: Record<string, StateSubscription> = {};
      readonly #waitingForId: Record<string, JsonRpcResponse> = {};
      #connectionStatePingerId: ReturnType<typeof setInterval> | null;
      #isConnected = false;
      #client: smoldot.SmoldotClient | undefined = undefined;
      // reference to the smoldot module so we can defer loading the wasm client
      // until connect is called
      #smoldot: smoldot.Smoldot;

      /*
       * How frequently to see if we have any peers
       */
      healthPingerInterval = CONNECTION_STATE_PINGER_INTERVAL;

       /**
       * @param chainSpec - The chainSpec for the WASM client
       * @param sm - (only used for testing) defaults to the actual smoldot module
       */
      //eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types
      public constructor(chainSpec: string, sm?: any) {
        this.#chainSpec = chainSpec;
        // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
        this.#smoldot = sm || smoldot;
        this.#connectionStatePingerId = null;
      }

      /**
       * Lets polkadot-js know we support subscriptions
       * @returns `true`
       */
      public get hasSubscriptions(): boolean {
        return true;
      }

      /**
       * Returns a clone of the object
       * @throws throws an error as this is not supported.
       */
      public clone(): SmoldotProvider {
        throw new Error('clone() is not supported.');
      }

      #handleRpcReponse = (res: string): void => {
        l.debug(() => ['received', res]);

        const response = JSON.parse(res) as JsonRpcResponse;

        return isUndefined(response.method)
          ? this.#onMessageResult(response)
          : this.#onMessageSubscribe(response);
      }

     #onMessageResult = (response: JsonRpcResponse): void => {
        const handler = this.#handlers[response.id];

        Iif (!handler) {
          l.debug(() => `Unable to find handler for id=${response.id}`);

          return;
        }

        try {
          const { method, subscription } = handler;
          const result = this.#coder.decodeResponse(response) as string;

          // first send the result - in case of subs, we may have an update
          // immediately if we have some queued results already
          handler.callback(null, result);

          if (subscription) {
            const subId = `${subscription.type}::${result}`;

            this.#subscriptions[subId] = {
              ...subscription,
              method
            };

            // if we have a result waiting for this subscription already
            if (this.#waitingForId[subId]) {
              this.#onMessageSubscribe(this.#waitingForId[subId]);
            }
          }
        } catch (error) {
          handler.callback(error, undefined);
        }

        delete this.#handlers[response.id];
      }

      #onMessageSubscribe = (response: JsonRpcResponse): void => {
        const method = ANGLICISMS[response.method as string] || response.method || 'invalid';
        const subId = `${method}::${response.params.subscription}`;
        const handler = this.#subscriptions[subId];

        if (!handler) {
          // store the response, we could have out-of-order subid coming in
          this.#waitingForId[subId] = response;

          l.debug(() => `Unable to find handler for subscription=${subId} responseId=${response.id}`);

          return;
        }

        // housekeeping
        delete this.#waitingForId[subId];

        try {
          const result = this.#coder.decodeResponse(response);

          handler.callback(null, result);
        } catch (error) {
          handler.callback(error, undefined);
        }
      }

      #simulateLifecycle = (health: HealthResponse): void => {
        // development chains should not have peers so we only emit connected
        // once and never disconnect
        if (health.shouldHavePeers == false) {

          if (!this.#isConnected) {
            this.#isConnected = true;
            this.emit('connected');
            l.debug(`emitted CONNECTED`);
            return;
          }

          return;
        }

        const peerCount = health.peers
        const peerChecks = (peerCount > 0 || !health.shouldHavePeers) && !health.isSyncing;

        l.debug(`Simulating lifecylce events from system_health`);
        l.debug(`isConnected: ${this.#isConnected.toString()}, new peerCount: ${peerCount}`);

        Iif (this.#isConnected && peerChecks) {
          // still connected
          return;
        }

        if (this.#isConnected && peerCount === 0) {
          this.#isConnected = false;
          this.emit('disconnected');
          l.debug(`emitted DISCONNECTED`);
          return;
        }

        if (!this.#isConnected && peerChecks) {
          this.#isConnected = true;
          this.emit('connected');
          l.debug(`emitted CONNECTED`);
          return;
        }

        // still not connected
      }

      #checkClientPeercount = (): void => {
        this.send('system_health', [])
          .then(this.#simulateLifecycle)
          .catch(error => this.emit('error', new HealthCheckError(error)));
      }

      /**
       * "Connect" the WASM client - starts the smoldot WASM client
       */
      public connect = async (): Promise<void> => {
        assert(!this.#client && !this.#isConnected, 'Client is already connected');
        try {
          this.#client = await this.#smoldot.start({
            forbidWs: true, /* suppress console warnings about insecure connections */
            chainSpecs: [this.#chainSpec],
            maxLogLevel: 3, /* no debug/trace messages */
            jsonRpcCallback: (response: string) => {
                this.#handleRpcReponse(response);
            }
          })
          this.#connectionStatePingerId = setInterval(
          this.#checkClientPeercount, this.healthPingerInterval);
        } catch(error: unknown) {
          this.emit('error', error);
        }
      }

      /**
       * Manually "disconnect" - drops the reference to the WASM client
       */
      // eslint-disable-next-line @typescript-eslint/require-await
      public async disconnect(): Promise<void> {
        try {
            Eif (this.#client) {
              // eslint-disable-next-line @typescript-eslint/no-unsafe-call
              this.#client.terminate();
            }
          } catch(error: unknown) {
            this.emit('error', error);
          } finally {
            Eif (this.#connectionStatePingerId !== null) {
              clearInterval(this.#connectionStatePingerId);
            }

            this.#isConnected = false;
            this.emit('disconnected');
          }
      }

      /**
       * Whether the node is connected or not.
       * @returns true if connected
       */
      public get isConnected (): boolean {
        return this.#isConnected;
      }

      /**
       * Listen to provider events - in practice the smoldot provider only
       * emits a `connected` event after successfully starting the smoldot client
       * and `disconnected` after `disconnect` is called.
       * @param type - Event
       * @param sub  - Callback
       */
      public on(
        type: ProviderInterfaceEmitted,
        sub: ProviderInterfaceEmitCb
      ): () => void {
        this.#eventemitter.on(type, sub);

        return (): void => {
          this.#eventemitter.removeListener(type, sub);
        };
      }

      /**
       * Send an RPC request  the wasm client
       * @param method       - The RPC methods to execute
       * @param params       - Encoded paramaters as applicable for the method
       * @param subscription - Subscription details (internally used by `subscribe`)
       */
      public async send(
        method: string,
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        params: any[],
        subscription?: SubscriptionHandler
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
      ): Promise<any> {
        return new Promise((resolve, reject): void => {
            assert(this.#client, 'Client is not initialised');
            const json = this.#coder.encodeJson(method, params);
            const id = this.#coder.getId();

            const callback = (error?: Error | null, result?: unknown): void => {
              error
                ? reject(error)
                : resolve(result);
            };

            l.debug(() => ['calling', method, json]);

            this.#handlers[id] = {
              callback,
              method,
              subscription
            };

          this.#client.sendJsonRpc(json, 0);
        });
      }

      /**
       * subscribe
       * Allows subscribing to a specific event.
       * @param  type     - Subscription type
       * @param  method   - Subscription method
       * @param  params   - Parameters of type any[]
       * @param  callback - ProviderInterfaceCallback
       * @returns A promise (Promise\<number|string\>) resolving to the id of the subscription you can use with [[unsubscribe]].
       *
       * @example
       * <BR>
       *
       * ```javascript
       * const provider = new SmoldotProvider(client);
       * const rpc = new Rpc(provider);
       *
       * rpc.state.subscribeStorage([[storage.balances.freeBalance, <Address>]], (_, values) => {
       *   console.log(values)
       * }).then((subscriptionId) => {
       *   console.log('balance changes subscription id: ', subscriptionId)
       * })
       * ```
       */
      public async subscribe(
        // the "method" property of the JSON response to this subscription
        type: string,
        // the "method" property of the JSON request to register the subscription
        method: string,
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        params: any[],
        callback: ProviderInterfaceCallback
      ): Promise<number | string> {
        // eslint-disable-next-line @typescript-eslint/no-unsafe-return
        return await this.send(method, params, { callback, type });
      }

      /**
       * Allows unsubscribing to subscriptions made with [[subscribe]].
       */
      public async unsubscribe(
        type: string,
        method: string,
        id: number | string
      ): Promise<boolean> {
        const subscription = `${type}::${id}`;

        if (isUndefined(this.#subscriptions[subscription])) {
          l.debug(() => `Unable to find active subscription=${subscription}`);

          return false;
        }

        delete this.#subscriptions[subscription];

        return await this.send(method, [id]) as Promise<boolean>;
      }

      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      private emit(type: ProviderInterfaceEmitted, ...args: any[]): void {
        this.#eventemitter.emit(type, ...args);
      }
    }