-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathstaked_nodes_updater_service.rs
50 lines (45 loc) · 1.47 KB
/
staked_nodes_updater_service.rs
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
use {
solana_runtime::bank_forks::BankForks,
solana_sdk::pubkey::Pubkey,
solana_streamer::streamer::StakedNodes,
std::{
collections::HashMap,
sync::{
atomic::{AtomicBool, Ordering},
Arc, RwLock,
},
thread::{self, Builder, JoinHandle},
time::Duration,
},
};
const STAKE_REFRESH_CYCLE: Duration = Duration::from_secs(5);
pub struct StakedNodesUpdaterService {
thread_hdl: JoinHandle<()>,
}
impl StakedNodesUpdaterService {
pub fn new(
exit: Arc<AtomicBool>,
bank_forks: Arc<RwLock<BankForks>>,
staked_nodes: Arc<RwLock<StakedNodes>>,
staked_nodes_overrides: Arc<RwLock<HashMap<Pubkey, u64>>>,
) -> Self {
let thread_hdl = Builder::new()
.name("solStakedNodeUd".to_string())
.spawn(move || {
while !exit.load(Ordering::Relaxed) {
let stakes = {
let root_bank = bank_forks.read().unwrap().root_bank();
root_bank.staked_nodes()
};
let overrides = staked_nodes_overrides.read().unwrap().clone();
*staked_nodes.write().unwrap() = StakedNodes::new(stakes, overrides);
std::thread::sleep(STAKE_REFRESH_CYCLE);
}
})
.unwrap();
Self { thread_hdl }
}
pub fn join(self) -> thread::Result<()> {
self.thread_hdl.join()
}
}