solana/core/src/banking_stage/decision_maker.rs

209 lines
6.9 KiB
Rust
Raw Normal View History

use {
solana_poh::poh_recorder::{BankStart, PohRecorder},
solana_sdk::{
clock::{
DEFAULT_TICKS_PER_SLOT, FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET,
HOLD_TRANSACTIONS_SLOT_OFFSET,
},
pubkey::Pubkey,
},
std::sync::{Arc, RwLock},
};
#[derive(Debug, Clone)]
pub enum BufferedPacketsDecision {
Consume(BankStart),
Forward,
ForwardAndHold,
Hold,
}
impl BufferedPacketsDecision {
/// Returns the `BankStart` if the decision is `Consume`. Otherwise, returns `None`.
pub fn bank_start(&self) -> Option<&BankStart> {
match self {
Self::Consume(bank_start) => Some(bank_start),
_ => None,
}
}
}
pub struct DecisionMaker {
my_pubkey: Pubkey,
poh_recorder: Arc<RwLock<PohRecorder>>,
}
impl DecisionMaker {
pub fn new(my_pubkey: Pubkey, poh_recorder: Arc<RwLock<PohRecorder>>) -> Self {
Self {
my_pubkey,
poh_recorder,
}
}
pub(crate) fn make_consume_or_forward_decision(&self) -> BufferedPacketsDecision {
2023-04-12 21:40:59 -07:00
let decision;
{
let poh_recorder = self.poh_recorder.read().unwrap();
decision = Self::consume_or_forward_packets(
&self.my_pubkey,
|| {
poh_recorder.bank_start().filter(|bank_start| {
bank_start.should_working_bank_still_be_processing_txs()
})
},
|| {
poh_recorder.would_be_leader(
(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET - 1)
* DEFAULT_TICKS_PER_SLOT,
)
2023-04-12 21:40:59 -07:00
},
|| {
poh_recorder
.would_be_leader(HOLD_TRANSACTIONS_SLOT_OFFSET * DEFAULT_TICKS_PER_SLOT)
},
|| poh_recorder.leader_after_n_slots(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET),
);
}
2023-04-12 21:40:59 -07:00
decision
}
fn consume_or_forward_packets(
my_pubkey: &Pubkey,
2023-04-12 21:40:59 -07:00
bank_start_fn: impl FnOnce() -> Option<BankStart>,
would_be_leader_shortly_fn: impl FnOnce() -> bool,
would_be_leader_fn: impl FnOnce() -> bool,
leader_pubkey_fn: impl FnOnce() -> Option<Pubkey>,
) -> BufferedPacketsDecision {
// If has active bank, then immediately process buffered packets
// otherwise, based on leader schedule to either forward or hold packets
2023-04-12 21:40:59 -07:00
if let Some(bank_start) = bank_start_fn() {
// If the bank is available, this node is the leader
BufferedPacketsDecision::Consume(bank_start)
2023-04-12 21:40:59 -07:00
} else if would_be_leader_shortly_fn() {
// If the node will be the leader soon, hold the packets for now
BufferedPacketsDecision::Hold
2023-04-12 21:40:59 -07:00
} else if would_be_leader_fn() {
// Node will be leader within ~20 slots, hold the transactions in
// case it is the only node which produces an accepted slot.
BufferedPacketsDecision::ForwardAndHold
2023-04-12 21:40:59 -07:00
} else if let Some(x) = leader_pubkey_fn() {
if x != *my_pubkey {
// If the current node is not the leader, forward the buffered packets
BufferedPacketsDecision::Forward
} else {
// If the current node is the leader, return the buffered packets as is
BufferedPacketsDecision::Hold
}
} else {
// We don't know the leader. Hold the packets for now
BufferedPacketsDecision::Hold
}
}
}
#[cfg(test)]
mod tests {
use {
super::*,
2023-04-12 21:40:59 -07:00
core::panic,
solana_runtime::bank::Bank,
std::{sync::Arc, time::Instant},
};
#[test]
fn test_buffered_packet_decision_bank_start() {
let bank = Arc::new(Bank::default_for_tests());
let bank_start = BankStart {
working_bank: bank,
bank_creation_time: Arc::new(Instant::now()),
};
assert!(BufferedPacketsDecision::Consume(bank_start)
.bank_start()
.is_some());
assert!(BufferedPacketsDecision::Forward.bank_start().is_none());
assert!(BufferedPacketsDecision::ForwardAndHold
.bank_start()
.is_none());
assert!(BufferedPacketsDecision::Hold.bank_start().is_none());
}
#[test]
fn test_should_process_or_forward_packets() {
let my_pubkey = solana_sdk::pubkey::new_rand();
let my_pubkey1 = solana_sdk::pubkey::new_rand();
let bank = Arc::new(Bank::default_for_tests());
let bank_start = Some(BankStart {
working_bank: bank,
bank_creation_time: Arc::new(Instant::now()),
});
// having active bank allows to consume immediately
assert_matches!(
DecisionMaker::consume_or_forward_packets(
&my_pubkey,
2023-04-12 21:40:59 -07:00
|| bank_start.clone(),
|| panic!("should not be called"),
|| panic!("should not be called"),
|| panic!("should not be called")
),
BufferedPacketsDecision::Consume(_)
);
2023-04-12 21:40:59 -07:00
// Unknown leader, hold the packets
assert_matches!(
2023-04-12 21:40:59 -07:00
DecisionMaker::consume_or_forward_packets(
&my_pubkey,
|| None,
|| false,
|| false,
|| None
),
BufferedPacketsDecision::Hold
);
2023-04-12 21:40:59 -07:00
// Leader other than me, forward the packets
assert_matches!(
DecisionMaker::consume_or_forward_packets(
&my_pubkey,
2023-04-12 21:40:59 -07:00
|| None,
|| false,
|| false,
|| Some(my_pubkey1),
),
BufferedPacketsDecision::Forward
);
2023-04-12 21:40:59 -07:00
// Will be leader shortly, hold the packets
assert_matches!(
DecisionMaker::consume_or_forward_packets(
&my_pubkey,
2023-04-12 21:40:59 -07:00
|| None,
|| true,
|| panic!("should not be called"),
|| panic!("should not be called"),
),
BufferedPacketsDecision::Hold
);
2023-04-12 21:40:59 -07:00
// Will be leader (not shortly), forward and hold
assert_matches!(
DecisionMaker::consume_or_forward_packets(
&my_pubkey,
2023-04-12 21:40:59 -07:00
|| None,
|| false,
|| true,
|| panic!("should not be called"),
),
BufferedPacketsDecision::ForwardAndHold
);
2023-04-12 21:40:59 -07:00
// Current leader matches my pubkey, hold
assert_matches!(
DecisionMaker::consume_or_forward_packets(
&my_pubkey1,
2023-04-12 21:40:59 -07:00
|| None,
|| false,
|| false,
|| Some(my_pubkey1),
),
BufferedPacketsDecision::Hold
);
}
}