Change bank to not create default (#5409)

This commit is contained in:
sakridge 2019-08-02 14:46:53 -07:00 committed by GitHub
parent 04d2db4dbb
commit 832dfd4ab0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 55 deletions

View File

@ -310,80 +310,81 @@ impl Bank {
/// Create a new bank that points to an immutable checkpoint of another bank. /// Create a new bank that points to an immutable checkpoint of another bank.
pub fn new_from_parent(parent: &Arc<Bank>, collector_id: &Pubkey, slot: u64) -> Self { pub fn new_from_parent(parent: &Arc<Bank>, collector_id: &Pubkey, slot: u64) -> Self {
Self::default().init_from_parent(parent, collector_id, slot)
}
/// Create a new bank that points to an immutable checkpoint of another bank.
pub fn init_from_parent(
mut self,
parent: &Arc<Bank>,
collector_id: &Pubkey,
slot: u64,
) -> Self {
parent.freeze(); parent.freeze();
assert_ne!(slot, parent.slot()); assert_ne!(slot, parent.slot());
// TODO: clean this up, soo much special-case copying... let rc = BankRc {
self.ticks_per_slot = parent.ticks_per_slot; accounts: Arc::new(Accounts::new_from_parent(&parent.rc.accounts)),
self.slots_per_segment = parent.slots_per_segment; parent: RwLock::new(Some(parent.clone())),
self.slots_per_year = parent.slots_per_year; };
self.epoch_schedule = parent.epoch_schedule; let src = StatusCacheRc {
status_cache: parent.src.status_cache.clone(),
};
let mut new = Bank {
rc,
src,
blockhash_queue: RwLock::new(parent.blockhash_queue.read().unwrap().clone()),
self.slot = slot; // TODO: clean this up, soo much special-case copying...
self.max_tick_height = (self.slot + 1) * self.ticks_per_slot - 1; ticks_per_slot: parent.ticks_per_slot,
slots_per_segment: parent.slots_per_segment,
slots_per_year: parent.slots_per_year,
epoch_schedule: parent.epoch_schedule,
slot,
max_tick_height: (slot + 1) * parent.ticks_per_slot - 1,
bank_height: parent.bank_height + 1,
fee_calculator: FeeCalculator::new_derived(
&parent.fee_calculator,
parent.signature_count(),
),
capitalization: AtomicUsize::new(parent.capitalization() as usize),
inflation: parent.inflation.clone(),
transaction_count: AtomicUsize::new(parent.transaction_count() as usize),
stakes: RwLock::new(parent.stakes.read().unwrap().clone_with_epoch(0)),
storage_accounts: RwLock::new(parent.storage_accounts.read().unwrap().clone()),
parent_hash: parent.hash(),
collector_id: *collector_id,
collector_fees: AtomicUsize::new(0),
ancestors: HashMap::new(),
epoch_stakes: HashMap::new(),
hash: RwLock::new(Hash::default()),
is_delta: AtomicBool::new(false),
tick_height: AtomicUsize::new(parent.tick_height.load(Ordering::Relaxed)),
signature_count: AtomicUsize::new(0),
message_processor: MessageProcessor::default(),
};
self.blockhash_queue = RwLock::new(parent.blockhash_queue.read().unwrap().clone()); {
self.src.status_cache = parent.src.status_cache.clone(); *new.stakes.write().unwrap() =
self.bank_height = parent.bank_height + 1; parent.stakes.read().unwrap().clone_with_epoch(new.epoch());
self.fee_calculator = }
FeeCalculator::new_derived(&parent.fee_calculator, parent.signature_count());
self.capitalization
.store(parent.capitalization() as usize, Ordering::Relaxed);
self.inflation = parent.inflation.clone();
self.transaction_count
.store(parent.transaction_count() as usize, Ordering::Relaxed);
self.stakes = RwLock::new(parent.stakes.read().unwrap().clone_with_epoch(self.epoch()));
self.storage_accounts = RwLock::new(parent.storage_accounts.read().unwrap().clone());
self.tick_height.store(
parent.tick_height.load(Ordering::Relaxed),
Ordering::Relaxed,
);
datapoint_info!( datapoint_info!(
"bank-new_from_parent-heights", "bank-new_from_parent-heights",
("slot_height", slot, i64), ("slot_height", slot, i64),
("bank_height", self.bank_height, i64) ("bank_height", new.bank_height, i64)
); );
self.rc.parent = RwLock::new(Some(parent.clone())); new.epoch_stakes = {
self.parent_hash = parent.hash();
self.collector_id = *collector_id;
self.rc.accounts = Arc::new(Accounts::new_from_parent(&parent.rc.accounts));
self.epoch_stakes = {
let mut epoch_stakes = parent.epoch_stakes.clone(); let mut epoch_stakes = parent.epoch_stakes.clone();
let epoch = self.get_stakers_epoch(self.slot); let epoch = new.get_stakers_epoch(new.slot);
// update epoch_vote_states cache // update epoch_vote_states cache
// if my parent didn't populate for this epoch, we've // if my parent didn't populate for this epoch, we've
// crossed a boundary // crossed a boundary
if epoch_stakes.get(&epoch).is_none() { if epoch_stakes.get(&epoch).is_none() {
epoch_stakes.insert(epoch, self.stakes.read().unwrap().clone()); epoch_stakes.insert(epoch, new.stakes.read().unwrap().clone());
} }
epoch_stakes epoch_stakes
}; };
self.ancestors.insert(self.slot(), 0); new.ancestors.insert(new.slot(), 0);
self.parents().iter().enumerate().for_each(|(i, p)| { new.parents().iter().enumerate().for_each(|(i, p)| {
self.ancestors.insert(p.slot(), i + 1); new.ancestors.insert(p.slot(), i + 1);
}); });
self.update_rewards(parent.epoch()); new.update_rewards(parent.epoch());
self.update_clock(); new.update_clock();
self.update_fees(); new.update_fees();
self new
} }
pub fn collector_id(&self) -> &Pubkey { pub fn collector_id(&self) -> &Pubkey {
@ -2335,9 +2336,9 @@ mod tests {
Some(Ok(())) Some(Ok(()))
); );
trace!("new form parent"); trace!("new from parent");
let bank = new_from_parent(&parent); let bank = new_from_parent(&parent);
trace!("done new form parent"); trace!("done new from parent");
assert_eq!( assert_eq!(
bank.get_signature_status(&tx_transfer_mint_to_1.signatures[0]), bank.get_signature_status(&tx_transfer_mint_to_1.signatures[0]),
Some(Ok(())) Some(Ok(()))