Skip to content

Commit

Permalink
refactor(papyrus_base_layer): extract last l1 number
Browse files Browse the repository at this point in the history
- add l1 latest block getter to base layer trait (will be used in later
  commits).
- use `BlockHashAndNumber` instead of tuple.
  • Loading branch information
Gilad Chase committed Dec 20, 2024
1 parent 0dd60ea commit 0a5c0c0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
13 changes: 8 additions & 5 deletions crates/papyrus_base_layer/src/base_layer_test.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<BlockHashAndNumber>);
let scenarios: Vec<Scenario> = vec![
(0, Some(third_sn_state_update)),
(5, Some(third_sn_state_update)),
Expand Down
17 changes: 11 additions & 6 deletions crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,10 +55,8 @@ impl BaseLayerContract for EthereumBaseLayerContract {
async fn latest_proved_block(
&self,
finality: u64,
) -> EthereumBaseLayerResult<Option<(BlockNumber, BlockHash)>> {
let ethereum_block_number =
self.contract.provider().get_block_number().await?.checked_sub(finality);
let Some(ethereum_block_number) = ethereum_block_number else {
) -> EthereumBaseLayerResult<Option<BlockHashAndNumber>> {
let Some(ethereum_block_number) = self.latest_l1_block_number(finality).await? else {
return Ok(None);
};

Expand All @@ -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<Option<u64>> {
Ok(self.contract.provider().get_block_number().await?.checked_sub(finality))
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/papyrus_base_layer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,5 +19,7 @@ pub trait BaseLayerContract {
async fn latest_proved_block(
&self,
finality: u64,
) -> Result<Option<(BlockNumber, BlockHash)>, Self::Error>;
) -> Result<Option<BlockHashAndNumber>, Self::Error>;

async fn latest_l1_block_number(&self, finality: u64) -> Result<Option<u64>, Self::Error>;
}
1 change: 1 addition & 0 deletions crates/papyrus_sync/src/sources/base_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}

0 comments on commit 0a5c0c0

Please sign in to comment.