casper_storage/data_access_layer/
fee.rs1use std::collections::BTreeSet;
2use thiserror::Error;
3
4use crate::system::{
5 runtime_native::{Config as NativeRuntimeConfig, TransferConfig},
6 transfer::TransferError,
7};
8use casper_types::{
9 account::AccountHash, execution::Effects, BlockTime, Digest, FeeHandling, ProtocolVersion,
10 Transfer,
11};
12
13use crate::tracking_copy::TrackingCopyError;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct FeeRequest {
18 config: NativeRuntimeConfig,
19 state_hash: Digest,
20 protocol_version: ProtocolVersion,
21 block_time: BlockTime,
22}
23
24impl FeeRequest {
25 pub fn new(
27 config: NativeRuntimeConfig,
28 state_hash: Digest,
29 protocol_version: ProtocolVersion,
30 block_time: BlockTime,
31 ) -> Self {
32 FeeRequest {
33 config,
34 state_hash,
35 protocol_version,
36 block_time,
37 }
38 }
39
40 pub fn config(&self) -> &NativeRuntimeConfig {
42 &self.config
43 }
44
45 pub fn state_hash(&self) -> Digest {
47 self.state_hash
48 }
49
50 pub fn protocol_version(&self) -> ProtocolVersion {
52 self.protocol_version
53 }
54
55 pub fn fee_handling(&self) -> &FeeHandling {
57 self.config.fee_handling()
58 }
59
60 pub fn block_time(&self) -> BlockTime {
62 self.block_time
63 }
64
65 pub fn administrative_accounts(&self) -> Option<&BTreeSet<AccountHash>> {
67 match self.config.transfer_config() {
68 TransferConfig::Administered {
69 administrative_accounts,
70 ..
71 } => Some(administrative_accounts),
72 TransferConfig::Unadministered => None,
73 }
74 }
75
76 pub fn should_distribute_fees(&self) -> bool {
78 if !self.fee_handling().is_accumulate() {
82 return false;
83 }
84
85 matches!(
86 self.config.transfer_config(),
87 TransferConfig::Administered { .. }
88 )
89 }
90}
91
92#[derive(Clone, Error, Debug)]
94pub enum FeeError {
95 #[error("Undistributed fees")]
97 NoFeesDistributed,
98 #[error(transparent)]
100 TrackingCopy(TrackingCopyError),
101 #[error("Registry entry not found: {0}")]
103 RegistryEntryNotFound(String),
104 #[error(transparent)]
106 Transfer(TransferError),
107 #[error("Named keys not found")]
109 NamedKeysNotFound,
110 #[error("Administrative accounts not found")]
112 AdministrativeAccountsNotFound,
113}
114
115#[derive(Debug, Clone)]
117pub enum FeeResult {
118 RootNotFound,
120 Failure(FeeError),
122 Success {
124 transfers: Vec<Transfer>,
126 post_state_hash: Digest,
128 effects: Effects,
130 },
131}
132
133impl FeeResult {
134 pub fn is_success(&self) -> bool {
136 matches!(self, FeeResult::Success { .. })
137 }
138}