Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement thin transaction converter #145

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion crates/gateway/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use starknet_api::external_transaction::{
};
use starknet_api::internal_transaction::{InternalInvokeTransaction, InternalTransaction};
use starknet_api::transaction::{
Calldata, InvokeTransaction, InvokeTransactionV3, ResourceBounds, ResourceBoundsMapping,
Calldata, InvokeTransaction, InvokeTransactionV3, ResourceBounds, ResourceBoundsMapping, Tip,
TransactionSignature,
};

Expand All @@ -21,6 +21,35 @@ pub enum TransactionType {
Invoke,
}

// TODO(Mohammad): Use the StarkNet API's get_tip function.
pub fn get_tip(tx: &ExternalTransaction) -> Tip {
match tx {
ExternalTransaction::Declare(ExternalDeclareTransaction::V3(tx)) => tx.tip,
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(tx)) => tx.tip,
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(tx)) => tx.tip,
}
}

// TODO(Mohammad): Use the StarkNet API's get_nonce function.
pub fn get_nonce(tx: &ExternalTransaction) -> Nonce {
match tx {
ExternalTransaction::Declare(ExternalDeclareTransaction::V3(tx)) => tx.nonce,
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(tx)) => tx.nonce,
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(tx)) => tx.nonce,
}
}

pub fn get_sender_address(tx: &ExternalTransaction) -> ContractAddress {
match tx {
ExternalTransaction::Declare(ExternalDeclareTransaction::V3(tx)) => tx.sender_address,
// TODO(Mohammad): Add support for deploy account.
ExternalTransaction::DeployAccount(ExternalDeployAccountTransaction::V3(_)) => {
ContractAddress::default()
}
ExternalTransaction::Invoke(ExternalInvokeTransaction::V3(tx)) => tx.sender_address,
}
}

pub fn create_internal_tx_for_testing() -> InternalTransaction {
let tx = InvokeTransactionV3 {
resource_bounds: ResourceBoundsMapping::try_from(vec![
Expand Down
16 changes: 14 additions & 2 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ use starknet_api::external_transaction::{
};
use starknet_api::transaction::{
DeclareTransaction, DeclareTransactionV3, DeployAccountTransaction, DeployAccountTransactionV3,
InvokeTransaction, InvokeTransactionV3, ResourceBoundsMapping, TransactionHasher,
TransactionSignature,
InvokeTransaction, InvokeTransactionV3, ResourceBoundsMapping, TransactionHash,
TransactionHasher, TransactionSignature,
};
use starknet_mempool_types::mempool_types::ThinTransaction;

use crate::errors::StatefulTransactionValidatorResult;
use crate::starknet_api_test_utils::{get_nonce, get_sender_address, get_tip};

macro_rules! implement_ref_getters {
($(($member_name:ident, $member_type:ty));* $(;)?) => {
Expand All @@ -43,6 +45,16 @@ impl ExternalTransactionExt for ExternalTransaction {
);
}

pub fn external_tx_to_thin_tx(external_tx: &ExternalTransaction) -> ThinTransaction {
ThinTransaction {
tip: get_tip(external_tx),
nonce: get_nonce(external_tx),
contract_address: get_sender_address(external_tx),
// TODO(Yael): Add transaction hash calculation.
tx_hash: TransactionHash::default(),
}
}

// TODO(Arni, 1/5/2025): Remove this trait once it is implemented in StarkNet API.
pub trait ExternalTransactionExt {
fn resource_bounds(&self) -> &ResourceBoundsMapping;
Expand Down
Loading