smoldot 0.7.13 → 1.0.0

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.
@@ -26,17 +26,23 @@ export interface Client {
26
26
  /**
27
27
  * Connects to a chain.
28
28
  *
29
- * Throws an exception if the chain specification isn't valid, or if the chain specification
30
- * concerns a parachain but no corresponding relay chain can be found.
29
+ * After you've called this function, the client will verify whether the chain specification is
30
+ * valid. Once this is done, the `Promise` returned by this function will yield a
31
+ * {@link Chain} that can be used to interact with that chain. Only after the `Promise` has
32
+ * yielded will the client actually start establishing networking connections to the chain.
33
+ *
34
+ * The `Promise` throws an exception if the chain specification isn't valid, or if the chain
35
+ * specification concerns a parachain but no corresponding relay chain can be found.
31
36
  *
32
37
  * Smoldot will automatically de-duplicate chains if multiple identical chains are added, in
33
38
  * order to save resources. In other words, it is not a problem to call `addChain` multiple
34
- * times with the same chain specifications and obtain multiple `Chain`.
39
+ * times with the same chain specifications and obtain multiple {@link Chain} objects.
35
40
  * When the same client is used for multiple different purposes, you are in fact strongly
36
41
  * encouraged to trust smoldot and not attempt to de-duplicate chains yourself, as determining
37
42
  * whether two chains are identical is complicated and might have security implications.
38
43
  *
39
- * Smoldot tries to distribute CPU resources equally between all active `Chain` objects.
44
+ * Smoldot tries to distribute CPU resources equally between all active {@link Chain} objects
45
+ * of the same client.
40
46
  *
41
47
  * @param options Configuration of the chain to add.
42
48
  *
@@ -48,6 +54,9 @@ export interface Client {
48
54
  /**
49
55
  * Terminates the client.
50
56
  *
57
+ * This implicitly calls {@link Chain.remove} on all the chains associated with this client,
58
+ * then shuts down the client itself.
59
+ *
51
60
  * Afterwards, trying to use the client or any of its chains again will lead to an exception
52
61
  * being thrown.
53
62
  *
@@ -70,10 +79,15 @@ export interface Chain {
70
79
  * Be aware that some requests will cause notifications to be sent back using the same callback
71
80
  * as the responses.
72
81
  *
73
- * A {@link MalformedJsonRpcError} is thrown if the request isn't a valid JSON-RPC request or
74
- * if the request is unreasonably large (64 MiB at the time of writing of this comment).
75
- * If, however, the request is a valid JSON-RPC request but that concerns an unknown method, a
76
- * error response is properly generated.
82
+ * A {@link MalformedJsonRpcError} is thrown if the request isn't a valid JSON-RPC request
83
+ * (for example if it is not valid JSON) or if the request is unreasonably large (64 MiB at the
84
+ * time of writing of this comment).
85
+ * If, however, the request is a valid JSON-RPC request but that concerns an unknown method, or
86
+ * if for example some parameters are missing, an error response is properly generated and
87
+ * yielded through the JSON-RPC callback.
88
+ * In other words, a {@link MalformedJsonRpcError} is thrown in situations where something
89
+ * is *so wrong* with the request that it is not possible for smoldot to send back an error
90
+ * through the JSON-RPC callback.
77
91
  *
78
92
  * Two JSON-RPC APIs are supported by smoldot:
79
93
  *
@@ -92,14 +106,17 @@ export interface Chain {
92
106
  /**
93
107
  * Waits for a JSON-RPC response or notification to be generated.
94
108
  *
95
- * If this function is called multiple times "simultaneously" (generating multiple different
96
- * `Promise`s), each `Promise` will return a different JSON-RPC response or notification.
97
- *
98
109
  * Each chain contains a buffer of the responses waiting to be sent out. Calling this function
99
110
  * pulls one element from the buffer. If this function is called at a slower rate than responses
100
- * are generated, then buffer will eventually become full, at which point calling
111
+ * are generated, then the buffer will eventually become full, at which point calling
101
112
  * {@link Chain.sendJsonRpc} will throw an exception.
102
113
  *
114
+ * If this function is called multiple times "simultaneously" (generating multiple different
115
+ * `Promise`s), each `Promise` will return a different JSON-RPC response or notification. In
116
+ * that situation, there is no guarantee in the ordering in which the responses or notifications
117
+ * are yielded. Calling this function multiple times "simultaneously" is in general a niche
118
+ * corner case that you are encouraged to avoid.
119
+ *
103
120
  * @throws {@link AlreadyDestroyedError} If the chain has been removed or the client has been terminated.
104
121
  * @throws {@link JsonRpcDisabledError} If the JSON-RPC system was disabled in the options of the chain.
105
122
  * @throws {@link CrashError} If the background client has crashed.
@@ -108,14 +125,15 @@ export interface Chain {
108
125
  /**
109
126
  * Disconnects from the blockchain.
110
127
  *
111
- * The JSON-RPC callback will no longer be called.
128
+ * The JSON-RPC callback will no longer be called. This is the case immediately after this
129
+ * function is called. Any on-going JSON-RPC request is instantaneously aborted.
112
130
  *
113
131
  * Trying to use the chain again will lead to an exception being thrown.
114
132
  *
115
133
  * If this chain is a relay chain, then all parachains that use it will continue to work. Smoldot
116
134
  * automatically keeps alive all relay chains that have an active parachains. There is no need
117
- * to track parachains and relaychains, or to destroy them in the correct order, as this is
118
- * handled automatically.
135
+ * to track parachains and relay chains, or to destroy them in the correct order, as this is
136
+ * handled automatically internally.
119
137
  *
120
138
  * @throws {@link AlreadyDestroyedError} If the chain has already been removed or the client has been terminated.
121
139
  * @throws {@link CrashError} If the background client has crashed.
@@ -140,10 +158,10 @@ export interface ClientOptions {
140
158
  */
141
159
  logCallback?: LogCallback;
142
160
  /**
143
- * The client will never call the callback with a value of `level` superior to this value.
161
+ * The client will never call the log callback with a value of `level` superior to this value.
144
162
  * Defaults to 3.
145
163
  *
146
- * While this filtering could be done directly by the `logCallback`, passing a maximum log level
164
+ * While this filtering could be done manually in the `logCallback`, passing a maximum log level
147
165
  * leads to better performances as the client doesn't even need to generate a `message` when it
148
166
  * knows that this message isn't interesting.
149
167
  */
@@ -226,32 +244,47 @@ export interface AddChainOptions {
226
244
  * `<client> build-spec --raw > spec.json`. Only "raw" chain specifications are supported by
227
245
  * smoldot at the moment.
228
246
  *
229
- * If the chain specification contains a `relay_chain` field, then smoldot will try to match
230
- * the value in `relay_chain` with the value in `id` of the chains in `potentialRelayChains`.
247
+ * If the chain specification contains a `relayChain` field, then smoldot will try to match
248
+ * the value in `relayChain` with the value in `id` of the chains in
249
+ * {@link AddChainOptions.potentialRelayChains}.
231
250
  */
232
251
  chainSpec: string;
233
252
  /**
234
- * Content of the database of this chain. Can be obtained by using the
235
- * `chainHead_unstable_finalizedDatabase` JSON-RPC function.
253
+ * Content of the database of this chain.
254
+ *
255
+ * The content of the database can be obtained by using the
256
+ * `chainHead_unstable_finalizedDatabase` JSON-RPC function. This undocumented JSON-RPC function
257
+ * accepts one parameter of type `number` indicating an upper limit to the size of the database.
258
+ * The content of the database is always a UTF-8 string whose content is at the discretion of
259
+ * the smoldot implementation.
236
260
  *
237
261
  * Smoldot reserves the right to change its database format, making previous databases
238
- * incompatible. For this reason, no error is generated if the content of the database is invalid
239
- * and/or can't be decoded.
262
+ * incompatible. For this reason, no error is generated if the content of the database is
263
+ * invalid and/or can't be decoded.
264
+ *
265
+ * Providing a database can considerably improve the time it takes for smoldot to be fully
266
+ * synchronized with a chain by reducing the amount of data that it has to download.
267
+ * Furthermore, the database also contains a list of nodes that smoldot can use in order to
268
+ * reduce the load that is being put on the bootnodes.
240
269
  *
241
270
  * Important: please note that using a malicious database content can lead to a security
242
271
  * vulnerability. This database content is considered by smoldot as trusted input. It is the
243
272
  * responsibility of the API user to make sure that the value passed in this field comes from
244
273
  * the same source of trust as the chain specification that was used when retrieving this
245
- * database content.
274
+ * database content. In other words, if you load this database content for example from the disk
275
+ * or from the browser's local storage, be absolutely certain that no malicious program has
276
+ * modified the content of that file or local storage.
246
277
  */
247
278
  databaseContent?: string;
248
279
  /**
249
280
  * If `chainSpec` concerns a parachain, contains the list of chains whose `id` smoldot will try
250
- * to match with the parachain's `relay_chain`.
281
+ * to match with the parachain's `relayChain`.
251
282
  * Defaults to `[]`.
252
283
  *
253
- * Must contain exactly the objects that were returned by previous calls to `addChain`. The
254
- * library uses a `WeakMap` in its implementation in order to identify chains.
284
+ * Must contain exactly the {@link Chain} objects that were returned by previous calls to
285
+ * `addChain`. The library uses a `WeakMap` in its implementation in order to identify chains.
286
+ *
287
+ * # Explanation and usage
255
288
  *
256
289
  * The primary way smoldot determines which relay chain is associated to a parachain is by
257
290
  * inspecting the chain specification of that parachain (i.e. the `chainSpec` field).
@@ -260,7 +293,7 @@ export interface AddChainOptions {
260
293
  * applications: multiple applications could add mutiple different chains with the same `id`,
261
294
  * creating an ambiguity, or an application could register malicious chains with small variations
262
295
  * of a popular chain's `id` and try to benefit from a typo in a legitimate application's
263
- * `relay_chain`.
296
+ * `relayChain`.
264
297
  *
265
298
  * These problems can be solved by using this parameter to segregate multiple different uses of
266
299
  * the same client. To use it, pass the list of all chains that the same application has
@@ -150,6 +150,10 @@ function connect(config, forbidTcp, forbidWs, forbidNonLocalWs, forbidWss) {
150
150
  inner: Deno.connect({
151
151
  hostname: tcpParsed[2],
152
152
  port: parseInt(tcpParsed[3], 10),
153
+ }).catch((error) => {
154
+ socket.destroyed = true;
155
+ config.onConnectionReset(error.toString());
156
+ return null;
153
157
  })
154
158
  };
155
159
  connection = { ty: 'tcp', socket };
@@ -164,7 +168,7 @@ function connect(config, forbidTcp, forbidWs, forbidNonLocalWs, forbidWss) {
164
168
  // The task ends automatically if an EOF or error is detected, which should also happen
165
169
  // if the user calls `close()`.
166
170
  const read = (readBuffer) => __awaiter(this, void 0, void 0, function* () {
167
- if (socket.destroyed)
171
+ if (socket.destroyed || established === null)
168
172
  return;
169
173
  let outcome = null;
170
174
  try {
@@ -232,7 +236,7 @@ function connect(config, forbidTcp, forbidWs, forbidNonLocalWs, forbidWss) {
232
236
  const socket = connection.socket;
233
237
  connection.socket.inner = connection.socket.inner.then((c) => __awaiter(this, void 0, void 0, function* () {
234
238
  while (dataCopy.length > 0) {
235
- if (socket.destroyed)
239
+ if (socket.destroyed || c === null)
236
240
  return c;
237
241
  let outcome;
238
242
  try {