Skip to content

Commit

Permalink
chore: add impl from rpc tx to snapi tx
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 29, 2024
1 parent 6b7dcbb commit 5ee840c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 51 deletions.
61 changes: 10 additions & 51 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,28 @@ use blockifier::transaction::transactions::{
DeployAccountTransaction as BlockifierDeployAccountTransaction,
InvokeTransaction as BlockifierInvokeTransaction,
};
use starknet_api::core::{calculate_contract_address, ChainId, ClassHash, ContractAddress};
use starknet_api::rpc_transaction::{
RpcDeclareTransaction,
RpcDeployAccountTransaction,
RpcInvokeTransaction,
RpcTransaction,
};
use starknet_api::core::{calculate_contract_address, ChainId, ContractAddress};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::{
DeclareTransaction,
DeclareTransactionV3,
DeployAccountTransaction,
DeployAccountTransactionV3,
InvokeTransactionV3,
InvokeTransaction,
TransactionHasher,
};
use tracing::error;

use crate::errors::{GatewaySpecError, StatefulTransactionValidatorResult};

// TODO(Arni): Remove this function. Replace with a function that take ownership of RpcTransaction.
pub fn rpc_tx_to_account_tx(
rpc_tx: &RpcTransaction,
// FIXME(yael 15/4/24): calculate class_info inside the function once compilation code is ready
optional_class_info: Option<ClassInfo>,
chain_id: &ChainId,
) -> StatefulTransactionValidatorResult<AccountTransaction> {
match rpc_tx {
RpcTransaction::Declare(RpcDeclareTransaction::V3(tx)) => {
let declare_tx = DeclareTransaction::V3(DeclareTransactionV3 {
class_hash: ClassHash::default(), /* FIXME(yael 15/4/24): call the starknet-api
* function once ready */
resource_bounds: tx.resource_bounds.clone().into(),
tip: tx.tip,
signature: tx.signature.clone(),
nonce: tx.nonce,
compiled_class_hash: tx.compiled_class_hash,
sender_address: tx.sender_address,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data.clone(),
account_deployment_data: tx.account_deployment_data.clone(),
});
RpcTransaction::Declare(tx) => {
let declare_tx: DeclareTransaction = tx.clone().into();
let tx_hash = declare_tx
.calculate_transaction_hash(chain_id, &declare_tx.version())
.map_err(|e| {
Expand All @@ -61,19 +42,8 @@ pub fn rpc_tx_to_account_tx(
})?;
Ok(AccountTransaction::Declare(declare_tx))
}
RpcTransaction::DeployAccount(RpcDeployAccountTransaction::V3(tx)) => {
let deploy_account_tx = DeployAccountTransaction::V3(DeployAccountTransactionV3 {
resource_bounds: tx.resource_bounds.clone().into(),
tip: tx.tip,
signature: tx.signature.clone(),
nonce: tx.nonce,
class_hash: tx.class_hash,
contract_address_salt: tx.contract_address_salt,
constructor_calldata: tx.constructor_calldata.clone(),
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data.clone(),
});
RpcTransaction::DeployAccount(tx) => {
let deploy_account_tx: DeployAccountTransaction = tx.clone().into();
let contract_address = calculate_contract_address(
deploy_account_tx.contract_address_salt(),
deploy_account_tx.class_hash(),
Expand All @@ -97,19 +67,8 @@ pub fn rpc_tx_to_account_tx(
);
Ok(AccountTransaction::DeployAccount(deploy_account_tx))
}
RpcTransaction::Invoke(RpcInvokeTransaction::V3(tx)) => {
let invoke_tx = starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 {
resource_bounds: tx.resource_bounds.clone().into(),
tip: tx.tip,
signature: tx.signature.clone(),
nonce: tx.nonce,
sender_address: tx.sender_address,
calldata: tx.calldata.clone(),
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data.clone(),
account_deployment_data: tx.account_deployment_data.clone(),
});
RpcTransaction::Invoke(tx) => {
let invoke_tx: InvokeTransaction = tx.clone().into();
let tx_hash = invoke_tx
.calculate_transaction_hash(chain_id, &invoke_tx.version())
.map_err(|e| {
Expand Down
96 changes: 96 additions & 0 deletions crates/starknet_api/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ use crate::core::{
use crate::data_availability::DataAvailabilityMode;
use crate::execution_resources::ExecutionResources;
use crate::hash::StarkHash;
use crate::rpc_transaction::{
RpcDeclareTransaction,
RpcDeclareTransactionV3,
RpcDeployAccountTransaction,
RpcDeployAccountTransactionV3,
RpcInvokeTransaction,
RpcInvokeTransactionV3,
RpcTransaction,
};
use crate::serde_utils::PrefixedBytesAsHex;
use crate::transaction_hash::{
get_declare_transaction_v0_hash,
Expand Down Expand Up @@ -99,6 +108,16 @@ impl Transaction {
}
}

impl From<RpcTransaction> for Transaction {
fn from(rpc_transaction: RpcTransaction) -> Self {
match rpc_transaction {
RpcTransaction::Declare(tx) => Transaction::Declare(tx.into()),
RpcTransaction::DeployAccount(tx) => Transaction::DeployAccount(tx.into()),
RpcTransaction::Invoke(tx) => Transaction::Invoke(tx.into()),
}
}
}

/// A transaction output.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub enum TransactionOutput {
Expand Down Expand Up @@ -229,6 +248,25 @@ pub struct DeclareTransactionV3 {
pub account_deployment_data: AccountDeploymentData,
}

impl From<RpcDeclareTransactionV3> for DeclareTransactionV3 {
fn from(tx: RpcDeclareTransactionV3) -> Self {
Self {
class_hash: ClassHash::default(), /* FIXME(yael 15/4/24): call the starknet-api
* function once ready */
resource_bounds: tx.resource_bounds.into(),
tip: tx.tip,
signature: tx.signature,
nonce: tx.nonce,
compiled_class_hash: tx.compiled_class_hash,
sender_address: tx.sender_address,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data,
account_deployment_data: tx.account_deployment_data,
}
}
}

impl TransactionHasher for DeclareTransactionV3 {
fn calculate_transaction_hash(
&self,
Expand Down Expand Up @@ -278,6 +316,14 @@ impl DeclareTransaction {
}
}

impl From<RpcDeclareTransaction> for DeclareTransaction {
fn from(rpc_declare_transaction: RpcDeclareTransaction) -> Self {
match rpc_declare_transaction {
RpcDeclareTransaction::V3(tx) => DeclareTransaction::V3(tx.into()),
}
}
}

impl TransactionHasher for DeclareTransaction {
fn calculate_transaction_hash(
&self,
Expand Down Expand Up @@ -337,6 +383,23 @@ pub struct DeployAccountTransactionV3 {
pub paymaster_data: PaymasterData,
}

impl From<RpcDeployAccountTransactionV3> for DeployAccountTransactionV3 {
fn from(tx: RpcDeployAccountTransactionV3) -> Self {
Self {
resource_bounds: tx.resource_bounds.into(),
tip: tx.tip,
signature: tx.signature,
nonce: tx.nonce,
class_hash: tx.class_hash,
contract_address_salt: tx.contract_address_salt,
constructor_calldata: tx.constructor_calldata,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data,
}
}
}

impl TransactionHasher for DeployAccountTransactionV3 {
fn calculate_transaction_hash(
&self,
Expand Down Expand Up @@ -383,6 +446,14 @@ impl DeployAccountTransaction {
}
}

impl From<RpcDeployAccountTransaction> for DeployAccountTransaction {
fn from(rpc_deploy_account_transaction: RpcDeployAccountTransaction) -> Self {
match rpc_deploy_account_transaction {
RpcDeployAccountTransaction::V3(tx) => DeployAccountTransaction::V3(tx.into()),
}
}
}

impl TransactionHasher for DeployAccountTransaction {
fn calculate_transaction_hash(
&self,
Expand Down Expand Up @@ -474,6 +545,23 @@ pub struct InvokeTransactionV3 {
pub account_deployment_data: AccountDeploymentData,
}

impl From<RpcInvokeTransactionV3> for InvokeTransactionV3 {
fn from(tx: RpcInvokeTransactionV3) -> Self {
Self {
resource_bounds: tx.resource_bounds.into(),
tip: tx.tip,
signature: tx.signature,
nonce: tx.nonce,
sender_address: tx.sender_address,
calldata: tx.calldata,
nonce_data_availability_mode: tx.nonce_data_availability_mode,
fee_data_availability_mode: tx.fee_data_availability_mode,
paymaster_data: tx.paymaster_data,
account_deployment_data: tx.account_deployment_data,
}
}
}

impl TransactionHasher for InvokeTransactionV3 {
fn calculate_transaction_hash(
&self,
Expand All @@ -491,6 +579,14 @@ pub enum InvokeTransaction {
V3(InvokeTransactionV3),
}

impl From<RpcInvokeTransaction> for InvokeTransaction {
fn from(rpc_invoke_tx: RpcInvokeTransaction) -> Self {
match rpc_invoke_tx {
RpcInvokeTransaction::V3(tx) => InvokeTransaction::V3(tx.into()),
}
}
}

macro_rules! implement_invoke_tx_getters {
($(($field:ident, $field_type:ty)),*) => {
$(pub fn $field(&self) -> $field_type {
Expand Down

0 comments on commit 5ee840c

Please sign in to comment.