From d335ee54d2805de0500b835398b07619d699e771 Mon Sep 17 00:00:00 2001 From: ShahakShama <70578257+ShahakShama@users.noreply.github.com> Date: Sun, 24 Dec 2023 16:54:07 +0200 Subject: [PATCH] chore(JSON-RPC): merge rpc v0_5 into v0_6 (#1545) * chore(JSON-RPC): merge rpc v0_5 into v0_6 * chore(JSON-RPC): support only query --- .../resources/V0_6/starknet_write_api.json | 6 +- crates/papyrus_rpc/src/v0_6/api/api_impl.rs | 8 +- crates/papyrus_rpc/src/v0_6/api/mod.rs | 6 +- crates/papyrus_rpc/src/v0_6/api/test.rs | 29 +++- crates/papyrus_rpc/src/v0_6/execution_test.rs | 13 +- crates/papyrus_rpc/src/v0_6/transaction.rs | 161 +++++++++++++----- .../papyrus_rpc/src/v0_6/transaction_test.rs | 22 ++- 7 files changed, 173 insertions(+), 72 deletions(-) diff --git a/crates/papyrus_rpc/resources/V0_6/starknet_write_api.json b/crates/papyrus_rpc/resources/V0_6/starknet_write_api.json index 3ae61b2a73..4a3f229e89 100644 --- a/crates/papyrus_rpc/resources/V0_6/starknet_write_api.json +++ b/crates/papyrus_rpc/resources/V0_6/starknet_write_api.json @@ -223,13 +223,13 @@ "$ref": "./starknet_api_openrpc.json#/components/schemas/TXN_HASH" }, "BROADCASTED_INVOKE_TXN": { - "$ref": "./api/starknet_api_openrpc.json#/components/schemas/BROADCASTED_INVOKE_TXN" + "$ref": "./starknet_api_openrpc.json#/components/schemas/BROADCASTED_INVOKE_TXN" }, "BROADCASTED_DECLARE_TXN": { - "$ref": "./api/starknet_api_openrpc.json#/components/schemas/BROADCASTED_DECLARE_TXN" + "$ref": "./starknet_api_openrpc.json#/components/schemas/BROADCASTED_DECLARE_TXN" }, "BROADCASTED_DEPLOY_ACCOUNT_TXN": { - "$ref": "./api/starknet_api_openrpc.json#/components/schemas/BROADCASTED_DEPLOY_ACCOUNT_TXN" + "$ref": "./starknet_api_openrpc.json#/components/schemas/BROADCASTED_DEPLOY_ACCOUNT_TXN" }, "FUNCTION_CALL": { "$ref": "./starknet_api_openrpc.json#/components/schemas/FUNCTION_CALL" diff --git a/crates/papyrus_rpc/src/v0_6/api/api_impl.rs b/crates/papyrus_rpc/src/v0_6/api/api_impl.rs index 2be79c3990..79bfc2f73e 100644 --- a/crates/papyrus_rpc/src/v0_6/api/api_impl.rs +++ b/crates/papyrus_rpc/src/v0_6/api/api_impl.rs @@ -75,10 +75,8 @@ use super::super::state::{AcceptedStateUpdate, PendingStateUpdate, StateUpdate}; use super::super::transaction::{ get_block_tx_hashes_by_number, get_block_txs_by_number, - DeployAccountTransaction, Event, GeneralTransactionReceipt, - InvokeTransaction, L1HandlerMsgHash, MessageFromL1, PendingTransactionFinalityStatus, @@ -90,6 +88,8 @@ use super::super::transaction::{ TransactionStatus, TransactionWithHash, Transactions, + TypedDeployAccountTransaction, + TypedInvokeTransaction, }; use super::super::write_api_error::{ starknet_error_to_declare_error, @@ -924,7 +924,7 @@ impl JsonRpcServer for JsonRpcServerV0_6Impl { #[instrument(skip(self), level = "debug", err, ret)] async fn add_invoke_transaction( &self, - invoke_transaction: InvokeTransaction, + invoke_transaction: TypedInvokeTransaction, ) -> RpcResult { let result = self.writer_client.add_invoke_transaction(&invoke_transaction.into()).await; match result { @@ -939,7 +939,7 @@ impl JsonRpcServer for JsonRpcServerV0_6Impl { #[instrument(skip(self), level = "debug", err, ret)] async fn add_deploy_account_transaction( &self, - deploy_account_transaction: DeployAccountTransaction, + deploy_account_transaction: TypedDeployAccountTransaction, ) -> RpcResult { let result = self .writer_client diff --git a/crates/papyrus_rpc/src/v0_6/api/mod.rs b/crates/papyrus_rpc/src/v0_6/api/mod.rs index aa72726ece..6f70ed4987 100644 --- a/crates/papyrus_rpc/src/v0_6/api/mod.rs +++ b/crates/papyrus_rpc/src/v0_6/api/mod.rs @@ -52,6 +52,8 @@ use super::transaction::{ MessageFromL1, TransactionStatus, TransactionWithHash, + TypedDeployAccountTransaction, + TypedInvokeTransaction, }; use super::write_api_result::{AddDeclareOkResult, AddDeployAccountOkResult, AddInvokeOkResult}; use crate::api::{BlockId, CallRequest}; @@ -185,14 +187,14 @@ pub trait JsonRpc { #[method(name = "addInvokeTransaction")] async fn add_invoke_transaction( &self, - invoke_transaction: InvokeTransaction, + invoke_transaction: TypedInvokeTransaction, ) -> RpcResult; /// Submits a new deploy account transaction to be added to the chain. #[method(name = "addDeployAccountTransaction")] async fn add_deploy_account_transaction( &self, - deploy_account_transaction: DeployAccountTransaction, + deploy_account_transaction: TypedDeployAccountTransaction, ) -> RpcResult; /// Submits a new declare transaction to be added to the chain. diff --git a/crates/papyrus_rpc/src/v0_6/api/test.rs b/crates/papyrus_rpc/src/v0_6/api/test.rs index b4d9ce5ad4..367218f17b 100644 --- a/crates/papyrus_rpc/src/v0_6/api/test.rs +++ b/crates/papyrus_rpc/src/v0_6/api/test.rs @@ -87,6 +87,7 @@ use starknet_client::writer::{MockStarknetWriter, WriterClientError, WriterClien use starknet_client::ClientError; use test_utils::{ auto_impl_get_test_instance, + get_number_of_variants, get_rng, get_test_block, get_test_body, @@ -143,6 +144,8 @@ use super::super::transaction::{ TransactionStatus, TransactionWithHash, Transactions, + TypedDeployAccountTransaction, + TypedInvokeTransaction, }; use super::super::write_api_result::{ AddDeclareOkResult, @@ -3320,14 +3323,15 @@ where // https://github.com/rust-lang/rfcs/blob/master/text/2289-associated-type-bounds.md <::ClientTransaction as TryFrom>::Error: Debug, { - type Transaction: GetTestInstance + Serialize + Clone + Send + Debug; + type Transaction: GetTestInstance + Serialize + Clone + Send + Sync + 'static + Debug; type ClientTransaction: TryFrom + Send + Debug; type Response: From + for<'de> Deserialize<'de> + Eq + Debug + Clone - + Send; + + Send + + Sync; type ClientResponse: GetTestInstance + Clone + Send; const METHOD_NAME: &'static str; @@ -3358,8 +3362,15 @@ where None, None, ); - let resp = module.call::<_, Self::Response>(Self::METHOD_NAME, [tx]).await.unwrap(); - assert_eq!(resp, expected_resp); + call_api_then_assert_and_validate_schema_for_result( + &module, + Self::METHOD_NAME, + vec![Box::new(tx)], + &VERSION, + SpecFile::WriteApi, + &expected_resp, + ) + .await; } async fn test_internal_error() { @@ -3460,7 +3471,7 @@ where struct AddInvokeTest {} impl AddTransactionTest for AddInvokeTest { - type Transaction = InvokeTransaction; + type Transaction = TypedInvokeTransaction; type ClientTransaction = ClientInvokeTransaction; type Response = AddInvokeOkResult; type ClientResponse = InvokeResponse; @@ -3482,7 +3493,7 @@ impl AddTransactionTest for AddInvokeTest { struct AddDeployAccountTest {} impl AddTransactionTest for AddDeployAccountTest { - type Transaction = DeployAccountTransaction; + type Transaction = TypedDeployAccountTransaction; type ClientTransaction = ClientDeployAccountTransaction; type Response = AddDeployAccountOkResult; type ClientResponse = DeployAccountResponse; @@ -3646,4 +3657,10 @@ auto_impl_get_test_instance! { pub price_in_wei: GasPrice, pub price_in_fri: GasPrice, } + pub enum TypedInvokeTransaction { + Invoke(InvokeTransaction) = 0, + } + pub enum TypedDeployAccountTransaction { + DeployAccount(DeployAccountTransaction) = 0, + } } diff --git a/crates/papyrus_rpc/src/v0_6/execution_test.rs b/crates/papyrus_rpc/src/v0_6/execution_test.rs index 5b128f32ff..14419f83b1 100644 --- a/crates/papyrus_rpc/src/v0_6/execution_test.rs +++ b/crates/papyrus_rpc/src/v0_6/execution_test.rs @@ -89,6 +89,7 @@ use super::transaction::{ InvokeTransaction, InvokeTransactionV1, MessageFromL1, + TransactionVersion1, }; use crate::api::{BlockHashOrNumber, BlockId, CallRequest, Tag}; use crate::test_utils::{ @@ -317,7 +318,7 @@ async fn call_estimate_fee() { let invoke = BroadcastedTransaction::Invoke(InvokeTransaction::Version1(InvokeTransactionV1 { max_fee: Fee(1000000 * GAS_PRICE.0), - version: TransactionVersion::ONE, + version: TransactionVersion1::Version1, sender_address: account_address, calldata: calldata![ *DEPRECATED_CONTRACT_ADDRESS.0.key(), // Contract address. @@ -376,7 +377,7 @@ async fn call_estimate_fee() { let non_existent_entry_point = BroadcastedTransaction::Invoke(InvokeTransaction::Version1(InvokeTransactionV1 { max_fee: Fee(1000000 * GAS_PRICE.0), - version: TransactionVersion::ONE, + version: TransactionVersion1::Version1, sender_address: account_address, calldata: calldata![ *DEPRECATED_CONTRACT_ADDRESS.0.key(), // Contract address. @@ -428,7 +429,7 @@ async fn pending_call_estimate_fee() { let invoke = BroadcastedTransaction::Invoke(InvokeTransaction::Version1(InvokeTransactionV1 { max_fee: Fee(1000000 * GAS_PRICE.0), - version: TransactionVersion::ONE, + version: TransactionVersion1::Version1, sender_address: account_address, calldata: calldata![ *DEPRECATED_CONTRACT_ADDRESS.0.key(), // Contract address. @@ -491,7 +492,7 @@ async fn test_call_simulate( ) { let mut invoke_v1 = InvokeTransactionV1 { max_fee: Fee(1000000 * GAS_PRICE.0), - version: TransactionVersion::ONE, + version: TransactionVersion1::Version1, sender_address: *ACCOUNT_ADDRESS, calldata: calldata![ *DEPRECATED_CONTRACT_ADDRESS.0.key(), // Contract address. @@ -572,7 +573,7 @@ async fn call_simulate_skip_validate() { let invoke = BroadcastedTransaction::Invoke(InvokeTransaction::Version1(InvokeTransactionV1 { max_fee: Fee(1000000 * GAS_PRICE.0), - version: TransactionVersion::ONE, + version: TransactionVersion1::Version1, sender_address: *ACCOUNT_ADDRESS, calldata: calldata![ *DEPRECATED_CONTRACT_ADDRESS.0.key(), // Contract address. @@ -620,7 +621,7 @@ async fn call_simulate_skip_fee_charge() { let invoke = BroadcastedTransaction::Invoke(InvokeTransaction::Version1(InvokeTransactionV1 { max_fee: Fee(1000000 * GAS_PRICE.0), - version: TransactionVersion::ONE, + version: TransactionVersion1::Version1, sender_address: *ACCOUNT_ADDRESS, calldata: calldata![ *DEPRECATED_CONTRACT_ADDRESS.0.key(), // Contract address. diff --git a/crates/papyrus_rpc/src/v0_6/transaction.rs b/crates/papyrus_rpc/src/v0_6/transaction.rs index b8666e402b..18ec83a50d 100644 --- a/crates/papyrus_rpc/src/v0_6/transaction.rs +++ b/crates/papyrus_rpc/src/v0_6/transaction.rs @@ -9,7 +9,6 @@ use std::sync::Arc; use ethers::core::abi::{encode_packed, Token}; use ethers::core::utils::keccak256; use jsonrpsee::types::ErrorObjectOwned; -use lazy_static::lazy_static; use papyrus_execution::objects::PriceUnit; use papyrus_storage::body::events::ThinTransactionOutput; use papyrus_storage::body::BodyStorageReader; @@ -50,11 +49,48 @@ use starknet_client::writer::objects::transaction as client_transaction; use super::error::BLOCK_NOT_FOUND; use crate::internal_server_error; -lazy_static! { - static ref TX_V0: TransactionVersion = TransactionVersion::ZERO; - static ref TX_V1: TransactionVersion = TransactionVersion::ONE; - static ref TX_V2: TransactionVersion = TransactionVersion::TWO; - static ref TX_V3: TransactionVersion = TransactionVersion::THREE; +#[derive( + Debug, Deserialize, Serialize, Default, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord, +)] +pub enum TransactionVersion0 { + #[serde(rename = "0x0")] + #[default] + Version0, + #[serde(rename = "0x100000000000000000000000000000000")] + Version0OnlyQuery, +} + +#[derive( + Debug, Deserialize, Serialize, Default, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord, +)] +pub enum TransactionVersion1 { + #[serde(rename = "0x1")] + #[default] + Version1, + #[serde(rename = "0x100000000000000000000000000000001")] + Version1OnlyQuery, +} + +#[derive( + Debug, Deserialize, Serialize, Default, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord, +)] +pub enum TransactionVersion2 { + #[serde(rename = "0x2")] + #[default] + Version2, + #[serde(rename = "0x100000000000000000000000000000002")] + Version2OnlyQuery, +} + +#[derive( + Debug, Deserialize, Serialize, Default, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord, +)] +pub enum TransactionVersion3 { + #[serde(rename = "0x3")] + #[default] + Version3, + #[serde(rename = "0x100000000000000000000000000000003")] + Version3OnlyQuery, } #[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] @@ -66,12 +102,22 @@ pub enum Transactions { #[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] #[serde(deny_unknown_fields)] -pub struct DeclareTransactionV0V1 { +pub struct DeclareTransactionV0 { + pub class_hash: ClassHash, + pub sender_address: ContractAddress, + pub nonce: Nonce, + pub max_fee: Fee, + pub version: TransactionVersion0, + pub signature: TransactionSignature, +} +#[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] +#[serde(deny_unknown_fields)] +pub struct DeclareTransactionV1 { pub class_hash: ClassHash, pub sender_address: ContractAddress, pub nonce: Nonce, pub max_fee: Fee, - pub version: TransactionVersion, + pub version: TransactionVersion1, pub signature: TransactionSignature, } #[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] @@ -81,7 +127,7 @@ pub struct DeclareTransactionV2 { pub sender_address: ContractAddress, pub nonce: Nonce, pub max_fee: Fee, - pub version: TransactionVersion, + pub version: TransactionVersion2, pub signature: TransactionSignature, } @@ -93,7 +139,7 @@ impl From for DeclareTransactio sender_address: tx.sender_address, nonce: tx.nonce, max_fee: tx.max_fee, - version: *TX_V2, + version: TransactionVersion2::Version2, signature: tx.signature, } } @@ -134,7 +180,7 @@ pub struct DeclareTransactionV3 { pub fee_data_availability_mode: DataAvailabilityMode, pub paymaster_data: PaymasterData, pub account_deployment_data: AccountDeploymentData, - pub version: TransactionVersion, + pub version: TransactionVersion3, } impl From for DeclareTransactionV3 { @@ -151,7 +197,7 @@ impl From for DeclareTransactio fee_data_availability_mode: tx.fee_data_availability_mode, paymaster_data: tx.paymaster_data, account_deployment_data: tx.account_deployment_data, - version: *TX_V3, + version: TransactionVersion3::Version3, } } } @@ -159,25 +205,12 @@ impl From for DeclareTransactio #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, PartialOrd, Ord)] #[serde(untagged)] pub enum DeclareTransaction { - #[serde(deserialize_with = "declare_v0_deserialize")] - Version0(DeclareTransactionV0V1), - Version1(DeclareTransactionV0V1), + Version0(DeclareTransactionV0), + Version1(DeclareTransactionV1), Version2(DeclareTransactionV2), Version3(DeclareTransactionV3), } -fn declare_v0_deserialize<'de, D>(deserializer: D) -> Result -where - D: Deserializer<'de>, -{ - let v0v1: DeclareTransactionV0V1 = Deserialize::deserialize(deserializer)?; - if v0v1.version == *TX_V0 { - Ok(v0v1) - } else { - Err(serde::de::Error::custom("Invalid version value")) - } -} - #[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] pub struct DeployAccountTransactionV1 { pub max_fee: Fee, @@ -186,7 +219,7 @@ pub struct DeployAccountTransactionV1 { pub class_hash: ClassHash, pub contract_address_salt: ContractAddressSalt, pub constructor_calldata: Calldata, - pub version: TransactionVersion, + pub version: TransactionVersion1, } #[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] @@ -196,7 +229,7 @@ pub struct DeployAccountTransactionV3 { pub class_hash: ClassHash, pub contract_address_salt: ContractAddressSalt, pub constructor_calldata: Calldata, - pub version: TransactionVersion, + pub version: TransactionVersion3, pub resource_bounds: ResourceBoundsMapping, pub tip: Tip, pub paymaster_data: PaymasterData, @@ -234,7 +267,7 @@ impl TryFrom for DeployAcco class_hash, contract_address_salt, constructor_calldata, - version: *TX_V1, + version: TransactionVersion1::Version1, })), starknet_api::transaction::DeployAccountTransaction::V3( starknet_api::transaction::DeployAccountTransactionV3 { @@ -255,7 +288,7 @@ impl TryFrom for DeployAcco class_hash, contract_address_salt, constructor_calldata, - version: *TX_V3, + version: TransactionVersion3::Version3, resource_bounds: resource_bounds.into(), tip, nonce_data_availability_mode, @@ -277,7 +310,7 @@ impl From for client_transaction::DeployAccountTransac nonce: deploy_account_tx.nonce, max_fee: deploy_account_tx.max_fee, signature: deploy_account_tx.signature, - version: deploy_account_tx.version, + version: TransactionVersion::ONE, r#type: client_transaction::DeployAccountType::DeployAccount, }) } @@ -288,7 +321,7 @@ impl From for client_transaction::DeployAccountTransac constructor_calldata: deploy_account_tx.constructor_calldata, nonce: deploy_account_tx.nonce, signature: deploy_account_tx.signature, - version: deploy_account_tx.version, + version: TransactionVersion::THREE, resource_bounds: deploy_account_tx.resource_bounds.into(), tip: deploy_account_tx.tip, nonce_data_availability_mode: @@ -306,7 +339,7 @@ impl From for client_transaction::DeployAccountTransac #[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] pub struct InvokeTransactionV0 { pub max_fee: Fee, - pub version: TransactionVersion, + pub version: TransactionVersion0, pub signature: TransactionSignature, pub contract_address: ContractAddress, pub entry_point_selector: EntryPointSelector, @@ -317,7 +350,7 @@ impl From for client_transaction::InvokeTransaction { fn from(tx: InvokeTransactionV0) -> Self { Self::InvokeV0(client_transaction::InvokeV0Transaction { max_fee: tx.max_fee, - version: tx.version, + version: TransactionVersion::ZERO, signature: tx.signature, contract_address: tx.contract_address, entry_point_selector: tx.entry_point_selector, @@ -330,7 +363,7 @@ impl From for client_transaction::InvokeTransaction { #[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] pub struct InvokeTransactionV1 { pub max_fee: Fee, - pub version: TransactionVersion, + pub version: TransactionVersion1, pub signature: TransactionSignature, pub nonce: Nonce, pub sender_address: ContractAddress, @@ -341,7 +374,7 @@ impl From for client_transaction::InvokeTransaction { fn from(tx: InvokeTransactionV1) -> Self { Self::InvokeV1(client_transaction::InvokeV1Transaction { max_fee: tx.max_fee, - version: tx.version, + version: TransactionVersion::ONE, signature: tx.signature, nonce: tx.nonce, sender_address: tx.sender_address, @@ -355,7 +388,7 @@ impl From for client_transaction::InvokeTransaction { pub struct InvokeTransactionV3 { pub sender_address: ContractAddress, pub calldata: Calldata, - pub version: TransactionVersion, + pub version: TransactionVersion3, pub signature: TransactionSignature, pub nonce: Nonce, pub resource_bounds: ResourceBoundsMapping, @@ -371,7 +404,7 @@ impl From for client_transaction::InvokeTransaction { Self::InvokeV3(client_transaction::InvokeV3Transaction { sender_address: tx.sender_address, calldata: tx.calldata, - version: tx.version, + version: TransactionVersion::THREE, signature: tx.signature, nonce: tx.nonce, resource_bounds: tx.resource_bounds.into(), @@ -419,7 +452,7 @@ impl TryFrom for InvokeTransaction }, ) => Ok(Self::Version0(InvokeTransactionV0 { max_fee, - version: *TX_V0, + version: TransactionVersion0::Version0, signature, contract_address, entry_point_selector, @@ -435,7 +468,7 @@ impl TryFrom for InvokeTransaction }, ) => Ok(Self::Version1(InvokeTransactionV1 { max_fee, - version: *TX_V1, + version: TransactionVersion1::Version1, signature, nonce, sender_address, @@ -457,7 +490,7 @@ impl TryFrom for InvokeTransaction ) => Ok(Self::Version3(InvokeTransactionV3 { sender_address, calldata, - version: *TX_V3, + version: TransactionVersion3::Version3, signature, nonce, resource_bounds: resource_bounds.into(), @@ -500,22 +533,22 @@ impl TryFrom for Transaction { match tx { starknet_api::transaction::Transaction::Declare(declare_tx) => match declare_tx { starknet_api::transaction::DeclareTransaction::V0(tx) => { - Ok(Self::Declare(DeclareTransaction::Version0(DeclareTransactionV0V1 { + Ok(Self::Declare(DeclareTransaction::Version0(DeclareTransactionV0 { class_hash: tx.class_hash, sender_address: tx.sender_address, nonce: tx.nonce, max_fee: tx.max_fee, - version: *TX_V0, + version: TransactionVersion0::Version0, signature: tx.signature, }))) } starknet_api::transaction::DeclareTransaction::V1(tx) => { - Ok(Self::Declare(DeclareTransaction::Version1(DeclareTransactionV0V1 { + Ok(Self::Declare(DeclareTransaction::Version1(DeclareTransactionV1 { class_hash: tx.class_hash, sender_address: tx.sender_address, nonce: tx.nonce, max_fee: tx.max_fee, - version: *TX_V1, + version: TransactionVersion1::Version1, signature: tx.signature, }))) } @@ -1150,3 +1183,39 @@ fn eth_address_to_felt(eth_address: EthAddress) -> StarkFelt { bytes[12..32].copy_from_slice(ð_address_as_bytes); StarkFelt::new(bytes).expect("Eth address should fit in Felt") } + +/// An InvokeTransactionV1 that has the type field. This enum can be used to serialize/deserialize +/// invoke v1 transactions directly while `InvokeTransactionV1` can be serialized/deserialized only +/// from the `Transaction` enum. +/// This allows RPC methods to receive an invoke v1 transaction directly. +#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] +#[serde(tag = "type")] +pub enum TypedInvokeTransaction { + #[serde(rename = "INVOKE")] + Invoke(InvokeTransaction), +} + +impl From for client_transaction::InvokeTransaction { + fn from(tx: TypedInvokeTransaction) -> Self { + let TypedInvokeTransaction::Invoke(tx) = tx; + tx.into() + } +} + +/// A DeployAccountTransaction that has the type field. This enum can be used to +/// serialize/deserialize deploy account transactions directly while `DeployAccountTransaction` can +/// be serialized/deserialized only from the `Transaction` enum. +/// This allows RPC methods to receive a deploy account transaction directly. +#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord)] +#[serde(tag = "type")] +pub enum TypedDeployAccountTransaction { + #[serde(rename = "DEPLOY_ACCOUNT")] + DeployAccount(DeployAccountTransaction), +} + +impl From for client_transaction::DeployAccountTransaction { + fn from(tx: TypedDeployAccountTransaction) -> Self { + let TypedDeployAccountTransaction::DeployAccount(tx) = tx; + tx.into() + } +} diff --git a/crates/papyrus_rpc/src/v0_6/transaction_test.rs b/crates/papyrus_rpc/src/v0_6/transaction_test.rs index 88e2b7a0d2..1394914520 100644 --- a/crates/papyrus_rpc/src/v0_6/transaction_test.rs +++ b/crates/papyrus_rpc/src/v0_6/transaction_test.rs @@ -39,6 +39,9 @@ use super::{ InvokeTransactionV3, ResourceBoundsMapping, TransactionOutput, + TransactionVersion0, + TransactionVersion1, + TransactionVersion3, }; lazy_static::lazy_static! { @@ -77,7 +80,7 @@ auto_impl_get_test_instance! { pub class_hash: ClassHash, pub contract_address_salt: ContractAddressSalt, pub constructor_calldata: Calldata, - pub version: TransactionVersion, + pub version: TransactionVersion1, } pub struct DeployAccountTransactionV3 { pub signature: TransactionSignature, @@ -85,7 +88,7 @@ auto_impl_get_test_instance! { pub class_hash: ClassHash, pub contract_address_salt: ContractAddressSalt, pub constructor_calldata: Calldata, - pub version: TransactionVersion, + pub version: TransactionVersion3, pub resource_bounds: ResourceBoundsMapping, pub tip: Tip, pub paymaster_data: PaymasterData, @@ -99,7 +102,7 @@ auto_impl_get_test_instance! { } pub struct InvokeTransactionV0 { pub max_fee: Fee, - pub version: TransactionVersion, + pub version: TransactionVersion0, pub signature: TransactionSignature, pub contract_address: ContractAddress, pub entry_point_selector: EntryPointSelector, @@ -107,7 +110,7 @@ auto_impl_get_test_instance! { } pub struct InvokeTransactionV1 { pub max_fee: Fee, - pub version: TransactionVersion, + pub version: TransactionVersion1, pub signature: TransactionSignature, pub nonce: Nonce, pub sender_address: ContractAddress, @@ -116,7 +119,7 @@ auto_impl_get_test_instance! { pub struct InvokeTransactionV3 { pub sender_address: ContractAddress, pub calldata: Calldata, - pub version: TransactionVersion, + pub version: TransactionVersion3, pub signature: TransactionSignature, pub nonce: Nonce, pub resource_bounds: ResourceBoundsMapping, @@ -126,6 +129,15 @@ auto_impl_get_test_instance! { pub nonce_data_availability_mode: DataAvailabilityMode, pub fee_data_availability_mode: DataAvailabilityMode, } + pub enum TransactionVersion0 { + Version0 = 0, + } + pub enum TransactionVersion1 { + Version1 = 0, + } + pub enum TransactionVersion3 { + Version3 = 0, + } } macro_rules! gen_test_from_thin_transaction_output_macro {