Skip to content

Commit

Permalink
feat(voyager): split messages out into a crate (#884)
Browse files Browse the repository at this point in the history
- split lightclients into a crate
- split voyager messages into a crate, with the previously split
`LightClient` trait moved to this crate (pending rename)
  • Loading branch information
benluelo authored Nov 3, 2023
2 parents 762a084 + 7cb4fd4 commit d3776cd
Show file tree
Hide file tree
Showing 35 changed files with 5,282 additions and 5,156 deletions.
48 changes: 46 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ chain-utils = { path = "lib/chain-utils", default-features = false }
hubble = { path = "hubble" }
token-factory-api = { path = "cosmwasm/token-factory-api", default-features = false }
ucs01-relay-api = { path = "cosmwasm/ucs01-relay-api", default-features = false }
voyager-message = { path = "voyager/voyager-message", default-features = false }
lightclient = { path = "lib/lightclient", default-features = false }

# external dependencies

Expand Down
416 changes: 367 additions & 49 deletions lib/chain-utils/src/evm.rs

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions lib/chain-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,3 @@ impl<T: Clone> Pool<T> {
r
}
}

pub trait MaybeRecoverableError: Error {
fn is_recoverable(&self) -> bool;
}

fn _is_object_safe(_: &dyn MaybeRecoverableError) {}
158 changes: 154 additions & 4 deletions lib/chain-utils/src/union.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::num::ParseIntError;
use std::{fmt::Debug, num::ParseIntError};

use ethers::prelude::k256::ecdsa;
use futures::{stream, Future, FutureExt, Stream, StreamExt};
Expand All @@ -15,15 +15,19 @@ use unionlabs::{
lightclients::{cometbls, wasm},
},
id::ClientId,
proof::{
AcknowledgementPath, ChannelEndPath, ClientConsensusStatePath, ClientStatePath,
CommitmentPath, ConnectionPath, IbcPath, IbcStateRead,
},
tendermint::abci::{event::Event, event_attribute::EventAttribute},
traits::{Chain, ClientState},
CosmosAccountId,
traits::{Chain, ClientState, ClientStateOf, ConsensusStateOf},
CosmosAccountId, MaybeRecoverableError, TryFromProto, TryFromProtoErrorOf,
};

use crate::{
private_key::PrivateKey,
union::tm_types::{CosmosSdkError, SdkError},
ChainEvent, EventSource, MaybeRecoverableError, Pool,
ChainEvent, EventSource, Pool,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -979,3 +983,149 @@ pub mod tm_types {
)
}
}

pub trait AbciStateRead<Counterparty>: IbcPath<Union, Counterparty>
where
Counterparty: Chain,
{
fn from_abci_bytes(bytes: Vec<u8>) -> Self::Output;
}

impl<Counterparty> AbciStateRead<Counterparty> for ClientStatePath<<Union as Chain>::ClientId>
where
Counterparty: Chain,
ClientStateOf<Counterparty>: TryFromProto,
TryFromProtoErrorOf<ClientStateOf<Counterparty>>: Debug,
{
fn from_abci_bytes(bytes: Vec<u8>) -> Self::Output {
Self::Output::try_from_proto_bytes(&bytes).unwrap()
}
}

impl<Counterparty> AbciStateRead<Counterparty>
for ClientConsensusStatePath<<Union as Chain>::ClientId, <Counterparty as Chain>::Height>
where
Counterparty: Chain,
ConsensusStateOf<Counterparty>: TryFromProto,
TryFromProtoErrorOf<ConsensusStateOf<Counterparty>>: Debug,
{
fn from_abci_bytes(bytes: Vec<u8>) -> Self::Output {
Self::Output::try_from_proto_bytes(&bytes).unwrap()
}
}

impl<Counterparty> AbciStateRead<Counterparty> for ConnectionPath
where
Counterparty: Chain,
// <Counterparty as Chain>::ClientId: ClientId,
// Self::Output: Proto + TryFrom<protos::ibc::core::connection::v1::ConnectionEnd>,
{
fn from_abci_bytes(bytes: Vec<u8>) -> Self::Output {
Self::Output::try_from_proto_bytes(&bytes).unwrap()
}
}

impl<Counterparty> AbciStateRead<Counterparty> for ChannelEndPath
where
Counterparty: Chain,
{
fn from_abci_bytes(bytes: Vec<u8>) -> Self::Output {
Self::Output::try_from_proto_bytes(&bytes).unwrap()
}
}

impl<Counterparty> AbciStateRead<Counterparty> for CommitmentPath
where
Counterparty: Chain,
{
fn from_abci_bytes(bytes: Vec<u8>) -> Self::Output {
bytes.try_into().unwrap()
}
}

impl<Counterparty> AbciStateRead<Counterparty> for AcknowledgementPath
where
Counterparty: Chain,
{
fn from_abci_bytes(bytes: Vec<u8>) -> Self::Output {
bytes.try_into().unwrap()
}
}

impl<Counterparty, P> IbcStateRead<Counterparty, P> for Union
where
Counterparty: Chain,
ClientStateOf<Counterparty>: TryFromProto,
ConsensusStateOf<Counterparty>: TryFromProto,
P: IbcPath<Union, Counterparty> + AbciStateRead<Counterparty> + 'static,
{
fn proof(&self, path: P, at: Height) -> impl Future<Output = Vec<u8>> + '_ {
async move {
tracing::info!(%path, %at, "fetching state proof");

let mut client =
protos::cosmos::base::tendermint::v1beta1::service_client::ServiceClient::connect(
self.grpc_url.clone(),
)
.await
.unwrap();

let query_result = client
.abci_query(
protos::cosmos::base::tendermint::v1beta1::AbciQueryRequest {
data: path.to_string().into_bytes(),
path: "store/ibc/key".to_string(),
height: i64::try_from(at.revision_height).unwrap(),
prove: true,
},
)
.await
.unwrap()
.into_inner();

protos::ibc::core::commitment::v1::MerkleProof {
proofs: query_result
.proof_ops
.unwrap()
.ops
.into_iter()
.map(|op| {
protos::cosmos::ics23::v1::CommitmentProof::decode(op.data.as_slice())
.unwrap()
})
.collect::<Vec<_>>(),
}
.encode_to_vec()
}
}

fn state(
&self,
path: P,
at: Self::Height,
) -> impl Future<Output = <P as IbcPath<Union, Counterparty>>::Output> + '_ {
async move {
let mut client =
protos::cosmos::base::tendermint::v1beta1::service_client::ServiceClient::connect(
self.grpc_url.clone(),
)
.await
.unwrap();

let query_result = client
.abci_query(
protos::cosmos::base::tendermint::v1beta1::AbciQueryRequest {
data: path.to_string().into_bytes(),
path: "store/ibc/key".to_string(),
height: i64::try_from(at.revision_height).unwrap(),
prove: false,
},
)
.await
.unwrap()
.into_inner();

P::from_abci_bytes(query_result.value)
}
}
}
15 changes: 15 additions & 0 deletions lib/lightclient/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "lightclient"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chain-utils.workspace = true
unionlabs = { workspace = true, features = ["ethabi"] }
serde = { version = "1.0.173", features = ["derive"] }
protos = { workspace = true, features = ["proto_full", "client"] }

[dev-dependencies]
hex-literal = "0.4.1"
Loading

0 comments on commit d3776cd

Please sign in to comment.