Skip to content

Commit

Permalink
[CLI] Add --gas-limit option (#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
vegetabledogdog authored Apr 30, 2024
1 parent 997001e commit b710b27
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
9 changes: 6 additions & 3 deletions crates/rooch-rpc-client/src/wallet_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl WalletContext {
&self,
sender: RoochAddress,
action: MoveAction,
max_gas_amount: Option<u64>,
) -> RoochResult<RoochTransactionData> {
let client = self.get_client().await?;
let chain_id = client.rooch.get_chain_id().await?;
Expand All @@ -156,7 +157,7 @@ impl WalletContext {
sender,
sequence_number,
chain_id,
GasConfig::DEFAULT_MAX_GAS_AMOUNT,
max_gas_amount.unwrap_or(GasConfig::DEFAULT_MAX_GAS_AMOUNT),
action,
);
Ok(tx_data)
Expand All @@ -167,6 +168,7 @@ impl WalletContext {
sender: RoochAddress,
action: MoveAction,
password: Option<String>,
max_gas_amount: Option<u64>,
) -> RoochResult<RoochTransaction> {
let kp = self
.keystore
Expand All @@ -178,7 +180,7 @@ impl WalletContext {
))
})?;

let tx_data = self.build_tx_data(sender, action).await?;
let tx_data = self.build_tx_data(sender, action, max_gas_amount).await?;
let signature = Signature::new_hashed(tx_data.tx_hash().as_bytes(), &kp);
Ok(RoochTransaction::new(
tx_data,
Expand All @@ -203,8 +205,9 @@ impl WalletContext {
sender: RoochAddress,
action: MoveAction,
password: Option<String>,
max_gas_amount: Option<u64>,
) -> RoochResult<ExecuteTransactionResponseView> {
let tx = self.sign(sender, action, password).await?;
let tx = self.sign(sender, action, password, max_gas_amount).await?;
self.execute(tx).await
}

Expand Down
5 changes: 5 additions & 0 deletions crates/rooch/src/cli_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ pub struct TransactionOptions {
#[clap(long, alias = "sender-account", value_parser=ParsedAddress::parse, default_value = "default")]
pub(crate) sender: ParsedAddress,

/// Custom the transaction's gas limit.
/// [default: 1_000_000_000] [alias: "gas-limit"]
#[clap(long, alias = "gas-limit")]
pub(crate) max_gas_amount: Option<u64>,

/// Custom the transaction's authenticator
/// format: `auth_validator_id:payload`, auth validator id is u64, payload is hex string
/// example: 123:0x2abc
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch/src/commands/account/commands/nullify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl CommandAction<ExecuteTransactionResponseView> for NullifyCommand {
// Execute the Move call as a transaction
let mut result = if context.keystore.get_if_password_is_empty() {
context
.sign_and_execute(existing_address, action, None)
.sign_and_execute(existing_address, action, None, None)
.await?
} else {
let password =
Expand All @@ -64,7 +64,7 @@ impl CommandAction<ExecuteTransactionResponseView> for NullifyCommand {
}

context
.sign_and_execute(existing_address, action, Some(password))
.sign_and_execute(existing_address, action, Some(password), None)
.await?
};
result = context.assert_execute_success(result)?;
Expand Down
13 changes: 9 additions & 4 deletions crates/rooch/src/commands/move_cli/commands/framework_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,23 @@ impl CommandAction<ExecuteTransactionResponseView> for FrameworkUpgrade {
);

// Build context and handle errors

let sender = context.resolve_address(self.tx_options.sender)?.into();
let max_gas_amount: Option<u64> = self.tx_options.max_gas_amount;

// Handle transaction with or without authenticator
match self.tx_options.authenticator {
Some(authenticator) => {
let tx_data = context.build_tx_data(sender, action).await?;
let tx_data = context
.build_tx_data(sender, action, max_gas_amount)
.await?;
let tx = RoochTransaction::new(tx_data, authenticator.into());
context.execute(tx).await
}
None => {
if context.keystore.get_if_password_is_empty() {
context.sign_and_execute(sender, action, None).await
context
.sign_and_execute(sender, action, None, max_gas_amount)
.await
} else {
let password =
prompt_password("Enter the password to publish:").unwrap_or_default();
Expand All @@ -97,7 +102,7 @@ impl CommandAction<ExecuteTransactionResponseView> for FrameworkUpgrade {
}

context
.sign_and_execute(sender, action, Some(password))
.sign_and_execute(sender, action, Some(password), max_gas_amount)
.await
}
}
Expand Down
18 changes: 13 additions & 5 deletions crates/rooch/src/commands/move_cli/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ impl CommandAction<ExecuteTransactionResponseView> for Publish {
let sender: RoochAddress = pkg_address.into();
eprintln!("Publish modules to address: {:?}", sender);

let max_gas_amount: Option<u64> = self.tx_options.max_gas_amount;

// Prepare and execute the transaction based on the action type
let tx_result = if !self.by_move_action {
let args = bcs::to_bytes(&bundles).unwrap();
Expand All @@ -137,13 +139,17 @@ impl CommandAction<ExecuteTransactionResponseView> for Publish {
// Handle transaction with or without authenticator
match self.tx_options.authenticator {
Some(authenticator) => {
let tx_data = context.build_tx_data(sender, action).await?;
let tx_data = context
.build_tx_data(sender, action, max_gas_amount)
.await?;
let tx = RoochTransaction::new(tx_data, authenticator.into());
context.execute(tx).await?
}
None => {
if context.keystore.get_if_password_is_empty() {
context.sign_and_execute(sender, action, None).await?
context
.sign_and_execute(sender, action, None, max_gas_amount)
.await?
} else {
let password =
prompt_password("Enter the password to publish:").unwrap_or_default();
Expand All @@ -159,7 +165,7 @@ impl CommandAction<ExecuteTransactionResponseView> for Publish {
}

context
.sign_and_execute(sender, action, Some(password))
.sign_and_execute(sender, action, Some(password), max_gas_amount)
.await?
}
}
Expand All @@ -169,7 +175,9 @@ impl CommandAction<ExecuteTransactionResponseView> for Publish {
let action = MoveAction::ModuleBundle(bundles);

if context.keystore.get_if_password_is_empty() {
context.sign_and_execute(sender, action, None).await?
context
.sign_and_execute(sender, action, None, max_gas_amount)
.await?
} else {
let password =
prompt_password("Enter the password to publish:").unwrap_or_default();
Expand All @@ -183,7 +191,7 @@ impl CommandAction<ExecuteTransactionResponseView> for Publish {
}

context
.sign_and_execute(sender, action, Some(password))
.sign_and_execute(sender, action, Some(password), max_gas_amount)
.await?
}
};
Expand Down
15 changes: 11 additions & 4 deletions crates/rooch/src/commands/move_cli/commands/run_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {
let context = self.context.build()?;
let address_mapping = context.address_mapping();
let sender: RoochAddress = context.resolve_address(self.tx_options.sender)?.into();
let max_gas_amount: Option<u64> = self.tx_options.max_gas_amount;
let function_id = self.function.into_function_id(&address_mapping)?;
let args = self
.args
Expand All @@ -78,14 +79,18 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {
let action = MoveAction::new_function_call(function_id, type_args, args);
match (self.tx_options.authenticator, self.tx_options.session_key) {
(Some(authenticator), _) => {
let tx_data = context.build_tx_data(sender, action).await?;
let tx_data = context
.build_tx_data(sender, action, max_gas_amount)
.await?;
//TODO the authenticator usually is associalted with the RoochTransactinData
//So we need to find a way to let user generate the authenticator based on the tx_data.
let tx = RoochTransaction::new(tx_data, authenticator.into());
context.execute(tx).await
}
(_, Some(session_key)) => {
let tx_data = context.build_tx_data(sender, action).await?;
let tx_data = context
.build_tx_data(sender, action, max_gas_amount)
.await?;
let tx = if context.keystore.get_if_password_is_empty() {
context
.keystore
Expand Down Expand Up @@ -119,7 +124,9 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {
}
(None, None) => {
if context.keystore.get_if_password_is_empty() {
context.sign_and_execute(sender, action, None).await
context
.sign_and_execute(sender, action, None, max_gas_amount)
.await
} else {
let password =
prompt_password("Enter the password to run functions:").unwrap_or_default();
Expand All @@ -135,7 +142,7 @@ impl CommandAction<ExecuteTransactionResponseView> for RunFunction {
}

context
.sign_and_execute(sender, action, Some(password))
.sign_and_execute(sender, action, Some(password), max_gas_amount)
.await
}
}
Expand Down
7 changes: 5 additions & 2 deletions crates/rooch/src/commands/session_key/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl CreateCommand {
let mut context = self.context_options.build()?;

let sender: RoochAddress = context.resolve_address(self.tx_options.sender)?.into();
let max_gas_amount: Option<u64> = self.tx_options.max_gas_amount;

let session_auth_key = if context.keystore.get_if_password_is_empty() {
context.keystore.generate_session_key(&sender, None)?
Expand Down Expand Up @@ -79,7 +80,9 @@ impl CreateCommand {
println!("Generated new session key {session_auth_key} for address [{sender}]",);

let result = if context.keystore.get_if_password_is_empty() {
context.sign_and_execute(sender, action, None).await?
context
.sign_and_execute(sender, action, None, max_gas_amount)
.await?
} else {
let password =
prompt_password("Enter the password to create a new key pair:").unwrap_or_default();
Expand All @@ -93,7 +96,7 @@ impl CreateCommand {
}

context
.sign_and_execute(sender, action, Some(password))
.sign_and_execute(sender, action, Some(password), max_gas_amount)
.await?
};
context.assert_execute_success(result)?;
Expand Down
2 changes: 1 addition & 1 deletion moveos/moveos-types/src/gas_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub struct GasConfig {
}

impl GasConfig {
pub const DEFAULT_MAX_GAS_AMOUNT: u64 = 1000000000u64;
pub const DEFAULT_MAX_GAS_AMOUNT: u64 = 1_000_000_000u64;
}

0 comments on commit b710b27

Please sign in to comment.