Export light-client functionality for WASM

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2023-06-06 21:07:23 +03:00
parent 600a5cd14c
commit 9be09e8daa
11 changed files with 1035 additions and 241 deletions
+239 -238
View File
@@ -12,6 +12,8 @@ use crate::{
use core::time::Duration;
use futures::{lock::Mutex as AsyncMutex, stream::StreamExt, Stream};
use subxt_light_client as _;
#[cfg(feature = "jsonrpsee-ws")]
use jsonrpsee::{
async_client::ClientBuilder,
@@ -136,260 +138,259 @@ impl LightClientInner {
}
}
/// Must stop the execution immediately. The message is a UTF-8 string found in the memory of
/// the WebAssembly at offset `message_ptr` and with length `message_len`.
///
/// > **Note**: This function is typically implemented using `throw`.
///
/// After this function has been called, no further Wasm functions must be called again on
/// this Wasm virtual machine. Explanation below.
///
/// # About throwing and safety
///
/// Rust programs can be configured in two panicking modes: `abort`, or `unwind`. Safe or
/// unsafe Rust code must be written by keeping in mind that the execution of a function can
/// be suddenly interrupted by a panic, but can rely on the fact that this panic will either
/// completely abort the program, or unwind the stack. In the latter case, they can rely on
/// the fact that `std::panic::catch_unwind` will catch this unwinding and let them perform
/// some additional clean-ups.
///
/// This function is typically implemented using `throw`. However, "just" throwing a JavaScript
/// exception from within the implementation of this function is neither `abort`, because the
/// JavaScript could call into the Wasm again later, nor `unwind`, because it isn't caught by
/// `std::panic::catch_unwind`. By being neither of the two, it breaks the assumptions that
/// some Rust codes might rely on for either correctness or safety.
/// In order to solve this problem, we enforce that `panic` must behave like `abort`, and
/// forbid calling into the Wasm virtual machine again.
///
/// Beyond the `panic` function itself, any other FFI function that throws must similarly
/// behave like `abort` and prevent any further execution.
#[no_mangle]
pub extern "C" fn panic(message_ptr: u32, message_len: u32) {
let slice =
unsafe { std::slice::from_raw_parts(message_ptr as *const u8, message_len as usize) };
if let Ok(message) = std::str::from_utf8(slice) {
panic!("{message}");
}
}
// /// Must stop the execution immediately. The message is a UTF-8 string found in the memory of
// /// the WebAssembly at offset `message_ptr` and with length `message_len`.
// ///
// /// > **Note**: This function is typically implemented using `throw`.
// ///
// /// After this function has been called, no further Wasm functions must be called again on
// /// this Wasm virtual machine. Explanation below.
// ///
// /// # About throwing and safety
// ///
// /// Rust programs can be configured in two panicking modes: `abort`, or `unwind`. Safe or
// /// unsafe Rust code must be written by keeping in mind that the execution of a function can
// /// be suddenly interrupted by a panic, but can rely on the fact that this panic will either
// /// completely abort the program, or unwind the stack. In the latter case, they can rely on
// /// the fact that `std::panic::catch_unwind` will catch this unwinding and let them perform
// /// some additional clean-ups.
// ///
// /// This function is typically implemented using `throw`. However, "just" throwing a JavaScript
// /// exception from within the implementation of this function is neither `abort`, because the
// /// JavaScript could call into the Wasm again later, nor `unwind`, because it isn't caught by
// /// `std::panic::catch_unwind`. By being neither of the two, it breaks the assumptions that
// /// some Rust codes might rely on for either correctness or safety.
// /// In order to solve this problem, we enforce that `panic` must behave like `abort`, and
// /// forbid calling into the Wasm virtual machine again.
// ///
// /// Beyond the `panic` function itself, any other FFI function that throws must similarly
// /// behave like `abort` and prevent any further execution.
// #[no_mangle]
// pub extern "C" fn panic(message_ptr: u32, message_len: u32) {
// let slice =
// unsafe { std::slice::from_raw_parts(message_ptr as *const u8, message_len as usize) };
// if let Ok(message) = std::str::from_utf8(slice) {
// panic!("{message}");
// }
// }
/// Copies the entire content of the buffer with the given index to the memory of the
/// WebAssembly at offset `target_pointer`.
///
/// In situations where a buffer must be provided from the JavaScript to the Rust code, the
/// JavaScript must (prior to calling the Rust function that requires the buffer) assign a
/// "buffer index" to the buffer it wants to provide. The Rust code then calls the
/// [`buffer_size`] and [`buffer_copy`] functions in order to obtain the length and content
/// of the buffer.
#[no_mangle]
pub extern "C" fn buffer_copy(buffer_index: u32, target_pointer: u32) {}
// /// Copies the entire content of the buffer with the given index to the memory of the
// /// WebAssembly at offset `target_pointer`.
// ///
// /// In situations where a buffer must be provided from the JavaScript to the Rust code, the
// /// JavaScript must (prior to calling the Rust function that requires the buffer) assign a
// /// "buffer index" to the buffer it wants to provide. The Rust code then calls the
// /// [`buffer_size`] and [`buffer_copy`] functions in order to obtain the length and content
// /// of the buffer.
// #[no_mangle]
// pub extern "C" fn buffer_copy(buffer_index: u32, target_pointer: u32) {}
/// Returns the size (in bytes) of the buffer with the given index.
///
/// See the documentation of [`buffer_copy`] for context.
#[no_mangle]
// /// Returns the size (in bytes) of the buffer with the given index.
// ///
// /// See the documentation of [`buffer_copy`] for context.
// #[no_mangle]
// pub extern "C" fn buffer_size(buffer_index: u32) -> u32 {
// 0
// }
pub extern "C" fn buffer_size(buffer_index: u32) -> u32 {
0
}
// /// The queue of JSON-RPC responses of the given chain is no longer empty.
// ///
// /// This function is only ever called after [`json_rpc_responses_peek`] has returned a `len`
// /// of 0.
// ///
// /// This function might be called spuriously, however this behavior must not be relied upon.
// #[no_mangle]
// pub extern "C" fn json_rpc_responses_non_empty(chain_id: u32) {}
/// The queue of JSON-RPC responses of the given chain is no longer empty.
///
/// This function is only ever called after [`json_rpc_responses_peek`] has returned a `len`
/// of 0.
///
/// This function might be called spuriously, however this behavior must not be relied upon.
#[no_mangle]
pub extern "C" fn json_rpc_responses_non_empty(chain_id: u32) {}
// /// Client is emitting a log entry.
// ///
// /// Each log entry is made of a log level (`1 = Error, 2 = Warn, 3 = Info, 4 = Debug,
// /// 5 = Trace`), a log target (e.g. "network"), and a log message.
// ///
// /// The log target and message is a UTF-8 string found in the memory of the WebAssembly
// /// virtual machine at offset `ptr` and with length `len`.
// #[no_mangle]
// pub extern "C" fn log(
// level: u32,
// target_ptr: u32,
// target_len: u32,
// message_ptr: u32,
// message_len: u32,
// ) {
// let target_slice =
// unsafe { std::slice::from_raw_parts(target_ptr as *const u8, target_len as usize) };
// let target = std::str::from_utf8(target_slice).unwrap_or_else(|_| "cannot decode target");
/// Client is emitting a log entry.
///
/// Each log entry is made of a log level (`1 = Error, 2 = Warn, 3 = Info, 4 = Debug,
/// 5 = Trace`), a log target (e.g. "network"), and a log message.
///
/// The log target and message is a UTF-8 string found in the memory of the WebAssembly
/// virtual machine at offset `ptr` and with length `len`.
#[no_mangle]
pub extern "C" fn log(
level: u32,
target_ptr: u32,
target_len: u32,
message_ptr: u32,
message_len: u32,
) {
let target_slice =
unsafe { std::slice::from_raw_parts(target_ptr as *const u8, target_len as usize) };
let target = std::str::from_utf8(target_slice).unwrap_or_else(|_| "cannot decode target");
// let msg_slice =
// unsafe { std::slice::from_raw_parts(message_ptr as *const u8, message_len as usize) };
// let message = std::str::from_utf8(msg_slice).unwrap_or_else(|_| "cannot decode message");
let msg_slice =
unsafe { std::slice::from_raw_parts(message_ptr as *const u8, message_len as usize) };
let message = std::str::from_utf8(msg_slice).unwrap_or_else(|_| "cannot decode message");
// println!("log level={level} target={target} message={message}");
// }
println!("log level={level} target={target} message={message}");
}
// /// Called when [`advance_execution`] should be executed again.
// ///
// /// This function might be called from within [`advance_execution`], in which case
// /// [`advance_execution`] should be called again immediately after it returns.
// #[no_mangle]
// pub extern "C" fn advance_execution_ready() {}
/// Called when [`advance_execution`] should be executed again.
///
/// This function might be called from within [`advance_execution`], in which case
/// [`advance_execution`] should be called again immediately after it returns.
#[no_mangle]
pub extern "C" fn advance_execution_ready() {}
// /// After at least `milliseconds` milliseconds have passed, [`timer_finished`] must be called.
// ///
// /// It is not a logic error to call [`timer_finished`] *before* `milliseconds` milliseconds
// /// have passed, and this will likely cause smoldot to restart a new timer for the remainder
// /// of the duration.
// ///
// /// When [`timer_finished`] is called, the value of the monotonic clock (in the WASI bindings)
// /// must have increased by at least the given number of `milliseconds`.
// ///
// /// If `milliseconds` is 0, [`timer_finished`] should be called as soon as possible.
// ///
// /// `milliseconds` never contains a negative number, `NaN` or infinite.
// #[no_mangle]
// pub extern "C" fn start_timer(milliseconds: f64) {}
/// After at least `milliseconds` milliseconds have passed, [`timer_finished`] must be called.
///
/// It is not a logic error to call [`timer_finished`] *before* `milliseconds` milliseconds
/// have passed, and this will likely cause smoldot to restart a new timer for the remainder
/// of the duration.
///
/// When [`timer_finished`] is called, the value of the monotonic clock (in the WASI bindings)
/// must have increased by at least the given number of `milliseconds`.
///
/// If `milliseconds` is 0, [`timer_finished`] should be called as soon as possible.
///
/// `milliseconds` never contains a negative number, `NaN` or infinite.
#[no_mangle]
pub extern "C" fn start_timer(milliseconds: f64) {}
// /// Must initialize a new connection that tries to connect to the given multiaddress.
// ///
// /// The multiaddress is a UTF-8 string found in the WebAssembly memory at offset `addr_ptr`
// /// and with `addr_len` bytes. The string is a multiaddress such as `/ip4/1.2.3.4/tcp/5/ws`.
// ///
// /// The `id` parameter is an identifier for this connection, as chosen by the Rust code. It
// /// must be passed on every interaction with this connection.
// ///
// /// Returns 0 to indicate success, or 1 to indicate that an error happened. If an error is
// /// returned, the `id` doesn't correspond to anything.
// ///
// /// > **Note**: If you implement this function using for example `new WebSocket()`, please
// /// > keep in mind that exceptions should be caught and turned into an error code.
// ///
// /// If an error happened, assign a so-called "buffer index" (a `u32`) representing the buffer
// /// containing the UTF-8 error message, then write this buffer index as little-endian to the
// /// memory of the WebAssembly indicated by `error_buffer_index_ptr`. The Rust code will call
// /// [`buffer_size`] and [`buffer_copy`] in order to obtain the content of this buffer. The
// /// buffer index should remain assigned and buffer alive until the next time the JavaScript
// /// code retains control. Then, write at location `error_buffer_index_ptr + 4` a `1` if the
// /// error is caused by the address being forbidden or unsupported, and `0` otherwise. If no
// /// error happens, nothing should be written to `error_buffer_index_ptr`.
// ///
// /// At any time, a connection can be in one of the three following states:
// ///
// /// - `Opening` (initial state)
// /// - `Open`
// /// - `Reset`
// ///
// /// When in the `Opening` or `Open` state, the connection can transition to the `Reset` state
// /// if the remote closes the connection or refuses the connection altogether. When that
// /// happens, [`connection_reset`] must be called. Once in the `Reset` state, the connection
// /// cannot transition back to another state.
// ///
// /// Initially in the `Opening` state, the connection can transition to the `Open` state if the
// /// remote accepts the connection. When that happens, [`connection_open_single_stream`] or
// /// [`connection_open_multi_stream`] must be called.
// ///
// /// There exists two kind of connections: single-stream and multi-stream. Single-stream
// /// connections are assumed to have a single stream open at all time and the encryption and
// /// multiplexing are handled internally by smoldot. Multi-stream connections open and close
// /// streams over time using [`connection_stream_opened`] and [`stream_reset`], and the
// /// encryption and multiplexing are handled by the user of these bindings.
// #[no_mangle]
// pub extern "C" fn connection_new(
// id: u32,
// addr_ptr: u32,
// addr_len: u32,
// error_buffer_index_ptr: u32,
// ) -> u32 {
// 0
// }
/// Must initialize a new connection that tries to connect to the given multiaddress.
///
/// The multiaddress is a UTF-8 string found in the WebAssembly memory at offset `addr_ptr`
/// and with `addr_len` bytes. The string is a multiaddress such as `/ip4/1.2.3.4/tcp/5/ws`.
///
/// The `id` parameter is an identifier for this connection, as chosen by the Rust code. It
/// must be passed on every interaction with this connection.
///
/// Returns 0 to indicate success, or 1 to indicate that an error happened. If an error is
/// returned, the `id` doesn't correspond to anything.
///
/// > **Note**: If you implement this function using for example `new WebSocket()`, please
/// > keep in mind that exceptions should be caught and turned into an error code.
///
/// If an error happened, assign a so-called "buffer index" (a `u32`) representing the buffer
/// containing the UTF-8 error message, then write this buffer index as little-endian to the
/// memory of the WebAssembly indicated by `error_buffer_index_ptr`. The Rust code will call
/// [`buffer_size`] and [`buffer_copy`] in order to obtain the content of this buffer. The
/// buffer index should remain assigned and buffer alive until the next time the JavaScript
/// code retains control. Then, write at location `error_buffer_index_ptr + 4` a `1` if the
/// error is caused by the address being forbidden or unsupported, and `0` otherwise. If no
/// error happens, nothing should be written to `error_buffer_index_ptr`.
///
/// At any time, a connection can be in one of the three following states:
///
/// - `Opening` (initial state)
/// - `Open`
/// - `Reset`
///
/// When in the `Opening` or `Open` state, the connection can transition to the `Reset` state
/// if the remote closes the connection or refuses the connection altogether. When that
/// happens, [`connection_reset`] must be called. Once in the `Reset` state, the connection
/// cannot transition back to another state.
///
/// Initially in the `Opening` state, the connection can transition to the `Open` state if the
/// remote accepts the connection. When that happens, [`connection_open_single_stream`] or
/// [`connection_open_multi_stream`] must be called.
///
/// There exists two kind of connections: single-stream and multi-stream. Single-stream
/// connections are assumed to have a single stream open at all time and the encryption and
/// multiplexing are handled internally by smoldot. Multi-stream connections open and close
/// streams over time using [`connection_stream_opened`] and [`stream_reset`], and the
/// encryption and multiplexing are handled by the user of these bindings.
#[no_mangle]
pub extern "C" fn connection_new(
id: u32,
addr_ptr: u32,
addr_len: u32,
error_buffer_index_ptr: u32,
) -> u32 {
0
}
// /// Abruptly close a connection previously initialized with [`connection_new`].
// ///
// /// This destroys the identifier passed as parameter. This identifier must never be passed
// /// through the FFI boundary, unless the same identifier is later allocated again with
// /// [`connection_new`].
// ///
// /// Must never be called if [`connection_reset`] has been called on that object in the past.
// ///
// /// The connection must be closed in the background. The Rust code isn't interested in incoming
// /// messages from this connection anymore.
// ///
// /// > **Note**: In JavaScript, remember to unregister event handlers before calling for
// /// > example `WebSocket.close()`.
// #[no_mangle]
// pub extern "C" fn reset_connection(id: u32) {}
/// Abruptly close a connection previously initialized with [`connection_new`].
///
/// This destroys the identifier passed as parameter. This identifier must never be passed
/// through the FFI boundary, unless the same identifier is later allocated again with
/// [`connection_new`].
///
/// Must never be called if [`connection_reset`] has been called on that object in the past.
///
/// The connection must be closed in the background. The Rust code isn't interested in incoming
/// messages from this connection anymore.
///
/// > **Note**: In JavaScript, remember to unregister event handlers before calling for
/// > example `WebSocket.close()`.
#[no_mangle]
pub extern "C" fn reset_connection(id: u32) {}
// /// Queues a new outbound substream opening. The [`connection_stream_opened`] function must
// /// later be called when the substream has been successfully opened.
// ///
// /// This function will only be called for multi-stream connections. The connection must
// /// currently be in the `Open` state. See the documentation of [`connection_new`] for details.
// ///
// /// > **Note**: No mechanism exists in this API to handle the situation where a substream fails
// /// > to open, as this is not supposed to happen. If you need to handle such a
// /// > situation, either try again opening a substream again or reset the entire
// /// > connection.
// #[no_mangle]
// pub extern "C" fn connection_stream_open(connection_id: u32) {}
/// Queues a new outbound substream opening. The [`connection_stream_opened`] function must
/// later be called when the substream has been successfully opened.
///
/// This function will only be called for multi-stream connections. The connection must
/// currently be in the `Open` state. See the documentation of [`connection_new`] for details.
///
/// > **Note**: No mechanism exists in this API to handle the situation where a substream fails
/// > to open, as this is not supposed to happen. If you need to handle such a
/// > situation, either try again opening a substream again or reset the entire
/// > connection.
#[no_mangle]
pub extern "C" fn connection_stream_open(connection_id: u32) {}
// /// Abruptly closes an existing substream of a multi-stream connection. The substream must
// /// currently be in the `Open` state.
// ///
// /// Must never be called if [`stream_reset`] has been called on that object in the past.
// ///
// /// This function will only be called for multi-stream connections. The connection must
// /// currently be in the `Open` state. See the documentation of [`connection_new`] for details.
// #[no_mangle]
// pub extern "C" fn connection_stream_reset(connection_id: u32, stream_id: u32) {}
/// Abruptly closes an existing substream of a multi-stream connection. The substream must
/// currently be in the `Open` state.
///
/// Must never be called if [`stream_reset`] has been called on that object in the past.
///
/// This function will only be called for multi-stream connections. The connection must
/// currently be in the `Open` state. See the documentation of [`connection_new`] for details.
#[no_mangle]
pub extern "C" fn connection_stream_reset(connection_id: u32, stream_id: u32) {}
// /// Queues data on the given stream. The data is found in the memory of the WebAssembly
// /// virtual machine, at the given pointer.
// ///
// /// If `connection_id` is a single-stream connection, then the value of `stream_id` should
// /// be ignored. If `connection_id` is a multi-stream connection, then the value of `stream_id`
// /// contains the identifier of the stream on which to send the data, as was provided to
// /// [`connection_stream_opened`].
// ///
// /// The connection associated with that stream (and, in the case of a multi-stream connection,
// /// the stream itself must currently be in the `Open` state. See the documentation of
// /// [`connection_new`] for details.
// ///
// /// The size of the buffer must not exceed the number of writable bytes of the given stream.
// /// Use [`stream_writable_bytes`] to notify that more data can be sent on the stream.
// #[no_mangle]
// pub extern "C" fn stream_send(connection_id: u32, stream_id: u32, ptr: u32, len: u32) {}
/// Queues data on the given stream. The data is found in the memory of the WebAssembly
/// virtual machine, at the given pointer.
///
/// If `connection_id` is a single-stream connection, then the value of `stream_id` should
/// be ignored. If `connection_id` is a multi-stream connection, then the value of `stream_id`
/// contains the identifier of the stream on which to send the data, as was provided to
/// [`connection_stream_opened`].
///
/// The connection associated with that stream (and, in the case of a multi-stream connection,
/// the stream itself must currently be in the `Open` state. See the documentation of
/// [`connection_new`] for details.
///
/// The size of the buffer must not exceed the number of writable bytes of the given stream.
/// Use [`stream_writable_bytes`] to notify that more data can be sent on the stream.
#[no_mangle]
pub extern "C" fn stream_send(connection_id: u32, stream_id: u32, ptr: u32, len: u32) {}
// /// Close the sending side of the given stream of the given connection.
// ///
// /// Never called for connection types where this isn't possible to implement (i.e. WebSocket
// /// and WebRTC at the moment).
// ///
// /// If `connection_id` is a single-stream connection, then the value of `stream_id` should
// /// be ignored. If `connection_id` is a multi-stream connection, then the value of `stream_id`
// /// contains the identifier of the stream whose sending side should be closed, as was provided
// /// to [`connection_stream_opened`].
// ///
// /// The connection associated with that stream (and, in the case of a multi-stream connection,
// /// the stream itself must currently be in the `Open` state. See the documentation of
// /// [`connection_new`] for details.
// #[no_mangle]
// pub extern "C" fn stream_send_close(connection_id: u32, stream_id: u32) {}
/// Close the sending side of the given stream of the given connection.
///
/// Never called for connection types where this isn't possible to implement (i.e. WebSocket
/// and WebRTC at the moment).
///
/// If `connection_id` is a single-stream connection, then the value of `stream_id` should
/// be ignored. If `connection_id` is a multi-stream connection, then the value of `stream_id`
/// contains the identifier of the stream whose sending side should be closed, as was provided
/// to [`connection_stream_opened`].
///
/// The connection associated with that stream (and, in the case of a multi-stream connection,
/// the stream itself must currently be in the `Open` state. See the documentation of
/// [`connection_new`] for details.
#[no_mangle]
pub extern "C" fn stream_send_close(connection_id: u32, stream_id: u32) {}
// /// Called when the Wasm execution enters the context of a certain task. This is useful for
// /// debugging purposes.
// ///
// /// Only one task can be currently executing at any time.
// ///
// /// The name of the task is a UTF-8 string found in the memory of the WebAssembly virtual
// /// machine at offset `ptr` and with length `len`.
// #[no_mangle]
// pub extern "C" fn current_task_entered(ptr: u32, len: u32) {}
/// Called when the Wasm execution enters the context of a certain task. This is useful for
/// debugging purposes.
///
/// Only one task can be currently executing at any time.
///
/// The name of the task is a UTF-8 string found in the memory of the WebAssembly virtual
/// machine at offset `ptr` and with length `len`.
#[no_mangle]
pub extern "C" fn current_task_entered(ptr: u32, len: u32) {}
/// Called when the Wasm execution leave the context of a certain task. This is useful for
/// debugging purposes.
///
/// Only one task can be currently executing at any time.
#[no_mangle]
pub extern "C" fn current_task_exit() {}
// /// Called when the Wasm execution leave the context of a certain task. This is useful for
// /// debugging purposes.
// ///
// /// Only one task can be currently executing at any time.
// #[no_mangle]
// pub extern "C" fn current_task_exit() {}
/// The LightClient RPC offers a slightly different RPC methods than the
/// substrate based chains. This is because the light client only exposes