Skip to content

Commit

Permalink
fix clippy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Oct 6, 2023
1 parent b7fadf5 commit 98a2681
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 43 deletions.
4 changes: 2 additions & 2 deletions crates/ef-testing/src/evm_sequencer/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl From<Address> for FeltSequencer {

impl From<FeltSequencer> for StarkFelt {
fn from(felt: FeltSequencer) -> Self {
StarkFelt::from(felt.0)
Self::from(felt.0)
}
}

Expand All @@ -39,7 +39,7 @@ impl TryFrom<FeltSequencer> for ContractAddress {

fn try_from(felt: FeltSequencer) -> Result<Self, Self::Error> {
let felt: StarkFelt = felt.into();
let contract_address = ContractAddress(TryInto::<PatriciaKey>::try_into(felt)?);
let contract_address = Self(TryInto::<PatriciaKey>::try_into(felt)?);
Ok(contract_address)
}
}
19 changes: 9 additions & 10 deletions crates/ef-testing/src/models/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ lazy_static::lazy_static! {
}

// Division of logic:
//// 'handle' methods attempt to abstract the data coming from BlockChainTestCase
//// from more general logic that can be used across tests
// 'handle' methods attempt to abstract the data coming from BlockChainTestCase
// from more general logic that can be used across tests
impl BlockchainTestCase {
/// Returns whether a given test should be skipped
/// # Panics
Expand All @@ -67,15 +67,15 @@ impl BlockchainTestCase {
.unwrap();
let name = path.file_name().unwrap().to_str().unwrap();

let mut should_skip = false;
if SKIP.filename.contains_key(dir) {
should_skip = SKIP
.filename
let mut should_skip = if SKIP.filename.contains_key(dir) {
SKIP.filename
.get(dir)
.unwrap()
.iter()
.any(|filename| filename == name);
}
.any(|filename| filename == name)
} else {
false
};

if !should_skip && SKIP.regex.contains_key(dir) {
should_skip = SKIP
Expand Down Expand Up @@ -127,8 +127,7 @@ impl BlockchainTestCase {
let block = test
.blocks
.first()
.ok_or_else(|| RunnerError::Other("test has no blocks".to_string()))?
.clone();
.ok_or_else(|| RunnerError::Other("test has no blocks".to_string()))?;
// we adjust the rlp to correspond with our currently hardcoded CHAIN_ID
let tx_encoded = get_signed_rlp_encoded_transaction(
&block.rlp,
Expand Down
4 changes: 2 additions & 2 deletions crates/ef-testing/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ pub(crate) fn update_post_state(
let post_account_storage = &post_state
.get(k)
.map(|x| x.storage.clone())
.unwrap_or(BTreeMap::new());
.unwrap_or_else(BTreeMap::new);

let pre_account_storage = pre_state
.get(k)
.map(|x| x.storage.clone())
.unwrap_or(BTreeMap::new());
.unwrap_or_else(BTreeMap::new);

for storage_key in pre_account_storage.keys() {
if !post_account_storage.contains_key(storage_key) {
Expand Down
2 changes: 1 addition & 1 deletion crates/sequencer/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use blockifier::state::{

pub trait Committer<S>
where
for<'a> &'a mut S: BlockifierState + BlockifierStateReader,
for<'any> &'any mut S: BlockifierState + BlockifierStateReader,
{
fn commit(cached_state: &mut CachedState<&mut S>) -> StateResult<()> {
let diff = cached_state.to_state_diff();
Expand Down
27 changes: 14 additions & 13 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ use tracing::{trace, warn};
/// Using a trait bound for the state allows for better
/// speed, as the type of the state is known at compile time.
/// We bound S such that a mutable reference to S (&'a mut S)
/// must implement State and StateReader. The `for` keyword
/// must implement State and `StateReader`. The `for` keyword
/// indicates that the bound must hold for any lifetime 'a.
/// For more details, check out https://doc.rust-lang.org/nomicon/hrtb.html
/// For more details, check out [rust-lang docs](https://doc.rust-lang.org/nomicon/hrtb.html)
pub struct Sequencer<S>
where
for<'a> &'a mut S: State + StateReader,
for<'any> &'any mut S: State + StateReader,
{
pub block_context: BlockContext,
pub state: S,
}

impl<S> Sequencer<S>
where
for<'a> &'a mut S: State + StateReader,
for<'any> &'any mut S: State + StateReader,
{
/// Creates a new Sequencer instance.
pub fn new(block_context: BlockContext, state: S) -> Self {
#[inline]
#[must_use]
pub const fn new(block_context: BlockContext, state: S) -> Self {
Self {
block_context,
state,
Expand All @@ -43,7 +45,7 @@ where

impl<S> Execution for Sequencer<S>
where
for<'a> &'a mut S: State + StateReader + Committer<S>,
for<'any> &'any mut S: State + StateReader + Committer<S>,
{
fn execute(&mut self, transaction: Transaction) -> Result<(), TransactionExecutionError> {
let sender_address = match &transaction {
Expand Down Expand Up @@ -71,18 +73,17 @@ where
warn!("Transaction execution failed: {:?}", err);
return Err(err);
}
Ok(execution_information) => match execution_information.revert_error {
Some(err) => {
Ok(execution_information) => {
if let Some(err) = execution_information.revert_error {
// If the transaction reverted, we increment the nonce.
(&mut self.state).increment_nonce(sender_address)?;
warn!("Transaction execution reverted: {:?}", err)
}
None => {
warn!("Transaction execution reverted: {:?}", err);
} else {
// If the transaction succeeded, we commit the state.
<&mut S>::commit(&mut cached_state)?;
trace!("Transaction execution succeeded {execution_information:?}")
trace!("Transaction execution succeeded {execution_information:?}");
}
},
}
}

Ok(())
Expand Down
27 changes: 13 additions & 14 deletions crates/sequencer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starknet_api::{
use crate::commit::Committer;

/// Generic state structure for the sequencer.
/// The use of FxHashMap allows for a better performance.
/// The use of `FxHashMap` allows for a better performance.
/// This hash map is used by rustc. It uses a non cryptographic hash function
/// which is faster than the default hash function. Think about changing
/// if the test sequencer is used for tests outside of ef-tests.
Expand Down Expand Up @@ -55,7 +55,7 @@ impl BlockifierState for &mut State {
let current_nonce = self
.nonces
.get(&contract_address)
.cloned()
.copied()
.unwrap_or_default();

let mut current_nonce: u64 = current_nonce.0.try_into()?;
Expand All @@ -75,12 +75,11 @@ impl BlockifierState for &mut State {
contract_address: ContractAddress,
class_hash: ClassHash,
) -> StateResult<()> {
match self.contracts.get(&contract_address) {
Some(_) => Err(StateError::UnavailableContractAddress(contract_address)),
None => {
self.contracts.insert(contract_address, class_hash);
Ok(())
}
if self.contracts.get(&contract_address).is_some() {
Err(StateError::UnavailableContractAddress(contract_address))
} else {
self.contracts.insert(contract_address, class_hash);
Ok(())
}
}

Expand All @@ -89,7 +88,7 @@ impl BlockifierState for &mut State {
class_hash: &ClassHash,
contract_class: ContractClass,
) -> StateResult<()> {
self.classes.insert(class_hash.to_owned(), contract_class);
self.classes.insert(*class_hash, contract_class);
Ok(())
}

Expand Down Expand Up @@ -117,23 +116,23 @@ impl BlockifierStateReader for &mut State {
Ok(self
.storage
.get(&(contract_address, key))
.cloned()
.copied()
.unwrap_or_default())
}

fn get_nonce_at(&mut self, contract_address: ContractAddress) -> StateResult<Nonce> {
Ok(self
.nonces
.get(&contract_address)
.cloned()
.copied()
.unwrap_or_default())
}

fn get_class_hash_at(&mut self, contract_address: ContractAddress) -> StateResult<ClassHash> {
Ok(self
.contracts
.get(&contract_address)
.cloned()
.copied()
.unwrap_or_default())
}

Expand All @@ -147,7 +146,7 @@ impl BlockifierStateReader for &mut State {
self.classes
.get(class_hash)
.cloned()
.ok_or_else(|| StateError::UndeclaredClassHash(class_hash.to_owned()))
.ok_or_else(|| StateError::UndeclaredClassHash(*class_hash))
}

/// # Errors
Expand All @@ -156,7 +155,7 @@ impl BlockifierStateReader for &mut State {
fn get_compiled_class_hash(&mut self, class_hash: ClassHash) -> StateResult<CompiledClassHash> {
self.compiled_class_hashes
.get(&class_hash)
.cloned()
.copied()
.ok_or_else(|| StateError::UndeclaredClassHash(class_hash))
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/sequencer/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ use starknet_api::transaction::{
pub struct StarknetTransaction(BroadcastedTransaction);

impl StarknetTransaction {
pub fn new(transaction: BroadcastedTransaction) -> Self {
#[must_use]
#[inline]
pub const fn new(transaction: BroadcastedTransaction) -> Self {
Self(transaction)
}

#[inline]
pub fn try_into_execution_transaction(
self,
chain_id: FieldElement,
Expand Down

0 comments on commit 98a2681

Please sign in to comment.