Refactor: Add `RuntimeConfig` field to Bank (#26946)

* Refactor: Simplify arguments for bank constructor methods

* Refactor: Add RuntimeConfig to Bank fields

* Arc wrap runtime_config

* Arc wrap all runtime config usages

* Remove Copy trait derivation from RuntimeConfig

* Remove some arc wrapping
This commit is contained in:
Justin Starry 2022-08-05 21:49:00 +02:00 committed by GitHub
parent a9a3c62907
commit 69598ed4c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 114 additions and 113 deletions

View File

@ -20,6 +20,7 @@ use {
bank::{Bank, BankSlotDelta},
bank_forks::BankForks,
genesis_utils::{create_genesis_config_with_leader, GenesisConfigInfo},
runtime_config::RuntimeConfig,
snapshot_archive_info::FullSnapshotArchiveInfo,
snapshot_config::SnapshotConfig,
snapshot_package::{
@ -94,14 +95,11 @@ impl SnapshotTestConfig {
genesis_config_info.genesis_config.cluster_type = cluster_type;
let bank0 = Bank::new_with_paths_for_tests(
&genesis_config_info.genesis_config,
Arc::<RuntimeConfig>::default(),
vec![accounts_dir.path().to_path_buf()],
None,
None,
AccountSecondaryIndexes::default(),
false,
accounts_db::AccountShrinkThreshold::default(),
false,
None,
);
bank0.freeze();
let mut bank_forks = BankForks::new(bank0);
@ -165,6 +163,7 @@ fn restore_from_snapshot(
&full_snapshot_archive_info,
None,
old_genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -831,6 +830,7 @@ fn restore_from_snapshots_and_check_banks_are_equal(
&snapshot_config.incremental_snapshot_archives_dir,
&[accounts_dir],
genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -1022,6 +1022,7 @@ fn test_snapshots_with_background_services(
.incremental_snapshot_archives_dir,
&[snapshot_test_config.accounts_dir.as_ref().to_path_buf()],
&snapshot_test_config.genesis_config_info.genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),

View File

@ -193,13 +193,14 @@ fn bank_forks_from_snapshot(
process::exit(1);
}
let (mut deserialized_bank, full_snapshot_archive_info, incremental_snapshot_archive_info) =
let (deserialized_bank, full_snapshot_archive_info, incremental_snapshot_archive_info) =
snapshot_utils::bank_from_latest_snapshot_archives(
&snapshot_config.bank_snapshots_dir,
&snapshot_config.full_snapshot_archives_dir,
&snapshot_config.incremental_snapshot_archives_dir,
&account_paths,
genesis_config,
&process_options.runtime_config,
process_options.debug_keys.clone(),
Some(&crate::builtins::get(
process_options.runtime_config.bpf_jit,
@ -220,8 +221,6 @@ fn bank_forks_from_snapshot(
deserialized_bank.set_shrink_paths(shrink_paths);
}
deserialized_bank.set_compute_budget(process_options.runtime_config.compute_budget);
let full_snapshot_hash = FullSnapshotHash {
hash: (
full_snapshot_archive_info.slot(),

View File

@ -744,8 +744,9 @@ pub(crate) fn process_blockstore_for_bank_0(
accounts_update_notifier: Option<AccountsUpdateNotifier>,
) -> Arc<RwLock<BankForks>> {
// Setup bank for slot 0
let mut bank0 = Bank::new_with_paths(
let bank0 = Bank::new_with_paths(
genesis_config,
Arc::new(opts.runtime_config.clone()),
account_paths,
opts.debug_keys.clone(),
Some(&crate::builtins::get(opts.runtime_config.bpf_jit)),
@ -756,7 +757,6 @@ pub(crate) fn process_blockstore_for_bank_0(
opts.accounts_db_config.clone(),
accounts_update_notifier,
);
bank0.set_compute_budget(opts.runtime_config.compute_budget);
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank0)));
info!("Processing ledger for slot 0...");
@ -3605,14 +3605,11 @@ pub mod tests {
) -> EpochSchedule {
let bank = Bank::new_with_paths_for_tests(
genesis_config,
Arc::<RuntimeConfig>::default(),
account_paths,
None,
None,
AccountSecondaryIndexes::default(),
false,
AccountShrinkThreshold::default(),
false,
None,
);
*bank.epoch_schedule()
}

View File

@ -20,6 +20,7 @@ use {
builtins::Builtin,
commitment::BlockCommitmentCache,
genesis_utils::{create_genesis_config_with_leader_ex, GenesisConfigInfo},
runtime_config::RuntimeConfig,
},
solana_sdk::{
account::{Account, AccountSharedData, ReadableAccount},
@ -781,7 +782,17 @@ impl ProgramTest {
debug!("Payer address: {}", mint_keypair.pubkey());
debug!("Genesis config: {}", genesis_config);
let mut bank = Bank::new_for_tests(&genesis_config);
let mut bank = Bank::new_with_runtime_config_for_tests(
&genesis_config,
Arc::new(RuntimeConfig {
bpf_jit: self.use_bpf_jit,
compute_budget: self.compute_max_units.map(|max_units| ComputeBudget {
compute_unit_limit: max_units,
..ComputeBudget::default()
}),
..RuntimeConfig::default()
}),
);
// Add loaders
macro_rules! add_builtin {
@ -819,12 +830,6 @@ impl ProgramTest {
bank.store_account(address, account);
}
bank.set_capitalization();
if let Some(max_units) = self.compute_max_units {
bank.set_compute_budget(Some(ComputeBudget {
compute_unit_limit: max_units,
..ComputeBudget::default()
}));
}
// Advance beyond slot 0 for a slightly more realistic test environment
let bank = {
let bank = Arc::new(bank);

View File

@ -48,16 +48,7 @@ fn deposit_many(bank: &Bank, pubkeys: &mut Vec<Pubkey>, num: usize) -> Result<()
#[bench]
fn test_accounts_create(bencher: &mut Bencher) {
let (genesis_config, _) = create_genesis_config(10_000);
let bank0 = Bank::new_with_paths_for_benches(
&genesis_config,
vec![PathBuf::from("bench_a0")],
None,
None,
AccountSecondaryIndexes::default(),
false,
AccountShrinkThreshold::default(),
false,
);
let bank0 = Bank::new_with_paths_for_benches(&genesis_config, vec![PathBuf::from("bench_a0")]);
bencher.iter(|| {
let mut pubkeys: Vec<Pubkey> = vec![];
deposit_many(&bank0, &mut pubkeys, 1000).unwrap();
@ -71,12 +62,6 @@ fn test_accounts_squash(bencher: &mut Bencher) {
let mut prev_bank = Arc::new(Bank::new_with_paths_for_benches(
&genesis_config,
vec![PathBuf::from("bench_a1")],
None,
None,
AccountSecondaryIndexes::default(),
false,
AccountShrinkThreshold::default(),
false,
));
let mut pubkeys: Vec<Pubkey> = vec![];
deposit_many(&prev_bank, &mut pubkeys, 250_000).unwrap();

View File

@ -58,6 +58,7 @@ use {
inline_spl_associated_token_account, inline_spl_token,
message_processor::MessageProcessor,
rent_collector::{CollectedInfo, RentCollector},
runtime_config::RuntimeConfig,
stake_account::{self, StakeAccount},
stake_weighted_timestamp::{
calculate_stake_weighted_timestamp, MaxAllowableDrift,
@ -1062,7 +1063,7 @@ impl PartialEq for Bank {
is_delta,
// TODO: Confirm if all these fields are intentionally ignored!
builtin_programs: _,
compute_budget: _,
runtime_config: _,
builtin_feature_transitions: _,
rewards: _,
cluster_type: _,
@ -1282,7 +1283,8 @@ pub struct Bank {
/// The builtin programs
builtin_programs: BuiltinPrograms,
compute_budget: Option<ComputeBudget>,
/// Optional config parameters that can override runtime behavior
runtime_config: Arc<RuntimeConfig>,
/// Dynamic feature transitions for builtin programs
#[allow(clippy::rc_buffer)]
@ -1414,16 +1416,7 @@ impl Bank {
}
pub fn new_for_benches(genesis_config: &GenesisConfig) -> Self {
Self::new_with_paths_for_benches(
genesis_config,
Vec::new(),
None,
None,
AccountSecondaryIndexes::default(),
false,
AccountShrinkThreshold::default(),
false,
)
Self::new_with_paths_for_benches(genesis_config, Vec::new())
}
pub fn new_for_tests(genesis_config: &GenesisConfig) -> Self {
@ -1435,6 +1428,20 @@ impl Bank {
)
}
pub fn new_with_runtime_config_for_tests(
genesis_config: &GenesisConfig,
runtime_config: Arc<RuntimeConfig>,
) -> Self {
Self::new_with_paths_for_tests(
genesis_config,
runtime_config,
Vec::new(),
AccountSecondaryIndexes::default(),
false,
AccountShrinkThreshold::default(),
)
}
pub fn new_no_wallclock_throttle_for_tests(genesis_config: &GenesisConfig) -> Self {
let mut bank = Self::new_for_tests(genesis_config);
@ -1450,14 +1457,11 @@ impl Bank {
) -> Self {
Self::new_with_paths_for_tests(
genesis_config,
Arc::<RuntimeConfig>::default(),
Vec::new(),
None,
None,
account_indexes,
accounts_db_caching_enabled,
shrink_ratio,
false,
None,
)
}
@ -1501,7 +1505,7 @@ impl Bank {
epoch_stakes: HashMap::<Epoch, EpochStakes>::default(),
is_delta: AtomicBool::default(),
builtin_programs: BuiltinPrograms::default(),
compute_budget: Option::<ComputeBudget>::default(),
runtime_config: Arc::<RuntimeConfig>::default(),
builtin_feature_transitions: Arc::<Vec<BuiltinFeatureTransition>>::default(),
rewards: RwLock::<Vec<(Pubkey, RewardInfo)>>::default(),
cluster_type: Option::<ClusterType>::default(),
@ -1532,48 +1536,38 @@ impl Bank {
pub fn new_with_paths_for_tests(
genesis_config: &GenesisConfig,
runtime_config: Arc<RuntimeConfig>,
paths: Vec<PathBuf>,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
additional_builtins: Option<&Builtins>,
account_indexes: AccountSecondaryIndexes,
accounts_db_caching_enabled: bool,
shrink_ratio: AccountShrinkThreshold,
debug_do_not_add_builtins: bool,
accounts_db_config: Option<AccountsDbConfig>,
) -> Self {
Self::new_with_paths(
genesis_config,
runtime_config,
paths,
debug_keys,
additional_builtins,
None,
None,
account_indexes,
accounts_db_caching_enabled,
shrink_ratio,
debug_do_not_add_builtins,
accounts_db_config.or(Some(ACCOUNTS_DB_CONFIG_FOR_TESTING)),
false,
Some(ACCOUNTS_DB_CONFIG_FOR_TESTING),
None,
)
}
pub fn new_with_paths_for_benches(
genesis_config: &GenesisConfig,
paths: Vec<PathBuf>,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
additional_builtins: Option<&Builtins>,
account_indexes: AccountSecondaryIndexes,
accounts_db_caching_enabled: bool,
shrink_ratio: AccountShrinkThreshold,
debug_do_not_add_builtins: bool,
) -> Self {
pub fn new_with_paths_for_benches(genesis_config: &GenesisConfig, paths: Vec<PathBuf>) -> Self {
Self::new_with_paths(
genesis_config,
Arc::<RuntimeConfig>::default(),
paths,
debug_keys,
additional_builtins,
account_indexes,
accounts_db_caching_enabled,
shrink_ratio,
debug_do_not_add_builtins,
None,
None,
AccountSecondaryIndexes::default(),
false,
AccountShrinkThreshold::default(),
false,
Some(ACCOUNTS_DB_CONFIG_FOR_BENCHMARKS),
None,
)
@ -1582,6 +1576,7 @@ impl Bank {
#[allow(clippy::too_many_arguments)]
pub fn new_with_paths(
genesis_config: &GenesisConfig,
runtime_config: Arc<RuntimeConfig>,
paths: Vec<PathBuf>,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
additional_builtins: Option<&Builtins>,
@ -1604,6 +1599,7 @@ impl Bank {
let mut bank = Self::default_with_accounts(accounts);
bank.ancestors = Ancestors::from(vec![bank.slot()]);
bank.transaction_debug_keys = debug_keys;
bank.runtime_config = runtime_config;
bank.cluster_type = Some(genesis_config.cluster_type);
bank.process_genesis_config(genesis_config);
@ -1811,7 +1807,7 @@ impl Bank {
tick_height: AtomicU64::new(parent.tick_height.load(Relaxed)),
signature_count: AtomicU64::new(0),
builtin_programs,
compute_budget: parent.compute_budget,
runtime_config: parent.runtime_config.clone(),
builtin_feature_transitions: parent.builtin_feature_transitions.clone(),
hard_forks: parent.hard_forks.clone(),
rewards: RwLock::new(vec![]),
@ -2102,6 +2098,7 @@ impl Bank {
pub(crate) fn new_from_fields(
bank_rc: BankRc,
genesis_config: &GenesisConfig,
runtime_config: Arc<RuntimeConfig>,
fields: BankFieldsToDeserialize,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
additional_builtins: Option<&Builtins>,
@ -2169,7 +2166,7 @@ impl Bank {
epoch_stakes: fields.epoch_stakes,
is_delta: AtomicBool::new(fields.is_delta),
builtin_programs: new(),
compute_budget: None,
runtime_config,
builtin_feature_transitions: new(),
rewards: new(),
cluster_type: Some(genesis_config.cluster_type),
@ -4519,31 +4516,32 @@ impl Bank {
feature_set_clone_time.as_us()
);
let compute_budget = if let Some(compute_budget) = self.compute_budget {
compute_budget
} else {
let mut compute_budget =
ComputeBudget::new(compute_budget::MAX_COMPUTE_UNIT_LIMIT as u64);
let compute_budget =
if let Some(compute_budget) = self.runtime_config.compute_budget {
compute_budget
} else {
let mut compute_budget =
ComputeBudget::new(compute_budget::MAX_COMPUTE_UNIT_LIMIT as u64);
let mut compute_budget_process_transaction_time =
Measure::start("compute_budget_process_transaction_time");
let process_transaction_result = compute_budget.process_instructions(
tx.message().program_instructions_iter(),
feature_set.is_active(&default_units_per_instruction::id()),
feature_set.is_active(&add_set_compute_unit_price_ix::id()),
);
compute_budget_process_transaction_time.stop();
saturating_add_assign!(
timings
.execute_accessories
.compute_budget_process_transaction_us,
compute_budget_process_transaction_time.as_us()
);
if let Err(err) = process_transaction_result {
return TransactionExecutionResult::NotExecuted(err);
}
compute_budget
};
let mut compute_budget_process_transaction_time =
Measure::start("compute_budget_process_transaction_time");
let process_transaction_result = compute_budget.process_instructions(
tx.message().program_instructions_iter(),
feature_set.is_active(&default_units_per_instruction::id()),
feature_set.is_active(&add_set_compute_unit_price_ix::id()),
);
compute_budget_process_transaction_time.stop();
saturating_add_assign!(
timings
.execute_accessories
.compute_budget_process_transaction_us,
compute_budget_process_transaction_time.as_us()
);
if let Err(err) = process_transaction_result {
return TransactionExecutionResult::NotExecuted(err);
}
compute_budget
};
self.execute_loaded_transaction(
tx,
@ -6545,10 +6543,6 @@ impl Bank {
*self.inflation.write().unwrap() = inflation;
}
pub fn set_compute_budget(&mut self, compute_budget: Option<ComputeBudget>) {
self.compute_budget = compute_budget;
}
pub fn hard_forks(&self) -> Arc<RwLock<HardForks>> {
self.hard_forks.clone()
}
@ -18742,7 +18736,7 @@ pub(crate) mod tests {
None,
);
let compute_budget = bank.compute_budget.unwrap_or_else(|| {
let compute_budget = bank.runtime_config.compute_budget.unwrap_or_else(|| {
ComputeBudget::new(compute_budget::DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64)
});
let transaction_context = TransactionContext::new(

View File

@ -1,7 +1,7 @@
use solana_program_runtime::compute_budget::ComputeBudget;
/// Encapsulates flags that can be used to tweak the runtime behavior.
#[derive(Default, Clone)]
#[derive(AbiExample, Debug, Default, Clone)]
pub struct RuntimeConfig {
pub bpf_jit: bool,
pub compute_budget: Option<ComputeBudget>,

View File

@ -14,6 +14,7 @@ use {
epoch_stakes::EpochStakes,
hardened_unpack::UnpackedAppendVecMap,
rent_collector::RentCollector,
runtime_config::RuntimeConfig,
serde_snapshot::storage::SerializableAccountStorageEntry,
snapshot_utils::{self, BANK_SNAPSHOT_PRE_FILENAME_EXTENSION},
stakes::Stakes,
@ -285,6 +286,7 @@ pub(crate) fn bank_from_streams<R>(
account_paths: &[PathBuf],
unpacked_append_vec_map: UnpackedAppendVecMap,
genesis_config: &GenesisConfig,
runtime_config: &RuntimeConfig,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
additional_builtins: Option<&Builtins>,
account_secondary_indexes: AccountSecondaryIndexes,
@ -303,6 +305,7 @@ where
bank_fields,
accounts_db_fields,
genesis_config,
runtime_config,
account_paths,
unpacked_append_vec_map,
debug_keys,
@ -483,6 +486,7 @@ fn reconstruct_bank_from_fields<E>(
bank_fields: BankFieldsToDeserialize,
snapshot_accounts_db_fields: SnapshotAccountsDbFields<E>,
genesis_config: &GenesisConfig,
runtime_config: &RuntimeConfig,
account_paths: &[PathBuf],
unpacked_append_vec_map: UnpackedAppendVecMap,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
@ -513,12 +517,14 @@ where
)?;
let bank_rc = BankRc::new(Accounts::new_empty(accounts_db), bank_fields.slot);
let runtime_config = Arc::new(runtime_config.clone());
// if limit_load_slot_count_from_snapshot is set, then we need to side-step some correctness checks beneath this call
let debug_do_not_add_builtins = limit_load_slot_count_from_snapshot.is_some();
let bank = Bank::new_from_fields(
bank_rc,
genesis_config,
runtime_config,
bank_fields,
debug_keys,
additional_builtins,

View File

@ -293,6 +293,7 @@ fn test_bank_serialize_style(
&dbank_paths,
unpacked_append_vec_map,
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -408,6 +409,7 @@ fn test_extra_fields_eof() {
&dbank_paths,
unpacked_append_vec_map,
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -467,6 +469,7 @@ fn test_extra_fields_full_snapshot_archive() {
&snapshot_archive_info,
None,
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -529,6 +532,7 @@ fn test_blank_extra_fields() {
&dbank_paths,
unpacked_append_vec_map,
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),

View File

@ -8,6 +8,7 @@ use {
bank::{Bank, BankFieldsToDeserialize, BankSlotDelta},
builtins::Builtins,
hardened_unpack::{unpack_snapshot, ParallelSelector, UnpackError, UnpackedAppendVecMap},
runtime_config::RuntimeConfig,
serde_snapshot::{
bank_from_streams, bank_to_stream, fields_from_streams, SerdeStyle, SnapshotStreams,
},
@ -891,6 +892,7 @@ pub fn bank_from_snapshot_archives(
full_snapshot_archive_info: &FullSnapshotArchiveInfo,
incremental_snapshot_archive_info: Option<&IncrementalSnapshotArchiveInfo>,
genesis_config: &GenesisConfig,
runtime_config: &RuntimeConfig,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
additional_builtins: Option<&Builtins>,
account_secondary_indexes: AccountSecondaryIndexes,
@ -929,6 +931,7 @@ pub fn bank_from_snapshot_archives(
account_paths,
unpacked_append_vec_map,
genesis_config,
runtime_config,
debug_keys,
additional_builtins,
account_secondary_indexes,
@ -974,6 +977,7 @@ pub fn bank_from_latest_snapshot_archives(
incremental_snapshot_archives_dir: impl AsRef<Path>,
account_paths: &[PathBuf],
genesis_config: &GenesisConfig,
runtime_config: &RuntimeConfig,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
additional_builtins: Option<&Builtins>,
account_secondary_indexes: AccountSecondaryIndexes,
@ -1017,6 +1021,7 @@ pub fn bank_from_latest_snapshot_archives(
&full_snapshot_archive_info,
incremental_snapshot_archive_info.as_ref(),
genesis_config,
runtime_config,
debug_keys,
additional_builtins,
account_secondary_indexes,
@ -1638,6 +1643,7 @@ fn rebuild_bank_from_snapshots(
account_paths: &[PathBuf],
unpacked_append_vec_map: UnpackedAppendVecMap,
genesis_config: &GenesisConfig,
runtime_config: &RuntimeConfig,
debug_keys: Option<Arc<HashSet<Pubkey>>>,
additional_builtins: Option<&Builtins>,
account_secondary_indexes: AccountSecondaryIndexes,
@ -1686,6 +1692,7 @@ fn rebuild_bank_from_snapshots(
account_paths,
unpacked_append_vec_map,
genesis_config,
runtime_config,
debug_keys,
additional_builtins,
account_secondary_indexes,
@ -3079,6 +3086,7 @@ mod tests {
&snapshot_archive_info,
None,
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -3190,6 +3198,7 @@ mod tests {
&full_snapshot_archive_info,
None,
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -3321,6 +3330,7 @@ mod tests {
&full_snapshot_archive_info,
Some(&incremental_snapshot_archive_info),
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -3442,6 +3452,7 @@ mod tests {
&incremental_snapshot_archives_dir,
&[accounts_dir.as_ref().to_path_buf()],
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -3500,14 +3511,11 @@ mod tests {
let lamports_to_transfer = sol_to_lamports(123_456.);
let bank0 = Arc::new(Bank::new_with_paths_for_tests(
&genesis_config,
Arc::<RuntimeConfig>::default(),
vec![accounts_dir.path().to_path_buf()],
None,
None,
AccountSecondaryIndexes::default(),
false,
AccountShrinkThreshold::default(),
false,
None,
));
bank0
.transfer(lamports_to_transfer, &mint_keypair, &key2.pubkey())
@ -3584,6 +3592,7 @@ mod tests {
&full_snapshot_archive_info,
Some(&incremental_snapshot_archive_info),
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),
@ -3647,6 +3656,7 @@ mod tests {
&full_snapshot_archive_info,
Some(&incremental_snapshot_archive_info),
&genesis_config,
&RuntimeConfig::default(),
None,
None,
AccountSecondaryIndexes::default(),