From 4d31bc94a6b409849d998872f8a7696ed8fad5b7 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Tue, 2 Jun 2020 17:34:12 -0700 Subject: [PATCH] Permit builtin programs to be swapped out at epoch boundaries --- runtime/src/bank.rs | 16 +++++++++-- runtime/src/builtin_programs.rs | 47 ++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 13c6425f60976e..1b3434a1c05dbe 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -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, ProcessInstruction}, nonce_utils, @@ -456,6 +456,18 @@ impl Bank { { entered_epoch_callback(&mut new) } + + if let Some(builtin_programs) = + get_epoch_activated_builtin_programs(self.operating_mode(), self.epoch) + { + for program in builtin_programs.iter() { + self.add_builtin_program( + &program.name, + program.id, + program.process_instruction, + ); + } + } } new.update_epoch_stakes(leader_schedule_epoch); @@ -2048,7 +2060,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); } diff --git a/runtime/src/builtin_programs.rs b/runtime/src/builtin_programs.rs index 180854990267ce..a5a2fe82764130 100644 --- a/runtime/src/builtin_programs.rs +++ b/runtime/src/builtin_programs.rs @@ -1,5 +1,5 @@ use crate::{message_processor::ProcessInstruction, system_instruction_processor}; -use solana_sdk::{pubkey::Pubkey, system_program}; +use solana_sdk::{clock::Epoch, genesis_config::OperatingMode, pubkey::Pubkey, system_program}; pub struct BuiltinProgram { pub name: String, @@ -16,13 +16,30 @@ impl BuiltinProgram { } } -pub fn get_builtin_programs() -> Vec { +fn new_system_program_activation_epoch(operating_mode: OperatingMode) -> Epoch { + match operating_mode { + OperatingMode::Development => 0, + OperatingMode::Preview => std::u64::MAX / 2, + OperatingMode::Stable => std::u64::MAX / 2, + } +} + +/// All builtin programs that should be active at the given (operating_mode, epoch) +pub fn get_builtin_programs(operating_mode: OperatingMode, epoch: Epoch) -> Vec { 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(), + system_instruction_processor::process_instruction, // TODO: This is the OLD system instruction processor + ) + } else { + BuiltinProgram::new( + "system_program", + system_program::id(), + system_instruction_processor::process_instruction, // TODO: This should be the NEW system instruction processor + ) + }, BuiltinProgram::new( "config_program", solana_config_program::id(), @@ -40,3 +57,19 @@ pub fn get_builtin_programs() -> Vec { ), ] } + +/// Builtin programs that activate at the given (operating_mode, epoch) +pub fn get_epoch_activated_builtin_programs( + operating_mode: OperatingMode, + epoch: Epoch, +) -> Option> { + if epoch == new_system_program_activation_epoch(operating_mode) { + Some(vec![BuiltinProgram::new( + "system_program", + system_program::id(), + system_instruction_processor::process_instruction, // TODO: This should be the NEW system instruction processor + )]) + } else { + None + } +}