From d8f0ab6c963c71efe56d8c75bcf32235ff4d33af Mon Sep 17 00:00:00 2001 From: benluelo Date: Tue, 30 Apr 2024 14:46:36 -0400 Subject: [PATCH] wip --- lib/chain-utils/src/scroll.rs | 25 ++++++++--- .../src/chain_impls/cosmos_sdk.rs | 5 ++- lib/relay-message/src/chain_impls/ethereum.rs | 5 ++- lib/relay-message/src/chain_impls/scroll.rs | 44 +++++++++++++------ lib/relay-message/src/data.rs | 7 +++ lib/relay-message/src/fetch.rs | 22 ++++++++++ lib/relay-message/src/lib.rs | 3 +- lib/relay-message/src/wait.rs | 4 +- lib/scroll-api/src/lib.rs | 15 +++++++ 9 files changed, 102 insertions(+), 28 deletions(-) diff --git a/lib/chain-utils/src/scroll.rs b/lib/chain-utils/src/scroll.rs index 9aac274faf..3992893c9d 100644 --- a/lib/chain-utils/src/scroll.rs +++ b/lib/chain-utils/src/scroll.rs @@ -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 } @@ -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 @@ -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()), )), @@ -216,6 +226,11 @@ impl Scroll { } } +pub enum BatchFinality { + Committed, + Finalized, +} + #[derive(Debug, Clone, PartialEq, Default)] pub struct ScrollChainType; impl FromStrExact for ScrollChainType { @@ -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 { @@ -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; diff --git a/lib/relay-message/src/chain_impls/cosmos_sdk.rs b/lib/relay-message/src/chain_impls/cosmos_sdk.rs index 440c3c4a5f..d46d2ee171 100644 --- a/lib/relay-message/src/chain_impls/cosmos_sdk.rs +++ b/lib/relay-message/src/chain_impls/cosmos_sdk.rs @@ -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 { + let height = hc.query_latest_height().await.unwrap(); + let QueueMsg::Data(relayer_msg) = fetch_abci_query::( hc, ClientStatePath { client_id }.into(), diff --git a/lib/relay-message/src/chain_impls/ethereum.rs b/lib/relay-message/src/chain_impls/ethereum.rs index fe84ad49ac..8f75d56bf2 100644 --- a/lib/relay-message/src/chain_impls/ethereum.rs +++ b/lib/relay-message/src/chain_impls/ethereum.rs @@ -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, Tr>( hc.execution_height_of_beacon_slot(height.revision_height) diff --git a/lib/relay-message/src/chain_impls/scroll.rs b/lib/relay-message/src/chain_impls/scroll.rs index 23fbd5da65..063ccc83f6 100644 --- a/lib/relay-message/src/chain_impls/scroll.rs +++ b/lib/relay-message/src/chain_impls/scroll.rs @@ -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}; @@ -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, @@ -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 { + 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() } } @@ -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 @@ -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; @@ -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 @@ -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; diff --git a/lib/relay-message/src/data.rs b/lib/relay-message/src/data.rs index 8f943ae68e..0cec33ba8e 100644 --- a/lib/relay-message/src/data.rs +++ b/lib/relay-message/src/data.rs @@ -24,6 +24,7 @@ pub enum Data { SelfConsensusState(SelfConsensusState), LatestHeight(LatestHeight), + UnfinalizedClientState(UnfinalizedTrustedClientState), PacketAcknowledgement(PacketAcknowledgement), @@ -84,6 +85,12 @@ pub struct LatestHeight { pub height: HeightOf, } +#[queue_msg] +pub struct UnfinalizedTrustedClientState { + pub height: HeightOf, + pub client_state: Hc::StoredClientState, +} + #[queue_msg] pub struct Header { pub header: HeaderOf, diff --git a/lib/relay-message/src/fetch.rs b/lib/relay-message/src/fetch.rs index b9b0f1f2f1..acf54df7eb 100644 --- a/lib/relay-message/src/fetch.rs +++ b/lib/relay-message/src/fetch.rs @@ -31,6 +31,8 @@ pub enum Fetch { LatestHeight(FetchLatestHeight), + UnfinalizedTrustedClientState(FetchUnfinalizedTrustedClientState), + SelfClientState(FetchSelfClientState), SelfConsensusState(FetchSelfConsensusState), @@ -106,6 +108,11 @@ pub struct FetchUpdateHeaders { #[queue_msg] pub struct FetchLatestHeight<#[cover] Hc: ChainExt, #[cover] Tr: ChainExt> {} +#[queue_msg] +pub struct FetchUnfinalizedTrustedClientState { + client_id: Hc::ClientId, +} + #[queue_msg] pub struct LightClientSpecificFetch(pub Hc::Fetch); @@ -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: _, diff --git a/lib/relay-message/src/lib.rs b/lib/relay-message/src/lib.rs index 4f5724cf28..ea30243a1e 100644 --- a/lib/relay-message/src/lib.rs +++ b/lib/relay-message/src/lib.rs @@ -394,10 +394,9 @@ pub trait DoFetchState: ChainExt { fn state(hc: &Hc, at: Hc::Height, path: PathOf) -> QueueMsg; // SEE: - fn query_client_state( + fn query_unfinalized_trusted_client_state( hc: &Hc, client_id: Hc::ClientId, - height: Hc::Height, ) -> impl Future> + '_; } diff --git a/lib/relay-message/src/wait.rs b/lib/relay-message/src/wait.rs index e66012e66f..9e29923b92 100644 --- a/lib/relay-message/src/wait.rs +++ b/lib/relay-message/src/wait.rs @@ -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!( diff --git a/lib/scroll-api/src/lib.rs b/lib/scroll-api/src/lib.rs index 23284826fc..6210971870 100644 --- a/lib/scroll-api/src/lib.rs +++ b/lib/scroll-api/src/lib.rs @@ -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)]