Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(starknet_api): remove duplicate definition of contract class #2298

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use starknet_api::block::GasPrice;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::executable_transaction::AccountTransaction;
use starknet_api::execution_resources::GasAmount;
use starknet_api::rpc_transaction::{ContractClass, RpcTransaction};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::state::SierraContractClass;
use starknet_api::test_utils::declare::rpc_declare_tx;
use starknet_api::test_utils::deploy_account::rpc_deploy_account_tx;
use starknet_api::test_utils::invoke::{rpc_invoke_tx, InvokeTxArgs};
Expand Down Expand Up @@ -65,7 +66,7 @@ pub fn test_valid_resource_bounds() -> ValidResourceBounds {
}

/// Get the contract class used for testing.
pub fn contract_class() -> ContractClass {
pub fn contract_class() -> SierraContractClass {
env::set_current_dir(resolve_project_relative_path(TEST_FILES_FOLDER).unwrap())
.expect("Couldn't set working dir.");
let json_file_path = Path::new(CONTRACT_CLASS_FILE);
Expand Down
66 changes: 1 addition & 65 deletions crates/papyrus_protobuf/src/converters/rpc_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ mod rpc_transaction_test;

use prost::Message;
use starknet_api::rpc_transaction::{
ContractClass,
EntryPointByType,
RpcDeclareTransaction,
RpcDeclareTransactionV3,
RpcDeployAccountTransaction,
Expand All @@ -14,19 +12,17 @@ use starknet_api::rpc_transaction::{
RpcInvokeTransactionV3,
RpcTransaction,
};
use starknet_api::state::EntryPoint;
use starknet_api::transaction::fields::{AllResourceBounds, ValidResourceBounds};
use starknet_api::transaction::{
DeclareTransactionV3,
DeployAccountTransactionV3,
InvokeTransactionV3,
};
use starknet_types_core::felt::Felt;

use super::ProtobufConversionError;
use crate::auto_impl_into_and_try_from_vec_u8;
use crate::mempool::RpcTransactionWrapper;
use crate::protobuf::{self, Felt252};
use crate::protobuf::{self};

auto_impl_into_and_try_from_vec_u8!(RpcTransactionWrapper, protobuf::RpcTransaction);

Expand Down Expand Up @@ -330,63 +326,3 @@ impl From<AllResourceBounds> for protobuf::ResourceBounds {
ValidResourceBounds::AllResources(value).into()
}
}

impl TryFrom<protobuf::Cairo1Class> for ContractClass {
type Error = ProtobufConversionError;
fn try_from(value: protobuf::Cairo1Class) -> Result<Self, Self::Error> {
let sierra_program =
value.program.into_iter().map(Felt::try_from).collect::<Result<Vec<_>, _>>()?;
let contract_class_version = value.contract_class_version;
let entry_points = value.entry_points.ok_or(ProtobufConversionError::MissingField {
field_description: "Cairo1Class::entry_points_by_type",
})?;
let entry_points_by_type = EntryPointByType {
constructor: entry_points
.constructors
.into_iter()
.map(EntryPoint::try_from)
.collect::<Result<Vec<_>, _>>()?,
external: entry_points
.externals
.into_iter()
.map(EntryPoint::try_from)
.collect::<Result<Vec<_>, _>>()?,
l1handler: entry_points
.l1_handlers
.into_iter()
.map(EntryPoint::try_from)
.collect::<Result<Vec<_>, _>>()?,
};
let abi = value.abi;
Ok(Self { sierra_program, contract_class_version, entry_points_by_type, abi })
}
}

impl From<ContractClass> for protobuf::Cairo1Class {
fn from(value: ContractClass) -> Self {
let program = value.sierra_program.into_iter().map(Felt252::from).collect();
let contract_class_version = value.contract_class_version;
let entry_points = protobuf::Cairo1EntryPoints {
constructors: value
.entry_points_by_type
.constructor
.into_iter()
.map(protobuf::SierraEntryPoint::from)
.collect(),
externals: value
.entry_points_by_type
.external
.into_iter()
.map(protobuf::SierraEntryPoint::from)
.collect(),
l1_handlers: value
.entry_points_by_type
.l1handler
.into_iter()
.map(protobuf::SierraEntryPoint::from)
.collect(),
};
let abi = value.abi;
Self { program, contract_class_version, entry_points: Some(entry_points), abi }
}
}
9 changes: 1 addition & 8 deletions crates/papyrus_test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ use starknet_api::deprecated_contract_class::{
use starknet_api::execution_resources::{Builtin, ExecutionResources, GasAmount, GasVector};
use starknet_api::hash::{PoseidonHash, StarkHash};
use starknet_api::rpc_transaction::{
ContractClass as RpcContractClass,
EntryPointByType as RpcEntryPointByType,
EntryPointByType,
RpcDeclareTransaction,
Expand Down Expand Up @@ -750,12 +749,6 @@ auto_impl_get_test_instance! {
pub max_amount: GasAmount,
pub max_price_per_unit: GasPrice,
}
pub struct RpcContractClass {
pub sierra_program: Vec<Felt>,
pub contract_class_version: String,
pub entry_points_by_type: RpcEntryPointByType,
pub abi: String,
}
pub enum RpcTransaction {
Declare(RpcDeclareTransaction) = 0,
DeployAccount(RpcDeployAccountTransaction) = 1,
Expand All @@ -769,7 +762,7 @@ auto_impl_get_test_instance! {
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
pub contract_class: RpcContractClass,
pub contract_class: SierraContractClass,
pub compiled_class_hash: CompiledClassHash,
pub sender_address: ContractAddress,
pub nonce_data_availability_mode: DataAvailabilityMode,
Expand Down
14 changes: 2 additions & 12 deletions crates/starknet_api/src/rpc_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod rpc_transaction_test;
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use starknet_types_core::felt::Felt;

use crate::contract_class::EntryPointType;
use crate::core::{
Expand All @@ -16,7 +15,7 @@ use crate::core::{
Nonce,
};
use crate::data_availability::DataAvailabilityMode;
use crate::state::EntryPoint;
use crate::state::{EntryPoint, SierraContractClass};
use crate::transaction::fields::{
AccountDeploymentData,
AllResourceBounds,
Expand Down Expand Up @@ -180,7 +179,7 @@ pub struct RpcDeclareTransactionV3 {
pub compiled_class_hash: CompiledClassHash,
pub signature: TransactionSignature,
pub nonce: Nonce,
pub contract_class: ContractClass,
pub contract_class: SierraContractClass,
pub resource_bounds: AllResourceBounds,
pub tip: Tip,
pub paymaster_data: PaymasterData,
Expand Down Expand Up @@ -272,15 +271,6 @@ impl From<RpcInvokeTransactionV3> for InvokeTransactionV3 {
}
}

// The contract class in SN_API state doesn't have `contract_class_version`, not following the spec.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize, Hash)]
pub struct ContractClass {
pub sierra_program: Vec<Felt>,
pub contract_class_version: String,
pub entry_points_by_type: EntryPointByType,
pub abi: String,
}

// TODO(Aviv): remove duplication with sequencer/crates/papyrus_rpc/src/v0_8/state.rs
#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize, Hash)]
pub struct EntryPointByType {
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_api/src/rpc_transaction_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use crate::block::GasPrice;
use crate::core::CompiledClassHash;
use crate::execution_resources::GasAmount;
use crate::rpc_transaction::{
ContractClass,
DataAvailabilityMode,
RpcDeclareTransaction,
RpcDeclareTransactionV3,
RpcDeployAccountTransaction,
RpcDeployAccountTransactionV3,
RpcTransaction,
};
use crate::state::SierraContractClass;
use crate::test_utils::invoke::{rpc_invoke_tx, InvokeTxArgs};
use crate::transaction::fields::{
AccountDeploymentData,
Expand All @@ -40,7 +40,7 @@ fn create_resource_bounds_for_testing() -> AllResourceBounds {

fn create_declare_v3() -> RpcDeclareTransaction {
RpcDeclareTransaction::V3(RpcDeclareTransactionV3 {
contract_class: ContractClass::default(),
contract_class: SierraContractClass::default(),
resource_bounds: create_resource_bounds_for_testing(),
tip: Tip(1),
signature: TransactionSignature(vec![Felt::ONE, Felt::TWO]),
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl StorageKey {
impl_from_through_intermediate!(u128, StorageKey, u8, u16, u32, u64);

/// A contract class.
#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize, Hash)]
pub struct SierraContractClass {
pub sierra_program: Vec<Felt>,
pub contract_class_version: String,
Expand Down
10 changes: 3 additions & 7 deletions crates/starknet_api/src/test_utils/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ use crate::contract_class::ClassInfo;
use crate::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use crate::data_availability::DataAvailabilityMode;
use crate::executable_transaction::DeclareTransaction as ExecutableDeclareTransaction;
use crate::rpc_transaction::{
ContractClass,
RpcDeclareTransaction,
RpcDeclareTransactionV3,
RpcTransaction,
};
use crate::rpc_transaction::{RpcDeclareTransaction, RpcDeclareTransactionV3, RpcTransaction};
use crate::state::SierraContractClass;
use crate::transaction::fields::{
AccountDeploymentData,
Fee,
Expand Down Expand Up @@ -142,7 +138,7 @@ pub fn executable_declare_tx(

pub fn rpc_declare_tx(
declare_tx_args: DeclareTxArgs,
contract_class: ContractClass,
contract_class: SierraContractClass,
) -> RpcTransaction {
if declare_tx_args.version != TransactionVersion::THREE {
panic!("Unsupported transaction version: {:?}.", declare_tx_args.version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl StatelessTransactionValidator {

fn validate_class_length(
&self,
contract_class: &starknet_api::rpc_transaction::ContractClass,
contract_class: &starknet_api::state::SierraContractClass,
) -> StatelessTransactionValidatorResult<()> {
let contract_class_object_size = serde_json::to_string(&contract_class)
.expect("Unexpected error serializing contract class.")
Expand All @@ -254,7 +254,7 @@ impl StatelessTransactionValidator {

fn validate_entry_points_sorted_and_unique(
&self,
contract_class: &starknet_api::rpc_transaction::ContractClass,
contract_class: &starknet_api::state::SierraContractClass,
) -> StatelessTransactionValidatorResult<()> {
let is_sorted_unique = |entry_points: &[EntryPoint]| {
entry_points.windows(2).all(|pair| pair[0].selector < pair[1].selector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use assert_matches::assert_matches;
use rstest::rstest;
use starknet_api::core::{EntryPointSelector, L2_ADDRESS_UPPER_BOUND};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::rpc_transaction::{ContractClass, EntryPointByType};
use starknet_api::state::EntryPoint;
use starknet_api::rpc_transaction::EntryPointByType;
use starknet_api::state::{EntryPoint, SierraContractClass};
use starknet_api::test_utils::declare::rpc_declare_tx;
use starknet_api::transaction::fields::{
AccountDeploymentData,
Expand Down Expand Up @@ -359,7 +359,7 @@ fn test_declare_sierra_version_failure(
let tx_validator =
StatelessTransactionValidator { config: DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone() };

let contract_class = ContractClass { sierra_program, ..Default::default() };
let contract_class = SierraContractClass { sierra_program, ..Default::default() };
let tx = rpc_declare_tx(declare_tx_args!(), contract_class);

assert_eq!(tx_validator.validate(&tx).unwrap_err(), expected_error);
Expand All @@ -379,7 +379,7 @@ fn test_declare_sierra_version_sucsses(#[case] sierra_program: Vec<Felt>) {
let tx_validator =
StatelessTransactionValidator { config: DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone() };

let contract_class = ContractClass { sierra_program, ..Default::default() };
let contract_class = SierraContractClass { sierra_program, ..Default::default() };
let tx = rpc_declare_tx(declare_tx_args!(), contract_class);

assert_matches!(tx_validator.validate(&tx), Ok(()));
Expand All @@ -394,7 +394,7 @@ fn test_declare_contract_class_size_too_long() {
..*DEFAULT_VALIDATOR_CONFIG_FOR_TESTING
},
};
let contract_class = ContractClass {
let contract_class = SierraContractClass {
sierra_program: create_sierra_program(&MIN_SIERRA_VERSION),
..Default::default()
};
Expand Down Expand Up @@ -458,7 +458,7 @@ fn test_declare_entry_points_not_sorted_by_selector(
let tx_validator =
StatelessTransactionValidator { config: DEFAULT_VALIDATOR_CONFIG_FOR_TESTING.clone() };

let contract_class = ContractClass {
let contract_class = SierraContractClass {
sierra_program: create_sierra_program(&MIN_SIERRA_VERSION),
entry_points_by_type: EntryPointByType {
constructor: entry_points.clone(),
Expand All @@ -471,7 +471,7 @@ fn test_declare_entry_points_not_sorted_by_selector(

assert_eq!(tx_validator.validate(&tx), expected);

let contract_class = ContractClass {
let contract_class = SierraContractClass {
sierra_program: create_sierra_program(&MIN_SIERRA_VERSION),
entry_points_by_type: EntryPointByType {
constructor: vec![],
Expand All @@ -484,7 +484,7 @@ fn test_declare_entry_points_not_sorted_by_selector(

assert_eq!(tx_validator.validate(&tx), expected);

let contract_class = ContractClass {
let contract_class = SierraContractClass {
sierra_program: create_sierra_program(&MIN_SIERRA_VERSION),
entry_points_by_type: EntryPointByType {
constructor: vec![],
Expand Down
5 changes: 3 additions & 2 deletions crates/starknet_gateway/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use starknet_api::block::GasPrice;
use starknet_api::core::ContractAddress;
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::execution_resources::GasAmount;
use starknet_api::rpc_transaction::{ContractClass, RpcTransaction};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::state::SierraContractClass;
use starknet_api::test_utils::declare::{rpc_declare_tx, TEST_SENDER_ADDRESS};
use starknet_api::test_utils::deploy_account::rpc_deploy_account_tx;
use starknet_api::test_utils::invoke::rpc_invoke_tx;
Expand Down Expand Up @@ -89,7 +90,7 @@ pub fn rpc_tx_for_testing(
match tx_type {
TransactionType::Declare => {
// Minimal contract class.
let contract_class = ContractClass {
let contract_class = SierraContractClass {
sierra_program: vec![
// Sierra Version ID.
felt!(1_u32),
Expand Down
10 changes: 5 additions & 5 deletions crates/starknet_sierra_compile/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use cairo_lang_starknet_classes::contract_class::{
ContractEntryPoints as CairoLangContractEntryPoints,
};
use cairo_lang_utils::bigint::BigUintAsHex;
use starknet_api::rpc_transaction::{
ContractClass as RpcContractClass,
EntryPointByType as StarknetApiEntryPointByType,
use starknet_api::rpc_transaction::EntryPointByType as StarknetApiEntryPointByType;
use starknet_api::state::{
EntryPoint as StarknetApiEntryPoint,
SierraContractClass as StarknetApiContractClass,
};
use starknet_api::state::EntryPoint as StarknetApiEntryPoint;
use starknet_types_core::felt::Felt;

/// Retruns a [`CairoLangContractClass`] struct ready for Sierra to Casm compilation. Note the `abi`
/// field is None as it is not relevant for the compilation.
pub fn into_contract_class_for_compilation(
rpc_contract_class: &RpcContractClass,
rpc_contract_class: &StarknetApiContractClass,
) -> CairoLangContractClass {
let sierra_program =
sierra_program_as_felts_to_big_uint_as_hex(&rpc_contract_class.sierra_program);
Expand Down
Loading