cfx_executor/state/state_object/
reward.rs1use super::State;
2use cfx_parameters::{
3 consensus::ONE_CFX_IN_DRIP, consensus_internal::CIP137_BASEFEE_PROP_INIT,
4};
5use cfx_types::{U256, U512};
6
7use cfx_parameters::staking::INTEREST_RATE_PER_BLOCK_SCALE;
8use cfx_statedb::global_params::*;
9
10impl State {
11 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 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 *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 burnt_gas_price(base_price, self.get_base_price_prop())
69 }
70}
71
72pub(crate) fn burnt_gas_price(base_price: U256, base_fee_prop: U256) -> U256 {
73 if base_price.is_zero() {
74 return U256::zero();
75 }
76 let denominator = U512::from(ONE_CFX_IN_DRIP) + U512::from(base_fee_prop);
77 let reduction =
78 (U512::from(base_price) * U512::from(base_fee_prop)) / denominator;
79 U256::try_from(U512::from(base_price) - reduction).unwrap()
80}
81
82#[cfg(test)]
83mod tests {
84 use super::burnt_gas_price;
85 use cfx_types::U256;
86
87 #[test]
88 fn burnt_gas_price_boundaries_are_safe() {
89 for base in [U256::zero(), U256::from(100), U256::MAX] {
90 let result = burnt_gas_price(base, U256::from(50));
91 assert!(result <= base);
92 }
93 assert_eq!(burnt_gas_price(U256::zero(), U256::MAX), U256::zero());
94 }
95}
96
97pub fn initialize_cip137(state: &mut State) {
99 debug!("set base_fee_prop to {}", CIP137_BASEFEE_PROP_INIT);
100 state.set_base_fee_prop(CIP137_BASEFEE_PROP_INIT.into());
101}