Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More client side checks #3154

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -287,6 +287,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 @@ -3150,7 +3151,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 @@ -3218,18 +3239,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: bool = (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 @@ -3253,10 +3307,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
Loading