Skip to content

Commit 2ad1e33

Browse files
authored
Allow SDK consumers construct their own Configs (#61)
1 parent 7f8dca4 commit 2ad1e33

File tree

3 files changed

+89
-63
lines changed

3 files changed

+89
-63
lines changed

examples/cloud.rs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use anyhow::{anyhow, Result};
12
use async_trait::async_trait;
23
use clap::Parser;
34
use reqwest::Url;
5+
use std::fs::File;
46

57
use scroll_proving_sdk::{
6-
config::{CloudProverConfig, Config},
8+
config::Config as SdkConfig,
79
prover::{
810
proving_service::{
911
GetVkRequest, GetVkResponse, ProveRequest, ProveResponse, QueryTaskRequest,
@@ -13,6 +15,7 @@ use scroll_proving_sdk::{
1315
},
1416
utils::init_tracing,
1517
};
18+
use serde::{Deserialize, Serialize};
1619

1720
#[derive(Parser, Debug)]
1821
#[clap(disable_version_flag = true)]
@@ -22,6 +25,51 @@ struct Args {
2225
config_file: String,
2326
}
2427

28+
#[derive(Debug, Serialize, Deserialize, Clone)]
29+
pub struct CloudProverConfig {
30+
pub sdk_config: SdkConfig,
31+
pub base_url: String,
32+
pub api_key: String,
33+
}
34+
35+
impl CloudProverConfig {
36+
pub fn from_reader<R>(reader: R) -> Result<Self>
37+
where
38+
R: std::io::Read,
39+
{
40+
serde_json::from_reader(reader).map_err(|e| anyhow!(e))
41+
}
42+
43+
pub fn from_file(file_name: String) -> Result<Self> {
44+
let file = File::open(file_name)?;
45+
Self::from_reader(&file)
46+
}
47+
48+
fn get_env_var(key: &str) -> Result<Option<String>> {
49+
std::env::var_os(key)
50+
.map(|val| {
51+
val.to_str()
52+
.ok_or_else(|| anyhow!("{key} env var is not valid UTF-8"))
53+
.map(String::from)
54+
})
55+
.transpose()
56+
}
57+
58+
pub fn from_file_and_env(file_name: String) -> Result<Self> {
59+
let mut cfg = Self::from_file(file_name)?;
60+
61+
if let Some(val) = Self::get_env_var("PROVING_SERVICE_BASE_URL")? {
62+
cfg.base_url = val;
63+
}
64+
65+
if let Some(val) = Self::get_env_var("PROVING_SERVICE_API_KEY")? {
66+
cfg.api_key = val;
67+
}
68+
69+
Ok(cfg)
70+
}
71+
}
72+
2573
struct CloudProver {
2674
base_url: Url,
2775
api_key: String,
@@ -58,14 +106,10 @@ async fn main() -> anyhow::Result<()> {
58106
init_tracing();
59107

60108
let args = Args::parse();
61-
let cfg: Config = Config::from_file_and_env(args.config_file)?;
62-
let cloud_prover = CloudProver::new(
63-
cfg.prover
64-
.cloud
65-
.clone()
66-
.ok_or_else(|| anyhow::anyhow!("Missing cloud prover configuration"))?,
67-
);
68-
let prover = ProverBuilder::new(cfg)
109+
let cfg = CloudProverConfig::from_file_and_env(args.config_file)?;
110+
let sdk_config = cfg.sdk_config.clone();
111+
let cloud_prover = CloudProver::new(cfg);
112+
let prover = ProverBuilder::new(sdk_config)
69113
.with_proving_service(Box::new(cloud_prover))
70114
.build()
71115
.await?;

examples/local.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use anyhow::{anyhow, Result};
12
use async_trait::async_trait;
23
use clap::Parser;
3-
44
use scroll_proving_sdk::{
5-
config::{Config, LocalProverConfig},
5+
config::Config as SdkConfig,
66
prover::{
77
proving_service::{
88
GetVkRequest, GetVkResponse, ProveRequest, ProveResponse, QueryTaskRequest,
@@ -12,6 +12,8 @@ use scroll_proving_sdk::{
1212
},
1313
utils::init_tracing,
1414
};
15+
use serde::{Deserialize, Serialize};
16+
use std::fs::File;
1517

1618
#[derive(Parser, Debug)]
1719
#[clap(disable_version_flag = true)]
@@ -21,6 +23,32 @@ struct Args {
2123
config_file: String,
2224
}
2325

26+
#[derive(Debug, Serialize, Deserialize, Clone)]
27+
pub struct LocalProverConfig {
28+
pub sdk_config: SdkConfig,
29+
pub conf1: String,
30+
pub conf2: String,
31+
}
32+
33+
impl LocalProverConfig {
34+
pub fn from_reader<R>(reader: R) -> Result<Self>
35+
where
36+
R: std::io::Read,
37+
{
38+
serde_json::from_reader(reader).map_err(|e| anyhow!(e))
39+
}
40+
41+
pub fn from_file(file_name: String) -> Result<Self> {
42+
let file = File::open(file_name)?;
43+
Self::from_reader(&file)
44+
}
45+
46+
pub fn from_file_and_env(file_name: String) -> Result<Self> {
47+
let cfg = Self::from_file(file_name)?;
48+
Ok(cfg)
49+
}
50+
}
51+
2452
struct LocalProver {}
2553

2654
#[async_trait]
@@ -50,9 +78,10 @@ async fn main() -> anyhow::Result<()> {
5078
init_tracing();
5179

5280
let args = Args::parse();
53-
let cfg: Config = Config::from_file_and_env(args.config_file)?;
54-
let local_prover = LocalProver::new(cfg.prover.local.clone().unwrap());
55-
let prover = ProverBuilder::new(cfg)
81+
let cfg = LocalProverConfig::from_file_and_env(args.config_file)?;
82+
let sdk_config = cfg.sdk_config.clone();
83+
let local_prover = LocalProver::new(cfg);
84+
let prover = ProverBuilder::new(sdk_config)
5685
.with_proving_service(Box::new(local_prover))
5786
.build()
5887
.await?;

src/config.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,7 @@ pub struct ProverConfig {
3636
pub circuit_version: String,
3737
#[serde(default = "default_n_workers")]
3838
pub n_workers: usize,
39-
pub cloud: Option<CloudProverConfig>,
40-
pub local: Option<LocalProverConfig>,
4139
}
42-
43-
#[derive(Debug, Serialize, Deserialize, Clone)]
44-
pub struct CloudProverConfig {
45-
pub base_url: String,
46-
pub api_key: String,
47-
pub retry_count: u32,
48-
pub retry_wait_time_sec: u64,
49-
pub connection_timeout_sec: u64,
50-
}
51-
52-
#[derive(Debug, Serialize, Deserialize, Clone)]
53-
pub struct LocalProverConfig {
54-
pub low_version_circuit: CircuitConfig,
55-
pub high_version_circuit: CircuitConfig,
56-
}
57-
58-
#[derive(Debug, Serialize, Deserialize, Clone)]
59-
pub struct CircuitConfig {
60-
pub hard_fork_name: String,
61-
pub params_path: String,
62-
pub assets_path: String,
63-
}
64-
6540
#[derive(Debug, Serialize, Deserialize, Clone)]
6641
pub struct DbConfig {}
6742

@@ -136,37 +111,15 @@ impl Config {
136111
})
137112
.collect::<Vec<CircuitType>>();
138113
}
139-
if let Some(val) = Self::get_env_var("PROVING_SERVICE_BASE_URL")? {
140-
if let Some(cloud) = &mut self.prover.cloud {
141-
cloud.base_url = val;
142-
}
143-
}
114+
144115
if let Some(val) = Self::get_env_var("N_WORKERS")? {
145116
self.prover.n_workers = val.parse()?;
146117
}
147-
if let Some(val) = Self::get_env_var("PROVING_SERVICE_API_KEY")? {
148-
if let Some(cloud) = &mut self.prover.cloud {
149-
cloud.api_key = val;
150-
}
151-
}
118+
152119
if let Some(val) = Self::get_env_var("DB_PATH")? {
153120
self.db_path = Option::from(val);
154121
}
155122

156123
Ok(())
157124
}
158125
}
159-
160-
impl LocalProverConfig {
161-
pub fn from_reader<R>(reader: R) -> Result<Self>
162-
where
163-
R: std::io::Read,
164-
{
165-
serde_json::from_reader(reader).map_err(|e| anyhow!(e))
166-
}
167-
168-
pub fn from_file(file_name: String) -> Result<Self> {
169-
let file = File::open(file_name)?;
170-
LocalProverConfig::from_reader(&file)
171-
}
172-
}

0 commit comments

Comments
 (0)