solana_accountsdb_plugin_postgres/
accounts_selector.rs

1use {log::*, std::collections::HashSet};
2
3#[derive(Debug)]
4pub(crate) struct AccountsSelector {
5    pub accounts: HashSet<Vec<u8>>,
6    pub owners: HashSet<Vec<u8>>,
7    pub select_all_accounts: bool,
8}
9
10impl AccountsSelector {
11    pub fn default() -> Self {
12        AccountsSelector {
13            accounts: HashSet::default(),
14            owners: HashSet::default(),
15            select_all_accounts: true,
16        }
17    }
18
19    pub fn new(accounts: &[String], owners: &[String]) -> Self {
20        info!(
21            "Creating AccountsSelector from accounts: {:?}, owners: {:?}",
22            accounts, owners
23        );
24
25        let select_all_accounts = accounts.iter().any(|key| key == "*");
26        if select_all_accounts {
27            return AccountsSelector {
28                accounts: HashSet::default(),
29                owners: HashSet::default(),
30                select_all_accounts,
31            };
32        }
33        let accounts = accounts
34            .iter()
35            .map(|key| bs58::decode(key).into_vec().unwrap())
36            .collect();
37        let owners = owners
38            .iter()
39            .map(|key| bs58::decode(key).into_vec().unwrap())
40            .collect();
41        AccountsSelector {
42            accounts,
43            owners,
44            select_all_accounts,
45        }
46    }
47
48    pub fn is_account_selected(&self, account: &[u8], owner: &[u8]) -> bool {
49        self.select_all_accounts || self.accounts.contains(account) || self.owners.contains(owner)
50    }
51
52    /// Check if any account is of interested at all
53    pub fn is_enabled(&self) -> bool {
54        self.select_all_accounts || !self.accounts.is_empty() || !self.owners.is_empty()
55    }
56}
57
58#[cfg(test)]
59pub(crate) mod tests {
60    use super::*;
61
62    #[test]
63    fn test_create_accounts_selector() {
64        AccountsSelector::new(
65            &["9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin".to_string()],
66            &[],
67        );
68
69        AccountsSelector::new(
70            &[],
71            &["9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin".to_string()],
72        );
73    }
74}