From 25f6e6dd95e5c23031f3237e5e8746040ae85115 Mon Sep 17 00:00:00 2001 From: xander42280 Date: Fri, 29 Mar 2024 17:08:19 +0800 Subject: [PATCH] add minigeth example add docker_compose storage mod & docker-compose --- docker-compose.yml | 62 +++++++++++++++++++ service/Cargo.toml | 2 + service/config/config.toml | 1 + service/config/config_docker_compose.toml | 5 ++ service/db.sql | 29 +++++++++ service/examples/README.md | 11 +--- service/src/config.rs | 2 + service/src/main.rs | 1 + service/src/stage_service.rs | 19 +++++- service/src/storage.rs | 74 +++++++++++++++++++++++ 10 files changed, 194 insertions(+), 12 deletions(-) create mode 100644 docker-compose.yml create mode 100644 service/config/config_docker_compose.toml create mode 100644 service/db.sql create mode 100644 service/src/storage.rs diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..82758f0 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,62 @@ +# image: zkm-prover:latest => https://github.com/zkMIPS/zkm-prover/blob/main/Dockerfile +# image: gnark-plonky2-verifier:latest => https://github.com/zkMIPS/gnark-plonky2-verifier/blob/main/Dockerfile +version: "3.5" +networks: + default: + name: zkm-prover +services: + zkm-stage: + container_name: zkm-stage + restart: unless-stopped + depends_on: + zkm-prover: + condition: service_started + zkm-snark: + condition: service_started + image: zkm-prover:latest + deploy: + resources: + limits: + memory: 4G + reservations: + memory: 256M + ports: + - 50000:50000 + environment: + - RUST_LOG=info + volumes: + - ./service/config/config_docker_compose.toml:/usr/local/bin/config.toml + - /tmp/zkm-prover/data:/zkm/data + command: + - "/bin/sh" + - "-c" + - "/usr/local/bin/service --config /usr/local/bin/config.toml" + + zkm-prover: + container_name: zkm-prover + restart: unless-stopped + image: zkm-prover:latest + deploy: + resources: + limits: + memory: 400G + reservations: + memory: 256M + environment: + - RUST_LOG=info + volumes: + - ./service/config/config_docker_compose.toml:/usr/local/bin/config.toml + - /tmp/zkm-prover/data:/zkm/data + command: + - "/bin/sh" + - "-c" + - "/usr/local/bin/service --config /usr/local/bin/config.toml" + + zkm-snark: + container_name: zkm-snark + restart: unless-stopped + image: gnark-plonky2-verifier:latest + volumes: + - /tmp/zkm-prover/data:/zkm/data + command: > + /usr/local/bin/start.sh diff --git a/service/Cargo.toml b/service/Cargo.toml index 6f0e4d2..89885a9 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -15,7 +15,9 @@ prost = "0.11.0" tokio = { version = "1.21.0", features = ["macros", "rt-multi-thread", "signal"] } once_cell = "1.8" uuid = { version = "1.2", features = ["v4", "fast-rng", "macro-diagnostics"] } +mysql_async = { version = "*", default-features = false, features = ["minimal"]} serde = "1.0.92" +serde_json = "1.0" serde_derive = "1.0.92" log = "0.4.0" env_logger = "0.10" diff --git a/service/config/config.toml b/service/config/config.toml index 31c09a2..a1fea4e 100644 --- a/service/config/config.toml +++ b/service/config/config.toml @@ -1,4 +1,5 @@ addr = "0.0.0.0:50000" +database_url = "mysql://root:123456@localhost:3306/stage" prover_addrs = ["127.0.0.1:50001", "127.0.0.1:50002"] snark_addrs = ["127.0.0.1:50051"] base_dir = "/tmp/zkm/test/test_proof" \ No newline at end of file diff --git a/service/config/config_docker_compose.toml b/service/config/config_docker_compose.toml new file mode 100644 index 0000000..858d901 --- /dev/null +++ b/service/config/config_docker_compose.toml @@ -0,0 +1,5 @@ +addr = "0.0.0.0:50000" +database_url = "mysql://root:123456@localhost:3306/stage" +prover_addrs = ["zkm-prover:50000"] +snark_addrs = ["zkm-snark:50051"] +base_dir = "/zkm/data" \ No newline at end of file diff --git a/service/db.sql b/service/db.sql new file mode 100644 index 0000000..78f22fd --- /dev/null +++ b/service/db.sql @@ -0,0 +1,29 @@ +CREATE DATABASE IF NOT EXISTS stage; + +use stage; + +CREATE TABLE IF NOT EXISTS stage_task +( + id varchar(255) primary key, + status int not null default 0, + context text , + result text , + check_at timestamp not null default now(), + created_at timestamp not null default now(), + updated_at timestamp not null default now() +); + +CREATE TABLE IF NOT EXISTS prove_task +( + id varchar(255) not null primary key, + proof_id varchar(255) not null default '', + type int not null default 0, + status int not null default 0, + time_cost int not null default 0, + node_info varchar(255) not null default '', + request text , + response text , + check_at timestamp not null default now(), + created_at timestamp not null default now(), + updated_at timestamp not null default now() +); \ No newline at end of file diff --git a/service/examples/README.md b/service/examples/README.md index f9b1100..5742e13 100644 --- a/service/examples/README.md +++ b/service/examples/README.md @@ -1,6 +1,6 @@ # Examples -## Tools +## Hello world * Compile the Go code to MIPS @@ -10,15 +10,6 @@ Write your own hello.go, and compile with GOOS=linux GOARCH=mips GOMIPS=softfloat go build hello.go -o /tmp/zkm/test/hello_world ``` -Download the block and place it in the corresponding directory [minigeth](https://github.com/zkMIPS/cannon-mips). - -``` -$ mkdir -p /tmp/cannon -$ export BASEDIR=/tmp/cannon; minigeth/go-ethereum 13284491 -$ mkdir -p /tmp/zkm/test/0_13284491 -$ cp /tmp/cannon/0_13284491/input /tmp/zkm/test/0_13284491 -``` - * Compile zkm-prover ``` diff --git a/service/src/config.rs b/service/src/config.rs index ecad0b8..286b483 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -14,6 +14,7 @@ pub fn instance() -> &'static Mutex { #[derive(Debug, Deserialize, Clone)] pub struct RuntimeConfig { pub addr: String, + pub database_url: String, pub prover_addrs: Vec, pub snark_addrs: Vec, pub base_dir: String, @@ -26,6 +27,7 @@ impl RuntimeConfig { pub fn new() -> Self { RuntimeConfig { addr: "0.0.0.0:50000".to_string(), + database_url: "mysql://user:password@localhost:3306/dbname".to_string(), prover_addrs: ["0.0.0.0:50000".to_string()].to_vec(), snark_addrs: ["0.0.0.0:50000".to_string()].to_vec(), base_dir: "/tmp".to_string(), diff --git a/service/src/main.rs b/service/src/main.rs index cc612e7..7b581d3 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -12,6 +12,7 @@ mod prover_client; mod prover_node; mod prover_service; mod stage_service; +mod storage; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] diff --git a/service/src/stage_service.rs b/service/src/stage_service.rs index 3825617..d1adefe 100644 --- a/service/src/stage_service.rs +++ b/service/src/stage_service.rs @@ -12,8 +12,8 @@ use tokio::sync::mpsc; use tokio::time; use tonic::{Request, Response, Status}; -use crate::config; use crate::prover_client; +use crate::{config, storage}; use prover::provers::{self, read_file_bin}; #[allow(clippy::module_inception)] @@ -30,6 +30,7 @@ lazy_static! { pub struct StageServiceSVC { tls_config: Option, + _storage: storage::Storage, } impl StageServiceSVC { @@ -46,7 +47,12 @@ impl StageServiceSVC { } else { None }; - Ok(StageServiceSVC { tls_config }) + let database_url = config.database_url.as_str(); + let storage = storage::Storage::new(database_url); + Ok(StageServiceSVC { + tls_config, + _storage: storage, + }) } } @@ -147,6 +153,15 @@ impl StageService for StageServiceSVC { request.get_ref().seg_size, ); + // let _ = self + // .storage + // .insert_stage_task( + // &request.get_ref().proof_id, + // stage_service::ExecutorError::Unspecified as i32, + // &serde_json::to_string(&generate_context).unwrap(), + // ) + // .await; + let mut stage = stage::stage::Stage::new(generate_context); let (tx, mut rx) = mpsc::channel(128); stage.dispatch(); diff --git a/service/src/storage.rs b/service/src/storage.rs new file mode 100644 index 0000000..f482b59 --- /dev/null +++ b/service/src/storage.rs @@ -0,0 +1,74 @@ +use mysql_async::prelude::*; + +pub struct Storage { + db_pool: mysql_async::Pool, +} + +impl Storage { + pub fn new(database_url: &str) -> Self { + let db_pool = mysql_async::Pool::new(database_url); + Storage { db_pool } + } + + #[allow(dead_code)] + pub async fn insert_stage_task( + &self, + proof_id: &str, + status: i32, + context: &str, + ) -> std::result::Result { + let mut conn = self.db_pool.get_conn().await.map_err(|e| (e.to_string()))?; + let stmt = conn + .prep("INSERT INTO stage_task (id, status, context) values (:id, :status, :context)") + .await + .map_err(|e| (e.to_string()))?; + let params = mysql_async::Params::from(vec![ + ( + String::from("id"), + mysql_async::Value::Bytes(proof_id.as_bytes().to_vec()), + ), + ( + String::from("status"), + mysql_async::Value::Int(status as i64), + ), + ( + String::from("context"), + mysql_async::Value::Bytes(context.as_bytes().to_vec()), + ), + ]); + let _: std::result::Result, String> = + conn.exec(stmt, params).await.map_err(|e| (e.to_string())); + Ok(true) + } + + #[allow(dead_code)] + pub async fn update_stage_task( + &mut self, + proof_id: &str, + status: i32, + result: &str, + ) -> std::result::Result { + let mut conn = self.db_pool.get_conn().await.map_err(|e| (e.to_string()))?; + let stmt = conn + .prep("UPDATE stage_task set status = :status, result = :result where id = :id)") + .await + .map_err(|e| (e.to_string()))?; + let params = mysql_async::Params::from(vec![ + ( + String::from("id"), + mysql_async::Value::Bytes(proof_id.as_bytes().to_vec()), + ), + ( + String::from("status"), + mysql_async::Value::Int(status as i64), + ), + ( + String::from("result"), + mysql_async::Value::Bytes(result.as_bytes().to_vec()), + ), + ]); + let _: std::result::Result, String> = + conn.exec(stmt, params).await.map_err(|e| (e.to_string())); + Ok(true) + } +}