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

refactor: remove most fields from statful validator output #521

Merged
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
3 changes: 2 additions & 1 deletion crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ fn process_tx(
// TODO(Yael 31/7/24): refactor after IntrnalTransaction is ready, delete validate_info and
// compute all the info outside of run_validate.
let validate_info = stateful_tx_validator.run_validate(&executable_tx, validator)?;
let sender_address = executable_tx.contract_address();

// TODO(Arni): Add the Sierra and the Casm to the mempool input.
Ok(MempoolInput {
tx: executable_tx,
account: Account {
sender_address: validate_info.sender_address,
sender_address,
state: AccountState { nonce: validate_info.account_nonce },
},
})
Expand Down
18 changes: 6 additions & 12 deletions crates/gateway/src/stateful_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use starknet_api::executable_transaction::{
InvokeTransaction as ExecutableInvokeTransaction,
Transaction as ExecutableTransaction,
};
use starknet_api::transaction::TransactionHash;
use starknet_gateway_types::errors::GatewaySpecError;
use starknet_types_core::felt::Felt;
use tracing::error;
Expand Down Expand Up @@ -74,9 +73,12 @@ impl StatefulTransactionValidator {
executable_tx: &ExecutableTransaction,
mut validator: V,
) -> StatefulTransactionValidatorResult<ValidateInfo> {
let tx_hash = executable_tx.tx_hash();
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(
// TODO(Arni): create a try_from for &ExecutableTransaction.
executable_tx.clone(),
Expand All @@ -85,15 +87,10 @@ impl StatefulTransactionValidator {
error!("Failed to convert executable transaction into account transaction: {}", error);
GatewaySpecError::UnexpectedError { data: "Internal server error".to_owned() }
})?;
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);
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 @@ -149,12 +146,9 @@ pub fn get_latest_block_info(
})
}

// TODO(Arni): Remove irrelevant fields.
/// Holds members created by the stateful transaction validator, needed for
/// [`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: 4 additions & 8 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ 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::Nonce;
use starknet_api::executable_transaction::Transaction;
use starknet_api::test_utils::deploy_account::executable_deploy_account_tx;
use starknet_api::test_utils::invoke::executable_invoke_tx;
use starknet_api::transaction::{Resource, TransactionHash};
use starknet_api::{contract_address, deploy_account_tx_args, felt, invoke_tx_args, patricia_key};
use starknet_api::transaction::Resource;
use starknet_api::{deploy_account_tx_args, invoke_tx_args};
use starknet_gateway_types::errors::GatewaySpecError;
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -66,11 +66,7 @@ fn stateful_validator(block_context: BlockContext) -> StatefulTransactionValidat
#[rstest]
#[case::valid_tx(
create_executable_invoke_tx(CairoVersion::Cairo1),
Ok(ValidateInfo{
tx_hash: TransactionHash::default(),
sender_address: contract_address!("0xc0020000"),
account_nonce: Nonce::default()
})
Ok(ValidateInfo{account_nonce: Nonce::default()})
)]
#[case::invalid_tx(
create_executable_invoke_tx(CairoVersion::Cairo1),
Expand Down
4 changes: 4 additions & 0 deletions crates/starknet_api/src/executable_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl Transaction {
}
}

pub fn sender_address(&self) -> ContractAddress {
self.contract_address()
}

pub fn nonce(&self) -> Nonce {
match self {
Transaction::Declare(tx_data) => tx_data.tx.nonce(),
Expand Down
Loading