Skip to content

Commit

Permalink
feat(prover): supporting upgrade4 (#1412)
Browse files Browse the repository at this point in the history
Co-authored-by: colin <[email protected]>
  • Loading branch information
amoylan2 and colinlyguo authored Jul 8, 2024
1 parent 7196c5c commit abe66c6
Show file tree
Hide file tree
Showing 13 changed files with 468 additions and 574 deletions.
520 changes: 150 additions & 370 deletions prover/Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ ethers-core = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "
ethers-providers = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "v2.0.7" }
halo2_proofs = { git = "https://github.com/scroll-tech/halo2.git", branch = "v1.1" }
snark-verifier-sdk = { git = "https://github.com/scroll-tech/snark-verifier", branch = "develop", default-features = false, features = ["loader_halo2", "loader_evm", "halo2-pse"] }
prover = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "v0.10", default-features = false, features = ["parallel_syn", "scroll", "shanghai"] }
prover_next = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.11.4", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] }
prover_curie = { git = "https://github.com/scroll-tech/zkevm-circuits.git", tag = "v0.11.4", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] }
prover_darwin = { git = "https://github.com/scroll-tech/zkevm-circuits.git", branch = "feat/agg_recursion", package = "prover", default-features = false, features = ["parallel_syn", "scroll"] }
base64 = "0.13.1"
reqwest = { version = "0.12.4", features = ["gzip"] }
reqwest-middleware = "0.3"
Expand Down
4 changes: 0 additions & 4 deletions prover/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ endif

prover:
GO_TAG=${GO_TAG} GIT_REV=${GIT_REV} ZK_VERSION=${ZK_VERSION} cargo build --release
rm -rf ./lib && mkdir ./lib
find target/ -name "libzktrie.so" | xargs -I{} cp {} ./lib

tests_binary:
cargo clean && cargo test --release --no-run
ls target/release/deps/prover* | grep -v "\.d" | xargs -I{} ln -sf {} ./prover.test
rm -rf ./lib && mkdir ./lib
find target/ -name "libzktrie.so" | xargs -I{} cp {} ./lib

lint:
cargo check --all-features
Expand Down
7 changes: 3 additions & 4 deletions prover/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
use std::fs::File;

use crate::types::ProofType;
use crate::types::ProverType;

#[derive(Debug, Serialize, Deserialize)]
pub struct CircuitConfig {
Expand All @@ -24,14 +24,13 @@ pub struct L2GethConfig {
pub endpoint: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Deserialize)]
pub struct Config {
pub prover_name: String,
pub keystore_path: String,
pub keystore_password: String,
pub db_path: String,
#[serde(default)]
pub proof_type: ProofType,
pub prover_type: ProverType,
pub low_version_circuit: CircuitConfig,
pub high_version_circuit: CircuitConfig,
pub coordinator: CoordinatorConfig,
Expand Down
8 changes: 7 additions & 1 deletion prover/src/coordinator_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ pub struct CoordinatorClient<'a> {
key_signer: Rc<KeySigner>,
rt: Runtime,
listener: Box<dyn Listener>,
vks: Vec<String>,
}

impl<'a> CoordinatorClient<'a> {
pub fn new(
config: &'a Config,
key_signer: Rc<KeySigner>,
listener: Box<dyn Listener>,
vks: Vec<String>,
) -> Result<Self> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand All @@ -46,6 +48,7 @@ impl<'a> CoordinatorClient<'a> {
key_signer,
rt,
listener,
vks,
};
client.login()?;
Ok(client)
Expand All @@ -68,12 +71,15 @@ impl<'a> CoordinatorClient<'a> {
challenge: token.clone(),
prover_name: self.config.prover_name.clone(),
prover_version: crate::version::get_version(),
prover_types: vec![self.config.prover_type],
vks: self.vks.clone(),
};

let buffer = login_message.rlp();
let buffer = rlp::encode(&login_message);
let signature = self.key_signer.sign_buffer(&buffer)?;
let login_request = LoginRequest {
message: login_message,
public_key: self.key_signer.get_public_key(),
signature,
};
let login_response = self.rt.block_on(api.login(&login_request, &token))?;
Expand Down
43 changes: 27 additions & 16 deletions prover/src/coordinator_client/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::errors::ErrorCode;
use crate::types::{ProofFailureType, ProofStatus};
use rlp::RlpStream;
use crate::types::{ProofFailureType, ProofStatus, ProverType, TaskType};
use rlp::{Encodable, RlpStream};
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
Expand All @@ -15,23 +15,36 @@ pub struct LoginMessage {
pub challenge: String,
pub prover_name: String,
pub prover_version: String,
pub prover_types: Vec<ProverType>,
pub vks: Vec<String>,
}

impl LoginMessage {
pub fn rlp(&self) -> Vec<u8> {
let mut rlp = RlpStream::new();
let num_fields = 3;
rlp.begin_list(num_fields);
rlp.append(&self.prover_name);
rlp.append(&self.prover_version);
rlp.append(&self.challenge);
rlp.out().freeze().into()
impl Encodable for LoginMessage {
fn rlp_append(&self, s: &mut RlpStream) {
let num_fields = 5;
s.begin_list(num_fields);
s.append(&self.challenge);
s.append(&self.prover_version);
s.append(&self.prover_name);
// The ProverType in go side is an type alias of uint8
// A uint8 slice is treated as a string when doing the rlp encoding
let prover_types = self
.prover_types
.iter()
.map(|prover_type: &ProverType| prover_type.to_u8())
.collect::<Vec<u8>>();
s.append(&prover_types);
s.begin_list(self.vks.len());
for vk in &self.vks {
s.append(vk);
}
}
}

#[derive(Serialize, Deserialize)]
pub struct LoginRequest {
pub message: LoginMessage,
pub public_key: String,
pub signature: String,
}

Expand All @@ -45,16 +58,15 @@ pub type ChallengeResponseData = LoginResponseData;

#[derive(Default, Serialize, Deserialize)]
pub struct GetTaskRequest {
pub task_type: crate::types::ProofType,
pub task_types: Vec<TaskType>,
pub prover_height: Option<u64>,
pub vks: Vec<String>,
}

#[derive(Serialize, Deserialize)]
pub struct GetTaskResponseData {
pub uuid: String,
pub task_id: String,
pub task_type: crate::types::ProofType,
pub task_type: TaskType,
pub task_data: String,
pub hard_fork_name: String,
}
Expand All @@ -63,12 +75,11 @@ pub struct GetTaskResponseData {
pub struct SubmitProofRequest {
pub uuid: String,
pub task_id: String,
pub task_type: crate::types::ProofType,
pub task_type: TaskType,
pub status: ProofStatus,
pub proof: String,
pub failure_type: Option<ProofFailureType>,
pub failure_msg: Option<String>,
pub hard_fork_name: String,
}

#[derive(Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion prover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
log::info!(
"prover start successfully. name: {}, type: {:?}, publickey: {}, version: {}",
config.prover_name,
config.proof_type,
config.prover_type,
prover.get_public_key(),
version::get_version(),
);
Expand Down
33 changes: 15 additions & 18 deletions prover/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::{
coordinator_client::{listener::Listener, types::*, CoordinatorClient},
geth_client::GethClient,
key_signer::KeySigner,
types::{ProofFailureType, ProofStatus, ProofType},
types::{ProofFailureType, ProofStatus, ProverType},
utils::get_task_types,
zk_circuits_handler::{CircuitsHandler, CircuitsHandlerProvider},
};

Expand All @@ -24,16 +25,11 @@ pub struct Prover<'a> {

impl<'a> Prover<'a> {
pub fn new(config: &'a Config, coordinator_listener: Box<dyn Listener>) -> Result<Self> {
let proof_type = config.proof_type;
let prover_type = config.prover_type;
let keystore_path = &config.keystore_path;
let keystore_password = &config.keystore_password;

let key_signer = Rc::new(KeySigner::new(keystore_path, keystore_password)?);
let coordinator_client =
CoordinatorClient::new(config, Rc::clone(&key_signer), coordinator_listener)
.context("failed to create coordinator_client")?;

let geth_client = if config.proof_type == ProofType::Chunk {
let geth_client = if config.prover_type == ProverType::Chunk {
Some(Rc::new(RefCell::new(
GethClient::new(
&config.prover_name,
Expand All @@ -45,9 +41,16 @@ impl<'a> Prover<'a> {
None
};

let provider = CircuitsHandlerProvider::new(proof_type, config, geth_client.clone())
let provider = CircuitsHandlerProvider::new(prover_type, config, geth_client.clone())
.context("failed to create circuits handler provider")?;

let vks = provider.init_vks(prover_type, config, geth_client.clone());

let key_signer = Rc::new(KeySigner::new(keystore_path, keystore_password)?);
let coordinator_client =
CoordinatorClient::new(config, Rc::clone(&key_signer), coordinator_listener, vks)
.context("failed to create coordinator_client")?;

let prover = Prover {
config,
key_signer: Rc::clone(&key_signer),
Expand All @@ -59,23 +62,19 @@ impl<'a> Prover<'a> {
Ok(prover)
}

pub fn get_proof_type(&self) -> ProofType {
self.config.proof_type
}

pub fn get_public_key(&self) -> String {
self.key_signer.get_public_key()
}

pub fn fetch_task(&self) -> Result<Task> {
log::info!("[prover] start to fetch_task");
let mut req = GetTaskRequest {
task_type: self.get_proof_type(),
task_types: get_task_types(self.config.prover_type),
prover_height: None,
vks: self.circuits_handler_provider.borrow().get_vks(),
// vks: self.circuits_handler_provider.borrow().get_vks(),
};

if self.get_proof_type() == ProofType::Chunk {
if self.config.prover_type == ProverType::Chunk {
let latest_block_number = self.get_latest_block_number_value()?;
if let Some(v) = latest_block_number {
if v.as_u64() == 0 {
Expand Down Expand Up @@ -130,7 +129,6 @@ impl<'a> Prover<'a> {
task_type: proof_detail.proof_type,
status: ProofStatus::Ok,
proof: proof_detail.proof_data,
hard_fork_name: task.hard_fork_name.clone(),
..Default::default()
};

Expand All @@ -151,7 +149,6 @@ impl<'a> Prover<'a> {
status: ProofStatus::Error,
failure_type: Some(failure_type),
failure_msg: Some(error.to_string()),
hard_fork_name: task.hard_fork_name.clone(),
..Default::default()
};
self.do_submit(&request)
Expand Down
Loading

0 comments on commit abe66c6

Please sign in to comment.