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
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, Sender};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use crate::grin_keychain::Keychain;
use crate::grin_util::secp::key::SecretKey;
use crate::grin_util::Mutex;
use crate::api_impl::owner;
use crate::types::NodeClient;
use crate::Error;
use crate::{WalletInst, WalletLCProvider};
const MESSAGE_QUEUE_MAX_LEN: usize = 10_000;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum StatusMessage {
UpdatingOutputs(String),
UpdatingTransactions(String),
FullScanWarn(String),
Scanning(String, u8),
ScanningComplete(String),
UpdateWarning(String),
}
pub fn start_updater_log_thread(
rx: Receiver<StatusMessage>,
queue: Arc<Mutex<Vec<StatusMessage>>>,
) -> Result<(), Error> {
let _ = thread::Builder::new()
.name("wallet-updater-status".to_string())
.spawn(move || {
while let Ok(m) = rx.recv() {
{
let mut q = queue.lock();
q.insert(0, m.clone());
while q.len() > MESSAGE_QUEUE_MAX_LEN {
q.pop();
}
}
match m {
StatusMessage::UpdatingOutputs(s) => debug!("{}", s),
StatusMessage::UpdatingTransactions(s) => debug!("{}", s),
StatusMessage::FullScanWarn(s) => warn!("{}", s),
StatusMessage::Scanning(s, m) => {
debug!("{}", s);
warn!("Scanning - {}% complete", m);
}
StatusMessage::ScanningComplete(s) => warn!("{}", s),
StatusMessage::UpdateWarning(s) => warn!("{}", s),
}
}
})?;
Ok(())
}
pub struct Updater<'a, L, C, K>
where
L: WalletLCProvider<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
is_running: Arc<AtomicBool>,
}
impl<'a, L, C, K> Updater<'a, L, C, K>
where
L: WalletLCProvider<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
pub fn new(
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
is_running: Arc<AtomicBool>,
) -> Self {
is_running.store(false, Ordering::Relaxed);
Updater {
wallet_inst,
is_running,
}
}
pub fn run(
&self,
frequency: Duration,
keychain_mask: Option<SecretKey>,
status_send_channel: &Option<Sender<StatusMessage>>,
) -> Result<(), Error> {
self.is_running.store(true, Ordering::Relaxed);
loop {
let wallet_opened = {
let mut w_lock = self.wallet_inst.lock();
let w_provider = w_lock.lc_provider()?;
w_provider.wallet_inst().is_ok()
};
if wallet_opened {
owner::update_wallet_state(
self.wallet_inst.clone(),
(&keychain_mask).as_ref(),
status_send_channel,
false,
)?;
}
if !self.is_running.load(Ordering::Relaxed) {
break;
}
thread::sleep(frequency);
}
Ok(())
}
}