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

Commit 3bbcf53

Browse files
Adjust nonce validity with blockifier (#839)
* update states to return zero if contract isnt deployed * remove now meaningless test * move handle nonce to before apply * remove nonce init in deploy_contract metthod
1 parent ce95a2b commit 3bbcf53

File tree

8 files changed

+14
-34
lines changed

8 files changed

+14
-34
lines changed

src/state/cached_state.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,6 @@ impl<T: StateReader> State for CachedState<T> {
236236
self.cache
237237
.class_hash_writes
238238
.insert(deploy_contract_address.clone(), class_hash);
239-
self.cache
240-
.nonce_writes_mut()
241-
.insert(deploy_contract_address, Felt252::zero());
242239
Ok(())
243240
}
244241

@@ -347,10 +344,11 @@ impl<T: StateReader> State for CachedState<T> {
347344
.nonce_initial_values
348345
.insert(contract_address.clone(), nonce);
349346
}
350-
self.cache
347+
Ok(self
348+
.cache
351349
.get_nonce(contract_address)
352-
.ok_or_else(|| StateError::NoneNonce(contract_address.clone()))
353-
.cloned()
350+
.unwrap_or(&Felt252::zero())
351+
.clone())
354352
}
355353

356354
fn get_storage_at(&mut self, storage_entry: &StorageEntry) -> Result<Felt252, StateError> {

src/state/in_memory_state_reader.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
};
1313
use cairo_vm::felt::Felt252;
1414
use getset::{Getters, MutGetters};
15+
use num_traits::Zero;
1516
use std::collections::HashMap;
1617

1718
/// A [StateReader] that holds all the data in memory.
@@ -98,11 +99,12 @@ impl StateReader for InMemoryStateReader {
9899
}
99100

100101
fn get_nonce_at(&self, contract_address: &Address) -> Result<Felt252, StateError> {
102+
let default = Felt252::zero();
101103
let nonce = self
102104
.address_to_nonce
103105
.get(contract_address)
104-
.ok_or_else(|| StateError::NoneContractState(contract_address.clone()));
105-
nonce.cloned()
106+
.unwrap_or(&default);
107+
Ok(nonce.clone())
106108
}
107109

108110
fn get_storage_at(&self, storage_entry: &StorageEntry) -> Result<Felt252, StateError> {

src/state/state_api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub trait State {
6767

6868
fn get_class_hash_at(&mut self, contract_address: &Address) -> Result<ClassHash, StateError>;
6969

70+
/// Default: 0 for an uninitialized contract address.
7071
fn get_nonce_at(&mut self, contract_address: &Address) -> Result<Felt252, StateError>;
7172

7273
fn get_storage_at(&mut self, storage_entry: &StorageEntry) -> Result<Felt252, StateError>;

src/testing/state.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ mod tests {
324324

325325
use super::*;
326326
use crate::{
327-
core::{contract_address::compute_deprecated_class_hash, errors::state_errors::StateError},
327+
core::contract_address::compute_deprecated_class_hash,
328328
definitions::{
329329
constants::CONSTRUCTOR_ENTRY_POINT_SELECTOR, transaction_type::TransactionType,
330330
},
@@ -719,17 +719,4 @@ mod tests {
719719
let err = starknet_state.consume_message_hash(msg_hash).unwrap_err();
720720
assert_matches!(err, StarknetStateError::InvalidMessageHash);
721721
}
722-
723-
#[test]
724-
fn test_create_invoke_function_should_fail_with_none_contract_state() {
725-
let mut starknet_state = StarknetState::new(None);
726-
727-
let err = starknet_state
728-
.create_invoke_function(Address(0.into()), 0.into(), vec![], 0, None, None, None)
729-
.unwrap_err();
730-
assert_matches!(
731-
err,
732-
TransactionError::State(StateError::NoneContractState(_))
733-
);
734-
}
735722
}

src/transaction/declare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ impl Declare {
267267
state: &mut CachedState<S>,
268268
block_context: &BlockContext,
269269
) -> Result<TransactionExecutionInfo, TransactionError> {
270-
let mut tx_exec_info = self.apply(state, block_context)?;
271270
self.handle_nonce(state)?;
271+
let mut tx_exec_info = self.apply(state, block_context)?;
272272

273273
let mut tx_execution_context =
274274
self.get_execution_context(block_context.invoke_tx_max_n_steps);

src/transaction/deploy_account.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,8 @@ impl DeployAccount {
155155
state: &mut CachedState<S>,
156156
block_context: &BlockContext,
157157
) -> Result<TransactionExecutionInfo, TransactionError> {
158-
let mut tx_info = self.apply(state, block_context)?;
159-
160158
self.handle_nonce(state)?;
159+
let mut tx_info = self.apply(state, block_context)?;
161160

162161
let mut tx_execution_context =
163162
self.get_execution_context(block_context.invoke_tx_max_n_steps);
@@ -256,16 +255,15 @@ impl DeployAccount {
256255
return Ok(());
257256
}
258257

258+
// In blockifier, get_nonce_at returns zero if no entry is found.
259259
let current_nonce = state.get_nonce_at(&self.contract_address)?;
260260
if current_nonce != self.nonce {
261261
return Err(TransactionError::InvalidTransactionNonce(
262262
current_nonce.to_string(),
263263
self.nonce.to_string(),
264264
));
265265
}
266-
267266
state.increment_nonce(&self.contract_address)?;
268-
269267
Ok(())
270268
}
271269

src/transaction/invoke_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ impl InvokeFunction {
274274
block_context: &BlockContext,
275275
remaining_gas: u128,
276276
) -> Result<TransactionExecutionInfo, TransactionError> {
277-
let mut tx_exec_info = self.apply(state, block_context, remaining_gas)?;
278277
self.handle_nonce(state)?;
278+
let mut tx_exec_info = self.apply(state, block_context, remaining_gas)?;
279279

280280
let mut tx_execution_context =
281281
self.get_execution_context(block_context.invoke_tx_max_n_steps)?;

tests/internals.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,12 +1251,6 @@ fn test_deploy_account() {
12511251

12521252
assert_eq!(state, state_before);
12531253

1254-
// Statement **not** in blockifier.
1255-
state.cache_mut().nonce_initial_values_mut().insert(
1256-
deploy_account_tx.contract_address().clone(),
1257-
Felt252::zero(),
1258-
);
1259-
12601254
let tx_info = deploy_account_tx
12611255
.execute(&mut state, &block_context)
12621256
.unwrap();

0 commit comments

Comments
 (0)