2019-07-31 14:28:14 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate solana_bpf_loader_program;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate solana_budget_program;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate solana_exchange_program;
|
|
|
|
#[macro_use]
|
2019-10-17 10:37:08 -07:00
|
|
|
extern crate solana_vest_program;
|
2019-07-31 14:28:14 -07:00
|
|
|
|
2019-10-23 11:50:10 -07:00
|
|
|
use log::*;
|
2020-08-24 17:27:40 +00:00
|
|
|
use solana_runtime::{
|
|
|
|
bank::{Bank, EnteredEpochCallback},
|
|
|
|
message_processor::{DEFAULT_COMPUTE_BUDGET, DEFAULT_MAX_INVOKE_DEPTH},
|
|
|
|
};
|
2020-08-22 01:54:50 +00:00
|
|
|
use solana_sdk::{
|
|
|
|
clock::Epoch, entrypoint_native::ProcessInstructionWithContext, genesis_config::OperatingMode,
|
|
|
|
inflation::Inflation, pubkey::Pubkey,
|
|
|
|
};
|
2019-10-23 11:50:10 -07:00
|
|
|
|
2019-11-07 18:33:14 -08:00
|
|
|
pub fn get_inflation(operating_mode: OperatingMode, epoch: Epoch) -> Option<Inflation> {
|
|
|
|
match operating_mode {
|
2020-07-15 18:36:09 +09:00
|
|
|
OperatingMode::Development => match epoch {
|
|
|
|
0 => Some(Inflation::default()),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
OperatingMode::Preview => match epoch {
|
|
|
|
// No inflation at epoch 0
|
|
|
|
0 => Some(Inflation::new_disabled()),
|
|
|
|
// testnet enabled inflation at epoch 44:
|
2020-07-21 14:37:19 +09:00
|
|
|
// https://github.com/solana-labs/solana/commit/d8e885f4259e6c7db420cce513cb34ebf961073d
|
2020-07-15 18:36:09 +09:00
|
|
|
44 => Some(Inflation::default()),
|
|
|
|
// Completely disable inflation prior to ship the inflation fix at epoch 68
|
|
|
|
68 => Some(Inflation::new_disabled()),
|
2020-07-21 14:37:19 +09:00
|
|
|
// Enable again after the inflation fix has landed:
|
|
|
|
// https://github.com/solana-labs/solana/commit/7cc2a6801bed29a816ef509cfc26a6f2522e46ff
|
2020-07-22 13:54:17 -06:00
|
|
|
74 => Some(Inflation::default()),
|
2020-07-15 18:36:09 +09:00
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
OperatingMode::Stable => match epoch {
|
|
|
|
// No inflation at epoch 0
|
|
|
|
0 => Some(Inflation::new_disabled()),
|
|
|
|
// Inflation starts
|
|
|
|
// The epoch of Epoch::MAX is a placeholder and is expected to be reduced in
|
|
|
|
// a future hard fork.
|
|
|
|
Epoch::MAX => Some(Inflation::default()),
|
|
|
|
_ => None,
|
|
|
|
},
|
2019-11-07 18:33:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 01:54:50 +00:00
|
|
|
enum Program {
|
|
|
|
Native((String, Pubkey)),
|
|
|
|
BuiltinLoader((String, Pubkey, ProcessInstructionWithContext)),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_programs(operating_mode: OperatingMode, epoch: Epoch) -> Option<Vec<Program>> {
|
2019-10-23 11:50:10 -07:00
|
|
|
match operating_mode {
|
|
|
|
OperatingMode::Development => {
|
|
|
|
if epoch == 0 {
|
|
|
|
Some(vec![
|
2020-08-22 01:54:50 +00:00
|
|
|
Program::BuiltinLoader(solana_bpf_loader_program!()),
|
|
|
|
Program::Native(solana_vest_program!()),
|
|
|
|
Program::Native(solana_budget_program!()),
|
|
|
|
Program::Native(solana_exchange_program!()),
|
2019-10-23 11:50:10 -07:00
|
|
|
])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 17:48:58 -07:00
|
|
|
OperatingMode::Stable => {
|
2020-08-22 01:54:50 +00:00
|
|
|
if epoch == std::u64::MAX {
|
|
|
|
// The epoch of std::u64::MAX is a placeholder and is expected
|
|
|
|
// to be reduced in a future network update.
|
|
|
|
Some(vec![
|
|
|
|
Program::BuiltinLoader(solana_bpf_loader_program!()),
|
|
|
|
Program::Native(solana_vest_program!()),
|
|
|
|
])
|
2020-02-19 17:48:58 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OperatingMode::Preview => {
|
2020-08-22 01:54:50 +00:00
|
|
|
if epoch == std::u64::MAX {
|
|
|
|
// The epoch of std::u64::MAX is a placeholder and is expected
|
|
|
|
// to be reduced in a future network update.
|
|
|
|
Some(vec![
|
|
|
|
Program::BuiltinLoader(solana_bpf_loader_program!()),
|
|
|
|
Program::Native(solana_vest_program!()),
|
|
|
|
])
|
2019-10-23 11:50:10 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 01:54:50 +00:00
|
|
|
pub fn get_native_programs(
|
|
|
|
operating_mode: OperatingMode,
|
|
|
|
epoch: Epoch,
|
|
|
|
) -> Option<Vec<(String, Pubkey)>> {
|
|
|
|
match get_programs(operating_mode, epoch) {
|
|
|
|
Some(programs) => {
|
|
|
|
let mut native_programs = vec![];
|
|
|
|
for program in programs {
|
|
|
|
if let Program::Native((string, key)) = program {
|
|
|
|
native_programs.push((string, key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(native_programs)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 11:50:10 -07:00
|
|
|
pub fn get_entered_epoch_callback(operating_mode: OperatingMode) -> EnteredEpochCallback {
|
|
|
|
Box::new(move |bank: &mut Bank| {
|
2019-11-07 18:33:14 -08:00
|
|
|
if let Some(inflation) = get_inflation(operating_mode, bank.epoch()) {
|
2020-07-15 18:36:09 +09:00
|
|
|
info!("Entering new epoch with inflation {:?}", inflation);
|
2019-11-07 18:33:14 -08:00
|
|
|
bank.set_inflation(inflation);
|
|
|
|
}
|
2020-08-22 01:54:50 +00:00
|
|
|
if let Some(programs) = get_programs(operating_mode, bank.epoch()) {
|
|
|
|
for program in programs {
|
|
|
|
match program {
|
|
|
|
Program::Native((name, program_id)) => {
|
|
|
|
bank.add_native_program(&name, &program_id);
|
|
|
|
}
|
|
|
|
Program::BuiltinLoader((
|
|
|
|
name,
|
|
|
|
program_id,
|
|
|
|
process_instruction_with_context,
|
|
|
|
)) => {
|
|
|
|
bank.add_builtin_loader(
|
|
|
|
&name,
|
|
|
|
program_id,
|
|
|
|
process_instruction_with_context,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 11:50:10 -07:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 21:30:23 -07:00
|
|
|
if OperatingMode::Stable == operating_mode {
|
2020-08-05 22:04:26 -07:00
|
|
|
bank.set_cross_program_support(bank.epoch() >= 63);
|
2020-08-05 21:30:23 -07:00
|
|
|
} else {
|
|
|
|
bank.set_cross_program_support(true);
|
|
|
|
}
|
2020-08-24 17:27:40 +00:00
|
|
|
|
|
|
|
bank.set_max_invoke_depth(DEFAULT_MAX_INVOKE_DEPTH);
|
|
|
|
bank.set_compute_budget(DEFAULT_COMPUTE_BUDGET);
|
2019-10-23 11:50:10 -07:00
|
|
|
})
|
2019-07-31 14:28:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-10-23 11:50:10 -07:00
|
|
|
use super::*;
|
2019-08-12 16:46:49 -06:00
|
|
|
use std::collections::HashSet;
|
2019-07-31 14:28:14 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_id_uniqueness() {
|
|
|
|
let mut unique = HashSet::new();
|
2020-08-22 01:54:50 +00:00
|
|
|
let programs = get_programs(OperatingMode::Development, 0).unwrap();
|
|
|
|
for program in programs {
|
|
|
|
match program {
|
|
|
|
Program::Native((name, id)) => assert!(unique.insert((name, id))),
|
|
|
|
Program::BuiltinLoader((name, id, _)) => assert!(unique.insert((name, id))),
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 14:28:14 -07:00
|
|
|
}
|
2019-10-23 11:50:10 -07:00
|
|
|
|
2019-11-07 18:33:14 -08:00
|
|
|
#[test]
|
|
|
|
fn test_development_inflation() {
|
|
|
|
assert_eq!(
|
|
|
|
get_inflation(OperatingMode::Development, 0).unwrap(),
|
|
|
|
Inflation::default()
|
|
|
|
);
|
|
|
|
assert_eq!(get_inflation(OperatingMode::Development, 1), None);
|
|
|
|
}
|
|
|
|
|
2019-10-23 11:50:10 -07:00
|
|
|
#[test]
|
|
|
|
fn test_development_programs() {
|
2019-11-07 18:33:14 -08:00
|
|
|
assert_eq!(
|
|
|
|
get_programs(OperatingMode::Development, 0).unwrap().len(),
|
2020-07-23 15:08:59 -06:00
|
|
|
4
|
2019-11-07 18:33:14 -08:00
|
|
|
);
|
2020-08-22 01:54:50 +00:00
|
|
|
assert!(get_programs(OperatingMode::Development, 1).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_native_development_programs() {
|
|
|
|
assert_eq!(
|
|
|
|
get_native_programs(OperatingMode::Development, 0)
|
|
|
|
.unwrap()
|
|
|
|
.len(),
|
|
|
|
3
|
|
|
|
);
|
|
|
|
assert!(get_native_programs(OperatingMode::Development, 1).is_none());
|
2019-11-07 18:33:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_softlaunch_inflation() {
|
|
|
|
assert_eq!(
|
2020-02-19 17:48:58 -07:00
|
|
|
get_inflation(OperatingMode::Stable, 0).unwrap(),
|
2019-11-07 18:33:14 -08:00
|
|
|
Inflation::new_disabled()
|
|
|
|
);
|
2020-02-19 17:48:58 -07:00
|
|
|
assert_eq!(get_inflation(OperatingMode::Stable, 1), None);
|
2019-11-07 18:33:14 -08:00
|
|
|
assert_eq!(
|
2020-02-19 17:48:58 -07:00
|
|
|
get_inflation(OperatingMode::Stable, std::u64::MAX).unwrap(),
|
2019-11-07 18:33:14 -08:00
|
|
|
Inflation::default()
|
|
|
|
);
|
2019-10-23 11:50:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_softlaunch_programs() {
|
2020-08-22 01:54:50 +00:00
|
|
|
assert!(get_programs(OperatingMode::Stable, 1).is_none());
|
2020-02-19 17:48:58 -07:00
|
|
|
assert!(get_programs(OperatingMode::Stable, std::u64::MAX).is_some());
|
2019-10-23 11:50:10 -07:00
|
|
|
}
|
2019-07-31 14:28:14 -07:00
|
|
|
}
|