2020-09-28 19:43:05 -07:00
|
|
|
//! The `optimistically_confirmed_bank_tracker` module implements a threaded service to track the
|
|
|
|
//! most recent optimistically confirmed bank for use in rpc services, and triggers gossip
|
|
|
|
//! subscription notifications
|
|
|
|
|
2021-05-18 23:54:28 -07:00
|
|
|
use {
|
|
|
|
crate::rpc_subscriptions::RpcSubscriptions,
|
|
|
|
crossbeam_channel::{Receiver, RecvTimeoutError, Sender},
|
|
|
|
solana_client::rpc_response::{SlotTransactionStats, SlotUpdate},
|
|
|
|
solana_runtime::{bank::Bank, bank_forks::BankForks},
|
|
|
|
solana_sdk::{clock::Slot, timing::timestamp},
|
|
|
|
std::{
|
|
|
|
collections::HashSet,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc, RwLock,
|
|
|
|
},
|
|
|
|
thread::{self, Builder, JoinHandle},
|
|
|
|
time::Duration,
|
2020-09-28 19:43:05 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct OptimisticallyConfirmedBank {
|
|
|
|
pub bank: Arc<Bank>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OptimisticallyConfirmedBank {
|
|
|
|
pub fn locked_from_bank_forks_root(bank_forks: &Arc<RwLock<BankForks>>) -> Arc<RwLock<Self>> {
|
|
|
|
Arc::new(RwLock::new(Self {
|
2020-12-27 05:28:05 -08:00
|
|
|
bank: bank_forks.read().unwrap().root_bank(),
|
2020-09-28 19:43:05 -07:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 14:26:17 -07:00
|
|
|
#[derive(Clone)]
|
2020-09-28 19:43:05 -07:00
|
|
|
pub enum BankNotification {
|
|
|
|
OptimisticallyConfirmed(Slot),
|
|
|
|
Frozen(Arc<Bank>),
|
|
|
|
Root(Arc<Bank>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for BankNotification {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
BankNotification::OptimisticallyConfirmed(slot) => {
|
|
|
|
write!(f, "OptimisticallyConfirmed({:?})", slot)
|
|
|
|
}
|
|
|
|
BankNotification::Frozen(bank) => write!(f, "Frozen({})", bank.slot()),
|
|
|
|
BankNotification::Root(bank) => write!(f, "Root({})", bank.slot()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type BankNotificationReceiver = Receiver<BankNotification>;
|
|
|
|
pub type BankNotificationSender = Sender<BankNotification>;
|
|
|
|
|
|
|
|
pub struct OptimisticallyConfirmedBankTracker {
|
|
|
|
thread_hdl: JoinHandle<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OptimisticallyConfirmedBankTracker {
|
|
|
|
pub fn new(
|
|
|
|
receiver: BankNotificationReceiver,
|
|
|
|
exit: &Arc<AtomicBool>,
|
|
|
|
bank_forks: Arc<RwLock<BankForks>>,
|
|
|
|
optimistically_confirmed_bank: Arc<RwLock<OptimisticallyConfirmedBank>>,
|
|
|
|
subscriptions: Arc<RpcSubscriptions>,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers: Option<Arc<RwLock<Vec<BankNotificationSender>>>>,
|
2020-09-28 19:43:05 -07:00
|
|
|
) -> Self {
|
|
|
|
let exit_ = exit.clone();
|
|
|
|
let mut pending_optimistically_confirmed_banks = HashSet::new();
|
2021-08-10 16:44:45 -07:00
|
|
|
let mut last_notified_confirmed_slot: Slot = 0;
|
|
|
|
let mut highest_confirmed_slot: Slot = 0;
|
2020-09-28 19:43:05 -07:00
|
|
|
let thread_hdl = Builder::new()
|
|
|
|
.name("solana-optimistic-bank-tracker".to_string())
|
|
|
|
.spawn(move || loop {
|
|
|
|
if exit_.load(Ordering::Relaxed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(RecvTimeoutError::Disconnected) = Self::recv_notification(
|
|
|
|
&receiver,
|
|
|
|
&bank_forks,
|
|
|
|
&optimistically_confirmed_bank,
|
|
|
|
&subscriptions,
|
|
|
|
&mut pending_optimistically_confirmed_banks,
|
2021-08-10 16:44:45 -07:00
|
|
|
&mut last_notified_confirmed_slot,
|
|
|
|
&mut highest_confirmed_slot,
|
2021-09-30 14:26:17 -07:00
|
|
|
&bank_notification_subscribers,
|
2020-09-28 19:43:05 -07:00
|
|
|
) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
Self { thread_hdl }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn recv_notification(
|
|
|
|
receiver: &Receiver<BankNotification>,
|
|
|
|
bank_forks: &Arc<RwLock<BankForks>>,
|
|
|
|
optimistically_confirmed_bank: &Arc<RwLock<OptimisticallyConfirmedBank>>,
|
|
|
|
subscriptions: &Arc<RpcSubscriptions>,
|
2021-10-05 22:24:48 -07:00
|
|
|
pending_optimistically_confirmed_banks: &mut HashSet<Slot>,
|
|
|
|
last_notified_confirmed_slot: &mut Slot,
|
|
|
|
highest_confirmed_slot: &mut Slot,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers: &Option<Arc<RwLock<Vec<BankNotificationSender>>>>,
|
2020-09-28 19:43:05 -07:00
|
|
|
) -> Result<(), RecvTimeoutError> {
|
|
|
|
let notification = receiver.recv_timeout(Duration::from_secs(1))?;
|
|
|
|
Self::process_notification(
|
|
|
|
notification,
|
|
|
|
bank_forks,
|
|
|
|
optimistically_confirmed_bank,
|
|
|
|
subscriptions,
|
2021-10-05 22:24:48 -07:00
|
|
|
pending_optimistically_confirmed_banks,
|
|
|
|
last_notified_confirmed_slot,
|
|
|
|
highest_confirmed_slot,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers,
|
2020-09-28 19:43:05 -07:00
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-30 14:26:17 -07:00
|
|
|
fn notify_slot_status(
|
|
|
|
bank_notification_subscribers: &Option<Arc<RwLock<Vec<BankNotificationSender>>>>,
|
|
|
|
notifcation: BankNotification,
|
|
|
|
) {
|
|
|
|
if let Some(bank_notification_subscribers) = bank_notification_subscribers {
|
|
|
|
for sender in bank_notification_subscribers.read().unwrap().iter() {
|
|
|
|
match sender.send(notifcation.clone()) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
info!(
|
|
|
|
"Failed to send notification {:?}, error: {:?}",
|
|
|
|
notifcation, err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 16:44:45 -07:00
|
|
|
fn notify_or_defer(
|
|
|
|
subscriptions: &Arc<RpcSubscriptions>,
|
|
|
|
bank_forks: &Arc<RwLock<BankForks>>,
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
last_notified_confirmed_slot: &mut Slot,
|
|
|
|
pending_optimistically_confirmed_banks: &mut HashSet<Slot>,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers: &Option<Arc<RwLock<Vec<BankNotificationSender>>>>,
|
2021-08-10 16:44:45 -07:00
|
|
|
) {
|
|
|
|
if bank.is_frozen() {
|
|
|
|
if bank.slot() > *last_notified_confirmed_slot {
|
|
|
|
debug!(
|
|
|
|
"notify_or_defer notifying via notify_gossip_subscribers for slot {:?}",
|
|
|
|
bank.slot()
|
|
|
|
);
|
|
|
|
subscriptions.notify_gossip_subscribers(bank.slot());
|
|
|
|
*last_notified_confirmed_slot = bank.slot();
|
2021-09-30 14:26:17 -07:00
|
|
|
Self::notify_slot_status(
|
|
|
|
bank_notification_subscribers,
|
|
|
|
BankNotification::OptimisticallyConfirmed(bank.slot()),
|
|
|
|
);
|
2021-08-10 16:44:45 -07:00
|
|
|
}
|
|
|
|
} else if bank.slot() > bank_forks.read().unwrap().root_bank().slot() {
|
|
|
|
pending_optimistically_confirmed_banks.insert(bank.slot());
|
|
|
|
debug!("notify_or_defer defer notifying for slot {:?}", bank.slot());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify_or_defer_confirmed_banks(
|
|
|
|
subscriptions: &Arc<RpcSubscriptions>,
|
|
|
|
bank_forks: &Arc<RwLock<BankForks>>,
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
slot_threshold: Slot,
|
2021-10-05 22:24:48 -07:00
|
|
|
last_notified_confirmed_slot: &mut Slot,
|
|
|
|
pending_optimistically_confirmed_banks: &mut HashSet<Slot>,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers: &Option<Arc<RwLock<Vec<BankNotificationSender>>>>,
|
2021-08-10 16:44:45 -07:00
|
|
|
) {
|
|
|
|
for confirmed_bank in bank.clone().parents_inclusive().iter().rev() {
|
|
|
|
if confirmed_bank.slot() > slot_threshold {
|
|
|
|
debug!(
|
|
|
|
"Calling notify_or_defer for confirmed_bank {:?}",
|
|
|
|
confirmed_bank.slot()
|
|
|
|
);
|
|
|
|
Self::notify_or_defer(
|
|
|
|
subscriptions,
|
|
|
|
bank_forks,
|
|
|
|
confirmed_bank,
|
2021-10-05 22:24:48 -07:00
|
|
|
last_notified_confirmed_slot,
|
|
|
|
pending_optimistically_confirmed_banks,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers,
|
2021-08-10 16:44:45 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 23:54:28 -07:00
|
|
|
pub fn process_notification(
|
2020-09-28 19:43:05 -07:00
|
|
|
notification: BankNotification,
|
|
|
|
bank_forks: &Arc<RwLock<BankForks>>,
|
|
|
|
optimistically_confirmed_bank: &Arc<RwLock<OptimisticallyConfirmedBank>>,
|
|
|
|
subscriptions: &Arc<RpcSubscriptions>,
|
2021-10-05 22:24:48 -07:00
|
|
|
pending_optimistically_confirmed_banks: &mut HashSet<Slot>,
|
|
|
|
last_notified_confirmed_slot: &mut Slot,
|
2021-08-10 16:44:45 -07:00
|
|
|
highest_confirmed_slot: &mut Slot,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers: &Option<Arc<RwLock<Vec<BankNotificationSender>>>>,
|
2020-09-28 19:43:05 -07:00
|
|
|
) {
|
|
|
|
debug!("received bank notification: {:?}", notification);
|
|
|
|
match notification {
|
|
|
|
BankNotification::OptimisticallyConfirmed(slot) => {
|
2021-08-10 16:44:45 -07:00
|
|
|
if let Some(bank) = bank_forks.read().unwrap().get(slot) {
|
2020-09-28 19:43:05 -07:00
|
|
|
let mut w_optimistically_confirmed_bank =
|
|
|
|
optimistically_confirmed_bank.write().unwrap();
|
2021-08-10 16:44:45 -07:00
|
|
|
|
|
|
|
if bank.slot() > w_optimistically_confirmed_bank.bank.slot() && bank.is_frozen()
|
|
|
|
{
|
2020-09-28 19:43:05 -07:00
|
|
|
w_optimistically_confirmed_bank.bank = bank.clone();
|
2021-08-10 16:44:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if slot > *highest_confirmed_slot {
|
|
|
|
Self::notify_or_defer_confirmed_banks(
|
|
|
|
subscriptions,
|
|
|
|
bank_forks,
|
|
|
|
bank,
|
|
|
|
*highest_confirmed_slot,
|
2021-10-05 22:24:48 -07:00
|
|
|
last_notified_confirmed_slot,
|
|
|
|
pending_optimistically_confirmed_banks,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers,
|
2021-08-10 16:44:45 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
*highest_confirmed_slot = slot;
|
2020-09-28 19:43:05 -07:00
|
|
|
}
|
|
|
|
drop(w_optimistically_confirmed_bank);
|
|
|
|
} else if slot > bank_forks.read().unwrap().root_bank().slot() {
|
|
|
|
pending_optimistically_confirmed_banks.insert(slot);
|
2021-02-23 14:03:22 -08:00
|
|
|
} else {
|
|
|
|
inc_new_counter_info!("dropped-already-rooted-optimistic-bank-notification", 1);
|
2020-09-28 19:43:05 -07:00
|
|
|
}
|
2021-02-28 23:29:11 -08:00
|
|
|
|
|
|
|
// Send slot notification regardless of whether the bank is replayed
|
|
|
|
subscriptions.notify_slot_update(SlotUpdate::OptimisticConfirmation {
|
|
|
|
slot,
|
|
|
|
timestamp: timestamp(),
|
|
|
|
});
|
2020-09-28 19:43:05 -07:00
|
|
|
}
|
|
|
|
BankNotification::Frozen(bank) => {
|
|
|
|
let frozen_slot = bank.slot();
|
2021-03-12 05:44:06 -08:00
|
|
|
if let Some(parent) = bank.parent() {
|
|
|
|
let num_successful_transactions = bank
|
|
|
|
.transaction_count()
|
|
|
|
.saturating_sub(parent.transaction_count());
|
|
|
|
subscriptions.notify_slot_update(SlotUpdate::Frozen {
|
|
|
|
slot: frozen_slot,
|
|
|
|
timestamp: timestamp(),
|
|
|
|
stats: SlotTransactionStats {
|
|
|
|
num_transaction_entries: bank.transaction_entries_count(),
|
|
|
|
num_successful_transactions,
|
|
|
|
num_failed_transactions: bank.transaction_error_count(),
|
|
|
|
max_transactions_per_entry: bank.transactions_per_entry_max(),
|
|
|
|
},
|
|
|
|
});
|
2021-09-30 14:26:17 -07:00
|
|
|
|
|
|
|
Self::notify_slot_status(
|
|
|
|
bank_notification_subscribers,
|
|
|
|
BankNotification::Frozen(bank.clone()),
|
|
|
|
);
|
2021-03-12 05:44:06 -08:00
|
|
|
}
|
|
|
|
|
2020-09-28 19:43:05 -07:00
|
|
|
if pending_optimistically_confirmed_banks.remove(&bank.slot()) {
|
2021-08-10 16:44:45 -07:00
|
|
|
debug!(
|
|
|
|
"Calling notify_gossip_subscribers to send deferred notification {:?}",
|
|
|
|
frozen_slot
|
|
|
|
);
|
|
|
|
|
|
|
|
Self::notify_or_defer_confirmed_banks(
|
|
|
|
subscriptions,
|
|
|
|
bank_forks,
|
|
|
|
&bank,
|
|
|
|
*last_notified_confirmed_slot,
|
2021-10-05 22:24:48 -07:00
|
|
|
last_notified_confirmed_slot,
|
|
|
|
pending_optimistically_confirmed_banks,
|
2021-09-30 14:26:17 -07:00
|
|
|
bank_notification_subscribers,
|
2021-08-10 16:44:45 -07:00
|
|
|
);
|
|
|
|
|
2020-09-28 19:43:05 -07:00
|
|
|
let mut w_optimistically_confirmed_bank =
|
|
|
|
optimistically_confirmed_bank.write().unwrap();
|
|
|
|
if frozen_slot > w_optimistically_confirmed_bank.bank.slot() {
|
|
|
|
w_optimistically_confirmed_bank.bank = bank;
|
|
|
|
}
|
|
|
|
drop(w_optimistically_confirmed_bank);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BankNotification::Root(bank) => {
|
2021-09-30 14:26:17 -07:00
|
|
|
Self::notify_slot_status(
|
|
|
|
bank_notification_subscribers,
|
|
|
|
BankNotification::Root(bank.clone()),
|
|
|
|
);
|
2020-09-28 19:43:05 -07:00
|
|
|
let root_slot = bank.slot();
|
|
|
|
let mut w_optimistically_confirmed_bank =
|
|
|
|
optimistically_confirmed_bank.write().unwrap();
|
|
|
|
if root_slot > w_optimistically_confirmed_bank.bank.slot() {
|
|
|
|
w_optimistically_confirmed_bank.bank = bank;
|
|
|
|
}
|
|
|
|
drop(w_optimistically_confirmed_bank);
|
|
|
|
pending_optimistically_confirmed_banks.retain(|&s| s > root_slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(self) -> thread::Result<()> {
|
|
|
|
self.join()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn join(self) -> thread::Result<()> {
|
|
|
|
self.thread_hdl.join()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-05-18 23:54:28 -07:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
solana_ledger::genesis_utils::{create_genesis_config, GenesisConfigInfo},
|
|
|
|
solana_runtime::{
|
|
|
|
accounts_background_service::AbsRequestSender, commitment::BlockCommitmentCache,
|
|
|
|
},
|
|
|
|
solana_sdk::pubkey::Pubkey,
|
2020-12-12 17:22:34 -08:00
|
|
|
};
|
2020-09-28 19:43:05 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_notification() {
|
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
|
|
|
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(100);
|
2021-08-05 06:42:38 -07:00
|
|
|
let bank = Bank::new_for_tests(&genesis_config);
|
2020-09-28 19:43:05 -07:00
|
|
|
let bank_forks = Arc::new(RwLock::new(BankForks::new(bank)));
|
|
|
|
let bank0 = bank_forks.read().unwrap().get(0).unwrap().clone();
|
|
|
|
let bank1 = Bank::new_from_parent(&bank0, &Pubkey::default(), 1);
|
|
|
|
bank_forks.write().unwrap().insert(bank1);
|
|
|
|
let bank1 = bank_forks.read().unwrap().get(1).unwrap().clone();
|
|
|
|
let bank2 = Bank::new_from_parent(&bank1, &Pubkey::default(), 2);
|
|
|
|
bank_forks.write().unwrap().insert(bank2);
|
|
|
|
let bank2 = bank_forks.read().unwrap().get(2).unwrap().clone();
|
|
|
|
let bank3 = Bank::new_from_parent(&bank2, &Pubkey::default(), 3);
|
|
|
|
bank_forks.write().unwrap().insert(bank3);
|
|
|
|
|
|
|
|
let optimistically_confirmed_bank =
|
|
|
|
OptimisticallyConfirmedBank::locked_from_bank_forks_root(&bank_forks);
|
|
|
|
|
|
|
|
let block_commitment_cache = Arc::new(RwLock::new(BlockCommitmentCache::default()));
|
2021-09-17 12:40:14 -07:00
|
|
|
let subscriptions = Arc::new(RpcSubscriptions::new_for_tests(
|
2020-09-28 19:43:05 -07:00
|
|
|
&exit,
|
|
|
|
bank_forks.clone(),
|
|
|
|
block_commitment_cache,
|
|
|
|
optimistically_confirmed_bank.clone(),
|
|
|
|
));
|
|
|
|
let mut pending_optimistically_confirmed_banks = HashSet::new();
|
|
|
|
|
|
|
|
assert_eq!(optimistically_confirmed_bank.read().unwrap().bank.slot(), 0);
|
|
|
|
|
2021-08-10 16:44:45 -07:00
|
|
|
let mut highest_confirmed_slot: Slot = 0;
|
|
|
|
let mut last_notified_confirmed_slot: Slot = 0;
|
2020-09-28 19:43:05 -07:00
|
|
|
OptimisticallyConfirmedBankTracker::process_notification(
|
|
|
|
BankNotification::OptimisticallyConfirmed(2),
|
|
|
|
&bank_forks,
|
|
|
|
&optimistically_confirmed_bank,
|
|
|
|
&subscriptions,
|
|
|
|
&mut pending_optimistically_confirmed_banks,
|
2021-08-10 16:44:45 -07:00
|
|
|
&mut last_notified_confirmed_slot,
|
|
|
|
&mut highest_confirmed_slot,
|
2021-09-01 14:10:16 -07:00
|
|
|
&None,
|
2020-09-28 19:43:05 -07:00
|
|
|
);
|
|
|
|
assert_eq!(optimistically_confirmed_bank.read().unwrap().bank.slot(), 2);
|
2021-08-10 16:44:45 -07:00
|
|
|
assert_eq!(highest_confirmed_slot, 2);
|
2020-09-28 19:43:05 -07:00
|
|
|
|
|
|
|
// Test max optimistically confirmed bank remains in the cache
|
|
|
|
OptimisticallyConfirmedBankTracker::process_notification(
|
|
|
|
BankNotification::OptimisticallyConfirmed(1),
|
|
|
|
&bank_forks,
|
|
|
|
&optimistically_confirmed_bank,
|
|
|
|
&subscriptions,
|
|
|
|
&mut pending_optimistically_confirmed_banks,
|
2021-08-10 16:44:45 -07:00
|
|
|
&mut last_notified_confirmed_slot,
|
|
|
|
&mut highest_confirmed_slot,
|
2021-09-01 14:10:16 -07:00
|
|
|
&None,
|
2020-09-28 19:43:05 -07:00
|
|
|
);
|
|
|
|
assert_eq!(optimistically_confirmed_bank.read().unwrap().bank.slot(), 2);
|
2021-08-10 16:44:45 -07:00
|
|
|
assert_eq!(highest_confirmed_slot, 2);
|
2020-09-28 19:43:05 -07:00
|
|
|
|
|
|
|
// Test bank will only be cached when frozen
|
|
|
|
OptimisticallyConfirmedBankTracker::process_notification(
|
|
|
|
BankNotification::OptimisticallyConfirmed(3),
|
|
|
|
&bank_forks,
|
|
|
|
&optimistically_confirmed_bank,
|
|
|
|
&subscriptions,
|
|
|
|
&mut pending_optimistically_confirmed_banks,
|
2021-08-10 16:44:45 -07:00
|
|
|
&mut last_notified_confirmed_slot,
|
|
|
|
&mut highest_confirmed_slot,
|
2021-09-01 14:10:16 -07:00
|
|
|
&None,
|
2020-09-28 19:43:05 -07:00
|
|
|
);
|
|
|
|
assert_eq!(optimistically_confirmed_bank.read().unwrap().bank.slot(), 2);
|
|
|
|
assert_eq!(pending_optimistically_confirmed_banks.len(), 1);
|
2021-05-19 07:31:47 -07:00
|
|
|
assert!(pending_optimistically_confirmed_banks.contains(&3));
|
2021-08-10 16:44:45 -07:00
|
|
|
assert_eq!(highest_confirmed_slot, 3);
|
2020-09-28 19:43:05 -07:00
|
|
|
|
|
|
|
// Test bank will only be cached when frozen
|
|
|
|
let bank3 = bank_forks.read().unwrap().get(3).unwrap().clone();
|
2021-08-10 16:44:45 -07:00
|
|
|
bank3.freeze();
|
|
|
|
|
2020-09-28 19:43:05 -07:00
|
|
|
OptimisticallyConfirmedBankTracker::process_notification(
|
|
|
|
BankNotification::Frozen(bank3),
|
|
|
|
&bank_forks,
|
|
|
|
&optimistically_confirmed_bank,
|
|
|
|
&subscriptions,
|
|
|
|
&mut pending_optimistically_confirmed_banks,
|
2021-08-10 16:44:45 -07:00
|
|
|
&mut last_notified_confirmed_slot,
|
|
|
|
&mut highest_confirmed_slot,
|
2021-09-01 14:10:16 -07:00
|
|
|
&None,
|
2020-09-28 19:43:05 -07:00
|
|
|
);
|
|
|
|
assert_eq!(optimistically_confirmed_bank.read().unwrap().bank.slot(), 3);
|
2021-08-10 16:44:45 -07:00
|
|
|
assert_eq!(highest_confirmed_slot, 3);
|
|
|
|
assert_eq!(pending_optimistically_confirmed_banks.len(), 0);
|
2020-09-28 19:43:05 -07:00
|
|
|
|
|
|
|
// Test higher root will be cached and clear pending_optimistically_confirmed_banks
|
|
|
|
let bank3 = bank_forks.read().unwrap().get(3).unwrap().clone();
|
|
|
|
let bank4 = Bank::new_from_parent(&bank3, &Pubkey::default(), 4);
|
|
|
|
bank_forks.write().unwrap().insert(bank4);
|
|
|
|
OptimisticallyConfirmedBankTracker::process_notification(
|
|
|
|
BankNotification::OptimisticallyConfirmed(4),
|
|
|
|
&bank_forks,
|
|
|
|
&optimistically_confirmed_bank,
|
|
|
|
&subscriptions,
|
|
|
|
&mut pending_optimistically_confirmed_banks,
|
2021-08-10 16:44:45 -07:00
|
|
|
&mut last_notified_confirmed_slot,
|
|
|
|
&mut highest_confirmed_slot,
|
2021-09-01 14:10:16 -07:00
|
|
|
&None,
|
2020-09-28 19:43:05 -07:00
|
|
|
);
|
|
|
|
assert_eq!(optimistically_confirmed_bank.read().unwrap().bank.slot(), 3);
|
|
|
|
assert_eq!(pending_optimistically_confirmed_banks.len(), 1);
|
2021-05-19 07:31:47 -07:00
|
|
|
assert!(pending_optimistically_confirmed_banks.contains(&4));
|
2021-08-10 16:44:45 -07:00
|
|
|
assert_eq!(highest_confirmed_slot, 4);
|
2020-09-28 19:43:05 -07:00
|
|
|
|
|
|
|
let bank4 = bank_forks.read().unwrap().get(4).unwrap().clone();
|
|
|
|
let bank5 = Bank::new_from_parent(&bank4, &Pubkey::default(), 5);
|
|
|
|
bank_forks.write().unwrap().insert(bank5);
|
|
|
|
let bank5 = bank_forks.read().unwrap().get(5).unwrap().clone();
|
|
|
|
OptimisticallyConfirmedBankTracker::process_notification(
|
|
|
|
BankNotification::Root(bank5),
|
|
|
|
&bank_forks,
|
|
|
|
&optimistically_confirmed_bank,
|
|
|
|
&subscriptions,
|
|
|
|
&mut pending_optimistically_confirmed_banks,
|
2021-08-10 16:44:45 -07:00
|
|
|
&mut last_notified_confirmed_slot,
|
|
|
|
&mut highest_confirmed_slot,
|
2021-09-01 14:10:16 -07:00
|
|
|
&None,
|
2020-09-28 19:43:05 -07:00
|
|
|
);
|
|
|
|
assert_eq!(optimistically_confirmed_bank.read().unwrap().bank.slot(), 5);
|
|
|
|
assert_eq!(pending_optimistically_confirmed_banks.len(), 0);
|
2021-05-19 07:31:47 -07:00
|
|
|
assert!(!pending_optimistically_confirmed_banks.contains(&4));
|
2021-08-10 16:44:45 -07:00
|
|
|
assert_eq!(highest_confirmed_slot, 4);
|
2020-09-28 19:43:05 -07:00
|
|
|
|
|
|
|
// Banks <= root do not get added to pending list, even if not frozen
|
|
|
|
let bank5 = bank_forks.read().unwrap().get(5).unwrap().clone();
|
|
|
|
let bank6 = Bank::new_from_parent(&bank5, &Pubkey::default(), 6);
|
|
|
|
bank_forks.write().unwrap().insert(bank6);
|
|
|
|
let bank5 = bank_forks.read().unwrap().get(5).unwrap().clone();
|
|
|
|
let bank7 = Bank::new_from_parent(&bank5, &Pubkey::default(), 7);
|
|
|
|
bank_forks.write().unwrap().insert(bank7);
|
2020-12-12 17:22:34 -08:00
|
|
|
bank_forks
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
2021-02-18 23:42:09 -08:00
|
|
|
.set_root(7, &AbsRequestSender::default(), None);
|
2020-09-28 19:43:05 -07:00
|
|
|
OptimisticallyConfirmedBankTracker::process_notification(
|
|
|
|
BankNotification::OptimisticallyConfirmed(6),
|
|
|
|
&bank_forks,
|
|
|
|
&optimistically_confirmed_bank,
|
|
|
|
&subscriptions,
|
|
|
|
&mut pending_optimistically_confirmed_banks,
|
2021-08-10 16:44:45 -07:00
|
|
|
&mut last_notified_confirmed_slot,
|
|
|
|
&mut highest_confirmed_slot,
|
2021-09-01 14:10:16 -07:00
|
|
|
&None,
|
2020-09-28 19:43:05 -07:00
|
|
|
);
|
|
|
|
assert_eq!(optimistically_confirmed_bank.read().unwrap().bank.slot(), 5);
|
|
|
|
assert_eq!(pending_optimistically_confirmed_banks.len(), 0);
|
2021-05-19 07:31:47 -07:00
|
|
|
assert!(!pending_optimistically_confirmed_banks.contains(&6));
|
2021-08-10 16:44:45 -07:00
|
|
|
assert_eq!(highest_confirmed_slot, 4);
|
2020-09-28 19:43:05 -07:00
|
|
|
}
|
|
|
|
}
|