Skip to content

Commit

Permalink
Merge pull request #3090 from dusk-network/fix_contract_deploy
Browse files Browse the repository at this point in the history
rusk-wallet: Fix contract deploy properly serialize init args
  • Loading branch information
Daksh14 authored Nov 30, 2024
2 parents 0b000c9 + b46ccf9 commit 9e2e712
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
14 changes: 12 additions & 2 deletions rusk-wallet/src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use rusk_wallet::gas::{
Gas, DEFAULT_LIMIT_CALL, DEFAULT_LIMIT_DEPLOYMENT, DEFAULT_LIMIT_TRANSFER,
DEFAULT_PRICE, MIN_PRICE_DEPLOYMENT,
};
use rusk_wallet::{Address, Error, Profile, Wallet, EPOCH, MAX_PROFILES};
use rusk_wallet::{
Address, Error, Profile, Wallet, EPOCH, MAX_CONTRACT_INIT_ARG_SIZE,
MAX_PROFILES,
};
use wallet_core::BalanceInfo;

use crate::io::prompt;
Expand Down Expand Up @@ -241,7 +244,7 @@ pub(crate) enum Command {

/// Arguments for init function
#[arg(short, long)]
init_args: Vec<u8>,
init_args: String,

/// Nonce used for the deploy transaction
#[arg(short, long)]
Expand Down Expand Up @@ -591,6 +594,13 @@ impl Command {
.map_err(|_| Error::InvalidWasmContractPath)?;

let gas = Gas::new(gas_limit).with_price(gas_price);
let init_args = rkyv::to_bytes::<
String,
{ MAX_CONTRACT_INIT_ARG_SIZE },
>(&init_args)
.map_err(|_| Error::Rkyv)?
.to_vec();

let tx = match address {
Address::Shielded(_) => {
wallet.sync().await?;
Expand Down
52 changes: 29 additions & 23 deletions rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,27 @@ pub(crate) async fn run_loop(
if confirm(&cmd, wallet)? {
// run command
prompt::hide_cursor()?;
let result = cmd.run(wallet, settings).await;
let res = cmd.run(wallet, settings).await?;

prompt::show_cursor()?;
// output results
match result {
Ok(res) => {
println!("\r{}", res);
if let RunResult::Tx(hash) = res {
let tx_id = hex::encode(hash.to_bytes());

// Wait for transaction confirmation
// from network
let gql = GraphQL::new(
settings.state.to_string(),
io::status::interactive,
)?;
gql.wait_for(&tx_id).await?;

if let Some(explorer) = &settings.explorer {
let url = format!("{explorer}{tx_id}");
println!("> URL: {url}");
prompt::launch_explorer(url)?;
}
}
println!("\r{}", res);
if let RunResult::Tx(hash) = res {
let tx_id = hex::encode(hash.to_bytes());

// Wait for transaction confirmation
// from network
let gql = GraphQL::new(
settings.state.to_string(),
io::status::interactive,
)?;
gql.wait_for(&tx_id).await?;

if let Some(explorer) = &settings.explorer {
let url = format!("{explorer}{tx_id}");
println!("> URL: {url}");
prompt::launch_explorer(url)?;
}

Err(err) => return Err(err),
}
}
}
Expand Down Expand Up @@ -442,14 +437,25 @@ fn confirm(cmd: &Command, wallet: &Wallet<WalletFile>) -> anyhow::Result<bool> {
gas_price,
} => {
let sender = address.as_ref().ok_or(Error::BadAddress)?;
let sender_index = wallet.find_index(sender)?;
let code_len = code.metadata()?.len();
let max_fee = gas_limit * gas_price;
let code_bytes = std::fs::read(code)?;

let contract_id = wallet.get_contract_id(
sender_index,
code_bytes,
*deploy_nonce,
)?;

let contract_id = hex::encode(contract_id);

println!(" > Pay with {}", sender.preview());
println!(" > Code len = {}", code_len);
println!(" > Init args = {}", hex::encode(init_args));
println!(" > Deploy nonce = {}", deploy_nonce);
println!(" > Max fee = {} DUSK", Dusk::from(max_fee));
println!(" > Calculated Contract Id = {}", contract_id);
if let Address::Public(_) = sender {
println!(" > ALERT: THIS IS A PUBLIC TRANSACTION");
}
Expand Down
9 changes: 7 additions & 2 deletions rusk-wallet/src/bin/interactive/command_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use rusk_wallet::gas::{
self, DEFAULT_LIMIT_CALL, DEFAULT_LIMIT_STAKE, DEFAULT_LIMIT_TRANSFER,
DEFAULT_PRICE, GAS_PER_DEPLOY_BYTE, MIN_PRICE_DEPLOYMENT,
};
use rusk_wallet::{Address, Wallet, MAX_FUNCTION_NAME_SIZE};
use rusk_wallet::{
Address, Wallet, MAX_CONTRACT_INIT_ARG_SIZE, MAX_FUNCTION_NAME_SIZE,
};

use super::ProfileOp;
use crate::settings::Settings;
Expand Down Expand Up @@ -263,7 +265,10 @@ pub(crate) async fn online(
ProfileOp::Run(Box::new(Command::ContractDeploy {
address: Some(addr),
code,
init_args: prompt::request_bytes("init arguments")?,
init_args: prompt::request_str(
"init arguments",
MAX_CONTRACT_INIT_ARG_SIZE,
)?,
deploy_nonce: prompt::request_nonce()?,
gas_limit: prompt::request_gas_limit(gas_limit)?,
gas_price,
Expand Down
38 changes: 29 additions & 9 deletions rusk-wallet/src/bin/io/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::fmt::Display;
use std::io::stdout;
use std::path::PathBuf;
use std::println;
use std::str::FromStr;
use std::{io::stdout, println};

use crossterm::{
cursor::{Hide, Show},
ExecutableCommand,
};

use anyhow::Result;
use bip39::{ErrorKind, Language, Mnemonic};
use crossterm::cursor::{Hide, Show};
use crossterm::ExecutableCommand;
use execution_core::stake::MINIMUM_STAKE;

use inquire::ui::RenderConfig;
use inquire::validator::Validation;
use inquire::{
Confirm, CustomType, InquireError, Password, PasswordDisplayMode, Select,
Text,
};
use rusk_wallet::currency::{Dusk, Lux};
use rusk_wallet::dat::DatFileVersion;
use rusk_wallet::gas::{self, MempoolGasPrices};
use rusk_wallet::{Address, Error, MAX_CONVERTIBLE, MIN_CONVERTIBLE};
use rusk_wallet::{
currency::{Dusk, Lux},
dat::DatFileVersion,
gas::{self, MempoolGasPrices},
Address, Error, MAX_CONVERTIBLE, MIN_CONVERTIBLE,
};
use sha2::{Digest, Sha256};

pub(crate) fn ask_pwd(msg: &str) -> Result<String, InquireError> {
Expand Down Expand Up @@ -351,7 +356,22 @@ pub(crate) fn request_transaction_model() -> anyhow::Result<TransactionModel> {

/// Request contract WASM file location
pub(crate) fn request_contract_code() -> anyhow::Result<PathBuf> {
request_dir("Location of the WASM contract", PathBuf::new())
let validator = |path_str: &str| {
let path = PathBuf::from(path_str);
if path.extension().map_or(false, |ext| ext == "wasm") {
Ok(Validation::Valid)
} else {
Ok(Validation::Invalid("Not a valid directory".into()))
}
};

let q = Text::new("Please Enter location of the WASM contract:")
.with_validator(validator)
.prompt()?;

let p = PathBuf::from(q);

Ok(p)
}

pub(crate) fn request_bytes(name: &str) -> anyhow::Result<Vec<u8>> {
Expand Down
2 changes: 2 additions & 0 deletions rusk-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ use currency::Dusk;

/// The maximum allowed size for function names, set to 64 bytes
pub const MAX_FUNCTION_NAME_SIZE: usize = 64;
/// Size for the init argument when deploying a contract
pub const MAX_CONTRACT_INIT_ARG_SIZE: usize = 128;
/// The largest amount of Dusk that is possible to convert
pub const MAX_CONVERTIBLE: Dusk = Dusk::MAX;
/// The smallest amount of Dusk that is possible to convert
Expand Down

0 comments on commit 9e2e712

Please sign in to comment.