Skip to content

Commit

Permalink
Merge 'origin/fraccaman/fix-sdk-tx-with-hash' (#1474)
Browse files Browse the repository at this point in the history
* origin/fraccaman/fix-sdk-tx-with-hash:
  sdk: use hash instead of wasm code
  • Loading branch information
Fraccaman committed Jun 2, 2023
2 parents 899bb44 + 548dfa4 commit 183681d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 24 deletions.
80 changes: 70 additions & 10 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,13 @@ pub mod args {
sub_prefix: self.sub_prefix,
amount: self.amount,
native_token: ctx.native_token.clone(),
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
}
}
}
Expand Down Expand Up @@ -2195,7 +2201,13 @@ pub mod args {
channel_id: self.channel_id,
timeout_height: self.timeout_height,
timeout_sec_offset: self.timeout_sec_offset,
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
}
}
}
Expand Down Expand Up @@ -2264,7 +2276,13 @@ pub mod args {
.unwrap()
.to_string()
.into_bytes(),
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
public_key: ctx.get_cached(&self.public_key),
}
}
Expand Down Expand Up @@ -2326,7 +2344,13 @@ pub mod args {
.to_string()
.into_bytes(),
unsafe_dont_encrypt: self.unsafe_dont_encrypt,
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
}
}
}
Expand Down Expand Up @@ -2465,7 +2489,13 @@ pub mod args {
amount: self.amount,
source: self.source.map(|x| ctx.get(&x)),
native_token: ctx.native_token.clone(),
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
}
}
}
Expand Down Expand Up @@ -2505,7 +2535,13 @@ pub mod args {
validator: ctx.get(&self.validator),
amount: self.amount,
source: self.source.map(|x| ctx.get(&x)),
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
}
}
}
Expand Down Expand Up @@ -2562,7 +2598,13 @@ pub mod args {
proposal_data: self.proposal_data,
offline: self.offline,
native_token: ctx.native_token.clone(),
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
}
}
}
Expand Down Expand Up @@ -2624,7 +2666,13 @@ pub mod args {
vote: self.vote,
offline: self.offline,
proposal_data: self.proposal_data,
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
proposal_pgf: self.proposal_pgf,
proposal_eth: self.proposal_eth,
}
Expand Down Expand Up @@ -2849,7 +2897,13 @@ pub mod args {
tx: self.tx.to_sdk(ctx),
validator: ctx.get(&self.validator),
source: self.source.map(|x| ctx.get(&x)),
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
}
}
}
Expand Down Expand Up @@ -3081,7 +3135,13 @@ pub mod args {
tx: self.tx.to_sdk(ctx),
validator: ctx.get(&self.validator),
rate: self.rate,
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
}
}
}
Expand Down
57 changes: 43 additions & 14 deletions shared/src/ledger/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use masp_primitives::transaction::components::transparent::fees::{
};
use masp_primitives::transaction::components::Amount;
use namada_core::types::address::{masp, masp_tx_key, Address};
use namada_core::types::transaction::DecryptedTx;
use namada_proof_of_stake::parameters::PosParams;
use namada_proof_of_stake::types::CommissionPair;
use prost::EncodeError;
Expand Down Expand Up @@ -344,12 +345,22 @@ pub async fn submit_reveal_pk_aux<
let addr: Address = public_key.into();
println!("Submitting a tx to reveal the public key for address {addr}...");
let tx_data = public_key.try_to_vec().map_err(Error::EncodeKeyFailure)?;
let tx_code = args.tx_reveal_code_path.clone();
let mut tx = Tx::new(TxType::Raw);

let tx_code_path =
String::from_utf8(args.tx_reveal_code_path.clone()).unwrap();
let tx_code_hash =
query_wasm_code_hash(client, tx_code_path).await.unwrap();

let mut tx = Tx::new(TxType::Decrypted(DecryptedTx::Decrypted {
#[cfg(not(feature = "mainnet"))]
// To be able to dry-run testnet faucet withdrawal, pretend
// that we got a valid PoW
has_valid_pow: true,
}));
tx.header.chain_id = args.chain_id.clone().expect("value should be there");
tx.header.expiration = args.expiration;
tx.set_data(Data::new(tx_data));
tx.set_code(Code::new(tx_code));
tx.set_code(Code::from_hash(tx_code_hash));

// submit_tx without signing the inner tx
let keypair = if let Some(signing_key) = &args.signing_key {
Expand Down Expand Up @@ -582,7 +593,9 @@ pub async fn submit_validator_commission_change<
) -> Result<(), Error> {
let epoch = rpc::query_epoch(client).await;

let tx_code = args.tx_code_path;
let tx_code_path = String::from_utf8(args.tx_code_path).unwrap();
let tx_code_hash =
query_wasm_code_hash(client, tx_code_path).await.unwrap();

// TODO: put following two let statements in its own function
let params_key = crate::ledger::pos::params_key();
Expand Down Expand Up @@ -649,7 +662,7 @@ pub async fn submit_validator_commission_change<
tx.header.chain_id = args.tx.chain_id.clone().unwrap();
tx.header.expiration = args.tx.expiration;
tx.set_data(Data::new(data));
tx.set_code(Code::new(tx_code));
tx.set_code(Code::from_hash(tx_code_hash));

let default_signer = args.validator.clone();
process_tx::<C, U>(
Expand Down Expand Up @@ -681,7 +694,10 @@ pub async fn submit_withdraw<
.await?;

let source = args.source.clone();
let tx_code = args.tx_code_path;

let tx_code_path = String::from_utf8(args.tx_code_path).unwrap();
let tx_code_hash =
query_wasm_code_hash(client, tx_code_path).await.unwrap();

// Check the source's current unbond amount
let bond_source = source.clone().unwrap_or_else(|| validator.clone());
Expand Down Expand Up @@ -714,7 +730,7 @@ pub async fn submit_withdraw<
tx.header.chain_id = args.tx.chain_id.clone().unwrap();
tx.header.expiration = args.tx.expiration;
tx.set_data(Data::new(data));
tx.set_code(Code::new(tx_code));
tx.set_code(Code::from_hash(tx_code_hash));

let default_signer = args.source.unwrap_or(args.validator);
process_tx::<C, U>(
Expand Down Expand Up @@ -742,7 +758,11 @@ pub async fn submit_unbond<
let source = args.source.clone();
// Check the source's current bond amount
let bond_source = source.clone().unwrap_or_else(|| args.validator.clone());
let tx_code = args.tx_code_path;

let tx_code_path = String::from_utf8(args.tx_code_path).unwrap();
let tx_code_hash =
query_wasm_code_hash(client, tx_code_path).await.unwrap();

if !args.tx.force {
known_validator_or_err(args.validator.clone(), args.tx.force, client)
.await?;
Expand Down Expand Up @@ -790,7 +810,7 @@ pub async fn submit_unbond<
tx.header.chain_id = args.tx.chain_id.clone().unwrap();
tx.header.expiration = args.tx.expiration;
tx.set_data(Data::new(data));
tx.set_code(Code::new(tx_code));
tx.set_code(Code::from_hash(tx_code_hash));

let default_signer = args.source.unwrap_or_else(|| args.validator.clone());
process_tx::<C, U>(
Expand Down Expand Up @@ -891,7 +911,10 @@ pub async fn submit_bond<
)
.await?;

let tx_code = args.tx_code_path;
let tx_code_path = String::from_utf8(args.tx_code_path).unwrap();
let tx_code_hash =
query_wasm_code_hash(client, tx_code_path).await.unwrap();

let bond = pos::Bond {
validator,
amount: args.amount,
Expand All @@ -903,7 +926,7 @@ pub async fn submit_bond<
tx.header.chain_id = args.tx.chain_id.clone().unwrap();
tx.header.expiration = args.tx.expiration;
tx.set_data(Data::new(data));
tx.set_code(Code::new(tx_code));
tx.set_code(Code::from_hash(tx_code_hash));

let default_signer = args.source.unwrap_or(args.validator);
process_tx::<C, U>(
Expand Down Expand Up @@ -989,7 +1012,9 @@ pub async fn submit_ibc_transfer<
)
.await?;

let tx_code = args.tx_code_path;
let tx_code_path = String::from_utf8(args.tx_code_path).unwrap();
let tx_code_hash =
query_wasm_code_hash(client, tx_code_path).await.unwrap();

let denom = match sub_prefix {
// To parse IbcToken address, remove the address prefix
Expand Down Expand Up @@ -1039,7 +1064,7 @@ pub async fn submit_ibc_transfer<
tx.header.chain_id = args.tx.chain_id.clone().unwrap();
tx.header.expiration = args.tx.expiration;
tx.set_data(Data::new(data));
tx.set_code(Code::new(tx_code));
tx.set_code(Code::from_hash(tx_code_hash));

process_tx::<C, U>(
client,
Expand Down Expand Up @@ -1346,6 +1371,10 @@ pub async fn submit_init_account<
let vp_code_hash =
query_wasm_code_hash(client, vp_code_path).await.unwrap();

let tx_code_path = String::from_utf8(args.tx_code_path).unwrap();
let tx_code_hash =
query_wasm_code_hash(client, tx_code_path).await.unwrap();

let mut tx = Tx::new(TxType::Raw);
tx.header.chain_id = args.tx.chain_id.clone().unwrap();
tx.header.expiration = args.tx.expiration;
Expand All @@ -1359,7 +1388,7 @@ pub async fn submit_init_account<
};
let data = data.try_to_vec().map_err(Error::EncodeTxFailure)?;
tx.set_data(Data::new(data));
tx.set_code(Code::new(args.tx_code_path));
tx.set_code(Code::from_hash(tx_code_hash));

// TODO Move unwrap to an either
let initialized_accounts = process_tx::<C, U>(
Expand Down

0 comments on commit 183681d

Please sign in to comment.