cfx_executor/state/state_object/
reward.rs

1use super::State;
2use cfx_parameters::{
3    consensus::ONE_CFX_IN_DRIP, consensus_internal::CIP137_BASEFEE_PROP_INIT,
4};
5use cfx_types::U256;
6
7use cfx_parameters::staking::INTEREST_RATE_PER_BLOCK_SCALE;
8use cfx_statedb::global_params::*;
9
10impl State {
11    /// Calculate the secondary reward for the next block number.
12    pub fn bump_block_number_accumulate_interest(&mut self) {
13        assert!(self.no_checkpoint());
14        let interset_rate_per_block = self.global_stat.get::<InterestRate>();
15        let accumulate_interest_rate =
16            self.global_stat.val::<AccumulateInterestRate>();
17        *accumulate_interest_rate = *accumulate_interest_rate
18            * (*INTEREST_RATE_PER_BLOCK_SCALE + interset_rate_per_block)
19            / *INTEREST_RATE_PER_BLOCK_SCALE;
20    }
21
22    pub fn secondary_reward(&self) -> U256 {
23        assert!(self.no_checkpoint());
24        let secondary_reward = *self.global_stat.refr::<TotalStorage>()
25            * *self.global_stat.refr::<InterestRate>()
26            / *INTEREST_RATE_PER_BLOCK_SCALE;
27        // TODO: the interest from tokens other than storage and staking should
28        // send to public fund.
29        secondary_reward
30    }
31
32    pub fn pow_base_reward(&self) -> U256 {
33        let base_reward = self.global_stat.get::<PowBaseReward>();
34        assert!(!base_reward.is_zero());
35        base_reward
36    }
37
38    pub fn distributable_pos_interest(&self) -> U256 {
39        self.global_stat.get::<DistributablePoSInterest>()
40    }
41
42    pub fn last_distribute_block(&self) -> u64 {
43        self.global_stat.refr::<LastDistributeBlock>().as_u64()
44    }
45
46    pub fn reset_pos_distribute_info(&mut self, current_block_number: u64) {
47        *self.global_stat.val::<DistributablePoSInterest>() = U256::zero();
48        *self.global_stat.val::<LastDistributeBlock>() =
49            U256::from(current_block_number);
50    }
51
52    pub fn burn_by_cip1559(&mut self, by: U256) {
53        // This function is called after transaction exeuction. At this time,
54        // the paid transaction fee has already been in the core space.
55        *self.global_stat.val::<TotalBurnt1559>() += by;
56        self.sub_total_issued(by);
57    }
58
59    pub fn get_base_price_prop(&self) -> U256 {
60        self.global_stat.get::<BaseFeeProp>()
61    }
62
63    pub fn set_base_fee_prop(&mut self, val: U256) {
64        *self.global_stat.val::<BaseFeeProp>() = val;
65    }
66
67    pub fn burnt_gas_price(&self, base_price: U256) -> U256 {
68        if base_price.is_zero() {
69            return U256::zero();
70        }
71        let prop = self.get_base_price_prop();
72        base_price - base_price * prop / (U256::from(ONE_CFX_IN_DRIP) + prop)
73    }
74}
75
76/// Initialize CIP-137 for the whole system.
77pub fn initialize_cip137(state: &mut State) {
78    debug!("set base_fee_prop to {}", CIP137_BASEFEE_PROP_INIT);
79    state.set_base_fee_prop(CIP137_BASEFEE_PROP_INIT.into());
80}