Skip to content

Commit

Permalink
Move tx::Error to error::TxError and Add it to the error::Error type
Browse files Browse the repository at this point in the history
We move the error type found within tx to the error module, renaming
it to TxError.

Further we add it to the error type found within, and refactor all
references to tx::Error into error:Error, promoting the tx errors into
the greater error type we wish to use
  • Loading branch information
mariari committed Aug 15, 2023
1 parent 72a31c3 commit 7f01b4f
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 259 deletions.
67 changes: 36 additions & 31 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use namada::proof_of_stake::parameters::PosParams;
use namada::proto::Tx;
use namada::types::address::{Address, ImplicitAddress};
use namada::types::dec::Dec;
use namada::types::error;
use namada::types::key::{self, *};
use namada::types::transaction::pos::InitValidator;

Expand All @@ -39,7 +40,7 @@ pub async fn submit_reveal_aux<C: namada::ledger::queries::Client + Sync>(
ctx: &mut Context,
args: args::Tx,
address: &Address,
) -> Result<(), tx::Error> {
) -> Result<(), error::Error> {
if args.dump_tx {
return Ok(());
}
Expand All @@ -48,7 +49,7 @@ pub async fn submit_reveal_aux<C: namada::ledger::queries::Client + Sync>(
let key = ctx
.wallet
.find_key_by_pkh(pkh, args.clone().password)
.map_err(|e| tx::Error::Other(e.to_string()))?;
.map_err(|e| error::Error::Other(e.to_string()))?;
let public_key = key.ref_to();

if tx::is_reveal_pk_needed::<C>(client, address, args.force).await? {
Expand Down Expand Up @@ -85,7 +86,7 @@ pub async fn submit_custom<C>(
client: &C,
ctx: &mut Context,
args: args::TxCustom,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -121,7 +122,7 @@ pub async fn submit_update_account<C>(
client: &C,
ctx: &mut Context,
args: args::TxUpdateAccount,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -156,7 +157,7 @@ pub async fn submit_init_account<C>(
client: &C,
ctx: &mut Context,
args: args::TxInitAccount,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -204,7 +205,7 @@ pub async fn submit_init_validator<C>(
unsafe_dont_encrypt,
tx_code_path: _,
}: args::TxInitValidator,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -605,7 +606,7 @@ pub async fn submit_transfer<C: Client + Sync>(
client: &C,
mut ctx: Context,
args: args::TxTransfer,
) -> Result<(), tx::Error> {
) -> Result<(), error::Error> {
for _ in 0..2 {
let default_signer = Some(args.source.effective_address());
let signing_data = signing::aux_signing_data(
Expand Down Expand Up @@ -674,7 +675,7 @@ pub async fn submit_ibc_transfer<C>(
client: &C,
mut ctx: Context,
args: args::TxIbcTransfer,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -710,7 +711,7 @@ pub async fn submit_init_proposal<C>(
client: &C,
mut ctx: Context,
args: args::InitProposal,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand All @@ -719,8 +720,8 @@ where
let governance_parameters = rpc::query_governance_parameters(client).await;

let (mut tx_builder, signing_data) = if args.is_offline {
let proposal = namada::core::ledger::governance::cli::offline::OfflineProposal::try_from(args.proposal_data.as_ref()).map_err(|e| tx::Error::FailedGovernaneProposalDeserialize(e.to_string()))?.validate(current_epoch)
.map_err(|e| tx::Error::InvalidProposal(e.to_string()))?;
let proposal = namada::core::ledger::governance::cli::offline::OfflineProposal::try_from(args.proposal_data.as_ref()).map_err(|e| error::TxError::FailedGovernaneProposalDeserialize(e.to_string()))?.validate(current_epoch)
.map_err(|e| error::TxError::InvalidProposal(e.to_string()))?;

let default_signer = Some(proposal.author.clone());
let signing_data = signing::aux_signing_data(
Expand All @@ -739,7 +740,9 @@ where
let output_file_path = signed_offline_proposal
.serialize(args.tx.output_folder)
.map_err(|e| {
tx::Error::FailedGovernaneProposalDeserialize(e.to_string())
error::TxError::FailedGovernaneProposalDeserialize(
e.to_string(),
)
})?;

println!("Proposal serialized to: {}", output_file_path);
Expand All @@ -748,10 +751,12 @@ where
let proposal =
PgfFundingProposal::try_from(args.proposal_data.as_ref())
.map_err(|e| {
tx::Error::FailedGovernaneProposalDeserialize(e.to_string())
error::TxError::FailedGovernaneProposalDeserialize(
e.to_string(),
)
})?
.validate(&governance_parameters, current_epoch)
.map_err(|e| tx::Error::InvalidProposal(e.to_string()))?;
.map_err(|e| error::TxError::InvalidProposal(e.to_string()))?;

let default_signer = Some(proposal.proposal.author.clone());
let signing_data = signing::aux_signing_data(
Expand Down Expand Up @@ -786,7 +791,7 @@ where
args.proposal_data.as_ref(),
)
.map_err(|e| {
tx::Error::FailedGovernaneProposalDeserialize(e.to_string())
error::TxError::FailedGovernaneProposalDeserialize(e.to_string())
})?;
let author_balane = rpc::get_token_balance(
client,
Expand All @@ -796,7 +801,7 @@ where
.await;
let proposal = proposal
.validate(&governance_parameters, current_epoch, author_balane)
.map_err(|e| tx::Error::InvalidProposal(e.to_string()))?;
.map_err(|e| error::TxError::InvalidProposal(e.to_string()))?;

let default_signer = Some(proposal.proposal.author.clone());
let signing_data = signing::aux_signing_data(
Expand Down Expand Up @@ -829,7 +834,7 @@ where
} else {
let proposal = DefaultProposal::try_from(args.proposal_data.as_ref())
.map_err(|e| {
tx::Error::FailedGovernaneProposalDeserialize(e.to_string())
error::TxError::FailedGovernaneProposalDeserialize(e.to_string())
})?;
let author_balane = rpc::get_token_balance(
client,
Expand All @@ -839,7 +844,7 @@ where
.await;
let proposal = proposal
.validate(&governance_parameters, current_epoch, author_balane)
.map_err(|e| tx::Error::InvalidProposal(e.to_string()))?;
.map_err(|e| error::TxError::InvalidProposal(e.to_string()))?;

let default_signer = Some(proposal.proposal.author.clone());
let signing_data = signing::aux_signing_data(
Expand Down Expand Up @@ -890,7 +895,7 @@ pub async fn submit_vote_proposal<C>(
client: &C,
mut ctx: Context,
args: args::VoteProposal,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand All @@ -909,17 +914,17 @@ where

let mut tx_builder = if args.is_offline {
let proposal_vote = ProposalVote::try_from(args.vote)
.map_err(|_| tx::Error::InvalidProposalVote)?;
.map_err(|_| error::TxError::InvalidProposalVote)?;

let proposal = OfflineSignedProposal::try_from(
args.proposal_data.clone().unwrap().as_ref(),
)
.map_err(|e| tx::Error::InvalidProposal(e.to_string()))?
.map_err(|e| error::TxError::InvalidProposal(e.to_string()))?
.validate(
&signing_data.account_public_keys_map.clone().unwrap(),
signing_data.threshold,
)
.map_err(|e| tx::Error::InvalidProposal(e.to_string()))?;
.map_err(|e| error::TxError::InvalidProposal(e.to_string()))?;
let delegations = rpc::get_delegators_delegation_at(
client,
&args.voter,
Expand Down Expand Up @@ -980,7 +985,7 @@ pub async fn sign_tx<C>(
tx_data,
owner,
}: args::SignTx,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -1061,7 +1066,7 @@ pub async fn submit_reveal_pk<C>(
client: &C,
ctx: &mut Context,
args: args::RevealPk,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand All @@ -1075,7 +1080,7 @@ pub async fn submit_bond<C>(
client: &C,
ctx: &mut Context,
args: args::Bond,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -1113,7 +1118,7 @@ pub async fn submit_unbond<C>(
client: &C,
ctx: &mut Context,
args: args::Unbond,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -1155,7 +1160,7 @@ pub async fn submit_withdraw<C>(
client: &C,
mut ctx: Context,
args: args::Withdraw,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -1191,7 +1196,7 @@ pub async fn submit_validator_commission_change<C>(
client: &C,
mut ctx: Context,
args: args::CommissionRateChange,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -1231,7 +1236,7 @@ pub async fn submit_unjail_validator<
client: &C,
mut ctx: Context,
args: args::TxUnjailValidator,
) -> Result<(), tx::Error>
) -> Result<(), error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down Expand Up @@ -1281,7 +1286,7 @@ pub async fn save_initialized_accounts<U: WalletUtils>(
pub async fn broadcast_tx<C>(
rpc_cli: &C,
to_broadcast: &TxBroadcastData,
) -> Result<Response, tx::Error>
) -> Result<Response, error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand All @@ -1300,7 +1305,7 @@ where
pub async fn submit_tx<C>(
client: &C,
to_broadcast: TxBroadcastData,
) -> Result<TxResponse, tx::Error>
) -> Result<TxResponse, error::Error>
where
C: namada::ledger::queries::Client + Sync,
C::Error: std::fmt::Display,
Expand Down
3 changes: 2 additions & 1 deletion shared/src/ledger/eth_bridge/bridge_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ use crate::eth_bridge::structs::RelayProof;
use crate::ledger::args;
use crate::ledger::queries::{Client, RPC};
use crate::ledger::rpc::{query_wasm_code_hash, validate_amount};
use crate::ledger::tx::{prepare_tx, Error};
use crate::ledger::tx::prepare_tx;
use crate::proto::Tx;
use crate::types::address::Address;
use crate::types::control_flow::time::{Duration, Instant};
use crate::types::control_flow::{
self, install_shutdown_signal, Halt, TryHalt,
};
use crate::types::error::Error;
use crate::types::eth_abi::Encode;
use crate::types::eth_bridge_pool::{
GasFee, PendingTransfer, TransferToEthereum,
Expand Down
20 changes: 12 additions & 8 deletions shared/src/ledger/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use namada_core::types::account::AccountPublicKeysMap;
use namada_core::types::address::{
masp, masp_tx_key, Address, ImplicitAddress,
};
use namada_core::types::token;
// use namada_core::types::storage::Key;
use namada_core::types::token::{self, Amount, DenominatedAmount, MaspDenom};
use namada_core::types::token::{Amount, DenominatedAmount, MaspDenom};
use namada_core::types::transaction::pos;
use prost::Message;
use serde::{Deserialize, Serialize};
Expand All @@ -29,15 +30,16 @@ use crate::ledger::masp::make_asset_type;
use crate::ledger::parameters::storage as parameter_storage;
use crate::ledger::rpc::{format_denominated_amount, query_wasm_code_hash};
use crate::ledger::tx::{
Error, TX_BOND_WASM, TX_CHANGE_COMMISSION_WASM, TX_IBC_WASM,
TX_INIT_ACCOUNT_WASM, TX_INIT_PROPOSAL, TX_INIT_VALIDATOR_WASM,
TX_REVEAL_PK, TX_TRANSFER_WASM, TX_UNBOND_WASM, TX_UPDATE_ACCOUNT_WASM,
TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM, VP_USER_WASM,
TX_BOND_WASM, TX_CHANGE_COMMISSION_WASM, TX_IBC_WASM, TX_INIT_ACCOUNT_WASM,
TX_INIT_PROPOSAL, TX_INIT_VALIDATOR_WASM, TX_REVEAL_PK, TX_TRANSFER_WASM,
TX_UNBOND_WASM, TX_UPDATE_ACCOUNT_WASM, TX_VOTE_PROPOSAL, TX_WITHDRAW_WASM,
VP_USER_WASM,
};
pub use crate::ledger::wallet::store::AddressVpType;
use crate::ledger::wallet::{Wallet, WalletUtils};
use crate::ledger::{args, rpc};
use crate::proto::{MaspBuilder, Section, Tx};
use crate::types::error::{Error, TxError};
use crate::types::key::*;
use crate::types::masp::{ExtendedViewingKey, PaymentAddress};
use crate::types::storage::Epoch;
Expand Down Expand Up @@ -243,22 +245,24 @@ pub async fn aux_signing_data<
if let Some(account) = account {
(Some(account.public_keys_map), account.threshold)
} else {
return Err(Error::InvalidAccount(owner.encode()));
return Err(Error::from(TxError::InvalidAccount(
owner.encode(),
)));
}
}
Some(Address::Implicit(_)) => (
Some(AccountPublicKeysMap::from_iter(public_keys.clone())),
1u8,
),
Some(owner @ Address::Internal(_)) => {
return Err(Error::InvalidAccount(owner.encode()));
return Err(Error::from(TxError::InvalidAccount(owner.encode())));
}
None => (None, 0u8),
};

let gas_payer = match &args.gas_payer {
Some(keypair) => keypair.ref_to(),
None => public_keys.get(0).ok_or(Error::InvalidFeePayer)?.clone(),
None => public_keys.get(0).ok_or(TxError::InvalidFeePayer)?.clone(),
};

Ok(SigningTxData {
Expand Down
Loading

0 comments on commit 7f01b4f

Please sign in to comment.