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

Commit 9405592

Browse files
authored
Fix skip validate (#1053)
* update version * fix skip validation for invoke txs * run fmt * fix clippy suggestion * simplify a bit the execute_tx function variants
1 parent a1fad26 commit 9405592

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

rpc_state_reader/src/rpc_state.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use starknet_api::{
1111
state::StorageKey,
1212
transaction::{Transaction as SNTransaction, TransactionHash},
1313
};
14+
use starknet_in_rust::definitions::block_context::StarknetChainId;
1415
use std::{collections::HashMap, env, fmt::Display};
1516
use thiserror::Error;
1617

@@ -24,6 +25,16 @@ pub enum RpcChain {
2425
TestNet2,
2526
}
2627

28+
impl From<RpcChain> for StarknetChainId {
29+
fn from(network: RpcChain) -> StarknetChainId {
30+
match network {
31+
RpcChain::MainNet => StarknetChainId::MainNet,
32+
RpcChain::TestNet => StarknetChainId::TestNet,
33+
RpcChain::TestNet2 => StarknetChainId::TestNet2,
34+
}
35+
}
36+
}
37+
2738
impl fmt::Display for RpcChain {
2839
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2940
match self {

rpc_state_reader/tests/sir_tests.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use starknet_in_rust::{
2828
state_cache::StorageEntry,
2929
BlockInfo,
3030
},
31-
transaction::{InvokeFunction, Transaction},
31+
transaction::InvokeFunction,
3232
utils::{Address, ClassHash},
3333
};
3434

@@ -87,10 +87,12 @@ impl StateReader for RpcStateReader {
8787
}
8888

8989
#[allow(unused)]
90-
pub fn execute_tx(
90+
pub fn execute_tx_configurable(
9191
tx_hash: &str,
9292
network: RpcChain,
9393
block_number: BlockNumber,
94+
skip_validate: bool,
95+
skip_nonce_check: bool,
9496
) -> (
9597
TransactionExecutionInfo,
9698
TransactionTrace,
@@ -138,9 +140,9 @@ pub fn execute_tx(
138140
// Get transaction before giving ownership of the reader
139141
let tx_hash = TransactionHash(stark_felt!(tx_hash));
140142
let tx = match rpc_reader.0.get_transaction(&tx_hash) {
141-
SNTransaction::Invoke(tx) => Transaction::InvokeFunction(
142-
InvokeFunction::from_invoke_transaction(tx, chain_id).unwrap(),
143-
),
143+
SNTransaction::Invoke(tx) => InvokeFunction::from_invoke_transaction(tx, chain_id)
144+
.unwrap()
145+
.create_for_simulation(skip_validate, false, false, false, skip_nonce_check),
144146
_ => unimplemented!(),
145147
};
146148

@@ -169,6 +171,30 @@ pub fn execute_tx(
169171
)
170172
}
171173

174+
pub fn execute_tx(
175+
tx_hash: &str,
176+
network: RpcChain,
177+
block_number: BlockNumber,
178+
) -> (
179+
TransactionExecutionInfo,
180+
TransactionTrace,
181+
RpcTransactionReceipt,
182+
) {
183+
execute_tx_configurable(tx_hash, network, block_number, false, false)
184+
}
185+
186+
pub fn execute_tx_without_validate(
187+
tx_hash: &str,
188+
network: RpcChain,
189+
block_number: BlockNumber,
190+
) -> (
191+
TransactionExecutionInfo,
192+
TransactionTrace,
193+
RpcTransactionReceipt,
194+
) {
195+
execute_tx_configurable(tx_hash, network, block_number, true, true)
196+
}
197+
172198
#[test]
173199
fn test_get_transaction_try_from() {
174200
let rpc_state = RpcState::new_infura(RpcChain::MainNet, BlockTag::Latest.into());
@@ -359,3 +385,17 @@ fn starknet_in_rust_test_case_reverted_tx(hash: &str, block_number: u64, chain:
359385
);
360386
}
361387
}
388+
389+
#[test_case(
390+
"0x038c307a0a324dc92778820f2c6317f40157c06b12a7e537f7a16b2c015f64e7",
391+
274333-1,
392+
RpcChain::MainNet
393+
)]
394+
fn test_validate_fee(hash: &str, block_number: u64, chain: RpcChain) {
395+
let (tx_info, _trace, receipt) = execute_tx(hash, chain, BlockNumber(block_number));
396+
let (tx_info_without_fee, _trace, _receipt) =
397+
execute_tx_without_validate(hash, chain, BlockNumber(block_number));
398+
399+
assert_eq!(tx_info.actual_fee, receipt.actual_fee);
400+
assert!(tx_info_without_fee.actual_fee < tx_info.actual_fee);
401+
}

src/transaction/invoke_function.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,12 @@ impl InvokeFunction {
244244
remaining_gas: u128,
245245
) -> Result<TransactionExecutionInfo, TransactionError> {
246246
let mut resources_manager = ExecutionResourcesManager::default();
247-
let validate_info =
248-
self.run_validate_entrypoint(state, &mut resources_manager, block_context)?;
247+
let validate_info = if self.skip_validation {
248+
None
249+
} else {
250+
self.run_validate_entrypoint(state, &mut resources_manager, block_context)?
251+
};
252+
249253
// Execute transaction
250254
let ExecutionResult {
251255
call_info,

0 commit comments

Comments
 (0)