Skip to content

Commit

Permalink
feat(sources): add mock source
Browse files Browse the repository at this point in the history
  • Loading branch information
mempirate committed Jan 9, 2025
1 parent 622e14a commit c85c435
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

pub(crate) mod registry;

#[derive(Debug, Clone, Serialize, Deserialize, Deref, DerefMut, From)]
#[derive(Debug, Clone, Serialize, Deserialize, Deref, DerefMut, From, PartialEq, Eq, Hash)]
pub(crate) struct BlsPublicKey(bls::PublicKey);

impl BlsPublicKey {
Expand Down
33 changes: 33 additions & 0 deletions src/sources/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::collections::HashMap;

use crate::primitives::{registry::RegistryEntry, BlsPublicKey};

use super::{ExternalSource, SourceError};

pub(crate) struct MockSource {
pub(crate) entries: HashMap<BlsPublicKey, RegistryEntry>,
}

impl MockSource {
pub(crate) fn new() -> Self {
Self { entries: HashMap::new() }
}

pub(crate) fn add_entry(&mut self, entry: RegistryEntry) {
self.entries.insert(entry.validator_pubkey.clone(), entry);
}
}

#[async_trait::async_trait]
impl ExternalSource for MockSource {
fn name(&self) -> &'static str {
"mock"
}

async fn get_validators(
&self,
pubkeys: &[BlsPublicKey],
) -> Result<Vec<RegistryEntry>, SourceError> {
Ok(pubkeys.iter().filter_map(|pubkey| self.entries.get(pubkey).cloned()).collect())
}
}
4 changes: 4 additions & 0 deletions src/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use crate::primitives::{registry::RegistryEntry, BlsPublicKey};
/// <https://github.com/lidofinance/lido-keys-api/tree/develop>
pub(crate) mod kapi;

/// Mock external source for testing.
#[cfg(test)]
pub(crate) mod mock;

#[derive(Debug, Error)]
pub(crate) enum SourceError {
#[error(transparent)]
Expand Down

0 comments on commit c85c435

Please sign in to comment.