@@ -4,13 +4,14 @@ use num_rational::Rational;
44use primitive_types:: U256 ;
55
66use near_primitives:: types:: { AccountId , Balance , BlockChunkValidatorStats } ;
7+ use near_primitives:: version:: { ProtocolVersion , ENABLE_INFLATION_PROTOCOL_VERSION } ;
78
89#[ derive( Clone , Debug ) ]
910pub struct RewardCalculator {
1011 pub max_inflation_rate : Rational ,
1112 pub num_blocks_per_year : u64 ,
1213 pub epoch_length : u64 ,
13- pub protocol_reward_percentage : Rational ,
14+ pub protocol_reward_rate : Rational ,
1415 pub protocol_treasury_account : AccountId ,
1516 pub online_min_threshold : Rational ,
1617 pub online_max_threshold : Rational ,
@@ -25,18 +26,26 @@ impl RewardCalculator {
2526 validator_block_chunk_stats : HashMap < AccountId , BlockChunkValidatorStats > ,
2627 validator_stake : & HashMap < AccountId , Balance > ,
2728 total_supply : Balance ,
29+ protocol_version : ProtocolVersion ,
30+ genesis_protocol_version : ProtocolVersion ,
2831 ) -> ( HashMap < AccountId , Balance > , Balance ) {
2932 let mut res = HashMap :: new ( ) ;
3033 let num_validators = validator_block_chunk_stats. len ( ) ;
31- let epoch_total_reward = ( U256 :: from ( * self . max_inflation_rate . numer ( ) as u64 )
34+ let use_hardcoded_value = genesis_protocol_version < protocol_version
35+ && protocol_version >= ENABLE_INFLATION_PROTOCOL_VERSION ;
36+ let max_inflation_rate =
37+ if use_hardcoded_value { Rational :: new_raw ( 1 , 20 ) } else { self . max_inflation_rate } ;
38+ let protocol_reward_rate =
39+ if use_hardcoded_value { Rational :: new_raw ( 1 , 10 ) } else { self . protocol_reward_rate } ;
40+ let epoch_total_reward = ( U256 :: from ( * max_inflation_rate. numer ( ) as u64 )
3241 * U256 :: from ( total_supply)
3342 * U256 :: from ( self . epoch_length )
3443 / ( U256 :: from ( self . num_blocks_per_year )
35- * U256 :: from ( * self . max_inflation_rate . denom ( ) as u64 ) ) )
44+ * U256 :: from ( * max_inflation_rate. denom ( ) as u64 ) ) )
3645 . as_u128 ( ) ;
3746 let epoch_protocol_treasury = ( U256 :: from ( epoch_total_reward)
38- * U256 :: from ( * self . protocol_reward_percentage . numer ( ) as u64 )
39- / U256 :: from ( * self . protocol_reward_percentage . denom ( ) as u64 ) )
47+ * U256 :: from ( * protocol_reward_rate . numer ( ) as u64 )
48+ / U256 :: from ( * protocol_reward_rate . denom ( ) as u64 ) )
4049 . as_u128 ( ) ;
4150 res. insert ( self . protocol_treasury_account . clone ( ) , epoch_protocol_treasury) ;
4251 if num_validators == 0 {
@@ -94,6 +103,7 @@ impl RewardCalculator {
94103mod tests {
95104 use crate :: RewardCalculator ;
96105 use near_primitives:: types:: { BlockChunkValidatorStats , ValidatorStats } ;
106+ use near_primitives:: version:: { ENABLE_INFLATION_PROTOCOL_VERSION , PROTOCOL_VERSION } ;
97107 use num_rational:: Rational ;
98108 use std:: collections:: HashMap ;
99109
@@ -103,7 +113,7 @@ mod tests {
103113 max_inflation_rate : Rational :: new ( 0 , 1 ) ,
104114 num_blocks_per_year : 1000000 ,
105115 epoch_length : 1 ,
106- protocol_reward_percentage : Rational :: new ( 0 , 1 ) ,
116+ protocol_reward_rate : Rational :: new ( 0 , 1 ) ,
107117 protocol_treasury_account : "near" . to_string ( ) ,
108118 online_min_threshold : Rational :: new ( 9 , 10 ) ,
109119 online_max_threshold : Rational :: new ( 1 , 1 ) ,
@@ -134,6 +144,8 @@ mod tests {
134144 validator_block_chunk_stats,
135145 & validator_stake,
136146 total_supply,
147+ PROTOCOL_VERSION ,
148+ PROTOCOL_VERSION ,
137149 ) ;
138150 assert_eq ! (
139151 result. 0 ,
@@ -154,7 +166,7 @@ mod tests {
154166 max_inflation_rate : Rational :: new ( 1 , 100 ) ,
155167 num_blocks_per_year : 1000 ,
156168 epoch_length : 1000 ,
157- protocol_reward_percentage : Rational :: new ( 0 , 10 ) ,
169+ protocol_reward_rate : Rational :: new ( 0 , 10 ) ,
158170 protocol_treasury_account : "near" . to_string ( ) ,
159171 online_min_threshold : Rational :: new ( 9 , 10 ) ,
160172 online_max_threshold : Rational :: new ( 99 , 100 ) ,
@@ -196,6 +208,8 @@ mod tests {
196208 validator_block_chunk_stats,
197209 & validator_stake,
198210 total_supply,
211+ PROTOCOL_VERSION ,
212+ PROTOCOL_VERSION ,
199213 ) ;
200214 // Total reward is 10_000_000. Divided by 3 equal stake validators - each gets 3_333_333.
201215 // test1 with 94.5% online gets 50% because of linear between (0.99-0.9) online.
@@ -222,7 +236,7 @@ mod tests {
222236 num_blocks_per_year : 60 * 60 * 24 * 365 ,
223237 // half a day
224238 epoch_length : 60 * 60 * 12 ,
225- protocol_reward_percentage : Rational :: new ( 1 , 10 ) ,
239+ protocol_reward_rate : Rational :: new ( 1 , 10 ) ,
226240 protocol_treasury_account : "near" . to_string ( ) ,
227241 online_min_threshold : Rational :: new ( 9 , 10 ) ,
228242 online_max_threshold : Rational :: new ( 1 , 1 ) ,
@@ -245,6 +259,48 @@ mod tests {
245259 validator_block_chunk_stats,
246260 & validator_stake,
247261 total_supply,
262+ PROTOCOL_VERSION ,
263+ PROTOCOL_VERSION ,
248264 ) ;
249265 }
266+
267+ #[ test]
268+ fn test_enable_inflation ( ) {
269+ // no inflation at the beginning
270+ let reward_calculator = RewardCalculator {
271+ max_inflation_rate : Rational :: new ( 0 , 1 ) ,
272+ num_blocks_per_year : 1000 ,
273+ epoch_length : 1000 ,
274+ protocol_reward_rate : Rational :: new ( 0 , 1 ) ,
275+ protocol_treasury_account : "near" . to_string ( ) ,
276+ online_min_threshold : Rational :: new ( 9 , 10 ) ,
277+ online_max_threshold : Rational :: new ( 99 , 100 ) ,
278+ } ;
279+ let validator_block_chunk_stats = vec ! [ (
280+ "test1" . to_string( ) ,
281+ BlockChunkValidatorStats {
282+ block_stats: ValidatorStats { produced: 990 , expected: 1000 } ,
283+ chunk_stats: ValidatorStats { produced: 990 , expected: 1000 } ,
284+ } ,
285+ ) ]
286+ . into_iter ( )
287+ . collect :: < HashMap < _ , _ > > ( ) ;
288+ let validator_stake =
289+ vec ! [ ( "test1" . to_string( ) , 100_000 ) ] . into_iter ( ) . collect :: < HashMap < _ , _ > > ( ) ;
290+ let total_supply = 1_000_000_000 ;
291+ let ( account_rewards, epoch_total_reward) = reward_calculator. calculate_reward (
292+ validator_block_chunk_stats,
293+ & validator_stake,
294+ total_supply,
295+ ENABLE_INFLATION_PROTOCOL_VERSION ,
296+ ENABLE_INFLATION_PROTOCOL_VERSION - 1 ,
297+ ) ;
298+ assert_eq ! (
299+ account_rewards,
300+ vec![ ( "test1" . to_string( ) , 45000000 ) , ( "near" . to_string( ) , 5000000 ) ]
301+ . into_iter( )
302+ . collect( )
303+ ) ;
304+ assert_eq ! ( epoch_total_reward, 50000000 ) ;
305+ }
250306}
0 commit comments