2018-06-06 08:58:49 -07:00
|
|
|
//! The `banking_stage` processes Transaction messages. It is intended to be used
|
|
|
|
//! to contruct a software pipeline. The stage uses all available CPU cores and
|
|
|
|
//! can do its processing in parallel with signature verification on the GPU.
|
2021-10-12 06:51:33 -07:00
|
|
|
use crate::packet_hasher::PacketHasher;
|
2019-06-26 18:42:27 -07:00
|
|
|
use crossbeam_channel::{Receiver as CrossbeamReceiver, RecvTimeoutError};
|
2019-05-07 10:23:02 -07:00
|
|
|
use itertools::Itertools;
|
2021-03-03 10:23:05 -08:00
|
|
|
use lru::LruCache;
|
2021-02-12 03:27:37 -08:00
|
|
|
use retain_mut::RetainMut;
|
2021-07-14 05:16:29 -07:00
|
|
|
use solana_entry::entry::hash_transactions;
|
2021-10-07 02:38:23 -07:00
|
|
|
use solana_gossip::{cluster_info::ClusterInfo, contact_info::ContactInfo};
|
2021-07-14 05:16:29 -07:00
|
|
|
use solana_ledger::blockstore_processor::TransactionStatusSender;
|
2021-04-09 17:21:01 -07:00
|
|
|
use solana_measure::measure::Measure;
|
2020-12-14 16:26:17 -08:00
|
|
|
use solana_metrics::{inc_new_counter_debug, inc_new_counter_info};
|
2020-03-17 23:30:23 -07:00
|
|
|
use solana_perf::{
|
|
|
|
cuda_runtime::PinnedVec,
|
2021-09-21 08:49:41 -07:00
|
|
|
data_budget::DataBudget,
|
2020-03-17 23:30:23 -07:00
|
|
|
packet::{limited_deserialize, Packet, Packets, PACKETS_PER_BATCH},
|
|
|
|
perf_libs,
|
|
|
|
};
|
2021-09-17 16:55:53 -07:00
|
|
|
use solana_poh::poh_recorder::{BankStart, PohRecorder, PohRecorderError, TransactionRecorder};
|
2019-12-07 11:54:10 -08:00
|
|
|
use solana_runtime::{
|
|
|
|
accounts_db::ErrorCounters,
|
2021-01-14 14:14:16 -08:00
|
|
|
bank::{
|
|
|
|
Bank, ExecuteTimings, TransactionBalancesSet, TransactionCheckResult,
|
|
|
|
TransactionExecutionResult,
|
|
|
|
},
|
2020-08-07 11:21:35 -07:00
|
|
|
bank_utils,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model::CostModel,
|
2021-10-12 06:51:33 -07:00
|
|
|
cost_tracker::CostTracker,
|
2019-12-07 11:54:10 -08:00
|
|
|
transaction_batch::TransactionBatch,
|
2020-08-07 11:21:35 -07:00
|
|
|
vote_sender_types::ReplayVoteSender,
|
2019-12-07 11:54:10 -08:00
|
|
|
};
|
2019-09-19 10:06:08 -07:00
|
|
|
use solana_sdk::{
|
|
|
|
clock::{
|
2020-09-21 22:36:23 -07:00
|
|
|
Slot, DEFAULT_TICKS_PER_SLOT, MAX_PROCESSING_AGE, MAX_TRANSACTION_FORWARDING_DELAY,
|
2020-09-03 02:31:28 -07:00
|
|
|
MAX_TRANSACTION_FORWARDING_DELAY_GPU,
|
2019-09-19 10:06:08 -07:00
|
|
|
},
|
2021-08-30 00:58:45 -07:00
|
|
|
feature_set,
|
2021-04-12 23:28:08 -07:00
|
|
|
message::Message,
|
2019-09-19 10:06:08 -07:00
|
|
|
pubkey::Pubkey,
|
2021-04-12 23:28:08 -07:00
|
|
|
short_vec::decode_shortu16_len,
|
|
|
|
signature::Signature,
|
2021-07-28 18:16:36 -07:00
|
|
|
timing::{duration_as_ms, timestamp, AtomicInterval},
|
2021-08-17 15:17:56 -07:00
|
|
|
transaction::{self, SanitizedTransaction, TransactionError, VersionedTransaction},
|
2019-09-19 10:06:08 -07:00
|
|
|
};
|
2021-09-29 20:49:43 -07:00
|
|
|
use solana_streamer::sendmmsg::{batch_send, SendPktsError};
|
2020-12-10 19:25:07 -08:00
|
|
|
use solana_transaction_status::token_balances::{
|
|
|
|
collect_token_balances, TransactionTokenBalancesSet,
|
|
|
|
};
|
2019-09-19 10:06:08 -07:00
|
|
|
use std::{
|
2020-12-10 19:25:07 -08:00
|
|
|
cmp,
|
2021-02-12 03:27:37 -08:00
|
|
|
collections::{HashMap, VecDeque},
|
2020-12-10 19:25:07 -08:00
|
|
|
env,
|
2021-04-12 23:28:08 -07:00
|
|
|
mem::size_of,
|
2021-10-07 02:38:23 -07:00
|
|
|
net::{SocketAddr, UdpSocket},
|
2021-03-03 10:23:05 -08:00
|
|
|
ops::DerefMut,
|
2021-06-04 08:23:06 -07:00
|
|
|
sync::atomic::{AtomicU64, AtomicUsize, Ordering},
|
2021-10-19 12:37:33 -07:00
|
|
|
sync::{Arc, Mutex, RwLock, RwLockReadGuard},
|
2019-09-19 10:06:08 -07:00
|
|
|
thread::{self, Builder, JoinHandle},
|
|
|
|
time::Duration,
|
|
|
|
time::Instant,
|
2019-05-10 14:28:38 -07:00
|
|
|
};
|
2018-05-14 16:31:13 -07:00
|
|
|
|
2021-03-03 10:23:05 -08:00
|
|
|
/// (packets, valid_indexes, forwarded)
|
|
|
|
/// Set of packets with a list of which are valid and if this batch has been forwarded.
|
|
|
|
type PacketsAndOffsets = (Packets, Vec<usize>, bool);
|
|
|
|
|
2021-02-12 03:27:37 -08:00
|
|
|
pub type UnprocessedPackets = VecDeque<PacketsAndOffsets>;
|
2018-10-18 22:57:48 -07:00
|
|
|
|
2019-06-24 15:56:50 -07:00
|
|
|
/// Transaction forwarding
|
2020-12-17 14:37:22 -08:00
|
|
|
pub const FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET: u64 = 2;
|
2021-03-03 10:23:05 -08:00
|
|
|
pub const HOLD_TRANSACTIONS_SLOT_OFFSET: u64 = 20;
|
2019-06-24 15:56:50 -07:00
|
|
|
|
2019-06-28 01:55:24 -07:00
|
|
|
// Fixed thread size seems to be fastest on GCP setup
|
|
|
|
pub const NUM_THREADS: u32 = 4;
|
|
|
|
|
|
|
|
const TOTAL_BUFFERED_PACKETS: usize = 500_000;
|
2018-09-26 05:52:13 -07:00
|
|
|
|
2019-09-19 16:29:52 -07:00
|
|
|
const MAX_NUM_TRANSACTIONS_PER_BATCH: usize = 128;
|
2019-09-10 11:04:03 -07:00
|
|
|
|
2021-03-03 10:23:05 -08:00
|
|
|
const DEFAULT_LRU_SIZE: usize = 200_000;
|
|
|
|
|
2021-10-07 02:38:23 -07:00
|
|
|
const NUM_VOTE_PROCESSING_THREADS: u32 = 2;
|
|
|
|
const MIN_THREADS_BANKING: u32 = 1;
|
|
|
|
|
2021-02-12 03:27:37 -08:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct BankingStageStats {
|
2021-07-28 18:16:36 -07:00
|
|
|
last_report: AtomicInterval,
|
2021-02-12 03:27:37 -08:00
|
|
|
id: u32,
|
|
|
|
process_packets_count: AtomicUsize,
|
|
|
|
new_tx_count: AtomicUsize,
|
2021-09-15 13:53:55 -07:00
|
|
|
dropped_packet_batches_count: AtomicUsize,
|
|
|
|
dropped_packets_count: AtomicUsize,
|
2021-10-20 19:56:48 -07:00
|
|
|
dropped_duplicated_packets_count: AtomicUsize,
|
2021-02-12 03:27:37 -08:00
|
|
|
newly_buffered_packets_count: AtomicUsize,
|
|
|
|
current_buffered_packets_count: AtomicUsize,
|
2021-09-15 13:53:55 -07:00
|
|
|
current_buffered_packet_batches_count: AtomicUsize,
|
2021-02-12 03:27:37 -08:00
|
|
|
rebuffered_packets_count: AtomicUsize,
|
|
|
|
consumed_buffered_packets_count: AtomicUsize,
|
2021-06-28 19:34:04 -07:00
|
|
|
cost_tracker_check_count: AtomicUsize,
|
|
|
|
cost_forced_retry_transactions_count: AtomicUsize,
|
2021-04-12 23:28:08 -07:00
|
|
|
|
|
|
|
// Timing
|
|
|
|
consume_buffered_packets_elapsed: AtomicU64,
|
|
|
|
process_packets_elapsed: AtomicU64,
|
|
|
|
handle_retryable_packets_elapsed: AtomicU64,
|
|
|
|
filter_pending_packets_elapsed: AtomicU64,
|
|
|
|
packet_duplicate_check_elapsed: AtomicU64,
|
|
|
|
packet_conversion_elapsed: AtomicU64,
|
2021-06-28 19:34:04 -07:00
|
|
|
unprocessed_packet_conversion_elapsed: AtomicU64,
|
2021-04-12 23:28:08 -07:00
|
|
|
transaction_processing_elapsed: AtomicU64,
|
2021-06-28 19:34:04 -07:00
|
|
|
cost_tracker_update_elapsed: AtomicU64,
|
|
|
|
cost_tracker_clone_elapsed: AtomicU64,
|
|
|
|
cost_tracker_check_elapsed: AtomicU64,
|
2021-02-12 03:27:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BankingStageStats {
|
|
|
|
pub fn new(id: u32) -> Self {
|
|
|
|
BankingStageStats {
|
|
|
|
id,
|
|
|
|
..BankingStageStats::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report(&self, report_interval_ms: u64) {
|
2021-07-28 18:16:36 -07:00
|
|
|
if self.last_report.should_update(report_interval_ms) {
|
2021-02-12 03:27:37 -08:00
|
|
|
datapoint_info!(
|
|
|
|
"banking_stage-loop-stats",
|
|
|
|
("id", self.id as i64, i64),
|
|
|
|
(
|
|
|
|
"process_packets_count",
|
|
|
|
self.process_packets_count.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"new_tx_count",
|
|
|
|
self.new_tx_count.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
2021-09-15 13:53:55 -07:00
|
|
|
"dropped_packet_batches_count",
|
|
|
|
self.dropped_packet_batches_count.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"dropped_packets_count",
|
|
|
|
self.dropped_packets_count.swap(0, Ordering::Relaxed) as i64,
|
2021-02-12 03:27:37 -08:00
|
|
|
i64
|
|
|
|
),
|
2021-10-20 19:56:48 -07:00
|
|
|
(
|
|
|
|
"dropped_duplicated_packets_count",
|
|
|
|
self.dropped_duplicated_packets_count
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-02-12 03:27:37 -08:00
|
|
|
(
|
|
|
|
"newly_buffered_packets_count",
|
|
|
|
self.newly_buffered_packets_count.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-09-15 13:53:55 -07:00
|
|
|
(
|
|
|
|
"current_buffered_packet_batches_count",
|
|
|
|
self.current_buffered_packet_batches_count
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-02-12 03:27:37 -08:00
|
|
|
(
|
|
|
|
"current_buffered_packets_count",
|
|
|
|
self.current_buffered_packets_count
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"rebuffered_packets_count",
|
|
|
|
self.rebuffered_packets_count.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-09-15 07:19:39 -07:00
|
|
|
(
|
|
|
|
"consumed_buffered_packets_count",
|
|
|
|
self.consumed_buffered_packets_count
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-06-28 19:34:04 -07:00
|
|
|
(
|
|
|
|
"cost_tracker_check_count",
|
|
|
|
self.cost_tracker_check_count.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cost_forced_retry_transactions_count",
|
|
|
|
self.cost_forced_retry_transactions_count
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-04-12 23:28:08 -07:00
|
|
|
(
|
|
|
|
"consume_buffered_packets_elapsed",
|
|
|
|
self.consume_buffered_packets_elapsed
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"process_packets_elapsed",
|
|
|
|
self.process_packets_elapsed.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"handle_retryable_packets_elapsed",
|
|
|
|
self.handle_retryable_packets_elapsed
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"filter_pending_packets_elapsed",
|
|
|
|
self.filter_pending_packets_elapsed
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"packet_duplicate_check_elapsed",
|
|
|
|
self.packet_duplicate_check_elapsed
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"packet_conversion_elapsed",
|
|
|
|
self.packet_conversion_elapsed.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-06-28 19:34:04 -07:00
|
|
|
(
|
|
|
|
"unprocessed_packet_conversion_elapsed",
|
|
|
|
self.unprocessed_packet_conversion_elapsed
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-04-12 23:28:08 -07:00
|
|
|
(
|
|
|
|
"transaction_processing_elapsed",
|
|
|
|
self.transaction_processing_elapsed
|
|
|
|
.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-06-28 19:34:04 -07:00
|
|
|
(
|
|
|
|
"cost_tracker_update_elapsed",
|
|
|
|
self.cost_tracker_update_elapsed.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cost_tracker_clone_elapsed",
|
|
|
|
self.cost_tracker_clone_elapsed.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cost_tracker_check_elapsed",
|
|
|
|
self.cost_tracker_check_elapsed.swap(0, Ordering::Relaxed) as i64,
|
|
|
|
i64
|
|
|
|
),
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 08:58:49 -07:00
|
|
|
/// Stores the stage's thread handle and output receiver.
|
2018-05-14 16:31:13 -07:00
|
|
|
pub struct BankingStage {
|
2019-03-03 16:44:06 -08:00
|
|
|
bank_thread_hdls: Vec<JoinHandle<()>>,
|
2018-09-26 05:52:13 -07:00
|
|
|
}
|
|
|
|
|
2021-03-15 17:11:15 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2019-04-13 23:19:54 -07:00
|
|
|
pub enum BufferedPacketsDecision {
|
2021-03-15 17:11:15 -07:00
|
|
|
Consume(u128),
|
2019-04-13 23:19:54 -07:00
|
|
|
Forward,
|
2021-03-03 10:23:05 -08:00
|
|
|
ForwardAndHold,
|
2019-04-13 23:19:54 -07:00
|
|
|
Hold,
|
|
|
|
}
|
|
|
|
|
2021-10-07 02:38:23 -07:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum ForwardOption {
|
|
|
|
NotForward,
|
|
|
|
ForwardTpuVote,
|
|
|
|
ForwardTransaction,
|
|
|
|
}
|
|
|
|
|
2018-05-14 16:31:13 -07:00
|
|
|
impl BankingStage {
|
2018-07-05 14:41:53 -07:00
|
|
|
/// Create the stage using `bank`. Exit when `verified_receiver` is dropped.
|
2018-12-07 19:01:28 -08:00
|
|
|
#[allow(clippy::new_ret_no_self)]
|
2018-05-14 16:31:13 -07:00
|
|
|
pub fn new(
|
2020-04-21 12:54:45 -07:00
|
|
|
cluster_info: &Arc<ClusterInfo>,
|
2019-02-26 10:48:18 -08:00
|
|
|
poh_recorder: &Arc<Mutex<PohRecorder>>,
|
2019-11-01 14:23:03 -07:00
|
|
|
verified_receiver: CrossbeamReceiver<Vec<Packets>>,
|
2021-10-07 02:38:23 -07:00
|
|
|
tpu_verified_vote_receiver: CrossbeamReceiver<Vec<Packets>>,
|
2019-11-01 14:23:03 -07:00
|
|
|
verified_vote_receiver: CrossbeamReceiver<Vec<Packets>>,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: ReplayVoteSender,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: Arc<RwLock<CostModel>>,
|
2019-03-18 22:08:21 -07:00
|
|
|
) -> Self {
|
|
|
|
Self::new_num_threads(
|
|
|
|
cluster_info,
|
|
|
|
poh_recorder,
|
|
|
|
verified_receiver,
|
2021-10-07 02:38:23 -07:00
|
|
|
tpu_verified_vote_receiver,
|
2019-04-17 21:07:45 -07:00
|
|
|
verified_vote_receiver,
|
2019-06-28 01:55:24 -07:00
|
|
|
Self::num_threads(),
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2019-03-18 22:08:21 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-01 15:13:10 -07:00
|
|
|
fn new_num_threads(
|
2020-04-21 12:54:45 -07:00
|
|
|
cluster_info: &Arc<ClusterInfo>,
|
2019-03-18 22:08:21 -07:00
|
|
|
poh_recorder: &Arc<Mutex<PohRecorder>>,
|
2019-11-01 14:23:03 -07:00
|
|
|
verified_receiver: CrossbeamReceiver<Vec<Packets>>,
|
|
|
|
verified_vote_receiver: CrossbeamReceiver<Vec<Packets>>,
|
2021-10-07 02:38:23 -07:00
|
|
|
tpu_verified_vote_receiver: CrossbeamReceiver<Vec<Packets>>,
|
2019-03-18 22:08:21 -07:00
|
|
|
num_threads: u32,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: ReplayVoteSender,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: Arc<RwLock<CostModel>>,
|
2019-03-03 16:44:06 -08:00
|
|
|
) -> Self {
|
2019-09-05 11:22:39 -07:00
|
|
|
let batch_limit = TOTAL_BUFFERED_PACKETS / ((num_threads - 1) as usize * PACKETS_PER_BATCH);
|
2018-09-26 05:52:13 -07:00
|
|
|
// Single thread to generate entries from many banks.
|
|
|
|
// This thread talks to poh_service and broadcasts the entries once they have been recorded.
|
2019-03-02 10:25:16 -08:00
|
|
|
// Once an entry has been recorded, its blockhash is registered with the bank.
|
2021-03-03 10:23:05 -08:00
|
|
|
let duplicates = Arc::new(Mutex::new((
|
|
|
|
LruCache::new(DEFAULT_LRU_SIZE),
|
|
|
|
PacketHasher::default(),
|
|
|
|
)));
|
2021-09-21 08:49:41 -07:00
|
|
|
let data_budget = Arc::new(DataBudget::default());
|
2018-09-26 05:52:13 -07:00
|
|
|
// Many banks that process transactions in parallel.
|
2021-10-07 02:38:23 -07:00
|
|
|
assert!(num_threads >= NUM_VOTE_PROCESSING_THREADS + MIN_THREADS_BANKING);
|
2019-03-27 04:37:36 -07:00
|
|
|
let bank_thread_hdls: Vec<JoinHandle<()>> = (0..num_threads)
|
2019-04-17 21:07:45 -07:00
|
|
|
.map(|i| {
|
2021-10-07 02:38:23 -07:00
|
|
|
let (verified_receiver, forward_option) = match i {
|
|
|
|
0 => {
|
|
|
|
// Disable forwarding of vote transactions
|
|
|
|
// from gossip. Note - votes can also arrive from tpu
|
|
|
|
(verified_vote_receiver.clone(), ForwardOption::NotForward)
|
|
|
|
}
|
|
|
|
1 => (
|
|
|
|
tpu_verified_vote_receiver.clone(),
|
|
|
|
ForwardOption::ForwardTpuVote,
|
|
|
|
),
|
|
|
|
_ => (verified_receiver.clone(), ForwardOption::ForwardTransaction),
|
2019-04-17 21:07:45 -07:00
|
|
|
};
|
|
|
|
|
2019-03-03 16:44:06 -08:00
|
|
|
let poh_recorder = poh_recorder.clone();
|
|
|
|
let cluster_info = cluster_info.clone();
|
2019-04-09 11:17:15 -07:00
|
|
|
let mut recv_start = Instant::now();
|
2019-11-20 15:43:10 -08:00
|
|
|
let transaction_status_sender = transaction_status_sender.clone();
|
2020-08-07 11:21:35 -07:00
|
|
|
let gossip_vote_sender = gossip_vote_sender.clone();
|
2021-03-03 10:23:05 -08:00
|
|
|
let duplicates = duplicates.clone();
|
2021-09-21 08:49:41 -07:00
|
|
|
let data_budget = data_budget.clone();
|
2021-10-19 12:37:33 -07:00
|
|
|
let cost_model = cost_model.clone();
|
2018-09-26 05:52:13 -07:00
|
|
|
Builder::new()
|
|
|
|
.name("solana-banking-stage-tx".to_string())
|
|
|
|
.spawn(move || {
|
2019-04-09 11:17:15 -07:00
|
|
|
Self::process_loop(
|
|
|
|
&verified_receiver,
|
|
|
|
&poh_recorder,
|
|
|
|
&cluster_info,
|
|
|
|
&mut recv_start,
|
2021-10-07 02:38:23 -07:00
|
|
|
forward_option,
|
2019-05-20 09:15:00 -07:00
|
|
|
i,
|
2019-06-28 01:55:24 -07:00
|
|
|
batch_limit,
|
2020-08-07 11:21:35 -07:00
|
|
|
transaction_status_sender,
|
|
|
|
gossip_vote_sender,
|
2021-03-03 10:23:05 -08:00
|
|
|
&duplicates,
|
2021-09-21 08:49:41 -07:00
|
|
|
&data_budget,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2019-04-09 11:17:15 -07:00
|
|
|
);
|
2018-12-07 19:01:28 -08:00
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.collect();
|
2019-03-03 16:44:06 -08:00
|
|
|
Self { bank_thread_hdls }
|
|
|
|
}
|
|
|
|
|
2021-02-12 03:27:37 -08:00
|
|
|
fn filter_valid_packets_for_forwarding<'a>(
|
|
|
|
all_packets: impl Iterator<Item = &'a PacketsAndOffsets>,
|
|
|
|
) -> Vec<&'a Packet> {
|
2019-05-10 14:28:38 -07:00
|
|
|
all_packets
|
2021-03-03 10:23:05 -08:00
|
|
|
.filter(|(_p, _indexes, forwarded)| !forwarded)
|
|
|
|
.flat_map(|(p, valid_indexes, _forwarded)| {
|
|
|
|
valid_indexes.iter().map(move |x| &p.packets[*x])
|
|
|
|
})
|
2019-05-10 14:28:38 -07:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-05-01 15:13:10 -07:00
|
|
|
fn forward_buffered_packets(
|
2019-03-06 12:01:21 -08:00
|
|
|
socket: &std::net::UdpSocket,
|
2019-07-30 14:50:02 -07:00
|
|
|
tpu_forwards: &std::net::SocketAddr,
|
2021-03-03 10:23:05 -08:00
|
|
|
unprocessed_packets: &UnprocessedPackets,
|
2021-09-21 08:49:41 -07:00
|
|
|
data_budget: &DataBudget,
|
2019-03-03 16:44:06 -08:00
|
|
|
) -> std::io::Result<()> {
|
2021-02-12 03:27:37 -08:00
|
|
|
let packets = Self::filter_valid_packets_for_forwarding(unprocessed_packets.iter());
|
2019-05-02 17:36:19 -07:00
|
|
|
inc_new_counter_info!("banking_stage-forwarded_packets", packets.len());
|
2021-09-21 08:49:41 -07:00
|
|
|
const INTERVAL_MS: u64 = 100;
|
|
|
|
const MAX_BYTES_PER_SECOND: usize = 10_000 * 1200;
|
|
|
|
const MAX_BYTES_PER_INTERVAL: usize = MAX_BYTES_PER_SECOND * INTERVAL_MS as usize / 1000;
|
|
|
|
const MAX_BYTES_BUDGET: usize = MAX_BYTES_PER_INTERVAL * 5;
|
|
|
|
data_budget.update(INTERVAL_MS, |bytes| {
|
|
|
|
std::cmp::min(bytes + MAX_BYTES_PER_INTERVAL, MAX_BYTES_BUDGET)
|
|
|
|
});
|
2021-09-29 20:49:43 -07:00
|
|
|
|
|
|
|
let mut packet_vec = Vec::with_capacity(packets.len());
|
2019-07-30 14:50:02 -07:00
|
|
|
for p in packets {
|
2021-09-21 08:49:41 -07:00
|
|
|
if data_budget.take(p.meta.size) {
|
2021-09-29 20:49:43 -07:00
|
|
|
packet_vec.push((&p.data[..p.meta.size], tpu_forwards));
|
2021-09-21 08:49:41 -07:00
|
|
|
}
|
2019-03-09 02:47:41 -08:00
|
|
|
}
|
2021-09-29 20:49:43 -07:00
|
|
|
if let Err(SendPktsError::IoError(ioerr, _num_failed)) = batch_send(socket, &packet_vec) {
|
|
|
|
return Err(ioerr);
|
|
|
|
}
|
2019-03-09 02:47:41 -08:00
|
|
|
|
2019-03-03 16:44:06 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-12 03:27:37 -08:00
|
|
|
// Returns whether the given `Packets` has any more remaining unprocessed
|
|
|
|
// transactions
|
|
|
|
fn update_buffered_packets_with_new_unprocessed(
|
|
|
|
original_unprocessed_indexes: &mut Vec<usize>,
|
|
|
|
new_unprocessed_indexes: Vec<usize>,
|
|
|
|
) -> bool {
|
|
|
|
let has_more_unprocessed_transactions =
|
|
|
|
Self::packet_has_more_unprocessed_transactions(&new_unprocessed_indexes);
|
|
|
|
if has_more_unprocessed_transactions {
|
|
|
|
*original_unprocessed_indexes = new_unprocessed_indexes
|
|
|
|
};
|
|
|
|
has_more_unprocessed_transactions
|
|
|
|
}
|
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2019-05-20 09:15:00 -07:00
|
|
|
pub fn consume_buffered_packets(
|
2019-05-23 23:20:04 -07:00
|
|
|
my_pubkey: &Pubkey,
|
2021-03-15 17:11:15 -07:00
|
|
|
max_tx_ingestion_ns: u128,
|
2019-03-08 16:48:15 -08:00
|
|
|
poh_recorder: &Arc<Mutex<PohRecorder>>,
|
2021-02-12 03:27:37 -08:00
|
|
|
buffered_packets: &mut UnprocessedPackets,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: &ReplayVoteSender,
|
2021-02-12 03:27:37 -08:00
|
|
|
test_fn: Option<impl Fn()>,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats: &BankingStageStats,
|
2021-03-23 07:10:04 -07:00
|
|
|
recorder: &TransactionRecorder,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: &Arc<RwLock<CostModel>>,
|
2021-02-12 03:27:37 -08:00
|
|
|
) {
|
|
|
|
let mut rebuffered_packets_len = 0;
|
2019-04-26 17:27:31 -07:00
|
|
|
let mut new_tx_count = 0;
|
2019-05-20 09:15:00 -07:00
|
|
|
let buffered_len = buffered_packets.len();
|
2019-06-29 06:34:49 -07:00
|
|
|
let mut proc_start = Measure::start("consume_buffered_process");
|
2021-02-12 03:27:37 -08:00
|
|
|
let mut reached_end_of_slot = None;
|
2021-04-12 23:28:08 -07:00
|
|
|
|
2021-03-03 10:23:05 -08:00
|
|
|
buffered_packets.retain_mut(|(msgs, ref mut original_unprocessed_indexes, _forwarded)| {
|
2021-02-12 03:27:37 -08:00
|
|
|
if let Some((next_leader, bank)) = &reached_end_of_slot {
|
|
|
|
// We've hit the end of this slot, no need to perform more processing,
|
|
|
|
// just filter the remaining packets for the invalid (e.g. too old) ones
|
|
|
|
let new_unprocessed_indexes = Self::filter_unprocessed_packets(
|
2021-06-18 06:34:46 -07:00
|
|
|
bank,
|
|
|
|
msgs,
|
|
|
|
original_unprocessed_indexes,
|
2021-02-12 03:27:37 -08:00
|
|
|
my_pubkey,
|
|
|
|
*next_leader,
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2019-06-26 22:39:50 -07:00
|
|
|
);
|
2021-02-12 03:27:37 -08:00
|
|
|
Self::update_buffered_packets_with_new_unprocessed(
|
|
|
|
original_unprocessed_indexes,
|
|
|
|
new_unprocessed_indexes,
|
|
|
|
)
|
|
|
|
} else {
|
2021-03-15 17:11:15 -07:00
|
|
|
let bank_start = poh_recorder.lock().unwrap().bank_start();
|
2021-09-17 16:55:53 -07:00
|
|
|
if let Some(BankStart {
|
|
|
|
working_bank,
|
|
|
|
bank_creation_time,
|
|
|
|
}) = bank_start
|
|
|
|
{
|
2021-02-12 03:27:37 -08:00
|
|
|
let (processed, verified_txs_len, new_unprocessed_indexes) =
|
2021-03-15 17:11:15 -07:00
|
|
|
Self::process_packets_transactions(
|
2021-09-17 16:55:53 -07:00
|
|
|
&working_bank,
|
2021-03-15 17:11:15 -07:00
|
|
|
&bank_creation_time,
|
2021-06-18 06:34:46 -07:00
|
|
|
recorder,
|
|
|
|
msgs,
|
2021-02-12 03:27:37 -08:00
|
|
|
original_unprocessed_indexes.to_owned(),
|
|
|
|
transaction_status_sender.clone(),
|
|
|
|
gossip_vote_sender,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
2021-03-15 17:11:15 -07:00
|
|
|
if processed < verified_txs_len
|
|
|
|
|| !Bank::should_bank_still_be_processing_txs(
|
|
|
|
&bank_creation_time,
|
|
|
|
max_tx_ingestion_ns,
|
|
|
|
)
|
|
|
|
{
|
2021-09-17 16:55:53 -07:00
|
|
|
reached_end_of_slot = Some((
|
|
|
|
poh_recorder.lock().unwrap().next_slot_leader(),
|
|
|
|
working_bank,
|
|
|
|
));
|
2021-02-12 03:27:37 -08:00
|
|
|
}
|
|
|
|
new_tx_count += processed;
|
|
|
|
// Out of the buffered packets just retried, collect any still unprocessed
|
|
|
|
// transactions in this batch for forwarding
|
|
|
|
rebuffered_packets_len += new_unprocessed_indexes.len();
|
|
|
|
let has_more_unprocessed_transactions =
|
|
|
|
Self::update_buffered_packets_with_new_unprocessed(
|
|
|
|
original_unprocessed_indexes,
|
|
|
|
new_unprocessed_indexes,
|
|
|
|
);
|
|
|
|
if let Some(test_fn) = &test_fn {
|
|
|
|
test_fn();
|
|
|
|
}
|
|
|
|
has_more_unprocessed_transactions
|
|
|
|
} else {
|
|
|
|
rebuffered_packets_len += original_unprocessed_indexes.len();
|
|
|
|
// `original_unprocessed_indexes` must have remaining packets to process
|
|
|
|
// if not yet processed.
|
|
|
|
assert!(Self::packet_has_more_unprocessed_transactions(
|
2021-06-18 06:34:46 -07:00
|
|
|
original_unprocessed_indexes
|
2021-02-12 03:27:37 -08:00
|
|
|
));
|
|
|
|
true
|
2019-05-13 14:40:05 -07:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 03:27:37 -08:00
|
|
|
});
|
2019-04-26 17:27:31 -07:00
|
|
|
|
2019-06-29 06:34:49 -07:00
|
|
|
proc_start.stop();
|
2019-05-20 09:15:00 -07:00
|
|
|
|
|
|
|
debug!(
|
|
|
|
"@{:?} done processing buffered batches: {} time: {:?}ms tx count: {} tx/s: {}",
|
2019-09-06 14:30:56 -07:00
|
|
|
timestamp(),
|
2019-05-20 09:15:00 -07:00
|
|
|
buffered_len,
|
2019-06-29 06:34:49 -07:00
|
|
|
proc_start.as_ms(),
|
2019-05-20 09:15:00 -07:00
|
|
|
new_tx_count,
|
2019-06-29 06:34:49 -07:00
|
|
|
(new_tx_count as f32) / (proc_start.as_s())
|
2019-05-20 09:15:00 -07:00
|
|
|
);
|
|
|
|
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.consume_buffered_packets_elapsed
|
|
|
|
.fetch_add(proc_start.as_us(), Ordering::Relaxed);
|
|
|
|
banking_stage_stats
|
|
|
|
.rebuffered_packets_count
|
|
|
|
.fetch_add(rebuffered_packets_len, Ordering::Relaxed);
|
|
|
|
banking_stage_stats
|
|
|
|
.consumed_buffered_packets_count
|
|
|
|
.fetch_add(new_tx_count, Ordering::Relaxed);
|
2019-04-11 17:23:45 -07:00
|
|
|
}
|
2019-03-29 11:20:36 -07:00
|
|
|
|
2019-05-01 15:13:10 -07:00
|
|
|
fn consume_or_forward_packets(
|
2019-07-18 14:54:27 -07:00
|
|
|
my_pubkey: &Pubkey,
|
2019-05-23 23:20:04 -07:00
|
|
|
leader_pubkey: Option<Pubkey>,
|
2021-03-15 17:11:15 -07:00
|
|
|
bank_still_processing_txs: Option<&Arc<Bank>>,
|
2019-04-23 11:56:30 -07:00
|
|
|
would_be_leader: bool,
|
2021-03-03 10:23:05 -08:00
|
|
|
would_be_leader_shortly: bool,
|
2019-04-13 23:19:54 -07:00
|
|
|
) -> BufferedPacketsDecision {
|
2019-05-23 23:20:04 -07:00
|
|
|
leader_pubkey.map_or(
|
2019-04-13 23:19:54 -07:00
|
|
|
// If leader is not known, return the buffered packets as is
|
|
|
|
BufferedPacketsDecision::Hold,
|
|
|
|
// else process the packets
|
|
|
|
|x| {
|
2021-03-15 17:11:15 -07:00
|
|
|
if let Some(bank) = bank_still_processing_txs {
|
2019-04-13 23:19:54 -07:00
|
|
|
// If the bank is available, this node is the leader
|
2021-03-15 17:11:15 -07:00
|
|
|
BufferedPacketsDecision::Consume(bank.ns_per_slot)
|
2021-03-03 10:23:05 -08:00
|
|
|
} else if would_be_leader_shortly {
|
2019-04-23 11:56:30 -07:00
|
|
|
// If the node will be the leader soon, hold the packets for now
|
|
|
|
BufferedPacketsDecision::Hold
|
2021-03-03 10:23:05 -08:00
|
|
|
} else if would_be_leader {
|
|
|
|
// Node will be leader within ~20 slots, hold the transactions in
|
|
|
|
// case it is the only node which produces an accepted slot.
|
|
|
|
BufferedPacketsDecision::ForwardAndHold
|
2019-05-23 23:20:04 -07:00
|
|
|
} else if x != *my_pubkey {
|
2019-04-13 23:19:54 -07:00
|
|
|
// If the current node is not the leader, forward the buffered packets
|
|
|
|
BufferedPacketsDecision::Forward
|
|
|
|
} else {
|
|
|
|
// We don't know the leader. Hold the packets for now
|
|
|
|
BufferedPacketsDecision::Hold
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-07 11:21:35 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2019-05-01 15:13:10 -07:00
|
|
|
fn process_buffered_packets(
|
2019-06-21 15:21:49 -07:00
|
|
|
my_pubkey: &Pubkey,
|
2019-04-11 17:23:45 -07:00
|
|
|
socket: &std::net::UdpSocket,
|
|
|
|
poh_recorder: &Arc<Mutex<PohRecorder>>,
|
2020-04-21 12:54:45 -07:00
|
|
|
cluster_info: &ClusterInfo,
|
2021-02-12 03:27:37 -08:00
|
|
|
buffered_packets: &mut UnprocessedPackets,
|
2021-10-07 02:38:23 -07:00
|
|
|
forward_option: &ForwardOption,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: &ReplayVoteSender,
|
2021-02-12 03:27:37 -08:00
|
|
|
banking_stage_stats: &BankingStageStats,
|
2021-03-23 07:10:04 -07:00
|
|
|
recorder: &TransactionRecorder,
|
2021-09-21 08:49:41 -07:00
|
|
|
data_budget: &DataBudget,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: &Arc<RwLock<CostModel>>,
|
2020-05-22 15:01:01 -07:00
|
|
|
) -> BufferedPacketsDecision {
|
2021-03-15 17:11:15 -07:00
|
|
|
let bank_start;
|
|
|
|
let (
|
|
|
|
leader_at_slot_offset,
|
|
|
|
bank_still_processing_txs,
|
|
|
|
would_be_leader,
|
|
|
|
would_be_leader_shortly,
|
|
|
|
) = {
|
2019-04-23 11:56:30 -07:00
|
|
|
let poh = poh_recorder.lock().unwrap();
|
2021-03-15 17:11:15 -07:00
|
|
|
bank_start = poh.bank_start();
|
2019-07-18 14:54:27 -07:00
|
|
|
(
|
2019-10-19 20:28:33 -07:00
|
|
|
poh.leader_after_n_slots(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET),
|
2021-09-17 16:55:53 -07:00
|
|
|
PohRecorder::get_working_bank_if_not_expired(&bank_start.as_ref()),
|
2021-03-03 10:23:05 -08:00
|
|
|
poh.would_be_leader(HOLD_TRANSACTIONS_SLOT_OFFSET * DEFAULT_TICKS_PER_SLOT),
|
2019-06-24 15:56:50 -07:00
|
|
|
poh.would_be_leader(
|
|
|
|
(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET - 1) * DEFAULT_TICKS_PER_SLOT,
|
2019-05-01 15:13:10 -07:00
|
|
|
),
|
2019-04-23 11:56:30 -07:00
|
|
|
)
|
|
|
|
};
|
2019-03-08 16:48:15 -08:00
|
|
|
|
2019-07-18 14:54:27 -07:00
|
|
|
let decision = Self::consume_or_forward_packets(
|
|
|
|
my_pubkey,
|
2019-10-02 14:07:34 -07:00
|
|
|
leader_at_slot_offset,
|
2021-03-15 17:11:15 -07:00
|
|
|
bank_still_processing_txs,
|
2019-07-18 14:54:27 -07:00
|
|
|
would_be_leader,
|
2021-03-03 10:23:05 -08:00
|
|
|
would_be_leader_shortly,
|
2019-07-18 14:54:27 -07:00
|
|
|
);
|
|
|
|
|
2019-04-13 23:19:54 -07:00
|
|
|
match decision {
|
2021-03-15 17:11:15 -07:00
|
|
|
BufferedPacketsDecision::Consume(max_tx_ingestion_ns) => {
|
2021-02-12 03:27:37 -08:00
|
|
|
Self::consume_buffered_packets(
|
2019-06-28 01:55:24 -07:00
|
|
|
my_pubkey,
|
2021-03-15 17:11:15 -07:00
|
|
|
max_tx_ingestion_ns,
|
2019-06-28 01:55:24 -07:00
|
|
|
poh_recorder,
|
|
|
|
buffered_packets,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender,
|
2021-02-12 03:27:37 -08:00
|
|
|
None::<Box<dyn Fn()>>,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats,
|
2021-03-23 07:10:04 -07:00
|
|
|
recorder,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2020-01-02 19:50:43 -08:00
|
|
|
);
|
2019-04-13 23:19:54 -07:00
|
|
|
}
|
|
|
|
BufferedPacketsDecision::Forward => {
|
2021-03-03 10:23:05 -08:00
|
|
|
Self::handle_forwarding(
|
2021-10-07 02:38:23 -07:00
|
|
|
forward_option,
|
2021-03-03 10:23:05 -08:00
|
|
|
cluster_info,
|
|
|
|
buffered_packets,
|
|
|
|
poh_recorder,
|
|
|
|
socket,
|
|
|
|
false,
|
2021-09-21 08:49:41 -07:00
|
|
|
data_budget,
|
2021-03-03 10:23:05 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
BufferedPacketsDecision::ForwardAndHold => {
|
|
|
|
Self::handle_forwarding(
|
2021-10-07 02:38:23 -07:00
|
|
|
forward_option,
|
2021-03-03 10:23:05 -08:00
|
|
|
cluster_info,
|
|
|
|
buffered_packets,
|
|
|
|
poh_recorder,
|
|
|
|
socket,
|
|
|
|
true,
|
2021-09-21 08:49:41 -07:00
|
|
|
data_budget,
|
2021-03-03 10:23:05 -08:00
|
|
|
);
|
2019-04-13 23:19:54 -07:00
|
|
|
}
|
2020-01-02 19:50:43 -08:00
|
|
|
_ => (),
|
2019-03-08 16:48:15 -08:00
|
|
|
}
|
2020-05-22 15:01:01 -07:00
|
|
|
decision
|
2019-03-08 16:48:15 -08:00
|
|
|
}
|
|
|
|
|
2021-03-03 10:23:05 -08:00
|
|
|
fn handle_forwarding(
|
2021-10-07 02:38:23 -07:00
|
|
|
forward_option: &ForwardOption,
|
2021-03-03 10:23:05 -08:00
|
|
|
cluster_info: &ClusterInfo,
|
|
|
|
buffered_packets: &mut UnprocessedPackets,
|
|
|
|
poh_recorder: &Arc<Mutex<PohRecorder>>,
|
|
|
|
socket: &UdpSocket,
|
|
|
|
hold: bool,
|
2021-09-21 08:49:41 -07:00
|
|
|
data_budget: &DataBudget,
|
2021-03-03 10:23:05 -08:00
|
|
|
) {
|
2021-10-07 02:38:23 -07:00
|
|
|
let addr = match forward_option {
|
|
|
|
ForwardOption::NotForward => {
|
|
|
|
if !hold {
|
|
|
|
buffered_packets.clear();
|
|
|
|
}
|
|
|
|
return;
|
2021-04-05 05:46:21 -07:00
|
|
|
}
|
2021-10-07 02:38:23 -07:00
|
|
|
ForwardOption::ForwardTransaction => {
|
|
|
|
next_leader_tpu_forwards(cluster_info, poh_recorder)
|
|
|
|
}
|
|
|
|
ForwardOption::ForwardTpuVote => next_leader_tpu_vote(cluster_info, poh_recorder),
|
|
|
|
};
|
|
|
|
let addr = match addr {
|
2021-04-05 05:46:21 -07:00
|
|
|
Some(addr) => addr,
|
|
|
|
None => return,
|
|
|
|
};
|
2021-09-21 08:49:41 -07:00
|
|
|
let _ = Self::forward_buffered_packets(socket, &addr, buffered_packets, data_budget);
|
2021-04-05 05:46:21 -07:00
|
|
|
if hold {
|
|
|
|
buffered_packets.retain(|(_, index, _)| !index.is_empty());
|
|
|
|
for (_, _, forwarded) in buffered_packets.iter_mut() {
|
|
|
|
*forwarded = true;
|
|
|
|
}
|
2021-03-03 10:23:05 -08:00
|
|
|
} else {
|
|
|
|
buffered_packets.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 11:21:35 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-06-17 10:14:21 -07:00
|
|
|
fn process_loop(
|
2019-11-01 14:23:03 -07:00
|
|
|
verified_receiver: &CrossbeamReceiver<Vec<Packets>>,
|
2019-03-03 16:44:06 -08:00
|
|
|
poh_recorder: &Arc<Mutex<PohRecorder>>,
|
2020-04-21 12:54:45 -07:00
|
|
|
cluster_info: &ClusterInfo,
|
2019-04-09 11:17:15 -07:00
|
|
|
recv_start: &mut Instant,
|
2021-10-07 02:38:23 -07:00
|
|
|
forward_option: ForwardOption,
|
2019-05-20 09:15:00 -07:00
|
|
|
id: u32,
|
2019-06-28 01:55:24 -07:00
|
|
|
batch_limit: usize,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: ReplayVoteSender,
|
2021-03-03 10:23:05 -08:00
|
|
|
duplicates: &Arc<Mutex<(LruCache<u64, ()>, PacketHasher)>>,
|
2021-09-21 08:49:41 -07:00
|
|
|
data_budget: &DataBudget,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: Arc<RwLock<CostModel>>,
|
2019-03-03 16:44:06 -08:00
|
|
|
) {
|
2021-03-23 07:10:04 -07:00
|
|
|
let recorder = poh_recorder.lock().unwrap().recorder();
|
2019-03-06 12:01:21 -08:00
|
|
|
let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
2021-02-12 03:27:37 -08:00
|
|
|
let mut buffered_packets = VecDeque::with_capacity(batch_limit);
|
|
|
|
let banking_stage_stats = BankingStageStats::new(id);
|
2019-03-03 16:44:06 -08:00
|
|
|
loop {
|
2021-06-17 13:51:06 -07:00
|
|
|
let my_pubkey = cluster_info.id();
|
2020-05-22 15:01:01 -07:00
|
|
|
while !buffered_packets.is_empty() {
|
|
|
|
let decision = Self::process_buffered_packets(
|
2019-06-21 15:21:49 -07:00
|
|
|
&my_pubkey,
|
2019-04-11 17:23:45 -07:00
|
|
|
&socket,
|
2021-06-18 06:34:46 -07:00
|
|
|
poh_recorder,
|
2019-04-11 17:23:45 -07:00
|
|
|
cluster_info,
|
2019-05-24 17:35:09 -07:00
|
|
|
&mut buffered_packets,
|
2021-10-07 02:38:23 -07:00
|
|
|
&forward_option,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender.clone(),
|
2020-08-07 11:21:35 -07:00
|
|
|
&gossip_vote_sender,
|
2021-02-12 03:27:37 -08:00
|
|
|
&banking_stage_stats,
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2021-09-21 08:49:41 -07:00
|
|
|
data_budget,
|
2021-10-19 12:37:33 -07:00
|
|
|
&cost_model,
|
2020-01-02 19:50:43 -08:00
|
|
|
);
|
2021-03-15 17:11:15 -07:00
|
|
|
if matches!(decision, BufferedPacketsDecision::Hold)
|
|
|
|
|| matches!(decision, BufferedPacketsDecision::ForwardAndHold)
|
2021-03-03 10:23:05 -08:00
|
|
|
{
|
2020-05-22 15:01:01 -07:00
|
|
|
// If we are waiting on a new bank,
|
|
|
|
// check the receiver for more transactions/for exiting
|
|
|
|
break;
|
|
|
|
}
|
2019-03-08 16:48:15 -08:00
|
|
|
}
|
|
|
|
|
2019-04-14 12:34:07 -07:00
|
|
|
let recv_timeout = if !buffered_packets.is_empty() {
|
|
|
|
// If packets are buffered, let's wait for less time on recv from the channel.
|
|
|
|
// This helps detect the next leader faster, and processing the buffered
|
|
|
|
// packets quickly
|
|
|
|
Duration::from_millis(10)
|
|
|
|
} else {
|
|
|
|
// Default wait time
|
|
|
|
Duration::from_millis(100)
|
|
|
|
};
|
|
|
|
|
2019-05-20 09:15:00 -07:00
|
|
|
match Self::process_packets(
|
2019-06-21 15:21:49 -07:00
|
|
|
&my_pubkey,
|
2021-06-18 06:34:46 -07:00
|
|
|
verified_receiver,
|
|
|
|
poh_recorder,
|
2019-05-20 09:15:00 -07:00
|
|
|
recv_start,
|
|
|
|
recv_timeout,
|
|
|
|
id,
|
2019-06-28 01:55:24 -07:00
|
|
|
batch_limit,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender.clone(),
|
2020-08-07 11:21:35 -07:00
|
|
|
&gossip_vote_sender,
|
2021-02-12 03:27:37 -08:00
|
|
|
&mut buffered_packets,
|
|
|
|
&banking_stage_stats,
|
2021-03-03 10:23:05 -08:00
|
|
|
duplicates,
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2021-10-19 12:37:33 -07:00
|
|
|
&cost_model,
|
2019-05-20 09:15:00 -07:00
|
|
|
) {
|
2021-02-12 03:27:37 -08:00
|
|
|
Ok(()) | Err(RecvTimeoutError::Timeout) => (),
|
2020-01-02 19:50:43 -08:00
|
|
|
Err(RecvTimeoutError::Disconnected) => break,
|
2019-03-03 16:44:06 -08:00
|
|
|
}
|
2021-02-12 03:27:37 -08:00
|
|
|
|
2021-03-03 10:58:47 -08:00
|
|
|
banking_stage_stats.report(1000);
|
2019-03-03 16:44:06 -08:00
|
|
|
}
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
|
|
|
|
2018-12-21 13:55:45 -08:00
|
|
|
pub fn num_threads() -> u32 {
|
2019-06-28 01:55:24 -07:00
|
|
|
cmp::max(
|
|
|
|
env::var("SOLANA_BANKING_THREADS")
|
|
|
|
.map(|x| x.parse().unwrap_or(NUM_THREADS))
|
|
|
|
.unwrap_or(NUM_THREADS),
|
2021-10-07 02:38:23 -07:00
|
|
|
NUM_VOTE_PROCESSING_THREADS + MIN_THREADS_BANKING,
|
2019-06-28 01:55:24 -07:00
|
|
|
)
|
2018-12-21 13:55:45 -08:00
|
|
|
}
|
|
|
|
|
2019-06-26 22:39:50 -07:00
|
|
|
#[allow(clippy::match_wild_err_arm)]
|
2021-08-17 15:17:56 -07:00
|
|
|
fn record_transactions(
|
2019-11-02 00:38:30 -07:00
|
|
|
bank_slot: Slot,
|
2021-08-17 15:17:56 -07:00
|
|
|
txs: &[SanitizedTransaction],
|
2020-11-29 11:21:55 -08:00
|
|
|
results: &[TransactionExecutionResult],
|
2021-03-23 07:10:04 -07:00
|
|
|
recorder: &TransactionRecorder,
|
2020-01-02 19:50:43 -08:00
|
|
|
) -> (Result<usize, PohRecorderError>, Vec<usize>) {
|
2019-06-29 06:34:49 -07:00
|
|
|
let mut processed_generation = Measure::start("record::process_generation");
|
2019-07-01 12:14:40 -07:00
|
|
|
let (processed_transactions, processed_transactions_indexes): (Vec<_>, Vec<_>) = results
|
2019-02-16 14:02:21 -08:00
|
|
|
.iter()
|
2021-04-12 23:28:08 -07:00
|
|
|
.zip(txs)
|
2019-06-26 22:39:50 -07:00
|
|
|
.enumerate()
|
2021-08-17 15:17:56 -07:00
|
|
|
.filter_map(|(i, ((r, _n), tx))| {
|
2019-04-23 15:32:19 -07:00
|
|
|
if Bank::can_commit(r) {
|
2021-08-17 15:17:56 -07:00
|
|
|
Some((tx.to_versioned_transaction(), i))
|
2019-04-23 15:32:19 -07:00
|
|
|
} else {
|
2019-02-16 14:02:21 -08:00
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2019-07-01 12:14:40 -07:00
|
|
|
.unzip();
|
2019-06-26 22:39:50 -07:00
|
|
|
|
2019-07-01 12:14:40 -07:00
|
|
|
processed_generation.stop();
|
2019-08-09 21:14:20 -07:00
|
|
|
let num_to_commit = processed_transactions.len();
|
|
|
|
debug!("num_to_commit: {} ", num_to_commit);
|
2019-02-16 14:02:21 -08:00
|
|
|
// unlock all the accounts with errors which are filtered by the above `filter_map`
|
|
|
|
if !processed_transactions.is_empty() {
|
2021-03-02 17:28:15 -08:00
|
|
|
inc_new_counter_info!("banking_stage-record_count", 1);
|
2020-12-14 16:26:17 -08:00
|
|
|
inc_new_counter_info!("banking_stage-record_transactions", num_to_commit);
|
2019-06-29 06:34:49 -07:00
|
|
|
|
|
|
|
let mut hash_time = Measure::start("record::hash");
|
2019-06-26 22:39:50 -07:00
|
|
|
let hash = hash_transactions(&processed_transactions[..]);
|
2019-06-29 06:34:49 -07:00
|
|
|
hash_time.stop();
|
|
|
|
|
|
|
|
let mut poh_record = Measure::start("record::poh_record");
|
2019-05-07 15:51:35 -07:00
|
|
|
// record and unlock will unlock all the successful transactions
|
2021-03-23 07:10:04 -07:00
|
|
|
let res = recorder.record(bank_slot, hash, processed_transactions);
|
2019-06-26 22:39:50 -07:00
|
|
|
match res {
|
|
|
|
Ok(()) => (),
|
2020-01-02 19:50:43 -08:00
|
|
|
Err(PohRecorderError::MaxHeightReached) => {
|
2021-03-02 17:28:15 -08:00
|
|
|
inc_new_counter_info!("banking_stage-max_height_reached", 1);
|
|
|
|
inc_new_counter_info!(
|
|
|
|
"banking_stage-max_height_reached_num_to_commit",
|
|
|
|
num_to_commit
|
|
|
|
);
|
2019-07-01 12:14:40 -07:00
|
|
|
// If record errors, add all the committable transactions (the ones
|
|
|
|
// we just attempted to record) as retryable
|
2019-08-09 21:14:20 -07:00
|
|
|
return (
|
2020-01-02 19:50:43 -08:00
|
|
|
Err(PohRecorderError::MaxHeightReached),
|
2019-08-09 21:14:20 -07:00
|
|
|
processed_transactions_indexes,
|
|
|
|
);
|
2019-06-26 22:39:50 -07:00
|
|
|
}
|
2021-02-18 23:42:09 -08:00
|
|
|
Err(e) => panic!("Poh recorder returned unexpected error: {:?}", e),
|
2019-06-26 22:39:50 -07:00
|
|
|
}
|
2019-06-29 06:34:49 -07:00
|
|
|
poh_record.stop();
|
2019-02-16 14:02:21 -08:00
|
|
|
}
|
2019-08-09 21:14:20 -07:00
|
|
|
(Ok(num_to_commit), vec![])
|
2019-02-16 14:02:21 -08:00
|
|
|
}
|
|
|
|
|
2019-03-23 13:30:56 -07:00
|
|
|
fn process_and_record_transactions_locked(
|
2019-11-20 15:43:10 -08:00
|
|
|
bank: &Arc<Bank>,
|
2021-03-23 07:10:04 -07:00
|
|
|
poh: &TransactionRecorder,
|
2019-09-19 10:06:08 -07:00
|
|
|
batch: &TransactionBatch,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: &ReplayVoteSender,
|
2020-01-02 19:50:43 -08:00
|
|
|
) -> (Result<usize, PohRecorderError>, Vec<usize>) {
|
2019-06-29 06:34:49 -07:00
|
|
|
let mut load_execute_time = Measure::start("load_execute_time");
|
2019-02-16 14:02:21 -08:00
|
|
|
// Use a shorter maximum age when adding transactions into the pipeline. This will reduce
|
|
|
|
// the likelihood of any single thread getting starved and processing old ids.
|
|
|
|
// TODO: Banking stage threads should be prioritized to complete faster then this queue
|
|
|
|
// expires.
|
2019-12-18 09:56:29 -08:00
|
|
|
let pre_balances = if transaction_status_sender.is_some() {
|
2020-07-23 12:35:10 -07:00
|
|
|
bank.collect_balances(batch)
|
2019-12-18 09:56:29 -08:00
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
2020-12-10 19:25:07 -08:00
|
|
|
|
|
|
|
let mut mint_decimals: HashMap<Pubkey, u8> = HashMap::new();
|
|
|
|
|
|
|
|
let pre_token_balances = if transaction_status_sender.is_some() {
|
2021-06-18 06:34:46 -07:00
|
|
|
collect_token_balances(bank, batch, &mut mint_decimals)
|
2020-12-10 19:25:07 -08:00
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
2021-01-14 14:14:16 -08:00
|
|
|
let mut execute_timings = ExecuteTimings::default();
|
2020-09-24 07:36:22 -07:00
|
|
|
let (
|
|
|
|
mut loaded_accounts,
|
|
|
|
results,
|
|
|
|
inner_instructions,
|
2020-10-08 12:06:15 -07:00
|
|
|
transaction_logs,
|
2020-09-24 07:36:22 -07:00
|
|
|
mut retryable_txs,
|
|
|
|
tx_count,
|
|
|
|
signature_count,
|
|
|
|
) = bank.load_and_execute_transactions(
|
|
|
|
batch,
|
|
|
|
MAX_PROCESSING_AGE,
|
2020-10-08 12:06:15 -07:00
|
|
|
transaction_status_sender.is_some(),
|
2020-09-24 07:36:22 -07:00
|
|
|
transaction_status_sender.is_some(),
|
2021-01-14 14:14:16 -08:00
|
|
|
&mut execute_timings,
|
2020-09-24 07:36:22 -07:00
|
|
|
);
|
2019-06-29 06:34:49 -07:00
|
|
|
load_execute_time.stop();
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2019-05-23 17:35:15 -07:00
|
|
|
let freeze_lock = bank.freeze_lock();
|
|
|
|
|
2019-08-09 21:14:20 -07:00
|
|
|
let mut record_time = Measure::start("record_time");
|
|
|
|
let (num_to_commit, retryable_record_txs) =
|
2021-08-17 15:17:56 -07:00
|
|
|
Self::record_transactions(bank.slot(), batch.sanitized_transactions(), &results, poh);
|
2021-03-02 17:28:15 -08:00
|
|
|
inc_new_counter_info!(
|
|
|
|
"banking_stage-record_transactions_num_to_commit",
|
|
|
|
*num_to_commit.as_ref().unwrap_or(&0)
|
|
|
|
);
|
|
|
|
inc_new_counter_info!(
|
|
|
|
"banking_stage-record_transactions_retryable_record_txs",
|
|
|
|
retryable_record_txs.len()
|
|
|
|
);
|
2019-08-09 21:14:20 -07:00
|
|
|
retryable_txs.extend(retryable_record_txs);
|
|
|
|
if num_to_commit.is_err() {
|
|
|
|
return (num_to_commit, retryable_txs);
|
|
|
|
}
|
|
|
|
record_time.stop();
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2019-08-09 21:14:20 -07:00
|
|
|
let mut commit_time = Measure::start("commit_time");
|
2021-07-15 20:51:27 -07:00
|
|
|
let sanitized_txs = batch.sanitized_transactions();
|
2019-08-09 21:14:20 -07:00
|
|
|
let num_to_commit = num_to_commit.unwrap();
|
|
|
|
if num_to_commit != 0 {
|
2020-08-07 11:21:35 -07:00
|
|
|
let tx_results = bank.commit_transactions(
|
2021-07-15 20:51:27 -07:00
|
|
|
sanitized_txs,
|
2020-08-07 11:21:35 -07:00
|
|
|
&mut loaded_accounts,
|
|
|
|
&results,
|
|
|
|
tx_count,
|
|
|
|
signature_count,
|
2021-01-14 14:14:16 -08:00
|
|
|
&mut execute_timings,
|
2020-08-07 11:21:35 -07:00
|
|
|
);
|
2019-12-18 09:56:29 -08:00
|
|
|
|
2021-07-15 20:51:27 -07:00
|
|
|
bank_utils::find_and_send_votes(sanitized_txs, &tx_results, Some(gossip_vote_sender));
|
2021-02-01 13:00:51 -08:00
|
|
|
if let Some(transaction_status_sender) = transaction_status_sender {
|
2021-08-17 15:17:56 -07:00
|
|
|
let txs = batch.sanitized_transactions().to_vec();
|
2020-07-23 12:35:10 -07:00
|
|
|
let post_balances = bank.collect_balances(batch);
|
2021-06-18 06:34:46 -07:00
|
|
|
let post_token_balances = collect_token_balances(bank, batch, &mut mint_decimals);
|
2021-03-26 15:47:35 -07:00
|
|
|
transaction_status_sender.send_transaction_status_batch(
|
2019-11-20 15:43:10 -08:00
|
|
|
bank.clone(),
|
2021-04-12 23:28:08 -07:00
|
|
|
txs,
|
2020-11-29 11:21:55 -08:00
|
|
|
tx_results.execution_results,
|
2019-12-18 09:56:29 -08:00
|
|
|
TransactionBalancesSet::new(pre_balances, post_balances),
|
2020-12-10 19:25:07 -08:00
|
|
|
TransactionTokenBalancesSet::new(pre_token_balances, post_token_balances),
|
2020-09-24 07:36:22 -07:00
|
|
|
inner_instructions,
|
2020-10-08 12:06:15 -07:00
|
|
|
transaction_logs,
|
2021-05-26 14:43:15 -07:00
|
|
|
tx_results.rent_debits,
|
2019-11-20 15:43:10 -08:00
|
|
|
);
|
|
|
|
}
|
2019-08-09 21:14:20 -07:00
|
|
|
}
|
|
|
|
commit_time.stop();
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2019-05-23 17:35:15 -07:00
|
|
|
drop(freeze_lock);
|
2019-05-07 15:51:35 -07:00
|
|
|
|
2019-03-23 13:30:56 -07:00
|
|
|
debug!(
|
2019-08-09 21:14:20 -07:00
|
|
|
"bank: {} process_and_record_locked: {}us record: {}us commit: {}us txs_len: {}",
|
2019-03-23 13:30:56 -07:00
|
|
|
bank.slot(),
|
2019-06-29 06:34:49 -07:00
|
|
|
load_execute_time.as_us(),
|
|
|
|
record_time.as_us(),
|
|
|
|
commit_time.as_us(),
|
2021-07-15 20:51:27 -07:00
|
|
|
sanitized_txs.len(),
|
2019-03-23 13:30:56 -07:00
|
|
|
);
|
|
|
|
|
2021-03-03 15:07:45 -08:00
|
|
|
debug!(
|
|
|
|
"process_and_record_transactions_locked: {:?}",
|
|
|
|
execute_timings
|
|
|
|
);
|
|
|
|
|
2019-08-09 21:14:20 -07:00
|
|
|
(Ok(num_to_commit), retryable_txs)
|
2019-03-23 13:30:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_and_record_transactions(
|
2019-11-20 15:43:10 -08:00
|
|
|
bank: &Arc<Bank>,
|
2021-07-15 20:51:27 -07:00
|
|
|
txs: &[SanitizedTransaction],
|
2021-03-23 07:10:04 -07:00
|
|
|
poh: &TransactionRecorder,
|
2019-05-07 10:23:02 -07:00
|
|
|
chunk_offset: usize,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: &ReplayVoteSender,
|
2020-01-02 19:50:43 -08:00
|
|
|
) -> (Result<usize, PohRecorderError>, Vec<usize>) {
|
2019-06-29 06:34:49 -07:00
|
|
|
let mut lock_time = Measure::start("lock_time");
|
2019-03-23 13:30:56 -07:00
|
|
|
// Once accounts are locked, other threads cannot encode transactions that will modify the
|
|
|
|
// same account state
|
2021-07-15 20:51:27 -07:00
|
|
|
let batch = bank.prepare_sanitized_batch(txs);
|
2019-06-29 06:34:49 -07:00
|
|
|
lock_time.stop();
|
2019-03-23 13:30:56 -07:00
|
|
|
|
2019-11-20 15:43:10 -08:00
|
|
|
let (result, mut retryable_txs) = Self::process_and_record_transactions_locked(
|
|
|
|
bank,
|
|
|
|
poh,
|
|
|
|
&batch,
|
|
|
|
transaction_status_sender,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender,
|
2019-11-20 15:43:10 -08:00
|
|
|
);
|
2019-06-26 22:39:50 -07:00
|
|
|
retryable_txs.iter_mut().for_each(|x| *x += chunk_offset);
|
2019-03-23 13:30:56 -07:00
|
|
|
|
2019-06-29 06:34:49 -07:00
|
|
|
let mut unlock_time = Measure::start("unlock_time");
|
2019-02-16 14:02:21 -08:00
|
|
|
// Once the accounts are new transactions can enter the pipeline to process them
|
2019-09-19 10:06:08 -07:00
|
|
|
drop(batch);
|
2019-06-29 06:34:49 -07:00
|
|
|
unlock_time.stop();
|
2019-03-23 13:30:56 -07:00
|
|
|
|
2019-02-16 14:02:21 -08:00
|
|
|
debug!(
|
2019-03-23 13:30:56 -07:00
|
|
|
"bank: {} lock: {}us unlock: {}us txs_len: {}",
|
2019-03-03 16:44:06 -08:00
|
|
|
bank.slot(),
|
2019-06-29 06:34:49 -07:00
|
|
|
lock_time.as_us(),
|
|
|
|
unlock_time.as_us(),
|
2019-02-16 14:02:21 -08:00
|
|
|
txs.len(),
|
|
|
|
);
|
2019-03-23 13:30:56 -07:00
|
|
|
|
2019-06-26 22:39:50 -07:00
|
|
|
(result, retryable_txs)
|
2019-02-16 14:02:21 -08:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:12:14 -08:00
|
|
|
/// Sends transactions to the bank.
|
|
|
|
///
|
|
|
|
/// Returns the number of transactions successfully processed by the bank, which may be less
|
|
|
|
/// than the total number if max PoH height was reached and the bank halted
|
2018-09-21 21:01:13 -07:00
|
|
|
fn process_transactions(
|
2019-11-20 15:43:10 -08:00
|
|
|
bank: &Arc<Bank>,
|
2021-03-15 17:11:15 -07:00
|
|
|
bank_creation_time: &Instant,
|
2021-07-15 20:51:27 -07:00
|
|
|
transactions: &[SanitizedTransaction],
|
2021-03-23 07:10:04 -07:00
|
|
|
poh: &TransactionRecorder,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: &ReplayVoteSender,
|
2019-06-26 22:39:50 -07:00
|
|
|
) -> (usize, Vec<usize>) {
|
2018-09-21 21:01:13 -07:00
|
|
|
let mut chunk_start = 0;
|
2019-05-07 10:23:02 -07:00
|
|
|
let mut unprocessed_txs = vec![];
|
2021-06-01 07:16:17 -07:00
|
|
|
|
2018-09-21 21:01:13 -07:00
|
|
|
while chunk_start != transactions.len() {
|
2019-09-10 11:04:03 -07:00
|
|
|
let chunk_end = std::cmp::min(
|
|
|
|
transactions.len(),
|
|
|
|
chunk_start + MAX_NUM_TRANSACTIONS_PER_BATCH,
|
|
|
|
);
|
2019-06-26 22:39:50 -07:00
|
|
|
let (result, retryable_txs_in_chunk) = Self::process_and_record_transactions(
|
2019-02-16 14:02:21 -08:00
|
|
|
bank,
|
|
|
|
&transactions[chunk_start..chunk_end],
|
|
|
|
poh,
|
2019-05-07 10:23:02 -07:00
|
|
|
chunk_start,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender.clone(),
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender,
|
2019-02-16 14:02:21 -08:00
|
|
|
);
|
2019-06-26 22:39:50 -07:00
|
|
|
trace!("process_transactions result: {:?}", result);
|
|
|
|
|
|
|
|
// Add the retryable txs (transactions that errored in a way that warrants a retry)
|
|
|
|
// to the list of unprocessed txs.
|
|
|
|
unprocessed_txs.extend_from_slice(&retryable_txs_in_chunk);
|
2021-03-15 17:11:15 -07:00
|
|
|
|
|
|
|
// If `bank_creation_time` is None, it's a test so ignore the option so
|
|
|
|
// allow processing
|
|
|
|
let should_bank_still_be_processing_txs =
|
|
|
|
Bank::should_bank_still_be_processing_txs(bank_creation_time, bank.ns_per_slot);
|
|
|
|
match (result, should_bank_still_be_processing_txs) {
|
|
|
|
(Err(PohRecorderError::MaxHeightReached), _) | (_, false) => {
|
|
|
|
info!(
|
|
|
|
"process transactions: max height reached slot: {} height: {}",
|
|
|
|
bank.slot(),
|
|
|
|
bank.tick_height()
|
|
|
|
);
|
|
|
|
// process_and_record_transactions has returned all retryable errors in
|
|
|
|
// transactions[chunk_start..chunk_end], so we just need to push the remaining
|
|
|
|
// transactions into the unprocessed queue.
|
|
|
|
unprocessed_txs.extend(chunk_end..transactions.len());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => (),
|
2019-02-13 19:12:14 -08:00
|
|
|
}
|
2019-06-26 22:39:50 -07:00
|
|
|
// Don't exit early on any other type of error, continue processing...
|
2019-05-07 10:23:02 -07:00
|
|
|
chunk_start = chunk_end;
|
2018-09-21 21:01:13 -07:00
|
|
|
}
|
2019-06-26 22:39:50 -07:00
|
|
|
|
|
|
|
(chunk_start, unprocessed_txs)
|
2018-09-21 21:01:13 -07:00
|
|
|
}
|
|
|
|
|
2019-05-08 10:32:25 -07:00
|
|
|
// This function creates a filter of transaction results with Ok() for every pending
|
|
|
|
// transaction. The non-pending transactions are marked with TransactionError
|
|
|
|
fn prepare_filter_for_pending_transactions(
|
2021-04-12 23:28:08 -07:00
|
|
|
transactions_len: usize,
|
2019-05-08 10:32:25 -07:00
|
|
|
pending_tx_indexes: &[usize],
|
|
|
|
) -> Vec<transaction::Result<()>> {
|
2021-04-12 23:28:08 -07:00
|
|
|
let mut mask = vec![Err(TransactionError::BlockhashNotFound); transactions_len];
|
2019-05-08 10:32:25 -07:00
|
|
|
pending_tx_indexes.iter().for_each(|x| mask[*x] = Ok(()));
|
|
|
|
mask
|
|
|
|
}
|
2019-05-07 10:23:02 -07:00
|
|
|
|
2019-05-08 10:32:25 -07:00
|
|
|
// This function returns a vector containing index of all valid transactions. A valid
|
|
|
|
// transaction has result Ok() as the value
|
|
|
|
fn filter_valid_transaction_indexes(
|
2020-11-29 11:21:55 -08:00
|
|
|
valid_txs: &[TransactionCheckResult],
|
2019-05-08 10:32:25 -07:00
|
|
|
transaction_indexes: &[usize],
|
|
|
|
) -> Vec<usize> {
|
2021-09-08 09:32:38 -07:00
|
|
|
valid_txs
|
2019-05-07 10:23:02 -07:00
|
|
|
.iter()
|
2019-05-08 10:32:25 -07:00
|
|
|
.enumerate()
|
2019-12-07 11:54:10 -08:00
|
|
|
.filter_map(|(index, (x, _h))| if x.is_ok() { Some(index) } else { None })
|
2021-09-08 09:32:38 -07:00
|
|
|
.map(|x| transaction_indexes[x])
|
|
|
|
.collect_vec()
|
2019-05-02 19:05:53 -07:00
|
|
|
}
|
|
|
|
|
2021-04-12 23:28:08 -07:00
|
|
|
/// Read the transaction message from packet data
|
|
|
|
fn packet_message(packet: &Packet) -> Option<&[u8]> {
|
|
|
|
let (sig_len, sig_size) = decode_shortu16_len(&packet.data).ok()?;
|
|
|
|
let msg_start = sig_len
|
|
|
|
.checked_mul(size_of::<Signature>())
|
|
|
|
.and_then(|v| v.checked_add(sig_size))?;
|
|
|
|
let msg_end = packet.meta.size;
|
|
|
|
Some(&packet.data[msg_start..msg_end])
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function deserializes packets into transactions, computes the blake3 hash of transaction messages,
|
|
|
|
// and verifies secp256k1 instructions. A list of valid transactions are returned with their message hashes
|
|
|
|
// and packet indexes.
|
2021-06-01 07:16:17 -07:00
|
|
|
// Also returned is packet indexes for transaction should be retried due to cost limits.
|
2021-06-28 19:34:04 -07:00
|
|
|
#[allow(clippy::needless_collect)]
|
2019-05-13 14:40:05 -07:00
|
|
|
fn transactions_from_packets(
|
2019-05-02 19:05:53 -07:00
|
|
|
msgs: &Packets,
|
2019-05-13 14:40:05 -07:00
|
|
|
transaction_indexes: &[usize],
|
2021-08-30 00:58:45 -07:00
|
|
|
feature_set: &Arc<feature_set::FeatureSet>,
|
2021-10-19 12:37:33 -07:00
|
|
|
read_cost_tracker: &RwLockReadGuard<CostTracker>,
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats: &BankingStageStats,
|
2021-09-03 20:05:30 -07:00
|
|
|
demote_program_write_locks: bool,
|
2021-09-16 06:12:26 -07:00
|
|
|
votes_only: bool,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: &Arc<RwLock<CostModel>>,
|
2021-08-17 15:17:56 -07:00
|
|
|
) -> (Vec<SanitizedTransaction>, Vec<usize>, Vec<usize>) {
|
2021-06-01 07:16:17 -07:00
|
|
|
let mut retryable_transaction_packet_indexes: Vec<usize> = vec![];
|
2021-06-28 19:34:04 -07:00
|
|
|
|
|
|
|
let verified_transactions_with_packet_indexes: Vec<_> = transaction_indexes
|
2021-04-12 23:28:08 -07:00
|
|
|
.iter()
|
|
|
|
.filter_map(|tx_index| {
|
|
|
|
let p = &msgs.packets[*tx_index];
|
2021-10-07 02:38:23 -07:00
|
|
|
if votes_only && !p.meta.is_simple_vote_tx {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-08-17 15:17:56 -07:00
|
|
|
let tx: VersionedTransaction = limited_deserialize(&p.data[0..p.meta.size]).ok()?;
|
2021-07-15 20:51:27 -07:00
|
|
|
let message_bytes = Self::packet_message(p)?;
|
|
|
|
let message_hash = Message::hash_raw_message(message_bytes);
|
2021-08-17 15:17:56 -07:00
|
|
|
let tx = SanitizedTransaction::try_create(tx, message_hash, |_| {
|
|
|
|
Err(TransactionError::UnsupportedVersion)
|
|
|
|
})
|
|
|
|
.ok()?;
|
2021-08-30 00:58:45 -07:00
|
|
|
tx.verify_precompiles(feature_set).ok()?;
|
2021-06-28 19:34:04 -07:00
|
|
|
Some((tx, *tx_index))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
banking_stage_stats.cost_tracker_check_count.fetch_add(
|
|
|
|
verified_transactions_with_packet_indexes.len(),
|
|
|
|
Ordering::Relaxed,
|
|
|
|
);
|
2021-06-01 07:16:17 -07:00
|
|
|
|
2021-06-28 19:34:04 -07:00
|
|
|
let mut cost_tracker_check_time = Measure::start("cost_tracker_check_time");
|
2021-07-15 20:51:27 -07:00
|
|
|
let (filtered_transactions, filter_transaction_packet_indexes) = {
|
2021-06-28 19:34:04 -07:00
|
|
|
verified_transactions_with_packet_indexes
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(tx, tx_index)| {
|
2021-10-07 10:49:07 -07:00
|
|
|
// excluding vote TX from cost_model, for now
|
|
|
|
let is_vote = &msgs.packets[tx_index].meta.is_simple_vote_tx;
|
|
|
|
if !is_vote
|
2021-10-19 12:37:33 -07:00
|
|
|
&& read_cost_tracker
|
2021-10-07 10:49:07 -07:00
|
|
|
.would_transaction_fit(
|
|
|
|
&tx,
|
2021-10-19 12:37:33 -07:00
|
|
|
&cost_model
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.calculate_cost(&tx, demote_program_write_locks),
|
2021-10-07 10:49:07 -07:00
|
|
|
)
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
// put transaction into retry queue if it wouldn't fit
|
|
|
|
// into current bank
|
|
|
|
debug!("transaction {:?} would exceed limit", tx);
|
2021-06-28 19:34:04 -07:00
|
|
|
retryable_transaction_packet_indexes.push(tx_index);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some((tx, tx_index))
|
|
|
|
})
|
2021-07-15 20:51:27 -07:00
|
|
|
.unzip()
|
2021-06-28 19:34:04 -07:00
|
|
|
};
|
|
|
|
cost_tracker_check_time.stop();
|
2021-06-01 07:16:17 -07:00
|
|
|
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.cost_tracker_check_elapsed
|
|
|
|
.fetch_add(cost_tracker_check_time.as_us(), Ordering::Relaxed);
|
2021-06-01 07:16:17 -07:00
|
|
|
|
|
|
|
(
|
|
|
|
filtered_transactions,
|
|
|
|
filter_transaction_packet_indexes,
|
|
|
|
retryable_transaction_packet_indexes,
|
|
|
|
)
|
2019-05-13 14:40:05 -07:00
|
|
|
}
|
|
|
|
|
2019-07-01 12:14:40 -07:00
|
|
|
/// This function filters pending packets that are still valid
|
|
|
|
/// # Arguments
|
|
|
|
/// * `transactions` - a batch of transactions deserialized from packets
|
|
|
|
/// * `transaction_to_packet_indexes` - maps each transaction to a packet index
|
|
|
|
/// * `pending_indexes` - identifies which indexes in the `transactions` list are still pending
|
|
|
|
fn filter_pending_packets_from_pending_txs(
|
2019-05-13 14:40:05 -07:00
|
|
|
bank: &Arc<Bank>,
|
2021-07-15 20:51:27 -07:00
|
|
|
transactions: &[SanitizedTransaction],
|
2019-07-01 12:14:40 -07:00
|
|
|
transaction_to_packet_indexes: &[usize],
|
2019-05-13 14:40:05 -07:00
|
|
|
pending_indexes: &[usize],
|
|
|
|
) -> Vec<usize> {
|
2021-04-12 23:28:08 -07:00
|
|
|
let filter =
|
|
|
|
Self::prepare_filter_for_pending_transactions(transactions.len(), pending_indexes);
|
2019-05-13 14:40:05 -07:00
|
|
|
|
|
|
|
let mut error_counters = ErrorCounters::default();
|
2019-06-24 15:56:50 -07:00
|
|
|
// The following code also checks if the blockhash for a transaction is too old
|
|
|
|
// The check accounts for
|
|
|
|
// 1. Transaction forwarding delay
|
|
|
|
// 2. The slot at which the next leader will actually process the transaction
|
|
|
|
// Drop the transaction if it will expire by the time the next node receives and processes it
|
2019-10-09 16:09:36 -07:00
|
|
|
let api = perf_libs::api();
|
|
|
|
let max_tx_fwd_delay = if api.is_none() {
|
|
|
|
MAX_TRANSACTION_FORWARDING_DELAY
|
|
|
|
} else {
|
|
|
|
MAX_TRANSACTION_FORWARDING_DELAY_GPU
|
|
|
|
};
|
2021-04-12 23:28:08 -07:00
|
|
|
|
|
|
|
let results = bank.check_transactions(
|
2019-05-13 14:40:05 -07:00
|
|
|
transactions,
|
|
|
|
&filter,
|
2019-06-24 16:46:34 -07:00
|
|
|
(MAX_PROCESSING_AGE)
|
2019-10-09 16:09:36 -07:00
|
|
|
.saturating_sub(max_tx_fwd_delay)
|
2020-09-03 02:31:28 -07:00
|
|
|
.saturating_sub(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET as usize),
|
2019-05-13 14:40:05 -07:00
|
|
|
&mut error_counters,
|
2019-05-08 10:32:25 -07:00
|
|
|
);
|
|
|
|
|
2021-04-12 23:28:08 -07:00
|
|
|
Self::filter_valid_transaction_indexes(&results, transaction_to_packet_indexes)
|
2019-05-13 14:40:05 -07:00
|
|
|
}
|
2019-05-08 10:32:25 -07:00
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-03-15 17:11:15 -07:00
|
|
|
fn process_packets_transactions(
|
2019-05-13 14:40:05 -07:00
|
|
|
bank: &Arc<Bank>,
|
2021-03-15 17:11:15 -07:00
|
|
|
bank_creation_time: &Instant,
|
2021-03-23 07:10:04 -07:00
|
|
|
poh: &TransactionRecorder,
|
2019-05-13 14:40:05 -07:00
|
|
|
msgs: &Packets,
|
2019-07-01 12:14:40 -07:00
|
|
|
packet_indexes: Vec<usize>,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: &ReplayVoteSender,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats: &BankingStageStats,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: &Arc<RwLock<CostModel>>,
|
2019-06-26 22:39:50 -07:00
|
|
|
) -> (usize, usize, Vec<usize>) {
|
2021-04-12 23:28:08 -07:00
|
|
|
let mut packet_conversion_time = Measure::start("packet_conversion");
|
2021-06-01 07:16:17 -07:00
|
|
|
let (transactions, transaction_to_packet_indexes, retryable_packet_indexes) =
|
|
|
|
Self::transactions_from_packets(
|
|
|
|
msgs,
|
|
|
|
&packet_indexes,
|
2021-08-30 00:58:45 -07:00
|
|
|
&bank.feature_set,
|
2021-10-19 12:37:33 -07:00
|
|
|
&bank.read_cost_tracker().unwrap(),
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats,
|
2021-09-03 20:05:30 -07:00
|
|
|
bank.demote_program_write_locks(),
|
2021-09-16 06:12:26 -07:00
|
|
|
bank.vote_only_bank(),
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2021-06-01 07:16:17 -07:00
|
|
|
);
|
2021-04-12 23:28:08 -07:00
|
|
|
packet_conversion_time.stop();
|
2021-06-28 19:34:04 -07:00
|
|
|
inc_new_counter_info!("banking_stage-packet_conversion", 1);
|
2021-04-12 23:28:08 -07:00
|
|
|
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.cost_forced_retry_transactions_count
|
|
|
|
.fetch_add(retryable_packet_indexes.len(), Ordering::Relaxed);
|
2019-05-08 10:32:25 -07:00
|
|
|
debug!(
|
2021-06-01 07:16:17 -07:00
|
|
|
"bank: {} filtered transactions {} cost limited transactions {}",
|
2019-05-08 10:32:25 -07:00
|
|
|
bank.slot(),
|
2021-06-01 07:16:17 -07:00
|
|
|
transactions.len(),
|
|
|
|
retryable_packet_indexes.len()
|
2019-05-08 10:32:25 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
let tx_len = transactions.len();
|
|
|
|
|
2021-04-12 23:28:08 -07:00
|
|
|
let mut process_tx_time = Measure::start("process_tx_time");
|
2020-08-07 11:21:35 -07:00
|
|
|
let (processed, unprocessed_tx_indexes) = Self::process_transactions(
|
|
|
|
bank,
|
2021-03-15 17:11:15 -07:00
|
|
|
bank_creation_time,
|
2020-08-07 11:21:35 -07:00
|
|
|
&transactions,
|
|
|
|
poh,
|
|
|
|
transaction_status_sender,
|
|
|
|
gossip_vote_sender,
|
|
|
|
);
|
2021-04-12 23:28:08 -07:00
|
|
|
process_tx_time.stop();
|
2019-05-13 14:40:05 -07:00
|
|
|
let unprocessed_tx_count = unprocessed_tx_indexes.len();
|
2021-06-28 19:34:04 -07:00
|
|
|
inc_new_counter_info!(
|
|
|
|
"banking_stage-unprocessed_transactions",
|
|
|
|
unprocessed_tx_count
|
|
|
|
);
|
2019-05-08 10:32:25 -07:00
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
// applying cost of processed transactions to shared cost_tracker
|
2021-06-28 19:34:04 -07:00
|
|
|
let mut cost_tracking_time = Measure::start("cost_tracking_time");
|
2021-07-06 08:41:25 -07:00
|
|
|
transactions.iter().enumerate().for_each(|(index, tx)| {
|
|
|
|
if unprocessed_tx_indexes.iter().all(|&i| i != index) {
|
2021-10-19 12:37:33 -07:00
|
|
|
bank.write_cost_tracker().unwrap().add_transaction_cost(
|
2021-10-05 06:57:39 -07:00
|
|
|
tx,
|
2021-10-19 12:37:33 -07:00
|
|
|
&cost_model
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.calculate_cost(tx, bank.demote_program_write_locks()),
|
2021-10-05 06:57:39 -07:00
|
|
|
);
|
2021-07-06 08:41:25 -07:00
|
|
|
}
|
|
|
|
});
|
2021-06-28 19:34:04 -07:00
|
|
|
cost_tracking_time.stop();
|
2021-06-01 07:16:17 -07:00
|
|
|
|
2021-04-12 23:28:08 -07:00
|
|
|
let mut filter_pending_packets_time = Measure::start("filter_pending_packets_time");
|
2021-06-01 07:16:17 -07:00
|
|
|
let mut filtered_unprocessed_packet_indexes = Self::filter_pending_packets_from_pending_txs(
|
2019-05-13 14:40:05 -07:00
|
|
|
bank,
|
2019-05-08 10:32:25 -07:00
|
|
|
&transactions,
|
2019-07-01 12:14:40 -07:00
|
|
|
&transaction_to_packet_indexes,
|
2019-05-13 14:40:05 -07:00
|
|
|
&unprocessed_tx_indexes,
|
|
|
|
);
|
2021-04-12 23:28:08 -07:00
|
|
|
filter_pending_packets_time.stop();
|
|
|
|
|
2019-05-13 14:40:05 -07:00
|
|
|
inc_new_counter_info!(
|
|
|
|
"banking_stage-dropped_tx_before_forwarding",
|
2019-07-01 12:14:40 -07:00
|
|
|
unprocessed_tx_count.saturating_sub(filtered_unprocessed_packet_indexes.len())
|
2019-05-08 10:32:25 -07:00
|
|
|
);
|
|
|
|
|
2021-06-28 19:34:04 -07:00
|
|
|
// combine cost-related unprocessed transactions with bank determined unprocessed for
|
|
|
|
// buffering
|
|
|
|
filtered_unprocessed_packet_indexes.extend(retryable_packet_indexes);
|
|
|
|
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.packet_conversion_elapsed
|
|
|
|
.fetch_add(packet_conversion_time.as_us(), Ordering::Relaxed);
|
|
|
|
banking_stage_stats
|
|
|
|
.transaction_processing_elapsed
|
|
|
|
.fetch_add(process_tx_time.as_us(), Ordering::Relaxed);
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.cost_tracker_update_elapsed
|
|
|
|
.fetch_add(cost_tracking_time.as_us(), Ordering::Relaxed);
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.filter_pending_packets_elapsed
|
|
|
|
.fetch_add(filter_pending_packets_time.as_us(), Ordering::Relaxed);
|
|
|
|
|
2019-07-01 12:14:40 -07:00
|
|
|
(processed, tx_len, filtered_unprocessed_packet_indexes)
|
2019-05-13 14:40:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn filter_unprocessed_packets(
|
|
|
|
bank: &Arc<Bank>,
|
|
|
|
msgs: &Packets,
|
|
|
|
transaction_indexes: &[usize],
|
2019-05-23 23:20:04 -07:00
|
|
|
my_pubkey: &Pubkey,
|
2019-05-22 17:54:28 -07:00
|
|
|
next_leader: Option<Pubkey>,
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats: &BankingStageStats,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: &Arc<RwLock<CostModel>>,
|
2019-05-13 14:40:05 -07:00
|
|
|
) -> Vec<usize> {
|
2019-05-22 17:54:28 -07:00
|
|
|
// Check if we are the next leader. If so, let's not filter the packets
|
|
|
|
// as we'll filter it again while processing the packets.
|
|
|
|
// Filtering helps if we were going to forward the packets to some other node
|
|
|
|
if let Some(leader) = next_leader {
|
2019-05-23 23:20:04 -07:00
|
|
|
if leader == *my_pubkey {
|
2019-05-22 17:54:28 -07:00
|
|
|
return transaction_indexes.to_vec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 19:34:04 -07:00
|
|
|
let mut unprocessed_packet_conversion_time =
|
|
|
|
Measure::start("unprocessed_packet_conversion");
|
2021-06-01 07:16:17 -07:00
|
|
|
let (transactions, transaction_to_packet_indexes, retry_packet_indexes) =
|
|
|
|
Self::transactions_from_packets(
|
|
|
|
msgs,
|
2021-06-18 06:34:46 -07:00
|
|
|
transaction_indexes,
|
2021-08-30 00:58:45 -07:00
|
|
|
&bank.feature_set,
|
2021-10-19 12:37:33 -07:00
|
|
|
&bank.read_cost_tracker().unwrap(),
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats,
|
2021-09-03 20:05:30 -07:00
|
|
|
bank.demote_program_write_locks(),
|
2021-09-16 06:12:26 -07:00
|
|
|
bank.vote_only_bank(),
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2021-06-01 07:16:17 -07:00
|
|
|
);
|
2021-06-28 19:34:04 -07:00
|
|
|
unprocessed_packet_conversion_time.stop();
|
2019-05-13 14:40:05 -07:00
|
|
|
|
2019-07-01 12:14:40 -07:00
|
|
|
let tx_count = transaction_to_packet_indexes.len();
|
2019-05-13 14:40:05 -07:00
|
|
|
|
|
|
|
let unprocessed_tx_indexes = (0..transactions.len()).collect_vec();
|
2021-06-01 07:16:17 -07:00
|
|
|
let mut filtered_unprocessed_packet_indexes = Self::filter_pending_packets_from_pending_txs(
|
2019-05-13 14:40:05 -07:00
|
|
|
bank,
|
|
|
|
&transactions,
|
2019-07-01 12:14:40 -07:00
|
|
|
&transaction_to_packet_indexes,
|
2019-05-13 14:40:05 -07:00
|
|
|
&unprocessed_tx_indexes,
|
|
|
|
);
|
|
|
|
|
2021-06-01 07:16:17 -07:00
|
|
|
filtered_unprocessed_packet_indexes.extend(retry_packet_indexes);
|
|
|
|
|
2019-05-13 14:40:05 -07:00
|
|
|
inc_new_counter_info!(
|
|
|
|
"banking_stage-dropped_tx_before_forwarding",
|
2019-07-01 12:14:40 -07:00
|
|
|
tx_count.saturating_sub(filtered_unprocessed_packet_indexes.len())
|
2019-05-13 14:40:05 -07:00
|
|
|
);
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.unprocessed_packet_conversion_elapsed
|
|
|
|
.fetch_add(
|
|
|
|
unprocessed_packet_conversion_time.as_us(),
|
|
|
|
Ordering::Relaxed,
|
|
|
|
);
|
2019-05-13 14:40:05 -07:00
|
|
|
|
2019-07-01 12:14:40 -07:00
|
|
|
filtered_unprocessed_packet_indexes
|
2019-04-11 17:23:45 -07:00
|
|
|
}
|
|
|
|
|
2019-11-07 19:48:33 -08:00
|
|
|
fn generate_packet_indexes(vers: &PinnedVec<Packet>) -> Vec<usize> {
|
2019-05-20 09:15:00 -07:00
|
|
|
vers.iter()
|
|
|
|
.enumerate()
|
2019-11-01 14:23:03 -07:00
|
|
|
.filter_map(
|
|
|
|
|(index, ver)| {
|
|
|
|
if !ver.meta.discard {
|
|
|
|
Some(index)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-05-20 09:15:00 -07:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2020-08-07 11:21:35 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2019-02-07 20:20:40 -08:00
|
|
|
/// Process the incoming packets
|
2021-06-17 10:14:21 -07:00
|
|
|
fn process_packets(
|
2019-06-21 15:21:49 -07:00
|
|
|
my_pubkey: &Pubkey,
|
2019-11-01 14:23:03 -07:00
|
|
|
verified_receiver: &CrossbeamReceiver<Vec<Packets>>,
|
2019-02-24 08:59:49 -08:00
|
|
|
poh: &Arc<Mutex<PohRecorder>>,
|
2019-04-09 11:17:15 -07:00
|
|
|
recv_start: &mut Instant,
|
2019-04-14 12:34:07 -07:00
|
|
|
recv_timeout: Duration,
|
2019-05-20 09:15:00 -07:00
|
|
|
id: u32,
|
2019-06-28 01:55:24 -07:00
|
|
|
batch_limit: usize,
|
2019-11-20 15:43:10 -08:00
|
|
|
transaction_status_sender: Option<TransactionStatusSender>,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender: &ReplayVoteSender,
|
2021-02-12 03:27:37 -08:00
|
|
|
buffered_packets: &mut UnprocessedPackets,
|
|
|
|
banking_stage_stats: &BankingStageStats,
|
2021-03-03 10:23:05 -08:00
|
|
|
duplicates: &Arc<Mutex<(LruCache<u64, ()>, PacketHasher)>>,
|
2021-03-23 07:10:04 -07:00
|
|
|
recorder: &TransactionRecorder,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model: &Arc<RwLock<CostModel>>,
|
2021-02-12 03:27:37 -08:00
|
|
|
) -> Result<(), RecvTimeoutError> {
|
2019-06-29 06:34:49 -07:00
|
|
|
let mut recv_time = Measure::start("process_packets_recv");
|
2019-06-26 18:42:27 -07:00
|
|
|
let mms = verified_receiver.recv_timeout(recv_timeout)?;
|
2019-06-29 06:34:49 -07:00
|
|
|
recv_time.stop();
|
2019-03-03 16:44:06 -08:00
|
|
|
|
2018-05-14 16:31:13 -07:00
|
|
|
let mms_len = mms.len();
|
2019-11-01 14:23:03 -07:00
|
|
|
let count: usize = mms.iter().map(|x| x.packets.len()).sum();
|
2019-03-28 11:45:34 -07:00
|
|
|
debug!(
|
2019-05-20 09:15:00 -07:00
|
|
|
"@{:?} process start stalled for: {:?}ms txs: {} id: {}",
|
2019-09-06 14:30:56 -07:00
|
|
|
timestamp(),
|
|
|
|
duration_as_ms(&recv_start.elapsed()),
|
2019-04-09 11:17:15 -07:00
|
|
|
count,
|
2019-05-20 09:15:00 -07:00
|
|
|
id,
|
2018-05-14 16:31:13 -07:00
|
|
|
);
|
2019-08-12 15:15:34 -07:00
|
|
|
inc_new_counter_debug!("banking_stage-transactions_received", count);
|
2021-03-15 17:11:15 -07:00
|
|
|
let mut proc_start = Measure::start("process_packets_transactions_process");
|
2018-10-23 14:44:14 -07:00
|
|
|
let mut new_tx_count = 0;
|
2019-05-20 09:15:00 -07:00
|
|
|
|
2019-05-13 14:40:05 -07:00
|
|
|
let mut mms_iter = mms.into_iter();
|
2021-09-15 13:53:55 -07:00
|
|
|
let mut dropped_packets_count = 0;
|
|
|
|
let mut dropped_packet_batches_count = 0;
|
2021-02-12 03:27:37 -08:00
|
|
|
let mut newly_buffered_packets_count = 0;
|
2019-11-01 14:23:03 -07:00
|
|
|
while let Some(msgs) = mms_iter.next() {
|
|
|
|
let packet_indexes = Self::generate_packet_indexes(&msgs.packets);
|
2021-09-17 16:55:53 -07:00
|
|
|
let poh_recorder_bank = poh.lock().unwrap().get_poh_recorder_bank();
|
|
|
|
let working_bank_start = poh_recorder_bank.working_bank_start();
|
|
|
|
if PohRecorder::get_working_bank_if_not_expired(&working_bank_start).is_none() {
|
2019-06-28 01:55:24 -07:00
|
|
|
Self::push_unprocessed(
|
2021-02-12 03:27:37 -08:00
|
|
|
buffered_packets,
|
2019-06-28 01:55:24 -07:00
|
|
|
msgs,
|
|
|
|
packet_indexes,
|
2021-09-15 13:53:55 -07:00
|
|
|
&mut dropped_packet_batches_count,
|
2021-10-20 19:56:48 -07:00
|
|
|
&mut dropped_packets_count,
|
2021-02-12 03:27:37 -08:00
|
|
|
&mut newly_buffered_packets_count,
|
2019-06-28 01:55:24 -07:00
|
|
|
batch_limit,
|
2021-03-03 10:23:05 -08:00
|
|
|
duplicates,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats,
|
2019-06-28 01:55:24 -07:00
|
|
|
);
|
2019-03-03 16:44:06 -08:00
|
|
|
continue;
|
|
|
|
}
|
2021-09-17 16:55:53 -07:00
|
|
|
|
|
|
|
// Destructure the `BankStart` behind an Arc
|
|
|
|
let BankStart {
|
|
|
|
working_bank,
|
|
|
|
bank_creation_time,
|
|
|
|
} = &*working_bank_start.unwrap();
|
2019-02-13 19:12:14 -08:00
|
|
|
|
2021-03-15 17:11:15 -07:00
|
|
|
let (processed, verified_txs_len, unprocessed_indexes) =
|
|
|
|
Self::process_packets_transactions(
|
2021-09-17 16:55:53 -07:00
|
|
|
working_bank,
|
|
|
|
bank_creation_time,
|
2021-03-23 07:10:04 -07:00
|
|
|
recorder,
|
2021-03-15 17:11:15 -07:00
|
|
|
&msgs,
|
|
|
|
packet_indexes,
|
|
|
|
transaction_status_sender.clone(),
|
|
|
|
gossip_vote_sender,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2021-03-15 17:11:15 -07:00
|
|
|
);
|
2019-02-13 19:12:14 -08:00
|
|
|
|
2019-05-13 14:40:05 -07:00
|
|
|
new_tx_count += processed;
|
|
|
|
|
2019-05-07 10:23:02 -07:00
|
|
|
// Collect any unprocessed transactions in this batch for forwarding
|
2019-06-28 01:55:24 -07:00
|
|
|
Self::push_unprocessed(
|
2021-02-12 03:27:37 -08:00
|
|
|
buffered_packets,
|
2019-06-28 01:55:24 -07:00
|
|
|
msgs,
|
|
|
|
unprocessed_indexes,
|
2021-09-15 13:53:55 -07:00
|
|
|
&mut dropped_packet_batches_count,
|
|
|
|
&mut dropped_packets_count,
|
2021-02-12 03:27:37 -08:00
|
|
|
&mut newly_buffered_packets_count,
|
2019-06-28 01:55:24 -07:00
|
|
|
batch_limit,
|
2021-03-03 10:23:05 -08:00
|
|
|
duplicates,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats,
|
2019-06-28 01:55:24 -07:00
|
|
|
);
|
2019-05-07 10:23:02 -07:00
|
|
|
|
2021-03-15 17:11:15 -07:00
|
|
|
// If there were retryable transactions, add the unexpired ones to the buffered queue
|
2019-05-13 14:40:05 -07:00
|
|
|
if processed < verified_txs_len {
|
2021-04-12 23:28:08 -07:00
|
|
|
let mut handle_retryable_packets_time = Measure::start("handle_retryable_packets");
|
2019-05-22 17:54:28 -07:00
|
|
|
let next_leader = poh.lock().unwrap().next_slot_leader();
|
2019-05-13 14:40:05 -07:00
|
|
|
// Walk thru rest of the transactions and filter out the invalid (e.g. too old) ones
|
2019-08-23 08:55:51 -07:00
|
|
|
#[allow(clippy::while_let_on_iterator)]
|
2019-11-01 14:23:03 -07:00
|
|
|
while let Some(msgs) = mms_iter.next() {
|
|
|
|
let packet_indexes = Self::generate_packet_indexes(&msgs.packets);
|
2019-05-22 17:54:28 -07:00
|
|
|
let unprocessed_indexes = Self::filter_unprocessed_packets(
|
2021-09-17 16:55:53 -07:00
|
|
|
working_bank,
|
2019-05-22 17:54:28 -07:00
|
|
|
&msgs,
|
|
|
|
&packet_indexes,
|
2021-06-18 06:34:46 -07:00
|
|
|
my_pubkey,
|
2019-05-22 17:54:28 -07:00
|
|
|
next_leader,
|
2021-06-28 19:34:04 -07:00
|
|
|
banking_stage_stats,
|
2021-10-19 12:37:33 -07:00
|
|
|
cost_model,
|
2019-05-22 17:54:28 -07:00
|
|
|
);
|
2019-06-28 01:55:24 -07:00
|
|
|
Self::push_unprocessed(
|
2021-02-12 03:27:37 -08:00
|
|
|
buffered_packets,
|
2019-06-28 01:55:24 -07:00
|
|
|
msgs,
|
|
|
|
unprocessed_indexes,
|
2021-09-15 13:53:55 -07:00
|
|
|
&mut dropped_packet_batches_count,
|
|
|
|
&mut dropped_packets_count,
|
2021-02-12 03:27:37 -08:00
|
|
|
&mut newly_buffered_packets_count,
|
2019-06-28 01:55:24 -07:00
|
|
|
batch_limit,
|
2021-03-03 10:23:05 -08:00
|
|
|
duplicates,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats,
|
2019-06-28 01:55:24 -07:00
|
|
|
);
|
2019-05-13 14:40:05 -07:00
|
|
|
}
|
2021-04-12 23:28:08 -07:00
|
|
|
handle_retryable_packets_time.stop();
|
|
|
|
banking_stage_stats
|
|
|
|
.handle_retryable_packets_elapsed
|
|
|
|
.fetch_add(handle_retryable_packets_time.as_us(), Ordering::Relaxed);
|
2019-05-13 14:40:05 -07:00
|
|
|
}
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
2019-06-29 06:34:49 -07:00
|
|
|
proc_start.stop();
|
|
|
|
|
2019-03-28 11:45:34 -07:00
|
|
|
debug!(
|
2019-05-20 09:15:00 -07:00
|
|
|
"@{:?} done processing transaction batches: {} time: {:?}ms tx count: {} tx/s: {} total count: {} id: {}",
|
2019-09-06 14:30:56 -07:00
|
|
|
timestamp(),
|
2018-05-14 16:31:13 -07:00
|
|
|
mms_len,
|
2019-06-29 06:34:49 -07:00
|
|
|
proc_start.as_ms(),
|
2019-04-09 11:17:15 -07:00
|
|
|
new_tx_count,
|
2019-06-29 06:34:49 -07:00
|
|
|
(new_tx_count as f32) / (proc_start.as_s()),
|
2019-05-20 09:15:00 -07:00
|
|
|
count,
|
|
|
|
id,
|
2018-05-14 16:31:13 -07:00
|
|
|
);
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.process_packets_elapsed
|
|
|
|
.fetch_add(proc_start.as_us(), Ordering::Relaxed);
|
2021-02-12 03:27:37 -08:00
|
|
|
banking_stage_stats
|
|
|
|
.process_packets_count
|
|
|
|
.fetch_add(count, Ordering::Relaxed);
|
|
|
|
banking_stage_stats
|
|
|
|
.new_tx_count
|
|
|
|
.fetch_add(new_tx_count, Ordering::Relaxed);
|
|
|
|
banking_stage_stats
|
2021-09-15 13:53:55 -07:00
|
|
|
.dropped_packet_batches_count
|
|
|
|
.fetch_add(dropped_packet_batches_count, Ordering::Relaxed);
|
2021-10-20 19:56:48 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.dropped_packets_count
|
|
|
|
.fetch_add(dropped_packets_count, Ordering::Relaxed);
|
2021-02-12 03:27:37 -08:00
|
|
|
banking_stage_stats
|
|
|
|
.newly_buffered_packets_count
|
|
|
|
.fetch_add(newly_buffered_packets_count, Ordering::Relaxed);
|
|
|
|
banking_stage_stats
|
2021-09-15 13:53:55 -07:00
|
|
|
.current_buffered_packet_batches_count
|
2021-02-12 03:27:37 -08:00
|
|
|
.swap(buffered_packets.len(), Ordering::Relaxed);
|
2021-09-15 13:53:55 -07:00
|
|
|
banking_stage_stats.current_buffered_packets_count.swap(
|
|
|
|
buffered_packets.iter().map(|packets| packets.1.len()).sum(),
|
|
|
|
Ordering::Relaxed,
|
|
|
|
);
|
2019-04-09 11:17:15 -07:00
|
|
|
*recv_start = Instant::now();
|
2021-02-12 03:27:37 -08:00
|
|
|
Ok(())
|
2019-02-13 19:12:14 -08:00
|
|
|
}
|
2019-05-20 09:15:00 -07:00
|
|
|
|
|
|
|
fn push_unprocessed(
|
|
|
|
unprocessed_packets: &mut UnprocessedPackets,
|
2019-05-20 17:48:42 -07:00
|
|
|
packets: Packets,
|
2021-03-03 10:23:05 -08:00
|
|
|
mut packet_indexes: Vec<usize>,
|
2021-09-15 13:53:55 -07:00
|
|
|
dropped_packet_batches_count: &mut usize,
|
|
|
|
dropped_packets_count: &mut usize,
|
2021-02-12 03:27:37 -08:00
|
|
|
newly_buffered_packets_count: &mut usize,
|
2019-06-28 01:55:24 -07:00
|
|
|
batch_limit: usize,
|
2021-03-03 10:23:05 -08:00
|
|
|
duplicates: &Arc<Mutex<(LruCache<u64, ()>, PacketHasher)>>,
|
2021-04-12 23:28:08 -07:00
|
|
|
banking_stage_stats: &BankingStageStats,
|
2019-05-20 09:15:00 -07:00
|
|
|
) {
|
2021-03-03 10:23:05 -08:00
|
|
|
{
|
2021-10-20 19:56:48 -07:00
|
|
|
let original_packets_count = packet_indexes.len();
|
2021-04-12 23:28:08 -07:00
|
|
|
let mut packet_duplicate_check_time = Measure::start("packet_duplicate_check");
|
2021-03-03 10:23:05 -08:00
|
|
|
let mut duplicates = duplicates.lock().unwrap();
|
|
|
|
let (cache, hasher) = duplicates.deref_mut();
|
|
|
|
packet_indexes.retain(|i| {
|
|
|
|
let packet_hash = hasher.hash_packet(&packets.packets[*i]);
|
|
|
|
match cache.get_mut(&packet_hash) {
|
|
|
|
Some(_hash) => false,
|
|
|
|
None => {
|
|
|
|
cache.put(packet_hash, ());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-04-12 23:28:08 -07:00
|
|
|
packet_duplicate_check_time.stop();
|
|
|
|
banking_stage_stats
|
|
|
|
.packet_duplicate_check_elapsed
|
|
|
|
.fetch_add(packet_duplicate_check_time.as_us(), Ordering::Relaxed);
|
2021-10-20 19:56:48 -07:00
|
|
|
banking_stage_stats
|
|
|
|
.dropped_duplicated_packets_count
|
|
|
|
.fetch_add(
|
|
|
|
original_packets_count.saturating_sub(packet_indexes.len()),
|
|
|
|
Ordering::Relaxed,
|
|
|
|
);
|
2021-03-03 10:23:05 -08:00
|
|
|
}
|
2021-02-12 03:27:37 -08:00
|
|
|
if Self::packet_has_more_unprocessed_transactions(&packet_indexes) {
|
2019-06-28 01:55:24 -07:00
|
|
|
if unprocessed_packets.len() >= batch_limit {
|
2021-09-15 13:53:55 -07:00
|
|
|
*dropped_packet_batches_count += 1;
|
|
|
|
if let Some(dropped_batch) = unprocessed_packets.pop_front() {
|
|
|
|
*dropped_packets_count += dropped_batch.1.len();
|
|
|
|
}
|
2019-06-27 00:32:32 -07:00
|
|
|
}
|
2021-02-12 03:27:37 -08:00
|
|
|
*newly_buffered_packets_count += packet_indexes.len();
|
2021-03-03 10:23:05 -08:00
|
|
|
unprocessed_packets.push_back((packets, packet_indexes, false));
|
2019-05-20 09:15:00 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-18 22:57:48 -07:00
|
|
|
|
2021-02-12 03:27:37 -08:00
|
|
|
fn packet_has_more_unprocessed_transactions(packet_indexes: &[usize]) -> bool {
|
|
|
|
!packet_indexes.is_empty()
|
|
|
|
}
|
|
|
|
|
2019-11-13 10:12:09 -08:00
|
|
|
pub fn join(self) -> thread::Result<()> {
|
2018-10-18 22:57:48 -07:00
|
|
|
for bank_thread_hdl in self.bank_thread_hdls {
|
2019-02-13 19:12:14 -08:00
|
|
|
bank_thread_hdl.join()?;
|
2018-09-26 05:52:13 -07:00
|
|
|
}
|
2019-02-13 19:12:14 -08:00
|
|
|
Ok(())
|
2018-07-03 21:14:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:22:00 -07:00
|
|
|
pub(crate) fn next_leader_tpu(
|
|
|
|
cluster_info: &ClusterInfo,
|
2021-04-28 11:46:16 -07:00
|
|
|
poh_recorder: &Mutex<PohRecorder>,
|
2021-04-17 10:22:00 -07:00
|
|
|
) -> Option<std::net::SocketAddr> {
|
2021-10-07 02:38:23 -07:00
|
|
|
next_leader_x(cluster_info, poh_recorder, |leader| leader.tpu)
|
2021-04-17 10:22:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn next_leader_tpu_forwards(
|
|
|
|
cluster_info: &ClusterInfo,
|
2021-10-07 02:38:23 -07:00
|
|
|
poh_recorder: &Mutex<PohRecorder>,
|
2021-04-17 10:22:00 -07:00
|
|
|
) -> Option<std::net::SocketAddr> {
|
2021-10-07 02:38:23 -07:00
|
|
|
next_leader_x(cluster_info, poh_recorder, |leader| leader.tpu_forwards)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn next_leader_tpu_vote(
|
|
|
|
cluster_info: &ClusterInfo,
|
|
|
|
poh_recorder: &Mutex<PohRecorder>,
|
|
|
|
) -> Option<std::net::SocketAddr> {
|
|
|
|
next_leader_x(cluster_info, poh_recorder, |leader| leader.tpu_vote)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_leader_x<F>(
|
|
|
|
cluster_info: &ClusterInfo,
|
|
|
|
poh_recorder: &Mutex<PohRecorder>,
|
|
|
|
port_selector: F,
|
|
|
|
) -> Option<std::net::SocketAddr>
|
|
|
|
where
|
|
|
|
F: FnOnce(&ContactInfo) -> SocketAddr,
|
|
|
|
{
|
2021-04-17 10:22:00 -07:00
|
|
|
if let Some(leader_pubkey) = poh_recorder
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.leader_after_n_slots(FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET)
|
|
|
|
{
|
2021-10-07 02:38:23 -07:00
|
|
|
cluster_info.lookup_contact_info(&leader_pubkey, port_selector)
|
2021-04-17 10:22:00 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-06-26 18:42:27 -07:00
|
|
|
use crossbeam_channel::unbounded;
|
2019-05-10 14:28:38 -07:00
|
|
|
use itertools::Itertools;
|
2021-07-14 05:16:29 -07:00
|
|
|
use solana_entry::entry::{next_entry, Entry, EntrySlice};
|
2021-07-23 08:25:03 -07:00
|
|
|
use solana_gossip::{cluster_info::Node, contact_info::ContactInfo};
|
2019-11-20 15:43:10 -08:00
|
|
|
use solana_ledger::{
|
2021-06-04 08:23:06 -07:00
|
|
|
blockstore::{entries_to_test_shreds, Blockstore},
|
2020-03-21 10:54:40 -07:00
|
|
|
genesis_utils::{create_genesis_config, GenesisConfigInfo},
|
2019-11-20 15:43:10 -08:00
|
|
|
get_tmp_ledger_path,
|
2021-06-04 08:23:06 -07:00
|
|
|
leader_schedule_cache::LeaderScheduleCache,
|
2019-11-20 15:43:10 -08:00
|
|
|
};
|
2021-01-28 16:32:38 -08:00
|
|
|
use solana_perf::packet::to_packets_chunked;
|
2021-06-04 08:23:06 -07:00
|
|
|
use solana_poh::{
|
2021-09-13 16:55:35 -07:00
|
|
|
poh_recorder::{create_test_recorder, Record, WorkingBankEntry},
|
2021-06-04 08:23:06 -07:00
|
|
|
poh_service::PohService,
|
|
|
|
};
|
|
|
|
use solana_rpc::transaction_status_service::TransactionStatusService;
|
2021-10-12 06:51:33 -07:00
|
|
|
use solana_runtime::cost_model::CostModel;
|
2019-11-20 15:43:10 -08:00
|
|
|
use solana_sdk::{
|
2021-04-12 23:28:08 -07:00
|
|
|
hash::Hash,
|
2019-11-20 15:43:10 -08:00
|
|
|
instruction::InstructionError,
|
2021-06-04 08:23:06 -07:00
|
|
|
poh_config::PohConfig,
|
2020-02-20 13:28:55 -08:00
|
|
|
signature::{Keypair, Signer},
|
2020-08-06 15:04:43 -07:00
|
|
|
system_instruction::SystemError,
|
2019-11-20 15:43:10 -08:00
|
|
|
system_transaction,
|
2021-08-17 15:17:56 -07:00
|
|
|
transaction::{Transaction, TransactionError},
|
2019-11-20 15:43:10 -08:00
|
|
|
};
|
2021-07-23 08:25:03 -07:00
|
|
|
use solana_streamer::socket::SocketAddrSpace;
|
2020-09-23 22:10:29 -07:00
|
|
|
use solana_transaction_status::TransactionWithStatusMeta;
|
2021-10-07 02:38:23 -07:00
|
|
|
use solana_vote_program::vote_transaction;
|
2021-03-23 07:10:04 -07:00
|
|
|
use std::{
|
2021-08-17 15:17:56 -07:00
|
|
|
convert::{TryFrom, TryInto},
|
2021-03-23 07:10:04 -07:00
|
|
|
net::SocketAddr,
|
|
|
|
path::Path,
|
2021-06-04 08:23:06 -07:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
mpsc::Receiver,
|
|
|
|
},
|
2021-03-23 07:10:04 -07:00
|
|
|
thread::sleep,
|
|
|
|
};
|
2018-09-25 15:01:51 -07:00
|
|
|
|
2021-07-23 08:25:03 -07:00
|
|
|
fn new_test_cluster_info(contact_info: ContactInfo) -> ClusterInfo {
|
|
|
|
ClusterInfo::new(
|
|
|
|
contact_info,
|
|
|
|
Arc::new(Keypair::new()),
|
|
|
|
SocketAddrSpace::Unspecified,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
#[test]
|
2018-09-26 05:52:13 -07:00
|
|
|
fn test_banking_stage_shutdown1() {
|
2019-11-08 20:56:57 -08:00
|
|
|
let genesis_config = create_genesis_config(2).genesis_config;
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2019-06-26 18:42:27 -07:00
|
|
|
let (verified_sender, verified_receiver) = unbounded();
|
2021-10-07 02:38:23 -07:00
|
|
|
let (gossip_verified_vote_sender, gossip_verified_vote_receiver) = unbounded();
|
|
|
|
let (tpu_vote_sender, tpu_vote_receiver) = unbounded();
|
2019-03-29 20:00:36 -07:00
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Arc::new(
|
|
|
|
Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger"),
|
2019-03-29 20:00:36 -07:00
|
|
|
);
|
|
|
|
let (exit, poh_recorder, poh_service, _entry_receiever) =
|
2020-01-13 13:13:52 -08:00
|
|
|
create_test_recorder(&bank, &blockstore, None);
|
2021-07-23 08:25:03 -07:00
|
|
|
let cluster_info = new_test_cluster_info(Node::new_localhost().info);
|
2020-04-21 12:54:45 -07:00
|
|
|
let cluster_info = Arc::new(cluster_info);
|
2021-10-07 02:38:23 -07:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
|
2019-04-17 21:07:45 -07:00
|
|
|
let banking_stage = BankingStage::new(
|
|
|
|
&cluster_info,
|
|
|
|
&poh_recorder,
|
|
|
|
verified_receiver,
|
2021-10-07 02:38:23 -07:00
|
|
|
tpu_vote_receiver,
|
|
|
|
gossip_verified_vote_receiver,
|
2019-11-20 15:43:10 -08:00
|
|
|
None,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender,
|
2021-10-19 12:37:33 -07:00
|
|
|
Arc::new(RwLock::new(CostModel::default())),
|
2019-04-17 21:07:45 -07:00
|
|
|
);
|
2019-03-29 20:00:36 -07:00
|
|
|
drop(verified_sender);
|
2021-10-07 02:38:23 -07:00
|
|
|
drop(gossip_verified_vote_sender);
|
|
|
|
drop(tpu_vote_sender);
|
2019-03-29 20:00:36 -07:00
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
banking_stage.join().unwrap();
|
|
|
|
poh_service.join().unwrap();
|
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2018-09-26 05:52:13 -07:00
|
|
|
}
|
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
#[test]
|
|
|
|
fn test_banking_stage_tick() {
|
2019-03-03 16:44:06 -08:00
|
|
|
solana_logger::setup();
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
mut genesis_config, ..
|
|
|
|
} = create_genesis_config(2);
|
|
|
|
genesis_config.ticks_per_slot = 4;
|
2019-10-16 12:37:27 -07:00
|
|
|
let num_extra_ticks = 2;
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2019-03-02 10:25:16 -08:00
|
|
|
let start_hash = bank.last_blockhash();
|
2019-06-26 18:42:27 -07:00
|
|
|
let (verified_sender, verified_receiver) = unbounded();
|
2021-10-07 02:38:23 -07:00
|
|
|
let (tpu_vote_sender, tpu_vote_receiver) = unbounded();
|
2019-03-29 20:00:36 -07:00
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Arc::new(
|
|
|
|
Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger"),
|
2019-03-29 20:00:36 -07:00
|
|
|
);
|
2020-12-13 17:26:34 -08:00
|
|
|
let poh_config = PohConfig {
|
|
|
|
target_tick_count: Some(bank.max_tick_height() + num_extra_ticks),
|
|
|
|
..PohConfig::default()
|
|
|
|
};
|
2019-03-29 20:00:36 -07:00
|
|
|
let (exit, poh_recorder, poh_service, entry_receiver) =
|
2020-01-13 13:13:52 -08:00
|
|
|
create_test_recorder(&bank, &blockstore, Some(poh_config));
|
2021-07-23 08:25:03 -07:00
|
|
|
let cluster_info = new_test_cluster_info(Node::new_localhost().info);
|
2020-04-21 12:54:45 -07:00
|
|
|
let cluster_info = Arc::new(cluster_info);
|
2021-10-07 02:38:23 -07:00
|
|
|
let (verified_gossip_vote_sender, verified_gossip_vote_receiver) = unbounded();
|
2020-08-07 11:21:35 -07:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
|
2019-04-17 21:07:45 -07:00
|
|
|
let banking_stage = BankingStage::new(
|
|
|
|
&cluster_info,
|
|
|
|
&poh_recorder,
|
|
|
|
verified_receiver,
|
2021-10-07 02:38:23 -07:00
|
|
|
tpu_vote_receiver,
|
|
|
|
verified_gossip_vote_receiver,
|
2019-11-20 15:43:10 -08:00
|
|
|
None,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender,
|
2021-10-19 12:37:33 -07:00
|
|
|
Arc::new(RwLock::new(CostModel::default())),
|
2019-04-17 21:07:45 -07:00
|
|
|
);
|
2019-03-29 20:00:36 -07:00
|
|
|
trace!("sending bank");
|
|
|
|
drop(verified_sender);
|
2021-10-07 02:38:23 -07:00
|
|
|
drop(verified_gossip_vote_sender);
|
|
|
|
drop(tpu_vote_sender);
|
2019-03-29 20:00:36 -07:00
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
poh_service.join().unwrap();
|
|
|
|
drop(poh_recorder);
|
|
|
|
|
|
|
|
trace!("getting entries");
|
|
|
|
let entries: Vec<_> = entry_receiver
|
|
|
|
.iter()
|
2019-09-18 12:16:22 -07:00
|
|
|
.map(|(_bank, (entry, _tick_height))| entry)
|
2019-03-29 20:00:36 -07:00
|
|
|
.collect();
|
|
|
|
trace!("done");
|
2019-11-08 20:56:57 -08:00
|
|
|
assert_eq!(entries.len(), genesis_config.ticks_per_slot as usize);
|
2021-05-19 07:31:47 -07:00
|
|
|
assert!(entries.verify(&start_hash));
|
2019-03-29 20:00:36 -07:00
|
|
|
assert_eq!(entries[entries.len() - 1].hash, bank.last_blockhash());
|
|
|
|
banking_stage.join().unwrap();
|
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2018-09-25 15:01:51 -07:00
|
|
|
}
|
|
|
|
|
2019-11-01 14:23:03 -07:00
|
|
|
pub fn convert_from_old_verified(mut with_vers: Vec<(Packets, Vec<u8>)>) -> Vec<Packets> {
|
|
|
|
with_vers.iter_mut().for_each(|(b, v)| {
|
|
|
|
b.packets
|
|
|
|
.iter_mut()
|
|
|
|
.zip(v)
|
|
|
|
.for_each(|(p, f)| p.meta.discard = *f == 0)
|
|
|
|
});
|
|
|
|
with_vers.into_iter().map(|(b, _)| b).collect()
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
#[test]
|
2018-09-26 13:31:39 -07:00
|
|
|
fn test_banking_stage_entries_only() {
|
2019-03-12 21:07:06 -07:00
|
|
|
solana_logger::setup();
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2021-03-24 14:05:43 -07:00
|
|
|
} = create_slow_genesis_config(10);
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2019-03-02 10:25:16 -08:00
|
|
|
let start_hash = bank.last_blockhash();
|
2019-06-26 18:42:27 -07:00
|
|
|
let (verified_sender, verified_receiver) = unbounded();
|
2021-10-07 02:38:23 -07:00
|
|
|
let (tpu_vote_sender, tpu_vote_receiver) = unbounded();
|
|
|
|
let (gossip_verified_vote_sender, gossip_verified_vote_receiver) = unbounded();
|
2019-03-29 20:00:36 -07:00
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Arc::new(
|
|
|
|
Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger"),
|
2019-03-29 20:00:36 -07:00
|
|
|
);
|
2020-12-13 17:26:34 -08:00
|
|
|
let poh_config = PohConfig {
|
|
|
|
// limit tick count to avoid clearing working_bank at PohRecord then
|
|
|
|
// PohRecorderError(MaxHeightReached) at BankingStage
|
|
|
|
target_tick_count: Some(bank.max_tick_height() - 1),
|
|
|
|
..PohConfig::default()
|
|
|
|
};
|
2019-03-29 20:00:36 -07:00
|
|
|
let (exit, poh_recorder, poh_service, entry_receiver) =
|
2020-01-13 13:13:52 -08:00
|
|
|
create_test_recorder(&bank, &blockstore, Some(poh_config));
|
2021-07-23 08:25:03 -07:00
|
|
|
let cluster_info = new_test_cluster_info(Node::new_localhost().info);
|
2020-04-21 12:54:45 -07:00
|
|
|
let cluster_info = Arc::new(cluster_info);
|
2020-08-07 11:21:35 -07:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
|
2019-04-17 21:07:45 -07:00
|
|
|
let banking_stage = BankingStage::new(
|
|
|
|
&cluster_info,
|
|
|
|
&poh_recorder,
|
|
|
|
verified_receiver,
|
2021-10-07 02:38:23 -07:00
|
|
|
tpu_vote_receiver,
|
|
|
|
gossip_verified_vote_receiver,
|
2019-11-20 15:43:10 -08:00
|
|
|
None,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender,
|
2021-10-19 12:37:33 -07:00
|
|
|
Arc::new(RwLock::new(CostModel::default())),
|
2019-04-17 21:07:45 -07:00
|
|
|
);
|
2018-09-25 15:01:51 -07:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
// fund another account so we can send 2 good transactions in a single batch.
|
|
|
|
let keypair = Keypair::new();
|
2019-10-19 18:23:27 -07:00
|
|
|
let fund_tx =
|
2019-10-23 22:01:22 -07:00
|
|
|
system_transaction::transfer(&mint_keypair, &keypair.pubkey(), 2, start_hash);
|
2019-03-29 20:00:36 -07:00
|
|
|
bank.process_transaction(&fund_tx).unwrap();
|
2019-03-12 21:07:06 -07:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
// good tx
|
2020-10-19 12:12:08 -07:00
|
|
|
let to = solana_sdk::pubkey::new_rand();
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx = system_transaction::transfer(&mint_keypair, &to, 1, start_hash);
|
2019-03-29 20:00:36 -07:00
|
|
|
|
|
|
|
// good tx, but no verify
|
2020-10-19 12:12:08 -07:00
|
|
|
let to2 = solana_sdk::pubkey::new_rand();
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx_no_ver = system_transaction::transfer(&keypair, &to2, 2, start_hash);
|
2019-03-29 20:00:36 -07:00
|
|
|
|
|
|
|
// bad tx, AccountNotFound
|
|
|
|
let keypair = Keypair::new();
|
2020-10-19 12:12:08 -07:00
|
|
|
let to3 = solana_sdk::pubkey::new_rand();
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx_anf = system_transaction::transfer(&keypair, &to3, 1, start_hash);
|
2019-03-29 20:00:36 -07:00
|
|
|
|
|
|
|
// send 'em over
|
2021-01-28 16:32:38 -08:00
|
|
|
let packets = to_packets_chunked(&[tx_no_ver, tx_anf, tx], 3);
|
2019-03-29 20:00:36 -07:00
|
|
|
|
|
|
|
// glad they all fit
|
|
|
|
assert_eq!(packets.len(), 1);
|
2019-05-20 09:15:00 -07:00
|
|
|
|
|
|
|
let packets = packets
|
|
|
|
.into_iter()
|
|
|
|
.map(|packets| (packets, vec![0u8, 1u8, 1u8]))
|
|
|
|
.collect();
|
2019-11-01 14:23:03 -07:00
|
|
|
let packets = convert_from_old_verified(packets);
|
2019-03-29 20:00:36 -07:00
|
|
|
verified_sender // no_ver, anf, tx
|
2019-05-20 09:15:00 -07:00
|
|
|
.send(packets)
|
2019-03-29 20:00:36 -07:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
drop(verified_sender);
|
2021-10-07 02:38:23 -07:00
|
|
|
drop(tpu_vote_sender);
|
|
|
|
drop(gossip_verified_vote_sender);
|
2019-10-16 12:37:27 -07:00
|
|
|
// wait until banking_stage to finish up all packets
|
|
|
|
banking_stage.join().unwrap();
|
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
poh_service.join().unwrap();
|
|
|
|
drop(poh_recorder);
|
|
|
|
|
|
|
|
let mut blockhash = start_hash;
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2019-03-29 20:00:36 -07:00
|
|
|
bank.process_transaction(&fund_tx).unwrap();
|
|
|
|
//receive entries + ticks
|
2019-10-16 12:37:27 -07:00
|
|
|
loop {
|
2019-09-18 12:16:22 -07:00
|
|
|
let entries: Vec<Entry> = entry_receiver
|
2019-03-29 20:00:36 -07:00
|
|
|
.iter()
|
2019-09-18 12:16:22 -07:00
|
|
|
.map(|(_bank, (entry, _tick_height))| entry)
|
2019-03-29 20:00:36 -07:00
|
|
|
.collect();
|
|
|
|
|
2021-05-19 07:31:47 -07:00
|
|
|
assert!(entries.verify(&blockhash));
|
2019-10-16 12:37:27 -07:00
|
|
|
if !entries.is_empty() {
|
|
|
|
blockhash = entries.last().unwrap().hash;
|
|
|
|
for entry in entries {
|
2021-08-17 15:17:56 -07:00
|
|
|
bank.process_entry_transactions(entry.transactions)
|
2019-10-16 12:37:27 -07:00
|
|
|
.iter()
|
|
|
|
.for_each(|x| assert_eq!(*x, Ok(())));
|
|
|
|
}
|
2019-03-12 21:07:06 -07:00
|
|
|
}
|
2019-02-25 13:50:31 -08:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
if bank.get_balance(&to) == 1 {
|
|
|
|
break;
|
|
|
|
}
|
2019-03-12 21:07:06 -07:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
sleep(Duration::from_millis(200));
|
|
|
|
}
|
2019-03-12 21:07:06 -07:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
assert_eq!(bank.get_balance(&to), 1);
|
|
|
|
assert_eq!(bank.get_balance(&to2), 0);
|
2018-09-26 05:52:13 -07:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
drop(entry_receiver);
|
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2018-09-26 13:31:39 -07:00
|
|
|
}
|
2019-03-03 16:44:06 -08:00
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
#[test]
|
|
|
|
fn test_banking_stage_entryfication() {
|
2019-03-18 22:08:21 -07:00
|
|
|
solana_logger::setup();
|
2018-09-25 15:01:51 -07:00
|
|
|
// In this attack we'll demonstrate that a verifier can interpret the ledger
|
|
|
|
// differently if either the server doesn't signal the ledger to add an
|
|
|
|
// Entry OR if the verifier tries to parallelize across multiple Entries.
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2021-03-24 14:05:43 -07:00
|
|
|
} = create_slow_genesis_config(2);
|
2019-06-26 18:42:27 -07:00
|
|
|
let (verified_sender, verified_receiver) = unbounded();
|
2018-09-25 15:01:51 -07:00
|
|
|
|
2019-03-05 16:58:52 -08:00
|
|
|
// Process a batch that includes a transaction that receives two lamports.
|
2018-09-25 15:01:51 -07:00
|
|
|
let alice = Keypair::new();
|
2019-10-23 22:01:22 -07:00
|
|
|
let tx =
|
2019-11-08 20:56:57 -08:00
|
|
|
system_transaction::transfer(&mint_keypair, &alice.pubkey(), 2, genesis_config.hash());
|
2018-09-25 15:01:51 -07:00
|
|
|
|
2021-01-28 16:32:38 -08:00
|
|
|
let packets = to_packets_chunked(&[tx], 1);
|
2019-05-20 09:15:00 -07:00
|
|
|
let packets = packets
|
|
|
|
.into_iter()
|
|
|
|
.map(|packets| (packets, vec![1u8]))
|
|
|
|
.collect();
|
2019-11-01 14:23:03 -07:00
|
|
|
let packets = convert_from_old_verified(packets);
|
2019-05-20 09:15:00 -07:00
|
|
|
verified_sender.send(packets).unwrap();
|
2018-09-25 15:01:51 -07:00
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
// Process a second batch that uses the same from account, so conflicts with above TX
|
|
|
|
let tx =
|
2019-11-08 20:56:57 -08:00
|
|
|
system_transaction::transfer(&mint_keypair, &alice.pubkey(), 1, genesis_config.hash());
|
2021-01-28 16:32:38 -08:00
|
|
|
let packets = to_packets_chunked(&[tx], 1);
|
2019-05-20 09:15:00 -07:00
|
|
|
let packets = packets
|
|
|
|
.into_iter()
|
|
|
|
.map(|packets| (packets, vec![1u8]))
|
|
|
|
.collect();
|
2019-11-01 14:23:03 -07:00
|
|
|
let packets = convert_from_old_verified(packets);
|
2019-05-20 09:15:00 -07:00
|
|
|
verified_sender.send(packets).unwrap();
|
2019-03-03 16:44:06 -08:00
|
|
|
|
2019-06-26 18:42:27 -07:00
|
|
|
let (vote_sender, vote_receiver) = unbounded();
|
2021-10-07 02:38:23 -07:00
|
|
|
let (tpu_vote_sender, tpu_vote_receiver) = unbounded();
|
2019-03-29 20:00:36 -07:00
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-08-07 11:21:35 -07:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
let entry_receiver = {
|
|
|
|
// start a banking_stage to eat verified receiver
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Arc::new(
|
|
|
|
Blockstore::open(&ledger_path)
|
2019-03-29 20:00:36 -07:00
|
|
|
.expect("Expected to be able to open database ledger"),
|
|
|
|
);
|
2020-12-13 17:26:34 -08:00
|
|
|
let poh_config = PohConfig {
|
|
|
|
// limit tick count to avoid clearing working_bank at
|
|
|
|
// PohRecord then PohRecorderError(MaxHeightReached) at BankingStage
|
|
|
|
target_tick_count: Some(bank.max_tick_height() - 1),
|
|
|
|
..PohConfig::default()
|
|
|
|
};
|
2019-03-29 20:00:36 -07:00
|
|
|
let (exit, poh_recorder, poh_service, entry_receiver) =
|
2020-01-13 13:13:52 -08:00
|
|
|
create_test_recorder(&bank, &blockstore, Some(poh_config));
|
2021-07-23 08:25:03 -07:00
|
|
|
let cluster_info = new_test_cluster_info(Node::new_localhost().info);
|
2020-04-21 12:54:45 -07:00
|
|
|
let cluster_info = Arc::new(cluster_info);
|
2019-03-29 20:00:36 -07:00
|
|
|
let _banking_stage = BankingStage::new_num_threads(
|
|
|
|
&cluster_info,
|
|
|
|
&poh_recorder,
|
|
|
|
verified_receiver,
|
2021-10-07 02:38:23 -07:00
|
|
|
tpu_vote_receiver,
|
2019-04-17 21:07:45 -07:00
|
|
|
vote_receiver,
|
2021-10-07 02:38:23 -07:00
|
|
|
3,
|
2019-11-20 15:43:10 -08:00
|
|
|
None,
|
2020-08-07 11:21:35 -07:00
|
|
|
gossip_vote_sender,
|
2021-10-19 12:37:33 -07:00
|
|
|
Arc::new(RwLock::new(CostModel::default())),
|
2019-03-29 20:00:36 -07:00
|
|
|
);
|
2019-03-18 22:08:21 -07:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
// wait for banking_stage to eat the packets
|
2019-10-23 22:01:22 -07:00
|
|
|
while bank.get_balance(&alice.pubkey()) < 2 {
|
2019-03-29 20:00:36 -07:00
|
|
|
sleep(Duration::from_millis(100));
|
|
|
|
}
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
poh_service.join().unwrap();
|
|
|
|
entry_receiver
|
|
|
|
};
|
|
|
|
drop(verified_sender);
|
2019-04-17 21:07:45 -07:00
|
|
|
drop(vote_sender);
|
2021-10-07 02:38:23 -07:00
|
|
|
drop(tpu_vote_sender);
|
2019-03-29 20:00:36 -07:00
|
|
|
|
|
|
|
// consume the entire entry_receiver, feed it into a new bank
|
|
|
|
// check that the balance is what we expect.
|
|
|
|
let entries: Vec<_> = entry_receiver
|
|
|
|
.iter()
|
2019-09-18 12:16:22 -07:00
|
|
|
.map(|(_bank, (entry, _tick_height))| entry)
|
2019-03-29 20:00:36 -07:00
|
|
|
.collect();
|
2018-09-25 15:01:51 -07:00
|
|
|
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Bank::new_no_wallclock_throttle_for_tests(&genesis_config);
|
2021-08-17 15:17:56 -07:00
|
|
|
for entry in entries {
|
|
|
|
bank.process_entry_transactions(entry.transactions)
|
2019-03-29 20:00:36 -07:00
|
|
|
.iter()
|
|
|
|
.for_each(|x| assert_eq!(*x, Ok(())));
|
|
|
|
}
|
2019-03-18 22:08:21 -07:00
|
|
|
|
2019-10-23 22:01:22 -07:00
|
|
|
// Assert the user holds two lamports, not three. If the stage only outputs one
|
2019-03-29 20:00:36 -07:00
|
|
|
// entry, then the second transaction will be rejected, because it drives
|
|
|
|
// the account balance below zero before the credit is added.
|
2019-10-23 22:01:22 -07:00
|
|
|
assert_eq!(bank.get_balance(&alice.pubkey()), 2);
|
2019-03-11 12:45:45 -07:00
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2018-10-18 22:57:48 -07:00
|
|
|
}
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2021-08-17 15:17:56 -07:00
|
|
|
fn sanitize_transactions(txs: Vec<Transaction>) -> Vec<SanitizedTransaction> {
|
|
|
|
txs.into_iter()
|
|
|
|
.map(|tx| SanitizedTransaction::try_from(tx).unwrap())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2019-02-16 14:02:21 -08:00
|
|
|
#[test]
|
|
|
|
fn test_bank_record_transactions() {
|
2021-03-23 07:10:04 -07:00
|
|
|
solana_logger::setup();
|
|
|
|
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2019-11-08 20:56:57 -08:00
|
|
|
} = create_genesis_config(10_000);
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2019-03-29 20:00:36 -07:00
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger");
|
2021-03-23 07:10:04 -07:00
|
|
|
let (poh_recorder, entry_receiver, record_receiver) = PohRecorder::new(
|
|
|
|
// TODO use record_receiver
|
2019-03-29 20:00:36 -07:00
|
|
|
bank.tick_height(),
|
|
|
|
bank.last_blockhash(),
|
2021-09-13 16:55:35 -07:00
|
|
|
bank.clone(),
|
2019-03-29 20:00:36 -07:00
|
|
|
None,
|
|
|
|
bank.ticks_per_slot(),
|
|
|
|
&Pubkey::default(),
|
2020-01-13 13:13:52 -08:00
|
|
|
&Arc::new(blockstore),
|
2019-04-19 02:39:44 -07:00
|
|
|
&Arc::new(LeaderScheduleCache::new_from_bank(&bank)),
|
2019-05-18 14:01:36 -07:00
|
|
|
&Arc::new(PohConfig::default()),
|
2021-04-14 10:07:21 -07:00
|
|
|
Arc::new(AtomicBool::default()),
|
2019-03-29 20:00:36 -07:00
|
|
|
);
|
2021-03-23 07:10:04 -07:00
|
|
|
let recorder = poh_recorder.recorder();
|
2019-03-29 20:00:36 -07:00
|
|
|
let poh_recorder = Arc::new(Mutex::new(poh_recorder));
|
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
let poh_simulator = simulate_poh(record_receiver, &poh_recorder);
|
2021-03-23 07:10:04 -07:00
|
|
|
|
2021-09-13 16:55:35 -07:00
|
|
|
poh_recorder.lock().unwrap().set_bank(&bank);
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
2019-05-07 15:51:35 -07:00
|
|
|
let keypair2 = Keypair::new();
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey2 = solana_sdk::pubkey::new_rand();
|
2019-03-29 20:00:36 -07:00
|
|
|
|
2021-08-17 15:17:56 -07:00
|
|
|
let txs = sanitize_transactions(vec![
|
2019-11-08 20:56:57 -08:00
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash()),
|
|
|
|
system_transaction::transfer(&keypair2, &pubkey2, 1, genesis_config.hash()),
|
2021-08-17 15:17:56 -07:00
|
|
|
]);
|
2019-03-29 20:00:36 -07:00
|
|
|
|
2020-11-29 11:21:55 -08:00
|
|
|
let mut results = vec![(Ok(()), None), (Ok(()), None)];
|
2021-08-17 15:17:56 -07:00
|
|
|
let _ = BankingStage::record_transactions(bank.slot(), &txs, &results, &recorder);
|
2019-09-18 12:16:22 -07:00
|
|
|
let (_bank, (entry, _tick_height)) = entry_receiver.recv().unwrap();
|
2021-08-17 15:17:56 -07:00
|
|
|
assert_eq!(entry.transactions.len(), txs.len());
|
2019-03-29 20:00:36 -07:00
|
|
|
|
|
|
|
// InstructionErrors should still be recorded
|
2019-12-07 11:54:10 -08:00
|
|
|
results[0] = (
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
1,
|
2020-08-06 15:04:43 -07:00
|
|
|
SystemError::ResultWithNegativeLamports.into(),
|
2019-12-07 11:54:10 -08:00
|
|
|
)),
|
2020-11-29 11:21:55 -08:00
|
|
|
None,
|
2019-12-07 11:54:10 -08:00
|
|
|
);
|
2021-08-17 15:17:56 -07:00
|
|
|
let (res, retryable) =
|
|
|
|
BankingStage::record_transactions(bank.slot(), &txs, &results, &recorder);
|
2019-06-26 22:39:50 -07:00
|
|
|
res.unwrap();
|
|
|
|
assert!(retryable.is_empty());
|
2019-09-18 12:16:22 -07:00
|
|
|
let (_bank, (entry, _tick_height)) = entry_receiver.recv().unwrap();
|
2021-08-17 15:17:56 -07:00
|
|
|
assert_eq!(entry.transactions.len(), txs.len());
|
2019-03-29 20:00:36 -07:00
|
|
|
|
|
|
|
// Other TransactionErrors should not be recorded
|
2019-12-07 11:54:10 -08:00
|
|
|
results[0] = (Err(TransactionError::AccountNotFound), None);
|
2021-08-17 15:17:56 -07:00
|
|
|
let (res, retryable) =
|
|
|
|
BankingStage::record_transactions(bank.slot(), &txs, &results, &recorder);
|
2019-06-26 22:39:50 -07:00
|
|
|
res.unwrap();
|
|
|
|
assert!(retryable.is_empty());
|
2019-09-18 12:16:22 -07:00
|
|
|
let (_bank, (entry, _tick_height)) = entry_receiver.recv().unwrap();
|
2021-08-17 15:17:56 -07:00
|
|
|
assert_eq!(entry.transactions.len(), txs.len() - 1);
|
2019-06-26 22:39:50 -07:00
|
|
|
|
|
|
|
// Once bank is set to a new bank (setting bank.slot() + 1 in record_transactions),
|
|
|
|
// record_transactions should throw MaxHeightReached and return the set of retryable
|
|
|
|
// txs
|
2021-09-13 16:55:35 -07:00
|
|
|
let next_slot = bank.slot() + 1;
|
2021-08-17 15:17:56 -07:00
|
|
|
let (res, retryable) =
|
2021-09-13 16:55:35 -07:00
|
|
|
BankingStage::record_transactions(next_slot, &txs, &results, &recorder);
|
2020-01-02 19:50:43 -08:00
|
|
|
assert_matches!(res, Err(PohRecorderError::MaxHeightReached));
|
2019-06-26 22:39:50 -07:00
|
|
|
// The first result was an error so it's filtered out. The second result was Ok(),
|
|
|
|
// so it should be marked as retryable
|
|
|
|
assert_eq!(retryable, vec![1]);
|
|
|
|
// Should receive nothing from PohRecorder b/c record failed
|
|
|
|
assert!(entry_receiver.try_recv().is_err());
|
2021-03-23 07:10:04 -07:00
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
poh_recorder
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.is_exited
|
|
|
|
.store(true, Ordering::Relaxed);
|
2021-03-23 07:10:04 -07:00
|
|
|
let _ = poh_simulator.join();
|
2019-03-29 20:00:36 -07:00
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2019-02-16 14:02:21 -08:00
|
|
|
}
|
|
|
|
|
2019-05-08 10:32:25 -07:00
|
|
|
#[test]
|
|
|
|
fn test_bank_prepare_filter_for_pending_transaction() {
|
|
|
|
assert_eq!(
|
2021-04-12 23:28:08 -07:00
|
|
|
BankingStage::prepare_filter_for_pending_transactions(6, &[2, 4, 5]),
|
2019-05-08 10:32:25 -07:00
|
|
|
vec![
|
|
|
|
Err(TransactionError::BlockhashNotFound),
|
|
|
|
Err(TransactionError::BlockhashNotFound),
|
|
|
|
Ok(()),
|
|
|
|
Err(TransactionError::BlockhashNotFound),
|
|
|
|
Ok(()),
|
|
|
|
Ok(())
|
|
|
|
]
|
|
|
|
);
|
2019-05-02 19:05:53 -07:00
|
|
|
|
2019-05-08 10:32:25 -07:00
|
|
|
assert_eq!(
|
2021-04-12 23:28:08 -07:00
|
|
|
BankingStage::prepare_filter_for_pending_transactions(6, &[0, 2, 3]),
|
2019-05-08 10:32:25 -07:00
|
|
|
vec![
|
|
|
|
Ok(()),
|
|
|
|
Err(TransactionError::BlockhashNotFound),
|
|
|
|
Ok(()),
|
|
|
|
Ok(()),
|
|
|
|
Err(TransactionError::BlockhashNotFound),
|
|
|
|
Err(TransactionError::BlockhashNotFound),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2019-05-02 19:05:53 -07:00
|
|
|
|
2019-05-08 10:32:25 -07:00
|
|
|
#[test]
|
|
|
|
fn test_bank_filter_valid_transaction_indexes() {
|
|
|
|
assert_eq!(
|
|
|
|
BankingStage::filter_valid_transaction_indexes(
|
2020-05-15 09:35:43 -07:00
|
|
|
&[
|
2019-12-07 11:54:10 -08:00
|
|
|
(Err(TransactionError::BlockhashNotFound), None),
|
|
|
|
(Err(TransactionError::BlockhashNotFound), None),
|
2020-11-29 11:21:55 -08:00
|
|
|
(Ok(()), None),
|
2019-12-07 11:54:10 -08:00
|
|
|
(Err(TransactionError::BlockhashNotFound), None),
|
2020-11-29 11:21:55 -08:00
|
|
|
(Ok(()), None),
|
|
|
|
(Ok(()), None),
|
2019-05-08 10:32:25 -07:00
|
|
|
],
|
2020-05-15 09:35:43 -07:00
|
|
|
&[2, 4, 5, 9, 11, 13]
|
2019-05-08 10:32:25 -07:00
|
|
|
),
|
2020-05-15 09:35:43 -07:00
|
|
|
[5, 11, 13]
|
2019-05-08 10:32:25 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
BankingStage::filter_valid_transaction_indexes(
|
2020-05-15 09:35:43 -07:00
|
|
|
&[
|
2020-11-29 11:21:55 -08:00
|
|
|
(Ok(()), None),
|
2019-12-07 11:54:10 -08:00
|
|
|
(Err(TransactionError::BlockhashNotFound), None),
|
|
|
|
(Err(TransactionError::BlockhashNotFound), None),
|
2020-11-29 11:21:55 -08:00
|
|
|
(Ok(()), None),
|
|
|
|
(Ok(()), None),
|
|
|
|
(Ok(()), None),
|
2019-05-08 10:32:25 -07:00
|
|
|
],
|
2020-05-15 09:35:43 -07:00
|
|
|
&[1, 6, 7, 9, 31, 43]
|
2019-05-08 10:32:25 -07:00
|
|
|
),
|
2020-05-15 09:35:43 -07:00
|
|
|
[1, 9, 31, 43]
|
2019-05-08 10:32:25 -07:00
|
|
|
);
|
2019-05-02 19:05:53 -07:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:19:54 -07:00
|
|
|
#[test]
|
|
|
|
fn test_should_process_or_forward_packets() {
|
2020-10-19 12:12:08 -07:00
|
|
|
let my_pubkey = solana_sdk::pubkey::new_rand();
|
|
|
|
let my_pubkey1 = solana_sdk::pubkey::new_rand();
|
2021-08-05 09:53:29 -07:00
|
|
|
let bank = Arc::new(Bank::default_for_tests());
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
|
|
|
BankingStage::consume_or_forward_packets(&my_pubkey, None, Some(&bank), false, false),
|
2019-04-13 23:19:54 -07:00
|
|
|
BufferedPacketsDecision::Hold
|
|
|
|
);
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
|
|
|
BankingStage::consume_or_forward_packets(&my_pubkey, None, None, false, false),
|
2019-04-13 23:19:54 -07:00
|
|
|
BufferedPacketsDecision::Hold
|
|
|
|
);
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
|
|
|
BankingStage::consume_or_forward_packets(&my_pubkey1, None, None, false, false),
|
2019-04-13 23:19:54 -07:00
|
|
|
BufferedPacketsDecision::Hold
|
|
|
|
);
|
|
|
|
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
2021-03-03 10:23:05 -08:00
|
|
|
BankingStage::consume_or_forward_packets(
|
|
|
|
&my_pubkey,
|
|
|
|
Some(my_pubkey1),
|
2021-03-15 17:11:15 -07:00
|
|
|
None,
|
2021-03-03 10:23:05 -08:00
|
|
|
false,
|
|
|
|
false
|
|
|
|
),
|
2019-04-13 23:19:54 -07:00
|
|
|
BufferedPacketsDecision::Forward
|
|
|
|
);
|
2021-03-03 10:23:05 -08:00
|
|
|
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
2021-03-03 10:23:05 -08:00
|
|
|
BankingStage::consume_or_forward_packets(
|
|
|
|
&my_pubkey,
|
|
|
|
Some(my_pubkey1),
|
2021-03-15 17:11:15 -07:00
|
|
|
None,
|
2021-03-03 10:23:05 -08:00
|
|
|
true,
|
|
|
|
true
|
|
|
|
),
|
2019-04-23 11:56:30 -07:00
|
|
|
BufferedPacketsDecision::Hold
|
|
|
|
);
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
2021-03-03 10:23:05 -08:00
|
|
|
BankingStage::consume_or_forward_packets(
|
|
|
|
&my_pubkey,
|
|
|
|
Some(my_pubkey1),
|
2021-03-15 17:11:15 -07:00
|
|
|
None,
|
2021-03-03 10:23:05 -08:00
|
|
|
true,
|
|
|
|
false
|
|
|
|
),
|
|
|
|
BufferedPacketsDecision::ForwardAndHold
|
|
|
|
);
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
2021-03-03 10:23:05 -08:00
|
|
|
BankingStage::consume_or_forward_packets(
|
|
|
|
&my_pubkey,
|
|
|
|
Some(my_pubkey1),
|
2021-03-15 17:11:15 -07:00
|
|
|
Some(&bank),
|
2021-03-03 10:23:05 -08:00
|
|
|
false,
|
|
|
|
false
|
|
|
|
),
|
2021-03-15 17:11:15 -07:00
|
|
|
BufferedPacketsDecision::Consume(_)
|
2019-04-13 23:19:54 -07:00
|
|
|
);
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
2021-03-03 10:23:05 -08:00
|
|
|
BankingStage::consume_or_forward_packets(
|
|
|
|
&my_pubkey1,
|
|
|
|
Some(my_pubkey1),
|
2021-03-15 17:11:15 -07:00
|
|
|
None,
|
2021-03-03 10:23:05 -08:00
|
|
|
false,
|
|
|
|
false
|
|
|
|
),
|
2019-04-13 23:19:54 -07:00
|
|
|
BufferedPacketsDecision::Hold
|
|
|
|
);
|
2021-03-15 17:11:15 -07:00
|
|
|
assert_matches!(
|
2021-03-03 10:23:05 -08:00
|
|
|
BankingStage::consume_or_forward_packets(
|
|
|
|
&my_pubkey1,
|
|
|
|
Some(my_pubkey1),
|
2021-03-15 17:11:15 -07:00
|
|
|
Some(&bank),
|
2021-03-03 10:23:05 -08:00
|
|
|
false,
|
|
|
|
false
|
|
|
|
),
|
2021-03-15 17:11:15 -07:00
|
|
|
BufferedPacketsDecision::Consume(_)
|
2019-04-13 23:19:54 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:05:43 -07:00
|
|
|
fn create_slow_genesis_config(lamports: u64) -> GenesisConfigInfo {
|
|
|
|
let mut config_info = create_genesis_config(lamports);
|
|
|
|
// For these tests there's only 1 slot, don't want to run out of ticks
|
|
|
|
config_info.genesis_config.ticks_per_slot *= 8;
|
|
|
|
config_info
|
|
|
|
}
|
|
|
|
|
2019-02-16 14:02:21 -08:00
|
|
|
#[test]
|
|
|
|
fn test_bank_process_and_record_transactions() {
|
2019-02-26 10:48:18 -08:00
|
|
|
solana_logger::setup();
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2021-03-24 14:05:43 -07:00
|
|
|
} = create_slow_genesis_config(10_000);
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2021-04-12 23:28:08 -07:00
|
|
|
let transactions =
|
|
|
|
vec![
|
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash())
|
2021-07-15 20:51:27 -07:00
|
|
|
.try_into()
|
|
|
|
.unwrap(),
|
2021-04-12 23:28:08 -07:00
|
|
|
];
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger");
|
2021-03-23 07:10:04 -07:00
|
|
|
let (poh_recorder, entry_receiver, record_receiver) = PohRecorder::new(
|
2019-03-29 20:00:36 -07:00
|
|
|
bank.tick_height(),
|
|
|
|
bank.last_blockhash(),
|
2021-09-13 16:55:35 -07:00
|
|
|
bank.clone(),
|
2019-07-26 11:33:51 -07:00
|
|
|
Some((4, 4)),
|
2019-03-29 20:00:36 -07:00
|
|
|
bank.ticks_per_slot(),
|
|
|
|
&pubkey,
|
2020-01-13 13:13:52 -08:00
|
|
|
&Arc::new(blockstore),
|
2019-04-19 02:39:44 -07:00
|
|
|
&Arc::new(LeaderScheduleCache::new_from_bank(&bank)),
|
2019-05-18 14:01:36 -07:00
|
|
|
&Arc::new(PohConfig::default()),
|
2021-04-14 10:07:21 -07:00
|
|
|
Arc::new(AtomicBool::default()),
|
2019-03-29 20:00:36 -07:00
|
|
|
);
|
2021-03-23 07:10:04 -07:00
|
|
|
let recorder = poh_recorder.recorder();
|
2019-03-29 20:00:36 -07:00
|
|
|
let poh_recorder = Arc::new(Mutex::new(poh_recorder));
|
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
let poh_simulator = simulate_poh(record_receiver, &poh_recorder);
|
2021-03-23 07:10:04 -07:00
|
|
|
|
2021-09-13 16:55:35 -07:00
|
|
|
poh_recorder.lock().unwrap().set_bank(&bank);
|
2020-08-07 11:21:35 -07:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
2019-03-29 20:00:36 -07:00
|
|
|
|
2019-11-20 15:43:10 -08:00
|
|
|
BankingStage::process_and_record_transactions(
|
|
|
|
&bank,
|
|
|
|
&transactions,
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2019-11-20 15:43:10 -08:00
|
|
|
0,
|
|
|
|
None,
|
2020-08-07 11:21:35 -07:00
|
|
|
&gossip_vote_sender,
|
2019-11-20 15:43:10 -08:00
|
|
|
)
|
|
|
|
.0
|
|
|
|
.unwrap();
|
2021-09-13 16:55:35 -07:00
|
|
|
|
|
|
|
// Tick up to max tick height
|
|
|
|
while poh_recorder.lock().unwrap().tick_height() != bank.max_tick_height() {
|
|
|
|
poh_recorder.lock().unwrap().tick();
|
|
|
|
}
|
2019-03-29 20:00:36 -07:00
|
|
|
|
|
|
|
let mut done = false;
|
|
|
|
// read entries until I find mine, might be ticks...
|
2019-09-18 12:16:22 -07:00
|
|
|
while let Ok((_bank, (entry, _tick_height))) = entry_receiver.recv() {
|
|
|
|
if !entry.is_tick() {
|
|
|
|
trace!("got entry");
|
|
|
|
assert_eq!(entry.transactions.len(), transactions.len());
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1);
|
|
|
|
done = true;
|
2019-03-29 20:00:36 -07:00
|
|
|
}
|
|
|
|
if done {
|
|
|
|
break;
|
2019-02-16 14:02:21 -08:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 20:00:36 -07:00
|
|
|
trace!("done ticking");
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2021-05-19 07:31:47 -07:00
|
|
|
assert!(done);
|
2019-02-26 10:48:18 -08:00
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
let transactions = vec![system_transaction::transfer(
|
2019-03-29 20:00:36 -07:00
|
|
|
&mint_keypair,
|
|
|
|
&pubkey,
|
|
|
|
2,
|
2019-11-08 20:56:57 -08:00
|
|
|
genesis_config.hash(),
|
2021-04-12 23:28:08 -07:00
|
|
|
)
|
2021-07-15 20:51:27 -07:00
|
|
|
.try_into()
|
|
|
|
.unwrap()];
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
assert_matches!(
|
2019-05-07 10:23:02 -07:00
|
|
|
BankingStage::process_and_record_transactions(
|
|
|
|
&bank,
|
|
|
|
&transactions,
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2019-11-20 15:43:10 -08:00
|
|
|
0,
|
|
|
|
None,
|
2020-08-07 11:21:35 -07:00
|
|
|
&gossip_vote_sender,
|
2019-05-07 10:23:02 -07:00
|
|
|
)
|
|
|
|
.0,
|
2020-01-02 19:50:43 -08:00
|
|
|
Err(PohRecorderError::MaxHeightReached)
|
2019-03-29 20:00:36 -07:00
|
|
|
);
|
2019-02-16 14:02:21 -08:00
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
poh_recorder
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.is_exited
|
|
|
|
.store(true, Ordering::Relaxed);
|
2021-03-23 07:10:04 -07:00
|
|
|
let _ = poh_simulator.join();
|
|
|
|
|
2019-03-29 20:00:36 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), 1);
|
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2019-02-16 14:02:21 -08:00
|
|
|
}
|
2019-05-07 10:23:02 -07:00
|
|
|
|
2021-03-23 07:10:04 -07:00
|
|
|
fn simulate_poh(
|
2021-04-14 10:07:21 -07:00
|
|
|
record_receiver: CrossbeamReceiver<Record>,
|
2021-03-23 07:10:04 -07:00
|
|
|
poh_recorder: &Arc<Mutex<PohRecorder>>,
|
2021-04-14 10:07:21 -07:00
|
|
|
) -> JoinHandle<()> {
|
2021-03-23 07:10:04 -07:00
|
|
|
let poh_recorder = poh_recorder.clone();
|
2021-04-14 10:07:21 -07:00
|
|
|
let is_exited = poh_recorder.lock().unwrap().is_exited.clone();
|
2021-03-23 07:10:04 -07:00
|
|
|
let tick_producer = Builder::new()
|
|
|
|
.name("solana-simulate_poh".to_string())
|
|
|
|
.spawn(move || loop {
|
|
|
|
PohService::read_record_receiver_and_process(
|
|
|
|
&poh_recorder,
|
|
|
|
&record_receiver,
|
|
|
|
Duration::from_millis(10),
|
|
|
|
);
|
2021-04-14 10:07:21 -07:00
|
|
|
if is_exited.load(Ordering::Relaxed) {
|
2021-03-23 07:10:04 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2021-04-14 10:07:21 -07:00
|
|
|
tick_producer.unwrap()
|
2021-03-23 07:10:04 -07:00
|
|
|
}
|
|
|
|
|
2019-05-07 10:23:02 -07:00
|
|
|
#[test]
|
|
|
|
fn test_bank_process_and_record_transactions_account_in_use() {
|
|
|
|
solana_logger::setup();
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-05-22 20:39:00 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2021-03-24 14:05:43 -07:00
|
|
|
} = create_slow_genesis_config(10_000);
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
|
|
|
let pubkey1 = solana_sdk::pubkey::new_rand();
|
2019-05-07 10:23:02 -07:00
|
|
|
|
|
|
|
let transactions = vec![
|
2021-07-15 20:51:27 -07:00
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash())
|
|
|
|
.try_into()
|
|
|
|
.unwrap(),
|
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey1, 1, genesis_config.hash())
|
|
|
|
.try_into()
|
|
|
|
.unwrap(),
|
2019-05-07 10:23:02 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger");
|
2021-03-23 07:10:04 -07:00
|
|
|
let (poh_recorder, _entry_receiver, record_receiver) = PohRecorder::new(
|
2019-05-07 10:23:02 -07:00
|
|
|
bank.tick_height(),
|
|
|
|
bank.last_blockhash(),
|
2021-09-13 16:55:35 -07:00
|
|
|
bank.clone(),
|
2019-07-26 11:33:51 -07:00
|
|
|
Some((4, 4)),
|
2019-05-07 10:23:02 -07:00
|
|
|
bank.ticks_per_slot(),
|
|
|
|
&pubkey,
|
2020-01-13 13:13:52 -08:00
|
|
|
&Arc::new(blockstore),
|
2019-05-07 10:23:02 -07:00
|
|
|
&Arc::new(LeaderScheduleCache::new_from_bank(&bank)),
|
2019-05-18 14:01:36 -07:00
|
|
|
&Arc::new(PohConfig::default()),
|
2021-04-14 10:07:21 -07:00
|
|
|
Arc::new(AtomicBool::default()),
|
2019-05-07 10:23:02 -07:00
|
|
|
);
|
2021-03-23 07:10:04 -07:00
|
|
|
let recorder = poh_recorder.recorder();
|
2019-05-07 10:23:02 -07:00
|
|
|
let poh_recorder = Arc::new(Mutex::new(poh_recorder));
|
|
|
|
|
2021-09-13 16:55:35 -07:00
|
|
|
poh_recorder.lock().unwrap().set_bank(&bank);
|
2019-05-07 10:23:02 -07:00
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
let poh_simulator = simulate_poh(record_receiver, &poh_recorder);
|
2021-03-23 07:10:04 -07:00
|
|
|
|
2020-08-07 11:21:35 -07:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
|
2019-05-07 10:23:02 -07:00
|
|
|
let (result, unprocessed) = BankingStage::process_and_record_transactions(
|
|
|
|
&bank,
|
|
|
|
&transactions,
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2019-05-07 10:23:02 -07:00
|
|
|
0,
|
2019-11-20 15:43:10 -08:00
|
|
|
None,
|
2020-08-07 11:21:35 -07:00
|
|
|
&gossip_vote_sender,
|
2019-05-07 10:23:02 -07:00
|
|
|
);
|
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
poh_recorder
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.is_exited
|
|
|
|
.store(true, Ordering::Relaxed);
|
2021-03-23 07:10:04 -07:00
|
|
|
let _ = poh_simulator.join();
|
|
|
|
|
2019-05-07 10:23:02 -07:00
|
|
|
assert!(result.is_ok());
|
|
|
|
assert_eq!(unprocessed.len(), 1);
|
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2019-05-07 10:23:02 -07:00
|
|
|
}
|
2019-05-10 14:28:38 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filter_valid_packets() {
|
|
|
|
solana_logger::setup();
|
|
|
|
|
2021-03-03 10:23:05 -08:00
|
|
|
let mut all_packets = (0..16)
|
2019-05-10 14:28:38 -07:00
|
|
|
.map(|packets_id| {
|
|
|
|
let packets = Packets::new(
|
|
|
|
(0..32)
|
|
|
|
.map(|packet_id| {
|
|
|
|
let mut p = Packet::default();
|
|
|
|
p.meta.port = packets_id << 8 | packet_id;
|
|
|
|
p
|
|
|
|
})
|
|
|
|
.collect_vec(),
|
|
|
|
);
|
|
|
|
let valid_indexes = (0..32)
|
|
|
|
.filter_map(|x| if x % 2 != 0 { Some(x as usize) } else { None })
|
|
|
|
.collect_vec();
|
2021-03-03 10:23:05 -08:00
|
|
|
(packets, valid_indexes, false)
|
2019-05-10 14:28:38 -07:00
|
|
|
})
|
|
|
|
.collect_vec();
|
|
|
|
|
2021-02-12 03:27:37 -08:00
|
|
|
let result = BankingStage::filter_valid_packets_for_forwarding(all_packets.iter());
|
2019-05-10 14:28:38 -07:00
|
|
|
|
|
|
|
assert_eq!(result.len(), 256);
|
|
|
|
|
|
|
|
let _ = result
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, p)| {
|
|
|
|
let packets_id = index / 16;
|
|
|
|
let packet_id = (index % 16) * 2 + 1;
|
|
|
|
assert_eq!(p.meta.port, (packets_id << 8 | packet_id) as u16);
|
|
|
|
})
|
|
|
|
.collect_vec();
|
2021-03-03 10:23:05 -08:00
|
|
|
|
|
|
|
all_packets[0].2 = true;
|
|
|
|
let result = BankingStage::filter_valid_packets_for_forwarding(all_packets.iter());
|
|
|
|
assert_eq!(result.len(), 240);
|
2019-05-10 14:28:38 -07:00
|
|
|
}
|
2019-06-26 22:39:50 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_process_transactions_returns_unprocessed_txs() {
|
|
|
|
solana_logger::setup();
|
2019-11-08 20:56:57 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
2019-06-26 22:39:50 -07:00
|
|
|
mint_keypair,
|
|
|
|
..
|
2021-03-24 14:05:43 -07:00
|
|
|
} = create_slow_genesis_config(10_000);
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2019-06-26 22:39:50 -07:00
|
|
|
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
2019-06-26 22:39:50 -07:00
|
|
|
|
2019-09-17 15:11:29 -07:00
|
|
|
let transactions =
|
2019-11-08 20:56:57 -08:00
|
|
|
vec![
|
2021-04-12 23:28:08 -07:00
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash())
|
2021-07-15 20:51:27 -07:00
|
|
|
.try_into()
|
|
|
|
.unwrap(),
|
2019-11-08 20:56:57 -08:00
|
|
|
];
|
2019-06-26 22:39:50 -07:00
|
|
|
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger");
|
2021-03-23 07:10:04 -07:00
|
|
|
let (poh_recorder, _entry_receiver, record_receiver) = PohRecorder::new(
|
2019-06-26 22:39:50 -07:00
|
|
|
bank.tick_height(),
|
|
|
|
bank.last_blockhash(),
|
2021-09-13 16:55:35 -07:00
|
|
|
bank.clone(),
|
2019-07-26 11:33:51 -07:00
|
|
|
Some((4, 4)),
|
2019-06-26 22:39:50 -07:00
|
|
|
bank.ticks_per_slot(),
|
2020-10-19 12:12:08 -07:00
|
|
|
&solana_sdk::pubkey::new_rand(),
|
2020-01-13 13:13:52 -08:00
|
|
|
&Arc::new(blockstore),
|
2019-06-26 22:39:50 -07:00
|
|
|
&Arc::new(LeaderScheduleCache::new_from_bank(&bank)),
|
|
|
|
&Arc::new(PohConfig::default()),
|
2021-04-14 10:07:21 -07:00
|
|
|
Arc::new(AtomicBool::default()),
|
2019-06-26 22:39:50 -07:00
|
|
|
);
|
|
|
|
|
2021-03-23 07:10:04 -07:00
|
|
|
// Poh Recorder has no working bank, so should throw MaxHeightReached error on
|
2019-06-26 22:39:50 -07:00
|
|
|
// record
|
2021-03-23 07:10:04 -07:00
|
|
|
let recorder = poh_recorder.recorder();
|
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
let poh_simulator = simulate_poh(record_receiver, &Arc::new(Mutex::new(poh_recorder)));
|
2019-06-26 22:39:50 -07:00
|
|
|
|
2020-08-07 11:21:35 -07:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
|
2019-06-26 22:39:50 -07:00
|
|
|
let (processed_transactions_count, mut retryable_txs) =
|
2020-08-07 11:21:35 -07:00
|
|
|
BankingStage::process_transactions(
|
|
|
|
&bank,
|
2021-03-15 17:11:15 -07:00
|
|
|
&Instant::now(),
|
2020-08-07 11:21:35 -07:00
|
|
|
&transactions,
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2020-08-07 11:21:35 -07:00
|
|
|
None,
|
|
|
|
&gossip_vote_sender,
|
|
|
|
);
|
2019-06-26 22:39:50 -07:00
|
|
|
|
|
|
|
assert_eq!(processed_transactions_count, 0,);
|
|
|
|
|
2020-12-13 17:26:34 -08:00
|
|
|
retryable_txs.sort_unstable();
|
2019-06-26 22:39:50 -07:00
|
|
|
let expected: Vec<usize> = (0..transactions.len()).collect();
|
|
|
|
assert_eq!(retryable_txs, expected);
|
2021-03-23 07:10:04 -07:00
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
recorder.is_exited.store(true, Ordering::Relaxed);
|
2021-03-23 07:10:04 -07:00
|
|
|
let _ = poh_simulator.join();
|
2019-06-26 22:39:50 -07:00
|
|
|
}
|
|
|
|
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2019-06-26 22:39:50 -07:00
|
|
|
}
|
2019-11-20 15:43:10 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_persist_transaction_status() {
|
|
|
|
solana_logger::setup();
|
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair,
|
|
|
|
..
|
2021-03-24 14:05:43 -07:00
|
|
|
} = create_slow_genesis_config(10_000);
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(&genesis_config));
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
|
|
|
let pubkey1 = solana_sdk::pubkey::new_rand();
|
2019-11-20 15:43:10 -08:00
|
|
|
let keypair1 = Keypair::new();
|
|
|
|
|
|
|
|
let success_tx =
|
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey, 1, genesis_config.hash());
|
|
|
|
let success_signature = success_tx.signatures[0];
|
|
|
|
let entry_1 = next_entry(&genesis_config.hash(), 1, vec![success_tx.clone()]);
|
|
|
|
let ix_error_tx =
|
|
|
|
system_transaction::transfer(&keypair1, &pubkey1, 10, genesis_config.hash());
|
|
|
|
let ix_error_signature = ix_error_tx.signatures[0];
|
|
|
|
let entry_2 = next_entry(&entry_1.hash, 1, vec![ix_error_tx.clone()]);
|
|
|
|
let fail_tx =
|
|
|
|
system_transaction::transfer(&mint_keypair, &pubkey1, 1, genesis_config.hash());
|
|
|
|
let entry_3 = next_entry(&entry_2.hash, 1, vec![fail_tx.clone()]);
|
|
|
|
let entries = vec![entry_1, entry_2, entry_3];
|
|
|
|
|
2021-07-15 20:51:27 -07:00
|
|
|
let transactions = vec![
|
|
|
|
success_tx.try_into().unwrap(),
|
|
|
|
ix_error_tx.try_into().unwrap(),
|
|
|
|
fail_tx.try_into().unwrap(),
|
|
|
|
];
|
2019-11-20 15:43:10 -08:00
|
|
|
bank.transfer(4, &mint_keypair, &keypair1.pubkey()).unwrap();
|
|
|
|
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2020-01-13 13:13:52 -08:00
|
|
|
let blockstore = Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger");
|
|
|
|
let blockstore = Arc::new(blockstore);
|
2021-03-23 07:10:04 -07:00
|
|
|
let (poh_recorder, _entry_receiver, record_receiver) = PohRecorder::new(
|
2019-11-20 15:43:10 -08:00
|
|
|
bank.tick_height(),
|
|
|
|
bank.last_blockhash(),
|
2021-09-13 16:55:35 -07:00
|
|
|
bank.clone(),
|
2019-11-20 15:43:10 -08:00
|
|
|
Some((4, 4)),
|
|
|
|
bank.ticks_per_slot(),
|
|
|
|
&pubkey,
|
2020-01-13 13:13:52 -08:00
|
|
|
&blockstore,
|
2019-11-20 15:43:10 -08:00
|
|
|
&Arc::new(LeaderScheduleCache::new_from_bank(&bank)),
|
|
|
|
&Arc::new(PohConfig::default()),
|
2021-04-14 10:07:21 -07:00
|
|
|
Arc::new(AtomicBool::default()),
|
2019-11-20 15:43:10 -08:00
|
|
|
);
|
2021-03-23 07:10:04 -07:00
|
|
|
let recorder = poh_recorder.recorder();
|
2019-11-20 15:43:10 -08:00
|
|
|
let poh_recorder = Arc::new(Mutex::new(poh_recorder));
|
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
let poh_simulator = simulate_poh(record_receiver, &poh_recorder);
|
2021-03-23 07:10:04 -07:00
|
|
|
|
2021-09-13 16:55:35 -07:00
|
|
|
poh_recorder.lock().unwrap().set_bank(&bank);
|
2019-11-20 15:43:10 -08:00
|
|
|
|
2020-05-15 09:35:43 -07:00
|
|
|
let shreds = entries_to_test_shreds(entries, bank.slot(), 0, true, 0);
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.insert_shreds(shreds, None, false).unwrap();
|
2021-07-01 20:02:40 -07:00
|
|
|
blockstore.set_roots(std::iter::once(&bank.slot())).unwrap();
|
2019-11-20 15:43:10 -08:00
|
|
|
|
2019-11-21 13:23:40 -08:00
|
|
|
let (transaction_status_sender, transaction_status_receiver) = unbounded();
|
2019-11-20 15:43:10 -08:00
|
|
|
let transaction_status_service = TransactionStatusService::new(
|
|
|
|
transaction_status_receiver,
|
2021-03-26 15:47:35 -07:00
|
|
|
Arc::new(AtomicU64::default()),
|
2020-01-13 13:13:52 -08:00
|
|
|
blockstore.clone(),
|
2019-11-20 15:43:10 -08:00
|
|
|
&Arc::new(AtomicBool::new(false)),
|
|
|
|
);
|
|
|
|
|
2020-08-07 11:21:35 -07:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
|
2019-11-20 15:43:10 -08:00
|
|
|
let _ = BankingStage::process_and_record_transactions(
|
|
|
|
&bank,
|
|
|
|
&transactions,
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2019-11-20 15:43:10 -08:00
|
|
|
0,
|
2021-02-01 13:00:51 -08:00
|
|
|
Some(TransactionStatusSender {
|
|
|
|
sender: transaction_status_sender,
|
|
|
|
enable_cpi_and_log_storage: false,
|
|
|
|
}),
|
2020-08-07 11:21:35 -07:00
|
|
|
&gossip_vote_sender,
|
2019-11-20 15:43:10 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
transaction_status_service.join().unwrap();
|
|
|
|
|
2021-03-26 15:47:35 -07:00
|
|
|
let confirmed_block = blockstore.get_rooted_block(bank.slot(), false).unwrap();
|
2019-11-20 15:43:10 -08:00
|
|
|
assert_eq!(confirmed_block.transactions.len(), 3);
|
|
|
|
|
2020-03-26 13:29:30 -07:00
|
|
|
for TransactionWithStatusMeta { transaction, meta } in
|
2020-01-14 23:25:45 -08:00
|
|
|
confirmed_block.transactions.into_iter()
|
|
|
|
{
|
2020-09-23 22:10:29 -07:00
|
|
|
if transaction.signatures[0] == success_signature {
|
|
|
|
let meta = meta.unwrap();
|
|
|
|
assert_eq!(meta.status, Ok(()));
|
|
|
|
} else if transaction.signatures[0] == ix_error_signature {
|
|
|
|
let meta = meta.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
meta.status,
|
|
|
|
Err(TransactionError::InstructionError(
|
|
|
|
0,
|
|
|
|
InstructionError::Custom(1)
|
|
|
|
))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
assert_eq!(meta, None);
|
2019-11-20 15:43:10 -08:00
|
|
|
}
|
|
|
|
}
|
2021-03-23 07:10:04 -07:00
|
|
|
|
2021-04-14 10:07:21 -07:00
|
|
|
poh_recorder
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.is_exited
|
|
|
|
.store(true, Ordering::Relaxed);
|
2021-03-23 07:10:04 -07:00
|
|
|
let _ = poh_simulator.join();
|
2019-11-20 15:43:10 -08:00
|
|
|
}
|
2020-01-13 13:13:52 -08:00
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
2019-11-20 15:43:10 -08:00
|
|
|
}
|
2021-02-12 03:27:37 -08:00
|
|
|
|
2021-03-23 07:10:04 -07:00
|
|
|
#[allow(clippy::type_complexity)]
|
2021-02-12 03:27:37 -08:00
|
|
|
fn setup_conflicting_transactions(
|
|
|
|
ledger_path: &Path,
|
|
|
|
) -> (
|
|
|
|
Vec<Transaction>,
|
|
|
|
Arc<Bank>,
|
|
|
|
Arc<Mutex<PohRecorder>>,
|
|
|
|
Receiver<WorkingBankEntry>,
|
2021-03-23 07:10:04 -07:00
|
|
|
JoinHandle<()>,
|
2021-02-12 03:27:37 -08:00
|
|
|
) {
|
2021-06-18 06:34:46 -07:00
|
|
|
Blockstore::destroy(ledger_path).unwrap();
|
2021-03-24 14:05:43 -07:00
|
|
|
let genesis_config_info = create_slow_genesis_config(10_000);
|
2021-02-12 03:27:37 -08:00
|
|
|
let GenesisConfigInfo {
|
|
|
|
genesis_config,
|
|
|
|
mint_keypair,
|
|
|
|
..
|
|
|
|
} = &genesis_config_info;
|
|
|
|
let blockstore =
|
2021-06-18 06:34:46 -07:00
|
|
|
Blockstore::open(ledger_path).expect("Expected to be able to open database ledger");
|
2021-08-05 12:50:25 -07:00
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(genesis_config));
|
2021-04-14 10:07:21 -07:00
|
|
|
let exit = Arc::new(AtomicBool::default());
|
2021-03-23 07:10:04 -07:00
|
|
|
let (poh_recorder, entry_receiver, record_receiver) = PohRecorder::new(
|
2021-02-12 03:27:37 -08:00
|
|
|
bank.tick_height(),
|
|
|
|
bank.last_blockhash(),
|
2021-09-13 16:55:35 -07:00
|
|
|
bank.clone(),
|
2021-02-12 03:27:37 -08:00
|
|
|
Some((4, 4)),
|
|
|
|
bank.ticks_per_slot(),
|
|
|
|
&solana_sdk::pubkey::new_rand(),
|
|
|
|
&Arc::new(blockstore),
|
|
|
|
&Arc::new(LeaderScheduleCache::new_from_bank(&bank)),
|
|
|
|
&Arc::new(PohConfig::default()),
|
2021-04-14 10:07:21 -07:00
|
|
|
exit,
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
|
|
|
let poh_recorder = Arc::new(Mutex::new(poh_recorder));
|
|
|
|
|
|
|
|
// Set up unparallelizable conflicting transactions
|
|
|
|
let pubkey0 = solana_sdk::pubkey::new_rand();
|
|
|
|
let pubkey1 = solana_sdk::pubkey::new_rand();
|
|
|
|
let pubkey2 = solana_sdk::pubkey::new_rand();
|
|
|
|
let transactions = vec![
|
2021-06-18 06:34:46 -07:00
|
|
|
system_transaction::transfer(mint_keypair, &pubkey0, 1, genesis_config.hash()),
|
|
|
|
system_transaction::transfer(mint_keypair, &pubkey1, 1, genesis_config.hash()),
|
|
|
|
system_transaction::transfer(mint_keypair, &pubkey2, 1, genesis_config.hash()),
|
2021-02-12 03:27:37 -08:00
|
|
|
];
|
2021-04-14 10:07:21 -07:00
|
|
|
let poh_simulator = simulate_poh(record_receiver, &poh_recorder);
|
2021-03-23 07:10:04 -07:00
|
|
|
|
|
|
|
(
|
|
|
|
transactions,
|
|
|
|
bank,
|
|
|
|
poh_recorder,
|
|
|
|
entry_receiver,
|
|
|
|
poh_simulator,
|
|
|
|
)
|
2021-02-12 03:27:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_consume_buffered_packets() {
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2021-04-14 10:07:21 -07:00
|
|
|
let (transactions, bank, poh_recorder, _entry_receiver, poh_simulator) =
|
2021-02-12 03:27:37 -08:00
|
|
|
setup_conflicting_transactions(&ledger_path);
|
2021-03-23 07:10:04 -07:00
|
|
|
let recorder = poh_recorder.lock().unwrap().recorder();
|
2021-02-12 03:27:37 -08:00
|
|
|
let num_conflicting_transactions = transactions.len();
|
|
|
|
let mut packets_vec = to_packets_chunked(&transactions, num_conflicting_transactions);
|
|
|
|
assert_eq!(packets_vec.len(), 1);
|
|
|
|
assert_eq!(packets_vec[0].packets.len(), num_conflicting_transactions);
|
|
|
|
let all_packets = packets_vec.pop().unwrap();
|
|
|
|
let mut buffered_packets: UnprocessedPackets = vec![(
|
|
|
|
all_packets,
|
|
|
|
(0..num_conflicting_transactions).into_iter().collect(),
|
2021-03-03 10:23:05 -08:00
|
|
|
false,
|
2021-02-12 03:27:37 -08:00
|
|
|
)]
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
|
|
|
|
// When the working bank in poh_recorder is None, no packets should be processed
|
|
|
|
assert!(!poh_recorder.lock().unwrap().has_bank());
|
2021-03-15 17:11:15 -07:00
|
|
|
let max_tx_processing_ns = std::u128::MAX;
|
2021-02-12 03:27:37 -08:00
|
|
|
BankingStage::consume_buffered_packets(
|
|
|
|
&Pubkey::default(),
|
2021-03-15 17:11:15 -07:00
|
|
|
max_tx_processing_ns,
|
2021-02-12 03:27:37 -08:00
|
|
|
&poh_recorder,
|
|
|
|
&mut buffered_packets,
|
|
|
|
None,
|
|
|
|
&gossip_vote_sender,
|
|
|
|
None::<Box<dyn Fn()>>,
|
2021-04-12 23:28:08 -07:00
|
|
|
&BankingStageStats::default(),
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
|
|
|
assert_eq!(buffered_packets[0].1.len(), num_conflicting_transactions);
|
|
|
|
// When the poh recorder has a bank, should process all non conflicting buffered packets.
|
|
|
|
// Processes one packet per iteration of the loop
|
|
|
|
for num_expected_unprocessed in (0..num_conflicting_transactions).rev() {
|
|
|
|
poh_recorder.lock().unwrap().set_bank(&bank);
|
|
|
|
BankingStage::consume_buffered_packets(
|
|
|
|
&Pubkey::default(),
|
2021-03-15 17:11:15 -07:00
|
|
|
max_tx_processing_ns,
|
2021-02-12 03:27:37 -08:00
|
|
|
&poh_recorder,
|
|
|
|
&mut buffered_packets,
|
|
|
|
None,
|
|
|
|
&gossip_vote_sender,
|
|
|
|
None::<Box<dyn Fn()>>,
|
2021-04-12 23:28:08 -07:00
|
|
|
&BankingStageStats::default(),
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
|
|
|
if num_expected_unprocessed == 0 {
|
|
|
|
assert!(buffered_packets.is_empty())
|
|
|
|
} else {
|
|
|
|
assert_eq!(buffered_packets[0].1.len(), num_expected_unprocessed);
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 10:07:21 -07:00
|
|
|
poh_recorder
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.is_exited
|
|
|
|
.store(true, Ordering::Relaxed);
|
2021-03-23 07:10:04 -07:00
|
|
|
let _ = poh_simulator.join();
|
2021-02-12 03:27:37 -08:00
|
|
|
}
|
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_consume_buffered_packets_interrupted() {
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
2021-04-14 10:07:21 -07:00
|
|
|
let (transactions, bank, poh_recorder, _entry_receiver, poh_simulator) =
|
2021-02-12 03:27:37 -08:00
|
|
|
setup_conflicting_transactions(&ledger_path);
|
|
|
|
let num_conflicting_transactions = transactions.len();
|
|
|
|
let packets_vec = to_packets_chunked(&transactions, 1);
|
|
|
|
assert_eq!(packets_vec.len(), num_conflicting_transactions);
|
|
|
|
for single_packets in &packets_vec {
|
|
|
|
assert_eq!(single_packets.packets.len(), 1);
|
|
|
|
}
|
|
|
|
let mut buffered_packets: UnprocessedPackets = packets_vec
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
2021-03-03 10:23:05 -08:00
|
|
|
.map(|single_packets| (single_packets, vec![0], false))
|
2021-02-12 03:27:37 -08:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let (continue_sender, continue_receiver) = unbounded();
|
|
|
|
let (finished_packet_sender, finished_packet_receiver) = unbounded();
|
|
|
|
|
|
|
|
let test_fn = Some(move || {
|
|
|
|
finished_packet_sender.send(()).unwrap();
|
|
|
|
continue_receiver.recv().unwrap();
|
|
|
|
});
|
|
|
|
// When the poh recorder has a bank, it should process all non conflicting buffered packets.
|
|
|
|
// Because each conflicting transaction is in it's own `Packet` within `packets_vec`, then
|
|
|
|
// each iteration of this loop will process one element of `packets_vec`per iteration of the
|
|
|
|
// loop.
|
|
|
|
let interrupted_iteration = 1;
|
|
|
|
poh_recorder.lock().unwrap().set_bank(&bank);
|
|
|
|
let poh_recorder_ = poh_recorder.clone();
|
2021-03-23 07:10:04 -07:00
|
|
|
let recorder = poh_recorder_.lock().unwrap().recorder();
|
2021-02-12 03:27:37 -08:00
|
|
|
let (gossip_vote_sender, _gossip_vote_receiver) = unbounded();
|
|
|
|
// Start up thread to process the banks
|
|
|
|
let t_consume = Builder::new()
|
|
|
|
.name("consume-buffered-packets".to_string())
|
|
|
|
.spawn(move || {
|
|
|
|
BankingStage::consume_buffered_packets(
|
|
|
|
&Pubkey::default(),
|
2021-03-15 17:11:15 -07:00
|
|
|
std::u128::MAX,
|
2021-02-12 03:27:37 -08:00
|
|
|
&poh_recorder_,
|
|
|
|
&mut buffered_packets,
|
|
|
|
None,
|
|
|
|
&gossip_vote_sender,
|
|
|
|
test_fn,
|
2021-04-12 23:28:08 -07:00
|
|
|
&BankingStageStats::default(),
|
2021-03-23 07:10:04 -07:00
|
|
|
&recorder,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check everything is correct. All indexes after `interrupted_iteration`
|
|
|
|
// should still be unprocessed
|
|
|
|
assert_eq!(
|
|
|
|
buffered_packets.len(),
|
2021-04-18 10:27:36 -07:00
|
|
|
packets_vec[interrupted_iteration + 1..].len()
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
2021-03-03 10:23:05 -08:00
|
|
|
for ((remaining_unprocessed_packet, _, _forwarded), original_packet) in
|
|
|
|
buffered_packets
|
|
|
|
.iter()
|
|
|
|
.zip(&packets_vec[interrupted_iteration + 1..])
|
2021-02-12 03:27:37 -08:00
|
|
|
{
|
|
|
|
assert_eq!(
|
|
|
|
remaining_unprocessed_packet.packets[0],
|
|
|
|
original_packet.packets[0]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
for i in 0..=interrupted_iteration {
|
|
|
|
finished_packet_receiver.recv().unwrap();
|
|
|
|
if i == interrupted_iteration {
|
|
|
|
poh_recorder
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.schedule_dummy_max_height_reached_failure();
|
|
|
|
}
|
|
|
|
continue_sender.send(()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
t_consume.join().unwrap();
|
2021-04-14 10:07:21 -07:00
|
|
|
poh_recorder
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.is_exited
|
|
|
|
.store(true, Ordering::Relaxed);
|
2021-03-23 07:10:04 -07:00
|
|
|
let _ = poh_simulator.join();
|
2021-02-12 03:27:37 -08:00
|
|
|
}
|
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:49:41 -07:00
|
|
|
#[test]
|
|
|
|
fn test_forwarder_budget() {
|
|
|
|
solana_logger::setup();
|
|
|
|
// Create `Packets` with 1 unprocessed element
|
|
|
|
let single_element_packets = Packets::new(vec![Packet::default()]);
|
|
|
|
let mut unprocessed_packets: UnprocessedPackets =
|
|
|
|
vec![(single_element_packets, vec![0], false)]
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let cluster_info = new_test_cluster_info(Node::new_localhost().info);
|
|
|
|
|
|
|
|
let genesis_config_info = create_slow_genesis_config(10_000);
|
|
|
|
let GenesisConfigInfo { genesis_config, .. } = &genesis_config_info;
|
|
|
|
|
|
|
|
let bank = Arc::new(Bank::new_no_wallclock_throttle_for_tests(genesis_config));
|
|
|
|
let ledger_path = get_tmp_ledger_path!();
|
|
|
|
{
|
|
|
|
let blockstore = Arc::new(
|
|
|
|
Blockstore::open(&ledger_path)
|
|
|
|
.expect("Expected to be able to open database ledger"),
|
|
|
|
);
|
|
|
|
let poh_config = PohConfig {
|
|
|
|
// limit tick count to avoid clearing working_bank at
|
|
|
|
// PohRecord then PohRecorderError(MaxHeightReached) at BankingStage
|
|
|
|
target_tick_count: Some(bank.max_tick_height() - 1),
|
|
|
|
..PohConfig::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let (exit, poh_recorder, poh_service, _entry_receiver) =
|
|
|
|
create_test_recorder(&bank, &blockstore, Some(poh_config));
|
|
|
|
|
|
|
|
let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
|
|
|
|
let data_budget = DataBudget::default();
|
|
|
|
BankingStage::handle_forwarding(
|
2021-10-07 02:38:23 -07:00
|
|
|
&ForwardOption::ForwardTransaction,
|
2021-09-21 08:49:41 -07:00
|
|
|
&cluster_info,
|
|
|
|
&mut unprocessed_packets,
|
|
|
|
&poh_recorder,
|
|
|
|
&socket,
|
|
|
|
false,
|
|
|
|
&data_budget,
|
|
|
|
);
|
|
|
|
exit.store(true, Ordering::Relaxed);
|
|
|
|
poh_service.join().unwrap();
|
|
|
|
}
|
|
|
|
Blockstore::destroy(&ledger_path).unwrap();
|
|
|
|
}
|
|
|
|
|
2021-02-12 03:27:37 -08:00
|
|
|
#[test]
|
|
|
|
fn test_push_unprocessed_batch_limit() {
|
2021-03-03 10:23:05 -08:00
|
|
|
solana_logger::setup();
|
2021-09-15 13:53:55 -07:00
|
|
|
// Create `Packets` with 2 unprocessed elements
|
|
|
|
let new_packets = Packets::new(vec![Packet::default(); 2]);
|
2021-02-12 03:27:37 -08:00
|
|
|
let mut unprocessed_packets: UnprocessedPackets =
|
2021-09-15 13:53:55 -07:00
|
|
|
vec![(new_packets, vec![0, 1], false)].into_iter().collect();
|
2021-02-12 03:27:37 -08:00
|
|
|
// Set the limit to 2
|
|
|
|
let batch_limit = 2;
|
|
|
|
// Create some new unprocessed packets
|
2021-09-15 13:53:55 -07:00
|
|
|
let new_packets = Packets::new(vec![Packet::default()]);
|
2021-02-12 03:27:37 -08:00
|
|
|
let packet_indexes = vec![];
|
|
|
|
|
2021-03-03 10:23:05 -08:00
|
|
|
let duplicates = Arc::new(Mutex::new((
|
|
|
|
LruCache::new(DEFAULT_LRU_SIZE),
|
|
|
|
PacketHasher::default(),
|
|
|
|
)));
|
2021-09-15 13:53:55 -07:00
|
|
|
let mut dropped_packet_batches_count = 0;
|
|
|
|
let mut dropped_packets_count = 0;
|
2021-02-12 03:27:37 -08:00
|
|
|
let mut newly_buffered_packets_count = 0;
|
2021-04-12 23:28:08 -07:00
|
|
|
let banking_stage_stats = BankingStageStats::default();
|
2021-02-12 03:27:37 -08:00
|
|
|
// Because the set of unprocessed `packet_indexes` is empty, the
|
|
|
|
// packets are not added to the unprocessed queue
|
|
|
|
BankingStage::push_unprocessed(
|
|
|
|
&mut unprocessed_packets,
|
|
|
|
new_packets.clone(),
|
|
|
|
packet_indexes,
|
2021-09-15 13:53:55 -07:00
|
|
|
&mut dropped_packet_batches_count,
|
|
|
|
&mut dropped_packets_count,
|
2021-02-12 03:27:37 -08:00
|
|
|
&mut newly_buffered_packets_count,
|
|
|
|
batch_limit,
|
2021-03-03 10:23:05 -08:00
|
|
|
&duplicates,
|
2021-04-12 23:28:08 -07:00
|
|
|
&banking_stage_stats,
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
|
|
|
assert_eq!(unprocessed_packets.len(), 1);
|
2021-09-15 13:53:55 -07:00
|
|
|
assert_eq!(dropped_packet_batches_count, 0);
|
|
|
|
assert_eq!(dropped_packets_count, 0);
|
2021-02-12 03:27:37 -08:00
|
|
|
assert_eq!(newly_buffered_packets_count, 0);
|
|
|
|
|
|
|
|
// Because the set of unprocessed `packet_indexes` is non-empty, the
|
|
|
|
// packets are added to the unprocessed queue
|
|
|
|
let packet_indexes = vec![0];
|
|
|
|
BankingStage::push_unprocessed(
|
|
|
|
&mut unprocessed_packets,
|
|
|
|
new_packets,
|
|
|
|
packet_indexes.clone(),
|
2021-09-15 13:53:55 -07:00
|
|
|
&mut dropped_packet_batches_count,
|
|
|
|
&mut dropped_packets_count,
|
2021-02-12 03:27:37 -08:00
|
|
|
&mut newly_buffered_packets_count,
|
|
|
|
batch_limit,
|
2021-03-03 10:23:05 -08:00
|
|
|
&duplicates,
|
2021-04-12 23:28:08 -07:00
|
|
|
&banking_stage_stats,
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
|
|
|
assert_eq!(unprocessed_packets.len(), 2);
|
2021-09-15 13:53:55 -07:00
|
|
|
assert_eq!(dropped_packet_batches_count, 0);
|
|
|
|
assert_eq!(dropped_packets_count, 0);
|
2021-02-12 03:27:37 -08:00
|
|
|
assert_eq!(newly_buffered_packets_count, 1);
|
|
|
|
|
|
|
|
// Because we've reached the batch limit, old unprocessed packets are
|
|
|
|
// dropped and the new one is appended to the end
|
|
|
|
let new_packets = Packets::new(vec![Packet::from_data(
|
2021-04-12 23:28:08 -07:00
|
|
|
Some(&SocketAddr::from(([127, 0, 0, 1], 8001))),
|
2021-02-12 03:27:37 -08:00
|
|
|
42,
|
|
|
|
)
|
|
|
|
.unwrap()]);
|
|
|
|
assert_eq!(unprocessed_packets.len(), batch_limit);
|
|
|
|
BankingStage::push_unprocessed(
|
|
|
|
&mut unprocessed_packets,
|
|
|
|
new_packets.clone(),
|
2021-03-03 10:23:05 -08:00
|
|
|
packet_indexes.clone(),
|
2021-09-15 13:53:55 -07:00
|
|
|
&mut dropped_packet_batches_count,
|
|
|
|
&mut dropped_packets_count,
|
2021-02-12 03:27:37 -08:00
|
|
|
&mut newly_buffered_packets_count,
|
|
|
|
batch_limit,
|
2021-03-03 10:23:05 -08:00
|
|
|
&duplicates,
|
2021-04-12 23:28:08 -07:00
|
|
|
&banking_stage_stats,
|
2021-03-03 10:23:05 -08:00
|
|
|
);
|
|
|
|
assert_eq!(unprocessed_packets.len(), 2);
|
|
|
|
assert_eq!(unprocessed_packets[1].0.packets[0], new_packets.packets[0]);
|
2021-09-15 13:53:55 -07:00
|
|
|
assert_eq!(dropped_packet_batches_count, 1);
|
|
|
|
assert_eq!(dropped_packets_count, 2);
|
2021-03-03 10:23:05 -08:00
|
|
|
assert_eq!(newly_buffered_packets_count, 2);
|
|
|
|
|
2021-09-15 13:53:55 -07:00
|
|
|
// Check duplicates are dropped (newly buffered shouldn't change)
|
2021-03-03 10:23:05 -08:00
|
|
|
BankingStage::push_unprocessed(
|
|
|
|
&mut unprocessed_packets,
|
|
|
|
new_packets.clone(),
|
|
|
|
packet_indexes,
|
2021-09-15 13:53:55 -07:00
|
|
|
&mut dropped_packet_batches_count,
|
|
|
|
&mut dropped_packets_count,
|
2021-03-03 10:23:05 -08:00
|
|
|
&mut newly_buffered_packets_count,
|
|
|
|
3,
|
|
|
|
&duplicates,
|
2021-04-12 23:28:08 -07:00
|
|
|
&banking_stage_stats,
|
2021-02-12 03:27:37 -08:00
|
|
|
);
|
|
|
|
assert_eq!(unprocessed_packets.len(), 2);
|
|
|
|
assert_eq!(unprocessed_packets[1].0.packets[0], new_packets.packets[0]);
|
2021-09-15 13:53:55 -07:00
|
|
|
assert_eq!(dropped_packet_batches_count, 1);
|
|
|
|
assert_eq!(dropped_packets_count, 2);
|
2021-02-12 03:27:37 -08:00
|
|
|
assert_eq!(newly_buffered_packets_count, 2);
|
|
|
|
}
|
2021-04-12 23:28:08 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_packet_message() {
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
|
|
|
let blockhash = Hash::new_unique();
|
|
|
|
let transaction = system_transaction::transfer(&keypair, &pubkey, 1, blockhash);
|
|
|
|
let packet = Packet::from_data(None, &transaction).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
BankingStage::packet_message(&packet).unwrap().to_vec(),
|
|
|
|
transaction.message_data()
|
|
|
|
);
|
|
|
|
}
|
2021-09-16 06:12:26 -07:00
|
|
|
|
2021-10-07 02:38:23 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
fn make_test_packets(
|
|
|
|
transactions: Vec<Transaction>,
|
|
|
|
vote_indexes: Vec<usize>,
|
|
|
|
) -> (Packets, Vec<usize>) {
|
|
|
|
let capacity = transactions.len();
|
|
|
|
let mut packets = Packets::with_capacity(capacity);
|
|
|
|
let mut packet_indexes = Vec::with_capacity(capacity);
|
|
|
|
packets.packets.resize(capacity, Packet::default());
|
|
|
|
for (index, tx) in transactions.iter().enumerate() {
|
|
|
|
Packet::populate_packet(&mut packets.packets[index], None, tx).ok();
|
|
|
|
packet_indexes.push(index);
|
|
|
|
}
|
|
|
|
for index in vote_indexes.iter() {
|
|
|
|
packets.packets[*index].meta.is_simple_vote_tx = true;
|
|
|
|
}
|
|
|
|
(packets, packet_indexes)
|
|
|
|
}
|
|
|
|
|
2021-09-16 06:12:26 -07:00
|
|
|
#[test]
|
|
|
|
fn test_transactions_from_packets() {
|
|
|
|
use solana_sdk::feature_set::FeatureSet;
|
2021-10-07 02:38:23 -07:00
|
|
|
let keypair = Keypair::new();
|
|
|
|
let transfer_tx =
|
|
|
|
system_transaction::transfer(&keypair, &keypair.pubkey(), 1, Hash::default());
|
|
|
|
let vote_tx = vote_transaction::new_vote_transaction(
|
|
|
|
vec![42],
|
2021-09-16 06:12:26 -07:00
|
|
|
Hash::default(),
|
2021-10-07 02:38:23 -07:00
|
|
|
Hash::default(),
|
|
|
|
&keypair,
|
|
|
|
&keypair,
|
|
|
|
&keypair,
|
|
|
|
None,
|
2021-09-16 06:12:26 -07:00
|
|
|
);
|
2021-10-07 02:38:23 -07:00
|
|
|
|
|
|
|
// packets with no votes
|
|
|
|
{
|
|
|
|
let vote_indexes = vec![];
|
|
|
|
let (packets, packet_indexes) =
|
|
|
|
make_test_packets(vec![transfer_tx.clone(), transfer_tx.clone()], vote_indexes);
|
|
|
|
|
|
|
|
let mut votes_only = false;
|
|
|
|
let (txs, tx_packet_index, _retryable_packet_indexes) =
|
|
|
|
BankingStage::transactions_from_packets(
|
|
|
|
&packets,
|
|
|
|
&packet_indexes,
|
|
|
|
&Arc::new(FeatureSet::default()),
|
2021-10-19 12:37:33 -07:00
|
|
|
&RwLock::new(CostTracker::default()).read().unwrap(),
|
2021-10-07 02:38:23 -07:00
|
|
|
&BankingStageStats::default(),
|
|
|
|
false,
|
|
|
|
votes_only,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-10-07 02:38:23 -07:00
|
|
|
);
|
|
|
|
assert_eq!(2, txs.len());
|
|
|
|
assert_eq!(vec![0, 1], tx_packet_index);
|
|
|
|
|
|
|
|
votes_only = true;
|
|
|
|
let (txs, tx_packet_index, _retryable_packet_indexes) =
|
|
|
|
BankingStage::transactions_from_packets(
|
|
|
|
&packets,
|
|
|
|
&packet_indexes,
|
|
|
|
&Arc::new(FeatureSet::default()),
|
2021-10-19 12:37:33 -07:00
|
|
|
&RwLock::new(CostTracker::default()).read().unwrap(),
|
2021-10-07 02:38:23 -07:00
|
|
|
&BankingStageStats::default(),
|
|
|
|
false,
|
|
|
|
votes_only,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-10-07 02:38:23 -07:00
|
|
|
);
|
|
|
|
assert_eq!(0, txs.len());
|
|
|
|
assert_eq!(0, tx_packet_index.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
// packets with some votes
|
|
|
|
{
|
|
|
|
let vote_indexes = vec![0, 2];
|
|
|
|
let (packets, packet_indexes) = make_test_packets(
|
|
|
|
vec![vote_tx.clone(), transfer_tx, vote_tx.clone()],
|
|
|
|
vote_indexes,
|
2021-09-16 06:12:26 -07:00
|
|
|
);
|
|
|
|
|
2021-10-07 02:38:23 -07:00
|
|
|
let mut votes_only = false;
|
|
|
|
let (txs, tx_packet_index, _retryable_packet_indexes) =
|
|
|
|
BankingStage::transactions_from_packets(
|
|
|
|
&packets,
|
|
|
|
&packet_indexes,
|
|
|
|
&Arc::new(FeatureSet::default()),
|
2021-10-19 12:37:33 -07:00
|
|
|
&RwLock::new(CostTracker::default()).read().unwrap(),
|
2021-10-07 02:38:23 -07:00
|
|
|
&BankingStageStats::default(),
|
|
|
|
false,
|
|
|
|
votes_only,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-10-07 02:38:23 -07:00
|
|
|
);
|
|
|
|
assert_eq!(3, txs.len());
|
|
|
|
assert_eq!(vec![0, 1, 2], tx_packet_index);
|
|
|
|
|
|
|
|
votes_only = true;
|
|
|
|
let (txs, tx_packet_index, _retryable_packet_indexes) =
|
|
|
|
BankingStage::transactions_from_packets(
|
|
|
|
&packets,
|
|
|
|
&packet_indexes,
|
|
|
|
&Arc::new(FeatureSet::default()),
|
2021-10-19 12:37:33 -07:00
|
|
|
&RwLock::new(CostTracker::default()).read().unwrap(),
|
2021-10-07 02:38:23 -07:00
|
|
|
&BankingStageStats::default(),
|
|
|
|
false,
|
|
|
|
votes_only,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-10-07 02:38:23 -07:00
|
|
|
);
|
|
|
|
assert_eq!(2, txs.len());
|
|
|
|
assert_eq!(vec![0, 2], tx_packet_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// packets with all votes
|
|
|
|
{
|
|
|
|
let vote_indexes = vec![0, 1, 2];
|
|
|
|
let (packets, packet_indexes) = make_test_packets(
|
|
|
|
vec![vote_tx.clone(), vote_tx.clone(), vote_tx],
|
|
|
|
vote_indexes,
|
2021-09-16 06:12:26 -07:00
|
|
|
);
|
2021-10-07 02:38:23 -07:00
|
|
|
|
|
|
|
let mut votes_only = false;
|
|
|
|
let (txs, tx_packet_index, _retryable_packet_indexes) =
|
|
|
|
BankingStage::transactions_from_packets(
|
|
|
|
&packets,
|
|
|
|
&packet_indexes,
|
|
|
|
&Arc::new(FeatureSet::default()),
|
2021-10-19 12:37:33 -07:00
|
|
|
&RwLock::new(CostTracker::default()).read().unwrap(),
|
2021-10-07 02:38:23 -07:00
|
|
|
&BankingStageStats::default(),
|
|
|
|
false,
|
|
|
|
votes_only,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-10-07 02:38:23 -07:00
|
|
|
);
|
|
|
|
assert_eq!(3, txs.len());
|
|
|
|
assert_eq!(vec![0, 1, 2], tx_packet_index);
|
|
|
|
|
|
|
|
votes_only = true;
|
|
|
|
let (txs, tx_packet_index, _retryable_packet_indexes) =
|
|
|
|
BankingStage::transactions_from_packets(
|
|
|
|
&packets,
|
|
|
|
&packet_indexes,
|
|
|
|
&Arc::new(FeatureSet::default()),
|
2021-10-19 12:37:33 -07:00
|
|
|
&RwLock::new(CostTracker::default()).read().unwrap(),
|
2021-10-07 02:38:23 -07:00
|
|
|
&BankingStageStats::default(),
|
|
|
|
false,
|
|
|
|
votes_only,
|
2021-10-19 12:37:33 -07:00
|
|
|
&Arc::new(RwLock::new(CostModel::default())),
|
2021-10-07 02:38:23 -07:00
|
|
|
);
|
|
|
|
assert_eq!(3, txs.len());
|
|
|
|
assert_eq!(vec![0, 1, 2], tx_packet_index);
|
|
|
|
}
|
2021-09-16 06:12:26 -07:00
|
|
|
}
|
2018-09-25 15:01:51 -07:00
|
|
|
}
|