casper_storage/data_access_layer/
handle_fee.rs

1use crate::{
2    data_access_layer::BalanceIdentifier, system::runtime_native::Config as NativeRuntimeConfig,
3    tracking_copy::TrackingCopyError,
4};
5use casper_types::{
6    execution::Effects, Digest, EraId, InitiatorAddr, ProtocolVersion, PublicKey, TransactionHash,
7    Transfer, U512,
8};
9
10/// Handle fee mode.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum HandleFeeMode {
13    /// Pay the fee.
14    Pay {
15        /// Initiator.
16        initiator_addr: Box<InitiatorAddr>,
17        /// Source.
18        source: Box<BalanceIdentifier>,
19        /// Target.
20        target: Box<BalanceIdentifier>,
21        /// Amount.
22        amount: U512,
23    },
24    /// Burn the fee.
25    Burn {
26        /// Source.
27        source: BalanceIdentifier,
28        /// Amount.
29        amount: Option<U512>,
30    },
31    /// Validator credit (used in no fee mode).
32    Credit {
33        /// Validator.
34        validator: Box<PublicKey>,
35        /// Amount.
36        amount: U512,
37        /// EraId.
38        era_id: EraId,
39    },
40}
41
42impl HandleFeeMode {
43    /// Ctor for Pay mode.
44    pub fn pay(
45        initiator_addr: Box<InitiatorAddr>,
46        source: BalanceIdentifier,
47        target: BalanceIdentifier,
48        amount: U512,
49    ) -> Self {
50        HandleFeeMode::Pay {
51            initiator_addr,
52            source: Box::new(source),
53            target: Box::new(target),
54            amount,
55        }
56    }
57
58    /// What source should be used to burn from, and how much?
59    /// If amount is None or greater than the available balance, the full available balance
60    /// will be burned. If amount is less than available balance, only that much will be
61    /// burned leaving a remaining balance.
62    pub fn burn(source: BalanceIdentifier, amount: Option<U512>) -> Self {
63        HandleFeeMode::Burn { source, amount }
64    }
65
66    /// Applies a staking credit to the imputed proposer for the imputed amount at the end
67    /// of the current era when the auction process is executed.
68    pub fn credit(validator: Box<PublicKey>, amount: U512, era_id: EraId) -> Self {
69        HandleFeeMode::Credit {
70            validator,
71            amount,
72            era_id,
73        }
74    }
75}
76
77/// Handle fee request.
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct HandleFeeRequest {
80    /// The runtime config.
81    pub(crate) config: NativeRuntimeConfig,
82    /// State root hash.
83    pub(crate) state_hash: Digest,
84    /// The protocol version.
85    pub(crate) protocol_version: ProtocolVersion,
86    /// Transaction hash.
87    pub(crate) transaction_hash: TransactionHash,
88    /// Handle fee mode.
89    pub(crate) handle_fee_mode: HandleFeeMode,
90}
91
92impl HandleFeeRequest {
93    /// Creates new request instance with runtime args.
94    #[allow(clippy::too_many_arguments)]
95    pub fn new(
96        config: NativeRuntimeConfig,
97        state_hash: Digest,
98        protocol_version: ProtocolVersion,
99        transaction_hash: TransactionHash,
100        handle_fee_mode: HandleFeeMode,
101    ) -> Self {
102        Self {
103            config,
104            state_hash,
105            protocol_version,
106            transaction_hash,
107            handle_fee_mode,
108        }
109    }
110
111    /// Returns config.
112    pub fn config(&self) -> &NativeRuntimeConfig {
113        &self.config
114    }
115
116    /// Returns state hash.
117    pub fn state_hash(&self) -> Digest {
118        self.state_hash
119    }
120
121    /// Returns handle protocol version.
122    pub fn protocol_version(&self) -> ProtocolVersion {
123        self.protocol_version
124    }
125
126    /// Returns handle transaction hash.
127    pub fn transaction_hash(&self) -> TransactionHash {
128        self.transaction_hash
129    }
130
131    /// Returns handle fee mode.
132    pub fn handle_fee_mode(&self) -> &HandleFeeMode {
133        &self.handle_fee_mode
134    }
135}
136
137/// Result enum that represents all possible outcomes of a handle  request.
138#[derive(Debug)]
139pub enum HandleFeeResult {
140    /// Invalid state root hash.
141    RootNotFound,
142    /// Handle request succeeded.
143    Success {
144        /// Transfers.
145        transfers: Vec<Transfer>,
146        /// Handle fee effects.
147        effects: Effects,
148    },
149    /// Handle  request failed.
150    Failure(TrackingCopyError),
151}
152
153impl HandleFeeResult {
154    /// The effects, if any.
155    pub fn effects(&self) -> Effects {
156        match self {
157            HandleFeeResult::RootNotFound | HandleFeeResult::Failure(_) => Effects::new(),
158            HandleFeeResult::Success { effects, .. } => effects.clone(),
159        }
160    }
161}