Skip to content

Commit

Permalink
Merge branch 'build_rs_bootloader' into feat/compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Apr 21, 2024
2 parents 1322fdb + 9e9e2ec commit fac390e
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 157 deletions.
11 changes: 1 addition & 10 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
pull_request:

jobs:
formatting-and-testing:
check-formatting:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand All @@ -25,14 +25,5 @@ jobs:
with:
python-version: '3.9'

- name: Provision Environment
run: python install.py

- name: Format code
run: cargo fmt --check

- name: Run cargo tests
run: cargo test

- name: Run snforge tests
run: snforge test
71 changes: 9 additions & 62 deletions cairo/bootloader/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,30 @@
from starkware.starkware_utils.marshmallow_dataclass_fields import additional_metadata
from starkware.starkware_utils.validated_dataclass import ValidatedMarshmallowDataclass


class TaskSpec(ValidatedMarshmallowDataclass):
"""
Contains task's specification.
"""

@abstractmethod
def load_task(self) -> "Task":
"""
Returns the corresponding task.
"""


class Task:
@abstractmethod
def get_program(self) -> ProgramBase:
"""
Returns the task's Cairo program.
"""


@marshmallow_dataclass.dataclass(frozen=True)
class RunProgramTask(TaskSpec, Task):
TYPE: ClassVar[str] = "RunProgramTask"
program: Program
program_input: dict
use_poseidon: bool

def get_program(self) -> Program:
return self.program

def load_task(self) -> "Task":
return self


@marshmallow_dataclass.dataclass(frozen=True)
class CairoPiePath(TaskSpec):
TYPE: ClassVar[str] = "CairoPiePath"
path: str
use_poseidon: bool

def load_task(self) -> "CairoPieTask":
"""
Loads the PIE to memory.
"""
return CairoPieTask(cairo_pie=CairoPie.from_file(self.path), use_poseidon=self.use_poseidon)


class TaskSchema(OneOfSchema):
"""
Schema for Task/CairoPiePath.
OneOfSchema adds a "type" field.
"""

type_schemas: Dict[str, Type[marshmallow.Schema]] = {
RunProgramTask.TYPE: RunProgramTask.Schema,
CairoPiePath.TYPE: CairoPiePath.Schema,
}

def get_obj_type(self, obj):
return obj.TYPE


@dataclasses.dataclass(frozen=True)
class CairoPieTask(Task):
class Job(Task):
reward: int
num_of_steps: int
cairo_pie: CairoPie
use_poseidon: bool
registry_address: bytearray
public_key: bytearray
signature: bytearray

def get_program(self) -> StrippedProgram:
return self.cairo_pie.program


@marshmallow_dataclass.dataclass(frozen=True)
class SimpleBootloaderInput(ValidatedMarshmallowDataclass):
tasks: List[TaskSpec] = field(
metadata=additional_metadata(marshmallow_field=mfields.List(mfields.Nested(TaskSchema)))
)
identity: bytearray
job: Job

fact_topologies_path: Optional[str]

# If true, the bootloader will put all the outputs in a single page, ignoring the
Expand Down
10 changes: 5 additions & 5 deletions crates/common/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use std::{

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Job {
pub reward: u32, // The reward offered for completing the task
pub reward: u32, // The reward offered for completing the task
pub num_of_steps: u32, // The number of steps expected to complete the task (executor ensures that this number is greater than or equal to the actual steps; in the future, the executor may charge a fee to the delegator if not met)
pub cairo_pie: Vec<u8>, // The task bytecode in compressed zip format, to conserve memory
pub cairo_pie_compressed: Vec<u8>, // The task bytecode in compressed zip format, to conserve memory
pub registry_address: String, // The address of the registry contract where the delegator expects the proof to be verified
pub public_key: PublicKey, // The public key of the delegator, used in the bootloader stage to confirm authenticity of the Job<->Delegator relationship
pub signature: Signature, // The signature of the delegator, used in the bootloader stage to confirm authenticity of the Job<->Delegator relationship
Expand All @@ -37,7 +37,7 @@ impl Job {
Self {
reward,
num_of_steps,
cairo_pie: fs::read(cairo_pie_file).unwrap(),
cairo_pie_compressed: fs::read(cairo_pie_file).unwrap(),
registry_address: registry_address.to_string(),
public_key: PublicKey::from_secret_key(&secret_key),
signature: libsecp256k1::sign(
Expand All @@ -62,7 +62,7 @@ impl Default for Job {
Self {
reward: 0,
num_of_steps: 0,
cairo_pie: vec![1, 2, 3],
cairo_pie_compressed: vec![1, 2, 3],
public_key,
signature,
registry_address: "0x0".to_string(),
Expand All @@ -74,7 +74,7 @@ impl Hash for Job {
fn hash<H: Hasher>(&self, state: &mut H) {
self.reward.hash(state);
self.num_of_steps.hash(state);
self.cairo_pie.hash(state);
self.cairo_pie_compressed.hash(state);
self.registry_address.hash(state);
}
}
Expand Down
25 changes: 0 additions & 25 deletions crates/compiler/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() {
Expand All @@ -8,29 +6,6 @@ fn main() {

// Check if cairo-compile command is present
check_command("cairo-compile");

let workspace_root =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env not present"))
.join("../../");
let out_dir =
PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR env not present")).join("../../../../");
let cairo_path = PathBuf::from(env::var("CAIRO_PATH").expect("CAIRO_PATH env not present"));
let bootloader_path =
PathBuf::from(env::var("BOOTLOADER_PATH").expect("BOOTLOADER_PATH env not present"));
let bootloader_out_name = PathBuf::from(
env::var("BOOTLOADER_OUT_NAME").expect("BOOTLOADER_OUT_NAME env not present"),
);

// Compile Bootloader
Command::new("cairo-compile")
.arg("--cairo_path")
.arg(&workspace_root.join(&cairo_path))
.arg(&workspace_root.join(&cairo_path).join(bootloader_path))
.arg("--output")
.arg(&out_dir.join(bootloader_out_name))
.arg("--proof_mode")
.output()
.expect("bootloader compile failed");
}

fn check_command(cmd: &str) {
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/cairo_compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl CompilerController for CairoCompiler {
cairo_pie.read_to_end(&mut cairo_pie_bytes)?;

// TODO: calculate details
Ok(Job { cairo_pie: cairo_pie_bytes, ..Default::default() })
Ok(Job { cairo_pie_compressed: cairo_pie_bytes, ..Default::default() })
});

Ok(Process::new(future, terminate_tx))
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/cairo_compiler/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod models;

#[cfg(test)]
#[cfg(all(test, feature = "full_test"))]
pub mod multiple_job;
#[cfg(test)]
pub mod single_job;
5 changes: 4 additions & 1 deletion crates/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ strum.workspace = true
tempfile.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing.workspace = true

[features]
full_test = []
2 changes: 1 addition & 1 deletion crates/prover/src/stone_prover/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod models;

#[cfg(test)]
#[cfg(all(test, feature = "full_test"))]
pub mod multiple_job;
#[cfg(test)]
pub mod single_job;
27 changes: 17 additions & 10 deletions crates/prover/src/stone_prover/tests/models.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::stone_prover::types::{config::Config, params::Params};
use crate::stone_prover::types::{
config::Config,
params::{Fri, Params, Stark},
};
use sharp_p2p_common::job_trace::JobTrace;
use std::{env, fs, io::Write, path::PathBuf};
use tempfile::NamedTempFile;
Expand All @@ -18,9 +21,6 @@ pub fn fixture() -> TestFixture {
let memory_path = ws_root.join("crates/tests/cairo/memory");
let trace_path = ws_root.join("crates/tests/cairo/trace");

let cpu_air_prover_config_path = ws_root.join("crates/tests/cairo/cpu_air_prover_config.json");
let cpu_air_params_path = ws_root.join("crates/tests/cairo/cpu_air_params.json");

let mut air_public_input = NamedTempFile::new().unwrap();
air_public_input.write_all(&fs::read(air_public_input_path).unwrap()).unwrap();

Expand All @@ -35,11 +35,18 @@ pub fn fixture() -> TestFixture {

TestFixture {
job_trace: JobTrace { air_public_input, air_private_input, memory, trace },
cpu_air_prover_config: serde_json::from_str(
&fs::read_to_string(cpu_air_prover_config_path).unwrap(),
)
.unwrap(),
cpu_air_params: serde_json::from_str(&fs::read_to_string(cpu_air_params_path).unwrap())
.unwrap(),
cpu_air_prover_config: Config::default(),
cpu_air_params: Params {
stark: Stark {
fri: Fri {
fri_step_list: vec![0, 4, 4, 3],
last_layer_degree_bound: 128,
n_queries: 1,
proof_of_work_bits: 1,
},
log_n_cosets: 1,
},
..Default::default()
},
}
}
5 changes: 4 additions & 1 deletion crates/runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ tempfile.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
zip-extensions.workspace = true
zip-extensions.workspace = true

[features]
full_test = []
2 changes: 1 addition & 1 deletion crates/runner/src/cairo_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl RunnerController for CairoRunner {
let layout: &str = Layout::RecursiveWithPoseidon.into();

let mut cairo_pie = NamedTempFile::new()?;
cairo_pie.write_all(&job.cairo_pie)?;
cairo_pie.write_all(&job.cairo_pie_compressed)?;

let input = BootloaderInput {
tasks: vec![BootloaderTask {
Expand Down
2 changes: 1 addition & 1 deletion crates/runner/src/cairo_runner/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod models;

#[cfg(test)]
#[cfg(all(test, feature = "full_test"))]
pub mod multiple_job;
#[cfg(test)]
pub mod single_job;
5 changes: 4 additions & 1 deletion crates/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ futures.workspace = true
sharp-p2p-prover.workspace = true
sharp-p2p-runner.workspace = true
sharp-p2p-compiler.workspace = true
tokio.workspace = true
tokio.workspace = true

[features]
full_test = []
28 changes: 0 additions & 28 deletions crates/tests/cairo/cpu_air_params.json

This file was deleted.

9 changes: 0 additions & 9 deletions crates/tests/cairo/cpu_air_prover_config.json

This file was deleted.

2 changes: 2 additions & 0 deletions crates/tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#[cfg(all(test, feature = "full_test"))]
mod compiler_runner_flow;
#[cfg(all(test, feature = "full_test"))]
mod runner_prover_flow;

0 comments on commit fac390e

Please sign in to comment.