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

fix: proto and env configs #1

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions core/lib/config/src/configs/da_client/avail.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use serde::Deserialize;
use zksync_basic_types::api_key::APIKey;
use zksync_basic_types::seed_phrase::SeedPhrase;
use zksync_basic_types::{api_key::APIKey, seed_phrase::SeedPhrase};

pub const AVAIL_GAS_RELAY_CLIENT_NAME: &str = "GasRelay";
pub const AVAIL_FULL_CLIENT_NAME: &str = "FullClient";

#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(tag = "avail_client")]
pub enum AvailClientConfig {
Default(AvailDefaultConfig),
FullClient(AvailDefaultConfig),
GasRelay(AvailGasRelayConfig),
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct AvailConfig {
pub bridge_api_url: String,
pub timeout: usize,
#[serde(flatten)]
pub config: AvailClientConfig,
}

Expand Down
2 changes: 1 addition & 1 deletion core/lib/config/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ impl Distribution<configs::da_client::DAClientConfig> for EncodeDist {
Avail(AvailConfig {
bridge_api_url: self.sample(rng),
timeout: self.sample(rng),
config: AvailClientConfig::Default(AvailDefaultConfig {
config: AvailClientConfig::FullClient(AvailDefaultConfig {
api_node_url: self.sample(rng),
app_id: self.sample(rng),
}),
Expand Down
44 changes: 30 additions & 14 deletions core/lib/env_config/src/da_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@ use std::env;

use zksync_config::configs::{
da_client::{
avail::AvailSecrets, DAClientConfig, AVAIL_CLIENT_CONFIG_NAME,
OBJECT_STORE_CLIENT_CONFIG_NAME,
avail::{
AvailClientConfig, AvailSecrets, AVAIL_FULL_CLIENT_NAME, AVAIL_GAS_RELAY_CLIENT_NAME,
},
DAClientConfig, AVAIL_CLIENT_CONFIG_NAME, OBJECT_STORE_CLIENT_CONFIG_NAME,
},
secrets::DataAvailabilitySecrets,
AvailConfig,
};

use crate::{envy_load, FromEnv};

impl FromEnv for DAClientConfig {
fn from_env() -> anyhow::Result<Self> {
let client_tag = std::env::var("DA_CLIENT")?;
let client_tag = env::var("DA_CLIENT")?;
let config = match client_tag.as_str() {
AVAIL_CLIENT_CONFIG_NAME => Self::Avail(envy_load("da_avail_config", "DA_")?),
AVAIL_CLIENT_CONFIG_NAME => Self::Avail(AvailConfig {
bridge_api_url: env::var("DA_BRIDGE_API_URL").ok().unwrap(),
timeout: env::var("DA_TIMEOUT")?.parse()?,

config: match env::var("DA_AVAIL_CLIENT_TYPE")?.as_str() {
AVAIL_FULL_CLIENT_NAME => {
AvailClientConfig::FullClient(envy_load("da_avail_full_client", "DA_")?)
}
AVAIL_GAS_RELAY_CLIENT_NAME => {
AvailClientConfig::GasRelay(envy_load("da_avail_gas_relay", "DA_")?)
}
_ => anyhow::bail!("Unknown Avail DA client type"),
},
}),
OBJECT_STORE_CLIENT_CONFIG_NAME => {
Self::ObjectStore(envy_load("da_object_store", "DA_")?)
}
Expand All @@ -33,13 +49,11 @@ impl FromEnv for DataAvailabilitySecrets {
let seed_phrase: Option<zksync_basic_types::seed_phrase::SeedPhrase> =
env::var("DA_SECRETS_SEED_PHRASE")
.ok()
.map(|s| s.parse())
.transpose()?;
.map(|s| s.parse().unwrap());
let gas_relay_api_key: Option<zksync_basic_types::api_key::APIKey> =
env::var("DA_SECRETS_GAS_RELAY_API_KEY")
.ok()
.map(|s| s.parse())
.transpose()?;
.map(|s| s.parse().unwrap());
if seed_phrase.is_none() && gas_relay_api_key.is_none() {
anyhow::bail!("No secrets provided for Avail DA client");
}
Expand Down Expand Up @@ -110,7 +124,7 @@ mod tests {
DAClientConfig::Avail(AvailConfig {
bridge_api_url: bridge_api_url.to_string(),
timeout,
config: AvailClientConfig::Default(AvailDefaultConfig {
config: AvailClientConfig::FullClient(AvailDefaultConfig {
api_node_url: api_node_url.to_string(),
app_id,
}),
Expand All @@ -122,10 +136,13 @@ mod tests {
let mut lock = MUTEX.lock();
let config = r#"
DA_CLIENT="Avail"
DA_API_NODE_URL="localhost:12345"
DA_AVAIL_CLIENT_TYPE="FullClient"

DA_BRIDGE_API_URL="localhost:54321"
DA_APP_ID="1"
DA_TIMEOUT="2"

DA_API_NODE_URL="localhost:12345"
DA_APP_ID="1"
"#;

lock.set_env(config);
Expand All @@ -148,7 +165,6 @@ mod tests {
let config = r#"
DA_CLIENT="Avail"
DA_SECRETS_SEED_PHRASE="bottom drive obey lake curtain smoke basket hold race lonely fit walk"
DA_SECRETS_GAS_RELAY_API_KEY="abcdefghijklmnopqrstuvwxyz0123456789"
"#;

lock.set_env(config);
Expand All @@ -158,12 +174,12 @@ mod tests {
};

assert_eq!(
(actual_seed.unwrap(), actual_key.unwrap()),
(actual_seed.unwrap(), actual_key),
(
"bottom drive obey lake curtain smoke basket hold race lonely fit walk"
.parse()
.unwrap(),
"abcdefghijklmnopqrstuvwxyz0123456789".parse().unwrap()
None
)
);
}
Expand Down
28 changes: 15 additions & 13 deletions core/lib/protobuf_config/src/da_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ impl ProtoRepr for proto::DataAvailabilityClient {
let client = match config {
proto::data_availability_client::Config::Avail(conf) => {
Avail(match conf.config.as_ref() {
Some(proto::avail_config::Config::Default(default_conf)) => AvailConfig {
bridge_api_url: required(&conf.bridge_api_url)
.context("bridge_api_url")?
.clone(),
timeout: *required(&conf.timeout).context("timeout")? as usize,
config: AvailClientConfig::Default(AvailDefaultConfig {
api_node_url: required(&default_conf.api_node_url)
.context("api_node_url")?
Some(proto::avail_config::Config::FullClient(full_client__conf)) => {
AvailConfig {
bridge_api_url: required(&conf.bridge_api_url)
.context("bridge_api_url")?
.clone(),
app_id: *required(&default_conf.app_id).context("app_id")?,
}),
},
timeout: *required(&conf.timeout).context("timeout")? as usize,
config: AvailClientConfig::FullClient(AvailDefaultConfig {
api_node_url: required(&full_client__conf.api_node_url)
.context("api_node_url")?
.clone(),
app_id: *required(&full_client__conf.app_id).context("app_id")?,
}),
}
}
Some(proto::avail_config::Config::GasRelay(gas_relay_conf)) => AvailConfig {
bridge_api_url: required(&conf.bridge_api_url)
.context("bridge_api_url")?
Expand Down Expand Up @@ -61,8 +63,8 @@ impl ProtoRepr for proto::DataAvailabilityClient {
bridge_api_url: Some(config.bridge_api_url.clone()),
timeout: Some(config.timeout as u64),
config: match &config.config {
AvailClientConfig::Default(conf) => Some(
proto::avail_config::Config::Default(proto::AvailDefaultConfig {
AvailClientConfig::FullClient(conf) => Some(
proto::avail_config::Config::FullClient(proto::AvailClientConfig {
api_node_url: Some(conf.api_node_url.clone()),
app_id: Some(conf.app_id),
}),
Expand Down
19 changes: 9 additions & 10 deletions core/lib/protobuf_config/src/proto/config/da_client.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ package zksync.config.da_client;
import "zksync/config/object_store.proto";

message AvailConfig {
optional string bridge_api_url = 1;
optional uint64 timeout = 2;
optional string bridge_api_url = 2;
optional uint64 timeout = 5;
oneof config {
AvailDefaultConfig default = 4;
AvailGasRelayConfig gas_relay = 5;
AvailClientConfig full_client = 7;
AvailGasRelayConfig gas_relay = 8;
}
reserved 3;
reserved "seed";
reserved 6;
reserved "max_retries";
reserved 1; reserved "api_node_url";
reserved 3; reserved "seed";
reserved 4; reserved "app_id";
reserved 6; reserved "max_retries";
}

message AvailDefaultConfig {
message AvailClientConfig {
optional string api_node_url = 1;
optional uint32 app_id = 2;
}

message AvailGasRelayConfig {
optional string gas_relay_api_url = 1;
reserved "gas_relay_api_key";
}

message DataAvailabilityClient {
Expand Down
7 changes: 4 additions & 3 deletions core/node/da_clients/src/avail/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{fmt::Debug, sync::Arc};

use anyhow::anyhow;
use async_trait::async_trait;
use jsonrpsee::ws_client::WsClientBuilder;
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, sync::Arc};
use subxt_signer::ExposeSecret;
use zksync_config::configs::da_client::avail::{AvailClientConfig, AvailConfig, AvailSecrets};
use zksync_da_client::{
Expand Down Expand Up @@ -111,7 +112,7 @@ impl AvailClient {
api_client,
})
}
AvailClientConfig::Default(conf) => {
AvailClientConfig::FullClient(conf) => {
let seed_phrase = secrets
.seed_phrase
.ok_or_else(|| anyhow::anyhow!("Seed phrase is missing"))?;
Expand Down Expand Up @@ -139,7 +140,7 @@ impl DataAvailabilityClient for AvailClient {
match self.sdk_client.as_ref() {
AvailClientMode::Default(client) => {
let default_config = match &self.config.config {
AvailClientConfig::Default(conf) => conf,
AvailClientConfig::FullClient(conf) => conf,
_ => unreachable!(), // validated in protobuf config
};
let ws_client = WsClientBuilder::default()
Expand Down
Loading