use crate::{message::MsgId, sync::message::msgid};
use cfx_types::H256;
use malloc_size_of_derive::MallocSizeOf as DeriveMallocSizeOf;
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::collections::HashSet;
#[derive(Hash, Eq, PartialEq, Debug, DeriveMallocSizeOf)]
pub enum Key {
Hash(H256),
Num(u64),
Id(u32),
}
#[derive(DeriveMallocSizeOf)]
pub struct KeyContainer {
keys: Vec<Option<RwLock<HashSet<Key>>>>,
}
impl Default for KeyContainer {
fn default() -> Self {
let mut keys: Vec<Option<RwLock<HashSet<Key>>>> = Default::default();
for _ in 0..256 {
keys.push(None);
}
keys[msgid::GET_BLOCK_HASHES_BY_EPOCH as usize] =
Some(Default::default());
keys[msgid::GET_BLOCK_HEADERS as usize] = Some(Default::default());
keys[msgid::GET_BLOCKS as usize] = Some(Default::default());
keys[msgid::GET_TRANSACTIONS as usize] = Some(Default::default());
keys[msgid::GET_TRANSACTIONS_FROM_TX_HASHES as usize] =
Some(Default::default());
keys[msgid::NET_INFLIGHT_BLOCKS as usize] = Some(Default::default());
KeyContainer { keys }
}
}
impl KeyContainer {
pub fn read(&self, msg_type: MsgId) -> RwLockReadGuard<HashSet<Key>> {
self.keys[msg_type as usize]
.as_ref()
.expect("msg not supported")
.read()
}
pub fn write(&self, msg_type: MsgId) -> RwLockWriteGuard<HashSet<Key>> {
self.keys[msg_type as usize]
.as_ref()
.expect("msg not supported")
.write()
}
pub fn add(&mut self, msg_type: MsgId, key: Key) -> bool {
self.write(msg_type).insert(key)
}
pub fn remove(&mut self, msg_type: MsgId, key: Key) -> bool {
self.write(msg_type).remove(&key)
}
}