Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main-mempool' into elin/merge-ma…
Browse files Browse the repository at this point in the history
…in-mempool-into-main
  • Loading branch information
elintul committed Jul 13, 2024
2 parents a6ae471 + 2f45685 commit eedc487
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
40 changes: 20 additions & 20 deletions src/rpc_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
30 changes: 15 additions & 15 deletions src/rpc_transaction_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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")),
Expand All @@ -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")])),
Expand All @@ -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);
}
26 changes: 25 additions & 1 deletion src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -978,3 +978,27 @@ 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";

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,
}
}
}

0 comments on commit eedc487

Please sign in to comment.