Skip to content

Commit

Permalink
Enable rolling update of "Permit paying oneself" / "No longer allow c…
Browse files Browse the repository at this point in the history
…reate-account to add funds to an existing account" (bp #10375) (#10404)

automerge
  • Loading branch information
mergify[bot] authored Jun 4, 2020
1 parent 56e8319 commit 6a72dab
Show file tree
Hide file tree
Showing 4 changed files with 1,608 additions and 10 deletions.
84 changes: 82 additions & 2 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
accounts_db::{ErrorCounters, SnapshotStorages},
accounts_index::Ancestors,
blockhash_queue::BlockhashQueue,
builtin_programs::get_builtin_programs,
builtin_programs::{get_builtin_programs, get_epoch_activated_builtin_programs},
epoch_stakes::{EpochStakes, NodeVoteAccounts},
message_processor::MessageProcessor,
nonce_utils,
Expand Down Expand Up @@ -457,6 +457,14 @@ impl Bank {
{
entered_epoch_callback(&mut new)
}

if let Some(builtin_programs) =
get_epoch_activated_builtin_programs(new.operating_mode(), new.epoch)
{
for program in builtin_programs.iter() {
new.add_builtin_program(&program.name, program.id, program.process_instruction);
}
}
}

new.update_epoch_stakes(leader_schedule_epoch);
Expand Down Expand Up @@ -2049,7 +2057,7 @@ impl Bank {
}

pub fn finish_init(&mut self) {
let builtin_programs = get_builtin_programs();
let builtin_programs = get_builtin_programs(self.operating_mode(), self.epoch);
for program in builtin_programs.iter() {
self.add_builtin_program(&program.name, program.id, program.process_instruction);
}
Expand Down Expand Up @@ -2552,6 +2560,7 @@ mod tests {
use super::*;
use crate::{
accounts_index::Ancestors,
builtin_programs::new_system_program_activation_epoch,
genesis_utils::{
create_genesis_config_with_leader, GenesisConfigInfo, BOOTSTRAP_VALIDATOR_LAMPORTS,
},
Expand Down Expand Up @@ -6589,6 +6598,77 @@ mod tests {
assert_eq!(bank.capitalization(), pre_capitalization - burn_amount);
}

#[test]
fn test_legacy_system_instruction_processor0_stable() {
let (mut genesis_config, mint_keypair) = create_genesis_config(1_000_000);
genesis_config.operating_mode = OperatingMode::Stable;
let bank0 = Arc::new(Bank::new(&genesis_config));

let activation_epoch = new_system_program_activation_epoch(bank0.operating_mode());
assert!(activation_epoch > bank0.epoch());

// Transfer to self is not supported by legacy_system_instruction_processor0
bank0
.transfer(1, &mint_keypair, &mint_keypair.pubkey())
.unwrap_err();

// Activate system_instruction_processor
let bank = Bank::new_from_parent(
&bank0,
&Pubkey::default(),
genesis_config
.epoch_schedule
.get_first_slot_in_epoch(activation_epoch),
);

// Transfer to self is supported by system_instruction_processor
bank.transfer(2, &mint_keypair, &mint_keypair.pubkey())
.unwrap();
}

#[test]
fn test_legacy_system_instruction_processor0_preview() {
let (mut genesis_config, mint_keypair) = create_genesis_config(1_000_000);
genesis_config.operating_mode = OperatingMode::Preview;
let bank0 = Arc::new(Bank::new(&genesis_config));

let activation_epoch = new_system_program_activation_epoch(bank0.operating_mode());
assert!(activation_epoch > bank0.epoch());

// Transfer to self is not supported by legacy_system_instruction_processor0
bank0
.transfer(1, &mint_keypair, &mint_keypair.pubkey())
.unwrap_err();

// Activate system_instruction_processor
let bank = Bank::new_from_parent(
&bank0,
&Pubkey::default(),
genesis_config
.epoch_schedule
.get_first_slot_in_epoch(activation_epoch),
);

// Transfer to self is supported by system_instruction_processor
bank.transfer(2, &mint_keypair, &mint_keypair.pubkey())
.unwrap();
}

#[test]
fn test_legacy_system_instruction_processor0_development() {
let (mut genesis_config, mint_keypair) = create_genesis_config(1_000_000);
genesis_config.operating_mode = OperatingMode::Development;
let bank0 = Arc::new(Bank::new(&genesis_config));

let activation_epoch = new_system_program_activation_epoch(bank0.operating_mode());
assert!(activation_epoch == bank0.epoch());

// Transfer to self is supported by system_instruction_processor
bank0
.transfer(2, &mint_keypair, &mint_keypair.pubkey())
.unwrap();
}

#[test]
fn test_duplicate_account_key() {
solana_logger::setup();
Expand Down
52 changes: 44 additions & 8 deletions runtime/src/builtin_programs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::system_instruction_processor;
use solana_sdk::{entrypoint_native::ProcessInstruction, pubkey::Pubkey, system_program};
use crate::{legacy_system_instruction_processor0, system_instruction_processor};
use solana_sdk::{
clock::Epoch, entrypoint_native::ProcessInstruction, genesis_config::OperatingMode,
pubkey::Pubkey, system_program,
};

pub struct BuiltinProgram {
pub name: String,
Expand All @@ -16,13 +19,30 @@ impl BuiltinProgram {
}
}

pub fn get_builtin_programs() -> Vec<BuiltinProgram> {
pub(crate) fn new_system_program_activation_epoch(operating_mode: OperatingMode) -> Epoch {
match operating_mode {
OperatingMode::Development => 0,
OperatingMode::Preview => 1_000_000,
OperatingMode::Stable => 1_000_000,
}
}

/// All builtin programs that should be active at the given (operating_mode, epoch)
pub fn get_builtin_programs(operating_mode: OperatingMode, epoch: Epoch) -> Vec<BuiltinProgram> {
vec![
BuiltinProgram::new(
"system_program",
system_program::id(),
system_instruction_processor::process_instruction,
),
if epoch < new_system_program_activation_epoch(operating_mode) {
BuiltinProgram::new(
"system_program",
system_program::id(),
legacy_system_instruction_processor0::process_instruction,
)
} else {
BuiltinProgram::new(
"system_program",
system_program::id(),
system_instruction_processor::process_instruction,
)
},
BuiltinProgram::new(
"config_program",
solana_config_program::id(),
Expand All @@ -40,3 +60,19 @@ pub fn get_builtin_programs() -> Vec<BuiltinProgram> {
),
]
}

/// Builtin programs that activate at the given (operating_mode, epoch)
pub fn get_epoch_activated_builtin_programs(
operating_mode: OperatingMode,
epoch: Epoch,
) -> Option<Vec<BuiltinProgram>> {
if epoch == new_system_program_activation_epoch(operating_mode) {
Some(vec![BuiltinProgram::new(
"system_program",
system_program::id(),
system_instruction_processor::process_instruction,
)])
} else {
None
}
}
Loading

0 comments on commit 6a72dab

Please sign in to comment.