Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Reorganise relayer config and add retry logic (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp authored Sep 25, 2023
1 parent 26728a1 commit 1241ff0
Show file tree
Hide file tree
Showing 28 changed files with 310 additions and 118 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
[workspace.package]
version = "0.2.0-dev"
authors = ["Webb Developers <[email protected]>"]
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
homepage = "https://webb.tools"
repository = "https://github.com/webb-tools/pallet-eth2-light-client"
edition = "2021"

[workspace]
members = [
"pallets/*",
Expand Down Expand Up @@ -48,7 +56,7 @@ thread = "*"
dotenvy = "0.15.7"
rand = { version = "0.7.3", default-features = false }
rand_chacha = { version = "0.3.1", default-features = false }

backoff = { version = "0.4.0", features = ["tokio"] }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }

Expand Down
6 changes: 4 additions & 2 deletions crates/bls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[package]
name = "webb-bls"
version = "0.2.0"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = ["Paul Hauner <[email protected]>, Webb Developers <[email protected]>"]
edition = "2021"


[dependencies]
ssz = { package = "webb-eth2-ssz", path = "../ssz", default-features = false }
Expand Down
7 changes: 4 additions & 3 deletions crates/consensus-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "webb-consensus-types"
version = "0.1.0"
authors = ["Webb Developers"]
edition = "2021"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = { workspace = true }

[dependencies]

Expand Down
6 changes: 4 additions & 2 deletions crates/eth-rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[package]
name = "webb-eth-rpc-client"
version = "0.1.0"
edition = "2021"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = { workspace = true }

[dependencies]
eth-types = { path = "../eth-types", default-features = false, features = ["eth2", "arbitrary"] }
Expand Down
6 changes: 4 additions & 2 deletions crates/eth-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[package]
name = "eth-types"
version = "0.1.0"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = ["Near Inc <[email protected]>", "Webb Developers <[email protected]>"]
edition = "2021"


[dependencies]
ssz = { package = "webb-eth2-ssz", path = "../ssz", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions crates/eth2-hashing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "webb-eth2-hashing"
version = "0.3.0"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = ["Paul Hauner <[email protected]>, Webb Developers <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
description = "Hashing primitives used in Ethereum 2.0"

[dependencies]
Expand Down
6 changes: 4 additions & 2 deletions crates/eth2-pallet-init/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[package]
name = "webb-eth2-pallet-init"
version = "0.1.0"
edition = "2021"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = { workspace = true }

[dependencies]
types = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/eth2-pallet-init/src/init_pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ mod tests {
Config::load_from_toml("config_for_tests.toml".try_into().unwrap());

let api = setup_api().await.unwrap();
let mut eth_client_pallet = EthClientPallet::new(api, TypedChainId::Evm(1));
let mut eth_client_pallet = EthClientPallet::new(api.into(), TypedChainId::Evm(1));
config_for_test.validate_updates = Some(false);

init_pallet(&config_for_test, &mut eth_client_pallet).await.unwrap();
Expand All @@ -209,7 +209,7 @@ mod tests {
Config::load_from_toml("config_for_tests.toml".try_into().unwrap());

let api = setup_api().await.unwrap();
let mut eth_client_pallet = EthClientPallet::new(api, TypedChainId::Evm(1));
let mut eth_client_pallet = EthClientPallet::new(api.into(), TypedChainId::Evm(1));
config_for_test.trusted_signer_account_id = None;

init_pallet(&config_for_test, &mut eth_client_pallet).await.unwrap();
Expand All @@ -220,7 +220,7 @@ mod tests {
let config_for_test = Config::load_from_toml("config_for_tests.toml".try_into().unwrap());

let api = setup_api().await.unwrap();
let mut eth_client_pallet = EthClientPallet::new(api, TypedChainId::Evm(1));
let mut eth_client_pallet = EthClientPallet::new(api.into(), TypedChainId::Evm(1));

init_pallet(&config_for_test, &mut eth_client_pallet).await.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/eth2-pallet-init/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn main() -> anyhow::Result<()> {
.await
.expect("failed to connect to substrate node");

let mut eth_client_contract = EthClientPallet::new(api, TypedChainId::None);
let mut eth_client_contract = EthClientPallet::new(api.into(), TypedChainId::None);
init_pallet(&config, &mut eth_client_contract)
.await
.expect("Error on pallet initialization");
Expand Down
9 changes: 5 additions & 4 deletions crates/eth2-pallet-init/src/substrate_pallet_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use eth_types::{
BlockHeader, H256,
};

use std::sync::Arc;
use subxt::{error::DispatchError, utils::AccountId32};
use webb::substrate::{
scale::{Decode, Encode},
Expand Down Expand Up @@ -64,26 +65,26 @@ pub async fn setup_api() -> anyhow::Result<OnlineClient<PolkadotConfig>> {

#[derive(Clone)]
pub struct EthClientPallet {
api: OnlineClient<PolkadotConfig>,
api: Arc<OnlineClient<PolkadotConfig>>,
pair: Pair,
chain: TypedChainId,
}

impl EthClientPallet {
pub fn new(api: OnlineClient<PolkadotConfig>, typed_chain_id: TypedChainId) -> Self {
pub fn new(api: Arc<OnlineClient<PolkadotConfig>>, typed_chain_id: TypedChainId) -> Self {
Self::new_with_pair(api, sp_keyring::AccountKeyring::Alice.pair(), typed_chain_id)
}

pub fn new_with_pair(
api: OnlineClient<PolkadotConfig>,
api: Arc<OnlineClient<PolkadotConfig>>,
pair: Pair,
typed_chain_id: TypedChainId,
) -> Self {
Self { api, pair, chain: typed_chain_id }
}

pub fn new_with_suri_key<T: AsRef<str>>(
api: OnlineClient<PolkadotConfig>,
api: Arc<OnlineClient<PolkadotConfig>>,
suri_key: T,
typed_chain_id: TypedChainId,
) -> anyhow::Result<Self> {
Expand Down
6 changes: 4 additions & 2 deletions crates/finality-update-verify/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[package]
name = "webb-finality-update-verify"
version = "0.1.0"
edition = "2021"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = { workspace = true }

[dependencies]
eth-types = { path = "../eth-types", default-features = false, features = ["eth2"] }
Expand Down
14 changes: 14 additions & 0 deletions crates/lc-relay-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "webb-lc-relay-config"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = { workspace = true }

[dependencies]
serde = { workspace = true }
eth-rpc-client = { package = "webb-eth-rpc-client", path = "../eth-rpc-client" }
eth2-pallet-init = { package = "webb-eth2-pallet-init", path = "../eth2-pallet-init" }
reqwest = { workspace = true, features = ["blocking", "json"] }
dotenvy = { workspace = true }
toml = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use std::{io::Read, path::PathBuf};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
pub struct RelayConfig {
// endpoint to a full node of Eth2 Beacon chain with Light Client API
pub beacon_endpoint: String,

Expand Down Expand Up @@ -70,12 +70,13 @@ pub struct Config {
pub get_light_client_update_by_epoch: Option<bool>,
}

impl Config {
impl RelayConfig {
pub fn load_from_toml(path: PathBuf) -> Self {
let mut config = std::fs::File::open(path).expect("Error on parsing path to config");
let mut content = String::new();
config.read_to_string(&mut content).expect("Error on reading config");
let mut config: Config = toml::from_str(content.as_str()).expect("Error on config parsing");
let mut config: RelayConfig =
toml::from_str(content.as_str()).expect("Error on config parsing");
dotenv().ok();
let api_key_string = std::env::var("ETH1_INFURA_API_KEY").unwrap();
config.eth1_endpoint = config.eth1_endpoint.replace("ETH1_INFURA_API_KEY", &api_key_string);
Expand All @@ -96,8 +97,8 @@ impl Config {
}
}

impl From<Config> for eth2_pallet_init::config::Config {
fn from(val: Config) -> Self {
impl From<RelayConfig> for eth2_pallet_init::config::Config {
fn from(val: RelayConfig) -> Self {
eth2_pallet_init::config::Config {
beacon_endpoint: val.beacon_endpoint,
eth1_endpoint: val.eth1_endpoint,
Expand Down
16 changes: 16 additions & 0 deletions crates/lc-relayer-context/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "webb-lc-relayer-context"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = { workspace = true }


[dependencies]
# Eth2 Light Client
eth2-pallet-init = { package = "webb-eth2-pallet-init", path = "../eth2-pallet-init" }
lc-relay-config = { package = "webb-lc-relay-config", path = "../lc-relay-config" }
anyhow = { workspace = true }
tracing = { workspace = true }
subxt = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt", "time"] }
86 changes: 86 additions & 0 deletions crates/lc-relayer-context/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use eth2_pallet_init::config::Config as InitConfig;
use lc_relay_config::RelayConfig;
use subxt::OnlineClient;
use tokio::sync::broadcast;

/// LightClientRelayerContext contains Relayer's configuration and shutdown signal.
#[derive(Clone)]
pub struct LightClientRelayerContext {
pub lc_relay_config: RelayConfig,
pub lc_init_config: InitConfig,
/// Broadcasts a shutdown signal to all active connections.
///
/// The initial `shutdown` trigger is provided by the `run` caller. The
/// server is responsible for gracefully shutting down active connections.
/// When a connection task is spawned, it is passed a broadcast receiver
/// handle. When a graceful shutdown is initiated, a `()` value is sent via
/// the broadcast::Sender. Each active connection receives it, reaches a
/// safe terminal state, and completes the task.
notify_shutdown: broadcast::Sender<()>,
}

impl LightClientRelayerContext {
pub fn new(lc_relay_config: RelayConfig, lc_init_config: InitConfig) -> Self {
let (notify_shutdown, _) = broadcast::channel(2);
Self { lc_relay_config, lc_init_config, notify_shutdown }
}
pub async fn substrate_provider(self) -> anyhow::Result<OnlineClient<subxt::PolkadotConfig>> {
let maybe_client = OnlineClient::<subxt::PolkadotConfig>::from_url(
self.lc_relay_config.substrate_endpoint.clone(),
)
.await;
let client = match maybe_client {
Ok(client) => client,
Err(err) => return Err(err.into()),
};
Ok(client)
}
/// Returns a broadcast receiver handle for the shutdown signal.
pub fn shutdown_signal(&self) -> Shutdown {
Shutdown::new(self.notify_shutdown.subscribe())
}
/// Sends a shutdown signal to all subscribed tasks/connections.
pub fn shutdown(&self) {
let _ = self.notify_shutdown.send(());
}
}

/// Listens for the server shutdown signal.
///
/// Shutdown is signalled using a `broadcast::Receiver`. Only a single value is
/// ever sent. Once a value has been sent via the broadcast channel, the server
/// should shutdown.
///
/// The `Shutdown` struct listens for the signal and tracks that the signal has
/// been received. Callers may query for whether the shutdown signal has been
/// received or not.
#[derive(Debug)]
pub struct Shutdown {
/// `true` if the shutdown signal has been received
shutdown: bool,

/// The receive half of the channel used to listen for shutdown.
notify: broadcast::Receiver<()>,
}

impl Shutdown {
/// Create a new `Shutdown` backed by the given `broadcast::Receiver`.
pub fn new(notify: broadcast::Receiver<()>) -> Shutdown {
Shutdown { shutdown: false, notify }
}

/// Receive the shutdown notice, waiting if necessary.
pub async fn recv(&mut self) {
// If the shutdown signal has already been received, then return
// immediately.
if self.shutdown {
return
}

// Cannot receive a "lag error" as only one value is ever sent.
let _ = self.notify.recv().await;

// Remember that the signal has been received.
self.shutdown = true;
}
}
5 changes: 3 additions & 2 deletions crates/merkle-proof/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "webb-merkle-proof"
version = "0.2.0"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = ["Michael Sproul <[email protected]>", "Webb Developers <[email protected]>"]
edition = "2021"

[dependencies]
eth2-hashing = { package = "webb-eth2-hashing", path = "../eth2-hashing", default-features = false, features = ["zero_hash_cache"] }
Expand Down
5 changes: 3 additions & 2 deletions crates/safe-arith/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
name = "webb-safe-arith"
version = "0.1.0"
authors = ["Michael Sproul <[email protected]>"]
edition = "2021"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }

[dependencies]
10 changes: 6 additions & 4 deletions crates/serde-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
[package]
name = "webb-eth2-serde-utils"
version = "0.1.1"
authors = ["Paul Hauner <[email protected]", "Michael Sproul <[email protected]>"]
edition = "2021"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = ["Paul Hauner <[email protected]", "Michael Sproul <[email protected]>", "Webb Developers <[email protected]>"]
description = "Serialization and deserialization utilities useful for JSON representations of Ethereum 2.0 types."
license = "Apache-2.0"



[dependencies]
serde = { workspace = true, features = ["derive"], optional = true }
Expand Down
8 changes: 4 additions & 4 deletions crates/ssz-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "webb-eth2-ssz-derive"
version = "0.3.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2021"
version = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
authors = ["Paul Hauner <[email protected]>", "Webb Developers <[email protected]>"]
description = "Procedural derive macros to accompany the eth2_ssz crate."
license = "Apache-2.0"

[lib]
name = "ssz_derive"
Expand Down
Loading

0 comments on commit 1241ff0

Please sign in to comment.