-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add blocklist struct #325
Open
ferranbt
wants to merge
6
commits into
develop
Choose a base branch
from
ferranbt/blocklist-struct
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add blocklist struct #325
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,148 @@ | ||||||||||||||
use ahash::{HashSet, HashSetExt}; | ||||||||||||||
use alloy_primitives::Address; | ||||||||||||||
use serde::{Deserialize, Deserializer}; | ||||||||||||||
use std::fs::read_to_string; | ||||||||||||||
use std::path::PathBuf; | ||||||||||||||
|
||||||||||||||
#[allow(clippy::len_without_is_empty)] | ||||||||||||||
#[derive(Debug, Clone, PartialEq, Eq, Default)] | ||||||||||||||
pub struct BlockList { | ||||||||||||||
list: HashSet<Address>, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
impl BlockList { | ||||||||||||||
fn new() -> Self { | ||||||||||||||
Self { | ||||||||||||||
list: HashSet::new(), | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fn from_file(path: PathBuf) -> eyre::Result<Self> { | ||||||||||||||
let blocklist_file = read_to_string(path)?; | ||||||||||||||
let blocklist: Vec<Address> = serde_json::from_str(&blocklist_file)?; | ||||||||||||||
|
||||||||||||||
Ok(Self { | ||||||||||||||
list: blocklist.into_iter().collect(), | ||||||||||||||
}) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
pub fn len(&self) -> usize { | ||||||||||||||
self.list.len() | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
pub fn contains(&self, address: &Address) -> bool { | ||||||||||||||
self.list.contains(address) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg(test)] | ||||||||||||||
fn add(&mut self, address: Address) { | ||||||||||||||
self.list.insert(address); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
impl From<PathBuf> for BlockList { | ||||||||||||||
fn from(path: PathBuf) -> Self { | ||||||||||||||
Self::from_file(path).unwrap_or_else(|_| Self::new()) | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect this type of method to return an error if the path doesn't exist instead of an empty list.
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
impl From<Vec<Address>> for BlockList { | ||||||||||||||
fn from(addresses: Vec<Address>) -> Self { | ||||||||||||||
Self { | ||||||||||||||
list: addresses.into_iter().collect(), | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
impl<'de> Deserialize<'de> for BlockList { | ||||||||||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||||||||||||||
where | ||||||||||||||
D: Deserializer<'de>, | ||||||||||||||
{ | ||||||||||||||
let path: Option<PathBuf> = Option::deserialize(deserializer)?; | ||||||||||||||
|
||||||||||||||
match path { | ||||||||||||||
Some(path) => BlockList::from_file(path).map_err(serde::de::Error::custom), | ||||||||||||||
None => Ok(BlockList::new()), | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg(test)] | ||||||||||||||
mod test { | ||||||||||||||
use super::*; | ||||||||||||||
use alloy_primitives::{address, Address}; | ||||||||||||||
use serde::Deserialize; | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn test_read_blocklist_from_file() { | ||||||||||||||
let block_list = BlockList::from_file( | ||||||||||||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/blocklist/testdata/blocklist.txt"), | ||||||||||||||
) | ||||||||||||||
.unwrap(); | ||||||||||||||
|
||||||||||||||
let addr0 = address!("14dC79964da2C08b23698B3D3cc7Ca32193d9955"); | ||||||||||||||
assert_eq!(block_list.contains(&addr0), true); | ||||||||||||||
|
||||||||||||||
let addr1 = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); | ||||||||||||||
assert_eq!(block_list.contains(&addr1), true); | ||||||||||||||
|
||||||||||||||
let addr2 = address!("a0Ee7A142d267C1f36714E4a8F75612F20a79720"); | ||||||||||||||
assert_eq!(block_list.contains(&addr2), false); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn test_blocklist() { | ||||||||||||||
let mut blocklist = BlockList::new(); | ||||||||||||||
let addr0 = Address::random(); | ||||||||||||||
|
||||||||||||||
blocklist.add(addr0); | ||||||||||||||
assert_eq!(blocklist.len(), 1); | ||||||||||||||
assert_eq!(blocklist.contains(&addr0), true); | ||||||||||||||
|
||||||||||||||
// you cannot add twice the same value | ||||||||||||||
blocklist.add(addr0); | ||||||||||||||
assert_eq!(blocklist.len(), 1); | ||||||||||||||
|
||||||||||||||
let addr1 = Address::random(); | ||||||||||||||
assert_eq!(blocklist.contains(&addr1), false); | ||||||||||||||
|
||||||||||||||
blocklist.add(addr1); | ||||||||||||||
assert_eq!(blocklist.len(), 2); | ||||||||||||||
assert_eq!(blocklist.contains(&addr1), true); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[derive(Deserialize)] | ||||||||||||||
struct Config { | ||||||||||||||
block_list: BlockList, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn test_deserialize_config() { | ||||||||||||||
let config_str = r#" | ||||||||||||||
block_list = "src/blocklist/testdata/blocklist.txt" | ||||||||||||||
"#; | ||||||||||||||
let config: Config = toml::from_str(config_str).unwrap(); | ||||||||||||||
assert_eq!(config.block_list.len(), 3); | ||||||||||||||
|
||||||||||||||
let addr1 = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); | ||||||||||||||
assert_eq!(config.block_list.contains(&addr1), true); | ||||||||||||||
|
||||||||||||||
let empty_config_str = r#""#; | ||||||||||||||
let config: Config = toml::from_str(empty_config_str).unwrap(); | ||||||||||||||
assert_eq!(config.block_list.len(), 0); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn test_from_vec() { | ||||||||||||||
let addr0 = address!("14dC79964da2C08b23698B3D3cc7Ca32193d9955"); | ||||||||||||||
let addr1 = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); | ||||||||||||||
|
||||||||||||||
let addresses = vec![addr0, addr1]; | ||||||||||||||
let blocklist = BlockList::from(addresses); | ||||||||||||||
|
||||||||||||||
assert_eq!(blocklist.len(), 2); | ||||||||||||||
assert_eq!(blocklist.contains(&addr0), true); | ||||||||||||||
assert_eq!(blocklist.contains(&addr1), true); | ||||||||||||||
} | ||||||||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
"0x14dC79964da2C08b23698B3D3cc7Ca32193d9955", | ||
"0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC", | ||
"f39Fd6e51aad88F6F4ce6aB8827279cffFb92266" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to impl
instead of the inner methods individually
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added Deref but not sure what changes I have to make to the len and contains functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove them now :)