Skip to content

Commit

Permalink
feat(voyager): split the LightClient trait into two
Browse files Browse the repository at this point in the history
splits out the types and methods related to the queue into a new trait
  • Loading branch information
benluelo committed Oct 22, 2023
1 parent 9b17887 commit 9a47214
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 266 deletions.
41 changes: 24 additions & 17 deletions voyager/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ impl AnyChain {
}

/// The IBC interface on a [`Chain`] that knows how to connect to a counterparty.
pub trait LightClient: Send + Sync + Sized {
pub trait LightClientBase: Send + Sync + Sized {
/// The chain that this light client is on.
type HostChain: Chain + IbcStateReadPaths<<Self::Counterparty as LightClient>::HostChain>;
type Counterparty: LightClient<Counterparty = Self>;
type HostChain: Chain + IbcStateReadPaths<<Self::Counterparty as LightClientBase>::HostChain>;
type Counterparty: LightClientBase<Counterparty = Self>;

type ClientId: traits::Id
+ TryFrom<<Self::HostChain as Chain>::ClientId>
Expand All @@ -113,6 +113,24 @@ pub trait LightClient: Send + Sync + Sized {
/// The config required to construct this light client.
type Config: Debug + Clone + PartialEq + Serialize + for<'de> Deserialize<'de>;

/// Get the underlying [`Self::HostChain`] that this client is on.
fn chain(&self) -> &Self::HostChain;

fn from_chain(chain: Self::HostChain) -> Self;

// TODO: Use state_proof instead
fn query_client_state(
&self,
// TODO: Make this Into<_>
client_id: <Self::HostChain as Chain>::ClientId,
height: HeightOf<Self::HostChain>,
) -> impl Future<Output = ClientStateOf<<Self::Counterparty as LightClientBase>::HostChain>> + '_;
}

pub trait LightClient: LightClientBase<Counterparty = Self::BaseCounterparty> {
// https://github.com/rust-lang/rust/issues/20671
type BaseCounterparty: LightClient<BaseCounterparty = Self, Counterparty = Self>;

type Data: Debug
+ Display
+ Clone
Expand Down Expand Up @@ -141,19 +159,6 @@ pub trait LightClient: Send + Sync + Sized {

fn msg(&self, msg: Msg<Self>) -> impl Future<Output = Result<(), Self::MsgError>> + '_;

/// Get the underlying [`Self::HostChain`] that this client is on.
fn chain(&self) -> &Self::HostChain;

fn from_chain(chain: Self::HostChain) -> Self;

// TODO: Use state_proof instead
fn query_client_state(
&self,
// TODO: Make this Into<_>
client_id: <Self::HostChain as Chain>::ClientId,
height: HeightOf<Self::HostChain>,
) -> impl Future<Output = ClientStateOf<<Self::Counterparty as LightClient>::HostChain>> + '_;

fn do_fetch(&self, msg: Self::Fetch) -> impl Future<Output = Vec<RelayerMsg>> + '_;

// Should (eventually) resolve to UpdateClientData
Expand All @@ -163,11 +168,13 @@ pub trait LightClient: Send + Sync + Sized {
) -> Vec<RelayerMsg>;
}

trait LightClientExt = LightClient where <Self as LightClientBase>::Counterparty: LightClient;

pub type ClientStateOf<C> = <C as Chain>::SelfClientState;
pub type ConsensusStateOf<C> = <C as Chain>::SelfConsensusState;
pub type HeaderOf<C> = <C as Chain>::Header;
pub type HeightOf<C> = <C as Chain>::Height;
pub type ChainOf<L> = <L as LightClient>::HostChain;
pub type ChainOf<L> = <L as LightClientBase>::HostChain;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
Expand Down
88 changes: 41 additions & 47 deletions voyager/src/chain/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ use unionlabs::{
CommitmentPath, ConnectionPath, IbcPath,
},
traits::{Chain, ClientState},
IntoEthAbi, IntoProto, TryFromProto, TryFromProtoErrorOf,
EthAbi, IntoEthAbi, IntoProto, Proto, TryFromProto, TryFromProtoErrorOf,
};

use crate::{
chain::{
try_from_relayer_msg,
union::{EthereumMainnet, EthereumMinimal},
ClientStateOf, ConsensusStateOf, HeaderOf, HeightOf, IbcStateRead, LightClient,
QueryHeight, StateProof,
LightClientBase, QueryHeight, StateProof,
},
msg::{
aggregate::{Aggregate, AnyAggregate, LightClientSpecificAggregate},
Expand Down Expand Up @@ -183,7 +183,7 @@ pub async fn setup_initial_channel<C: ChainSpec>(
todo!()
}

impl LightClient for CometblsMainnet {
impl LightClientBase for CometblsMainnet {
type HostChain = Evm<Mainnet>;
type Counterparty = EthereumMainnet;

Expand All @@ -192,16 +192,6 @@ impl LightClient for CometblsMainnet {

type Config = CometblsConfig;

type Data = CometblsDataMsg<Mainnet>;
type Fetch = CometblsFetchMsg<Mainnet>;
type Aggregate = CometblsAggregateMsg<Self, Mainnet>;

type MsgError = TxSubmitError;

fn msg(&self, msg: Msg<Self>) -> impl Future<Output = Result<(), Self::MsgError>> + '_ {
self::msg(&self.chain, msg)
}

fn chain(&self) -> &Self::HostChain {
&self.chain
}
Expand All @@ -214,10 +204,24 @@ impl LightClient for CometblsMainnet {
&self,
client_id: <Self::HostChain as Chain>::ClientId,
height: HeightOf<Self::HostChain>,
) -> impl Future<Output = ClientStateOf<<Self::Counterparty as LightClient>::HostChain>> + '_
) -> impl Future<Output = ClientStateOf<<Self::Counterparty as LightClientBase>::HostChain>> + '_
{
query_client_state(&self.chain, client_id, height)
}
}

impl LightClient for CometblsMainnet {
type BaseCounterparty = Self::Counterparty;

type Data = CometblsDataMsg<Mainnet>;
type Fetch = CometblsFetchMsg<Mainnet>;
type Aggregate = CometblsAggregateMsg<Self, Mainnet>;

type MsgError = TxSubmitError;

fn msg(&self, msg: Msg<Self>) -> impl Future<Output = Result<(), Self::MsgError>> + '_ {
self::msg(&self.chain, msg)
}

fn do_fetch(&self, msg: Self::Fetch) -> impl Future<Output = Vec<RelayerMsg>> + '_ {
do_fetch::<_, Self>(&self.chain, msg)
Expand All @@ -231,7 +235,7 @@ impl LightClient for CometblsMainnet {
}
}

impl LightClient for CometblsMinimal {
impl LightClientBase for CometblsMinimal {
type HostChain = Evm<Minimal>;
type Counterparty = EthereumMinimal;

Expand All @@ -240,16 +244,6 @@ impl LightClient for CometblsMinimal {

type Config = CometblsConfig;

type Data = CometblsDataMsg<Minimal>;
type Fetch = CometblsFetchMsg<Minimal>;
type Aggregate = CometblsAggregateMsg<Self, Minimal>;

type MsgError = TxSubmitError;

fn msg(&self, msg: Msg<Self>) -> impl Future<Output = Result<(), Self::MsgError>> + '_ {
self::msg(&self.chain, msg)
}

fn chain(&self) -> &Self::HostChain {
&self.chain
}
Expand All @@ -262,10 +256,24 @@ impl LightClient for CometblsMinimal {
&self,
client_id: <Self::HostChain as Chain>::ClientId,
height: HeightOf<Self::HostChain>,
) -> impl Future<Output = ClientStateOf<<Self::Counterparty as LightClient>::HostChain>> + '_
) -> impl Future<Output = ClientStateOf<<Self::Counterparty as LightClientBase>::HostChain>> + '_
{
query_client_state(&self.chain, client_id, height)
}
}

impl LightClient for CometblsMinimal {
type BaseCounterparty = Self::Counterparty;

type Data = CometblsDataMsg<Minimal>;
type Fetch = CometblsFetchMsg<Minimal>;
type Aggregate = CometblsAggregateMsg<Self, Minimal>;

type MsgError = TxSubmitError;

fn msg(&self, msg: Msg<Self>) -> impl Future<Output = Result<(), Self::MsgError>> + '_ {
self::msg(&self.chain, msg)
}

fn do_fetch(&self, msg: Self::Fetch) -> impl Future<Output = Vec<RelayerMsg>> + '_ {
do_fetch::<_, Self>(&self.chain, msg)
Expand Down Expand Up @@ -713,24 +721,6 @@ pub struct FinalityUpdate<C: ChainSpec>(pub LightClientFinalityUpdate<C>);
#[serde(bound(serialize = "", deserialize = ""))]
pub struct LightClientUpdates<C: ChainSpec>(pub Vec<LightClientUpdate<C>>);

// fn outer<L, C>()
// where
// C: ChainSpec,
// L: LightClient<
// HostChain = Evm<C>,
// Aggregate = CometblsAggregateMsg<L, C>,
// Fetch = CometblsFetchMsg<C>,
// >,
// {
// fn lc_specific<
// T: TryFrom<AnyLightClientIdentified<AnyData>, Error = AnyLightClientIdentified<AnyData>>
// + Into<AnyLightClientIdentified<AnyData>>,
// >() {
// }

// lc_specific::<Identified<L, AccountUpdateData<C>>>()
// }

impl<C, L> DoAggregate<L> for CometblsAggregateMsg<L, C>
where
C: ChainSpec,
Expand Down Expand Up @@ -951,9 +941,13 @@ async fn msg<C, L>(evm: &Evm<C>, msg: Msg<L>) -> Result<(), TxSubmitError>
where
C: ChainSpec,
L: LightClient<HostChain = Evm<C>, Config = CometblsConfig>,
ClientStateOf<<L::Counterparty as LightClient>::HostChain>: IntoProto,
ConsensusStateOf<<L::Counterparty as LightClient>::HostChain>: IntoProto,
HeaderOf<<L::Counterparty as LightClient>::HostChain>: IntoEthAbi,
ClientStateOf<<L::Counterparty as LightClientBase>::HostChain>: Proto + IntoProto,
ConsensusStateOf<<L::Counterparty as LightClientBase>::HostChain>: Proto + IntoProto,
HeaderOf<<L::Counterparty as LightClientBase>::HostChain>: EthAbi + IntoEthAbi,
// not sure why these bounds are required
<<L::BaseCounterparty as LightClientBase>::HostChain as Chain>::SelfClientState: Proto,
<<L::BaseCounterparty as LightClientBase>::HostChain as Chain>::SelfConsensusState: Proto,
<<L::BaseCounterparty as LightClientBase>::HostChain as Chain>::Header: EthAbi,
{
evm.ibc_handlers
.with(|ibc_handler| async move {
Expand Down
Loading

0 comments on commit 9a47214

Please sign in to comment.