Skip to content

Commit

Permalink
SVM: Unify different instances of epoch_schedule in SVM and Bank into…
Browse files Browse the repository at this point in the history
… one
  • Loading branch information
pgarg66 committed Jun 17, 2024
1 parent 7f2beb2 commit fed69d2
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 74 deletions.
50 changes: 19 additions & 31 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,6 @@ impl PartialEq for Bank {
fee_rate_governor,
collected_rent,
rent_collector,
epoch_schedule,
inflation,
stakes_cache,
epoch_stakes,
Expand Down Expand Up @@ -628,7 +627,7 @@ impl PartialEq for Bank {
&& fee_rate_governor == &other.fee_rate_governor
&& collected_rent.load(Relaxed) == other.collected_rent.load(Relaxed)
&& rent_collector == &other.rent_collector
&& epoch_schedule == &other.epoch_schedule
&& self.epoch_schedule() == other.epoch_schedule()
&& *inflation.read().unwrap() == *other.inflation.read().unwrap()
&& *stakes_cache.stakes() == *other.stakes_cache.stakes()
&& epoch_stakes == &other.epoch_stakes
Expand Down Expand Up @@ -808,9 +807,6 @@ pub struct Bank {
/// latest rent collector, knows the epoch
rent_collector: RentCollector,

/// initialized from genesis
pub(crate) epoch_schedule: EpochSchedule,

/// inflation specs
inflation: Arc<RwLock<Inflation>>,

Expand Down Expand Up @@ -974,7 +970,6 @@ impl Bank {
fee_rate_governor: FeeRateGovernor::default(),
collected_rent: AtomicU64::default(),
rent_collector: RentCollector::default(),
epoch_schedule: EpochSchedule::default(),
inflation: Arc::<RwLock<Inflation>>::default(),
stakes_cache: StakesCache::default(),
epoch_stakes: HashMap::<Epoch, EpochStakes>::default(),
Expand Down Expand Up @@ -1004,12 +999,8 @@ impl Bank {
transaction_account_lock_limit: None,
};

bank.transaction_processor = TransactionBatchProcessor::new(
bank.slot,
bank.epoch,
bank.epoch_schedule.clone(),
HashSet::default(),
);
bank.transaction_processor =
TransactionBatchProcessor::new(bank.slot, bank.epoch, HashSet::default());

let accounts_data_size_initial = bank.get_total_accounts_stats().unwrap().data_len as u64;
bank.accounts_data_size_initial = accounts_data_size_initial;
Expand Down Expand Up @@ -1193,7 +1184,6 @@ impl Bank {
ns_per_slot: parent.ns_per_slot,
genesis_creation_time: parent.genesis_creation_time,
slots_per_year: parent.slots_per_year,
epoch_schedule,
collected_rent: AtomicU64::new(0),
rent_collector: Self::get_rent_collector_from(&parent.rent_collector, epoch),
max_tick_height: (slot + 1) * parent.ticks_per_slot,
Expand Down Expand Up @@ -1375,7 +1365,7 @@ impl Bank {
/// Epoch in which the new cooldown warmup rate for stake was activated
pub fn new_warmup_cooldown_rate_epoch(&self) -> Option<Epoch> {
self.feature_set
.new_warmup_cooldown_rate_epoch(&self.epoch_schedule)
.new_warmup_cooldown_rate_epoch(self.epoch_schedule())
}

/// process for the start of a new epoch
Expand Down Expand Up @@ -1414,7 +1404,7 @@ impl Bank {
);

// Save a snapshot of stakes for use in consensus and stake weighted networking
let leader_schedule_epoch = self.epoch_schedule.get_leader_schedule_epoch(slot);
let leader_schedule_epoch = self.epoch_schedule().get_leader_schedule_epoch(slot);
let (_, update_epoch_stakes_time) = measure!(
self.update_epoch_stakes(leader_schedule_epoch),
"update_epoch_stakes",
Expand Down Expand Up @@ -1608,7 +1598,6 @@ impl Bank {
collected_rent: AtomicU64::new(fields.collected_rent),
// clone()-ing is needed to consider a gated behavior in rent_collector
rent_collector: Self::get_rent_collector_from(&fields.rent_collector, fields.epoch),
epoch_schedule: fields.epoch_schedule,
inflation: Arc::new(RwLock::new(fields.inflation)),
stakes_cache: StakesCache::new(stakes),
epoch_stakes: fields.epoch_stakes,
Expand Down Expand Up @@ -1639,12 +1628,11 @@ impl Bank {
transaction_account_lock_limit: runtime_config.transaction_account_lock_limit,
};

bank.transaction_processor = TransactionBatchProcessor::new(
bank.slot,
bank.epoch,
bank.epoch_schedule.clone(),
HashSet::default(),
);
bank.transaction_processor =
TransactionBatchProcessor::new(bank.slot, bank.epoch, HashSet::default());

bank.transaction_processor
.set_epoch_schedule(&fields.epoch_schedule);

let thread_pool = ThreadPoolBuilder::new()
.thread_name(|i| format!("solBnkNewFlds{i:02}"))
Expand Down Expand Up @@ -1685,8 +1673,8 @@ impl Bank {
bank.ticks_per_slot,
)
);
assert_eq!(bank.epoch_schedule, genesis_config.epoch_schedule);
assert_eq!(bank.epoch, bank.epoch_schedule.get_epoch(bank.slot));
assert_eq!(bank.epoch_schedule(), &genesis_config.epoch_schedule);
assert_eq!(bank.epoch, bank.epoch_schedule().get_epoch(bank.slot));

datapoint_info!(
"bank-new-from-fields",
Expand Down Expand Up @@ -1736,7 +1724,7 @@ impl Bank {
fee_rate_governor: self.fee_rate_governor.clone(),
collected_rent: self.collected_rent.load(Relaxed),
rent_collector: self.rent_collector.clone(),
epoch_schedule: self.epoch_schedule.clone(),
epoch_schedule: self.epoch_schedule().clone(),
inflation: *self.inflation.read().unwrap(),
stakes: StakesEnum::from(self.stakes_cache.stakes().clone()),
epoch_stakes: self.epoch_stakes.clone(),
Expand Down Expand Up @@ -2856,7 +2844,7 @@ impl Bank {
}

pub fn epoch_schedule(&self) -> &EpochSchedule {
&self.epoch_schedule
self.transaction_processor.epoch_schedule()
}

/// squash the parent's state up into this Bank,
Expand Down Expand Up @@ -2968,8 +2956,8 @@ impl Bank {
self.max_tick_height = (self.slot + 1) * self.ticks_per_slot;
self.slots_per_year = genesis_config.slots_per_year();

self.epoch_schedule = genesis_config.epoch_schedule.clone();
self.transaction_processor.epoch_schedule = genesis_config.epoch_schedule.clone();
self.transaction_processor
.set_epoch_schedule(&genesis_config.epoch_schedule);

self.inflation = Arc::new(RwLock::new(genesis_config.inflation));

Expand Down Expand Up @@ -4504,7 +4492,7 @@ impl Bank {
if !rent_paying_pubkeys.contains(pubkey) {
let partition_from_pubkey = accounts_partition::partition_from_pubkey(
pubkey,
self.epoch_schedule.slots_per_epoch,
self.epoch_schedule().slots_per_epoch,
);
// Submit datapoint instead of assert while we verify this is correct
datapoint_warn!(
Expand Down Expand Up @@ -6016,7 +6004,7 @@ impl Bank {
let config = CalcAccountsHashConfig {
use_bg_thread_pool: true,
ancestors: None, // does not matter, will not be used
epoch_schedule: &self.epoch_schedule,
epoch_schedule: self.epoch_schedule(),
rent_collector: &self.rent_collector,
store_detailed_debug_info_on_failure: false,
};
Expand Down Expand Up @@ -7141,7 +7129,7 @@ impl Bank {
pub fn new_program_cache_for_tx_batch_for_slot(&self, slot: Slot) -> ProgramCacheForTxBatch {
ProgramCacheForTxBatch::new_from_cache(
slot,
self.epoch_schedule.get_epoch(slot),
self.epoch_schedule().get_epoch(slot),
&self.transaction_processor.program_cache.read().unwrap(),
)
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/bank/partitioned_epoch_rewards/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Bank {
let distribution_end_exclusive =
distribution_starting_block_height + status.stake_rewards_by_partition.len() as u64;
assert!(
self.epoch_schedule.get_slots_in_epoch(self.epoch)
self.epoch_schedule().get_slots_in_epoch(self.epoch)
> distribution_end_exclusive.saturating_sub(distribution_starting_block_height)
);

Expand Down Expand Up @@ -278,7 +278,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "self.epoch_schedule.get_slots_in_epoch")]
#[should_panic(expected = "self.epoch_schedule().get_slots_in_epoch")]
fn test_distribute_partitioned_epoch_rewards_too_many_partitions() {
let (genesis_config, _mint_keypair) = create_genesis_config(1_000_000 * LAMPORTS_PER_SOL);
let mut bank = Bank::new_for_tests(&genesis_config);
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/bank/partitioned_epoch_rewards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl Bank {
rewards: &PartitionedStakeRewards,
) -> u64 {
let total_stake_accounts = rewards.len();
if self.epoch_schedule.warmup && self.epoch < self.first_normal_epoch() {
if self.epoch_schedule().warmup && self.epoch < self.first_normal_epoch() {
1
} else {
const MAX_FACTOR_OF_REWARD_BLOCKS_IN_EPOCH: u64 = 10;
Expand All @@ -199,7 +199,8 @@ impl Bank {
// Limit the reward credit interval to 10% of the total number of slots in a epoch
num_chunks.clamp(
1,
(self.epoch_schedule.slots_per_epoch / MAX_FACTOR_OF_REWARD_BLOCKS_IN_EPOCH).max(1),
(self.epoch_schedule().slots_per_epoch / MAX_FACTOR_OF_REWARD_BLOCKS_IN_EPOCH)
.max(1),
)
}
}
Expand Down
9 changes: 3 additions & 6 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8149,10 +8149,10 @@ fn test_update_clock_timestamp() {
}

fn poh_estimate_offset(bank: &Bank) -> Duration {
let mut epoch_start_slot = bank.epoch_schedule.get_first_slot_in_epoch(bank.epoch());
let mut epoch_start_slot = bank.epoch_schedule().get_first_slot_in_epoch(bank.epoch());
if epoch_start_slot == bank.slot() {
epoch_start_slot = bank
.epoch_schedule
.epoch_schedule()
.get_first_slot_in_epoch(bank.epoch() - 1);
}
bank.slot().saturating_sub(epoch_start_slot) as u32
Expand Down Expand Up @@ -9116,10 +9116,7 @@ fn test_epoch_schedule_from_genesis_config() {
Arc::default(),
));

assert_eq!(
&bank.transaction_processor.epoch_schedule,
&genesis_config.epoch_schedule
);
assert_eq!(bank.epoch_schedule(), &genesis_config.epoch_schedule);
}

fn check_stake_vote_account_validity<F>(check_owner_change: bool, load_vote_and_stake_accounts: F)
Expand Down
18 changes: 9 additions & 9 deletions svm/src/program_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ mod tests {
&batch_processor.get_environments_for_epoch(50).unwrap(),
&key,
500,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
);
assert!(result.is_none());
Expand All @@ -517,7 +517,7 @@ mod tests {
&batch_processor.get_environments_for_epoch(20).unwrap(),
&key,
0, // Slot 0
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
);

Expand Down Expand Up @@ -552,7 +552,7 @@ mod tests {
&batch_processor.get_environments_for_epoch(20).unwrap(),
&key,
200,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
);
let loaded_program = ProgramCacheEntry::new_tombstone(
Expand Down Expand Up @@ -580,7 +580,7 @@ mod tests {
&batch_processor.get_environments_for_epoch(20).unwrap(),
&key,
200,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
);

Expand Down Expand Up @@ -634,7 +634,7 @@ mod tests {
&batch_processor.get_environments_for_epoch(0).unwrap(),
&key1,
0,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
);
let loaded_program = ProgramCacheEntry::new_tombstone(
Expand Down Expand Up @@ -672,7 +672,7 @@ mod tests {
&batch_processor.get_environments_for_epoch(20).unwrap(),
&key1,
200,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
);

Expand Down Expand Up @@ -722,7 +722,7 @@ mod tests {
&batch_processor.get_environments_for_epoch(0).unwrap(),
&key,
0,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
);
let loaded_program = ProgramCacheEntry::new_tombstone(
Expand Down Expand Up @@ -756,7 +756,7 @@ mod tests {
&batch_processor.get_environments_for_epoch(20).unwrap(),
&key,
200,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
);

Expand Down Expand Up @@ -807,7 +807,7 @@ mod tests {
.unwrap(),
&key,
200,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
)
.unwrap();
Expand Down
26 changes: 12 additions & 14 deletions svm/src/transaction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct TransactionBatchProcessor<FG: ForkGraph> {
epoch: Epoch,

/// initialized from genesis
pub epoch_schedule: EpochSchedule,
epoch_schedule: EpochSchedule,

/// Transaction fee structure
pub fee_structure: FeeStructure,
Expand Down Expand Up @@ -195,16 +195,11 @@ impl<FG: ForkGraph> Default for TransactionBatchProcessor<FG> {
}

impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
pub fn new(
slot: Slot,
epoch: Epoch,
epoch_schedule: EpochSchedule,
builtin_program_ids: HashSet<Pubkey>,
) -> Self {
pub fn new(slot: Slot, epoch: Epoch, builtin_program_ids: HashSet<Pubkey>) -> Self {
Self {
slot,
epoch,
epoch_schedule,
epoch_schedule: EpochSchedule::default(),
fee_structure: FeeStructure::default(),
sysvar_cache: RwLock::<SysvarCache>::default(),
program_cache: Arc::new(RwLock::new(ProgramCache::new(slot, epoch))),
Expand All @@ -224,6 +219,14 @@ impl<FG: ForkGraph> TransactionBatchProcessor<FG> {
}
}

pub fn set_epoch_schedule(&mut self, epoch_schedule: &EpochSchedule) {
self.epoch_schedule = epoch_schedule.clone()
}

pub fn epoch_schedule(&self) -> &EpochSchedule {
&self.epoch_schedule
}

/// Returns the current environments depending on the given epoch
/// Returns None if the call could result in a deadlock
#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
Expand Down Expand Up @@ -1856,12 +1859,7 @@ mod tests {
#[test]
fn fast_concur_test() {
let mut mock_bank = MockBankCallback::default();
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::new(
5,
5,
EpochSchedule::default(),
HashSet::new(),
);
let batch_processor = TransactionBatchProcessor::<TestForkGraph>::new(5, 5, HashSet::new());
batch_processor.program_cache.write().unwrap().fork_graph =
Some(Arc::new(RwLock::new(TestForkGraph {})));

Expand Down
10 changes: 2 additions & 8 deletions svm/tests/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use {
solana_sdk::{
account::{AccountSharedData, ReadableAccount, WritableAccount},
bpf_loader_upgradeable,
epoch_schedule::EpochSchedule,
feature_set::{FeatureSet, FEATURE_NAMES},
hash::Hash,
instruction::AccountMeta,
Expand Down Expand Up @@ -247,12 +246,7 @@ fn run_fixture(fixture: InstrFixture, filename: OsString, execute_as_instr: bool
create_program_runtime_environment_v1(&feature_set, &compute_budget, false, false).unwrap();

mock_bank.override_feature_set(feature_set);
let batch_processor = TransactionBatchProcessor::<MockForkGraph>::new(
42,
2,
EpochSchedule::default(),
HashSet::new(),
);
let batch_processor = TransactionBatchProcessor::<MockForkGraph>::new(42, 2, HashSet::new());

{
let mut program_cache = batch_processor.program_cache.write().unwrap();
Expand Down Expand Up @@ -445,7 +439,7 @@ fn execute_fixture_as_instr(
&batch_processor.get_environments_for_epoch(2).unwrap(),
&program_id,
42,
&batch_processor.epoch_schedule,
batch_processor.epoch_schedule(),
false,
)
.unwrap();
Expand Down
Loading

0 comments on commit fed69d2

Please sign in to comment.