Skip to content

Commit

Permalink
chore: add missing deploy_tx to get_sender_address and make run_valid…
Browse files Browse the repository at this point in the history
…ate return the mempool_input
  • Loading branch information
Yael-Starkware committed Jul 24, 2024
1 parent fc5813a commit fb2cf30
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 42 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
16 changes: 11 additions & 5 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use blockifier::versioned_constants::VersionedConstants;
use mockall::automock;
use starknet_api::core::{ContractAddress, 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 Down Expand Up @@ -60,17 +62,21 @@ impl StatefulTransactionValidator {
external_tx: &RPCTransaction,
optional_class_info: Option<ClassInfo>,
mut validator: V,
) -> 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 account_nonce = validator.get_nonce(get_sender_address(external_tx))?;
let sender_address = get_sender_address(&account_tx);
let account_nonce = validator.get_nonce(sender_address)?;
let skip_validate = skip_stateful_validations(external_tx, account_nonce);
validator.validate(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
41 changes: 24 additions & 17 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ use crate::stateful_transaction_validator::{
MockStatefulTransactionValidatorTrait, StatefulTransactionValidator,
};

pub const STATEFUL_TRANSACTION_VALIDATOR_FEE_ERROR: StatefulTransactionValidatorError =
StatefulTransactionValidatorError::StatefulValidatorError(
StatefulValidatorError::TransactionPreValidationError(
TransactionPreValidationError::TransactionFeeError(
TransactionFeeError::L1GasBoundsExceedBalance {
max_amount: VALID_L1_GAS_MAX_AMOUNT,
max_price: VALID_L1_GAS_MAX_PRICE_PER_UNIT,
balance: BigUint::ZERO,
},
),
),
);

#[fixture]
fn block_context() -> BlockContext {
BlockContext::create_for_testing()
Expand All @@ -50,26 +63,19 @@ fn stateful_validator(block_context: BlockContext) -> StatefulTransactionValidat
#[rstest]
#[case::valid_tx(
invoke_tx(CairoVersion::Cairo1),
Ok(()),
Ok(TransactionHash(felt!(
"0x152b8dd0c30e95fa3a4ee7a9398fcfc46fb00c048b4fdcfa9958c64d65899b8"
)))
"0x152b8dd0c30e95fa3a4ee7a9398fcfc46fb00c048b4fdcfa9958c64d65899b8"
))),
)]
#[case::invalid_tx(
invoke_tx(CairoVersion::Cairo1),
Err(StatefulTransactionValidatorError::StatefulValidatorError(
StatefulValidatorError::TransactionPreValidationError(
TransactionPreValidationError::TransactionFeeError(
TransactionFeeError::L1GasBoundsExceedBalance {
max_amount: VALID_L1_GAS_MAX_AMOUNT,
max_price: VALID_L1_GAS_MAX_PRICE_PER_UNIT,
balance: BigUint::ZERO,
}
)
)
))
Err(STATEFUL_TRANSACTION_VALIDATOR_FEE_ERROR),
Err(STATEFUL_TRANSACTION_VALIDATOR_FEE_ERROR)
)]
fn test_stateful_tx_validator(
#[case] external_tx: RPCTransaction,
#[case] expected_blockifier_response: StatefulTransactionValidatorResult<()>,
#[case] expected_result: StatefulTransactionValidatorResult<TransactionHash>,
stateful_validator: StatefulTransactionValidator,
) {
Expand All @@ -82,14 +88,15 @@ fn test_stateful_tx_validator(
_ => None,
};

let expected_result_msg = format!("{:?}", expected_result);

let mut mock_validator = MockStatefulTransactionValidatorTrait::new();
mock_validator.expect_validate().return_once(|_, _| expected_result.map(|_| ()));
mock_validator.expect_validate().return_once(|_, _| expected_blockifier_response);
mock_validator.expect_get_nonce().returning(|_| Ok(Nonce(Felt::ZERO)));

let result = stateful_validator.run_validate(&external_tx, optional_class_info, mock_validator);
assert_eq!(format!("{:?}", result), expected_result_msg);
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 fb2cf30

Please sign in to comment.