Skip to content

Commit

Permalink
Merge pull request #4 from gattaca-com/fb/pubkey-url-mapping
Browse files Browse the repository at this point in the history
mapping
  • Loading branch information
ltitanb authored Jul 24, 2024
2 parents ec4748d + 5e5b722 commit 225b002
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 113 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ tracing = "0.1.37"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tree_hash = "0.6.0"
tree_hash_derive = "0.6.0"
url = "2.5.0"
url = { version="2.5.0", features=["serde"] }


[dev-dependencies]
Expand Down
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"
141 changes: 131 additions & 10 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,63 @@
use std::{fs, path::Path};

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)]
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_all = "kebab-case")]
pub enum Provider {
Lookahead,
Registry,
}

#[derive(Debug, Deserialize)]
pub struct LookaheadProvider {
#[serde(rename = "chain-id")]
pub struct Config {
#[serde(rename = "lookahead")]
pub lookaheads: Vec<Lookahead>,
#[serde(rename = "beacon-nodes")]
pub beacon_nodes: Vec<String>,
}

#[derive(Debug)]
pub struct Lookahead {
pub chain_id: u16,
#[serde(rename = "relay-urls")]
pub relay_urls: Vec<String>,
pub relays: Vec<String>,
pub registry: Option<HashMap<BlsPublicKey, Url>>,
pub provider: Provider,
}

impl<'de> Deserialize<'de> for Lookahead {
fn deserialize<D>(deserializer: D) -> Result<Lookahead, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(rename_all = "kebab-case")]
struct LookaheadHelper {
chain_id: u16,
relays: Vec<String>,
registry: Option<HashMap<BlsPublicKey, Url>>,
url_provider: Provider,
}

let helper = LookaheadHelper::deserialize(deserializer)?;

if matches!(helper.url_provider, Provider::Registry) && helper.registry.is_none() {
return Err(serde::de::Error::custom(
"registry map is mandatory when url-provider is set to registry",
));
}

Ok(Lookahead {
chain_id: helper.chain_id,
relays: helper.relays,
registry: helper.registry,
provider: helper.url_provider,
})
}
}

impl Config {
Expand All @@ -25,3 +66,83 @@ 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),
provider: Provider::Lookahead,
};

let _expected_config = Config {
lookaheads: 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,
provider: Provider::Lookahead,
};

let _expected_config = Config {
lookaheads: 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_fail_if_wrong_registry_combination() {
let data = r#"
beacon-nodes = ["node1", "node2"]
[[lookahead]]
chain-id = 1
url-provider = "registry"
relays = ["relay1", "relay2"]
"#;
let config: Result<Config> = toml::from_str(data).wrap_err("error parsing config");
assert!(config.is_err());
}
}
Loading

0 comments on commit 225b002

Please sign in to comment.