Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(l1): fixed l1 thread to reflect correct state_root, block_number, block_hash #14

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions crates/client/sync/src/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
27 changes: 12 additions & 15 deletions crates/client/sync/src/utils/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,24 +181,21 @@ pub fn format_address(address: &str) -> String {
}
}

pub fn event_to_l1_state_update(log_state_update: LogStateUpdate) -> Result<L1StateUpdate, &'static str> {
let block_number_u64 = if log_state_update.block_number >= I256::from(0) {
pub fn u256_to_starkfelt(u256: U256) -> Result<StarkFelt, &'static str> {
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<L1StateUpdate, &'static str> {
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 })
}
Loading