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
use libp2p::PeerId;
use sled_extensions::{
bincode::{BincodeEncoding, Tree},
structured::Iter,
DbExt,
};
use state::Storage;
use std::collections::BTreeMap;
use std::sync::RwLock;
use super::CryptoState;
use crate::services::messaging::proto;
use crate::storage::database::DataBase;
pub static CRYPTOSTORAGE: Storage<RwLock<CryptoStorage>> = Storage::new();
#[derive(Clone)]
pub struct CryptoAccount {
pub state: Tree<CryptoState>,
pub cache: Tree<proto::Encrypted>,
}
impl CryptoAccount {
fn create_state_key(remote_id: PeerId, session_id: u32) -> Vec<u8> {
let mut remote_id_bytes = remote_id.to_bytes();
let mut session_id_bytes = session_id.to_be_bytes().to_vec();
remote_id_bytes.append(&mut session_id_bytes);
remote_id_bytes
}
fn create_state_key_range(remote_id: PeerId) -> (Vec<u8>, Vec<u8>) {
let first_key = Self::create_state_key(remote_id, 0);
let last_key = Self::create_state_key(remote_id, u32::MAX);
(first_key, last_key)
}
fn create_cache_key(remote_id: PeerId, session_id: u32, nonce: u64) -> Vec<u8> {
let mut nonce_bytes = nonce.to_be_bytes().to_vec();
let mut session_key = Self::create_state_key(remote_id, session_id);
session_key.append(&mut nonce_bytes);
session_key
}
#[allow(dead_code)]
fn create_cache_key_range(remote_id: PeerId, session_id: u32) -> (Vec<u8>, Vec<u8>) {
let first_key = Self::create_cache_key(remote_id, session_id, 0);
let last_key = Self::create_cache_key(remote_id, session_id, u64::MAX);
(first_key, last_key)
}
pub fn get_state(&self, remote_id: PeerId) -> Option<CryptoState> {
let (first_key, last_key) = Self::create_state_key_range(remote_id);
let mut state_option: Option<CryptoState> = None;
let iterator = self.state.range(first_key..last_key);
for result in iterator {
match result {
Ok((_key, session)) => match session.state {
super::CryptoProcessState::HalfOutgoing => state_option = Some(session),
super::CryptoProcessState::HalfIncoming => return Some(session),
super::CryptoProcessState::Transport => return Some(session),
},
Err(e) => log::error!("{}", e),
}
}
state_option
}
pub fn get_state_by_id(&self, remote_id: PeerId, session_id: u32) -> Option<CryptoState> {
let key = Self::create_state_key(remote_id, session_id);
match self.state.get(key) {
Ok(state_option) => {
return state_option;
}
Err(e) => log::error!("{}", e),
}
None
}
pub fn save_state(&self, remote_id: PeerId, session_id: u32, crypto_state: CryptoState) {
let key = Self::create_state_key(remote_id, session_id);
if let Err(e) = self.state.insert(key, crypto_state) {
log::error!("Error handshake to db: {}", e);
}
if let Err(e) = self.state.flush() {
log::error!("Error db flush: {}", e);
}
}
pub fn save_cache_message(
&self,
remote_id: PeerId,
session_id: u32,
nonce: u64,
message: proto::Encrypted,
) {
let key = Self::create_cache_key(remote_id, session_id, nonce);
if let Err(e) = self.cache.insert(key, message) {
log::error!("Error handshake to db: {}", e);
}
if let Err(e) = self.cache.flush() {
log::error!("Error db flush: {}", e);
}
}
#[allow(dead_code)]
pub fn get_cache_messages(
&self,
remote_id: PeerId,
session_id: u32,
) -> Iter<proto::Encrypted, BincodeEncoding> {
let (first_key, last_key) = Self::create_cache_key_range(remote_id, session_id);
let result = self.cache.range(first_key..last_key);
result
}
}
pub struct CryptoStorage {
db_ref: BTreeMap<Vec<u8>, CryptoAccount>,
}
impl CryptoStorage {
pub fn init() {
let crypto_storage = CryptoStorage {
db_ref: BTreeMap::new(),
};
CRYPTOSTORAGE.set(RwLock::new(crypto_storage));
}
pub fn get_db_ref(account_id: PeerId) -> CryptoAccount {
{
let crypto_storage = CRYPTOSTORAGE.get().read().unwrap();
if let Some(crypto_account_db) = crypto_storage.db_ref.get(&account_id.to_bytes()) {
return CryptoAccount {
state: crypto_account_db.state.clone(),
cache: crypto_account_db.cache.clone(),
};
}
}
let crypto_account = Self::create_groupaccountdb(account_id);
crypto_account.clone()
}
fn create_groupaccountdb(account_id: PeerId) -> CryptoAccount {
let db = DataBase::get_user_db(account_id);
let state: Tree<CryptoState> = db.open_bincode_tree("crypto_state").unwrap();
let cache: Tree<proto::Encrypted> = db.open_bincode_tree("crypto_cache").unwrap();
let crypto_account = CryptoAccount { state, cache };
let mut crypto_storage = CRYPTOSTORAGE.get().write().unwrap();
crypto_storage
.db_ref
.insert(account_id.to_bytes(), crypto_account.clone());
crypto_account
}
}