-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcompleted_data_sets_service.rs
More file actions
295 lines (266 loc) · 11.6 KB
/
completed_data_sets_service.rs
File metadata and controls
295 lines (266 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! [`CompletedDataSetsService`] is a hub, that runs different operations when a "completed data
//! set", also known as a [`Vec<Entry>`], is received by the validator.
//!
//! Currently, `WindowService` sends [`CompletedDataSetInfo`]s via a `completed_sets_receiver`
//! provided to the [`CompletedDataSetsService`].
use {
crossbeam_channel::{Receiver, RecvTimeoutError, Sender},
solana_entry::entry::Entry,
solana_ledger::{
blockstore::{Blockstore, CompletedDataSetInfo},
deshred_transaction_notifier_interface::DeshredTransactionNotifierArc,
},
solana_measure::measure::Measure,
solana_message::{v0::LoadedAddresses, VersionedMessage},
solana_metrics::*,
solana_rpc::{max_slots::MaxSlots, rpc_subscriptions::RpcSubscriptions},
solana_runtime::bank_forks::BankForks,
solana_signature::Signature,
solana_svm_transaction::message_address_table_lookup::SVMMessageAddressTableLookup,
solana_transaction::{
simple_vote_transaction_checker::is_simple_vote_transaction_impl,
versioned::VersionedTransaction,
},
std::{
sync::{
atomic::{AtomicBool, Ordering},
Arc, RwLock,
},
thread::{self, Builder, JoinHandle},
time::Duration,
},
};
pub type CompletedDataSetsReceiver = Receiver<Vec<CompletedDataSetInfo>>;
pub type CompletedDataSetsSender = Sender<Vec<CompletedDataSetInfo>>;
/// Check if a versioned transaction is a simple vote transaction.
/// This avoids cloning by extracting the required data directly.
fn is_simple_vote_transaction(tx: &VersionedTransaction) -> bool {
let is_legacy = matches!(&tx.message, VersionedMessage::Legacy(_));
let (account_keys, instructions) = match &tx.message {
VersionedMessage::Legacy(msg) => (&msg.account_keys[..], &msg.instructions[..]),
VersionedMessage::V0(msg) => (&msg.account_keys[..], &msg.instructions[..]),
};
let instruction_programs = instructions
.iter()
.filter_map(|ix| account_keys.get(ix.program_id_index as usize));
is_simple_vote_transaction_impl(&tx.signatures, is_legacy, instruction_programs)
}
/// Load addresses from address lookup tables for a versioned transaction.
/// Returns None for legacy transactions or if address resolution fails.
/// Takes a Bank reference to avoid repeated lock acquisition.
fn load_transaction_addresses(
tx: &VersionedTransaction,
bank: &solana_runtime::bank::Bank,
) -> Option<LoadedAddresses> {
let VersionedMessage::V0(message) = &tx.message else {
// Legacy transactions don't have address table lookups
return None;
};
if message.address_table_lookups.is_empty() {
return None;
}
bank.load_addresses_from_ref(
message
.address_table_lookups
.iter()
.map(SVMMessageAddressTableLookup::from),
)
.ok()
.map(|(addresses, _deactivation_slot)| addresses)
}
pub struct CompletedDataSetsService {
thread_hdl: JoinHandle<()>,
}
impl CompletedDataSetsService {
pub fn new(
completed_sets_receiver: CompletedDataSetsReceiver,
blockstore: Arc<Blockstore>,
rpc_subscriptions: Arc<RpcSubscriptions>,
deshred_transaction_notifier: Option<DeshredTransactionNotifierArc>,
exit: Arc<AtomicBool>,
max_slots: Arc<MaxSlots>,
bank_forks: Arc<RwLock<BankForks>>,
) -> Self {
let thread_hdl = Builder::new()
.name("solComplDataSet".to_string())
.spawn(move || {
info!("CompletedDataSetsService has started");
loop {
if exit.load(Ordering::Relaxed) {
break;
}
if let Err(RecvTimeoutError::Disconnected) = Self::recv_completed_data_sets(
&completed_sets_receiver,
&blockstore,
&rpc_subscriptions,
&deshred_transaction_notifier,
&max_slots,
&bank_forks,
) {
break;
}
}
info!("CompletedDataSetsService has stopped");
})
.unwrap();
Self { thread_hdl }
}
fn recv_completed_data_sets(
completed_sets_receiver: &CompletedDataSetsReceiver,
blockstore: &Blockstore,
rpc_subscriptions: &RpcSubscriptions,
deshred_transaction_notifier: &Option<DeshredTransactionNotifierArc>,
max_slots: &Arc<MaxSlots>,
bank_forks: &RwLock<BankForks>,
) -> Result<(), RecvTimeoutError> {
const RECV_TIMEOUT: Duration = Duration::from_secs(1);
let mut batch_measure = Measure::start("deshred_geyser_batch");
// Get root bank once per batch to minimize lock contention
let root_bank = deshred_transaction_notifier
.as_ref()
.and_then(|_| bank_forks.read().ok())
.map(|forks| forks.root_bank());
// Metrics accumulators
let mut total_lut_load_us: u64 = 0;
let mut total_notify_us: u64 = 0;
let mut total_transactions: u64 = 0;
let mut total_entries: u64 = 0;
let mut total_data_sets: u64 = 0;
let mut lut_transactions: u64 = 0;
let handle_completed_data_set_info = |completed_data_set_info: CompletedDataSetInfo,
total_lut_load_us: &mut u64,
total_notify_us: &mut u64,
total_transactions: &mut u64,
total_entries: &mut u64,
total_data_sets: &mut u64,
lut_transactions: &mut u64| {
let CompletedDataSetInfo { slot, indices } = completed_data_set_info;
match blockstore.get_entries_in_data_block(slot, indices, /*slot_meta:*/ None) {
Ok(entries) => {
*total_data_sets += 1;
*total_entries += entries.len() as u64;
// Notify deshred transactions if notifier is enabled
if let Some(notifier) = deshred_transaction_notifier {
for entry in entries.iter() {
for tx in &entry.transactions {
if let Some(signature) = tx.signatures.first() {
*total_transactions += 1;
let is_vote = is_simple_vote_transaction(tx);
// Measure LUT loading time
let mut lut_measure = Measure::start("load_lut");
let loaded_addresses = root_bank
.as_ref()
.and_then(|bank| load_transaction_addresses(tx, bank));
lut_measure.stop();
if loaded_addresses.is_some() {
*lut_transactions += 1;
*total_lut_load_us += lut_measure.as_us();
}
// Measure notification time
let mut notify_measure = Measure::start("notify_deshred");
notifier.notify_deshred_transaction(
slot,
signature,
is_vote,
tx,
loaded_addresses.as_ref(),
);
notify_measure.stop();
*total_notify_us += notify_measure.as_us();
}
}
}
}
// Existing: notify signatures for RPC subscriptions
let transactions = Self::get_transaction_signatures(entries);
if !transactions.is_empty() {
rpc_subscriptions.notify_signatures_received((slot, transactions));
}
}
Err(e) => warn!("completed-data-set-service deserialize error: {e:?}"),
}
slot
};
let slots = completed_sets_receiver
.recv_timeout(RECV_TIMEOUT)
.map(std::iter::once)?
.chain(completed_sets_receiver.try_iter())
.flatten()
.map(|info| {
handle_completed_data_set_info(
info,
&mut total_lut_load_us,
&mut total_notify_us,
&mut total_transactions,
&mut total_entries,
&mut total_data_sets,
&mut lut_transactions,
)
});
if let Some(slot) = slots.max() {
max_slots.shred_insert.fetch_max(slot, Ordering::Relaxed);
}
batch_measure.stop();
// Report metrics if we processed any transactions
if total_transactions > 0 {
datapoint_info!(
"deshred_geyser_timing",
("batch_total_us", batch_measure.as_us() as i64, i64),
("notify_total_us", total_notify_us as i64, i64),
("lut_load_total_us", total_lut_load_us as i64, i64),
("transactions_count", total_transactions as i64, i64),
("lut_transactions_count", lut_transactions as i64, i64),
("entries_count", total_entries as i64, i64),
("data_sets_count", total_data_sets as i64, i64),
(
"avg_notify_us",
(total_notify_us / total_transactions) as i64,
i64
),
);
}
Ok(())
}
fn get_transaction_signatures(entries: Vec<Entry>) -> Vec<Signature> {
entries
.into_iter()
.flat_map(|e| {
e.transactions
.into_iter()
.filter_map(|mut t| t.signatures.drain(..).next())
})
.collect::<Vec<Signature>>()
}
pub fn join(self) -> thread::Result<()> {
self.thread_hdl.join()
}
}
#[cfg(test)]
pub mod test {
use {
super::*, solana_hash::Hash, solana_keypair::Keypair, solana_signer::Signer,
solana_transaction::Transaction,
};
#[test]
fn test_zero_signatures() {
let tx = Transaction::new_with_payer(&[], None);
let entries = vec![Entry::new(&Hash::default(), 1, vec![tx])];
let signatures = CompletedDataSetsService::get_transaction_signatures(entries);
assert!(signatures.is_empty());
}
#[test]
fn test_multi_signatures() {
let kp = Keypair::new();
let tx =
Transaction::new_signed_with_payer(&[], Some(&kp.pubkey()), &[&kp], Hash::default());
let entries = vec![Entry::new(&Hash::default(), 1, vec![tx.clone()])];
let signatures = CompletedDataSetsService::get_transaction_signatures(entries);
assert_eq!(signatures.len(), 1);
let entries = vec![
Entry::new(&Hash::default(), 1, vec![tx.clone(), tx.clone()]),
Entry::new(&Hash::default(), 1, vec![tx]),
];
let signatures = CompletedDataSetsService::get_transaction_signatures(entries);
assert_eq!(signatures.len(), 3);
}
}