Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Oct 25, 2023
1 parent 0d6198f commit b1e3c79
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ cairo-lang
venv
# Hint Dependency symlink
starkware

.DS_Store
7 changes: 7 additions & 0 deletions scripts/pie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from starkware.cairo.lang.vm.cairo_pie import CairoPie
import base64

with open("sexy_pie.txt", "rb") as f:
cairo_pie = CairoPie.deserialize(base64.b64decode(f.read()))

cairo_pie.to_file("sexy.zip")
1 change: 1 addition & 0 deletions scripts/setup-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ cairo-compile cairo-lang/src/starkware/starknet/core/os/os.cairo --output build/
# compile starknet contract
echo -e "compiling starknet contrarcts...\n"
mkdir -p build/contracts
mkdir -p build/pie
ln -s cairo-lang/src/starkware starkware
starknet-compile-deprecated --no_debug_info tests/contracts/token_for_testing.cairo --output build/contracts/token_for_testing.json --cairo_path cairo-lang/src --account_contract
starknet-compile-deprecated --no_debug_info tests/contracts/dummy_account.cairo --output build/contracts/dummy_account.json --cairo_path cairo-lang/src --account_contract
Expand Down
16 changes: 12 additions & 4 deletions src/sharp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,23 @@ pub struct SharpClient {
pie_path: Option<PathBuf>,
}

pub enum SharPie {
EncodedPie(String),
PieObject(CairoPie),
}

impl SharpClient {
pub fn new(sharp_addr: Option<String>, pie_path: Option<PathBuf>) -> Self {
Self { client: reqwest::blocking::Client::new(), sharp_addr: sharp_addr.unwrap_or_default(), pie_path }
}

pub fn submit_pie(&self, pie_raw: CairoPie) -> Result<CairoJobResponse, SnOsError> {
let pie_enc = match &self.pie_path {
Some(pp) => pie::encode_pie(pie_raw, pp.as_path())?,
None => pie::encode_pie_mem(pie_raw)?,
pub fn submit_pie(&self, pie: SharPie) -> Result<CairoJobResponse, SnOsError> {
let pie_enc = match pie {
SharPie::EncodedPie(encoded_pie) => encoded_pie,
SharPie::PieObject(pie_object) => match &self.pie_path {
Some(pp) => pie::encode_pie(pie_object, pp.as_path())?,
None => pie::encode_pie_mem(pie_object)?,
},
};

let data = json!({ "action": "add_job", "request": { "cairo_pie": pie_enc } });
Expand Down
13 changes: 11 additions & 2 deletions src/sharp/pie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use base64::engine::general_purpose;
use base64::Engine as _;
use cairo_vm::vm::runners::cairo_pie::CairoPie;
use zip::write::FileOptions;
use zip::ZipWriter;
use zip::{ZipArchive, ZipWriter};

use crate::error::SnOsError;

const PIE_FILES: [&str; 5] = ["metadata", "memory", "additional_data", "execution_resources", "version"];
pub const PIE_FILES: [&str; 5] = ["metadata", "memory", "additional_data", "execution_resources", "version"];

/// Writes [CairoPie] to zip file and returns the encoded base64 of the pie.
pub fn encode_pie(pie: CairoPie, dst: &Path) -> Result<String, SnOsError> {
let output = File::create(dst).map_err(|e| SnOsError::PieZipping(format!("{e}")))?;
let zip = zip::ZipWriter::new(output);
Expand Down Expand Up @@ -40,6 +41,7 @@ pub fn encode_pie_mem(pie: CairoPie) -> Result<String, SnOsError> {
Ok(general_purpose::STANDARD.encode(data))
}

/// Write [CairoPie] to a zip Writer (either a file or a rust object).
fn write_to_zip<W: Write + Seek>(pie: CairoPie, mut zip: ZipWriter<W>) -> Result<(), SnOsError> {
let options = FileOptions::default().compression_method(zip::CompressionMethod::Deflated).unix_permissions(0o755);

Expand All @@ -62,3 +64,10 @@ fn write_to_zip<W: Write + Seek>(pie: CairoPie, mut zip: ZipWriter<W>) -> Result

Ok(())
}

/// Convert the base64 encoding of the pie to an unzipped folder.
pub fn decode_base64_to_unzipped(encoded_pie: &str) -> Result<(), SnOsError> {
let buffer = general_purpose::STANDARD_NO_PAD.decode(encoded_pie.as_bytes()).unwrap();
ZipArchive::new(Cursor::new(&buffer)).unwrap().extract(&Path::new("build/pie")).unwrap();
Ok(())
}
1 change: 1 addition & 0 deletions tests/common/output_pie.b64

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions tests/pie.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
mod common;
use std::collections::HashMap;
use std::path::Path;

use cairo_vm::vm::runners::builtin_runner::OUTPUT_BUILTIN_NAME;
use cairo_vm::vm::runners::cairo_pie::{BuiltinAdditionalData, CairoPie, OutputBuiltinAdditionalData, SegmentInfo};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use common::setup_pie;
use rstest::rstest;
use serde_json::json;
use snos::sharp::pie::{decode_base64_to_unzipped, PIE_FILES};

#[rstest]
fn pie_metadata_ok(setup_pie: CairoPie) {
Expand Down Expand Up @@ -71,3 +73,15 @@ fn pie_memory_ok(setup_pie: CairoPie) {
let pie_s = serde_json::to_value(setup_pie).unwrap();
assert_eq!(pie_s["memory"], "00000000000000800080ff7f018006400000000000000000000000000000000000000000000000000100000000000080640000000000000000000000000000000000000000000000000000000000000002000000000000800080fd7f0080024800000000000000000000000000000000000000000000000003000000000000800080ff7f018006400000000000000000000000000000000000000000000000000400000000000080c80000000000000000000000000000000000000000000000000000000000000005000000000000800080fd7f0180024800000000000000000000000000000000000000000000000006000000000000800080ff7f0180064000000000000000000000000000000000000000000000000007000000000000802c0100000000000000000000000000000000000000000000000000000000000008000000000000800080fd7f0280024800000000000000000000000000000000000000000000000009000000000000800080fd7f018026480000000000000000000000000000000000000000000000000a0000000000008003000000000000000000000000000000000000000000000000000000000000000b00000000000080fe7fff7fff7f8b20000000000000000000000000000000000000000000000000000000000080008000000000000001000000000000000000000000000000000000000000000000800100000000800080000000000080010000000000000000000000000000000000000000000000008002000000008000800000000000000200000000000000000000000000000000000000000000000080030000000080008064000000000000000000000000000000000000000000000000000000000000000400000000800080c80000000000000000000000000000000000000000000000000000000000000005000000008000802c0100000000000000000000000000000000000000000000000000000000000006000000008000800300000000000100000000000000000000000000000000000000000000000080000000000000018064000000000000000000000000000000000000000000000000000000000000000100000000000180c80000000000000000000000000000000000000000000000000000000000000002000000000001802c01000000000000000000000000000000000000000000000000000000000000");
}

#[rstest]
fn convert_b64_to_raw() {
decode_base64_to_unzipped(&std::fs::read_to_string("./tests/common/output_pie.b64").unwrap()).unwrap();

for file in PIE_FILES {
assert!(
Path::new(&format!("build/pie/{file:}.{:}", if file != "memory" { "json" } else { "bin" })).exists(),
"Missing file {file:}"
);
}
}
13 changes: 11 additions & 2 deletions tests/sharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rstest::*;
use snos::sharp::SharpClient;

const TEST_CAIRO_JOB_ID: &str = "3a24bbca-ad75-49d5-8ced-12796c6c0738";
const NEW_CAIRO_JOB: &str = "59b5138d-4c6e-49d3-9234-6495223acb43";

// #[rstest]
// fn sharp_client_submit(setup_pie: CairoPie) {
Expand All @@ -14,11 +15,19 @@ const TEST_CAIRO_JOB_ID: &str = "3a24bbca-ad75-49d5-8ced-12796c6c0738";
// }

#[rstest]
#[ignore]
fn sharp_client_status() {
let sharp_client = SharpClient::default();
let submit_resp = sharp_client.get_status(TEST_CAIRO_JOB_ID).unwrap();
let submit_resp = sharp_client.get_status(NEW_CAIRO_JOB).unwrap();
println!("{submit_resp:?}");

assert_eq!(submit_resp.version.unwrap(), 1);
assert!(submit_resp.validation_done.unwrap());
}

#[rstest]
fn prove_os_run() {
let sharp_client = SharpClient::default();
let pie = std::fs::read_to_string("./first_pie.txt").unwrap();
let submit_resp = sharp_client.submit_pie(snos::sharp::SharPie::EncodedPie(pie)).unwrap();
println!("{submit_resp:?}");
}

0 comments on commit b1e3c79

Please sign in to comment.