cfx_statedb/
global_params.rs

1use cfx_parameters::{
2    internal_contract_addresses::*,
3    staking::{
4        ACCUMULATED_INTEREST_RATE_SCALE, BLOCKS_PER_YEAR,
5        INITIAL_INTEREST_RATE_PER_BLOCK,
6    },
7};
8use cfx_types::{Address, Space, U256};
9use primitives::{StorageKey, StorageKeyWithSpace};
10
11pub trait GlobalParamKey {
12    const SPACE: Space = Space::Native;
13    const ADDRESS: Address = STORAGE_INTEREST_STAKING_CONTRACT_ADDRESS;
14    const ID: usize;
15    const KEY: &'static [u8];
16    const STORAGE_KEY: StorageKeyWithSpace<'static> =
17        StorageKey::new_storage_key(&Self::ADDRESS, Self::KEY)
18            .with_space(Self::SPACE);
19
20    /// How to initialize such a variable in the executor
21    fn init_vm_value() -> U256 { U256::zero() }
22    /// How to convert such a variable from the executor representing to the db
23    /// representing
24    fn from_vm_value(val: U256) -> U256 { val }
25    /// How to convert such a variable from the db representing to the executor
26    /// representing
27    fn into_vm_value(val: U256) -> U256 { val }
28}
29
30#[macro_export]
31macro_rules! for_all_global_param_keys {
32    ($f:ident::<Key>($($args:expr),*);) => {
33        $f::<InterestRate>($($args),*);
34        $f::<AccumulateInterestRate>($($args),*);
35        $f::<TotalIssued>($($args),*);
36        $f::<TotalStaking>($($args),*);
37        $f::<TotalStorage>($($args),*);
38        $f::<TotalEvmToken>($($args),*);
39        $f::<UsedStoragePoints>($($args),*);
40        $f::<ConvertedStoragePoints>($($args),*);
41        $f::<TotalPosStaking>($($args),*);
42        $f::<DistributablePoSInterest>($($args),*);
43        $f::<LastDistributeBlock>($($args),*);
44        $f::<PowBaseReward>($($args),*);
45        $f::<TotalBurnt1559>($($args),*);
46        $f::<BaseFeeProp>($($args),*);
47    };
48    ($f:ident::<Key>($($args:expr),*)?;) => {
49        $f::<InterestRate>($($args),*)?;
50        $f::<AccumulateInterestRate>($($args),*)?;
51        $f::<TotalIssued>($($args),*)?;
52        $f::<TotalStaking>($($args),*)?;
53        $f::<TotalStorage>($($args),*)?;
54        $f::<TotalEvmToken>($($args),*)?;
55        $f::<UsedStoragePoints>($($args),*)?;
56        $f::<ConvertedStoragePoints>($($args),*)?;
57        $f::<TotalPosStaking>($($args),*)?;
58        $f::<DistributablePoSInterest>($($args),*)?;
59        $f::<LastDistributeBlock>($($args),*)?;
60        $f::<PowBaseReward>($($args),*)?;
61        $f::<TotalBurnt1559>($($args),*)?;
62        $f::<BaseFeeProp>($($args),*)?;
63    };
64}
65
66pub struct InterestRate;
67impl GlobalParamKey for InterestRate {
68    const ID: usize = 0;
69    const KEY: &'static [u8] = b"interest_rate";
70
71    fn init_vm_value() -> U256 { *INITIAL_INTEREST_RATE_PER_BLOCK }
72
73    fn from_vm_value(val: U256) -> U256 { val * U256::from(BLOCKS_PER_YEAR) }
74
75    fn into_vm_value(val: U256) -> U256 { val / U256::from(BLOCKS_PER_YEAR) }
76}
77
78pub struct AccumulateInterestRate;
79impl GlobalParamKey for AccumulateInterestRate {
80    const ID: usize = InterestRate::ID + 1;
81    const KEY: &'static [u8] = b"accumulate_interest_rate";
82
83    fn init_vm_value() -> U256 { *ACCUMULATED_INTEREST_RATE_SCALE }
84}
85
86pub struct TotalIssued;
87impl GlobalParamKey for TotalIssued {
88    const ID: usize = AccumulateInterestRate::ID + 1;
89    const KEY: &'static [u8] = b"total_issued_tokens";
90}
91
92pub struct TotalStaking;
93impl GlobalParamKey for TotalStaking {
94    const ID: usize = TotalIssued::ID + 1;
95    const KEY: &'static [u8] = b"total_staking_tokens";
96}
97
98pub struct TotalStorage;
99impl GlobalParamKey for TotalStorage {
100    const ID: usize = TotalStaking::ID + 1;
101    const KEY: &'static [u8] = b"total_storage_tokens";
102}
103
104pub struct TotalEvmToken;
105impl GlobalParamKey for TotalEvmToken {
106    const ID: usize = TotalStorage::ID + 1;
107    const KEY: &'static [u8] = b"total_evm_tokens";
108}
109
110pub struct UsedStoragePoints;
111impl GlobalParamKey for UsedStoragePoints {
112    const ID: usize = TotalEvmToken::ID + 1;
113    const KEY: &'static [u8] = b"used_storage_points";
114}
115
116pub struct ConvertedStoragePoints;
117impl GlobalParamKey for ConvertedStoragePoints {
118    const ID: usize = UsedStoragePoints::ID + 1;
119    const KEY: &'static [u8] = b"converted_storage_points_key";
120}
121
122pub struct TotalPosStaking;
123impl GlobalParamKey for TotalPosStaking {
124    const ID: usize = ConvertedStoragePoints::ID + 1;
125    const KEY: &'static [u8] = b"total_pos_staking_tokens";
126}
127
128pub struct DistributablePoSInterest;
129impl GlobalParamKey for DistributablePoSInterest {
130    const ID: usize = TotalPosStaking::ID + 1;
131    const KEY: &'static [u8] = b"distributable_pos_interest";
132}
133pub struct LastDistributeBlock;
134impl GlobalParamKey for LastDistributeBlock {
135    const ID: usize = DistributablePoSInterest::ID + 1;
136    const KEY: &'static [u8] = b"last_distribute_block";
137}
138
139pub struct PowBaseReward;
140impl GlobalParamKey for PowBaseReward {
141    const ADDRESS: Address = PARAMS_CONTROL_CONTRACT_ADDRESS;
142    const ID: usize = LastDistributeBlock::ID + 1;
143    const KEY: &'static [u8] = b"pow_base_reward";
144}
145
146pub struct TotalBurnt1559;
147impl GlobalParamKey for TotalBurnt1559 {
148    const ID: usize = PowBaseReward::ID + 1;
149    const KEY: &'static [u8] = b"total_burnt_tokens_by_cip1559";
150}
151
152pub struct BaseFeeProp;
153impl GlobalParamKey for BaseFeeProp {
154    const ID: usize = TotalBurnt1559::ID + 1;
155    const KEY: &'static [u8] = b"base_fee_prop";
156}
157
158pub const TOTAL_GLOBAL_PARAMS: usize = BaseFeeProp::ID + 1;