|
| 1 | +use bob_light_client_types::{ClientStateV1, ConsensusState, Header}; |
| 2 | +use cosmwasm_std::{Addr, Empty}; |
| 3 | +use ethereum_light_client::client::EthereumLightClient; |
| 4 | +use ethereum_light_client_types::StorageProof; |
| 5 | +use ibc_union_light_client::{ |
| 6 | + ClientCreationResult, IbcClient, IbcClientCtx, IbcClientError, StateUpdate, |
| 7 | +}; |
| 8 | +use ibc_union_msg::lightclient::Status; |
| 9 | +use unionlabs::encoding::Bincode; |
| 10 | + |
| 11 | +use crate::errors::Error; |
| 12 | + |
| 13 | +pub struct BobLightClient; |
| 14 | + |
| 15 | +impl IbcClient for BobLightClient { |
| 16 | + type Error = Error; |
| 17 | + |
| 18 | + type Header = Header; |
| 19 | + |
| 20 | + type Misbehaviour = (); |
| 21 | + |
| 22 | + type ClientState = ClientStateV1; |
| 23 | + |
| 24 | + type ConsensusState = ConsensusState; |
| 25 | + |
| 26 | + type Encoding = Bincode; |
| 27 | + |
| 28 | + type CustomQuery = Empty; |
| 29 | + |
| 30 | + type StorageProof = StorageProof; |
| 31 | + |
| 32 | + fn verify_membership( |
| 33 | + ctx: IbcClientCtx<Self>, |
| 34 | + height: u64, |
| 35 | + key: Vec<u8>, |
| 36 | + storage_proof: Self::StorageProof, |
| 37 | + value: Vec<u8>, |
| 38 | + ) -> Result<(), IbcClientError<Self>> { |
| 39 | + let consensus_state = ctx.read_self_consensus_state(height)?; |
| 40 | + ethereum_light_client::client::verify_membership( |
| 41 | + key, |
| 42 | + consensus_state.ibc_storage_root, |
| 43 | + storage_proof, |
| 44 | + value, |
| 45 | + ) |
| 46 | + .map_err(Into::<Error>::into)?; |
| 47 | + Ok(()) |
| 48 | + } |
| 49 | + |
| 50 | + fn verify_non_membership( |
| 51 | + ctx: IbcClientCtx<Self>, |
| 52 | + height: u64, |
| 53 | + key: Vec<u8>, |
| 54 | + storage_proof: Self::StorageProof, |
| 55 | + ) -> Result<(), IbcClientError<Self>> { |
| 56 | + let consensus_state = ctx.read_self_consensus_state(height)?; |
| 57 | + ethereum_light_client::client::verify_non_membership( |
| 58 | + key, |
| 59 | + consensus_state.ibc_storage_root, |
| 60 | + storage_proof, |
| 61 | + ) |
| 62 | + .map_err(Into::<Error>::into)?; |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | + |
| 66 | + fn get_timestamp(consensus_state: &Self::ConsensusState) -> u64 { |
| 67 | + consensus_state.timestamp |
| 68 | + } |
| 69 | + |
| 70 | + fn get_latest_height(client_state: &Self::ClientState) -> u64 { |
| 71 | + client_state.latest_height |
| 72 | + } |
| 73 | + |
| 74 | + fn get_counterparty_chain_id(client_state: &Self::ClientState) -> String { |
| 75 | + client_state.chain_id.to_string() |
| 76 | + } |
| 77 | + |
| 78 | + fn status(ctx: IbcClientCtx<Self>, client_state: &Self::ClientState) -> Status { |
| 79 | + let _ = client_state; |
| 80 | + let _ = ctx; |
| 81 | + // FIXME: expose the ctx to this call to allow threading this call to L1 |
| 82 | + // client. generally, we want to thread if a client is an L2 so always |
| 83 | + // provide the ctx? |
| 84 | + Status::Active |
| 85 | + } |
| 86 | + |
| 87 | + fn verify_creation( |
| 88 | + _caller: Addr, |
| 89 | + _client_state: &Self::ClientState, |
| 90 | + _consensus_state: &Self::ConsensusState, |
| 91 | + _relayer: Addr, |
| 92 | + ) -> Result<ClientCreationResult<Self>, IbcClientError<Self>> { |
| 93 | + Ok(ClientCreationResult::new()) |
| 94 | + } |
| 95 | + |
| 96 | + fn verify_header( |
| 97 | + ctx: IbcClientCtx<Self>, |
| 98 | + _caller: Addr, |
| 99 | + header: Self::Header, |
| 100 | + _relayer: Addr, |
| 101 | + ) -> Result<StateUpdate<Self>, IbcClientError<Self>> { |
| 102 | + let mut client_state = ctx.read_self_client_state()?; |
| 103 | + |
| 104 | + let l1_consensus_state = ctx |
| 105 | + .read_consensus_state::<EthereumLightClient>( |
| 106 | + client_state.l1_client_id, |
| 107 | + header.l1_height, |
| 108 | + ) |
| 109 | + .map_err(Into::<Error>::into)?; |
| 110 | + |
| 111 | + bob_verifier::verify_header( |
| 112 | + &client_state, |
| 113 | + &header, |
| 114 | + l1_consensus_state.state_root, |
| 115 | + ctx.env.block.time.seconds(), |
| 116 | + ) |
| 117 | + .map_err(Into::<Error>::into)?; |
| 118 | + |
| 119 | + let update_height = header.l2_header.number.try_into().expect("impossible"); |
| 120 | + |
| 121 | + let consensus_state = ConsensusState { |
| 122 | + timestamp: header.l2_header.timestamp, |
| 123 | + state_root: header.l2_header.state_root, |
| 124 | + ibc_storage_root: header.l2_ibc_account_proof.storage_root, |
| 125 | + }; |
| 126 | + |
| 127 | + let state_update = StateUpdate::new(update_height, consensus_state); |
| 128 | + |
| 129 | + if client_state.latest_height < update_height { |
| 130 | + client_state.latest_height = update_height; |
| 131 | + Ok(state_update.overwrite_client_state(client_state)) |
| 132 | + } else { |
| 133 | + Ok(state_update) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + fn misbehaviour( |
| 138 | + _ctx: IbcClientCtx<Self>, |
| 139 | + _caller: Addr, |
| 140 | + _misbehaviour: Self::Misbehaviour, |
| 141 | + _relayer: Addr, |
| 142 | + ) -> Result<Self::ClientState, IbcClientError<Self>> { |
| 143 | + Err(Error::Unimplemented.into()) |
| 144 | + } |
| 145 | +} |
0 commit comments