use std::collections::BTreeSet;
use thiserror::Error;
use crate::system::{
runtime_native::{Config as NativeRuntimeConfig, TransferConfig},
transfer::TransferError,
};
use casper_types::{
account::AccountHash, execution::Effects, BlockTime, Digest, FeeHandling, ProtocolVersion,
Transfer,
};
use crate::tracking_copy::TrackingCopyError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeeRequest {
config: NativeRuntimeConfig,
state_hash: Digest,
protocol_version: ProtocolVersion,
block_time: BlockTime,
}
impl FeeRequest {
pub fn new(
config: NativeRuntimeConfig,
state_hash: Digest,
protocol_version: ProtocolVersion,
block_time: BlockTime,
) -> Self {
FeeRequest {
config,
state_hash,
protocol_version,
block_time,
}
}
pub fn config(&self) -> &NativeRuntimeConfig {
&self.config
}
pub fn state_hash(&self) -> Digest {
self.state_hash
}
pub fn protocol_version(&self) -> ProtocolVersion {
self.protocol_version
}
pub fn fee_handling(&self) -> &FeeHandling {
self.config.fee_handling()
}
pub fn block_time(&self) -> BlockTime {
self.block_time
}
pub fn administrative_accounts(&self) -> Option<&BTreeSet<AccountHash>> {
match self.config.transfer_config() {
TransferConfig::Administered {
administrative_accounts,
..
} => Some(administrative_accounts),
TransferConfig::Unadministered => None,
}
}
pub fn should_distribute_fees(&self) -> bool {
if !self.fee_handling().is_accumulate() {
return false;
}
matches!(
self.config.transfer_config(),
TransferConfig::Administered { .. }
)
}
}
#[derive(Clone, Error, Debug)]
pub enum FeeError {
#[error("Undistributed fees")]
NoFeesDistributed,
#[error(transparent)]
TrackingCopy(TrackingCopyError),
#[error("Registry entry not found: {0}")]
RegistryEntryNotFound(String),
#[error(transparent)]
Transfer(TransferError),
#[error("Named keys not found")]
NamedKeysNotFound,
#[error("Administrative accounts not found")]
AdministrativeAccountsNotFound,
}
#[derive(Debug, Clone)]
pub enum FeeResult {
RootNotFound,
Failure(FeeError),
Success {
transfers: Vec<Transfer>,
post_state_hash: Digest,
effects: Effects,
},
}
impl FeeResult {
pub fn is_success(&self) -> bool {
matches!(self, FeeResult::Success { .. })
}
}