Skip to content

Commit

Permalink
Unconstrain transfers and combine transfer amounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Jul 1, 2024
1 parent 6110726 commit 317b3ad
Show file tree
Hide file tree
Showing 14 changed files with 431 additions and 348 deletions.
16 changes: 9 additions & 7 deletions crates/benches/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use namada::core::account::AccountPublicKeysMap;
use namada::core::address;
use namada::core::collections::{HashMap, HashSet};
use namada::ledger::storage::DB;
use namada::token::{Amount, Transfer, TransparentTransfer};
use namada::token::{Amount, Transfer};
use namada::tx::Authorization;
use namada::vm::wasm::TxCache;
use namada_apps_lib::wallet::defaults;
Expand All @@ -17,12 +17,14 @@ use namada_node::bench_utils::{
// transaction
fn tx_section_signature_validation(c: &mut Criterion) {
let shell = BenchShell::default();
let transfer_data = Transfer::transparent(vec![TransparentTransfer {
source: defaults::albert_address(),
target: defaults::bertha_address(),
token: address::testing::nam(),
amount: Amount::native_whole(500).native_denominated(),
}]);
let transfer_data = Transfer::default()
.transfer(
defaults::albert_address(),
defaults::bertha_address(),
address::testing::nam(),
Amount::native_whole(500).native_denominated(),
)
.unwrap();
let tx = shell.generate_tx(
TX_TRANSFER_WASM,
transfer_data,
Expand Down
16 changes: 9 additions & 7 deletions crates/benches/native_vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use namada::sdk::masp_primitives::merkle_tree::CommitmentTree;
use namada::sdk::masp_primitives::transaction::Transaction;
use namada::sdk::masp_proofs::sapling::SaplingVerificationContextInner;
use namada::state::{Epoch, StorageRead, StorageWrite, TxIndex};
use namada::token::{Amount, Transfer, TransparentTransfer};
use namada::token::{Amount, Transfer};
use namada::tx::{BatchedTx, Code, Section, Tx};
use namada_apps_lib::wallet::defaults;
use namada_node::bench_utils::{
Expand Down Expand Up @@ -475,12 +475,14 @@ fn vp_multitoken(c: &mut Criterion) {

let transfer = shell.generate_tx(
TX_TRANSFER_WASM,
Transfer::transparent(vec![TransparentTransfer {
source: defaults::albert_address(),
target: defaults::bertha_address(),
token: address::testing::nam(),
amount: Amount::native_whole(1000).native_denominated(),
}]),
Transfer::default()
.transfer(
defaults::albert_address(),
defaults::bertha_address(),
address::testing::nam(),
Amount::native_whole(1000).native_denominated(),
)
.unwrap(),
None,
None,
vec![&defaults::albert_keypair()],
Expand Down
16 changes: 9 additions & 7 deletions crates/benches/process_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use namada::core::address;
use namada::core::key::RefTo;
use namada::core::storage::BlockHeight;
use namada::core::time::DateTimeUtc;
use namada::token::{Amount, DenominatedAmount, Transfer, TransparentTransfer};
use namada::token::{Amount, DenominatedAmount, Transfer};
use namada::tx::data::{Fee, WrapperTx};
use namada::tx::Authorization;
use namada_apps_lib::wallet::defaults;
Expand All @@ -19,12 +19,14 @@ fn process_tx(c: &mut Criterion) {

let mut batched_tx = shell.generate_tx(
TX_TRANSFER_WASM,
Transfer::transparent(vec![TransparentTransfer {
source: defaults::albert_address(),
target: defaults::bertha_address(),
token: address::testing::nam(),
amount: Amount::native_whole(1).native_denominated(),
}]),
Transfer::default()
.transfer(
defaults::albert_address(),
defaults::bertha_address(),
address::testing::nam(),
Amount::native_whole(1).native_denominated(),
)
.unwrap(),
None,
None,
vec![&defaults::albert_keypair()],
Expand Down
38 changes: 3 additions & 35 deletions crates/light_sdk/src/transaction/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use namada_sdk::address::Address;
use namada_sdk::hash::Hash;
use namada_sdk::key::common;
use namada_sdk::token::transaction::Transaction;
use namada_sdk::token::TransparentTransfer;
pub use namada_sdk::token::{DenominatedAmount, Transfer};
use namada_sdk::tx::data::GasLimit;
use namada_sdk::tx::{Authorization, Tx, TxError, TX_TRANSFER_WASM};
Expand All @@ -16,18 +15,10 @@ pub struct TransferBuilder(Tx);

impl TransferBuilder {
/// Build a transparent transfer transaction from the given parameters
pub fn transparent(
transfers: Vec<TransparentTransfer>,
args: GlobalArgs,
) -> Self {
let data = namada_sdk::token::Transfer {
transparent: transfers,
shielded_section_hash: None,
};

pub fn transfer(transfer: Transfer, args: GlobalArgs) -> Self {
Self(transaction::build_tx(
args,
data,
transfer,
TX_TRANSFER_WASM.to_string(),
))
}
Expand All @@ -38,30 +29,7 @@ impl TransferBuilder {
transaction: Transaction,
args: GlobalArgs,
) -> Self {
let data = namada_sdk::token::Transfer {
transparent: vec![],
shielded_section_hash: Some(shielded_section_hash),
};

let mut tx =
transaction::build_tx(args, data, TX_TRANSFER_WASM.to_string());
tx.add_masp_tx_section(transaction);

Self(tx)
}

/// Build a MASP transfer transaction from the given parameters
pub fn masp(
transfers: Vec<TransparentTransfer>,
shielded_section_hash: Hash,
transaction: Transaction,
args: GlobalArgs,
) -> Self {
let data = namada_sdk::token::Transfer {
transparent: transfers,
shielded_section_hash: Some(shielded_section_hash),
};

let data = Transfer::masp(shielded_section_hash);
let mut tx =
transaction::build_tx(args, data, TX_TRANSFER_WASM.to_string());
tx.add_masp_tx_section(transaction);
Expand Down
54 changes: 28 additions & 26 deletions crates/node/src/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use namada::ledger::queries::{
};
use namada::masp::MaspTxRefs;
use namada::state::StorageRead;
use namada::token::{Amount, DenominatedAmount, Transfer, TransparentTransfer};
use namada::token::{Amount, DenominatedAmount, Transfer};
use namada::tx::data::pos::Bond;
use namada::tx::data::{
BatchResults, BatchedTxResult, Fee, TxResult, VpsResult,
Expand Down Expand Up @@ -1113,42 +1113,37 @@ impl BenchShieldedCtx {
{
namada.client().generate_tx(
TX_TRANSFER_WASM,
Transfer {
transparent: vec![],
shielded_section_hash: Some(shielded_section_hash),
},
Transfer::masp(shielded_section_hash),
Some(shielded),
None,
vec![&defaults::albert_keypair()],
)
} else if target.effective_address() == MASP {
namada.client().generate_tx(
TX_TRANSFER_WASM,
Transfer {
transparent: vec![TransparentTransfer {
source: source.effective_address(),
target: MASP,
token: address::testing::nam(),
amount: DenominatedAmount::native(amount),
}],
shielded_section_hash: Some(shielded_section_hash),
},
Transfer::masp(shielded_section_hash)
.transfer(
source.effective_address(),
MASP,
address::testing::nam(),
DenominatedAmount::native(amount),
)
.unwrap(),
Some(shielded),
None,
vec![&defaults::albert_keypair()],
)
} else {
namada.client().generate_tx(
TX_TRANSFER_WASM,
Transfer {
transparent: vec![TransparentTransfer {
source: MASP,
target: target.effective_address(),
token: address::testing::nam(),
amount: DenominatedAmount::native(amount),
}],
shielded_section_hash: Some(shielded_section_hash),
},
Transfer::masp(shielded_section_hash)
.transfer(
MASP,
target.effective_address(),
address::testing::nam(),
DenominatedAmount::native(amount),
)
.unwrap(),
Some(shielded),
None,
vec![&defaults::albert_keypair()],
Expand Down Expand Up @@ -1215,10 +1210,17 @@ impl BenchShieldedCtx {
let vectorized_transfer =
Transfer::deserialize(&mut tx.tx.data(&tx.cmt).unwrap().as_slice())
.unwrap();
let sources =
vec![vectorized_transfer.sources.into_iter().next().unwrap()]
.into_iter()
.collect();
let targets =
vec![vectorized_transfer.targets.into_iter().next().unwrap()]
.into_iter()
.collect();
let transfer = Transfer {
transparent: vec![
vectorized_transfer.transparent.first().unwrap().to_owned(),
],
sources,
targets,
shielded_section_hash: Some(
vectorized_transfer.shielded_section_hash.unwrap(),
),
Expand Down
49 changes: 21 additions & 28 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,6 @@ pub mod testing {
use ripemd::Digest as RipemdDigest;
use sha2::Digest;
use token::testing::arb_vectorized_transparent_transfer;
use token::TransparentTransfer;

use super::*;
use crate::account::tests::{arb_init_account, arb_update_account};
Expand Down Expand Up @@ -1108,8 +1107,8 @@ pub mod testing {
code_hash in arb_hash(),
(masp_tx_type, (shielded_transfer, asset_types, build_params)) in prop_oneof![
(Just(MaspTxType::Shielded), arb_shielded_transfer(0..MAX_ASSETS)),
(Just(MaspTxType::Shielding), arb_shielding_transfer(encode_address(&transfers.transparent.first().unwrap().source), 1)),
(Just(MaspTxType::Unshielding), arb_deshielding_transfer(encode_address(&transfers.transparent.first().unwrap().target), 1)),
(Just(MaspTxType::Shielding), arb_shielding_transfer(encode_address(&transfers.sources.keys().next().unwrap().owner), 1)),
(Just(MaspTxType::Unshielding), arb_deshielding_transfer(encode_address(&transfers.targets.keys().next().unwrap().owner), 1)),
],
transfers in Just(transfers),
) -> (Tx, TxData) {
Expand All @@ -1121,10 +1120,7 @@ pub mod testing {
let tx_data = match masp_tx_type {
MaspTxType::Shielded => {
tx.add_code_from_hash(code_hash, Some(TX_TRANSFER_WASM.to_owned()));
let data = Transfer {
transparent: vec![],
shielded_section_hash: Some(shielded_section_hash),
};
let data = Transfer::masp(shielded_section_hash);
tx.add_data(data.clone());
TxData::MaspTransfer(data, (build_params, build_param_bytes))
},
Expand All @@ -1137,18 +1133,15 @@ pub mod testing {
decoded.denom,
);
tx.add_code_from_hash(code_hash, Some(TX_TRANSFER_WASM.to_owned()));
let transparent = transfers.transparent.into_iter().map(|transfer|
TransparentTransfer{
source: transfer.source,
token: token.clone(),
amount,
target: MASP,
}
).collect();
let data = Transfer{
transparent,
shielded_section_hash: Some(shielded_section_hash),
};
let data = transfers.sources.into_iter().try_fold(
Transfer::masp(shielded_section_hash),
|acc, transfer| acc.transfer(
transfer.0.owner,
MASP,
token.clone(),
amount,
),
).unwrap();
tx.add_data(data.clone());
TxData::MaspTransfer(data, (build_params, build_param_bytes))
},
Expand All @@ -1161,15 +1154,15 @@ pub mod testing {
decoded.denom,
);
tx.add_code_from_hash(code_hash, Some(TX_TRANSFER_WASM.to_owned()));
let transparent = transfers.transparent.into_iter().map(|transfer|
TransparentTransfer{
target: transfer.target,
token: token.clone(),
amount,
source: MASP,
}
).collect();
let data = Transfer{transparent, shielded_section_hash: Some(shielded_section_hash) };
let data = transfers.targets.into_iter().try_fold(
Transfer::masp(shielded_section_hash),
|acc, transfer| acc.transfer(
MASP,
transfer.0.owner,
token.clone(),
amount,
)
).unwrap();
tx.add_data(data.clone());
TxData::MaspTransfer(data, (build_params, build_param_bytes))
},
Expand Down
Loading

0 comments on commit 317b3ad

Please sign in to comment.