Skip to content

Commit

Permalink
Merge branch 'fraccaman/fix-sdk-tx-with-hash' (#1474) into draft
Browse files Browse the repository at this point in the history
* fraccaman/fix-sdk-tx-with-hash:
  sdk: use hash instead of wasm code
  • Loading branch information
juped authored and brentstone committed May 31, 2023
1 parent e286dfc commit 6aa7b67
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 90 deletions.
59 changes: 17 additions & 42 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ 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.to_path_buf(),
}
}
}
Expand Down Expand Up @@ -2195,7 +2195,7 @@ 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.to_path_buf(),
}
}
}
Expand Down Expand Up @@ -2256,15 +2256,8 @@ pub mod args {
TxInitAccount::<SdkTypes> {
tx: self.tx.to_sdk(ctx),
source: ctx.get(&self.source),
vp_code: ctx.read_wasm(self.vp_code),
vp_code_path: self
.vp_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
tx_code_path: ctx.read_wasm(self.tx_code_path),
vp_code_path: self.vp_code_path.to_path_buf(),
tx_code_path: self.tx_code_path.to_path_buf(),
public_key: ctx.get_cached(&self.public_key),
}
}
Expand All @@ -2277,13 +2270,11 @@ pub mod args {
let vp_code_path = CODE_PATH_OPT
.parse(matches)
.unwrap_or_else(|| PathBuf::from(VP_USER_WASM));
let vp_code = vp_code_path.clone();
let tx_code_path = PathBuf::from(TX_INIT_ACCOUNT_WASM);
let public_key = PUBLIC_KEY.parse(matches);
Self {
tx,
source,
vp_code,
vp_code_path,
public_key,
tx_code_path,
Expand Down Expand Up @@ -2320,13 +2311,9 @@ pub mod args {
max_commission_rate_change: self.max_commission_rate_change,
validator_vp_code_path: self
.validator_vp_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
.to_path_buf(),
unsafe_dont_encrypt: self.unsafe_dont_encrypt,
tx_code_path: ctx.read_wasm(self.tx_code_path),
tx_code_path: self.tx_code_path.to_path_buf(),
}
}
}
Expand Down Expand Up @@ -2410,20 +2397,8 @@ pub mod args {
fn to_sdk(self, ctx: &mut Context) -> TxUpdateVp<SdkTypes> {
TxUpdateVp::<SdkTypes> {
tx: self.tx.to_sdk(ctx),
vp_code_path: self
.vp_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
tx_code_path: self
.tx_code_path
.as_path()
.to_str()
.unwrap()
.to_string()
.into_bytes(),
vp_code_path: self.vp_code_path,
tx_code_path: self.tx_code_path,
addr: ctx.get(&self.addr),
}
}
Expand Down Expand Up @@ -2465,7 +2440,7 @@ 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.to_path_buf(),
}
}
}
Expand Down Expand Up @@ -2505,7 +2480,7 @@ 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.to_path_buf(),
}
}
}
Expand Down Expand Up @@ -2552,7 +2527,7 @@ pub mod args {
/// Native token address
pub native_token: C::NativeAddress,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
}

impl CliToSdk<InitProposal<SdkTypes>> for InitProposal<CliTypes> {
Expand All @@ -2562,7 +2537,7 @@ 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,
}
}
}
Expand Down Expand Up @@ -2613,7 +2588,7 @@ pub mod args {
/// The proposal file path
pub proposal_data: Option<PathBuf>,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
}

impl CliToSdk<VoteProposal<SdkTypes>> for VoteProposal<CliTypes> {
Expand All @@ -2624,7 +2599,7 @@ 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.to_path_buf(),
proposal_pgf: self.proposal_pgf,
proposal_eth: self.proposal_eth,
}
Expand Down Expand Up @@ -2849,7 +2824,7 @@ 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.to_path_buf(),
}
}
}
Expand Down Expand Up @@ -3081,7 +3056,7 @@ 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.to_path_buf(),
}
}
}
Expand Down Expand Up @@ -3276,7 +3251,7 @@ pub mod args {
gas_limit: self.gas_limit,
signing_key: self.signing_key.map(|x| ctx.get_cached(&x)),
signer: self.signer.map(|x| ctx.get(&x)),
tx_reveal_code_path: ctx.read_wasm(self.tx_reveal_code_path),
tx_reveal_code_path: self.tx_reveal_code_path,
password: self.password,
expiration: self.expiration,
chain_id: self.chain_id,
Expand Down
20 changes: 13 additions & 7 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ pub async fn submit_init_validator<
.expect("DKG sessions keys should have been created")
.public();

let vp_code_path = String::from_utf8(validator_vp_code_path).unwrap();
let validator_vp_code_hash =
query_wasm_code_hash::<C>(client, vp_code_path)
.await
.unwrap();
let validator_vp_code_hash = query_wasm_code_hash::<C>(
client,
validator_vp_code_path.to_str().unwrap(),
)
.await
.unwrap();

// Validate the commission rate data
if commission_rate > Decimal::ONE || commission_rate < Decimal::ZERO {
Expand Down Expand Up @@ -824,12 +825,17 @@ pub async fn submit_vote_proposal<C: namada::ledger::queries::Client + Sync>(
.try_to_vec()
.expect("Encoding proposal data shouldn't fail");

let tx_code = args.tx_code_path;
let tx_code_hash = query_wasm_code_hash(
client,
args.tx_code_path.to_str().unwrap(),
)
.await
.unwrap();
let mut tx = Tx::new(TxType::Raw);
tx.header.chain_id = chain_id;
tx.header.expiration = 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>(
client,
Expand Down
30 changes: 15 additions & 15 deletions shared/src/ledger/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Structures encapsulating SDK arguments
use std::path::PathBuf;

use namada_core::types::chain::ChainId;
use namada_core::types::time::DateTimeUtc;
use rust_decimal::Decimal;
Expand Down Expand Up @@ -98,7 +100,7 @@ pub struct TxTransfer<C: NamadaTypes = SdkTypes> {
/// Native token address
pub native_token: C::NativeAddress,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
}

/// IBC transfer transaction arguments
Expand All @@ -125,7 +127,7 @@ pub struct TxIbcTransfer<C: NamadaTypes = SdkTypes> {
/// Timeout timestamp offset
pub timeout_sec_offset: Option<u64>,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
}

/// Transaction to initialize a new account
Expand All @@ -135,12 +137,10 @@ pub struct TxInitAccount<C: NamadaTypes = SdkTypes> {
pub tx: Tx<C>,
/// Address of the source account
pub source: C::Address,
/// Wasm VP for the new account
pub vp_code: C::Data,
/// Path to the VP WASM code file for the new account
pub vp_code_path: C::Data,
pub vp_code_path: PathBuf,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
/// Public key for the new account
pub public_key: C::PublicKey,
}
Expand All @@ -165,9 +165,9 @@ pub struct TxInitValidator<C: NamadaTypes = SdkTypes> {
/// Maximum commission rate change
pub max_commission_rate_change: Decimal,
/// Path to the VP WASM code file
pub validator_vp_code_path: C::Data,
pub validator_vp_code_path: PathBuf,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
/// Don't encrypt the keypair
pub unsafe_dont_encrypt: bool,
}
Expand All @@ -178,9 +178,9 @@ pub struct TxUpdateVp<C: NamadaTypes = SdkTypes> {
/// Common tx arguments
pub tx: Tx<C>,
/// Path to the VP WASM code file
pub vp_code_path: C::Data,
pub vp_code_path: PathBuf,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
/// Address of the account whose VP is to be updated
pub addr: C::Address,
}
Expand All @@ -200,7 +200,7 @@ pub struct Bond<C: NamadaTypes = SdkTypes> {
/// Native token address
pub native_token: C::NativeAddress,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
}

/// Unbond arguments
Expand All @@ -216,7 +216,7 @@ pub struct Unbond<C: NamadaTypes = SdkTypes> {
/// self-bonds, the validator is also the source
pub source: Option<C::Address>,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
}

/// Reveal public key
Expand Down Expand Up @@ -255,7 +255,7 @@ pub struct Withdraw<C: NamadaTypes = SdkTypes> {
/// from self-bonds, the validator is also the source
pub source: Option<C::Address>,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
}

/// Query asset conversions
Expand Down Expand Up @@ -327,7 +327,7 @@ pub struct TxCommissionRateChange<C: NamadaTypes = SdkTypes> {
/// Value to which the tx changes the commission rate
pub rate: Decimal,
/// Path to the TX WASM code file
pub tx_code_path: C::Data,
pub tx_code_path: PathBuf,
}

/// Query PoS commission rate
Expand Down Expand Up @@ -411,7 +411,7 @@ pub struct Tx<C: NamadaTypes = SdkTypes> {
/// Sign the tx with the keypair of the public key of the given address
pub signer: Option<C::Address>,
/// Path to the TX WASM code file to reveal PK
pub tx_reveal_code_path: C::Data,
pub tx_reveal_code_path: PathBuf,
/// Password to decrypt key
pub password: Option<Zeroizing<String>>,
}
Expand Down
Loading

0 comments on commit 6aa7b67

Please sign in to comment.