Skip to content

Commit

Permalink
Merge branch 'murisi/unconstrained-transfers' (#3459)
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Jul 2, 2024
2 parents 4455bba + 98c88ec commit 3f467a8
Show file tree
Hide file tree
Showing 39 changed files with 974 additions and 1,375 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Combined the various Transfer formats into one general one.
([\#3446](https://github.com/anoma/namada/pull/3446))
17 changes: 8 additions & 9 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3127,11 +3127,10 @@ pub mod args {
TX_CHANGE_METADATA_WASM, TX_CLAIM_REWARDS_WASM,
TX_DEACTIVATE_VALIDATOR_WASM, TX_IBC_WASM, TX_INIT_ACCOUNT_WASM,
TX_INIT_PROPOSAL, TX_REACTIVATE_VALIDATOR_WASM, TX_REDELEGATE_WASM,
TX_RESIGN_STEWARD, TX_REVEAL_PK, TX_SHIELDED_TRANSFER_WASM,
TX_SHIELDING_TRANSFER_WASM, TX_TRANSPARENT_TRANSFER_WASM,
TX_UNBOND_WASM, TX_UNJAIL_VALIDATOR_WASM, TX_UNSHIELDING_TRANSFER_WASM,
TX_UPDATE_ACCOUNT_WASM, TX_UPDATE_STEWARD_COMMISSION, TX_VOTE_PROPOSAL,
TX_WITHDRAW_WASM, VP_USER_WASM,
TX_RESIGN_STEWARD, TX_REVEAL_PK, TX_TRANSFER_WASM, TX_UNBOND_WASM,
TX_UNJAIL_VALIDATOR_WASM, TX_UPDATE_ACCOUNT_WASM,
TX_UPDATE_STEWARD_COMMISSION, TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM,
VP_USER_WASM,
};
use namada_sdk::DEFAULT_GAS_LIMIT;

Expand Down Expand Up @@ -4394,7 +4393,7 @@ pub mod args {
let target = TARGET.parse(matches);
let token = TOKEN.parse(matches);
let amount = InputAmount::Unvalidated(AMOUNT.parse(matches));
let tx_code_path = PathBuf::from(TX_TRANSPARENT_TRANSFER_WASM);
let tx_code_path = PathBuf::from(TX_TRANSFER_WASM);
let data = vec![TxTransparentTransferData {
source,
target,
Expand Down Expand Up @@ -4467,7 +4466,7 @@ pub mod args {
let target = PAYMENT_ADDRESS_TARGET.parse(matches);
let token = TOKEN.parse(matches);
let amount = InputAmount::Unvalidated(AMOUNT.parse(matches));
let tx_code_path = PathBuf::from(TX_SHIELDED_TRANSFER_WASM);
let tx_code_path = PathBuf::from(TX_TRANSFER_WASM);
let data = vec![TxShieldedTransferData {
source,
target,
Expand Down Expand Up @@ -4547,7 +4546,7 @@ pub mod args {
let target = PAYMENT_ADDRESS_TARGET.parse(matches);
let token = TOKEN.parse(matches);
let amount = InputAmount::Unvalidated(AMOUNT.parse(matches));
let tx_code_path = PathBuf::from(TX_SHIELDING_TRANSFER_WASM);
let tx_code_path = PathBuf::from(TX_TRANSFER_WASM);
let data = vec![TxShieldingTransferData {
source,
token,
Expand Down Expand Up @@ -4625,7 +4624,7 @@ pub mod args {
let target = TARGET.parse(matches);
let token = TOKEN.parse(matches);
let amount = InputAmount::Unvalidated(AMOUNT.parse(matches));
let tx_code_path = PathBuf::from(TX_UNSHIELDING_TRANSFER_WASM);
let tx_code_path = PathBuf::from(TX_TRANSFER_WASM);
let data = vec![TxUnshieldingTransferData {
target,
token,
Expand Down
27 changes: 14 additions & 13 deletions crates/benches/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ use namada::core::account::AccountPublicKeysMap;
use namada::core::address;
use namada::core::collections::{HashMap, HashSet};
use namada::ledger::storage::DB;
use namada::token::{Amount, TransparentTransfer, TransparentTransferData};
use namada::token::{Amount, Transfer};
use namada::tx::Authorization;
use namada::vm::wasm::TxCache;
use namada_apps_lib::wallet::defaults;
use namada_apps_lib::wasm_loader;
use namada_node::bench_utils::{
BenchShell, TX_INIT_PROPOSAL_WASM, TX_REVEAL_PK_WASM,
TX_TRANSPARENT_TRANSFER_WASM, TX_UPDATE_ACCOUNT_WASM, VP_USER_WASM,
WASM_DIR,
BenchShell, TX_INIT_PROPOSAL_WASM, TX_REVEAL_PK_WASM, TX_TRANSFER_WASM,
TX_UPDATE_ACCOUNT_WASM, VP_USER_WASM, WASM_DIR,
};

// Benchmarks the validation of a single signature on a single `Section` of a
// transaction
fn tx_section_signature_validation(c: &mut Criterion) {
let shell = BenchShell::default();
let transfer_data = TransparentTransfer(vec![TransparentTransferData {
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_TRANSPARENT_TRANSFER_WASM,
TX_TRANSFER_WASM,
transfer_data,
None,
None,
Expand Down Expand Up @@ -62,7 +63,7 @@ fn compile_wasm(c: &mut Criterion) {
let mut txs: HashMap<&str, Vec<u8>> = HashMap::default();

for tx in [
TX_TRANSPARENT_TRANSFER_WASM,
TX_TRANSFER_WASM,
TX_INIT_PROPOSAL_WASM,
TX_REVEAL_PK_WASM,
TX_UPDATE_ACCOUNT_WASM,
Expand Down Expand Up @@ -111,7 +112,7 @@ fn untrusted_wasm_validation(c: &mut Criterion) {
let mut txs: HashMap<&str, Vec<u8>> = HashMap::default();

for tx in [
TX_TRANSPARENT_TRANSFER_WASM,
TX_TRANSFER_WASM,
TX_INIT_PROPOSAL_WASM,
TX_REVEAL_PK_WASM,
TX_UPDATE_ACCOUNT_WASM,
Expand Down
21 changes: 11 additions & 10 deletions crates/benches/native_vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ 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, TransparentTransfer, TransparentTransferData};
use namada::token::{Amount, Transfer};
use namada::tx::{BatchedTx, Code, Section, Tx};
use namada_apps_lib::wallet::defaults;
use namada_node::bench_utils::{
generate_foreign_key_tx, BenchShell, BenchShieldedCtx,
ALBERT_PAYMENT_ADDRESS, ALBERT_SPENDING_KEY, BERTHA_PAYMENT_ADDRESS,
TX_BRIDGE_POOL_WASM, TX_IBC_WASM, TX_INIT_PROPOSAL_WASM, TX_RESIGN_STEWARD,
TX_TRANSPARENT_TRANSFER_WASM, TX_UPDATE_STEWARD_COMMISSION,
TX_VOTE_PROPOSAL_WASM,
TX_TRANSFER_WASM, TX_UPDATE_STEWARD_COMMISSION, TX_VOTE_PROPOSAL_WASM,
};
use rand_core::OsRng;

Expand Down Expand Up @@ -475,13 +474,15 @@ fn vp_multitoken(c: &mut Criterion) {
generate_foreign_key_tx(&defaults::albert_keypair());

let transfer = shell.generate_tx(
TX_TRANSPARENT_TRANSFER_WASM,
TransparentTransfer(vec![TransparentTransferData {
source: defaults::albert_address(),
target: defaults::bertha_address(),
token: address::testing::nam(),
amount: Amount::native_whole(1000).native_denominated(),
}]),
TX_TRANSFER_WASM,
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
22 changes: 11 additions & 11 deletions crates/benches/process_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use namada::core::key::RefTo;
use namada::core::storage::BlockHeight;
use namada::core::time::DateTimeUtc;
use namada::state::TxIndex;
use namada::token::{
Amount, DenominatedAmount, TransparentTransfer, TransparentTransferData,
};
use namada::token::{Amount, DenominatedAmount, Transfer};
use namada::tx::data::{Fee, WrapperTx};
use namada::tx::Authorization;
use namada_apps_lib::wallet::defaults;
use namada_node::bench_utils::{BenchShell, TX_TRANSPARENT_TRANSFER_WASM};
use namada_node::bench_utils::{BenchShell, TX_TRANSFER_WASM};
use namada_node::shell::process_proposal::ValidationMeta;

fn process_tx(c: &mut Criterion) {
Expand All @@ -21,13 +19,15 @@ fn process_tx(c: &mut Criterion) {
BlockHeight(2);

let mut batched_tx = shell.generate_tx(
TX_TRANSPARENT_TRANSFER_WASM,
TransparentTransfer(vec![TransparentTransferData {
source: defaults::albert_address(),
target: defaults::bertha_address(),
token: address::testing::nam(),
amount: Amount::native_whole(1).native_denominated(),
}]),
TX_TRANSFER_WASM,
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
21 changes: 5 additions & 16 deletions crates/encoding_spec/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let signature_schema = schema_container_of::<Signature>();
let init_account_schema = schema_container_of::<account::InitAccount>();
let init_validator_schema = schema_container_of::<pos::BecomeValidator>();
let transparent_transfer_schema =
schema_container_of::<token::TransparentTransfer>();
let shielded_transfer_schema =
schema_container_of::<token::ShieldedTransfer>();
let shielding_transfer_schema =
schema_container_of::<token::ShieldingTransfer>();
let unshielding_transfer_schema =
schema_container_of::<token::UnshieldingTransfer>();
let transfer_schema = schema_container_of::<token::Transfer>();
let update_account = schema_container_of::<account::UpdateAccount>();
let pos_bond_schema = schema_container_of::<pos::Bond>();
let pos_withdraw_schema = schema_container_of::<pos::Withdraw>();
Expand All @@ -115,10 +108,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
definitions.extend(btree(&signature_schema));
definitions.extend(btree(&init_account_schema));
definitions.extend(btree(&init_validator_schema));
definitions.extend(btree(&transparent_transfer_schema));
definitions.extend(btree(&shielded_transfer_schema));
definitions.extend(btree(&shielding_transfer_schema));
definitions.extend(btree(&unshielding_transfer_schema));
definitions.extend(btree(&transfer_schema));
definitions.extend(btree(&update_account));
definitions.extend(btree(&pos_bond_schema));
definitions.extend(btree(&pos_withdraw_schema));
Expand Down Expand Up @@ -191,11 +181,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/transaction/struct.InitValidator.html");
tables.push(init_validator_table);

let token_transfer_definition = definitions
.remove(transparent_transfer_schema.declaration())
.unwrap();
let token_transfer_definition =
definitions.remove(transfer_schema.declaration()).unwrap();
let token_transfer_table = definition_to_table(
transparent_transfer_schema.declaration(),
transfer_schema.declaration(),
token_transfer_definition,
).with_rust_doc_link("https://dev.namada.net/master/rustdoc/namada/types/token/struct.Transfer.html");
tables.push(token_transfer_table);
Expand Down
86 changes: 86 additions & 0 deletions crates/events/src/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use std::marker::PhantomData;
use std::ops::ControlFlow;
use std::str::FromStr;

use namada_core::address::Address;
use namada_core::collections::HashMap;
use namada_core::hash::Hash;
use namada_core::masp::MaspTxRefs;
use namada_core::storage::{BlockHeight, TxIndex};
use serde::Deserializer;

use super::*;

Expand Down Expand Up @@ -666,6 +668,90 @@ where
}
}

/// Implement the Display and FromStr traits for any serde type
#[derive(Default, Clone, Serialize, Deserialize)]
pub struct EventValue<T>(pub T);

impl<T: Serialize> Display for EventValue<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let ser =
serde_json::to_string(&self.0).map_err(|_| std::fmt::Error)?;
write!(f, "{}", ser)
}
}

impl<T: for<'de> Deserialize<'de>> FromStr for EventValue<T> {
type Err = serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s).map(Self)
}
}

impl<T> From<T> for EventValue<T> {
fn from(t: T) -> Self {
Self(t)
}
}

/// A user account.
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum UserAccount {
/// Internal chain address in Namada.
Internal(Address),
/// External chain address.
External(String),
}

impl fmt::Display for UserAccount {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Internal(addr) => write!(f, "internal-address/{addr}"),
Self::External(addr) => write!(f, "external-address/{addr}"),
}
}
}

impl FromStr for UserAccount {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.split_once('/') {
Some(("internal-address", addr)) => {
Ok(Self::Internal(Address::decode(addr).map_err(|err| {
format!(
"Unknown internal address balance change target \
{s:?}: {err}"
)
})?))
}
Some(("external-address", addr)) => {
Ok(Self::External(addr.to_owned()))
}
_ => Err(format!("Unknown balance change target {s:?}")),
}
}
}

impl serde::Serialize for UserAccount {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(&self.to_string())
}
}

impl<'de> Deserialize<'de> for UserAccount {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = <String as Deserialize>::deserialize(deserializer)?;
FromStr::from_str(&s).map_err(serde::de::Error::custom)
}
}

#[cfg(test)]
mod event_composition_tests {
use super::*;
Expand Down
1 change: 0 additions & 1 deletion crates/ibc/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ where
let data = MsgTransfer {
message,
transfer: None,
fee_unshield: None,
}
.serialize_to_vec();

Expand Down
Loading

0 comments on commit 3f467a8

Please sign in to comment.