smoldot 1.0.1 → 1.0.3

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.
@@ -2,9 +2,11 @@ import type { SmoldotWasmInstance } from './bindings.js';
2
2
  export interface Config {
3
3
  instance?: SmoldotWasmInstance;
4
4
  /**
5
- * Returns the number of milliseconds since an arbitrary epoch.
5
+ * Array used to store the buffers provided to the Rust code.
6
+ *
7
+ * When `buffer_size` or `buffer_index` are called, the buffer is found here.
6
8
  */
7
- performanceNow: () => number;
9
+ bufferIndices: Array<Uint8Array>;
8
10
  /**
9
11
  * Tries to open a new connection using the given configuration.
10
12
  *
@@ -65,7 +67,7 @@ export interface Connection {
65
67
  *
66
68
  * The connection and stream must currently be in the `Open` state.
67
69
  *
68
- * The number of bytes most never exceed the number of "writable bytes" of the stream.
70
+ * The number of bytes must never exceed the number of "writable bytes" of the stream.
69
71
  * `onWritableBytes` can be used in order to notify that more writable bytes are available.
70
72
  *
71
73
  * The `streamId` must be provided if and only if the connection is of type "multi-stream".
@@ -154,7 +156,8 @@ export interface ConnectionConfig {
154
156
  */
155
157
  onStreamReset: (streamId: number) => void;
156
158
  /**
157
- * Callback called when more data can be written on the stream.
159
+ * Callback called when some data sent using {@link Connection.send} has effectively been
160
+ * written on the stream, meaning that some buffer space is now free.
158
161
  *
159
162
  * Can only happen while the connection is in the `Open` state.
160
163
  *
@@ -162,6 +165,9 @@ export interface ConnectionConfig {
162
165
  *
163
166
  * The `streamId` parameter must be provided if and only if the connection is of type
164
167
  * "multi-stream".
168
+ *
169
+ * Only a number of bytes equal to the size of the data provided to {@link Connection.send}
170
+ * must be reported. In other words, the `initialWritableBytes` must never be exceeded.
165
171
  */
166
172
  onWritableBytes: (numExtra: number, streamId?: number) => void;
167
173
  /**
@@ -51,6 +51,16 @@ export default function (config) {
51
51
  const message = buffer.utf8BytesToString(new Uint8Array(instance.exports.memory.buffer), ptr, len);
52
52
  config.onPanic(message);
53
53
  },
54
+ buffer_size: (bufferIndex) => {
55
+ const buf = config.bufferIndices[bufferIndex];
56
+ return buf.byteLength;
57
+ },
58
+ buffer_copy: (bufferIndex, targetPtr) => {
59
+ const instance = config.instance;
60
+ targetPtr = targetPtr >>> 0;
61
+ const buf = config.bufferIndices[bufferIndex];
62
+ new Uint8Array(instance.exports.memory.buffer).set(buf, targetPtr);
63
+ },
54
64
  // Used by the Rust side to notify that a JSON-RPC response or subscription notification
55
65
  // is available in the queue of JSON-RPC responses.
56
66
  json_rpc_responses_non_empty: (chainId) => {
@@ -75,10 +85,6 @@ export default function (config) {
75
85
  config.logCallback(level, target, message);
76
86
  }
77
87
  },
78
- // Must return the UNIX time in milliseconds.
79
- unix_time_ms: () => Date.now(),
80
- // Must return the value of a monotonic clock in milliseconds.
81
- monotonic_clock_ms: () => config.performanceNow(),
82
88
  // Must call `timer_finished` after the given number of milliseconds has elapsed.
83
89
  start_timer: (id, ms) => {
84
90
  if (killedTracked.killed)
@@ -116,11 +122,11 @@ export default function (config) {
116
122
  },
117
123
  // Must create a new connection object. This implementation stores the created object in
118
124
  // `connections`.
119
- connection_new: (connectionId, addrPtr, addrLen, errorPtrPtr) => {
125
+ connection_new: (connectionId, addrPtr, addrLen, errorBufferIndexPtr) => {
120
126
  const instance = config.instance;
121
127
  addrPtr >>>= 0;
122
128
  addrLen >>>= 0;
123
- errorPtrPtr >>>= 0;
129
+ errorBufferIndexPtr >>>= 0;
124
130
  if (!!connections[connectionId]) {
125
131
  throw new Error("internal error: connection already allocated");
126
132
  }
@@ -140,13 +146,13 @@ export default function (config) {
140
146
  break;
141
147
  }
142
148
  case 'multi-stream': {
143
- const bufferLen = 1 + info.localTlsCertificateMultihash.length + info.remoteTlsCertificateMultihash.length;
144
- const ptr = instance.exports.alloc(bufferLen) >>> 0;
145
- const mem = new Uint8Array(instance.exports.memory.buffer);
146
- buffer.writeUInt8(mem, ptr, 0);
147
- mem.set(info.localTlsCertificateMultihash, ptr + 1);
148
- mem.set(info.remoteTlsCertificateMultihash, ptr + 1 + info.localTlsCertificateMultihash.length);
149
- instance.exports.connection_open_multi_stream(connectionId, ptr, bufferLen);
149
+ const handshakeTy = new Uint8Array(1 + info.localTlsCertificateMultihash.length + info.remoteTlsCertificateMultihash.length);
150
+ buffer.writeUInt8(handshakeTy, 0, 0);
151
+ handshakeTy.set(info.localTlsCertificateMultihash, 1);
152
+ handshakeTy.set(info.remoteTlsCertificateMultihash, 1 + info.localTlsCertificateMultihash.length);
153
+ config.bufferIndices[0] = handshakeTy;
154
+ instance.exports.connection_open_multi_stream(connectionId, 0);
155
+ delete config.bufferIndices[0];
150
156
  break;
151
157
  }
152
158
  }
@@ -157,10 +163,9 @@ export default function (config) {
157
163
  if (killedTracked.killed)
158
164
  return;
159
165
  try {
160
- const encoded = new TextEncoder().encode(message);
161
- const ptr = instance.exports.alloc(encoded.length) >>> 0;
162
- new Uint8Array(instance.exports.memory.buffer).set(encoded, ptr);
163
- instance.exports.connection_reset(connectionId, ptr, encoded.length);
166
+ config.bufferIndices[0] = new TextEncoder().encode(message);
167
+ instance.exports.connection_reset(connectionId, 0);
168
+ delete config.bufferIndices[0];
164
169
  }
165
170
  catch (_error) { }
166
171
  },
@@ -176,9 +181,9 @@ export default function (config) {
176
181
  if (killedTracked.killed)
177
182
  return;
178
183
  try {
179
- const ptr = instance.exports.alloc(message.length) >>> 0;
180
- new Uint8Array(instance.exports.memory.buffer).set(message, ptr);
181
- instance.exports.stream_message(connectionId, streamId || 0, ptr, message.length);
184
+ config.bufferIndices[0] = message;
185
+ instance.exports.stream_message(connectionId, streamId || 0, 0);
186
+ delete config.bufferIndices[0];
182
187
  }
183
188
  catch (_error) { }
184
189
  },
@@ -209,12 +214,9 @@ export default function (config) {
209
214
  errorStr = error.toString();
210
215
  }
211
216
  const mem = new Uint8Array(instance.exports.memory.buffer);
212
- const encoded = new TextEncoder().encode(errorStr);
213
- const ptr = instance.exports.alloc(encoded.length) >>> 0;
214
- mem.set(encoded, ptr);
215
- buffer.writeUInt32LE(mem, errorPtrPtr, ptr);
216
- buffer.writeUInt32LE(mem, errorPtrPtr + 4, encoded.length);
217
- buffer.writeUInt8(mem, errorPtrPtr + 8, isBadAddress ? 1 : 0);
217
+ config.bufferIndices[0] = new TextEncoder().encode(errorStr);
218
+ buffer.writeUInt32LE(mem, errorBufferIndexPtr, 0);
219
+ buffer.writeUInt8(mem, errorBufferIndexPtr + 4, isBadAddress ? 1 : 0);
218
220
  return 1;
219
221
  }
220
222
  },
@@ -5,6 +5,10 @@ export interface Config {
5
5
  * Fills the given buffer with randomly-generated bytes.
6
6
  */
7
7
  getRandomValues: (buffer: Uint8Array) => void;
8
+ /**
9
+ * Returns the number of milliseconds since an arbitrary epoch.
10
+ */
11
+ performanceNow: () => number;
8
12
  /**
9
13
  * List of environment variables to feed to the Rust program. An array of strings.
10
14
  * Example: `["RUST_BACKTRACE=1", "RUST_LOG=foo"];`
@@ -39,6 +39,36 @@ export default (config) => {
39
39
  }
40
40
  return 0;
41
41
  },
42
+ clock_time_get: (clockId, _precision, outPtr) => {
43
+ // See <https://github.com/rust-lang/rust/blob/master/library/std/src/sys/wasi/time.rs>
44
+ // and <docs.rs/wasi/> for help.
45
+ const instance = config.instance;
46
+ const mem = new Uint8Array(instance.exports.memory.buffer);
47
+ outPtr >>>= 0;
48
+ // We ignore the precision, as it can't be implemented anyway.
49
+ switch (clockId) {
50
+ case 0: {
51
+ // Realtime clock.
52
+ const now = BigInt(Math.floor(Date.now())) * BigInt(1000000);
53
+ buffer.writeUInt64LE(mem, outPtr, now);
54
+ // Success.
55
+ return 0;
56
+ }
57
+ case 1: {
58
+ // Monotonic clock.
59
+ const nowMs = config.performanceNow();
60
+ const nowMsInt = Math.floor(nowMs);
61
+ const now = BigInt(nowMsInt) * BigInt(1000000) +
62
+ BigInt(Math.floor(((nowMs - nowMsInt) * 1000000)));
63
+ buffer.writeUInt64LE(mem, outPtr, now);
64
+ // Success.
65
+ return 0;
66
+ }
67
+ default:
68
+ // Return an `EINVAL` error.
69
+ return 28;
70
+ }
71
+ },
42
72
  // Writing to a file descriptor is used in order to write to stdout/stderr.
43
73
  fd_write: (fd, addr, num, outPtr) => {
44
74
  const instance = config.instance;
@@ -9,22 +9,21 @@ export interface SmoldotWasmExports extends WebAssembly.Exports {
9
9
  init: (maxLogLevel: number, enableCurrentTask: number, cpuRateLimit: number, periodicallyYield: number) => void;
10
10
  set_periodically_yield: (periodicallyYield: number) => void;
11
11
  start_shutdown: () => void;
12
- alloc: (len: number) => number;
13
- add_chain: (chainSpecPointer: number, chainSpecLen: number, databaseContentPointer: number, databaseContentLen: number, jsonRpcRunning: number, potentialRelayChainsPtr: number, potentialRelayChainsLen: number) => number;
12
+ add_chain: (chainSpecBufferIndex: number, databaseContentBufferIndex: number, jsonRpcRunning: number, potentialRelayChainsBufferIndex: number) => number;
14
13
  remove_chain: (chainId: number) => void;
15
14
  chain_is_ok: (chainId: number) => number;
16
15
  chain_error_len: (chainId: number) => number;
17
16
  chain_error_ptr: (chainId: number) => number;
18
- json_rpc_send: (textPtr: number, textLen: number, chainId: number) => number;
17
+ json_rpc_send: (textBufferIndex: number, chainId: number) => number;
19
18
  json_rpc_responses_peek: (chainId: number) => number;
20
19
  json_rpc_responses_pop: (chainId: number) => void;
21
20
  timer_finished: (timerId: number) => void;
22
21
  connection_open_single_stream: (connectionId: number, handshakeTy: number, initialWritableBytes: number, writeClosable: number) => void;
23
- connection_open_multi_stream: (connectionId: number, handshakeTyPtr: number, handshakeTyLen: number) => void;
22
+ connection_open_multi_stream: (connectionId: number, handshakeTyBufferIndex: number) => void;
24
23
  stream_writable_bytes: (connectionId: number, streamId: number, numBytes: number) => void;
25
- stream_message: (connectionId: number, streamId: number, ptr: number, len: number) => void;
24
+ stream_message: (connectionId: number, streamId: number, bufferIndex: number) => void;
26
25
  connection_stream_opened: (connectionId: number, streamId: number, outbound: number, initialWritableBytes: number) => void;
27
- connection_reset: (connectionId: number, ptr: number, len: number) => void;
26
+ connection_reset: (connectionId: number, bufferIndex: number) => void;
28
27
  stream_reset: (connectionId: number, streamId: number) => void;
29
28
  }
30
29
  export interface SmoldotWasmInstance extends WebAssembly.Instance {
@@ -8,3 +8,4 @@ export declare function readUInt32LE(buffer: Uint8Array, offset: number): number
8
8
  */
9
9
  export declare function writeUInt8(buffer: Uint8Array, offset: number, value: number): void;
10
10
  export declare function writeUInt32LE(buffer: Uint8Array, offset: number, value: number): void;
11
+ export declare function writeUInt64LE(buffer: Uint8Array, offset: number, value: bigint): void;
@@ -38,6 +38,17 @@ export function writeUInt32LE(buffer, offset, value) {
38
38
  buffer[offset + 1] = (value >>> 8) & 0xff;
39
39
  buffer[offset] = value & 0xff;
40
40
  }
41
+ export function writeUInt64LE(buffer, offset, value) {
42
+ checkRange(buffer, offset, 8);
43
+ buffer[offset + 7] = Number((value >> BigInt(56)) & BigInt(0xff));
44
+ buffer[offset + 6] = Number((value >> BigInt(48)) & BigInt(0xff));
45
+ buffer[offset + 5] = Number((value >> BigInt(40)) & BigInt(0xff));
46
+ buffer[offset + 4] = Number((value >> BigInt(32)) & BigInt(0xff));
47
+ buffer[offset + 3] = Number((value >> BigInt(24)) & BigInt(0xff));
48
+ buffer[offset + 2] = Number((value >> BigInt(16)) & BigInt(0xff));
49
+ buffer[offset + 1] = Number((value >> BigInt(8)) & BigInt(0xff));
50
+ buffer[offset] = Number(value & BigInt(0xff));
51
+ }
41
52
  function checkRange(buffer, offset, length) {
42
53
  if (!Number.isInteger(offset) || offset < 0)
43
54
  throw new RangeError();
@@ -97,7 +97,7 @@ export function start(configMessage, platformBindings) {
97
97
  cpuRateLimit: configMessage.cpuRateLimit,
98
98
  };
99
99
  state = {
100
- initialized: false, promise: instance.startInstance(config, platformBindings).then((instance) => {
100
+ initialized: false, promise: instance.startInstance(config, platformBindings).then(([instance, bufferIndices]) => {
101
101
  // `config.cpuRateLimit` is a floating point that should be between 0 and 1, while the value
102
102
  // to pass as parameter must be between `0` and `2^32-1`.
103
103
  // The few lines of code below should handle all possible values of `number`, including
@@ -120,8 +120,8 @@ export function start(configMessage, platformBindings) {
120
120
  }
121
121
  });
122
122
  instance.exports.init(configMessage.maxLogLevel, configMessage.enableCurrentTask ? 1 : 0, cpuRateLimit, periodicallyYield ? 1 : 0);
123
- state = { initialized: true, instance, unregisterCallback };
124
- return instance;
123
+ state = { initialized: true, instance, bufferIndices, unregisterCallback };
124
+ return [instance, bufferIndices];
125
125
  })
126
126
  };
127
127
  function queueOperation(operation) {
@@ -131,11 +131,11 @@ export function start(configMessage, platformBindings) {
131
131
  if (!state.initialized) {
132
132
  // A message has been received while the Wasm VM is still initializing. Queue it for when
133
133
  // initialization is over.
134
- return state.promise.then((instance) => operation(instance));
134
+ return state.promise.then(([instance, bufferIndices]) => operation(instance, bufferIndices));
135
135
  }
136
136
  else {
137
137
  // Everything is already initialized. Process the message synchronously.
138
- return operation(state.instance);
138
+ return operation(state.instance, state.bufferIndices);
139
139
  }
140
140
  });
141
141
  }
@@ -150,10 +150,8 @@ export function start(configMessage, platformBindings) {
150
150
  throw crashError.error;
151
151
  let retVal;
152
152
  try {
153
- const encoded = new TextEncoder().encode(request);
154
- const ptr = state.instance.exports.alloc(encoded.length) >>> 0;
155
- new Uint8Array(state.instance.exports.memory.buffer).set(encoded, ptr);
156
- retVal = state.instance.exports.json_rpc_send(ptr, encoded.length, chainId) >>> 0;
153
+ state.bufferIndices[0] = new TextEncoder().encode(request);
154
+ retVal = state.instance.exports.json_rpc_send(0, chainId) >>> 0;
157
155
  }
158
156
  catch (_error) {
159
157
  console.assert(crashError.error);
@@ -200,29 +198,25 @@ export function start(configMessage, platformBindings) {
200
198
  }
201
199
  }),
202
200
  addChain: (chainSpec, databaseContent, potentialRelayChains, disableJsonRpc) => {
203
- return queueOperation((instance) => {
201
+ return queueOperation((instance, bufferIndices) => {
204
202
  if (crashError.error)
205
203
  throw crashError.error;
206
204
  try {
207
- // Write the chain specification into memory.
208
- const chainSpecEncoded = new TextEncoder().encode(chainSpec);
209
- const chainSpecPtr = instance.exports.alloc(chainSpecEncoded.length) >>> 0;
210
- new Uint8Array(instance.exports.memory.buffer).set(chainSpecEncoded, chainSpecPtr);
211
- // Write the database content into memory.
212
- const databaseContentEncoded = new TextEncoder().encode(databaseContent);
213
- const databaseContentPtr = instance.exports.alloc(databaseContentEncoded.length) >>> 0;
214
- new Uint8Array(instance.exports.memory.buffer).set(databaseContentEncoded, databaseContentPtr);
215
- // Write the potential relay chains into memory.
216
- const potentialRelayChainsLen = potentialRelayChains.length;
217
- const potentialRelayChainsPtr = instance.exports.alloc(potentialRelayChainsLen * 4) >>> 0;
218
- for (let idx = 0; idx < potentialRelayChains.length; ++idx) {
219
- buffer.writeUInt32LE(new Uint8Array(instance.exports.memory.buffer), potentialRelayChainsPtr + idx * 4, potentialRelayChains[idx]);
220
- }
221
205
  // `add_chain` unconditionally allocates a chain id. If an error occurs, however, this chain
222
206
  // id will refer to an *erroneous* chain. `chain_is_ok` is used below to determine whether it
223
207
  // has succeeeded or not.
224
208
  // Note that `add_chain` properly de-allocates buffers even if it failed.
225
- const chainId = instance.exports.add_chain(chainSpecPtr, chainSpecEncoded.length, databaseContentPtr, databaseContentEncoded.length, disableJsonRpc ? 0 : 1, potentialRelayChainsPtr, potentialRelayChainsLen);
209
+ bufferIndices[0] = new TextEncoder().encode(chainSpec);
210
+ bufferIndices[1] = new TextEncoder().encode(databaseContent);
211
+ const potentialRelayChainsEncoded = new Uint8Array(potentialRelayChains.length * 4);
212
+ for (let idx = 0; idx < potentialRelayChains.length; ++idx) {
213
+ buffer.writeUInt32LE(potentialRelayChainsEncoded, idx * 4, potentialRelayChains[idx]);
214
+ }
215
+ bufferIndices[2] = potentialRelayChainsEncoded;
216
+ const chainId = instance.exports.add_chain(0, 1, disableJsonRpc ? 0 : 1, 2);
217
+ delete bufferIndices[0];
218
+ delete bufferIndices[1];
219
+ delete bufferIndices[2];
226
220
  if (instance.exports.chain_is_ok(chainId) != 0) {
227
221
  console.assert(!chains.has(chainId));
228
222
  chains.set(chainId, {
@@ -62,4 +62,4 @@ export interface PlatformBindings {
62
62
  */
63
63
  connect(config: ConnectionConfig): Connection;
64
64
  }
65
- export declare function startInstance(config: Config, platformBindings: PlatformBindings): Promise<SmoldotWasmInstance>;
65
+ export declare function startInstance(config: Config, platformBindings: PlatformBindings): Promise<[SmoldotWasmInstance, Array<Uint8Array>]>;
@@ -32,8 +32,9 @@ export function startInstance(config, platformBindings) {
32
32
  // cross-platform cross-bundler approach.
33
33
  const wasmBytecode = yield platformBindings.trustedBase64DecodeAndZlibInflate(wasmBase64);
34
34
  let killAll;
35
+ const bufferIndices = new Array;
35
36
  // Used to bind with the smoldot-light bindings. See the `bindings-smoldot-light.js` file.
36
- const smoldotJsConfig = Object.assign({ performanceNow: platformBindings.performanceNow, connect: platformBindings.connect, onPanic: (message) => {
37
+ const smoldotJsConfig = Object.assign({ bufferIndices, connect: platformBindings.connect, onPanic: (message) => {
37
38
  killAll();
38
39
  config.onWasmPanic(message);
39
40
  throw new Error();
@@ -42,6 +43,7 @@ export function startInstance(config, platformBindings) {
42
43
  const wasiConfig = {
43
44
  envVars: [],
44
45
  getRandomValues: platformBindings.getRandomValues,
46
+ performanceNow: platformBindings.performanceNow,
45
47
  onProcExit: (retCode) => {
46
48
  killAll();
47
49
  config.onWasmPanic(`proc_exit called: ${retCode}`);
@@ -62,6 +64,6 @@ export function startInstance(config, platformBindings) {
62
64
  const instance = result.instance;
63
65
  smoldotJsConfig.instance = instance;
64
66
  wasiConfig.instance = instance;
65
- return instance;
67
+ return [instance, bufferIndices];
66
68
  });
67
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smoldot",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Light client that connects to Polkadot and Substrate-based blockchains",
5
5
  "author": "Parity Technologies <admin@parity.io>",
6
6
  "license": "GPL-3.0-or-later WITH Classpath-exception-2.0",
@@ -46,8 +46,8 @@
46
46
  "@types/ws": "^8.5.3",
47
47
  "ava": "^5.0.1",
48
48
  "dtslint": "^4.0.6",
49
- "rimraf": "^4.1.2",
50
- "typedoc": "^0.23.15",
49
+ "rimraf": "^5.0.0",
50
+ "typedoc": "^0.24.1",
51
51
  "typescript": "^5.0.2"
52
52
  }
53
53
  }