Skip to content

Commit

Permalink
refactor: remove most fields from statful validator output
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 21, 2024
1 parent cc0e749 commit ad0f9b7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 38 deletions.
8 changes: 6 additions & 2 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,13 @@ fn process_tx(

// TODO(Arni): Add the Sierra and the Casm to the mempool input.
Ok(MempoolInput {
tx: Transaction::new_from_rpc_tx(tx, validate_info.tx_hash, validate_info.sender_address),
tx: Transaction::new_from_rpc_tx(
tx,
executable_tx.tx_hash(),
executable_tx.contract_address(),
),
account: Account {
sender_address: validate_info.sender_address,
sender_address: executable_tx.contract_address(),
state: AccountState { nonce: validate_info.account_nonce },
},
})
Expand Down
17 changes: 6 additions & 11 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ use starknet_api::executable_transaction::{
InvokeTransaction as ExecutableInvokeTransaction,
Transaction as ExecutableTransaction,
};
use starknet_api::transaction::TransactionHash;
use starknet_types_core::felt::Felt;
use tracing::error;

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

#[cfg(test)]
#[path = "stateful_transaction_validator_test.rs"]
Expand Down Expand Up @@ -74,21 +72,20 @@ impl StatefulTransactionValidator {
executable_tx: &ExecutableTransaction,
mut validator: V,
) -> StatefulTransactionValidatorResult<ValidateInfo> {
let account_tx = AccountTransaction::try_from(executable_tx.clone()).map_err(|error| {
error!("Failed to executable transaction into account transaction: {}", error);
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
})?;
let tx_hash = account_tx.tx_hash();
let sender_address = get_sender_address(&account_tx);
let sender_address = executable_tx.contract_address();
let account_nonce = validator.get_nonce(sender_address).map_err(|e| {
error!("Failed to get nonce for sender address {}: {}", sender_address, e);
GatewaySpecError::UnexpectedError { data: "Internal server error.".to_owned() }
})?;
let skip_validate = skip_stateful_validations(executable_tx, account_nonce);
let account_tx = AccountTransaction::try_from(executable_tx.clone()).map_err(|error| {
error!("Failed to executable transaction into account transaction: {}", error);
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
})?;
validator
.validate(account_tx, skip_validate)
.map_err(|err| GatewaySpecError::ValidationFailure { data: err.to_string() })?;
Ok(ValidateInfo { tx_hash, sender_address, account_nonce })
Ok(ValidateInfo { account_nonce })
}

pub fn instantiate_validator(
Expand Down Expand Up @@ -148,7 +145,5 @@ pub fn get_latest_block_info(
/// [`MempoolInput`](starknet_mempool_types::mempool_types::MempoolInput).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ValidateInfo {
pub tx_hash: TransactionHash,
pub sender_address: ContractAddress,
pub account_nonce: Nonce,
}
12 changes: 2 additions & 10 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ use mockall::predicate::eq;
use num_bigint::BigUint;
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
use starknet_api::core::{ContractAddress, Nonce, PatriciaKey};
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_api::{contract_address, felt, patricia_key};
use starknet_sierra_compile::config::SierraToCasmCompilationConfig;
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -68,13 +66,7 @@ fn stateful_validator(block_context: BlockContext) -> StatefulTransactionValidat
#[rstest]
#[case::valid_tx(
invoke_tx(CairoVersion::Cairo1),
Ok(ValidateInfo{
tx_hash: TransactionHash(felt!(
"0x152b8dd0c30e95fa3a4ee7a9398fcfc46fb00c048b4fdcfa9958c64d65899b8"
)),
sender_address: contract_address!("0xc0020000"),
account_nonce: Nonce::default()
})
Ok(ValidateInfo{account_nonce: Nonce::default()})
)]
#[case::invalid_tx(invoke_tx(CairoVersion::Cairo1), Err(STATEFUL_VALIDATOR_FEE_ERROR))]
fn test_stateful_tx_validator(
Expand Down
15 changes: 0 additions & 15 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,3 @@ fn external_invoke_tx_to_executable_tx(
})?;
Ok(ExecutableInvokeTransaction { tx: invoke_tx, tx_hash })
}

// 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() {
starknet_api::transaction::InvokeTransaction::V3(tx) => tx.sender_address,
_ => panic!("Unsupported transaction version"),
},
}
}
1 change: 1 addition & 0 deletions crates/starknet_api/src/executable_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum Transaction {
}

impl Transaction {
/// This parameter is somethimes called `sender_address`.
pub fn contract_address(&self) -> ContractAddress {
match self {
Transaction::Declare(tx_data) => tx_data.tx.sender_address(),
Expand Down

0 comments on commit ad0f9b7

Please sign in to comment.