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

refactor(wasm): modularize wasm crate logic #3432

Merged
merged 10 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions crates/core/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub use effect_hash::EffectingData;
pub use error::Error;
pub use id::Id;
pub use is_action::IsAction;
pub use plan::ActionPlan;
pub use transaction::{Transaction, TransactionBody, TransactionParameters};
pub use view::{ActionView, MemoPlaintextView, MemoView, TransactionPerspective, TransactionView};
pub use witness_data::WitnessData;
Expand Down
6 changes: 3 additions & 3 deletions crates/core/transaction/src/plan/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ impl ActionPlan {
/// This method is useful for controlling how a transaction's actions are
/// built (e.g., building them in parallel, or via Web Workers).
pub fn build_unauth(
&self,
action_plan: ActionPlan,
fvk: &FullViewingKey,
witness_data: &WitnessData,
memo_key: Option<PayloadKey>,
) -> Result<Action> {
use ActionPlan::*;

Ok(match self {
Ok(match action_plan {
Spend(spend_plan) => {
let note_commitment = spend_plan.note.commit();
let auth_path = witness_data
Expand Down Expand Up @@ -130,7 +130,7 @@ impl ActionPlan {
.get(&note_commitment)
.context(format!("could not get proof for {note_commitment:?}"))?;

Action::SwapClaim(swap_claim_plan.swap_claim(&fvk, auth_path))
Action::SwapClaim(swap_claim_plan.swap_claim(fvk, auth_path))
}
Delegate(plan) => Action::Delegate(plan.clone()),
Undelegate(plan) => Action::Undelegate(plan.clone()),
Expand Down
10 changes: 8 additions & 2 deletions crates/core/transaction/src/plan/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rand_core::{CryptoRng, RngCore};
use std::fmt::Debug;

use super::TransactionPlan;
use crate::ActionPlan;
use crate::{
action::Action,
transaction::{DetectionData, TransactionParameters},
Expand Down Expand Up @@ -143,7 +144,12 @@ impl TransactionPlan {
.actions
.iter()
.map(|action_plan| {
action_plan.build_unauth(full_viewing_key, witness_data, self.memo_key())
ActionPlan::build_unauth(
action_plan.clone(),
full_viewing_key,
witness_data,
self.memo_key(),
)
})
.collect::<Result<Vec<_>>>()?;

Expand Down Expand Up @@ -181,7 +187,7 @@ impl TransactionPlan {
let witness_data2 = witness_data.clone(); // Arc
let memo_key2 = self.memo_key();
tokio::task::spawn_blocking(move || {
action_plan.build_unauth(&fvk2, &*witness_data2, memo_key2)
ActionPlan::build_unauth(action_plan, &fvk2, &*witness_data2, memo_key2)
})
})
.collect::<Vec<_>>();
Expand Down
5 changes: 4 additions & 1 deletion crates/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ web-sys = { version = "0.3.64", features = ["console"] }

[dev-dependencies]
wasm-bindgen-test = "0.3.37"
serde_json = "1.0.107"
serde_json = "1.0.107"

[profile.release]
lto = true
6 changes: 3 additions & 3 deletions crates/wasm/publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"wasm-pack": "^0.12.1"
},
"devDependencies": {
"@types/node": "^20.5.6",
"@types/node": "^20.9.4",
"prettier": "^3.0.2",
"tsx": "^3.12.7",
"typescript": "^5.2.2",
"prettier": "^3.0.2"
"typescript": "^5.2.2"
}
}
35 changes: 34 additions & 1 deletion crates/wasm/publish/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { execSync } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';

const TARGETS = ['web', 'nodejs', 'bundler'];

Expand All @@ -13,9 +13,42 @@ TARGETS.forEach(target => {
},
);

if (target === 'bundler') {
// Copy binary files to the package directory
const binaryDir = path.join(process.cwd(), '../../crypto/proof-params/src/gen/');
const targetPackageDir = path.join(process.cwd(), `${target}`);

// Ensure the target binary directory exists
if (existsSync(binaryDir)) {
const targetBinaryDir = path.join(targetPackageDir, 'bin');
if (!existsSync(targetBinaryDir)) {
mkdirSync(targetBinaryDir);
}

// Copy binary files to the package directory
const binaryFiles = [
'delegator_vote_pk.bin',
'nullifier_derivation_pk.bin',
'output_pk.bin',
'spend_pk.bin',
'swap_pk.bin',
'swapclaim_pk.bin',
'undelegateclaim_pk.bin'
];
binaryFiles.forEach(file => {
const sourcePath = path.join(binaryDir, file);
const targetPath = path.join(targetBinaryDir, file);
copyFileSync(sourcePath, targetPath);
});
}
}

// Rename package to target-specific names
const packageJsonPath = path.join(process.cwd(), `${target}/package.json`);
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
if (!packageJson.files.includes('bin')) {
packageJson.files.push('bin');
}
packageJson.name = `@penumbra-zone/wasm-${target}`;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8');

Expand Down
44 changes: 44 additions & 0 deletions crates/wasm/src/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::error::WasmResult;
use std::str::FromStr;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;

use crate::utils;
use penumbra_keys::FullViewingKey;
use penumbra_proto::core::transaction::v1alpha1 as pb;
use penumbra_transaction::{plan::ActionPlan, plan::TransactionPlan, WitnessData};

/// Builds a planned [`Action`] specified by
/// the [`ActionPlan`] in a [`TransactionPlan`].
/// Arguments:
/// transaction_plan: `TransactionPlan`
/// action_plan: `ActionPlan`
/// full_viewing_key: `bech32m String`,
/// witness_data: `WitnessData``
/// Returns: `Action`
#[wasm_bindgen]
pub fn build_action(
transaction_plan: JsValue,
action_plan: JsValue,
full_viewing_key: &str,
witness_data: JsValue,
) -> WasmResult<JsValue> {
utils::set_panic_hook();

let transaction_plan: TransactionPlan =
serde_wasm_bindgen::from_value(transaction_plan.clone())?;

let witness_data_proto: pb::WitnessData = serde_wasm_bindgen::from_value(witness_data)?;
let witness_data: WitnessData = witness_data_proto.try_into()?;
TalDerei marked this conversation as resolved.
Show resolved Hide resolved

let action_plan: ActionPlan = serde_wasm_bindgen::from_value(action_plan)?;

let full_viewing_key: FullViewingKey = FullViewingKey::from_str(full_viewing_key)?;

let memo_key = transaction_plan.memo_plan.map(|memo_plan| memo_plan.key);

let action = ActionPlan::build_unauth(action_plan, &full_viewing_key, &witness_data, memo_key)?;

let action_result_proto = serde_wasm_bindgen::to_value(&Some(action))?;
TalDerei marked this conversation as resolved.
Show resolved Hide resolved
Ok(action_result_proto)
TalDerei marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]
extern crate core;

pub mod build;
pub mod error;
pub mod keys;
pub mod note_record;
Expand Down
117 changes: 5 additions & 112 deletions crates/wasm/src/wasm_planner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::str::FromStr;

use anyhow::{anyhow, Context};
use anyhow::anyhow;
use ark_ff::UniformRand;
use decaf377::Fq;
use rand_core::OsRng;
Expand All @@ -9,20 +7,17 @@ use wasm_bindgen::JsValue;

use penumbra_chain::params::{ChainParameters, FmdParameters};
use penumbra_dex::swap_claim::SwapClaimPlan;
use penumbra_keys::FullViewingKey;
use penumbra_proto::{
core::{
asset::v1alpha1::{DenomMetadata, Value},
component::fee::v1alpha1::{Fee, GasPrices},
component::ibc::v1alpha1::Ics20Withdrawal,
keys::v1alpha1::{Address, AddressIndex},
transaction::v1alpha1 as pb,
transaction::v1alpha1::MemoPlaintext,
},
crypto::tct::v1alpha1::StateCommitment,
DomainType,
};
use penumbra_transaction::{action::Action, plan::ActionPlan, plan::TransactionPlan, WitnessData};

use crate::error::WasmResult;
use crate::planner::Planner;
Expand Down Expand Up @@ -65,112 +60,6 @@ impl WasmPlanner {
Ok(planner)
}

/// Builds a planned [`Action`] specified by
/// the [`ActionPlan`] in a [`TransactionPlan`].
/// Arguments:
/// &self: `WasmPlanner`
/// transaction_plan: `TransactionPlan`
/// action_plan: `ActionPlan`
/// full_viewing_key: `bech32m String`,
/// witness_data: `WitnessData``
/// Returns: `Action`
#[wasm_bindgen]
pub fn build_action(
&self,
transaction_plan: JsValue,
action_plan: JsValue,
full_viewing_key: &str,
witness_data: JsValue,
) -> WasmResult<JsValue> {
utils::set_panic_hook();

let transaction_plan: TransactionPlan =
serde_wasm_bindgen::from_value(transaction_plan.clone())?;

let witness_data_proto: pb::WitnessData = serde_wasm_bindgen::from_value(witness_data)?;
let witness_data: WitnessData = witness_data_proto.try_into()?;

let action_plan: ActionPlan = serde_wasm_bindgen::from_value(action_plan)?;

let full_viewing_key: FullViewingKey = FullViewingKey::from_str(full_viewing_key)?;

let memo_key = transaction_plan.memo_plan.map(|memo_plan| memo_plan.key);

let action = match action_plan {
ActionPlan::Spend(spend_plan) => {
let spend = ActionPlan::Spend(spend_plan);
Some(spend.build_unauth(&full_viewing_key, &witness_data, memo_key)?)
}
ActionPlan::Output(output_plan) => {
let output = ActionPlan::Output(output_plan);
Some(output.build_unauth(&full_viewing_key, &witness_data, memo_key)?)
}

// TODO: Other action variants besides 'Spend' and 'Output' still require testing.
ActionPlan::Swap(swap_plan) => {
let swap = ActionPlan::Swap(swap_plan);
Some(swap.build_unauth(&full_viewing_key, &witness_data, memo_key)?)
}
ActionPlan::SwapClaim(swap_claim_plan) => {
let swap_claim = ActionPlan::SwapClaim(swap_claim_plan);
Some(swap_claim.build_unauth(&full_viewing_key, &witness_data, memo_key)?)
}
ActionPlan::Delegate(delegation) => Some(Action::Delegate(delegation)),
ActionPlan::Undelegate(undelegation) => Some(Action::Undelegate(undelegation)),
ActionPlan::UndelegateClaim(undelegate_claim) => {
let undelegate_claim = undelegate_claim.undelegate_claim();
Some(Action::UndelegateClaim(undelegate_claim))
}
ActionPlan::ProposalSubmit(proposal_submit) => {
Some(Action::ProposalSubmit(proposal_submit))
}
ActionPlan::ProposalWithdraw(proposal_withdraw) => {
Some(Action::ProposalWithdraw(proposal_withdraw))
}
ActionPlan::ValidatorVote(validator_vote) => {
Some(Action::ValidatorVote(validator_vote))
}
ActionPlan::DelegatorVote(delegator_vote) => {
let note_commitment = delegator_vote.staked_note.commit();
let auth_path = witness_data
.state_commitment_proofs
.get(&note_commitment)
.context(format!("could not get proof for {note_commitment:?}"))?;

Some(Action::DelegatorVote(delegator_vote.delegator_vote(
&full_viewing_key,
[0; 64].into(),
auth_path.clone(),
)))
}
ActionPlan::ProposalDepositClaim(proposal_deposit_claim) => {
Some(Action::ProposalDepositClaim(proposal_deposit_claim))
}
ActionPlan::ValidatorDefinition(validator_definition) => {
Some(Action::ValidatorDefinition(validator_definition))
}
ActionPlan::IbcAction(ibc_action) => Some(Action::IbcRelay(ibc_action)),
ActionPlan::DaoSpend(dao_spend) => Some(Action::DaoSpend(dao_spend)),
ActionPlan::DaoOutput(dao_output) => Some(Action::DaoOutput(dao_output)),
ActionPlan::DaoDeposit(dao_deposit) => Some(Action::DaoDeposit(dao_deposit)),
ActionPlan::PositionOpen(position_open) => Some(Action::PositionOpen(position_open)),
ActionPlan::PositionClose(position_close) => {
Some(Action::PositionClose(position_close))
}
ActionPlan::PositionWithdraw(position_withdrawn) => Some(Action::PositionWithdraw(
position_withdrawn.position_withdraw(),
)),
ActionPlan::Withdrawal(ics20_withdrawal) => {
Some(Action::Ics20Withdrawal(ics20_withdrawal))
}
// TODO: Should we handle `PositionRewardClaim`?
_ => None,
};

let action_result_proto = serde_wasm_bindgen::to_value(&Some(action))?;
Ok(action_result_proto)
}

/// Public getter for the 'storage' field
pub fn get_storage(&self) -> *const IndexedDBStorage {
&self.storage
Expand All @@ -190,6 +79,7 @@ impl WasmPlanner {
/// Set gas prices
/// Arguments:
/// gas_prices: `GasPrices`
#[wasm_bindgen]
pub fn set_gas_prices(&mut self, gas_prices: JsValue) -> WasmResult<()> {
TalDerei marked this conversation as resolved.
Show resolved Hide resolved
let gas_prices_proto: GasPrices = serde_wasm_bindgen::from_value(gas_prices)?;
self.planner.set_gas_prices(gas_prices_proto.try_into()?);
Expand All @@ -199,6 +89,7 @@ impl WasmPlanner {
/// Add memo to plan
/// Arguments:
/// memo: `MemoPlaintext`
#[wasm_bindgen]
pub fn memo(&mut self, memo: JsValue) -> WasmResult<()> {
utils::set_panic_hook();
let memo_proto: MemoPlaintext = serde_wasm_bindgen::from_value(memo)?;
Expand All @@ -209,6 +100,7 @@ impl WasmPlanner {
/// Add fee to plan
/// Arguments:
/// fee: `Fee`
#[wasm_bindgen]
pub fn fee(&mut self, fee: JsValue) -> WasmResult<()> {
utils::set_panic_hook();

Expand All @@ -222,6 +114,7 @@ impl WasmPlanner {
/// Arguments:
/// value: `Value`
/// address: `Address`
#[wasm_bindgen]
pub fn output(&mut self, value: JsValue, address: JsValue) -> WasmResult<()> {
utils::set_panic_hook();

Expand Down
Loading
Loading