From 1c6269442cd502c9dbf39777ac5be3e8c1d390b7 Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Mon, 1 Jul 2024 11:21:58 +0300 Subject: [PATCH 1/4] feat: add name method to builtin enum (#288) --- src/transaction.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/transaction.rs b/src/transaction.rs index d4a1d2af..a2190a1d 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -944,3 +944,27 @@ pub enum Builtin { #[serde(rename = "segment_arena_builtin")] SegmentArena, } + +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"; + +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, + } + } +} From f77bb3b407320e82aa6f52619011a573b7cc9c30 Mon Sep 17 00:00:00 2001 From: mohammad-starkware <130282237+MohammadNassar1@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:10:54 +0300 Subject: [PATCH 2/4] refactor: use small letters for rpc transaction enum (#285) --- src/rpc_transaction.rs | 40 ++++++++++++++++++------------------- src/rpc_transaction_test.rs | 30 ++++++++++++++-------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/rpc_transaction.rs b/src/rpc_transaction.rs index f1592953..b0639082 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!( (resource_bounds, ResourceBoundsMapping), (signature, TransactionSignature) @@ -63,9 +63,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. @@ -76,9 +76,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. @@ -89,15 +89,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, @@ -115,7 +115,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, @@ -130,7 +130,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 3d14ffb4..c0931af2 100644 --- a/src/rpc_transaction_test.rs +++ b/src/rpc_transaction_test.rs @@ -5,9 +5,9 @@ use rstest::rstest; use crate::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey}; use crate::hash::{StarkFelt, StarkHash}; 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(stark_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![stark_felt!("0x2000"), stark_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); } From 2f45685c6a697396d8ef78b9d4a10b238f03d71e Mon Sep 17 00:00:00 2001 From: Arnon Hod Date: Wed, 3 Jul 2024 11:32:40 +0300 Subject: [PATCH 3/4] chore: derive enum-iter for builtin enum (#290) --- src/transaction.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction.rs b/src/transaction.rs index a2190a1d..7689302c 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -925,7 +925,7 @@ pub struct ExecutionResources { pub da_l1_data_gas_consumed: u64, } -#[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, From a8bc274b10706b664a7cf4ca5fe1162620fe9cec Mon Sep 17 00:00:00 2001 From: Elin Tulchinsky Date: Sat, 13 Jul 2024 15:08:00 +0300 Subject: [PATCH 4/4] Add new builtins to `name` method. --- src/transaction.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/transaction.rs b/src/transaction.rs index 4a106ff6..dde4b3dc 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -987,6 +987,9 @@ 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 { @@ -999,6 +1002,9 @@ impl Builtin { 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, } } }