Skip to content

Use ruzstd to decompress bytecodes in Wasm #2421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ quote = "1.0"
rand = { version = "0.8.5", default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }
rand_distr = { version = "0.4.3", default-features = false }
ruzstd = "0.7.1"
k8s-openapi = { version = "0.21.1", features = ["v1_28"] }
pathdiff = "0.2.1"
kube = "0.88.1"
Expand Down
21 changes: 21 additions & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions linera-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ tracing-subscriber = { workspace = true, features = ["json", "fmt", "ansi"] }
wasm-bindgen-futures = { workspace = true, optional = true }
web-time = { workspace = true, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
ruzstd.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
chrono.workspace = true
rand = { workspace = true, features = ["getrandom", "std", "std_rng"] }
Expand Down
40 changes: 34 additions & 6 deletions linera-base/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,12 +839,18 @@ impl fmt::Debug for Bytecode {
}
}

/// A type for errors happening during compression.
/// A type for errors happening during decompression.
#[derive(Error, Debug)]
pub enum CompressionError {
pub enum DecompressionError {
/// Compressed bytecode is invalid, and could not be decompressed.
#[cfg(not(target_arch = "wasm32"))]
#[error("Bytecode could not be decompressed")]
InvalidCompressedBytecode(#[source] io::Error),

/// Compressed bytecode is invalid, and could not be decompressed.
#[cfg(target_arch = "wasm32")]
#[error("Bytecode could not be decompressed")]
InvalidCompressedBytecode(#[from] ruzstd::frame_decoder::FrameDecoderError),
}

/// A compressed WebAssembly module's bytecode.
Expand Down Expand Up @@ -874,19 +880,41 @@ impl From<Bytecode> for CompressedBytecode {

#[cfg(not(target_arch = "wasm32"))]
impl TryFrom<&CompressedBytecode> for Bytecode {
type Error = CompressionError;
type Error = DecompressionError;

fn try_from(compressed_bytecode: &CompressedBytecode) -> Result<Self, Self::Error> {
let bytes = zstd::stream::decode_all(&*compressed_bytecode.compressed_bytes)
.map_err(CompressionError::InvalidCompressedBytecode)?;
.map_err(DecompressionError::InvalidCompressedBytecode)?;

Ok(Bytecode { bytes })
}
}

#[cfg(target_arch = "wasm32")]
impl TryFrom<&CompressedBytecode> for Bytecode {
type Error = DecompressionError;

fn try_from(compressed_bytecode: &CompressedBytecode) -> Result<Self, Self::Error> {
use ruzstd::{io::Read, streaming_decoder::StreamingDecoder};

let compressed_bytes = &*compressed_bytecode.compressed_bytes;
let mut bytes = Vec::new();
let mut decoder = StreamingDecoder::new(compressed_bytes)?;

// Decode multiple frames, if present
// (https://github.com/KillingSpark/zstd-rs/issues/57)
while !decoder.get_ref().is_empty() {
decoder
.read_to_end(&mut bytes)
.expect("Reading from a slice in memory should not result in IO errors");
}

Ok(Bytecode { bytes })
}
}

#[cfg(not(target_arch = "wasm32"))]
impl TryFrom<CompressedBytecode> for Bytecode {
type Error = CompressionError;
type Error = DecompressionError;

fn try_from(compressed_bytecode: CompressedBytecode) -> Result<Self, Self::Error> {
Bytecode::try_from(&compressed_bytecode)
Expand Down
4 changes: 2 additions & 2 deletions linera-execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use linera_base::{
abi::Abi,
crypto::CryptoHash,
data_types::{
Amount, ApplicationPermissions, ArithmeticError, Blob, BlockHeight, CompressionError,
Amount, ApplicationPermissions, ArithmeticError, Blob, BlockHeight, DecompressionError,
Resources, SendMessageRequest, Timestamp, UserApplicationDescription,
},
doc_scalar, hex_debug,
Expand Down Expand Up @@ -125,7 +125,7 @@ pub enum ExecutionError {
#[error(transparent)]
JoinError(#[from] linera_base::task::Error),
#[error(transparent)]
CompressionError(#[from] CompressionError),
DecompressionError(#[from] DecompressionError),
#[error("The given promise is invalid or was polled once already")]
InvalidPromise,

Expand Down
Loading