-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add relay and utxo data adapters with mock implementations (#10)
* feat: add relay and utxo data providers with mock implementations * refactor: rename UtxoDataProvider to UtxoDataAdapter and update related implementations
- Loading branch information
Showing
6 changed files
with
122 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod relay; | ||
pub mod utxo; |
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,41 @@ | ||
use async_trait::async_trait; | ||
|
||
/// A simple trait for returning a list of relay addresses in the format "hostname:port". | ||
#[async_trait] | ||
pub trait RelayDataProvider { | ||
async fn get_relays(&self) -> Vec<String>; | ||
} | ||
|
||
/// A mock provider that returns a pre-defined list of string addresses, e.g., ["relay1:3001", "relay2:3002"]. | ||
pub struct MockRelayDataProvider { | ||
pub mock_relays: Vec<String>, | ||
} | ||
|
||
#[async_trait] | ||
impl RelayDataProvider for MockRelayDataProvider { | ||
async fn get_relays(&self) -> Vec<String> { | ||
self.mock_relays.clone() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use tokio::test; | ||
|
||
#[test] | ||
async fn it_returns_mock_relays() { | ||
let provider = MockRelayDataProvider { | ||
mock_relays: vec![ | ||
"relay1:3001".to_string(), | ||
"relay2:3002".to_string(), | ||
], | ||
}; | ||
|
||
let relays = provider.get_relays().await; | ||
|
||
assert_eq!(relays.len(), 2); | ||
assert_eq!(relays[0], "relay1:3001"); | ||
assert_eq!(relays[1], "relay2:3002"); | ||
} | ||
} |
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,75 @@ | ||
use async_trait::async_trait; | ||
use std::collections::HashMap; | ||
use thiserror::Error; | ||
|
||
/// Errors related to querying UTxOs. | ||
#[derive(Debug, Error)] | ||
pub enum UtxoQueryError { | ||
#[error("network error")] | ||
Network(#[from] std::io::Error), | ||
#[error("unknown error: {0}")] | ||
Unknown(String), | ||
} | ||
|
||
#[async_trait] | ||
pub trait UtxoDataAdapter { | ||
async fn fetch_utxos( | ||
&self, | ||
utxo_refs: &[String], | ||
) -> Result<HashMap<String, Vec<u8>>, UtxoQueryError>; | ||
} | ||
|
||
pub struct MockUtxoDataAdapter { | ||
pub known_utxos: HashMap<String, Vec<u8>>, | ||
} | ||
|
||
#[async_trait] | ||
impl UtxoDataAdapter for MockUtxoDataAdapter { | ||
async fn fetch_utxos( | ||
&self, | ||
utxo_refs: &[String], | ||
) -> Result<HashMap<String, Vec<u8>>, UtxoQueryError> { | ||
let mut result = HashMap::new(); | ||
|
||
// Check each requested reference; if known, clone the data into the result | ||
for reference in utxo_refs { | ||
if let Some(cbor_data) = self.known_utxos.get(reference) { | ||
result.insert(reference.clone(), cbor_data.clone()); | ||
} | ||
} | ||
|
||
Ok(result) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::collections::HashMap; | ||
|
||
#[tokio::test] | ||
async fn it_returns_found_utxos_only() { | ||
// Arrange: a mock with two known references | ||
let mut known = HashMap::new(); | ||
known.insert("abc123#0".to_string(), vec![0x82, 0xa0]); // example CBOR | ||
known.insert("abc123#1".to_string(), vec![0x83, 0x04]); | ||
|
||
let provider = MockUtxoDataAdapter { known_utxos: known }; | ||
|
||
// We'll request three references, one of which doesn't exist | ||
let requested = vec![ | ||
"abc123#0".to_string(), | ||
"abc123#1".to_string(), | ||
"missing#2".to_string(), | ||
]; | ||
|
||
// Act | ||
let results = provider.fetch_utxos(&requested).await.unwrap(); | ||
|
||
// Assert | ||
assert_eq!(results.len(), 2, "Should only contain two known references"); | ||
assert!(results.contains_key("abc123#0")); | ||
assert!(results.contains_key("abc123#1")); | ||
assert!(!results.contains_key("missing#2")); | ||
} | ||
} |
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