2021-09-30 14:26:17 -07:00
|
|
|
use {log::*, std::collections::HashSet};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct AccountsSelector {
|
|
|
|
pub accounts: HashSet<Vec<u8>>,
|
|
|
|
pub owners: HashSet<Vec<u8>>,
|
|
|
|
pub select_all_accounts: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountsSelector {
|
|
|
|
pub fn default() -> Self {
|
|
|
|
AccountsSelector {
|
|
|
|
accounts: HashSet::default(),
|
|
|
|
owners: HashSet::default(),
|
|
|
|
select_all_accounts: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(accounts: &[String], owners: &[String]) -> Self {
|
|
|
|
info!(
|
|
|
|
"Creating AccountsSelector from accounts: {:?}, owners: {:?}",
|
|
|
|
accounts, owners
|
|
|
|
);
|
|
|
|
|
|
|
|
let select_all_accounts = accounts.iter().any(|key| key == "*");
|
|
|
|
if select_all_accounts {
|
|
|
|
return AccountsSelector {
|
|
|
|
accounts: HashSet::default(),
|
|
|
|
owners: HashSet::default(),
|
|
|
|
select_all_accounts,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let accounts = accounts
|
|
|
|
.iter()
|
|
|
|
.map(|key| bs58::decode(key).into_vec().unwrap())
|
|
|
|
.collect();
|
|
|
|
let owners = owners
|
|
|
|
.iter()
|
|
|
|
.map(|key| bs58::decode(key).into_vec().unwrap())
|
|
|
|
.collect();
|
|
|
|
AccountsSelector {
|
|
|
|
accounts,
|
|
|
|
owners,
|
|
|
|
select_all_accounts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_account_selected(&self, account: &[u8], owner: &[u8]) -> bool {
|
|
|
|
self.select_all_accounts || self.accounts.contains(account) || self.owners.contains(owner)
|
|
|
|
}
|
2021-11-17 17:11:38 -08:00
|
|
|
|
|
|
|
/// Check if any account is of interested at all
|
|
|
|
pub fn is_enabled(&self) -> bool {
|
|
|
|
self.select_all_accounts || !self.accounts.is_empty() || !self.owners.is_empty()
|
|
|
|
}
|
2021-09-30 14:26:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_accounts_selector() {
|
|
|
|
AccountsSelector::new(
|
|
|
|
&["9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin".to_string()],
|
|
|
|
&[],
|
|
|
|
);
|
|
|
|
|
|
|
|
AccountsSelector::new(
|
|
|
|
&[],
|
|
|
|
&["9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin".to_string()],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|