Skip to content

Commit

Permalink
Merge pull request #45 from keep-starknet-strange/dev/pie-testing
Browse files Browse the repository at this point in the history
dev: pie testing
  • Loading branch information
drspacemn authored Oct 26, 2023
2 parents c0d384e + b23de4f commit b7afd0f
Show file tree
Hide file tree
Showing 17 changed files with 751 additions and 530 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ cairo-lang
venv
# Hint Dependency symlink
starkware

.DS_Store
1 change: 0 additions & 1 deletion scripts/debug-hint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# --program_input build/input.json \
# --layout=small --print_output


echo -e "\nrecompiling cairo program($1)...\n"
cairo-format -i tests/programs/*
cairo-compile tests/programs/$1.cairo --output build/programs/$1.json --cairo_path cairo-lang/src:~/cairo_venv/lib/python3.9/site-packages/
Expand Down
1 change: 1 addition & 0 deletions scripts/setup-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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
4 changes: 4 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub struct StorageCommitment {
pub height: usize,
}

// TODO:
// evaluate if we can use a more standard top level transaction type
// - starknet_api::transaction::Transaction -> no deserialization tag information
// - starknet::types::Transaction -> deserializes `transaction_hash` we have `hash_value`
#[serde_as]
#[derive(Deserialize, Clone, Debug, Serialize, Default)]
pub struct InternalTransaction {
Expand Down
95 changes: 55 additions & 40 deletions src/sharp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod pie;
use std::path::PathBuf;

use cairo_vm::vm::runners::cairo_pie::CairoPie;
use reqwest::blocking::Client;
use serde::Deserialize;
use serde_json::json;
use uuid::Uuid;
Expand All @@ -13,46 +14,51 @@ use crate::error::SnOsError;
pub const DEFUALT_SHARP_URL: &str = "https://testnet.provingservice.io";
pub const _LAMBDA_MAX_PIE_MB: u64 = 20_971_520;

#[derive(Debug)]
#[allow(dead_code)]
#[derive(Default, Clone, Debug, Deserialize, PartialEq)]
#[allow(non_camel_case_types)]
pub enum CairoJobStatus {
Unknown,
NotCreated,
InProgress,
Processed,
Onchain,
Invalid,
Failed,
#[default]
UNKNOWN,
NOT_CREATED,
IN_PROGRESS,
PROCESSED,
ONCHAIN,
INVALID,
FAILED,
}

#[allow(dead_code)]
impl CairoJobStatus {
fn as_str(&self) -> &'static str {
match self {
CairoJobStatus::Unknown => "UNKNOWN",
CairoJobStatus::NotCreated => "NOT_CREATED",
CairoJobStatus::InProgress => "IN_PROGRESS",
CairoJobStatus::Processed => "PROCESSED",
CairoJobStatus::Onchain => "ONCHAIN",
CairoJobStatus::Invalid => "INVALID",
CairoJobStatus::Failed => "FAILED",
}
}
#[derive(Default, Clone, Debug, Deserialize, PartialEq)]
#[allow(non_camel_case_types)]
pub enum InvalidReason {
#[default]
CAIRO_PIE_RUN_FAILURE,
FAILED_TO_GENERATE_FACT,
INCOMPATIBLE_PRIME,
NO_COMPATIBLE_LAYOUT,
INVALID_BUILTIN_ORDER_DECLERATION,
INVALID_BUILTIN_USAGE,
INVALID_CAIRO_PIE_FILE_FORMAT,
INVALID_CAIRO_PIE_STORAGE_KEY,
PAGE_SIZE_EXCEEDS_LIMIT,
SECURITY_CHECK_FAILURE,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Clone, Default, Debug, PartialEq, Deserialize)]
pub struct CairoStatusResponse {
pub status: Option<String>,
#[serde(rename = "validation_done")]
#[serde(default)]
pub version: u64,
#[serde(default)]
pub status: CairoJobStatus,
pub validation_done: Option<bool>,
pub version: Option<u64>,
pub error_log: Option<String>,
pub invalid_reason: Option<InvalidReason>,
}

#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct CairoJobResponse {
#[serde(default)]
pub version: u64,
pub cairo_job_key: Option<Uuid>,
pub version: Option<u64>,
#[serde(rename = "errorMessage")]
pub error_message: Option<String>,
#[serde(rename = "errorType")]
Expand All @@ -62,24 +68,27 @@ pub struct CairoJobResponse {
}

pub struct SharpClient {
client: reqwest::blocking::Client,
client: Client,
sharp_addr: String,
pie_path: Option<PathBuf>,
}

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 enum SharpPie {
EncodedPie(String),
PieObject(Box<CairoPie>),
}

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)?,
impl SharpClient {
pub fn submit_pie(&self, pie: SharpPie) -> Result<CairoJobResponse, SnOsError> {
let pie_enc = match pie {
SharpPie::EncodedPie(encoded_pie) => encoded_pie,
SharpPie::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 } });
println!("DATA: {:?}", data);

// CAREFUL NOT TO OVERWHELM SHARP DUE TO SHORT BLOCK TIMES
let resp = self
Expand All @@ -95,7 +104,7 @@ impl SharpClient {
}
}

pub fn get_status(&self, job_key: &str) -> Result<CairoStatusResponse, SnOsError> {
pub fn get_status(&self, job_key: &Uuid) -> Result<CairoStatusResponse, SnOsError> {
let data = serde_json::json!({ "action": "get_status", "request": { "cairo_job_key": job_key } });

let resp = self
Expand All @@ -110,10 +119,16 @@ impl SharpClient {
_ => Err(SnOsError::SharpRequest("could not get job status".to_string())),
}
}
pub fn with_sharp_addr(sharp_addr: &str) -> Self {
Self { sharp_addr: sharp_addr.to_string(), ..Self::default() }
}
pub fn with_pie_path(pie_path: &str) -> Self {
Self { pie_path: Some(PathBuf::from(pie_path)), ..Self::default() }
}
}

impl Default for SharpClient {
fn default() -> Self {
Self { client: reqwest::blocking::Client::new(), sharp_addr: DEFUALT_SHARP_URL.to_string(), pie_path: None }
Self { client: Client::new(), sharp_addr: DEFUALT_SHARP_URL.to_string(), pie_path: None }
}
}
19 changes: 16 additions & 3 deletions src/sharp/pie.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::fs::File;
use std::io::{Cursor, Read, Seek, Write};
use std::path::Path;
use std::path::{Path, PathBuf};

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,14 @@ 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(pie_str: &str, dst: &str) -> Result<(), SnOsError> {
let buffer =
general_purpose::STANDARD.decode(pie_str.as_bytes()).map_err(|e| SnOsError::PieZipping(format!("{e}")))?;
ZipArchive::new(Cursor::new(&buffer))
.unwrap()
.extract(&PathBuf::from(dst))
.map_err(|e| SnOsError::PieZipping(format!("{e}")))?;
Ok(())
}
Binary file added tests/common/data/memory.bin
Binary file not shown.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tests/common/data/output_pie.b64

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions tests/common/defs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use lazy_static::lazy_static;
use starknet_api::core::{ContractAddress, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::{contract_address, patricia_key};

pub const TESTING_FEE: u128 = 0x10000000000000000000000000;
pub const TESTING_TRANSFER_AMOUNT: u128 = 0x01000000000000000000000000000000;

// -------------------------------Contract Addresses - 0.12.2-------------------------------
lazy_static! {
pub static ref DUMMY_ACCOUNT_ADDRESS_0_12_2: ContractAddress =
contract_address!("5ca2b81086d3fbb4f4af2f1deba4b7fd35e8f4b2caee4e056005c51c05c3dd0");
pub static ref TESTING_1_ADDREESS_0_12_2: ContractAddress =
contract_address!("46fd0893101585e0c7ebd3caf8097b179f774102d6373760c8f60b1a5ef8c92");
pub static ref TESTING_2_ADDREESS_0_12_2: ContractAddress =
contract_address!("4e9665675ca1ac12820b7aff2f44fec713e272efcd3f20aa0fd8ca277f25dc6");
pub static ref TESTING_3_ADDREESS_0_12_2: ContractAddress =
contract_address!("74cebec93a58b4400af9c082fb3c5adfa0800ff1489f8fc030076491ff86c48");
pub static ref TESTING_DELEGATE_ADDREESS_0_12_2: ContractAddress =
contract_address!("238e6b5dffc9f0eb2fe476855d0cd1e9e034e5625663c7eda2d871bd4b6eac0");
}

// -------------------------------Class Hashes - 0.12.2-------------------------------
pub const TOKEN_FOR_TESTING_HASH_0_12_2: &str = "45000d731e6d5ad0023e448dd15cab6f997b04a39120daf56a8816d9f436376";
pub const DUMMY_ACCOUNT_HASH_0_12_2: &str = "16dc3038da22dde8ad61a786ab9930699cc496c8bccb90d77cc8abee89803f7";
pub const DUMMY_TOKEN_HASH_0_12_2: &str = "7cea4d7710723fa9e33472b6ceb71587a0ce4997ef486638dd0156bdb6c2daa";
pub const TESTING_HASH_0_12_2: &str = "7364bafc3d2c56bc84404a6d8be799f533e518b8808bce86395a9442e1e5160";
pub const TESTING_HASH_2_0_12_2: &str = "49bcc976d628b1b238aefc20e77303a251a14ba6c99cd543a86708513414057";
pub const DELEGATE_PROXY_HASH_0_12_2: &str = "1880d2c303f26b658392a2c92a0677f3939f5fdfb960ecf5912afa06ad0b9d9";

pub const EXPECTED_PREV_ROOT: &str = "473010ec333f16b84334f9924912d7a13ce8296b0809c2091563ddfb63011d";
#[allow(dead_code)]
pub const TESTING_BLOCK_HASH: &str = "59b01ba262c999f2617412ffbba780f80b0103d928cbce1aecbaa50de90abda";
#[allow(dead_code)]
pub const EXPECTED_UPDATED_ROOT: &str = "482c9ce8a99afddc9777ff048520fcbfab6c0389f51584016c80a2e94ab8ca7";
Loading

0 comments on commit b7afd0f

Please sign in to comment.