Skip to content

Commit

Permalink
wasm/tx: insert_verifier from txs where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Mar 20, 2024
1 parent 1aaeb67 commit 2550d2d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/tx_prelude/src/proof_of_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl Ctx {
validator: &Address,
amount: token::Amount,
) -> TxResult {
// The tx must be authorized by the source address
let verifier = source.as_ref().unwrap_or(&validator);
self.insert_verifier(verifier)?;

Check warning on line 31 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L29-L31

Added lines #L29 - L31 were not covered by tests

let current_epoch = self.get_block_epoch()?;
bond_tokens(self, source, validator, amount, current_epoch, None)
}
Expand All @@ -39,6 +43,10 @@ impl Ctx {
validator: &Address,
amount: token::Amount,
) -> EnvResult<ResultSlashing> {
// The tx must be authorized by the source address
let verifier = source.as_ref().unwrap_or(&validator);
self.insert_verifier(verifier)?;

Check warning on line 48 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L46-L48

Added lines #L46 - L48 were not covered by tests

let current_epoch = self.get_block_epoch()?;
unbond_tokens(self, source, validator, amount, current_epoch, false)
}
Expand All @@ -51,6 +59,10 @@ impl Ctx {
source: Option<&Address>,
validator: &Address,
) -> EnvResult<token::Amount> {
// The tx must be authorized by the source address
let verifier = source.as_ref().unwrap_or(&validator);
self.insert_verifier(verifier)?;

Check warning on line 64 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L62-L64

Added lines #L62 - L64 were not covered by tests

let current_epoch = self.get_block_epoch()?;
withdraw_tokens(self, source, validator, current_epoch)
}
Expand All @@ -61,6 +73,9 @@ impl Ctx {
validator: &Address,
consensus_key: &common::PublicKey,
) -> TxResult {
// The tx must be authorized by the source address
self.insert_verifier(validator)?;

Check warning on line 77 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L76-L77

Added lines #L76 - L77 were not covered by tests

let current_epoch = self.get_block_epoch()?;
change_consensus_key(self, validator, consensus_key, current_epoch)
}
Expand All @@ -71,12 +86,18 @@ impl Ctx {
validator: &Address,
rate: &Dec,
) -> TxResult {
// The tx must be authorized by the source address
self.insert_verifier(validator)?;

Check warning on line 90 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L89-L90

Added lines #L89 - L90 were not covered by tests

let current_epoch = self.get_block_epoch()?;
change_validator_commission_rate(self, validator, *rate, current_epoch)
}

/// Unjail a jailed validator and re-enter the validator sets.
pub fn unjail_validator(&mut self, validator: &Address) -> TxResult {
// The tx must be authorized by the source address
self.insert_verifier(validator)?;

Check warning on line 99 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L98-L99

Added lines #L98 - L99 were not covered by tests

let current_epoch = self.get_block_epoch()?;
unjail_validator(self, validator, current_epoch)
}
Expand All @@ -89,6 +110,9 @@ impl Ctx {
dest_validator: &Address,
amount: token::Amount,
) -> TxResult {
// The tx must be authorized by the source address
self.insert_verifier(owner)?;

Check warning on line 114 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L113-L114

Added lines #L113 - L114 were not covered by tests

let current_epoch = self.get_block_epoch()?;
redelegate_tokens(
self,
Expand All @@ -106,6 +130,10 @@ impl Ctx {
source: Option<&Address>,
validator: &Address,
) -> EnvResult<token::Amount> {
// The tx must be authorized by the source address
let verifier = source.as_ref().unwrap_or(&validator);
self.insert_verifier(verifier)?;

Check warning on line 135 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L133-L135

Added lines #L133 - L135 were not covered by tests

let current_epoch = self.get_block_epoch()?;
claim_reward_tokens(self, source, validator, current_epoch)
}
Expand Down Expand Up @@ -134,6 +162,9 @@ impl Ctx {
let eth_hot_key = key::common::PublicKey::Secp256k1(eth_hot_key);
let params = read_pos_params(self)?;

// The tx must be authorized by the source address
self.insert_verifier(&address)?;

Check warning on line 166 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L166

Added line #L166 was not covered by tests

become_validator(
self,
namada_proof_of_stake::BecomeValidator {
Expand Down Expand Up @@ -162,12 +193,18 @@ impl Ctx {

/// Deactivate validator
pub fn deactivate_validator(&mut self, validator: &Address) -> TxResult {
// The tx must be authorized by the source address
self.insert_verifier(validator)?;

Check warning on line 197 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L196-L197

Added lines #L196 - L197 were not covered by tests

let current_epoch = self.get_block_epoch()?;
deactivate_validator(self, validator, current_epoch)
}

/// Reactivate validator
pub fn reactivate_validator(&mut self, validator: &Address) -> TxResult {
// The tx must be authorized by the source address
self.insert_verifier(validator)?;

Check warning on line 206 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L205-L206

Added lines #L205 - L206 were not covered by tests

let current_epoch = self.get_block_epoch()?;
reactivate_validator(self, validator, current_epoch)
}
Expand All @@ -184,6 +221,9 @@ impl Ctx {
avatar: Option<String>,
commission_rate: Option<Dec>,
) -> TxResult {
// The tx must be authorized by the source address
self.insert_verifier(validator)?;

Check warning on line 225 in crates/tx_prelude/src/proof_of_stake.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/proof_of_stake.rs#L224-L225

Added lines #L224 - L225 were not covered by tests

let current_epoch = self.get_block_epoch()?;
change_validator_metadata(
self,
Expand Down
7 changes: 7 additions & 0 deletions crates/tx_prelude/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use namada_proof_of_stake::token::storage_key::{
};
use namada_storage::{Error as StorageError, ResultExt};
pub use namada_token::*;
use namada_tx_env::TxEnv;

use crate::{Ctx, StorageRead, StorageWrite, TxResult};

Expand All @@ -16,6 +17,9 @@ pub fn transfer(
token: &Address,
amount: DenominatedAmount,
) -> TxResult {
// The tx must be authorized by the source address
ctx.insert_verifier(src)?;

let amount = denom_to_amount(amount, token, ctx)?;
if amount != Amount::default() && src != dest {
let src_key = balance_key(token, src);
Expand All @@ -41,6 +45,9 @@ pub fn undenominated_transfer(
token: &Address,
amount: Amount,
) -> TxResult {
// The tx must be authorized by the source address
ctx.insert_verifier(src)?;

Check warning on line 49 in crates/tx_prelude/src/token.rs

View check run for this annotation

Codecov / codecov/patch

crates/tx_prelude/src/token.rs#L48-L49

Added lines #L48 - L49 were not covered by tests

if amount != Amount::default() && src != dest {
let src_key = balance_key(token, src);
let dest_key = balance_key(token, dest);
Expand Down
3 changes: 3 additions & 0 deletions wasm/wasm_source/src/tx_init_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ fn apply_tx(ctx: &mut Ctx, tx: Tx) -> TxResult {
let tx_data = governance::InitProposalData::try_from_slice(&data[..])
.wrap_err("failed to decode InitProposalData")?;

// The tx must be authorized by the author address
ctx.insert_verifier(&tx_data.author)?;

// Get the content from the referred to section
let content = tx
.get_section(&tx_data.content)
Expand Down
3 changes: 3 additions & 0 deletions wasm/wasm_source/src/tx_resign_steward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ fn apply_tx(ctx: &mut Ctx, tx_data: Tx) -> TxResult {
let steward_address = Address::try_from_slice(&data[..])
.wrap_err("failed to decode an Address")?;

// The tx must be authorized by the source address
ctx.insert_verifier(&steward_address)?;

pgf::remove_steward(ctx, &steward_address)?;

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions wasm/wasm_source/src/tx_update_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ fn apply_tx(ctx: &mut Ctx, tx: Tx) -> TxResult {
let owner = &tx_data.addr;
debug_log!("update VP for: {:#?}", tx_data.addr);

// The tx must be authorized by the source address
ctx.insert_verifier(owner)?;

if let Some(hash) = tx_data.vp_code_hash {
let vp_code_sec = signed
.get_section(&hash)
Expand Down
3 changes: 3 additions & 0 deletions wasm/wasm_source/src/tx_update_steward_commission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ fn apply_tx(ctx: &mut Ctx, tx_data: Tx) -> TxResult {
let steward_commission = UpdateStewardCommission::try_from_slice(&data[..])
.wrap_err("failed to decode an UpdateStewardCommission")?;

// The tx must be authorized by the source address
ctx.insert_verifier(&steward_commission.steward)?;

pgf::update_steward_commission(ctx, steward_commission)?;

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions wasm/wasm_source/src/tx_vote_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ fn apply_tx(ctx: &mut Ctx, tx_data: Tx) -> TxResult {
let tx_data = governance::VoteProposalData::try_from_slice(&data[..])
.wrap_err("failed to decode VoteProposalData")?;

// The tx must be authorized by the source address
ctx.insert_verifier(&tx_data.voter)?;

debug_log!("apply_tx called to vote a governance proposal");

governance::vote_proposal(ctx, tx_data)
Expand Down

0 comments on commit 2550d2d

Please sign in to comment.