Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Fix skip validate #1053

Merged
merged 7 commits into from
Sep 29, 2023
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
11 changes: 11 additions & 0 deletions rpc_state_reader/src/rpc_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starknet_api::{
state::StorageKey,
transaction::{Transaction as SNTransaction, TransactionHash},
};
use starknet_in_rust::definitions::block_context::StarknetChainId;
use std::{collections::HashMap, env, fmt::Display};
use thiserror::Error;

Expand All @@ -24,6 +25,16 @@ pub enum RpcChain {
TestNet2,
}

impl From<RpcChain> for StarknetChainId {
fn from(network: RpcChain) -> StarknetChainId {
match network {
RpcChain::MainNet => StarknetChainId::MainNet,
RpcChain::TestNet => StarknetChainId::TestNet,
RpcChain::TestNet2 => StarknetChainId::TestNet2,
}
}
}

impl fmt::Display for RpcChain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
50 changes: 45 additions & 5 deletions rpc_state_reader/tests/sir_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use starknet_in_rust::{
state_cache::StorageEntry,
BlockInfo,
},
transaction::{InvokeFunction, Transaction},
transaction::InvokeFunction,
utils::{Address, ClassHash},
};

Expand Down Expand Up @@ -87,10 +87,12 @@ impl StateReader for RpcStateReader {
}

#[allow(unused)]
pub fn execute_tx(
pub fn execute_tx_configurable(
tx_hash: &str,
network: RpcChain,
block_number: BlockNumber,
skip_validate: bool,
skip_nonce_check: bool,
) -> (
TransactionExecutionInfo,
TransactionTrace,
Expand Down Expand Up @@ -138,9 +140,9 @@ pub fn execute_tx(
// Get transaction before giving ownership of the reader
let tx_hash = TransactionHash(stark_felt!(tx_hash));
let tx = match rpc_reader.0.get_transaction(&tx_hash) {
SNTransaction::Invoke(tx) => Transaction::InvokeFunction(
InvokeFunction::from_invoke_transaction(tx, chain_id).unwrap(),
),
SNTransaction::Invoke(tx) => InvokeFunction::from_invoke_transaction(tx, chain_id)
.unwrap()
.create_for_simulation(skip_validate, false, false, false, skip_nonce_check),
_ => unimplemented!(),
};

Expand Down Expand Up @@ -169,6 +171,30 @@ pub fn execute_tx(
)
}

pub fn execute_tx(
tx_hash: &str,
network: RpcChain,
block_number: BlockNumber,
) -> (
TransactionExecutionInfo,
TransactionTrace,
RpcTransactionReceipt,
) {
execute_tx_configurable(tx_hash, network, block_number, false, false)
}

pub fn execute_tx_without_validate(
tx_hash: &str,
network: RpcChain,
block_number: BlockNumber,
) -> (
TransactionExecutionInfo,
TransactionTrace,
RpcTransactionReceipt,
) {
execute_tx_configurable(tx_hash, network, block_number, true, true)
}

#[test]
fn test_get_transaction_try_from() {
let rpc_state = RpcState::new_infura(RpcChain::MainNet, BlockTag::Latest.into());
Expand Down Expand Up @@ -359,3 +385,17 @@ fn starknet_in_rust_test_case_reverted_tx(hash: &str, block_number: u64, chain:
);
}
}

#[test_case(
"0x038c307a0a324dc92778820f2c6317f40157c06b12a7e537f7a16b2c015f64e7",
274333-1,
RpcChain::MainNet
)]
fn test_validate_fee(hash: &str, block_number: u64, chain: RpcChain) {
let (tx_info, _trace, receipt) = execute_tx(hash, chain, BlockNumber(block_number));
let (tx_info_without_fee, _trace, _receipt) =
execute_tx_without_validate(hash, chain, BlockNumber(block_number));

assert_eq!(tx_info.actual_fee, receipt.actual_fee);
assert!(tx_info_without_fee.actual_fee < tx_info.actual_fee);
}
8 changes: 6 additions & 2 deletions src/transaction/invoke_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,12 @@ impl InvokeFunction {
remaining_gas: u128,
) -> Result<TransactionExecutionInfo, TransactionError> {
let mut resources_manager = ExecutionResourcesManager::default();
let validate_info =
self.run_validate_entrypoint(state, &mut resources_manager, block_context)?;
let validate_info = if self.skip_validation {
None
} else {
self.run_validate_entrypoint(state, &mut resources_manager, block_context)?
};

// Execute transaction
let ExecutionResult {
call_info,
Expand Down
Loading