Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
noot authored and bharath-123 committed May 7, 2024
1 parent 25e3e10 commit 4cd0930
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 19 deletions.
14 changes: 2 additions & 12 deletions crates/astria-core/src/protocol/transaction/v1alpha1/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,21 +1291,11 @@ pub enum FeeChange {
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone)]
pub struct FeeChangeAction {
fee_change: FeeChange,
new_value: u128,
pub fee_change: FeeChange,
pub new_value: u128,
}

impl FeeChangeAction {
#[must_use]
pub fn fee_change(&self) -> &FeeChange {
&self.fee_change
}

#[must_use]
pub fn new_value(&self) -> u128 {
self.new_value
}

#[must_use]
pub fn into_raw(self) -> raw::FeeChangeAction {
self.to_raw()
Expand Down
140 changes: 133 additions & 7 deletions crates/astria-sequencer/src/authority/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,155 @@ impl ActionHandler for FeeChangeAction {
sequence::state_ext::StateWriteExt as _,
};

match self.fee_change() {
match self.fee_change {
FeeChange::TransferBaseFee => {
state
.put_transfer_base_fee(self.new_value())
.put_transfer_base_fee(self.new_value)
.context("failed to put transfer base fee in state")?;
}
FeeChange::SequenceBaseFee => state.put_sequence_action_base_fee(self.new_value()),
FeeChange::SequenceBaseFee => state.put_sequence_action_base_fee(self.new_value),
FeeChange::SequenceByteCostMultiplier => {
state.put_sequence_action_byte_cost_multiplier(self.new_value())
state.put_sequence_action_byte_cost_multiplier(self.new_value);
}
FeeChange::InitBridgeAccountBaseFee => {
state.put_init_bridge_account_base_fee(self.new_value())
state.put_init_bridge_account_base_fee(self.new_value);
}
FeeChange::BridgeLockByteCostMultiplier => {
state.put_bridge_lock_byte_cost_multiplier(self.new_value())
state.put_bridge_lock_byte_cost_multiplier(self.new_value);
}
FeeChange::Ics20WithdrawalBaseFee => {
state
.put_ics20_withdrawal_base_fee(self.new_value())
.put_ics20_withdrawal_base_fee(self.new_value)
.context("failed to put ics20 withdrawal base fee in state")?;
}
}

Ok(())
}
}

#[cfg(test)]
mod test {
use cnidarium::StateDelta;

use super::*;
use crate::{
accounts::state_ext::{
StateReadExt as _,
StateWriteExt as _,
},
bridge::state_ext::{
StateReadExt as _,
StateWriteExt as _,
},
ibc::state_ext::{
StateReadExt as _,
StateWriteExt as _,
},
sequence::state_ext::{
StateReadExt as _,
StateWriteExt as _,
},
};

#[tokio::test]
async fn fee_change_action_execute() {
let storage = cnidarium::TempStorage::new().await.unwrap();
let snapshot = storage.latest_snapshot();
let mut state = StateDelta::new(snapshot);
let transfer_fee = 12;
state.put_transfer_base_fee(transfer_fee).unwrap();

let fee_change = FeeChangeAction {
fee_change: FeeChange::TransferBaseFee,
new_value: 10,
};

fee_change
.execute(&mut state, Address::from([1; 20]))
.await
.unwrap();
assert_eq!(state.get_transfer_base_fee().await.unwrap(), 10);

let sequence_base_fee = 5;
state.put_sequence_action_base_fee(sequence_base_fee);

let fee_change = FeeChangeAction {
fee_change: FeeChange::SequenceBaseFee,
new_value: 3,
};

fee_change
.execute(&mut state, Address::from([1; 20]))
.await
.unwrap();
assert_eq!(state.get_sequence_action_base_fee().await.unwrap(), 3);

let sequence_byte_cost_multiplier = 2;
state.put_sequence_action_byte_cost_multiplier(sequence_byte_cost_multiplier);

let fee_change = FeeChangeAction {
fee_change: FeeChange::SequenceByteCostMultiplier,
new_value: 4,
};

fee_change
.execute(&mut state, Address::from([1; 20]))
.await
.unwrap();
assert_eq!(
state
.get_sequence_action_byte_cost_multiplier()
.await
.unwrap(),
4
);

let init_bridge_account_base_fee = 1;
state.put_init_bridge_account_base_fee(init_bridge_account_base_fee);

let fee_change = FeeChangeAction {
fee_change: FeeChange::InitBridgeAccountBaseFee,
new_value: 2,
};

fee_change
.execute(&mut state, Address::from([1; 20]))
.await
.unwrap();
assert_eq!(state.get_init_bridge_account_base_fee().await.unwrap(), 2);

let bridge_lock_byte_cost_multiplier = 1;
state.put_bridge_lock_byte_cost_multiplier(bridge_lock_byte_cost_multiplier);

let fee_change = FeeChangeAction {
fee_change: FeeChange::BridgeLockByteCostMultiplier,
new_value: 2,
};

fee_change
.execute(&mut state, Address::from([1; 20]))
.await
.unwrap();
assert_eq!(
state.get_bridge_lock_byte_cost_multiplier().await.unwrap(),
2
);

let ics20_withdrawal_base_fee = 1;
state
.put_ics20_withdrawal_base_fee(ics20_withdrawal_base_fee)
.unwrap();

let fee_change = FeeChangeAction {
fee_change: FeeChange::Ics20WithdrawalBaseFee,
new_value: 2,
};

fee_change
.execute(&mut state, Address::from([1; 20]))
.await
.unwrap();
assert_eq!(state.get_ics20_withdrawal_base_fee().await.unwrap(), 2);
}
}

0 comments on commit 4cd0930

Please sign in to comment.