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 1ed59b5 commit d8f0ab6
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 28 deletions.
25 changes: 20 additions & 5 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)
self.batch_index_of_beacon_slot(slot, BatchFinality::Finalized)
.then(|bi| self.scroll_height_of_batch_index(bi))
.await
}
Expand Down Expand Up @@ -173,7 +173,11 @@ impl Scroll {
})
}

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

pub enum BatchFinality {
Committed,
Finalized,
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct ScrollChainType;
impl FromStrExact for ScrollChainType {
Expand Down Expand Up @@ -292,7 +307,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)
.batch_index_of_beacon_slot(height.revision_height, BatchFinality::Finalized)
.await,
latest_batch_index_slot: self.rollup_last_finalized_batch_index_slot,
frozen_height: Height {
Expand All @@ -309,7 +324,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)
.batch_index_of_beacon_slot(height.revision_height, BatchFinality::Finalized)
.await;
let scroll_height = self.scroll_height_of_batch_index(batch_index).await;

Expand Down
5 changes: 3 additions & 2 deletions lib/relay-message/src/chain_impls/cosmos_sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,12 @@ where
])
}

async fn query_client_state(
async fn query_unfinalized_trusted_client_state(
hc: &Hc,
client_id: Hc::ClientId,
height: Hc::Height,
) -> Hc::StoredClientState<Tr> {
let height = hc.query_latest_height().await.unwrap();

let QueueMsg::Data(relayer_msg) = fetch_abci_query::<Hc, Tr>(
hc,
ClientStatePath { client_id }.into(),
Expand Down
5 changes: 3 additions & 2 deletions lib/relay-message/src/chain_impls/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,12 @@ where
))
}

async fn query_client_state(
async fn query_unfinalized_trusted_client_state(
hc: &Self,
client_id: Self::ClientId,
height: Self::Height,
) -> 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)
Expand Down
44 changes: 30 additions & 14 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::Scroll,
scroll::{BatchFinality, Scroll},
};
use ethers::{abi::AbiDecode, providers::Middleware};
use frunk::{hlist_pat, HList};
Expand All @@ -18,7 +18,7 @@ use unionlabs::{
encoding::{Decode, Encode, EthAbi},
hash::{H160, H256},
ibc::{
core::client::{height::IsHeight, msg_update_client::MsgUpdateClient},
core::client::msg_update_client::MsgUpdateClient,
lightclients::{
ethereum::{account_proof::AccountProof, storage_proof::StorageProof},
scroll,
Expand Down Expand Up @@ -104,18 +104,34 @@ where
))
}

async fn query_client_state(
async fn query_unfinalized_trusted_client_state(
hc: &Self,
client_id: Self::ClientId,
height: Self::Height,
) -> Tr::SelfClientState {
hc.ibc_handler()
.ibc_state_read::<_, Scroll, Tr>(
hc.execution_height_of_beacon_slot(height.revision_height())
.await,
ClientStatePath { client_id },
) -> 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;

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

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

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

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

let batch = scroll.scroll_api_client.batch(batch_index).await;
Expand Down
7 changes: 7 additions & 0 deletions lib/relay-message/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum Data<Hc: ChainExt, Tr: ChainExt> {
SelfConsensusState(SelfConsensusState<Hc, Tr>),

LatestHeight(LatestHeight<Hc, Tr>),
UnfinalizedClientState(UnfinalizedTrustedClientState<Hc, Tr>),

PacketAcknowledgement(PacketAcknowledgement<Hc, Tr>),

Expand Down Expand Up @@ -84,6 +85,12 @@ pub struct LatestHeight<Hc: ChainExt, #[cover] Tr: ChainExt> {
pub height: HeightOf<Hc>,
}

#[queue_msg]
pub struct UnfinalizedTrustedClientState<Hc: ChainExt, Tr: ChainExt> {
pub height: HeightOf<Hc>,
pub client_state: Hc::StoredClientState<Tr>,
}

#[queue_msg]
pub struct Header<Hc: ChainExt, #[cover] Tr: ChainExt> {
pub header: HeaderOf<Hc>,
Expand Down
22 changes: 22 additions & 0 deletions lib/relay-message/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub enum Fetch<Hc: ChainExt, Tr: ChainExt> {

LatestHeight(FetchLatestHeight<Hc, Tr>),

UnfinalizedTrustedClientState(FetchUnfinalizedTrustedClientState<Hc, Tr>),

SelfClientState(FetchSelfClientState<Hc, Tr>),
SelfConsensusState(FetchSelfConsensusState<Hc, Tr>),

Expand Down Expand Up @@ -106,6 +108,11 @@ pub struct FetchUpdateHeaders<Hc: ChainExt, Tr: ChainExt> {
#[queue_msg]
pub struct FetchLatestHeight<#[cover] Hc: ChainExt, #[cover] Tr: ChainExt> {}

#[queue_msg]
pub struct FetchUnfinalizedTrustedClientState<Hc: ChainExt, #[cover] Tr: ChainExt> {
client_id: Hc::ClientId,
}

#[queue_msg]
pub struct LightClientSpecificFetch<Hc: ChainExt, Tr: ChainExt>(pub Hc::Fetch<Tr>);

Expand Down Expand Up @@ -140,6 +147,21 @@ where
__marker: PhantomData,
},
)),
Fetch::UnfinalizedTrustedClientState(FetchUnfinalizedTrustedClientState {
client_id,
__marker: _,
}) => {
let _client_state = Hc::query_unfinalized_trusted_client_state(&c, client_id).await;

// data(id(
// c.chain_id(),
// UnfinalizedTrustedClientState {
// height,
// client_state,
// },
// ))
todo!()
}
Fetch::SelfClientState(FetchSelfClientState {
at: height,
__marker: _,
Expand Down
3 changes: 1 addition & 2 deletions lib/relay-message/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,9 @@ pub trait DoFetchState<Hc: ChainExt, Tr: ChainExt>: ChainExt {
fn state(hc: &Hc, at: Hc::Height, path: PathOf<Hc, Tr>) -> QueueMsg<RelayMessageTypes>;

// SEE: <https://github.com/unionlabs/union/issues/1813>
fn query_client_state(
fn query_unfinalized_trusted_client_state(
hc: &Hc,
client_id: Hc::ClientId,
height: Hc::Height,
) -> impl Future<Output = Hc::StoredClientState<Tr>> + '_;
}

Expand Down
4 changes: 1 addition & 3 deletions lib/relay-message/src/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ where
counterparty_chain_id,
height,
}) => {
let latest_height = c.query_latest_height_as_destination().await.unwrap();

let trusted_client_state =
Hc::query_client_state(c, client_id.clone(), latest_height).await;
Hc::query_unfinalized_trusted_client_state(c, client_id.clone()).await;

if trusted_client_state.height().revision_height() >= height.revision_height() {
tracing::debug!(
Expand Down
15 changes: 15 additions & 0 deletions lib/scroll-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ impl ScrollClient {
.await
.unwrap()
}

// #[instrument(level = "debug", skip(self))]
// pub async fn batches(&self, page: u64, per_page: u64) -> BatchResponse {
// self.client
// .get(format!(
// "{}/api/batches?page={page}&per_page={per_page}",
// self.base_url
// ))
// .send()
// .await
// .unwrap()
// .json()
// .await
// .unwrap()
// }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down

0 comments on commit d8f0ab6

Please sign in to comment.