Skip to content

Commit

Permalink
Merge branch 'tomas/feature-guard-masp-proofs-load' (#1828)
Browse files Browse the repository at this point in the history
* origin/tomas/feature-guard-masp-proofs-load:
  changelog: add #1828
  shared/types/control_flow: fix warn when no-default-features
  shared/ledger/masp: "testing" feature guard proofs load/save
  make check-crates: check that shared can build for wasm target
  • Loading branch information
Fraccaman committed Sep 6, 2023
2 parents 37753b0 + 63e1adb commit e7b3d26
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Ensure that Namada (shared) crate can be built for WASM target.
([\#1828](https://github.com/anoma/namada/pull/1828))
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ check:
check-mainnet:
$(cargo) check --workspace --features "mainnet"

# Check that every crate can be built with default features
# Check that every crate can be built with default features and that shared crate
# can be built for wasm
check-crates:
$(foreach p,$(crates), echo "Checking $(p)" && cargo +$(nightly) check -Z unstable-options --tests -p $(p) && ) \
make -C $(wasms_for_tests) check
make -C $(wasms_for_tests) check && \
cargo check --package namada --target wasm32-unknown-unknown --no-default-features --features "abciplus,namada-sdk"

clippy-wasm = $(cargo) +$(nightly) clippy --manifest-path $(wasm)/Cargo.toml --all-targets -- -D warnings

Expand Down
112 changes: 65 additions & 47 deletions shared/src/ledger/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub struct ShieldedTransfer {
pub epoch: Epoch,
}

#[cfg(feature = "testing")]
#[derive(Clone, Copy, Debug)]
enum LoadOrSaveProofs {
Load,
Expand Down Expand Up @@ -1583,6 +1584,7 @@ impl<U: ShieldedUtils> ShieldedContext<U> {
}

// To speed up integration tests, we can save and load proofs
#[cfg(feature = "testing")]
let load_or_save = if let Ok(masp_proofs) =
env::var(ENV_VAR_MASP_TEST_PROOFS)
{
Expand All @@ -1607,55 +1609,71 @@ impl<U: ShieldedUtils> ShieldedContext<U> {
};

let builder_clone = builder.clone().map_builder(WalletMap);
#[cfg(feature = "testing")]
let builder_bytes = BorshSerialize::try_to_vec(&builder_clone).unwrap();
let builder_hash =
namada_core::types::hash::Hash::sha256(&builder_bytes);
let saved_filepath = env::current_dir()
.unwrap()
// One up from "tests" dir to the root dir
.parent()
.unwrap()
.join(MASP_TEST_PROOFS_DIR)
.join(format!("{builder_hash}.bin"));

if let LoadOrSaveProofs::Load = load_or_save {
let recommendation = format!(
"Re-run the tests with {ENV_VAR_MASP_TEST_PROOFS}=save to \
re-generate proofs."
);
let exp_str = format!(
"Read saved MASP proofs from {}. {recommendation}",
saved_filepath.to_string_lossy()
);
let loaded_bytes =
tokio::fs::read(&saved_filepath).await.expect(&exp_str);
let exp_str = format!(
"Valid `ShieldedTransfer` bytes in {}. {recommendation}",
saved_filepath.to_string_lossy()
);
let loaded: ShieldedTransfer =
BorshDeserialize::try_from_slice(&loaded_bytes)
.expect(&exp_str);
Ok(Some(loaded))
} else {
// Build and return the constructed transaction
let (masp_tx, metadata) = builder.build(
&self.utils.local_tx_prover(),
// Fees are always paid outside of MASP
&FeeRule::non_standard(Amount::zero()),
)?;
let built = ShieldedTransfer {
builder: builder_clone,
masp_tx,
metadata,
epoch,
};
if let LoadOrSaveProofs::Save = load_or_save {
let built_bytes = BorshSerialize::try_to_vec(&built).unwrap();
tokio::fs::write(&saved_filepath, built_bytes)
.await
.unwrap();
let build_transfer =
|| -> Result<ShieldedTransfer, builder::Error<std::convert::Infallible>> {
let (masp_tx, metadata) = builder.build(
&self.utils.local_tx_prover(),
&FeeRule::non_standard(Amount::zero()),
)?;
Ok(ShieldedTransfer {
builder: builder_clone,
masp_tx,
metadata,
epoch,
})
};

#[cfg(feature = "testing")]
{
let builder_hash =
namada_core::types::hash::Hash::sha256(&builder_bytes);
let saved_filepath = env::current_dir()
.unwrap()
// One up from "tests" dir to the root dir
.parent()
.unwrap()
.join(MASP_TEST_PROOFS_DIR)
.join(format!("{builder_hash}.bin"));

if let LoadOrSaveProofs::Load = load_or_save {
let recommendation = format!(
"Re-run the tests with {ENV_VAR_MASP_TEST_PROOFS}=save to \
re-generate proofs."
);
let exp_str = format!(
"Read saved MASP proofs from {}. {recommendation}",
saved_filepath.to_string_lossy()
);
let loaded_bytes =
tokio::fs::read(&saved_filepath).await.expect(&exp_str);
let exp_str = format!(
"Valid `ShieldedTransfer` bytes in {}. {recommendation}",
saved_filepath.to_string_lossy()
);
let loaded: ShieldedTransfer =
BorshDeserialize::try_from_slice(&loaded_bytes)
.expect(&exp_str);
Ok(Some(loaded))
} else {
// Build and return the constructed transaction
let built = build_transfer()?;
if let LoadOrSaveProofs::Save = load_or_save {
let built_bytes =
BorshSerialize::try_to_vec(&built).unwrap();
tokio::fs::write(&saved_filepath, built_bytes)
.await
.unwrap();
}
Ok(Some(built))
}
}

#[cfg(not(feature = "testing"))]
{
// Build and return the constructed transaction
let built = build_transfer()?;
Ok(Some(built))
}
}
Expand Down
1 change: 1 addition & 0 deletions shared/src/types/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::ops::ControlFlow;
use std::pin::Pin;
use std::task::{Context, Poll};

#[cfg(any(unix, windows))]
use futures::future::FutureExt;
#[cfg(any(unix, windows))]
use tokio::sync::oneshot;
Expand Down

0 comments on commit e7b3d26

Please sign in to comment.