Skip to content
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

feat(autonomi): run self encryption non-blocking #2397

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 39 additions & 14 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 autonomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ evmlib = { path = "../evmlib", version = "0.1.3", features = ["wasm-bindgen"] }
# See https://github.com/sebcrozet/instant/blob/7bd13f51f5c930239fddc0476a837870fb239ed7/README.md#using-instant-for-a-wasm-platform-where-performancenow-is-not-available
instant = { version = "0.1", features = ["wasm-bindgen", "inaccurate"] }
js-sys = "0.3.70"
tokio_with_wasm = { version = "0.7.2", features = ["rt"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-web = "0.1.3"
xor_name = { version = "5.0.0", features = ["serialize-hex"] }
Expand Down
2 changes: 2 additions & 0 deletions autonomi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ RUST_LOG=autonomi EVM_NETWORK=arbitrum-one EVM_PRIVATE_KEY=<PRIVATE_KEY> cargo t

### WebAssembly

> Note: compilation requires a nightly Rust compiler which is passed `RUSTFLAGS='-C target-feature=+atomics,+bulk-memory,+mutable-globals'` and `-Z build-std=std,panic_abort`.

To run a WASM test

- Install `wasm-pack`
Expand Down
4 changes: 2 additions & 2 deletions autonomi/src/client/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Client {
payment_option: PaymentOption,
) -> Result<DataAddr, PutError> {
let now = sn_networking::target_arch::Instant::now();
let (data_map_chunk, chunks) = encrypt(data)?;
let (data_map_chunk, chunks) = encrypt(data).await?;
let data_map_addr = data_map_chunk.address();
debug!("Encryption took: {:.2?}", now.elapsed());
info!("Uploading datamap chunk to the network at: {data_map_addr:?}");
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Client {
/// Get the estimated cost of storing a piece of data.
pub async fn data_cost(&self, data: Bytes) -> Result<AttoTokens, CostError> {
let now = sn_networking::target_arch::Instant::now();
let (data_map_chunk, chunks) = encrypt(data)?;
let (data_map_chunk, chunks) = encrypt(data).await?;

debug!("Encryption took: {:.2?}", now.elapsed());

Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/client/data_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Client {
payment_option: PaymentOption,
) -> Result<PrivateDataAccess, PutError> {
let now = sn_networking::target_arch::Instant::now();
let (data_map_chunk, chunks) = encrypt(data)?;
let (data_map_chunk, chunks) = encrypt(data).await?;
debug!("Encryption took: {:.2?}", now.elapsed());

// Pay for all chunks
Expand Down
4 changes: 2 additions & 2 deletions autonomi/src/client/external_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl Client {
/// Encrypts data as chunks.
///
/// Returns the data map chunk and file chunks.
pub fn encrypt_data(data: Bytes) -> Result<(Chunk, Vec<Chunk>), PutError> {
pub async fn encrypt_data(data: Bytes) -> Result<(Chunk, Vec<Chunk>), PutError> {
let now = sn_networking::target_arch::Instant::now();
let result = encrypt(data)?;
let result = encrypt(data).await?;

debug!("Encryption took: {:.2?}", now.elapsed());

Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/client/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl Client {
// re-do encryption to get the correct map xorname here
// this code needs refactor
let now = sn_networking::target_arch::Instant::now();
let (data_map_chunk, _) = crate::self_encryption::encrypt(file_bytes)?;
let (data_map_chunk, _) = crate::self_encryption::encrypt(file_bytes).await?;
tracing::debug!("Encryption took: {:.2?}", now.elapsed());
let map_xor_name = *data_map_chunk.address().xorname();

Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extern crate tracing;

pub mod client;
#[cfg(feature = "data")]
mod self_encryption;
pub mod self_encryption;
mod utils;

pub use sn_evm::get_evm_network_from_env;
Expand Down
10 changes: 8 additions & 2 deletions autonomi/src/self_encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use bytes::{BufMut, Bytes, BytesMut};
use self_encryption::{DataMap, MAX_CHUNK_SIZE};
use serde::{Deserialize, Serialize};
use sn_protocol::storage::Chunk;
#[cfg(not(target_arch = "wasm32"))]
use tokio::task;
#[cfg(target_arch = "wasm32")]
use tokio_with_wasm::task;
use tracing::debug;

#[derive(Debug, thiserror::Error)]
Expand All @@ -18,6 +22,8 @@ pub enum Error {
Encoding(#[from] rmp_serde::encode::Error),
#[error(transparent)]
SelfEncryption(#[from] self_encryption::Error),
#[error(transparent)]
Tokio(#[from] task::JoinError),
}

#[derive(Serialize, Deserialize)]
Expand All @@ -30,8 +36,8 @@ pub(crate) enum DataMapLevel {
Additional(DataMap),
}

pub(crate) fn encrypt(data: Bytes) -> Result<(Chunk, Vec<Chunk>), Error> {
let (data_map, chunks) = self_encryption::encrypt(data)?;
pub(crate) async fn encrypt(data: Bytes) -> Result<(Chunk, Vec<Chunk>), Error> {
let (data_map, chunks) = task::spawn_blocking(move || self_encryption::encrypt(data)).await??;
let (data_map_chunk, additional_chunks) = pack_data_map(data_map)?;

// Transform `EncryptedChunk` into `Chunk`
Expand Down
2 changes: 1 addition & 1 deletion autonomi/tests/external_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tokio::time::sleep;
use xor_name::XorName;

async fn pay_for_data(client: &Client, wallet: &Wallet, data: Bytes) -> eyre::Result<Receipt> {
let (data_map_chunk, chunks) = encrypt_data(data)?;
let (data_map_chunk, chunks) = encrypt_data(data).await?;

let map_xor_name = *data_map_chunk.address().xorname();
let mut xor_names = vec![map_xor_name];
Expand Down
31 changes: 22 additions & 9 deletions autonomi/tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,33 @@ use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

// #[wasm_bindgen_test]
// async fn put() -> Result<(), Box<dyn std::error::Error>> {
// enable_logging_wasm("sn_networking,autonomi,wasm");

// let client = Client::connect(&peers_from_env()?).await?;
// let wallet = get_funded_wallet();
// let data = gen_random_data(1024 * 1024 * 10);

// let addr = client.data_put(data.clone(), wallet.into()).await?;

// sleep(Duration::from_secs(10)).await;

// let data_fetched = client.data_get(addr).await?;
// assert_eq!(data, data_fetched, "data fetched should match data put");

// Ok(())
// }

#[wasm_bindgen_test]
async fn put() -> Result<(), Box<dyn std::error::Error>> {
async fn self_encryption_timing() -> Result<(), Box<dyn std::error::Error>> {
enable_logging_wasm("sn_networking,autonomi,wasm");

let client = Client::connect(&peers_from_env()?).await?;
let wallet = get_funded_wallet();
let data = gen_random_data(1024 * 1024 * 10);

let addr = client.data_put(data.clone(), wallet.into()).await?;

sleep(Duration::from_secs(10)).await;

let data_fetched = client.data_get(addr).await?;
assert_eq!(data, data_fetched, "data fetched should match data put");
let now = sn_networking::target_arch::Instant::now();
let (data_map_chunk, chunks) = autonomi::self_encryption::encrypt(data).await?;
tracing::info!("Encryption took: {:.2?}", now.elapsed());

Ok(())
}
Expand Down
Loading