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

Generate test vectors for batched txs. #4217

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Require the hardware wallet to support displaying batched transactions.
([\#4217](https://github.com/anoma/namada/pull/4217))
33 changes: 33 additions & 0 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,39 @@ pub mod testing {
}
}

prop_compose! {
/// Generate an arbitrary batched memoed transaction
pub fn arb_memoed_batch_tx()(
mut header in arb_header(0),
wrapper in arb_wrapper_tx(),
txs in collection::vec(arb_memoed_tx(), 0..=5),
) -> (Tx, Vec<TxData>) {
header.tx_type = TxType::Wrapper(Box::new(wrapper));
let mut batched_tx = Tx { header, sections: vec![] };
let mut tx_datas = vec![];
for (tx, tx_data) in txs {
let cmt = tx.first_commitments().unwrap().to_owned();
batched_tx.add_inner_tx(tx, cmt);
tx_datas.push(tx_data);
}
(batched_tx, tx_datas)
}
}

prop_compose! {
/// Generate an arbitrary signed batched memoed transaction
pub fn arb_signed_batch_tx()(tx in arb_memoed_batch_tx())(
sigs in collection::vec(arb_signature(tx.0.sechashes()), 0..3),
mut tx in Just(tx),
) -> (Tx, Vec<TxData>) {
for sig in sigs {
// Add all the generated signature sections
tx.0.add_section(Section::Authorization(sig));
}
(tx.0, tx.1)
}
}

prop_compose! {
/// Generate an arbitrary vote proposal transaction
pub fn arb_vote_proposal_tx()(
Expand Down
50 changes: 25 additions & 25 deletions crates/sdk/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ pub async fn to_ledger_vector(
blob: HEXLOWER.encode(&tx.serialize_to_vec()),
index: 0,
valid: true,
name: "Custom_0".to_string(),
name: "__".to_string(),
..Default::default()
};

Expand Down Expand Up @@ -1092,7 +1092,7 @@ pub async fn to_ledger_vector(
.map_err(|err| {
Error::from(EncodingError::Conversion(err.to_string()))
})?;
tv.name = "Init_Account_0".to_string();
tv.name.push_str("Init_Account__");

let extra = tx
.get_section(&init_account.vp_code_hash)
Expand Down Expand Up @@ -1136,7 +1136,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Become_Validator_0".to_string();
tv.name.push_str("Become_Validator__");

tv.output
.extend(vec!["Type : Become Validator".to_string()]);
Expand Down Expand Up @@ -1209,7 +1209,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Init_Proposal_0".to_string();
tv.name.push_str("Init_Proposal__");

let extra = tx
.get_section(&init_proposal_data.content)
Expand Down Expand Up @@ -1271,7 +1271,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Vote_Proposal_0".to_string();
tv.name.push_str("Vote_Proposal__");

tv.output.extend(vec![
format!("Type : Vote Proposal"),
Expand All @@ -1294,7 +1294,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Reveal_Pubkey_0".to_string();
tv.name.push_str("Reveal_Pubkey__");

tv.output.extend(vec![
format!("Type : Reveal Pubkey"),
Expand All @@ -1312,7 +1312,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Update_Account_0".to_string();
tv.name.push_str("Update_Account__");
tv.output.extend(vec![
format!("Type : Update Account"),
format!("Address : {}", update_account.addr),
Expand Down Expand Up @@ -1377,7 +1377,7 @@ pub async fn to_ledger_vector(
.map_err(|err| {
Error::from(EncodingError::Conversion(err.to_string()))
})?;
tv.name = "Transfer_0".to_string();
tv.name.push_str("Transfer__");
tv.output.push("Type : Transfer".to_string());

// To facilitate lookups of MASP AssetTypes
Expand Down Expand Up @@ -1412,7 +1412,7 @@ pub async fn to_ledger_vector(
if let Ok(transfer) =
MsgTransfer::<token::Transfer>::try_from_slice(data.as_ref())
{
tv.name = "IBC_Transfer_0".to_string();
tv.name.push_str("IBC_Transfer__");
tv.output.push("Type : IBC Transfer".to_string());
let transfer_token = format!(
"{} {}",
Expand Down Expand Up @@ -1506,7 +1506,7 @@ pub async fn to_ledger_vector(
} else if let Ok(transfer) =
MsgNftTransfer::<token::Transfer>::try_from_slice(data.as_ref())
{
tv.name = "IBC_NFT_Transfer_0".to_string();
tv.name.push_str("IBC_NFT_Transfer__");
tv.output.push("Type : IBC NFT Transfer".to_string());
tv.output.extend(vec![
format!("Source port : {}", transfer.message.port_id_on_a),
Expand Down Expand Up @@ -1684,7 +1684,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Bond_0".to_string();
tv.name.push_str("Bond__");

tv.output.push("Type : Bond".to_string());
if let Some(source) = bond.source.as_ref() {
Expand Down Expand Up @@ -1717,7 +1717,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Unbond_0".to_string();
tv.name.push_str("Unbond__");

tv.output.push("Type : Unbond".to_string());
if let Some(source) = unbond.source.as_ref() {
Expand Down Expand Up @@ -1750,7 +1750,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Withdraw_0".to_string();
tv.name.push_str("Withdraw__");

tv.output.push("Type : Withdraw".to_string());
if let Some(source) = withdraw.source.as_ref() {
Expand All @@ -1773,7 +1773,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Claim_Rewards_0".to_string();
tv.name.push_str("Claim_Rewards__");

tv.output.push("Type : Claim Rewards".to_string());
if let Some(source) = claim.source.as_ref() {
Expand All @@ -1795,7 +1795,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Change_Commission_0".to_string();
tv.name.push_str("Change_Commission__");

tv.output.extend(vec![
format!("Type : Change commission"),
Expand All @@ -1816,7 +1816,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Change_MetaData_0".to_string();
tv.name.push_str("Change_MetaData__");

tv.output.extend(vec!["Type : Change metadata".to_string()]);

Expand Down Expand Up @@ -1859,7 +1859,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Change_Consensus_Key_0".to_string();
tv.name.push_str("Change_Consensus_Key__");

tv.output.extend(vec![
format!("Type : Change consensus key"),
Expand All @@ -1886,7 +1886,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Unjail_Validator_0".to_string();
tv.name.push_str("Unjail_Validator__");

tv.output.extend(vec![
format!("Type : Unjail Validator"),
Expand All @@ -1904,7 +1904,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Deactivate_Validator_0".to_string();
tv.name.push_str("Deactivate_Validator__");

tv.output.extend(vec![
format!("Type : Deactivate Validator"),
Expand All @@ -1922,7 +1922,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Reactivate_Validator_0".to_string();
tv.name.push_str("Reactivate_Validator__");

tv.output.extend(vec![
format!("Type : Reactivate Validator"),
Expand All @@ -1939,7 +1939,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Redelegate_0".to_string();
tv.name.push_str("Redelegate__");

tv.output.extend(vec![
format!("Type : Redelegate"),
Expand Down Expand Up @@ -1977,7 +1977,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Update_Steward_Commission_0".to_string();
tv.name.push_str("Update_Steward_Commission__");
tv.output.extend(vec![
format!("Type : Update Steward Commission"),
format!("Steward : {}", update.steward),
Expand All @@ -2002,7 +2002,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Resign_Steward_0".to_string();
tv.name.push_str("Resign_Steward__");

tv.output.extend(vec![
format!("Type : Resign Steward"),
Expand All @@ -2019,7 +2019,7 @@ pub async fn to_ledger_vector(
Error::from(EncodingError::Conversion(err.to_string()))
})?;

tv.name = "Bridge_Pool_Transfer_0".to_string();
tv.name.push_str("Bridge_Pool_Transfer__");

tv.output.extend(vec![
format!("Type : Bridge Pool Transfer"),
Expand All @@ -2044,7 +2044,7 @@ pub async fn to_ledger_vector(
format!("Gas Amount : {}", transfer.gas_fee.amount),
]);
} else {
tv.name = "Custom_0".to_string();
tv.name.push_str("Custom__");
tv.output.push("Type : Custom".to_string());
}

Expand Down
5 changes: 3 additions & 2 deletions examples/generate_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;

use data_encoding::HEXLOWER;
use namada_sdk::signing::to_ledger_vector;
use namada_sdk::testing::arb_signed_tx;
use namada_sdk::testing::arb_signed_batch_tx;
use namada_sdk::wallet::fs::FsWalletUtils;
use proptest::strategy::{Strategy, ValueTree};
use proptest::test_runner::{Reason, TestRunner};
Expand All @@ -16,7 +16,8 @@ async fn main() -> Result<(), Reason> {
let mut test_vectors = vec![];
let mut serialized_txs = vec![];
for i in 0..1000 {
let (tx, tx_data) = arb_signed_tx().new_tree(&mut runner)?.current();
let (tx, tx_data) =
arb_signed_batch_tx().new_tree(&mut runner)?.current();
let mut ledger_vector = to_ledger_vector(&wallet, &tx)
.await
.expect("unable to construct test vector");
Expand Down
Loading