Skip to content

Commit

Permalink
Revert "✨ OsInput Missing Fields (#10)"
Browse files Browse the repository at this point in the history
This reverts commit b4948de.
  • Loading branch information
EvolveArt authored Sep 25, 2023
1 parent b4948de commit 104b60a
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 818 deletions.
7 changes: 2 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ hex = "0.4.3"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs" }
uuid = { version = "1.4.0", features = ["v4", "serde"] }
reqwest = { version = "0.11.18", features = ["blocking", "json"] }
bitvec = { version = "1.0.1", features = ["serde"] }
tokio = "1.32.0"
pretty_assertions = "1.4.0"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "nightly-2023-08-02"
channel = "nightly-2023-08-03"
components = ["rustfmt", "clippy"]
profile = "minimal"
1 change: 0 additions & 1 deletion src/business_logic/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/business_logic/transaction/mod.rs

This file was deleted.

18 changes: 0 additions & 18 deletions src/business_logic/transaction/types.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cairo_felt::Felt252;
use starknet::core::types::FieldElement;

#[derive(thiserror::Error, Clone, Debug)]
pub enum SnOsError {
Expand All @@ -19,11 +19,11 @@ pub enum CommitmentInfoError {
#[error("Inconsistent tree heights : {0} {1}.")]
InconsistentTreeHeights(usize, usize),
#[error("Inconsistent tree roots : {0} {1}.")]
InconsistentTreeRoots(Felt252, Felt252),
InconsistentTreeRoots(FieldElement, FieldElement),
}

#[derive(thiserror::Error, Clone, Debug)]
pub enum FactTreeError {
#[error("Unexpected result on single leaf index : {0}")]
UnexpectedResult(Felt252),
UnexpectedResult(FieldElement),
}
61 changes: 0 additions & 61 deletions src/fact_state/contract_state.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/fact_state/mod.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![feature(async_fn_in_trait)]

pub mod business_logic;
pub mod error;
pub mod fact_state;
pub mod hints;
pub mod os_input;
pub mod sharp;
Expand Down
25 changes: 8 additions & 17 deletions src/os_input.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use std::collections::HashMap;

use cairo_felt::Felt252;
use serde::{Deserialize, Serialize};
use starknet::core::types::FieldElement;

use crate::{
business_logic::transaction::types::InternalTransaction,
fact_state::contract_state::ContractState,
storage::{starknet::CommitmentInfo, Storage},
utils::{definitions::general_config::StarknetGeneralConfig, hasher::HasherT},
};
use crate::storage::starknet::CommitmentInfo;

// TODO: Add missing fields
#[derive(Serialize, Deserialize)]
struct StarknetOsInput<S: Storage, H: HasherT> {
contract_state_commitment_info: CommitmentInfo<S, H>,
contract_class_commitment_info: CommitmentInfo<S, H>,
deprecated_compiled_classes: HashMap<Felt252, Felt252>, // TODO: Add contract_class module
compiled_classes: HashMap<Felt252, Felt252>, // TODO: Add contract_class module
contracts: HashMap<Felt252, ContractState>,
class_hash_to_compiled_class_hash: HashMap<Felt252, Felt252>,
general_config: StarknetGeneralConfig,
transactions: Vec<InternalTransaction>,
block_hash: Felt252,
struct StarknetOsInput {
contract_state_commitment_info: CommitmentInfo,
contract_class_commitment_info: CommitmentInfo,
class_hash_to_compiled_class_hash: HashMap<FieldElement, FieldElement>,
block_hash: FieldElement,
}
24 changes: 10 additions & 14 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
pub mod starknet;

use std::marker::PhantomData;

use serde::{Deserialize, Serialize};
use serde_json::{from_slice, to_vec};

use crate::utils::hasher::{pedersen::PedersenHasher, HasherT};
type HashFunctionType = fn(&[u8], &[u8]) -> Vec<u8>;

pub trait Storage: Clone {
async fn set_value(&self, key: Vec<u8>, value: Vec<u8>);
async fn get_value(&self, key: Vec<u8>) -> Option<Vec<u8>>;
async fn del_value(&self, key: Vec<u8>);
}

pub trait DBObject: Serialize + for<'de> Deserialize<'de> {
pub trait DBObject: Serialize + for<'de> Deserialize<'de> + Copy {
/// Method to get the database key for the object
fn db_key(suffix: Vec<u8>) -> Vec<u8>;

Expand All @@ -36,22 +34,20 @@ pub trait DBObject: Serialize + for<'de> Deserialize<'de> {
}
}

pub const HASH_BYTES: [u8; 4] = 32u32.to_be_bytes();

#[derive(Debug)]
pub struct FactCheckingContext<S: Storage, H: HasherT> {
pub storage: S,
pub _n_workers: Option<u32>,
phantom_data: PhantomData<H>,
#[derive(Debug, Clone)]
pub struct FactCheckingContext<S: Storage> {
storage: S,
hash_func: HashFunctionType,
_n_workers: Option<u32>,
}

pub trait Fact: DBObject {
/// A fact is a DB object with a DB key that is a hash of its value.
/// Use set_fact() and get() to read and write facts.
fn _hash<H: HasherT>(&self) -> Vec<u8>;
fn _hash(&self, hash_func: HashFunctionType) -> Vec<u8>;

async fn set_fact<S: Storage, H: HasherT>(&self, ffc: FactCheckingContext<S, H>) -> Vec<u8> {
let hash_val = self._hash::<PedersenHasher>();
async fn set_fact<S: Storage>(&self, ffc: FactCheckingContext<S>) -> Vec<u8> {
let hash_val = self._hash(ffc.hash_func);
self.set(&ffc.storage, &hash_val);
hash_val
}
Expand Down
52 changes: 23 additions & 29 deletions src/storage/starknet.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,56 @@
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, marker::PhantomData};
use std::collections::HashMap;

use cairo_felt::Felt252;
use starknet::core::types::FieldElement;

use crate::{
error::CommitmentInfoError,
utils::{
commitment_tree::{
binary_fact_tree::BinaryFactTree, nodes::InnerNodeFact, patricia_tree::PatriciaTree,
},
hasher::HasherT,
utils::commitment_tree::{
binary_fact_tree::BinaryFactTree, leaf_fact::LeafFact, patricia_tree::PatriciaTree,
},
};

use super::{FactCheckingContext, Storage};

type CommitmentFacts = HashMap<Felt252, Vec<Felt252>>;
type CommitmentFacts = HashMap<FieldElement, Vec<FieldElement>>;

#[derive(Serialize, Deserialize)]
pub struct CommitmentInfo<S: Storage, H: HasherT> {
pub previous_root: Felt252,
pub updated_root: Felt252,
pub struct CommitmentInfo {
previous_root: FieldElement,
updated_root: FieldElement,
tree_height: usize,
commitment_facts: CommitmentFacts,
_phantom_data: PhantomData<S>,
_phantom_data_2: PhantomData<H>,
}

impl<S: Storage, H: HasherT> CommitmentInfo<S, H> {
impl CommitmentInfo {
/// # Returns
/// * `commitment_info` - Commitment information corresponding to the expected modifications
/// and updated tree
pub async fn create_from_expected_updated_tree(
pub async fn create_from_expected_updated_tree<S: Storage, L: LeafFact>(
&mut self,
previous_tree: PatriciaTree,
expected_updated_tree: PatriciaTree,
expected_accessed_indices: Vec<Felt252>,
ffc: FactCheckingContext<S, H>,
) -> Result<CommitmentInfo<S, H>, CommitmentInfoError> {
expected_accessed_indices: Vec<FieldElement>,
_leaft_fact_type: L,
ffc: FactCheckingContext<S>,
) -> Result<CommitmentInfo, CommitmentInfoError> {
if previous_tree.height != expected_updated_tree.height {
return Err(CommitmentInfoError::InconsistentTreeHeights(
previous_tree.height,
expected_updated_tree.height,
));
}

let modifications: HashMap<Felt252, InnerNodeFact> = expected_updated_tree
.get_leaves(&ffc, expected_accessed_indices, None)
let modifications: HashMap<FieldElement, L> = expected_updated_tree
.get_leaves(ffc.clone(), expected_accessed_indices, None)
.await;

let commitment_info = self
.create_from_modifications(
previous_tree,
expected_updated_tree.root,
modifications,
&ffc,
ffc,
)
.await?;

Expand All @@ -63,18 +59,18 @@ impl<S: Storage, H: HasherT> CommitmentInfo<S, H> {

/// # Returns
/// * `commitment_info` - Commitment information corresponding to the given modifications.
pub async fn create_from_modifications(
pub async fn create_from_modifications<S: Storage, L: LeafFact>(
&mut self,
previous_tree: PatriciaTree,
expected_updated_root: Felt252,
modifications: HashMap<Felt252, InnerNodeFact>,
ffc: &FactCheckingContext<S, H>,
) -> Result<CommitmentInfo<S, H>, CommitmentInfoError> {
expected_updated_root: FieldElement,
modifications: HashMap<FieldElement, L>,
ffc: FactCheckingContext<S>,
) -> Result<CommitmentInfo, CommitmentInfoError> {
let mut commitment_facts = CommitmentFacts::new();
let actual_updated_tree = previous_tree
.update(ffc, modifications, Some(&mut commitment_facts))
.await;
let actual_updated_root: Felt252 = actual_updated_tree.root;
let actual_updated_root: FieldElement = actual_updated_tree.root;

if actual_updated_root != expected_updated_root {
return Err(CommitmentInfoError::InconsistentTreeRoots(
Expand All @@ -88,8 +84,6 @@ impl<S: Storage, H: HasherT> CommitmentInfo<S, H> {
updated_root: actual_updated_root,
tree_height: previous_tree.height,
commitment_facts,
_phantom_data: PhantomData,
_phantom_data_2: PhantomData,
})
}
}
Loading

0 comments on commit 104b60a

Please sign in to comment.