casper_storage/data_access_layer/
block_rewards.rs

1use std::collections::BTreeMap;
2
3use thiserror::Error;
4
5use casper_types::{
6    execution::Effects, system::auction::Error as AuctionError, BlockTime, Digest, ProtocolVersion,
7    PublicKey, U512,
8};
9
10use crate::{
11    system::{runtime_native::Config, transfer::TransferError},
12    tracking_copy::TrackingCopyError,
13};
14
15/// Block rewards request.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct BlockRewardsRequest {
18    config: Config,
19    state_hash: Digest,
20    protocol_version: ProtocolVersion,
21    rewards: BTreeMap<PublicKey, Vec<U512>>,
22    block_time: BlockTime,
23}
24
25impl BlockRewardsRequest {
26    /// Ctor.
27    pub fn new(
28        config: Config,
29        state_hash: Digest,
30        protocol_version: ProtocolVersion,
31        block_time: BlockTime,
32        rewards: BTreeMap<PublicKey, Vec<U512>>,
33    ) -> Self {
34        BlockRewardsRequest {
35            config,
36            state_hash,
37            protocol_version,
38            rewards,
39            block_time,
40        }
41    }
42
43    /// Returns config.
44    pub fn config(&self) -> &Config {
45        &self.config
46    }
47
48    /// Returns state_hash.
49    pub fn state_hash(&self) -> Digest {
50        self.state_hash
51    }
52
53    /// Returns protocol_version.
54    pub fn protocol_version(&self) -> ProtocolVersion {
55        self.protocol_version
56    }
57
58    /// Returns rewards.
59    pub fn rewards(&self) -> &BTreeMap<PublicKey, Vec<U512>> {
60        &self.rewards
61    }
62
63    /// Returns block time.
64    pub fn block_time(&self) -> BlockTime {
65        self.block_time
66    }
67}
68
69/// Block rewards error.
70#[derive(Clone, Error, Debug)]
71pub enum BlockRewardsError {
72    /// Undistributed rewards error.
73    #[error("Undistributed rewards")]
74    UndistributedRewards,
75    /// Tracking copy error.
76    #[error(transparent)]
77    TrackingCopy(TrackingCopyError),
78    /// Registry entry not found error.
79    #[error("Registry entry not found: {0}")]
80    RegistryEntryNotFound(String),
81    /// Transfer error.
82    #[error(transparent)]
83    Transfer(TransferError),
84    /// Auction error.
85    #[error("Auction error: {0}")]
86    Auction(AuctionError),
87}
88
89/// Block reward result.
90#[derive(Debug, Clone)]
91pub enum BlockRewardsResult {
92    /// Root not found in global state.
93    RootNotFound,
94    /// Block rewards failure error.
95    Failure(BlockRewardsError),
96    /// Success result.
97    Success {
98        /// State hash after distribution outcome is committed to the global state.
99        post_state_hash: Digest,
100        /// Effects of the distribution process.
101        effects: Effects,
102    },
103}
104
105impl BlockRewardsResult {
106    /// Returns true if successful, else false.
107    pub fn is_success(&self) -> bool {
108        matches!(self, BlockRewardsResult::Success { .. })
109    }
110}