diff --git a/CHANGELOG.md b/CHANGELOG.md index ba720efcd..f210f7b28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ git # Deoxys Changelog ## Next release +- fix l1 thread to reflect correct state_root, block_number, block_hash - fix: remove gas_price and update starknet-rs from fork (temporary fix) - fix(root): got state root to work (does not support class root yet) - refactor(substrate_hash): Substrate hash is now retrieved via rpc client in diff --git a/crates/client/sync/src/l1.rs b/crates/client/sync/src/l1.rs index d58b6400c..90d795e36 100644 --- a/crates/client/sync/src/l1.rs +++ b/crates/client/sync/src/l1.rs @@ -6,19 +6,19 @@ use anyhow::Result; use ethers::contract::{abigen, EthEvent}; use ethers::providers::{Http, Middleware, Provider}; use ethers::types::transaction::eip2718::TypedTransaction; -use ethers::types::{Address, BlockNumber as EthBlockNumber, Filter, TransactionRequest, I256, U64}; +use ethers::types::{Address, BlockNumber as EthBlockNumber, Filter, TransactionRequest, I256, U256, U64}; use ethers::utils::hex::decode; use futures::stream::StreamExt; use lazy_static::lazy_static; use mp_felt::Felt252Wrapper; -use primitive_types::{H256, U256}; +use primitive_types::H256; use reqwest::Url; use serde::Deserialize; use serde_json::Value; use starknet_api::hash::StarkHash; use crate::l2::STARKNET_STATE_UPDATE; -use crate::utility::{event_to_l1_state_update, get_config, get_state_update_at}; +use crate::utility::{convert_log_state_update, get_config, get_state_update_at}; use crate::utils::constant::LOG_STATE_UPDTATE_TOPIC; lazy_static! { @@ -41,7 +41,6 @@ pub struct L1StateUpdate { /// Starknet core LogStateUpdate event #[derive(Clone, Debug, EthEvent, Deserialize)] pub struct LogStateUpdate { - #[ethevent(indexed)] pub global_root: U256, pub block_number: I256, pub block_hash: U256, @@ -170,10 +169,8 @@ impl EthereumClient { while let Some(event_result) = event_stream.next().await { match event_result { Ok(log) => { - println!("Log event in log format: {:?}", log.clone()); let format_event = - event_to_l1_state_update(log.clone()).expect("Failed to format event into an L1StateUpdate"); - println!("LogStateUpdate event: {:?}, format event: {:?}", log, format_event.clone()); + convert_log_state_update(log.clone()).expect("Failed to format event into an L1StateUpdate"); update_l1(format_event); } Err(e) => println!("Error while listening for events: {:?}", e), diff --git a/crates/client/sync/src/utils/utility.rs b/crates/client/sync/src/utils/utility.rs index 6116aa29c..b1ba05f81 100644 --- a/crates/client/sync/src/utils/utility.rs +++ b/crates/client/sync/src/utils/utility.rs @@ -5,7 +5,7 @@ use std::sync::RwLock; use std::thread::sleep; use std::time::Duration; -use ethers::types::I256; +use ethers::types::{I256, U256}; use lazy_static::lazy_static; use rand::seq::SliceRandom; use rand::thread_rng; @@ -181,24 +181,21 @@ pub fn format_address(address: &str) -> String { } } -pub fn event_to_l1_state_update(log_state_update: LogStateUpdate) -> Result { - let block_number_u64 = if log_state_update.block_number >= I256::from(0) { +pub fn u256_to_starkfelt(u256: U256) -> Result { + let mut bytes = [0u8; 32]; + u256.to_big_endian(&mut bytes); + StarkFelt::new(bytes).map_err(|_| "Failed to convert U256 to StarkFelt") +} + +pub fn convert_log_state_update(log_state_update: LogStateUpdate) -> Result { + let block_number = if log_state_update.block_number >= I256::zero() { log_state_update.block_number.low_u64() } else { return Err("Block number is negative"); }; - let global_root_u128 = log_state_update.global_root.low_u128(); - let block_hash_u128 = log_state_update.block_hash.low_u128(); - - if global_root_u128 != log_state_update.global_root.low_u128() - || block_hash_u128 != log_state_update.block_hash.low_u128() - { - return Err("Conversion from U256 to u128 resulted in data loss"); - } - - let global_root = StarkFelt::from(global_root_u128); - let block_hash = StarkFelt::from(block_hash_u128); + let global_root = u256_to_starkfelt(log_state_update.global_root)?; + let block_hash = u256_to_starkfelt(log_state_update.block_hash)?; - Ok(L1StateUpdate { block_number: block_number_u64, global_root, block_hash }) + Ok(L1StateUpdate { block_number, global_root, block_hash }) }