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

change exp_param to exp_params #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 12 additions & 12 deletions proto/vm_runtime.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ syntax = "proto3";
package vm_runtime;

service VmRuntime {
rpc Create(CreateRequest) returns (CreateResponse);
rpc Execute(ExecuteRequest) returns (ExecuteResponse);
rpc Create(CreateRequest) returns (CreateResponse);
rpc Execute(ExecuteRequest) returns (ExecuteResponse);
}

message CreateRequest {
uint64 projectID = 1;
string content = 2;
string expParam = 3;
uint64 projectID = 1;
string content = 2;
repeated string expParams = 3;
}

message CreateResponse {
}

message ExecuteRequest {
uint64 projectID = 1;
uint64 taskID = 2;
string clientID = 3;
string sequencerSignature = 4;
repeated string datas = 5;
uint64 projectID = 1;
uint64 taskID = 2;
string clientID = 3;
string sequencerSignature = 4;
repeated string datas = 5;
}

message ExecuteResponse {
bytes result = 1;
}
bytes result = 1;
}
11 changes: 5 additions & 6 deletions risc0-server/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


integration_test_depends_stop:
@docker stop postgres_test || true && docker container rm postgres_test || true

Expand All @@ -17,9 +15,10 @@ postgres_test:

.PHONY: risc0_depends
risc0_depends:
cargo install cargo-binstall
echo yes | cargo binstall cargo-risczero
cargo risczero install
cargo install cargo-binstall
echo yes | cargo binstall cargo-risczero
curl -L https://foundry.paradigm.xyz | bash
cargo risczero install

integration_test: integration_test_depends
@cd risc0-server/ && cargo test
cargo test
2 changes: 1 addition & 1 deletion risc0-server/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod prove;
pub mod prove;
54 changes: 37 additions & 17 deletions risc0-server/src/core/prove.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use risc0_zkvm::{
default_prover, ExecutorEnv, ProverOpts, Receipt, VerifierContext
};
use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts, Receipt, VerifierContext};

use anyhow::Result;

Expand All @@ -13,11 +11,16 @@ pub fn generate_proof_with_elf(
elf: &[u8],
) -> Result<Receipt> {
let env = ExecutorEnv::builder()
.write(&project_id).unwrap()
.write(&task_id).unwrap()
.write(&client_id).unwrap()
.write(&sequencer_sign).unwrap()
.write(&input_datas).unwrap()
.write(&project_id)
.unwrap()
.write(&task_id)
.unwrap()
.write(&client_id)
.unwrap()
.write(&sequencer_sign)
.unwrap()
.write(&input_datas)
.unwrap()
.build()
.unwrap();

Expand All @@ -26,13 +29,27 @@ pub fn generate_proof_with_elf(
Ok(prover.prove(env, elf).unwrap().receipt)
}

pub fn bonsai_groth16_prove_with_env(project_id: u64, task_id: u64, client_id: String, sequencer_sign: String, input_datas: Vec<String>, elf: &[u8], bonsai_url: String, bonsai_key: String) -> Result<Receipt> {
pub fn bonsai_groth16_prove_with_env(
project_id: u64,
task_id: u64,
client_id: String,
sequencer_sign: String,
input_datas: Vec<String>,
elf: &[u8],
bonsai_url: String,
bonsai_key: String,
) -> Result<Receipt> {
let env = ExecutorEnv::builder()
.write(&project_id).unwrap()
.write(&task_id).unwrap()
.write(&client_id).unwrap()
.write(&sequencer_sign).unwrap()
.write(&input_datas).unwrap()
.write(&project_id)
.unwrap()
.write(&task_id)
.unwrap()
.write(&client_id)
.unwrap()
.write(&sequencer_sign)
.unwrap()
.write(&input_datas)
.unwrap()
.build()
.unwrap();

Expand All @@ -41,12 +58,15 @@ pub fn bonsai_groth16_prove_with_env(project_id: u64, task_id: u64, client_id: S

// Obtain the default prover.
let receipt = default_prover()
.prove_with_ctx( // no bonsai -> Groth16Receipt with bonsai -> Groth16
.prove_with_ctx(
// no bonsai -> Groth16Receipt with bonsai -> Groth16
env,
&VerifierContext::default(),
elf,
&ProverOpts::groth16(),
).unwrap().receipt;
)
.unwrap()
.receipt;

Ok(receipt)
}
}
2 changes: 1 addition & 1 deletion risc0-server/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod models;
pub mod pgdb;
mod schema;
pub mod pgdb;
16 changes: 8 additions & 8 deletions risc0-server/src/db/models.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::db::schema::vms;
use crate::db::schema::proofs;
use crate::db::schema::vms;
use diesel::prelude::*;

#[derive(Queryable, Selectable)]
Expand All @@ -9,12 +9,12 @@ pub struct Vm {
pub id: i32,
pub project_name: String,
pub elf: String,
pub image_id: String
pub image_id: String,
}

impl Vm {
pub fn new() -> Self {
Vm{
Vm {
id: 0,
project_name: "init".to_string(),
elf: "init".to_string(),
Expand Down Expand Up @@ -49,17 +49,17 @@ pub struct Proof {

impl Proof {
pub fn new() -> Self {
Proof{
Proof {
id: 0,
project_id: "0".to_string(),
task_id: "0".to_string(),
client_id: "init".to_string(),
sequencer_sign: "init".to_string(),
image_id: "init".to_string(),
datas_input: "init".to_string(),
image_id: "init".to_string(),
datas_input: "init".to_string(),
receipt_type: "init".to_string(),
receipt: Some("init".to_string()),
status: "init".to_string(),
receipt: Some("init".to_string()),
status: "init".to_string(),
}
}
}
Expand Down
70 changes: 51 additions & 19 deletions risc0-server/src/db/pgdb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env;

use diesel::{PgConnection, Connection, SelectableHelper, RunQueryDsl, QueryDsl, ExpressionMethods};
use diesel::{
Connection, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper,
};
use dotenvy::dotenv;

use crate::db::models::{NewPoof, NewVm, Proof, Vm};
Expand All @@ -13,9 +15,14 @@ pub fn establish_connection() -> PgConnection {
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}

pub fn create_vm<'a>(conn: &mut PgConnection, prj_name: &'a str, elf_str: &'a str, id_str: &'a str) -> Result<Vm, diesel::result::Error> {
use crate::db::schema::vms::dsl::*;
pub fn create_vm<'a>(
conn: &mut PgConnection,
prj_name: &'a str,
elf_str: &'a str,
id_str: &'a str,
) -> Result<Vm, diesel::result::Error> {
use crate::db::schema::vms;
use crate::db::schema::vms::dsl::*;

let new_vm = NewVm {
project_name: prj_name,
Expand All @@ -27,36 +34,53 @@ pub fn create_vm<'a>(conn: &mut PgConnection, prj_name: &'a str, elf_str: &'a st
Ok(_) => (),
Err(err) => {
return Err(err);
},
}
};


diesel::insert_into(vms::table)
.values(&new_vm)
.returning(Vm::as_returning())
.get_result(conn)
.values(&new_vm)
.returning(Vm::as_returning())
.get_result(conn)
}

pub fn get_vm<'a>(conn: &mut PgConnection, id_str: &'a str) -> Result<Vm, diesel::result::Error> {
use crate::db::schema::vms::dsl::*;

// let results = vms.filter(image_id.eq(id_str)).limit(1).select(Vm::as_select()).load(conn).expect("Error loading vms");

let vm = vms.filter(image_id.eq(id_str)).select(Vm::as_select()).first(conn)?;
let vm = vms
.filter(image_id.eq(id_str))
.select(Vm::as_select())
.first(conn)?;
Ok(vm)
}

pub fn get_vm_by_project<'a>(conn: &mut PgConnection, project: &'a str) -> Result<Vm, diesel::result::Error> {
pub fn get_vm_by_project<'a>(
conn: &mut PgConnection,
project: &'a str,
) -> Result<Vm, diesel::result::Error> {
use crate::db::schema::vms::dsl::*;

// let results = vms.filter(image_id.eq(id_str)).limit(1).select(Vm::as_select()).load(conn).expect("Error loading vms");

let vm = vms.filter(project_name.eq(project)).select(Vm::as_select()).first(conn)?;
let vm = vms
.filter(project_name.eq(project))
.select(Vm::as_select())
.first(conn)?;
Ok(vm)
}

pub fn create_proof<'a>(conn: &mut PgConnection, project_id: &'a str, task_id: &'a str, client_id: &'a str, sequencer_sign: &'a str, image_id: &'a str,
datas_input: &'a str, receipt_type: &'a str, status: &'a str) -> Proof {
pub fn create_proof<'a>(
conn: &mut PgConnection,
project_id: &'a str,
task_id: &'a str,
client_id: &'a str,
sequencer_sign: &'a str,
image_id: &'a str,
datas_input: &'a str,
receipt_type: &'a str,
status: &'a str,
) -> Proof {
use crate::db::schema::proofs;

let new_proof = NewPoof {
Expand All @@ -71,13 +95,17 @@ pub fn create_proof<'a>(conn: &mut PgConnection, project_id: &'a str, task_id: &
};

diesel::insert_into(proofs::table)
.values(&new_proof)
.returning(Proof::as_returning())
.get_result(conn)
.expect("Error saving new proof")
.values(&new_proof)
.returning(Proof::as_returning())
.get_result(conn)
.expect("Error saving new proof")
}

pub fn update_proof_with_receipt<'a>(conn: &mut PgConnection, p: &'a Proof, r: &'a String) -> Proof {
pub fn update_proof_with_receipt<'a>(
conn: &mut PgConnection,
p: &'a Proof,
r: &'a String,
) -> Proof {
use crate::db::schema::proofs::dsl::*;

diesel::update(proofs.filter(id.eq(p.id)))
Expand All @@ -87,7 +115,11 @@ pub fn update_proof_with_receipt<'a>(conn: &mut PgConnection, p: &'a Proof, r: &
.expect("Error updating proof")
}

pub fn update_proof_status_with_receipt<'a>(conn: &mut PgConnection, p: &'a Proof, s: &'a String) -> Proof {
pub fn update_proof_status_with_receipt<'a>(
conn: &mut PgConnection,
p: &'a Proof,
s: &'a String,
) -> Proof {
use crate::db::schema::proofs::dsl::*;

diesel::update(proofs.filter(id.eq(p.id)))
Expand Down
5 changes: 1 addition & 4 deletions risc0-server/src/db/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ diesel::table! {
}
}

diesel::allow_tables_to_appear_in_same_query!(
proofs,
vms,
);
diesel::allow_tables_to_appear_in_same_query!(proofs, vms,);
2 changes: 1 addition & 1 deletion risc0-server/src/grpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod server;
pub mod server;
Loading