|
| 1 | +// Copyright 2022 Blockdaemon Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use { |
| 16 | + crate::*, |
| 17 | + solana_program::pubkey::Pubkey, |
| 18 | + std::{collections::HashSet, str::FromStr}, |
| 19 | +}; |
| 20 | + |
| 21 | +pub struct Filter { |
| 22 | + program_ignores: HashSet<[u8; 32]>, |
| 23 | +} |
| 24 | + |
| 25 | +impl Filter { |
| 26 | + pub fn new(config: &Config) -> Self { |
| 27 | + Self { |
| 28 | + program_ignores: config |
| 29 | + .program_ignores |
| 30 | + .iter() |
| 31 | + .flat_map(|p| Pubkey::from_str(p).ok().map(|p| p.to_bytes())) |
| 32 | + .collect(), |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + pub fn wants_program(&self, program: &[u8]) -> bool { |
| 37 | + let key = match <&[u8; 32]>::try_from(program) { |
| 38 | + Ok(key) => key, |
| 39 | + _ => return true, |
| 40 | + }; |
| 41 | + !self.program_ignores.contains(key) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[cfg(test)] |
| 46 | +mod tests { |
| 47 | + use super::*; |
| 48 | + |
| 49 | + #[test] |
| 50 | + fn test_filter() { |
| 51 | + let mut config = Config::default(); |
| 52 | + config.program_ignores = vec![ |
| 53 | + "Sysvar1111111111111111111111111111111111111".to_owned(), |
| 54 | + "Vote111111111111111111111111111111111111111".to_owned(), |
| 55 | + ]; |
| 56 | + |
| 57 | + let filter = Filter::new(&config); |
| 58 | + assert_eq!(filter.program_ignores.len(), 2); |
| 59 | + |
| 60 | + assert!(filter.wants_program( |
| 61 | + &Pubkey::from_str("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin") |
| 62 | + .unwrap() |
| 63 | + .to_bytes() |
| 64 | + )); |
| 65 | + assert!(!filter.wants_program( |
| 66 | + &Pubkey::from_str("Vote111111111111111111111111111111111111111") |
| 67 | + .unwrap() |
| 68 | + .to_bytes() |
| 69 | + )); |
| 70 | + } |
| 71 | +} |
0 commit comments