-
Notifications
You must be signed in to change notification settings - Fork 1
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
mapping #4
Merged
Merged
mapping #4
Changes from 4 commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
beacon-urls = ["https:://beacon-url"] | ||
beacon-nodes = ["https:://beacon-url"] | ||
|
||
[[lookahead-providers-relays]] | ||
[[lookahead]] | ||
url-provider = "lookahead" | ||
chain-id = 1 | ||
relay-urls = ["https://relay-url"] | ||
relays = [] | ||
[[lookahead.registry]] | ||
"0x8248efd1f054fcccd090879c4011ed91ee9f9d0db5ad125ae1af74fdd33de809ddc882400d99b5184ca065d4570df8cc" = "http://a-preconfer-url.xyz" |
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 |
---|---|---|
@@ -1,22 +1,62 @@ | ||
use std::{fs, path::Path}; | ||
use std::{fs, path::Path, str::FromStr}; | ||
|
||
use alloy::rpc::types::beacon::BlsPublicKey; | ||
use eyre::{Result, WrapErr}; | ||
use serde::Deserialize; | ||
use hashbrown::HashMap; | ||
use serde::{Deserialize, Deserializer}; | ||
use url::Url; | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub enum UrlProvider { | ||
Lookahead, | ||
Registry, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Config { | ||
#[serde(rename = "lookahead-providers-relays")] | ||
pub lookahead_providers_relays: Vec<LookaheadProvider>, | ||
#[serde(rename = "beacon-urls")] | ||
pub beacon_urls: Vec<String>, | ||
#[serde(rename = "lookahead")] | ||
pub lookahead_providers_relays: Vec<Lookahead>, | ||
#[serde(rename = "beacon-nodes")] | ||
pub beacon_nodes: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct LookaheadProvider { | ||
pub struct Lookahead { | ||
#[serde(rename = "chain-id")] | ||
pub chain_id: u16, | ||
#[serde(rename = "relay-urls")] | ||
pub relay_urls: Vec<String>, | ||
#[serde(rename = "relays")] | ||
pub relays: Vec<String>, | ||
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. do we need this? |
||
#[serde(rename = "registry", default, deserialize_with = "deserialize_registry")] | ||
pub registry: Option<HashMap<BlsPublicKey, Url>>, | ||
#[serde(rename = "url-provider")] | ||
pub url_provider: UrlProvider, | ||
} | ||
|
||
fn deserialize_registry<'de, D>( | ||
deserializer: D, | ||
) -> Result<Option<HashMap<BlsPublicKey, Url>>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let temp_registry: Option<HashMap<String, String>> = Option::deserialize(deserializer)?; | ||
if let Some(temp_registry) = temp_registry { | ||
let mut registry: HashMap<BlsPublicKey, Url> = HashMap::new(); | ||
|
||
for (key, value) in temp_registry { | ||
match BlsPublicKey::from_str(key.as_str()) { | ||
Ok(bls_key) => { | ||
registry.insert(bls_key, Url::from_str(&value).unwrap()); | ||
} | ||
Err(_) => { | ||
return Err(serde::de::Error::custom(format!("Failed to convert key: {}", key))); | ||
} | ||
} | ||
} | ||
Ok(Some(registry)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
impl Config { | ||
|
@@ -25,3 +65,70 @@ impl Config { | |
toml::from_str(&toml_str).wrap_err("could not parse configuration file") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::str::FromStr; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_deserialize_config() { | ||
let data = r#" | ||
beacon-nodes = ["node1", "node2"] | ||
[[lookahead]] | ||
chain-id = 1 | ||
url-provider = "lookahead" | ||
relays = ["relay1", "relay2"] | ||
[lookahead.registry] | ||
"0x8248efd1f054fcccd090879c4011ed91ee9f9d0db5ad125ae1af74fdd33de809ddc882400d99b5184ca065d4570df8cc" = "localhost:21009" | ||
"#; | ||
|
||
let expected_registry = { | ||
let mut registry = HashMap::new(); | ||
registry.insert(BlsPublicKey::from_str("0x8248efd1f054fcccd090879c4011ed91ee9f9d0db5ad125ae1af74fdd33de809ddc882400d99b5184ca065d4570df8cc").unwrap(), Url::from_str("localhost:21009").unwrap()); | ||
registry | ||
}; | ||
|
||
let expected_lookahead = Lookahead { | ||
chain_id: 1, | ||
relays: vec!["relay1".to_string(), "relay2".to_string()], | ||
registry: Some(expected_registry), | ||
url_provider: UrlProvider::Lookahead, | ||
}; | ||
|
||
let _expected_config = Config { | ||
lookahead_providers_relays: vec![expected_lookahead], | ||
beacon_nodes: vec!["node1".to_string(), "node2".to_string()], | ||
}; | ||
|
||
let config: Config = toml::from_str(data).unwrap(); | ||
assert!(matches!(config, _expected_config)); | ||
} | ||
|
||
#[test] | ||
fn test_deserialize_config_no_lookahead_registry() { | ||
let data = r#" | ||
beacon-nodes = ["node1", "node2"] | ||
[[lookahead]] | ||
chain-id = 1 | ||
url-provider = "lookahead" | ||
relays = ["relay1", "relay2"] | ||
"#; | ||
|
||
let expected_lookahead = Lookahead { | ||
chain_id: 1, | ||
relays: vec!["relay1".to_string(), "relay2".to_string()], | ||
registry: None, | ||
url_provider: UrlProvider::Lookahead, | ||
}; | ||
|
||
let _expected_config = Config { | ||
lookahead_providers_relays: vec![expected_lookahead], | ||
beacon_nodes: vec!["node1".to_string(), "node2".to_string()], | ||
}; | ||
|
||
let config: Config = toml::from_str(data).unwrap(); | ||
assert!(matches!(config, _expected_config)); | ||
} | ||
} |
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.
should this be something like
lookaheads
?