Skip to content

Commit

Permalink
chore(katana): remove redundant code + rename functions (starkware-li…
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Dec 28, 2023
1 parent e083f7d commit 3fa7549
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 417 deletions.
2 changes: 1 addition & 1 deletion crates/katana/core/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use katana_primitives::block::{BlockHash, BlockHashOrNumber, BlockIdOrTag, Block
use katana_primitives::contract::{
ClassHash, CompiledContractClass, ContractAddress, Nonce, StorageKey, StorageValue,
};
use katana_primitives::event::{ContinuationToken, ContinuationTokenError};
use katana_primitives::receipt::Event;
use katana_primitives::transaction::{ExecutableTxWithHash, TxHash, TxWithHash};
use katana_primitives::FieldElement;
Expand All @@ -38,7 +39,6 @@ use crate::service::messaging::MessagingConfig;
#[cfg(feature = "messaging")]
use crate::service::messaging::MessagingService;
use crate::service::{NodeService, TransactionMiner};
use crate::utils::event::{ContinuationToken, ContinuationTokenError};

type SequencerResult<T> = Result<T, SequencerError>;

Expand Down
3 changes: 1 addition & 2 deletions crates/katana/core/src/sequencer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use blockifier::state::errors::StateError;
use blockifier::transaction::errors::TransactionExecutionError;
use katana_primitives::block::BlockIdOrTag;
use katana_primitives::contract::ContractAddress;
use katana_primitives::event::ContinuationTokenError;
use katana_primitives::transaction::TxHash;
use starknet_api::StarknetApiError;

use crate::utils::event::ContinuationTokenError;

#[derive(Debug, thiserror::Error)]
pub enum SequencerError {
#[error("Block {0:?} not found.")]
Expand Down
147 changes: 0 additions & 147 deletions crates/katana/core/src/utils/contract.rs

This file was deleted.

96 changes: 0 additions & 96 deletions crates/katana/core/src/utils/event.rs

This file was deleted.

74 changes: 2 additions & 72 deletions crates/katana/core/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
pub mod contract;
pub mod event;

use std::collections::HashMap;
use std::time::{Duration, SystemTime};
use std::time::SystemTime;

use anyhow::Result;
use blockifier::state::cached_state::CommitmentStateDiff;
use katana_primitives::state::{StateUpdates, StateUpdatesWithDeclaredClasses};
use starknet::core::types::{
ContractStorageDiffItem, DeclaredClassItem, DeployedContractItem, NonceUpdate, StateDiff,
StorageEntry,
};
use starknet_api::hash::StarkFelt;
use starknet_api::StarknetApiError;

use crate::constants::{
ERC20_CONTRACT, ERC20_CONTRACT_CLASS_HASH, ERC20_CONTRACT_COMPILED_CLASS_HASH,
Expand All @@ -22,71 +11,12 @@ use crate::constants::{
UDC_COMPILED_CLASS_HASH, UDC_CONTRACT,
};

pub fn get_current_timestamp() -> Duration {
pub(super) fn get_current_timestamp() -> std::time::Duration {
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("should get current UNIX timestamp")
}

pub fn starkfelt_to_u128(felt: StarkFelt) -> Result<u128, StarknetApiError> {
const COMPLIMENT_OF_U128: usize =
std::mem::size_of::<StarkFelt>() - std::mem::size_of::<u128>();

let (rest, u128_bytes) = felt.bytes().split_at(COMPLIMENT_OF_U128);
if rest != [0u8; COMPLIMENT_OF_U128] {
Err(StarknetApiError::OutOfRange { string: felt.to_string() })
} else {
Ok(u128::from_be_bytes(u128_bytes.try_into().expect("u128_bytes should be of size usize.")))
}
}

pub fn convert_state_diff_to_rpc_state_diff(state_diff: CommitmentStateDiff) -> StateDiff {
StateDiff {
storage_diffs: state_diff
.storage_updates
.iter()
.map(|(address, entries)| ContractStorageDiffItem {
address: (*address.0.key()).into(),
storage_entries: entries
.iter()
.map(|(key, value)| StorageEntry {
key: (*key.0.key()).into(),
value: (*value).into(),
})
.collect(),
})
.collect(),
deprecated_declared_classes: vec![],
// TODO: This will change with RPC spec v3.0.0. Also, are we supposed to return the class
// hash or the compiled class hash?
declared_classes: state_diff
.class_hash_to_compiled_class_hash
.iter()
.map(|(class_hash, compiled_class_hash)| DeclaredClassItem {
class_hash: class_hash.0.into(),
compiled_class_hash: compiled_class_hash.0.into(),
})
.collect(),
deployed_contracts: state_diff
.address_to_class_hash
.iter()
.map(|(address, class_hash)| DeployedContractItem {
address: (*address.0.key()).into(),
class_hash: class_hash.0.into(),
})
.collect(),
replaced_classes: vec![],
nonces: state_diff
.address_to_nonce
.iter()
.map(|(address, nonce)| NonceUpdate {
contract_address: (*address.0.key()).into(),
nonce: nonce.0.into(),
})
.collect(),
}
}

pub(super) fn get_genesis_states_for_testing() -> StateUpdatesWithDeclaredClasses {
let nonce_updates =
HashMap::from([(*UDC_ADDRESS, 0u8.into()), (*FEE_TOKEN_ADDRESS, 0u8.into())]);
Expand Down
Loading

0 comments on commit 3fa7549

Please sign in to comment.