Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Yael-Starkware committed Jul 24, 2024
1 parent 177aaf0 commit 2e1173a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
10 changes: 3 additions & 7 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet_api::rpc_transaction::RPCTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool_infra::component_runner::{ComponentStartError, ComponentStarter};
use starknet_mempool_types::communication::SharedMempoolClient;
use starknet_mempool_types::mempool_types::{Account, MempoolInput};
use starknet_mempool_types::mempool_types::MempoolInput;
use tracing::{info, instrument};

use crate::compilation::GatewayCompiler;
Expand All @@ -20,7 +20,6 @@ use crate::rpc_state_reader::RpcStateReaderFactory;
use crate::state_reader::StateReaderFactory;
use crate::stateful_transaction_validator::StatefulTransactionValidator;
use crate::stateless_transaction_validator::StatelessTransactionValidator;
use crate::utils::{external_tx_to_thin_tx, get_sender_address};

#[cfg(test)]
#[path = "gateway_test.rs"]
Expand Down Expand Up @@ -134,13 +133,10 @@ fn process_tx(
};

let validator = stateful_tx_validator.instantiate_validator(state_reader_factory)?;
let tx_hash = stateful_tx_validator.run_validate(&tx, optional_class_info, validator)?;
let mempool_input = stateful_tx_validator.run_validate(&tx, optional_class_info, validator)?;

// TODO(Arni): Add the Sierra and the Casm to the mempool input.
Ok(MempoolInput {
tx: external_tx_to_thin_tx(&tx, tx_hash),
account: Account { sender_address: get_sender_address(&tx), ..Default::default() },
})
Ok(mempool_input)
}

pub fn create_gateway(
Expand Down
14 changes: 9 additions & 5 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use blockifier::state::cached_state::CachedState;
use blockifier::versioned_constants::VersionedConstants;
use starknet_api::core::Nonce;
use starknet_api::rpc_transaction::{RPCInvokeTransaction, RPCTransaction};
use starknet_api::transaction::TransactionHash;
use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput};
use starknet_types_core::felt::Felt;

use crate::config::StatefulTransactionValidatorConfig;
use crate::errors::StatefulTransactionValidatorResult;
use crate::state_reader::{MempoolStateReader, StateReaderFactory};
use crate::utils::{external_tx_to_account_tx, get_sender_address, get_tx_hash};
use crate::utils::{external_tx_to_account_tx, external_tx_to_thin_tx, get_sender_address, get_tx_hash};

#[cfg(test)]
#[path = "stateful_transaction_validator_test.rs"]
Expand All @@ -31,18 +31,22 @@ impl StatefulTransactionValidator {
external_tx: &RPCTransaction,
optional_class_info: Option<ClassInfo>,
mut validator: BlockifierStatefulValidator,
) -> StatefulTransactionValidatorResult<TransactionHash> {
) -> StatefulTransactionValidatorResult<MempoolInput> {
let account_tx = external_tx_to_account_tx(
external_tx,
optional_class_info,
&self.config.chain_info.chain_id,
)?;
let tx_hash = get_tx_hash(&account_tx);
let sender_address = get_sender_address(&account_tx);
let account_nonce = validator.get_nonce(sender_address)?;

let account_nonce = validator.get_nonce(get_sender_address(external_tx))?;
let skip_validate = skip_stateful_validations(external_tx, account_nonce)?;
validator.perform_validations(account_tx, skip_validate)?;
Ok(tx_hash)
Ok(MempoolInput {
tx: external_tx_to_thin_tx(external_tx, tx_hash, sender_address),
account: Account { sender_address, state: AccountState { nonce: account_nonce } },
})
}

pub fn instantiate_validator(
Expand Down
5 changes: 4 additions & 1 deletion crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ fn test_stateful_tx_validator(
let validator = stateful_validator.instantiate_validator(&state_reader_factory).unwrap();

let result = stateful_validator.run_validate(&external_tx, optional_class_info, validator);
assert_eq!(format!("{:?}", result), format!("{:?}", expected_result));
match expected_result {
Ok(expected_result) => assert_eq!(result.unwrap().tx.tx_hash, expected_result),
Err(_) => assert_eq!(format!("{:?}", result), format!("{:?}", expected_result)),
}
}

#[test]
Expand Down
31 changes: 18 additions & 13 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,16 @@ impl RPCTransactionExt for RPCTransaction {
pub fn external_tx_to_thin_tx(
external_tx: &RPCTransaction,
tx_hash: TransactionHash,
sender_address: ContractAddress,
) -> ThinTransaction {
ThinTransaction {
tip: *external_tx.tip(),
nonce: *external_tx.nonce(),
sender_address: get_sender_address(external_tx),
sender_address,
tx_hash,
}
}

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

// TODO(Mohammad): Remove this trait once it is implemented in StarkNet API.
#[allow(dead_code)]
pub trait RPCTransactionExt {
Expand Down Expand Up @@ -152,7 +142,7 @@ pub fn external_tx_to_account_tx(
}
}

// TODO(yael 9/5/54): Remove once we we transition to InternalTransaction
// TODO(yael 9/5/54): Should be implemented as part of InternalTransaction in starknet-api
pub fn get_tx_hash(tx: &AccountTransaction) -> TransactionHash {
match tx {
AccountTransaction::Declare(tx) => tx.tx_hash,
Expand All @@ -161,6 +151,21 @@ pub fn get_tx_hash(tx: &AccountTransaction) -> TransactionHash {
}
}

// TODO(yael 9/5/54): Should be implemented as part of InternalTransaction in starknet-api
pub fn get_sender_address(tx: &AccountTransaction) -> ContractAddress {
match tx {
AccountTransaction::Declare(tx) => match &tx.tx {
DeclareTransaction::V3(tx) => tx.sender_address,
_ => panic!("Unsupported transaction version"),
},
AccountTransaction::DeployAccount(tx) => tx.contract_address,
AccountTransaction::Invoke(tx) => match &tx.tx {
InvokeTransaction::V3(tx) => tx.sender_address,
_ => panic!("Unsupported transaction version"),
},
}
}

/// Checks whether 'subsequence' is a subsequence of 'sequence'.
pub fn is_subsequence<T: Eq>(subsequence: &[T], sequence: &[T]) -> bool {
let mut offset = 0;
Expand Down

0 comments on commit 2e1173a

Please sign in to comment.