diff --git a/src/rpc_transaction.rs b/src/rpc_transaction.rs index 3555613..dd9892e 100644 --- a/src/rpc_transaction.rs +++ b/src/rpc_transaction.rs @@ -20,34 +20,34 @@ use crate::transaction::{ #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(tag = "type")] #[serde(deny_unknown_fields)] -pub enum RPCTransaction { +pub enum RpcTransaction { #[serde(rename = "DECLARE")] - Declare(RPCDeclareTransaction), + Declare(RpcDeclareTransaction), #[serde(rename = "DEPLOY_ACCOUNT")] - DeployAccount(RPCDeployAccountTransaction), + DeployAccount(RpcDeployAccountTransaction), #[serde(rename = "INVOKE")] - Invoke(RPCInvokeTransaction), + Invoke(RpcInvokeTransaction), } macro_rules! implement_ref_getters { ($(($member_name:ident, $member_type:ty)), *) => { $(pub fn $member_name(&self) -> &$member_type { match self { - RPCTransaction::Declare( - RPCDeclareTransaction::V3(tx) + RpcTransaction::Declare( + RpcDeclareTransaction::V3(tx) ) => &tx.$member_name, - RPCTransaction::DeployAccount( - RPCDeployAccountTransaction::V3(tx) + RpcTransaction::DeployAccount( + RpcDeployAccountTransaction::V3(tx) ) => &tx.$member_name, - RPCTransaction::Invoke( - RPCInvokeTransaction::V3(tx) + RpcTransaction::Invoke( + RpcInvokeTransaction::V3(tx) ) => &tx.$member_name } })* }; } -impl RPCTransaction { +impl RpcTransaction { implement_ref_getters!( (nonce, Nonce), (resource_bounds, ResourceBoundsMapping), @@ -65,9 +65,9 @@ impl RPCTransaction { /// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(tag = "version")] -pub enum RPCDeclareTransaction { +pub enum RpcDeclareTransaction { #[serde(rename = "0x3")] - V3(RPCDeclareTransactionV3), + V3(RpcDeclareTransactionV3), } /// A RPC deploy account transaction. @@ -78,9 +78,9 @@ pub enum RPCDeclareTransaction { /// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json #[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] #[serde(tag = "version")] -pub enum RPCDeployAccountTransaction { +pub enum RpcDeployAccountTransaction { #[serde(rename = "0x3")] - V3(RPCDeployAccountTransactionV3), + V3(RpcDeployAccountTransactionV3), } /// A RPC invoke transaction. @@ -91,15 +91,15 @@ pub enum RPCDeployAccountTransaction { /// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json #[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] #[serde(tag = "version")] -pub enum RPCInvokeTransaction { +pub enum RpcInvokeTransaction { #[serde(rename = "0x3")] - V3(RPCInvokeTransactionV3), + V3(RpcInvokeTransactionV3), } /// A declare transaction of a Cairo-v1 contract class that can be added to Starknet through the /// RPC. #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] -pub struct RPCDeclareTransactionV3 { +pub struct RpcDeclareTransactionV3 { // TODO: Check with Shahak why we need to keep the DeclareType. // pub r#type: DeclareType, pub sender_address: ContractAddress, @@ -117,7 +117,7 @@ pub struct RPCDeclareTransactionV3 { /// A deploy account transaction that can be added to Starknet through the RPC. #[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -pub struct RPCDeployAccountTransactionV3 { +pub struct RpcDeployAccountTransactionV3 { pub signature: TransactionSignature, pub nonce: Nonce, pub class_hash: ClassHash, @@ -132,7 +132,7 @@ pub struct RPCDeployAccountTransactionV3 { /// An invoke account transaction that can be added to Starknet through the RPC. #[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] -pub struct RPCInvokeTransactionV3 { +pub struct RpcInvokeTransactionV3 { pub sender_address: ContractAddress, pub calldata: Calldata, pub signature: TransactionSignature, diff --git a/src/rpc_transaction_test.rs b/src/rpc_transaction_test.rs index 1eac74e..d288a5d 100644 --- a/src/rpc_transaction_test.rs +++ b/src/rpc_transaction_test.rs @@ -5,9 +5,9 @@ use starknet_types_core::felt::Felt; use crate::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey}; use crate::rpc_transaction::{ - ContractClass, DataAvailabilityMode, RPCDeclareTransaction, RPCDeclareTransactionV3, - RPCDeployAccountTransaction, RPCDeployAccountTransactionV3, RPCInvokeTransaction, - RPCInvokeTransactionV3, RPCTransaction, ResourceBoundsMapping, + ContractClass, DataAvailabilityMode, ResourceBoundsMapping, RpcDeclareTransaction, + RpcDeclareTransactionV3, RpcDeployAccountTransaction, RpcDeployAccountTransactionV3, + RpcInvokeTransaction, RpcInvokeTransactionV3, RpcTransaction, }; use crate::transaction::{ AccountDeploymentData, Calldata, ContractAddressSalt, PaymasterData, ResourceBounds, Tip, @@ -22,8 +22,8 @@ fn create_resource_bounds_for_testing() -> ResourceBoundsMapping { } } -fn create_declare_v3() -> RPCDeclareTransaction { - RPCDeclareTransaction::V3(RPCDeclareTransactionV3 { +fn create_declare_v3() -> RpcDeclareTransaction { + RpcDeclareTransaction::V3(RpcDeclareTransactionV3 { contract_class: ContractClass::default(), resource_bounds: create_resource_bounds_for_testing(), tip: Tip(1), @@ -38,8 +38,8 @@ fn create_declare_v3() -> RPCDeclareTransaction { }) } -fn create_deploy_account_v3() -> RPCDeployAccountTransaction { - RPCDeployAccountTransaction::V3(RPCDeployAccountTransactionV3 { +fn create_deploy_account_v3() -> RpcDeployAccountTransaction { + RpcDeployAccountTransaction::V3(RpcDeployAccountTransactionV3 { resource_bounds: create_resource_bounds_for_testing(), tip: Tip::default(), contract_address_salt: ContractAddressSalt(felt!("0x23")), @@ -53,8 +53,8 @@ fn create_deploy_account_v3() -> RPCDeployAccountTransaction { }) } -fn create_invoke_v3() -> RPCInvokeTransaction { - RPCInvokeTransaction::V3(RPCInvokeTransactionV3 { +fn create_invoke_v3() -> RpcInvokeTransaction { + RpcInvokeTransaction::V3(RpcInvokeTransactionV3 { resource_bounds: create_resource_bounds_for_testing(), tip: Tip(50), calldata: Calldata(Arc::new(vec![felt!("0x2000"), felt!("0x1000")])), @@ -68,13 +68,13 @@ fn create_invoke_v3() -> RPCInvokeTransaction { }) } -// We are testing the `RPCTransaction` serialization. Passing non-default values. +// We are testing the `RpcTransaction` serialization. Passing non-default values. #[rstest] -#[case(RPCTransaction::Declare(create_declare_v3()))] -#[case(RPCTransaction::DeployAccount(create_deploy_account_v3()))] -#[case(RPCTransaction::Invoke(create_invoke_v3()))] -fn test_rpc_transactions(#[case] tx: RPCTransaction) { +#[case(RpcTransaction::Declare(create_declare_v3()))] +#[case(RpcTransaction::DeployAccount(create_deploy_account_v3()))] +#[case(RpcTransaction::Invoke(create_invoke_v3()))] +fn test_rpc_transactions(#[case] tx: RpcTransaction) { let serialized = serde_json::to_string(&tx).unwrap(); - let deserialized: RPCTransaction = serde_json::from_str(&serialized).unwrap(); + let deserialized: RpcTransaction = serde_json::from_str(&serialized).unwrap(); assert_eq!(tx, deserialized); } diff --git a/src/transaction.rs b/src/transaction.rs index a7ab6b6..dde4b3d 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -953,7 +953,7 @@ pub struct ExecutionResources { pub gas_consumed: GasVector, } -#[derive(Hash, Debug, Deserialize, Serialize, Clone, Eq, PartialEq)] +#[derive(Clone, Debug, Deserialize, EnumIter, Eq, Hash, PartialEq, Serialize)] pub enum Builtin { #[serde(rename = "range_check_builtin_applications")] RangeCheck, @@ -978,3 +978,33 @@ pub enum Builtin { #[serde(rename = "range_check96_builtin")] RangeCheck96, } + +const RANGE_CHACK_BUILTIN_NAME: &str = "range_check"; +const PEDERSEN_BUILTIN_NAME: &str = "pedersen"; +const POSEIDON_BUILTIN_NAME: &str = "poseidon"; +const EC_OP_BUILTIN_NAME: &str = "ec_op"; +const ECDSA_BUILTIN_NAME: &str = "ecdsa"; +const BITWISE_BUILTIN_NAME: &str = "bitwise"; +const KECCAK_BUILTIN_NAME: &str = "keccak"; +const SEGMENT_ARENA_BUILTIN_NAME: &str = "segment_arena"; +const ADD_MOD_BUILTIN_NAME: &str = "add_mod"; +const MUL_MOD_BUILTIN_NAME: &str = "mul_mod"; +const RANGE_CHECK96_BUILTIN_NAME: &str = "range_check96"; + +impl Builtin { + pub fn name(&self) -> &'static str { + match self { + Builtin::RangeCheck => RANGE_CHACK_BUILTIN_NAME, + Builtin::Pedersen => PEDERSEN_BUILTIN_NAME, + Builtin::Poseidon => POSEIDON_BUILTIN_NAME, + Builtin::EcOp => EC_OP_BUILTIN_NAME, + Builtin::Ecdsa => ECDSA_BUILTIN_NAME, + Builtin::Bitwise => BITWISE_BUILTIN_NAME, + Builtin::Keccak => KECCAK_BUILTIN_NAME, + Builtin::SegmentArena => SEGMENT_ARENA_BUILTIN_NAME, + Builtin::AddMod => ADD_MOD_BUILTIN_NAME, + Builtin::MulMod => MUL_MOD_BUILTIN_NAME, + Builtin::RangeCheck96 => RANGE_CHECK96_BUILTIN_NAME, + } + } +}