diff --git a/crates/papyrus_base_layer/src/base_layer_test.rs b/crates/papyrus_base_layer/src/base_layer_test.rs index 1793ffe674..43eb3db327 100644 --- a/crates/papyrus_base_layer/src/base_layer_test.rs +++ b/crates/papyrus_base_layer/src/base_layer_test.rs @@ -1,5 +1,5 @@ use pretty_assertions::assert_eq; -use starknet_api::block::{BlockHash, BlockNumber}; +use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber}; use starknet_api::felt; use crate::ethereum_base_layer_contract::{EthereumBaseLayerConfig, EthereumBaseLayerContract}; @@ -26,11 +26,14 @@ async fn latest_proved_block_ethereum() { }; let contract = EthereumBaseLayerContract::new(config); - let first_sn_state_update = (BlockNumber(100), BlockHash(felt!("0x100"))); - let second_sn_state_update = (BlockNumber(200), BlockHash(felt!("0x200"))); - let third_sn_state_update = (BlockNumber(300), BlockHash(felt!("0x300"))); + let first_sn_state_update = + BlockHashAndNumber { number: BlockNumber(100), hash: BlockHash(felt!("0x100")) }; + let second_sn_state_update = + BlockHashAndNumber { number: BlockNumber(200), hash: BlockHash(felt!("0x200")) }; + let third_sn_state_update = + BlockHashAndNumber { number: BlockNumber(300), hash: BlockHash(felt!("0x300")) }; - type Scenario = (u64, Option<(BlockNumber, BlockHash)>); + type Scenario = (u64, Option); let scenarios: Vec = vec![ (0, Some(third_sn_state_update)), (5, Some(third_sn_state_update)), diff --git a/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs b/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs index 7e43cc7979..f29aca518a 100644 --- a/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs +++ b/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs @@ -13,7 +13,7 @@ use async_trait::async_trait; use papyrus_config::dumping::{ser_param, ser_required_param, SerializeConfig}; use papyrus_config::{ParamPath, ParamPrivacyInput, SerializationType, SerializedParam}; use serde::{Deserialize, Serialize}; -use starknet_api::block::{BlockHash, BlockNumber}; +use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber}; use starknet_api::hash::StarkHash; use starknet_types_core::felt; use url::Url; @@ -55,10 +55,8 @@ impl BaseLayerContract for EthereumBaseLayerContract { async fn latest_proved_block( &self, finality: u64, - ) -> EthereumBaseLayerResult> { - let ethereum_block_number = - self.contract.provider().get_block_number().await?.checked_sub(finality); - let Some(ethereum_block_number) = ethereum_block_number else { + ) -> EthereumBaseLayerResult> { + let Some(ethereum_block_number) = self.latest_l1_block_number(finality).await? else { return Ok(None); }; @@ -75,7 +73,14 @@ impl BaseLayerContract for EthereumBaseLayerContract { let validate = true; let block_number = sol_data::Uint::<64>::abi_decode(&state_block_number, validate)?; let block_hash = sol_data::FixedBytes::<32>::abi_decode(&state_block_hash, validate)?; - Ok(Some((BlockNumber(block_number), BlockHash(StarkHash::from_bytes_be(&block_hash))))) + Ok(Some(BlockHashAndNumber { + number: BlockNumber(block_number), + hash: BlockHash(StarkHash::from_bytes_be(&block_hash)), + })) + } + + async fn latest_l1_block_number(&self, finality: u64) -> EthereumBaseLayerResult> { + Ok(self.contract.provider().get_block_number().await?.checked_sub(finality)) } } diff --git a/crates/papyrus_base_layer/src/lib.rs b/crates/papyrus_base_layer/src/lib.rs index a3bc340cce..271fd0990a 100644 --- a/crates/papyrus_base_layer/src/lib.rs +++ b/crates/papyrus_base_layer/src/lib.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use starknet_api::block::{BlockHash, BlockNumber}; +use starknet_api::block::BlockHashAndNumber; pub mod ethereum_base_layer_contract; @@ -19,5 +19,7 @@ pub trait BaseLayerContract { async fn latest_proved_block( &self, finality: u64, - ) -> Result, Self::Error>; + ) -> Result, Self::Error>; + + async fn latest_l1_block_number(&self, finality: u64) -> Result, Self::Error>; } diff --git a/crates/papyrus_sync/src/sources/base_layer.rs b/crates/papyrus_sync/src/sources/base_layer.rs index 2ecac3b8c4..9478c29b21 100644 --- a/crates/papyrus_sync/src/sources/base_layer.rs +++ b/crates/papyrus_sync/src/sources/base_layer.rs @@ -39,6 +39,7 @@ impl< let finality = 0; self.latest_proved_block(finality) .await + .map(|block| block.map(|block| (block.number, block.hash))) .map_err(|e| BaseLayerSourceError::BaseLayerContractError(Box::new(e))) } }