Skip to content

Commit

Permalink
Merge pull request #375 from nervosnetwork/optimizing-wasm-experience
Browse files Browse the repository at this point in the history
feat: optimizing wasm experience
  • Loading branch information
driftluo authored Oct 24, 2024
2 parents b0eff72 + e1a46f4 commit 96fe296
Show file tree
Hide file tree
Showing 37 changed files with 557 additions and 341 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @nervosnetwork/ckb-dev @driftluo @TheWaWaR
* @nervosnetwork/ckb-dev @driftluo @eval-exec
7 changes: 4 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name: Github Action

on:
pull_request: # trigger on pull requests
pull_request: # trigger on pull requests
push:
branches:
- master # trigger on push to master
- master # trigger on push to master

jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
build: [ linux, macos, windows ]
build: [linux, macos, windows]
include:
- build: linux
os: ubuntu-latest
Expand Down Expand Up @@ -63,6 +63,7 @@ jobs:
run: |
echo "stable" > rust-toolchain
rustup target add wasm32-unknown-unknown
rustup target add wasm32-wasip1
make features-check
fuzz_test:
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MOLC := moleculec
MOLC_VERSION := 0.7.0
MOLC_VERSION := 0.8.0

MOL_FILES := \
tentacle/src/protocol_select/protocol_select.mol \
Expand Down Expand Up @@ -47,6 +47,9 @@ features-check:
# required wasm32-unknown-unknown target
$(Change_Work_Path) && cargo build --features wasm-timer,unstable --no-default-features --target=wasm32-unknown-unknown
$(Change_Work_Path) && cargo build --features wasm-timer,unstable,secio-async-trait --no-default-features --target=wasm32-unknown-unknown
# required wasm32-wasip1 target
$(Change_Work_Path) && cargo build --features wasm-timer,unstable --no-default-features --target=wasm32-wasip1
$(Change_Work_Path) && cargo build --features wasm-timer,unstable,secio-async-trait --no-default-features --target=wasm32-wasip1
bench_p2p:
cd bench && cargo run --release

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.75.0
1.81.0
7 changes: 3 additions & 4 deletions secio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ molecule = "0.8.0"

unsigned-varint = "0.8"
bs58 = "0.5.0"
secp256k1 = "0.29"
secp256k1 = "0.30"
rand = "0.8"

[target.'cfg(unix)'.dependencies]
openssl = "0.10.25"
openssl-sys = "0.9"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
[target.'cfg(not(target_family = "wasm"))'.dependencies]
ring = "0.17"

[target.'cfg(target_arch = "wasm32")'.dependencies]
[target.'cfg(target_family = "wasm")'.dependencies]
rand_core = { version = "0.6" }
getrandom = { version = "0.2", features = ["js"] }
sha2 = "0.10.0"
Expand All @@ -60,7 +60,6 @@ hmac = "0.12.0"
x25519-dalek = "2"
chacha20poly1305 = "0.10"
rand_core = { version = "0.6" }
once_cell = "1.8.0"
proptest = "1"

[[bench]]
Expand Down
8 changes: 4 additions & 4 deletions secio/src/codec/hmac_compat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#[cfg(unix)]
mod openssl_impl;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(any(test, not(unix)))]
mod ring_impl;
#[cfg(any(target_arch = "wasm32", test))]
#[cfg(any(target_family = "wasm", test))]
mod wasm_compat;

#[cfg(unix)]
pub use openssl_impl::*;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(not(unix))]
pub use ring_impl::*;
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub use wasm_compat::*;

#[cfg(test)]
Expand Down
3 changes: 1 addition & 2 deletions secio/src/codec/secure_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ mod tests {
use tokio_util::codec::{length_delimited::LengthDelimitedCodec, Framed};

fn rt() -> &'static tokio::runtime::Runtime {
static RT: once_cell::sync::OnceCell<tokio::runtime::Runtime> =
once_cell::sync::OnceCell::new();
static RT: std::sync::OnceLock<tokio::runtime::Runtime> = std::sync::OnceLock::new();
RT.get_or_init(|| tokio::runtime::Runtime::new().unwrap())
}

Expand Down
8 changes: 4 additions & 4 deletions secio/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use bytes::BytesMut;
pub mod cipher;
#[cfg(unix)]
mod openssl_impl;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(any(not(ossl110), test, not(unix)))]
mod ring_impl;
#[cfg(any(target_arch = "wasm32", test))]
#[cfg(any(target_family = "wasm", test))]
mod wasm_compat;

/// Variant cipher which contains all possible stream ciphers
Expand Down Expand Up @@ -66,15 +66,15 @@ pub fn new_stream(t: cipher::CipherType, key: &[u8], mode: CryptoMode) -> BoxStr

/// Generate a specific Cipher with key and initialize vector
#[doc(hidden)]
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(not(unix))]
pub fn new_stream(t: cipher::CipherType, key: &[u8], mode: CryptoMode) -> BoxStreamCipher {
Box::new(ring_impl::RingAeadCipher::new(t, key, mode))
}

/// Generate a specific Cipher with key and initialize vector
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub fn new_stream(t: cipher::CipherType, key: &[u8], _mode: CryptoMode) -> BoxStreamCipher {
Box::new(wasm_compat::WasmCrypt::new(t, key))
}
Expand Down
8 changes: 4 additions & 4 deletions secio/src/dh_compat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[cfg(unix)]
mod openssl_impl;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(any(test, not(unix), not(ossl110)))]
mod ring_impl;
#[cfg(any(target_arch = "wasm32", test))]
#[cfg(any(target_family = "wasm", test))]
mod wasm_compat;

#[cfg(target_arch = "wasm32")]
#[cfg(target_family = "wasm")]
pub use wasm_compat::*;

/// Possible key agreement algorithms.
Expand All @@ -19,7 +19,7 @@ pub enum KeyAgreement {

#[cfg(all(ossl110, unix))]
pub use openssl_impl::*;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
#[cfg(not(unix))]
pub use ring_impl::*;
#[cfg(all(not(ossl110), unix))]
Expand Down
2 changes: 1 addition & 1 deletion secio/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl From<openssl::error::ErrorStack> for SecioError {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_family = "wasm"))]
impl From<ring::error::Unspecified> for SecioError {
fn from(_err: ring::error::Unspecified) -> SecioError {
SecioError::CryptoError
Expand Down
Loading

0 comments on commit 96fe296

Please sign in to comment.