Skip to content

Commit

Permalink
Merge pull request eqlabs#2105 from eqlabs/chris/more-p2p-stream-tests
Browse files Browse the repository at this point in the history
more p2p stream tests
  • Loading branch information
CHr15F0x authored Jun 28, 2024
2 parents b9ec6b8 + 1117b40 commit d0ec6fb
Show file tree
Hide file tree
Showing 27 changed files with 2,027 additions and 1,213 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ serde_json = { workspace = true, features = [
] }
serde_with = { workspace = true }
sha3 = { workspace = true }
tagged = { path = "../tagged" }
tagged-debug-derive = { path = "../tagged-debug-derive" }
thiserror = { workspace = true }

[dev-dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::borrow::Cow;
use std::fmt;

use fake::{Dummy, Fake, Faker};
use pathfinder_crypto::Felt;
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
use serde_with::serde_as;

use crate::request::contract::{SelectorAndFunctionIndex, SelectorAndOffset};
use crate::{ByteCodeOffset, EntryPoint};

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Dummy)]
pub enum ClassDefinition<'a> {
Sierra(Sierra<'a>),
Cairo(Cairo<'a>),
Expand Down Expand Up @@ -87,3 +89,85 @@ pub struct CairoEntryPoints {
#[serde(rename = "CONSTRUCTOR")]
pub constructor: Vec<SelectorAndOffset>,
}

#[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq, Hash, Eq)]
#[serde(deny_unknown_fields)]
pub enum EntryPointType {
#[serde(rename = "EXTERNAL")]
External,
#[serde(rename = "L1_HANDLER")]
L1Handler,
#[serde(rename = "CONSTRUCTOR")]
Constructor,
}

impl fmt::Display for EntryPointType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use EntryPointType::*;
f.pad(match self {
External => "EXTERNAL",
L1Handler => "L1_HANDLER",
Constructor => "CONSTRUCTOR",
})
}
}

#[serde_as]
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct SelectorAndOffset {
pub selector: EntryPoint,
#[serde_as(as = "OffsetSerde")]
pub offset: ByteCodeOffset,
}

#[derive(serde::Deserialize, serde::Serialize)]
#[serde(untagged)]
pub enum OffsetSerde {
HexStr(Felt),
Decimal(u64),
}

impl serde_with::SerializeAs<ByteCodeOffset> for OffsetSerde {
fn serialize_as<S>(source: &ByteCodeOffset, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::Serialize;

Felt::serialize(&source.0, serializer)
}
}

impl<'de> serde_with::DeserializeAs<'de, ByteCodeOffset> for OffsetSerde {
fn deserialize_as<D>(deserializer: D) -> Result<ByteCodeOffset, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;

let offset = OffsetSerde::deserialize(deserializer)?;
let offset = match offset {
OffsetSerde::HexStr(felt) => felt,
OffsetSerde::Decimal(decimal) => Felt::from_u64(decimal),
};
Ok(ByteCodeOffset(offset))
}
}

impl<T> Dummy<T> for SelectorAndOffset {
fn dummy_with_rng<R: rand::prelude::Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
Self {
selector: Faker.fake_with_rng(rng),
offset: ByteCodeOffset(Felt::from_u64(rng.gen())),
}
}
}

/// Descriptor of an entry point in a Sierra class.
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, Dummy)]
#[serde(deny_unknown_fields)]
pub struct SelectorAndFunctionIndex {
pub selector: EntryPoint,
pub function_idx: u64,
}
4 changes: 3 additions & 1 deletion crates/common/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use fake::Dummy;
use num_bigint::BigUint;
use pathfinder_crypto::Felt;
use serde_with::serde_conv;
use tagged::Tagged;
use tagged_debug_derive::TaggedDebug;

use crate::{ContractAddress, EventData, EventKey};

#[serde_with::serde_as]
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq, Dummy)]
#[derive(Clone, serde::Deserialize, serde::Serialize, PartialEq, Eq, Dummy, TaggedDebug)]
#[serde(deny_unknown_fields)]
pub struct Event {
#[serde_as(as = "Vec<EventDataAsDecimalStr>")]
Expand Down
1 change: 1 addition & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use pathfinder_crypto::Felt;
use primitive_types::H160;
use serde::{Deserialize, Serialize};

pub mod class_definition;
pub mod consts;
pub mod event;
pub mod hash;
Expand Down
16 changes: 15 additions & 1 deletion crates/common/src/state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct StateUpdate {
pub declared_sierra_classes: HashMap<SierraHash, CasmHash>,
}

#[derive(Default, Debug, Clone, PartialEq)]
#[derive(Default, Debug, Clone, PartialEq, Dummy)]
pub struct StateUpdateData {
pub contract_updates: HashMap<ContractAddress, ContractUpdate>,
pub system_contract_updates: HashMap<ContractAddress, SystemContractUpdate>,
Expand Down Expand Up @@ -312,6 +312,20 @@ impl StateUpdateData {
cairo: self.declared_cairo_classes.clone(),
}
}

pub fn state_diff_length(&self) -> usize {
let mut len = 0;
self.contract_updates.iter().for_each(|(_, update)| {
len += update.storage.len();
len += usize::from(update.nonce.is_some());
len += usize::from(update.class.is_some());
});
self.system_contract_updates.iter().for_each(|(_, update)| {
len += update.storage.len();
});
len += self.declared_cairo_classes.len() + self.declared_sierra_classes.len();
len
}
}

impl From<StateUpdate> for StateUpdateData {
Expand Down
4 changes: 2 additions & 2 deletions crates/gateway-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,9 @@ mod tests {
mod add_transaction {
use std::collections::HashMap;

use pathfinder_common::class_definition::{EntryPointType, SelectorAndOffset};
use pathfinder_common::ContractAddress;
use starknet_gateway_types::request::add_transaction::CairoContractDefinition;
use starknet_gateway_types::request::contract::{EntryPointType, SelectorAndOffset};

use super::*;

Expand Down Expand Up @@ -739,8 +739,8 @@ mod tests {
}

mod declare {
use pathfinder_common::class_definition::SelectorAndFunctionIndex;
use starknet_gateway_types::request::add_transaction::SierraContractDefinition;
use starknet_gateway_types::request::contract::SelectorAndFunctionIndex;

use super::*;

Expand Down
20 changes: 11 additions & 9 deletions crates/gateway-types/src/class_hash.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use anyhow::{Context, Error, Result};
use pathfinder_common::class_definition::EntryPointType::*;
use pathfinder_common::{felt_bytes, ClassHash};
use pathfinder_crypto::hash::{HashChain, PoseidonHasher};
use pathfinder_crypto::Felt;
use serde::Serialize;
use sha3::Digest;

use crate::request::contract::EntryPointType;

#[derive(Debug, PartialEq)]
pub enum ComputedClassHash {
Cairo(ClassHash),
Expand Down Expand Up @@ -99,12 +98,15 @@ pub mod from_parts {
use std::collections::HashMap;

use anyhow::Result;
use pathfinder_common::class_definition::{
EntryPointType,
SelectorAndOffset,
SierraEntryPoints,
};
use pathfinder_common::ClassHash;
use pathfinder_crypto::Felt;

use super::json;
use crate::class_definition::SierraEntryPoints;
use crate::request::contract::{EntryPointType, SelectorAndOffset};

pub fn compute_cairo_class_hash(
abi: &[u8],
Expand Down Expand Up @@ -178,8 +180,6 @@ pub mod from_parts {
fn compute_cairo_class_hash(
mut contract_definition: json::CairoContractDefinition<'_>,
) -> Result<ClassHash> {
use EntryPointType::*;

// the other modification is handled by skipping if the attributes vec is empty
contract_definition.program.debug_info = None;

Expand Down Expand Up @@ -390,8 +390,6 @@ fn compute_cairo_class_hash(
fn compute_sierra_class_hash(
contract_definition: json::SierraContractDefinition<'_>,
) -> Result<ClassHash> {
use EntryPointType::*;

if contract_definition.contract_class_version != "0.1.0" {
anyhow::bail!("Unsupported Sierra class version");
}
Expand Down Expand Up @@ -541,7 +539,11 @@ mod json {
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};

use crate::request::contract::{EntryPointType, SelectorAndFunctionIndex, SelectorAndOffset};
use pathfinder_common::class_definition::{
EntryPointType,
SelectorAndFunctionIndex,
SelectorAndOffset,
};

pub enum ContractDefinition<'a> {
Cairo(CairoContractDefinition<'a>),
Expand Down
1 change: 0 additions & 1 deletion crates/gateway-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod class_definition;
pub mod class_hash;
pub mod error;
pub mod reply;
Expand Down
Loading

0 comments on commit d0ec6fb

Please sign in to comment.