Skip to content

Commit

Permalink
Merge branch 'fraccaman/minor-cli-fixe4s' (#3154)
Browse files Browse the repository at this point in the history
* fraccaman/minor-cli-fixe4s:
  bug fix: handle empty public keys correctly
  some improvements
  added init account, update account and steward commission cli checks
  • Loading branch information
brentstone committed May 1, 2024
2 parents d301e3b + 98d5639 commit 8421354
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
3 changes: 3 additions & 0 deletions crates/governance/src/pgf/cli/steward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ impl Commission {

let mut sum = Dec::zero();
for percentage in self.reward_distribution.values() {
if *percentage < Dec::zero() {
return false;
}
sum = sum.add(percentage);
if sum > Dec::one() {
return false;
Expand Down
3 changes: 3 additions & 0 deletions crates/sdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ pub enum TxSubmitError {
/// Account threshold is not set
#[error("Account threshold must be set.")]
MissingAccountThreshold,
/// Account threshold is not set
#[error("Account threshold is invalid.")]
InvalidAccountThreshold,
/// Not enough signature
#[error("Account threshold is {0} but the valid signatures are {1}.")]
MissingSigningKeys(u8, u8),
Expand Down
68 changes: 61 additions & 7 deletions crates/sdk/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use namada_tx::data::pgf::UpdateStewardCommission;
use namada_tx::data::pos::{BecomeValidator, ConsensusKeyChange};
use namada_tx::data::{pos, ResultCode, TxResult};
pub use namada_tx::{Authorization, *};
use num_traits::Zero;
use rand_core::{OsRng, RngCore};

use crate::args::{self, InputAmount};
Expand Down Expand Up @@ -3136,7 +3137,27 @@ pub async fn build_init_account(
let vp_code_hash = query_wasm_code_hash_buf(context, vp_code_path).await?;

let threshold = match threshold {
Some(threshold) => *threshold,
Some(threshold) => {
let threshold = *threshold;
if (threshold > 0 && public_keys.len() as u8 >= threshold)
|| tx_args.force
{
threshold
} else {
edisplay_line!(
context.io(),
"Invalid account threshold: either the provided threshold \
is zero or the number of public keys is less than the \
threshold."
);
if !tx_args.force {
return Err(Error::from(
TxSubmitError::InvalidAccountThreshold,
));
}
threshold
}
}
None => {
if public_keys.len() == 1 {
1u8
Expand Down Expand Up @@ -3204,18 +3225,51 @@ pub async fn build_update_account(
)
.await?;

let addr = if let Some(account) =
let account = if let Some(account) =
rpc::get_account_info(context.client(), addr).await?
{
account.address
} else if tx_args.force {
addr.clone()
account
} else {
return Err(Error::from(TxSubmitError::LocationDoesNotExist(
addr.clone(),
)));
};

let threshold = if let Some(threshold) = threshold {
let threshold = *threshold;

let invalid_threshold = threshold.is_zero();
let invalid_too_few_pks: bool = (public_keys.is_empty()
&& public_keys.len() < threshold as usize)
|| (account.get_all_public_keys().len() < threshold as usize);

if invalid_threshold || invalid_too_few_pks {
edisplay_line!(
context.io(),
"Invalid account threshold: either the provided threshold is \
zero or the number of public keys is less than the threshold."
);
if !tx_args.force {
return Err(Error::from(
TxSubmitError::InvalidAccountThreshold,
));
}
}

Some(threshold)
} else {
let invalid_too_few_pks = (!public_keys.is_empty()
&& public_keys.len() < account.threshold as usize)
|| (account.get_all_public_keys().len()
< account.threshold as usize);

if invalid_too_few_pks {
return Err(Error::from(TxSubmitError::InvalidAccountThreshold));
}

None
};

let vp_code_hash = match vp_code_path {
Some(code_path) => {
let vp_hash = query_wasm_code_hash_buf(context, code_path).await?;
Expand All @@ -3239,10 +3293,10 @@ pub async fn build_update_account(
);

let data = UpdateAccount {
addr,
addr: account.address,
vp_code_hash: extra_section_hash,
public_keys: public_keys.clone(),
threshold: *threshold,
threshold,
};

let add_code_hash = |tx: &mut Tx, data: &mut UpdateAccount| {
Expand Down

0 comments on commit 8421354

Please sign in to comment.