smoldot 2.0.18 → 2.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -66,7 +66,8 @@ function start(options, wasmModule, platformBindings) {
66
66
  instance: { status: "not-created" },
67
67
  chainIds: new WeakMap(),
68
68
  connections: new Map(),
69
- addChainResults: [],
69
+ addChainIdAllocations: [],
70
+ addChainResults: new Map(),
70
71
  onExecutorShutdownOrWasmPanic: () => { },
71
72
  chains: new Map(),
72
73
  };
@@ -85,10 +86,14 @@ function start(options, wasmModule, platformBindings) {
85
86
  };
86
87
  state.connections.forEach((connec) => connec.reset());
87
88
  state.connections.clear();
88
- for (const addChainResult of state.addChainResults) {
89
+ for (const addChainResult of state.addChainIdAllocations) {
89
90
  addChainResult({ success: false, error: "Smoldot has crashed" });
90
91
  }
91
- state.addChainResults = [];
92
+ state.addChainIdAllocations = [];
93
+ state.addChainResults.forEach((addChainResult) => {
94
+ addChainResult({ success: false, error: "Smoldot has crashed" });
95
+ });
96
+ state.addChainResults.clear();
92
97
  for (const chain of Array.from(state.chains.values())) {
93
98
  for (const callback of chain.jsonRpcResponsesPromises) {
94
99
  callback();
@@ -111,8 +116,14 @@ function start(options, wasmModule, platformBindings) {
111
116
  logCallback(event.level, event.target, event.message);
112
117
  break;
113
118
  }
119
+ case "add-chain-id-allocated": {
120
+ const callback = state.addChainIdAllocations.shift();
121
+ state.addChainResults.set(event.chainId, callback);
122
+ break;
123
+ }
114
124
  case "add-chain-result": {
115
- (state.addChainResults.shift())(event);
125
+ (state.addChainResults.get(event.chainId))(event);
126
+ state.addChainResults.delete(event.chainId);
116
127
  break;
117
128
  }
118
129
  case "json-rpc-responses-non-empty": {
@@ -290,7 +301,7 @@ function start(options, wasmModule, platformBindings) {
290
301
  // Sanitize `databaseContent`.
291
302
  if (options.databaseContent !== undefined && typeof options.databaseContent !== 'string')
292
303
  throw new public_types_js_1.AddChainError("`databaseContent` is not a string");
293
- const promise = new Promise((resolve) => state.addChainResults.push(resolve));
304
+ const promise = new Promise((resolve) => state.addChainIdAllocations.push(resolve));
294
305
  state.instance.instance.addChain(options.chainSpec, options.databaseContent || "", potentialRelayChainsIds, !!options.disableJsonRpc, jsonRpcMaxPendingRequests, jsonRpcMaxSubscriptions);
295
306
  const outcome = yield promise;
296
307
  if (!outcome.success)
@@ -371,10 +382,14 @@ function start(options, wasmModule, platformBindings) {
371
382
  state.instance = { status: "destroyed", error: new public_types_js_1.AlreadyDestroyedError() };
372
383
  state.connections.forEach((connec) => connec.reset());
373
384
  state.connections.clear();
374
- for (const addChainResult of state.addChainResults) {
385
+ for (const addChainResult of state.addChainIdAllocations) {
375
386
  addChainResult({ success: false, error: "Client.terminate() has been called" });
376
387
  }
377
- state.addChainResults = [];
388
+ state.addChainIdAllocations = [];
389
+ state.addChainResults.forEach((addChainResult) => {
390
+ addChainResult({ success: false, error: "Client.terminate() has been called" });
391
+ });
392
+ state.addChainResults.clear();
378
393
  for (const chain of Array.from(state.chains.values())) {
379
394
  for (const callback of chain.jsonRpcResponsesPromises) {
380
395
  callback();
@@ -30,11 +30,15 @@ export interface Config {
30
30
  getRandomValues: (buffer: Uint8Array) => void;
31
31
  }
32
32
  export type Event = {
33
+ ty: "add-chain-id-allocated";
34
+ chainId: number;
35
+ } | {
33
36
  ty: "add-chain-result";
34
- success: true;
35
37
  chainId: number;
38
+ success: true;
36
39
  } | {
37
40
  ty: "add-chain-result";
41
+ chainId: number;
38
42
  success: false;
39
43
  error: string;
40
44
  } | {
@@ -92,6 +96,15 @@ export type ParsedMultiaddr = {
92
96
  export interface Instance {
93
97
  request: (request: string, chainId: number) => number;
94
98
  peekJsonRpcResponse: (chainId: number) => string | null;
99
+ /**
100
+ * Later generates a `add-chain-id-allocated` event.
101
+ * If this function is called multiple times, the `add-chain-id-allocated` events are generated
102
+ * in the same order as the function calls.
103
+ *
104
+ * Then, later and asynchronously, a `add-chain-result` event is generated in order to
105
+ * indicate whether the initialization was actually successful. If `success` is `false`, then
106
+ * the `chainId` becomes unallocated.
107
+ */
95
108
  addChain: (chainSpec: string, databaseContent: string, potentialRelayChains: number[], disableJsonRpc: boolean, jsonRpcMaxPendingRequests: number, jsonRpcMaxSubscriptions: number) => void;
96
109
  removeChain: (chainId: number) => void;
97
110
  /**
@@ -59,6 +59,19 @@ function startLocalInstance(config, wasmModule, eventCallback) {
59
59
  state.onShutdownExecutorOrWasmPanic = () => { };
60
60
  throw new Error();
61
61
  },
62
+ chain_initialized: (chainId, errorMsgPtr, errorMsgLen) => {
63
+ const instance = state.instance;
64
+ const mem = new Uint8Array(instance.exports.memory.buffer);
65
+ errorMsgPtr >>>= 0;
66
+ errorMsgLen >>>= 0;
67
+ if (errorMsgPtr === 0) {
68
+ eventCallback({ ty: "add-chain-result", chainId, success: true });
69
+ }
70
+ else {
71
+ const errorMsg = buffer.utf8BytesToString(mem, errorMsgPtr, errorMsgLen);
72
+ eventCallback({ ty: "add-chain-result", chainId, success: false, error: errorMsg });
73
+ }
74
+ },
62
75
  random_get: (ptr, len) => {
63
76
  const instance = state.instance;
64
77
  ptr >>>= 0;
@@ -368,7 +381,8 @@ function startLocalInstance(config, wasmModule, eventCallback) {
368
381
  },
369
382
  addChain: (chainSpec, databaseContent, potentialRelayChains, disableJsonRpc, jsonRpcMaxPendingRequests, jsonRpcMaxSubscriptions) => {
370
383
  if (!state.instance) {
371
- eventCallback({ ty: "add-chain-result", success: false, error: "Smoldot has crashed" });
384
+ eventCallback({ ty: "add-chain-id-allocated", chainId: 0 });
385
+ eventCallback({ ty: "add-chain-result", chainId: 0, success: false, error: "Smoldot has crashed" });
372
386
  return;
373
387
  }
374
388
  // The caller is supposed to avoid this situation.
@@ -387,16 +401,7 @@ function startLocalInstance(config, wasmModule, eventCallback) {
387
401
  delete state.bufferIndices[0];
388
402
  delete state.bufferIndices[1];
389
403
  delete state.bufferIndices[2];
390
- if (state.instance.exports.chain_is_ok(chainId) != 0) {
391
- eventCallback({ ty: "add-chain-result", success: true, chainId });
392
- }
393
- else {
394
- const errorMsgLen = state.instance.exports.chain_error_len(chainId) >>> 0;
395
- const errorMsgPtr = state.instance.exports.chain_error_ptr(chainId) >>> 0;
396
- const errorMsg = buffer.utf8BytesToString(new Uint8Array(state.instance.exports.memory.buffer), errorMsgPtr, errorMsgLen);
397
- state.instance.exports.remove_chain(chainId);
398
- eventCallback({ ty: "add-chain-result", success: false, error: errorMsg });
399
- }
404
+ eventCallback({ ty: "add-chain-id-allocated", chainId });
400
405
  },
401
406
  removeChain: (chainId) => {
402
407
  if (!state.instance)