Skip to content

Commit ce31777

Browse files
bowenwang1996Bowen Wang
authored andcommitted
feat: enable inflation for phase 2 (#3294)
Enable inflation for phase 2. This PR is no-op except for mainnet. Fixes #3265. Test plan --------- * `test_enable_inflation` * `enable_inflation.py`
1 parent 1384bcd commit ce31777

17 files changed

Lines changed: 203 additions & 43 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chain/chain/src/test_utils.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,6 @@ pub fn setup_with_tx_validity_period(
960960
min_gas_price: 100,
961961
max_gas_price: 1_000_000_000,
962962
total_supply: 1_000_000_000,
963-
max_inflation_rate: Rational::from_integer(0),
964963
gas_price_adjustment_rate: Rational::from_integer(0),
965964
transaction_validity_period: tx_validity_period,
966965
epoch_length: 10,
@@ -1001,7 +1000,6 @@ pub fn setup_with_validators(
10011000
min_gas_price: 100,
10021001
max_gas_price: 1_000_000_000,
10031002
total_supply: 1_000_000_000,
1004-
max_inflation_rate: Rational::from_integer(0),
10051003
gas_price_adjustment_rate: Rational::from_integer(0),
10061004
transaction_validity_period: tx_validity_period,
10071005
epoch_length,
@@ -1121,7 +1119,6 @@ impl ChainGenesis {
11211119
min_gas_price: 0,
11221120
max_gas_price: 1_000_000_000,
11231121
total_supply: 1_000_000_000,
1124-
max_inflation_rate: Rational::from_integer(0),
11251122
gas_price_adjustment_rate: Rational::from_integer(0),
11261123
transaction_validity_period: 100,
11271124
epoch_length: 5,

chain/chain/src/types.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ pub struct ChainGenesis {
192192
pub min_gas_price: Balance,
193193
pub max_gas_price: Balance,
194194
pub total_supply: Balance,
195-
pub max_inflation_rate: Rational,
196195
pub gas_price_adjustment_rate: Rational,
197196
pub transaction_validity_period: NumBlocks,
198197
pub epoch_length: BlockHeightDelta,
@@ -212,7 +211,6 @@ where
212211
min_gas_price: genesis_config.min_gas_price,
213212
max_gas_price: genesis_config.max_gas_price,
214213
total_supply: genesis_config.total_supply,
215-
max_inflation_rate: genesis_config.max_inflation_rate,
216214
gas_price_adjustment_rate: genesis_config.gas_price_adjustment_rate,
217215
transaction_validity_period: genesis_config.transaction_validity_period,
218216
epoch_length: genesis_config.epoch_length,

chain/client/src/test_utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ pub fn setup(
8686
min_gas_price: 100,
8787
max_gas_price: 1_000_000_000,
8888
total_supply: 3_000_000_000_000_000_000_000_000_000_000_000,
89-
max_inflation_rate: Rational::from_integer(0),
9089
gas_price_adjustment_rate: Rational::from_integer(0),
9190
transaction_validity_period,
9291
epoch_length,

chain/client/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl Message for GetChunk {
185185
}
186186

187187
/// Queries client for given path / data.
188-
#[derive(Deserialize, Clone)]
188+
#[derive(Deserialize, Clone, Debug)]
189189
pub struct Query {
190190
pub query_id: String,
191191
pub block_reference: BlockReference,

chain/epoch_manager/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub struct EpochManager {
4343
/// TODO: must be dynamically changing over time, so there should be a way to change it.
4444
config: EpochConfig,
4545
reward_calculator: RewardCalculator,
46+
/// Genesis protocol version. Useful when there are protocol upgrades.
47+
genesis_protocol_version: ProtocolVersion,
4648

4749
/// Cache of epoch information.
4850
epochs_info: SizedCache<EpochId, EpochInfo>,
@@ -69,6 +71,7 @@ impl EpochManager {
6971
store,
7072
config,
7173
reward_calculator,
74+
genesis_protocol_version,
7275
epochs_info: SizedCache::with_size(EPOCH_CACHE_SIZE),
7376
blocks_info: SizedCache::with_size(BLOCK_CACHE_SIZE),
7477
epoch_id_to_start: SizedCache::with_size(EPOCH_CACHE_SIZE),
@@ -311,6 +314,7 @@ impl EpochManager {
311314
} = self.collect_blocks_info(&block_info, last_block_hash)?;
312315
let epoch_id = self.get_epoch_id(last_block_hash)?;
313316
let epoch_info = self.get_epoch_info(&epoch_id)?;
317+
let epoch_protocol_version = epoch_info.protocol_version;
314318
let validator_stake = epoch_info
315319
.validators
316320
.clone()
@@ -323,6 +327,8 @@ impl EpochManager {
323327
validator_block_chunk_stats,
324328
&validator_stake,
325329
block_info.total_supply,
330+
epoch_protocol_version,
331+
self.genesis_protocol_version,
326332
);
327333
let next_next_epoch_info = match proposals_to_epoch_info(
328334
&self.config,
@@ -1849,7 +1855,7 @@ mod tests {
18491855
max_inflation_rate: Rational::new(5, 100),
18501856
num_blocks_per_year: 50,
18511857
epoch_length,
1852-
protocol_reward_percentage: Rational::new(1, 10),
1858+
protocol_reward_rate: Rational::new(1, 10),
18531859
protocol_treasury_account: "near".to_string(),
18541860
online_min_threshold: Rational::new(90, 100),
18551861
online_max_threshold: Rational::new(99, 100),
@@ -1910,6 +1916,8 @@ mod tests {
19101916
validator_online_ratio,
19111917
&validator_stakes,
19121918
total_supply,
1919+
PROTOCOL_VERSION,
1920+
PROTOCOL_VERSION,
19131921
);
19141922
let test2_reward = *validator_reward.get("test2").unwrap();
19151923
let protocol_reward = *validator_reward.get("near").unwrap();
@@ -1942,7 +1950,7 @@ mod tests {
19421950
max_inflation_rate: Rational::new(5, 100),
19431951
num_blocks_per_year: 50,
19441952
epoch_length,
1945-
protocol_reward_percentage: Rational::new(1, 10),
1953+
protocol_reward_rate: Rational::new(1, 10),
19461954
protocol_treasury_account: "near".to_string(),
19471955
online_min_threshold: Rational::new(90, 100),
19481956
online_max_threshold: Rational::new(99, 100),
@@ -2011,6 +2019,8 @@ mod tests {
20112019
validator_online_ratio,
20122020
&validators_stakes,
20132021
total_supply,
2022+
PROTOCOL_VERSION,
2023+
PROTOCOL_VERSION,
20142024
);
20152025
let test1_reward = *validator_reward.get("test1").unwrap();
20162026
let test2_reward = *validator_reward.get("test2").unwrap();
@@ -2054,7 +2064,7 @@ mod tests {
20542064
max_inflation_rate: Rational::new(5, 100),
20552065
num_blocks_per_year: 1_000_000,
20562066
epoch_length,
2057-
protocol_reward_percentage: Rational::new(1, 10),
2067+
protocol_reward_rate: Rational::new(1, 10),
20582068
protocol_treasury_account: "near".to_string(),
20592069
online_min_threshold: Rational::new(90, 100),
20602070
online_max_threshold: Rational::new(99, 100),
@@ -2116,6 +2126,8 @@ mod tests {
21162126
validator_online_ratio,
21172127
&validators_stakes,
21182128
total_supply,
2129+
PROTOCOL_VERSION,
2130+
PROTOCOL_VERSION,
21192131
);
21202132
let test2_reward = *validator_reward.get("test2").unwrap();
21212133
let protocol_reward = *validator_reward.get("near").unwrap();

chain/epoch_manager/src/reward_calculator.rs

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use num_rational::Rational;
44
use primitive_types::U256;
55

66
use near_primitives::types::{AccountId, Balance, BlockChunkValidatorStats};
7+
use near_primitives::version::{ProtocolVersion, ENABLE_INFLATION_PROTOCOL_VERSION};
78

89
#[derive(Clone, Debug)]
910
pub 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 {
94103
mod 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
}

chain/epoch_manager/src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub fn default_reward_calculator() -> RewardCalculator {
158158
max_inflation_rate: Rational::from_integer(0),
159159
num_blocks_per_year: 1,
160160
epoch_length: 1,
161-
protocol_reward_percentage: Rational::from_integer(0),
161+
protocol_reward_rate: Rational::from_integer(0),
162162
protocol_treasury_account: "near".to_string(),
163163
online_min_threshold: Rational::new(90, 100),
164164
online_max_threshold: Rational::new(99, 100),

chain/network/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ delay-detector = { path = "../../tools/delay_detector", optional = true}
3636
near-logger-utils = {path = "../../test-utils/logger"}
3737
tempfile = "3"
3838
bencher = "0.1.5"
39-
num-rational = "0.2.4"
4039

4140
near-client = { path = "../client" }
4241
near-telemetry = { path = "../telemetry" }

chain/network/tests/runner/mod.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ use near_network::{
2525
};
2626
use near_primitives::types::{AccountId, ValidatorId};
2727
use near_primitives::validator_signer::InMemoryValidatorSigner;
28-
use near_primitives::version::PROTOCOL_VERSION;
2928
use near_store::test_utils::create_test_store;
3029
use near_telemetry::{TelemetryActor, TelemetryConfig};
31-
use num_rational::Rational;
3230

3331
pub type SharedRunningInfo = Arc<RwLock<RunningInfo>>;
3432

@@ -59,19 +57,8 @@ pub fn setup_network_node(
5957
account_id.as_str(),
6058
));
6159
let telemetry_actor = TelemetryActor::new(TelemetryConfig::default()).start();
62-
let chain_genesis = ChainGenesis {
63-
time: genesis_time,
64-
height: 0,
65-
gas_limit: 1_000_000,
66-
min_gas_price: 100,
67-
max_gas_price: 1_000_000_000,
68-
total_supply: 1_000_000_000,
69-
max_inflation_rate: Rational::from_integer(0),
70-
gas_price_adjustment_rate: Rational::from_integer(0),
71-
transaction_validity_period: 1000,
72-
epoch_length: 5,
73-
protocol_version: PROTOCOL_VERSION,
74-
};
60+
let mut chain_genesis = ChainGenesis::test();
61+
chain_genesis.time = genesis_time;
7562

7663
let peer_manager = PeerManagerActor::create(move |ctx| {
7764
let mut client_config = ClientConfig::test(false, 100, 200, num_validators, false);

0 commit comments

Comments
 (0)