Skip to content
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 6 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ To execute the forward service, use the following command:
Example `configuration.toml`:

```toml
beacon-urls = ["beacon-url-1", "beacon-url-2"]
beacon-nodes = ["beacon-url-1", "beacon-url-2"]

[[lookahead-providers-relays]]
[[lookahead]]
chain-id = 1
relay-urls = ["relay-1", "relay-2"]
relays = ["relay-1", "relay-2"]

[[lookahead-providers-relays]]
[[lookahead]]
url-provider = "lookahead"
chain-id = 2
relay-urls = ["relay-3"]
relays = ["relay-3"]
[[lookahead.registry]]
"0x8248efd1f054fcccd090879c4011ed91ee9f9d0db5ad125ae1af74fdd33de809ddc882400d99b5184ca065d4570df8cc" = "http://a-preconfer-url.xyz"
```

### Details
- url-provider: Specifies the source of the URL. It can be either lookahead or url-mapping.
- If set to **lookahead**, the URL is derived from the lookahead entry.
- If set to **url-mapping**, the URL is determined by looking up the public keys between the lookahead entry public key and the map provided in registry.

Make sure to provide the necessary beacon and relay URLs in the configuration file.
9 changes: 6 additions & 3 deletions config.example.toml
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"
125 changes: 116 additions & 9 deletions src/config.rs
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")]
Copy link
Collaborator

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 ?

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>,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 {
Expand All @@ -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));
}
}
Loading
Loading