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

feat: support local prover && multiple task types #54

Merged
merged 38 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7641e75
feat: support multiple task
yiweichi Jan 2, 2025
8381bab
feat: support multiple task
yiweichi Jan 2, 2025
a1dbcd9
feat: implement default for circuitType
yiweichi Jan 2, 2025
688ede5
feat: add local prover config
yiweichi Jan 4, 2025
6dd13b6
fix: clone for ProveRequest
yiweichi Jan 5, 2025
7d010b0
fix: lint
yiweichi Jan 5, 2025
f289997
remove unused config fields
yiweichi Jan 6, 2025
2dfae63
update config file
yiweichi Jan 6, 2025
4dab63a
update config file
yiweichi Jan 6, 2025
c52ccdc
Update src/config.rs
yiweichi Jan 8, 2025
c319139
fix: comments
yiweichi Jan 8, 2025
aacd757
Merge branch 'feat-integrate-proving-sdk' of https://github.com/scrol…
yiweichi Jan 8, 2025
61bbbe1
feat: add default n_workers
yiweichi Jan 8, 2025
f8f2ecd
update get prover version
yiweichi Jan 9, 2025
e29b98d
update get prover name
yiweichi Jan 9, 2025
79a1c05
remove ds_store
yiweichi Jan 10, 2025
1a1748f
add prover_provider_type
yiweichi Jan 12, 2025
ea4b1e3
rm ds_store
yiweichi Jan 14, 2025
d976336
Revert "rm ds_store"
yiweichi Jan 14, 2025
9d4dcfb
feat: local prover task resume
yiweichi Jan 14, 2025
e23bb7d
fix: login message rlp encode(#55)
yiweichi Jan 15, 2025
f4f8d14
fix: local prover task cache & db refactor
yiweichi Jan 15, 2025
575423f
Apply suggestions from code review
yiweichi Jan 15, 2025
b0ab0e1
Revert "Apply suggestions from code review" && refactor: ProverProvid…
yiweichi Jan 15, 2025
6fbd996
refactor: ProverProviderType to_u8
yiweichi Jan 15, 2025
4c65b38
test: add test for coordinator signature compatibility
yiweichi Jan 16, 2025
db00a03
format
yiweichi Jan 16, 2025
4ae7687
revert local prover pre-insert task
yiweichi Jan 17, 2025
1e83846
update cargo.toml
yiweichi Jan 17, 2025
b57526e
merge example config files
yiweichi Jan 20, 2025
1e79097
fix: chunk task input serialization issue (#59)
yiweichi Jan 21, 2025
c28acd0
fix: get block trace deserialization
yiweichi Jan 21, 2025
92634c6
feat: add default for QueryTaskResponse (#60)
yiweichi Jan 21, 2025
7f8dca4
revert N_WORKERS env
yiweichi Jan 22, 2025
2ad1e33
Allow SDK consumers construct their own Configs (#61)
omerfirmak Jan 28, 2025
32defde
enable env override
yiweichi Feb 17, 2025
69dff84
add err log for get block number
yiweichi Feb 17, 2025
160db6c
add err log for get block number
yiweichi Feb 17, 2025
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
Binary file removed .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ db/
*.lock
*.tgz
.vscode/

.DS_Store
26 changes: 0 additions & 26 deletions conf/config.json

This file was deleted.

27 changes: 27 additions & 0 deletions conf/config_cloud.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"prover_name_prefix": "prover_name",
"keys_dir": "keys",
"coordinator": {
"base_url": "https://coordinator-api.scrollsdk",
"retry_count": 3,
"retry_wait_time_sec": 5,
"connection_timeout_sec": 60
},
"l2geth": {
"endpoint": "https://l2-rpc.scrollsdk"
},
"prover": {
"circuit_type": [1,2,3],
"circuit_version": "v0.13.1",
"n_workers": 1,
"cloud": {
"base_url": "<CLOUD_PROVING_SERVICE_BASE_URL>",
"api_key": "<API_KEY>",
"retry_count": 3,
"retry_wait_time_sec": 5,
"connection_timeout_sec": 60
}
},
"db_path": "db"
}

31 changes: 31 additions & 0 deletions conf/config_local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"prover_name_prefix": "prover_name",
"keys_dir": "keys",
"coordinator": {
"base_url": "https://coordinator-api.scrollsdk",
"retry_count": 3,
"retry_wait_time_sec": 5,
"connection_timeout_sec": 60
},
"l2geth": {
"endpoint": "https://l2-rpc.scrollsdk"
},
"prover": {
"circuit_type": [1,2,3],
"circuit_version": "v0.13.1",
"local": {
"low_version_circuit": {
"hard_fork_name": "bernoulli",
"params_path": "params",
"assets_path": "assets"
},
"high_version_circuit": {
"hard_fork_name": "curie",
"params_path": "params",
"assets_path": "assets"
}
}
},
"db_path": "db"
}

2 changes: 1 addition & 1 deletion examples/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl ProvingService for CloudProver {
fn is_local(&self) -> bool {
false
}
async fn get_vk(&self, req: GetVkRequest) -> GetVkResponse {
async fn get_vks(&self, req: GetVkRequest) -> GetVkResponse {
todo!()
}
async fn prove(&self, req: ProveRequest) -> ProveResponse {
Expand Down
4 changes: 2 additions & 2 deletions examples/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl ProvingService for LocalProver {
fn is_local(&self) -> bool {
true
}
async fn get_vk(&self, req: GetVkRequest) -> GetVkResponse {
async fn get_vks(&self, req: GetVkRequest) -> GetVkResponse {
todo!()
}
async fn prove(&self, req: ProveRequest) -> ProveResponse {
Expand All @@ -50,7 +50,7 @@ async fn main() -> anyhow::Result<()> {
init_tracing();

let args = Args::parse();
let cfg: Config = Config::from_file(args.config_file)?;
let cfg: Config = Config::from_file_and_env(args.config_file)?;
let local_prover = LocalProver::new(cfg.prover.local.clone().unwrap());
let prover = ProverBuilder::new(cfg)
.with_proving_service(Box::new(local_prover))
Expand Down
Binary file removed src/.DS_Store
Binary file not shown.
82 changes: 59 additions & 23 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prover::CircuitType;
use anyhow::{anyhow, Result};
use dotenv::dotenv;
use serde::{Deserialize, Serialize};
use serde_json;
Expand All @@ -16,10 +17,6 @@ pub struct Config {
pub health_listener_addr: String,
}

fn default_health_listener_addr() -> String {
"0.0.0.0:80".to_string()
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CoordinatorConfig {
pub base_url: String,
Expand All @@ -35,8 +32,9 @@ pub struct L2GethConfig {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProverConfig {
pub circuit_type: CircuitType,
pub circuit_types: Vec<CircuitType>,
pub circuit_version: String,
#[serde(default = "default_n_workers")]
pub n_workers: usize,
pub cloud: Option<CloudProverConfig>,
pub local: Option<LocalProverConfig>,
Expand All @@ -53,45 +51,58 @@ pub struct CloudProverConfig {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LocalProverConfig {
// TODO:
// params path
// assets path
// DB config
pub low_version_circuit: CircuitConfig,
pub high_version_circuit: CircuitConfig,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CircuitConfig {
pub hard_fork_name: String,
pub params_path: String,
pub assets_path: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DbConfig {}

fn default_health_listener_addr() -> String {
"0.0.0.0:80".to_string()
}

fn default_n_workers() -> usize {
1
}

impl Config {
pub fn from_reader<R>(reader: R) -> anyhow::Result<Self>
pub fn from_reader<R>(reader: R) -> Result<Self>
where
R: std::io::Read,
{
serde_json::from_reader(reader).map_err(|e| anyhow::anyhow!(e))
serde_json::from_reader(reader).map_err(|e| anyhow!(e))
}

pub fn from_file(file_name: String) -> anyhow::Result<Self> {
pub fn from_file(file_name: String) -> Result<Self> {
let file = File::open(file_name)?;
Config::from_reader(&file)
}

pub fn from_file_and_env(file_name: String) -> anyhow::Result<Self> {
pub fn from_file_and_env(file_name: String) -> Result<Self> {
let mut cfg = Config::from_file(file_name)?;
cfg.override_with_env()?;
Ok(cfg)
}

fn get_env_var(key: &str) -> anyhow::Result<Option<String>> {
Ok(std::env::var_os(key)
fn get_env_var(key: &str) -> Result<Option<String>> {
std::env::var_os(key)
.map(|val| {
val.to_str()
.ok_or_else(|| anyhow::anyhow!("{key} env var is not valid UTF-8"))
.ok_or_else(|| anyhow!("{key} env var is not valid UTF-8"))
.map(String::from)
})
.transpose()?)
.transpose()
}

fn override_with_env(&mut self) -> anyhow::Result<()> {
fn override_with_env(&mut self) -> Result<()> {
dotenv().ok();

if let Some(val) = Self::get_env_var("PROVER_NAME_PREFIX")? {
Expand All @@ -108,11 +119,22 @@ impl Config {
l2geth.endpoint = val;
}
}
if let Some(val) = Self::get_env_var("CIRCUIT_TYPE")? {
self.prover.circuit_type = CircuitType::from_u8(val.parse()?);
}
if let Some(val) = Self::get_env_var("N_WORKERS")? {
self.prover.n_workers = val.parse()?;
if let Some(val) = Self::get_env_var("CIRCUIT_TYPES")? {
let values_vec: Vec<&str> = val
.trim_matches(|c| c == '[' || c == ']')
.split(',')
.map(|s| s.trim())
.collect();

self.prover.circuit_types = values_vec
.iter()
.map(|value| match value.parse::<u8>() {
Ok(num) => CircuitType::from_u8(num),
Err(e) => {
panic!("Failed to parse circuit type: {}", e);
}
})
.collect::<Vec<CircuitType>>();
}
if let Some(val) = Self::get_env_var("PROVING_SERVICE_BASE_URL")? {
if let Some(cloud) = &mut self.prover.cloud {
Expand All @@ -131,3 +153,17 @@ impl Config {
Ok(())
}
}

impl LocalProverConfig {
pub fn from_reader<R>(reader: R) -> Result<Self>
where
R: std::io::Read,
{
serde_json::from_reader(reader).map_err(|e| anyhow!(e))
}

pub fn from_file(file_name: String) -> Result<Self> {
let file = File::open(file_name)?;
LocalProverConfig::from_reader(&file)
}
}
34 changes: 22 additions & 12 deletions src/coordinator_handler/coordinator_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ use super::{
api::Api, error::ErrorCode, GetTaskRequest, GetTaskResponseData, KeySigner, LoginMessage,
LoginRequest, Response, SubmitProofRequest, SubmitProofResponseData,
};
use crate::{config::CoordinatorConfig, prover::CircuitType, utils::get_version};
use crate::{
config::CoordinatorConfig,
prover::{CircuitType, ProverProviderType},
utils::get_version,
};
use tokio::sync::{Mutex, MutexGuard};

pub struct CoordinatorClient {
circuit_type: CircuitType,
circuit_types: Vec<CircuitType>,
vks: Vec<String>,
circuit_version: String,
pub prover_name: String,
pub prover_provider_type: ProverProviderType,
pub key_signer: KeySigner,
api: Api,
token: Mutex<Option<String>>,
Expand All @@ -18,18 +22,18 @@ pub struct CoordinatorClient {
impl CoordinatorClient {
pub fn new(
cfg: CoordinatorConfig,
circuit_type: CircuitType,
circuit_types: Vec<CircuitType>,
vks: Vec<String>,
circuit_version: String,
prover_name: String,
prover_provider_type: ProverProviderType,
key_signer: KeySigner,
) -> anyhow::Result<Self> {
let api = Api::new(cfg)?;
let client = Self {
circuit_type,
circuit_types,
vks,
circuit_version,
prover_name,
prover_provider_type,
key_signer,
api,
token: Mutex::new(None),
Expand Down Expand Up @@ -107,15 +111,21 @@ impl CoordinatorClient {
.as_ref()
.ok_or_else(|| anyhow::anyhow!("Missing challenge token"))?;

let prover_types = match self.circuit_type {
CircuitType::Batch | CircuitType::Bundle => vec![CircuitType::Batch], // to conform to coordinator logic
_ => vec![self.circuit_type],
};
let mut prover_types = vec![];
if self.circuit_types.contains(&CircuitType::Bundle)
|| self.circuit_types.contains(&CircuitType::Batch)
{
prover_types.push(CircuitType::Batch)
}
if self.circuit_types.contains(&CircuitType::Chunk) {
prover_types.push(CircuitType::Chunk)
}

let login_message = LoginMessage {
challenge: login_response_data.token.clone(),
prover_version: get_version().to_string(),
prover_name: self.prover_name.clone(),
prover_version: get_version(&self.circuit_version).to_string(),
prover_provider_type: self.prover_provider_type,
prover_types,
vks: self.vks.clone(),
};
Expand Down
10 changes: 10 additions & 0 deletions src/coordinator_handler/key_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ impl KeySigner {
})
}

pub fn new_from_secret_key(secret_key: &str) -> anyhow::Result<Self> {
let secret = hex::decode(secret_key).unwrap();
let secret_key = SecretKey::from_bytes(secret.as_slice().into())?;
let signing_key = SigningKey::from(secret_key.clone());
Ok(Self {
public_key: secret_key.public_key(),
signing_key,
})
}

pub fn get_public_key(&self) -> String {
let v: Vec<u8> = Vec::from(self.public_key.to_encoded_point(true).as_bytes());
buffer_to_hex(&v, false)
Expand Down
Loading
Loading