penumbra_fee/
component.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
mod fee_pay;
pub mod rpc;
mod view;

use std::sync::Arc;

use crate::{event::EventBlockFees, genesis, Fee};
use async_trait::async_trait;
use cnidarium::StateWrite;
use cnidarium_component::Component;
use penumbra_proto::state::StateWriteProto as _;
use penumbra_proto::DomainType as _;
use tendermint::abci;
use tracing::instrument;

pub use fee_pay::FeePay;
pub use view::{StateReadExt, StateWriteExt};

// Fee component
pub struct FeeComponent {}

#[async_trait]
impl Component for FeeComponent {
    type AppState = genesis::Content;

    #[instrument(name = "fee", skip(state, app_state))]
    async fn init_chain<S: StateWrite>(mut state: S, app_state: Option<&Self::AppState>) {
        match app_state {
            Some(genesis) => {
                state.put_fee_params(genesis.fee_params.clone());
            }
            None => { /* perform upgrade specific check */ }
        }
    }

    #[instrument(name = "fee", skip(_state, _begin_block))]
    async fn begin_block<S: StateWrite + 'static>(
        _state: &mut Arc<S>,
        _begin_block: &abci::request::BeginBlock,
    ) {
    }

    #[instrument(name = "fee", skip(state, _end_block))]
    async fn end_block<S: StateWrite + 'static>(
        state: &mut Arc<S>,
        _end_block: &abci::request::EndBlock,
    ) {
        let state_ref = Arc::get_mut(state).expect("unique ref in end_block");
        // Grab the total fees and use them to emit an event.
        let fees = state_ref.accumulated_base_fees_and_tips();

        let (swapped_base, swapped_tip) = fees
            .get(&penumbra_asset::STAKING_TOKEN_ASSET_ID)
            .cloned()
            .unwrap_or_default();

        let swapped_total = swapped_base + swapped_tip;

        state_ref.record_proto(
            EventBlockFees {
                swapped_fee_total: Fee::from_staking_token_amount(swapped_total),
                swapped_base_fee_total: Fee::from_staking_token_amount(swapped_base),
                swapped_tip_total: Fee::from_staking_token_amount(swapped_tip),
            }
            .to_proto(),
        );
    }

    #[instrument(name = "fee", skip(_state))]
    async fn end_epoch<S: StateWrite + 'static>(_state: &mut Arc<S>) -> anyhow::Result<()> {
        Ok(())
    }
}