Skip to content

Commit

Permalink
test(protobuf): add auto impl for statediffchunk
Browse files Browse the repository at this point in the history
  • Loading branch information
eitanm-starkware committed Jun 17, 2024
1 parent b9c9593 commit b0f2ac3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/papyrus_common/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct ReplacedClass {
pub class_hash: ClassHash,
}

// TODO: move to used crate
pub fn create_random_state_diff(rng: &mut impl RngCore) -> ThinStateDiff {
let contract0 = ContractAddress::from(rng.next_u64());
let contract1 = ContractAddress::from(rng.next_u64());
Expand Down
8 changes: 8 additions & 0 deletions crates/papyrus_protobuf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ edition.workspace = true
repository.workspace = true
license-file.workspace = true

[features]
testing = ["rand", "rand_chacha", "test_utils"]

[dependencies]
bytes.workspace = true
indexmap.workspace = true
lazy_static.workspace = true
primitive-types.workspace = true
prost.workspace = true
prost-types.workspace = true
rand = { workspace = true, optional = true }
rand_chacha = { workspace = true, optional = true }
starknet_api.workspace = true
starknet-types-core.workspace = true
test_utils = { path = "../test_utils", optional = true }
thiserror.workspace = true
papyrus_common = { path = "../papyrus_common", version = "0.4.0-dev.2" }

[dev-dependencies]
rand.workspace = true
rand_chacha.workspace = true
test_utils = { path = "../test_utils" }

[build-dependencies]
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_protobuf/src/converters/state_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl TryFrom<protobuf::StateDiffsResponse> for DataOrFin<ThinStateDiff> {
}
}
auto_impl_try_from_vec_u8!(DataOrFin<ThinStateDiff>, protobuf::StateDiffsResponse);
auto_impl_try_from_vec_u8!(DataOrFin<StateDiffChunk>, protobuf::StateDiffsResponse);

impl TryFrom<protobuf::StateDiffsResponse> for DataOrFin<StateDiffChunk> {
type Error = ProtobufConversionError;
Expand Down
36 changes: 33 additions & 3 deletions crates/papyrus_protobuf/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use starknet_api::block::{BlockHash, BlockHeader, BlockNumber, BlockSignature};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;
#[cfg(any(feature = "testing", test))]
use test_utils::{auto_impl_get_test_instance, get_number_of_variants, GetTestInstance};

#[derive(Debug, PartialEq, Eq, Clone, Copy, Default, Hash)]
pub enum Direction {
Expand Down Expand Up @@ -49,7 +51,7 @@ pub struct SignedBlockHeader {
pub signatures: Vec<BlockSignature>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ContractDiff {
pub contract_address: ContractAddress,
// Has value only if the contract was deployed or replaced in this block.
Expand All @@ -59,13 +61,13 @@ pub struct ContractDiff {
pub storage_diffs: IndexMap<StorageKey, Felt>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DeclaredClass {
pub class_hash: ClassHash,
pub compiled_class_hash: CompiledClassHash,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct DeprecatedDeclaredClass {
pub class_hash: ClassHash,
}
Expand All @@ -76,3 +78,31 @@ pub enum StateDiffChunk {
DeclaredClass(DeclaredClass),
DeprecatedDeclaredClass(DeprecatedDeclaredClass),
}

impl Default for StateDiffChunk {
fn default() -> Self {
Self::ContractDiff(ContractDiff::default())
}
}

#[cfg(any(feature = "testing", test))]
auto_impl_get_test_instance! {
pub enum StateDiffChunk{
ContractDiff(ContractDiff) = 0,
DeclaredClass(DeclaredClass) = 1,
DeprecatedDeclaredClass(DeprecatedDeclaredClass) = 2,
}
pub struct ContractDiff{
pub contract_address: ContractAddress,
pub class_hash: Option<ClassHash>,
pub nonce: Option<Nonce>,
pub storage_diffs: IndexMap<StorageKey, Felt>,
}
pub struct DeclaredClass {
pub class_hash: ClassHash,
pub compiled_class_hash: CompiledClassHash,
}
pub struct DeprecatedDeclaredClass {
pub class_hash: ClassHash,
}
}

0 comments on commit b0f2ac3

Please sign in to comment.