Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo committed Apr 30, 2024
1 parent d8f0ab6 commit dce9e0f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 49 deletions.
25 changes: 5 additions & 20 deletions lib/chain-utils/src/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct Config {

impl EthereumChain for Scroll {
async fn execution_height_of_beacon_slot(&self, slot: u64) -> u64 {
self.batch_index_of_beacon_slot(slot, BatchFinality::Finalized)
self.batch_index_of_beacon_slot(slot)
.then(|bi| self.scroll_height_of_batch_index(bi))
.await
}
Expand Down Expand Up @@ -173,11 +173,7 @@ impl Scroll {
})
}

pub async fn batch_index_of_beacon_slot(
&self,
slot: u64,
batch_finality: BatchFinality,
) -> u64 {
pub async fn batch_index_of_beacon_slot(&self, slot: u64) -> u64 {
let execution_height = self
.l1
.beacon_api_client
Expand All @@ -190,13 +186,7 @@ impl Scroll {
.provider
.get_storage_at(
ethers::types::H160(self.rollup_contract_address.0),
H256::from(match batch_finality {
BatchFinality::Committed => self.rollup_committed_batches_slot.to_be_bytes(),
BatchFinality::Finalized => {
self.rollup_last_finalized_batch_index_slot.to_be_bytes()
}
})
.into(),
H256::from(self.rollup_last_finalized_batch_index_slot.to_be_bytes()).into(),
Some(ethers::types::BlockId::Number(
ethers::types::BlockNumber::Number(execution_height.into()),
)),
Expand Down Expand Up @@ -226,11 +216,6 @@ impl Scroll {
}
}

pub enum BatchFinality {
Committed,
Finalized,
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct ScrollChainType;
impl FromStrExact for ScrollChainType {
Expand Down Expand Up @@ -307,7 +292,7 @@ impl Chain for Scroll {
l1_client_id: self.l1_client_id.to_string(),
chain_id: self.chain_id(),
latest_batch_index: self
.batch_index_of_beacon_slot(height.revision_height, BatchFinality::Finalized)
.batch_index_of_beacon_slot(height.revision_height)
.await,
latest_batch_index_slot: self.rollup_last_finalized_batch_index_slot,
frozen_height: Height {
Expand All @@ -324,7 +309,7 @@ impl Chain for Scroll {

async fn self_consensus_state(&self, height: Self::Height) -> Self::SelfConsensusState {
let batch_index = self
.batch_index_of_beacon_slot(height.revision_height, BatchFinality::Finalized)
.batch_index_of_beacon_slot(height.revision_height)
.await;
let scroll_height = self.scroll_height_of_batch_index(batch_index).await;

Expand Down
5 changes: 1 addition & 4 deletions lib/relay-message/src/chain_impls/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,9 @@ where
hc: &Self,
client_id: Self::ClientId,
) -> Tr::SelfClientState {
let height = hc.query_latest_height_as_destination().await.unwrap();

hc.ibc_handler()
.ibc_state_read::<_, Ethereum<C>, Tr>(
hc.execution_height_of_beacon_slot(height.revision_height)
.await,
hc.provider.get_block_number().await.unwrap().as_u64(),
ClientStatePath { client_id },
)
.await
Expand Down
31 changes: 6 additions & 25 deletions lib/relay-message/src/chain_impls/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use chain_utils::{
ethereum::{EthereumChain, EthereumChainExt, IbcHandlerExt},
scroll::{BatchFinality, Scroll},
scroll::Scroll,
};
use ethers::{abi::AbiDecode, providers::Middleware};
use frunk::{hlist_pat, HList};
Expand Down Expand Up @@ -108,26 +108,7 @@ where
hc: &Self,
client_id: Self::ClientId,
) -> Self::StoredClientState<Tr> {
let storage = hc
.l1
.provider
.get_storage_at(
ethers::types::H160(hc.rollup_contract_address.0),
H256::from(hc.rollup_committed_batches_slot.to_be_bytes()).into(),
Some(ethers::types::BlockId::Number(
ethers::types::BlockNumber::Latest,
)),
)
.await
.unwrap();

let batch_index = U256::from_be_bytes(storage.to_fixed_bytes())
.try_into()
.unwrap();

// tracing::debug!("execution height {execution_height} is batch index {batch_index}");

let latest_scroll_height = hc.scroll_height_of_batch_index(batch_index).await;
let latest_scroll_height = hc.provider.get_block_number().await.unwrap().as_u64();

hc.ibc_handler()
.ibc_state_read::<_, Scroll, Tr>(latest_scroll_height, ClientStatePath { client_id })
Expand Down Expand Up @@ -309,7 +290,7 @@ where
rollup_contract_address,
}) => {
let batch_index = scroll
.batch_index_of_beacon_slot(height.revision_height, BatchFinality::Finalized)
.batch_index_of_beacon_slot(height.revision_height)
.await;

let batch_hash_proof = scroll
Expand Down Expand Up @@ -367,7 +348,7 @@ where
ibc_contract_address,
}) => {
let batch_index = scroll
.batch_index_of_beacon_slot(height.revision_height, BatchFinality::Finalized)
.batch_index_of_beacon_slot(height.revision_height)
.await;

let batch = scroll.scroll_api_client.batch(batch_index).await;
Expand Down Expand Up @@ -401,7 +382,7 @@ where
rollup_contract_address,
}) => {
let batch_index = scroll
.batch_index_of_beacon_slot(height.revision_height, BatchFinality::Finalized)
.batch_index_of_beacon_slot(height.revision_height)
.await;

let batch_hash_proof = scroll
Expand Down Expand Up @@ -459,7 +440,7 @@ where
rollup_contract_address: _,
}) => {
let batch_index = scroll
.batch_index_of_beacon_slot(height.revision_height, BatchFinality::Finalized)
.batch_index_of_beacon_slot(height.revision_height)
.await;

let batch = scroll.scroll_api_client.batch(batch_index).await;
Expand Down

0 comments on commit dce9e0f

Please sign in to comment.