Skip to content

Commit

Permalink
chore(sequencer): refactor fees (#1811)
Browse files Browse the repository at this point in the history
## Summary
This reduces a lot of boilerplate around fees.

## Background
The boilerplate code is very prone to copy-paste errors. Changing the 14
different (but structurally identical) `FeeComponent` structs into a
single one with a generic arg reduces a lot of the boilerplate code.

This PR replaces #1739 (an initial, more restricted refactor) and #1794
(a PR on top of #1739). It comprises the net changes of these two PRs in
[the first
commit](a8ec424),
followed by most of the changes suggested by @eoroshiba's review of
#1794 in [the second
commit](c92dc98).

[The third
commit](1316e07)
is also in response to @eoroshiba's suggestion to remove the fee
aliases, and is a single commit in case others prefer that change to be
reverted.

## Changes
- In core, replaced fee component structs with a new `FeeComponents<T>`
where `T` will be some action type.
- In sequencer, changed the `FeeHandler` trait fairly radically to
support the new form of fees.

## Testing
Existing tests are sufficient - there should be no changed functionality
to the business logic.

## Changelogs
No updates required.

## Related Issues
Closes #1715.
  • Loading branch information
Fraser999 authored Dec 6, 2024
1 parent 3419c74 commit 903e1f3
Show file tree
Hide file tree
Showing 28 changed files with 1,279 additions and 2,158 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 85 additions & 83 deletions crates/astria-core/src/protocol/fees/v1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
use std::{
fmt::{
self,
Debug,
Formatter,
},
marker::PhantomData,
};

use penumbra_ibc::IbcRelay;
use prost::Name as _;

use crate::{
generated::astria::protocol::fees::v1 as raw,
primitive::v1::asset,
protocol::transaction::v1::action::{
BridgeLock,
BridgeSudoChange,
BridgeUnlock,
FeeAssetChange,
FeeChange,
IbcRelayerChange,
IbcSudoChange,
Ics20Withdrawal,
InitBridgeAccount,
RollupDataSubmission,
SudoAddressChange,
Transfer,
ValidatorUpdate,
},
Protobuf,
};

Expand Down Expand Up @@ -49,13 +74,15 @@ macro_rules! impl_protobuf_for_fee_components {
multiplier: multiplier
.ok_or_else(|| Self::Error::missing_field(Self::Raw::full_name(), "multiplier"))?
.into(),
_phantom: PhantomData,
})
}

fn to_raw(&self) -> Self::Raw {
let Self {
base,
multiplier,
_phantom,
} = self;
Self::Raw {
base: Some(base.into()),
Expand All @@ -67,104 +94,79 @@ macro_rules! impl_protobuf_for_fee_components {
};
}
impl_protobuf_for_fee_components!(
TransferFeeComponents => raw::TransferFeeComponents,
RollupDataSubmissionFeeComponents => raw::RollupDataSubmissionFeeComponents,
Ics20WithdrawalFeeComponents => raw::Ics20WithdrawalFeeComponents,
InitBridgeAccountFeeComponents => raw::InitBridgeAccountFeeComponents,
BridgeLockFeeComponents => raw::BridgeLockFeeComponents,
BridgeUnlockFeeComponents => raw::BridgeUnlockFeeComponents,
BridgeSudoChangeFeeComponents => raw::BridgeSudoChangeFeeComponents,
ValidatorUpdateFeeComponents => raw::ValidatorUpdateFeeComponents,
IbcRelayerChangeFeeComponents => raw::IbcRelayerChangeFeeComponents,
IbcRelayFeeComponents => raw::IbcRelayFeeComponents,
FeeAssetChangeFeeComponents => raw::FeeAssetChangeFeeComponents,
FeeChangeFeeComponents => raw::FeeChangeFeeComponents,
SudoAddressChangeFeeComponents => raw::SudoAddressChangeFeeComponents,
IbcSudoChangeFeeComponents => raw::IbcSudoChangeFeeComponents,
FeeComponents<Transfer> => raw::TransferFeeComponents,
FeeComponents<RollupDataSubmission> => raw::RollupDataSubmissionFeeComponents,
FeeComponents<Ics20Withdrawal> => raw::Ics20WithdrawalFeeComponents,
FeeComponents<InitBridgeAccount> => raw::InitBridgeAccountFeeComponents,
FeeComponents<BridgeLock> => raw::BridgeLockFeeComponents,
FeeComponents<BridgeUnlock> => raw::BridgeUnlockFeeComponents,
FeeComponents<BridgeSudoChange> => raw::BridgeSudoChangeFeeComponents,
FeeComponents<ValidatorUpdate> => raw::ValidatorUpdateFeeComponents,
FeeComponents<IbcRelayerChange> => raw::IbcRelayerChangeFeeComponents,
FeeComponents<IbcRelay> => raw::IbcRelayFeeComponents,
FeeComponents<FeeAssetChange> => raw::FeeAssetChangeFeeComponents,
FeeComponents<FeeChange> => raw::FeeChangeFeeComponents,
FeeComponents<SudoAddressChange> => raw::SudoAddressChangeFeeComponents,
FeeComponents<IbcSudoChange> => raw::IbcSudoChangeFeeComponents,
);

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TransferFeeComponents {
pub base: u128,
pub multiplier: u128,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RollupDataSubmissionFeeComponents {
pub base: u128,
pub multiplier: u128,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Ics20WithdrawalFeeComponents {
pub base: u128,
pub multiplier: u128,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct InitBridgeAccountFeeComponents {
pub base: u128,
pub multiplier: u128,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BridgeLockFeeComponents {
pub base: u128,
pub multiplier: u128,
pub struct FeeComponents<T: ?Sized> {
base: u128,
multiplier: u128,
_phantom: PhantomData<T>,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BridgeUnlockFeeComponents {
pub base: u128,
pub multiplier: u128,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BridgeSudoChangeFeeComponents {
pub base: u128,
pub multiplier: u128,
}
impl<T: ?Sized> FeeComponents<T> {
#[must_use]
pub fn new(base: u128, multiplier: u128) -> Self {
Self {
base,
multiplier,
_phantom: PhantomData,
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct IbcRelayFeeComponents {
pub base: u128,
pub multiplier: u128,
}
#[must_use]
pub fn base(&self) -> u128 {
self.base
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ValidatorUpdateFeeComponents {
pub base: u128,
pub multiplier: u128,
#[must_use]
pub fn multiplier(&self) -> u128 {
self.multiplier
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FeeAssetChangeFeeComponents {
pub base: u128,
pub multiplier: u128,
impl<T: Protobuf> Debug for FeeComponents<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct(&format!("FeeComponents<{}>", T::Raw::NAME))
.field("base", &self.base)
.field("multiplier", &self.multiplier)
.finish()
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FeeChangeFeeComponents {
pub base: u128,
pub multiplier: u128,
impl Debug for FeeComponents<IbcRelay> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("FeeComponents<IbcRelay>")
.field("base", &self.base)
.field("multiplier", &self.multiplier)
.finish()
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct IbcRelayerChangeFeeComponents {
pub base: u128,
pub multiplier: u128,
impl<T: ?Sized> Clone for FeeComponents<T> {
fn clone(&self) -> Self {
*self
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SudoAddressChangeFeeComponents {
pub base: u128,
pub multiplier: u128,
}
impl<T: ?Sized> Copy for FeeComponents<T> {}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct IbcSudoChangeFeeComponents {
pub base: u128,
pub multiplier: u128,
impl<T: ?Sized> PartialEq for FeeComponents<T> {
fn eq(&self, other: &Self) -> bool {
self.base == other.base && self.multiplier == other.multiplier
}
}

#[derive(Debug, Clone)]
Expand Down
Loading

0 comments on commit 903e1f3

Please sign in to comment.