Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-savu committed Nov 10, 2023
1 parent 4dc3fdd commit 3148a0d
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 117 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.lock

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

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ cosmrs = { version = "0.14", default-features = false, features = [
"tokio",
"grpc",
] }
cosmwasm-std = { version = "*", features = ["stargate"] }
crunchy = "0.2"
ctrlc = "3.2"
curve25519-dalek = { version = "~3.2", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions rust/chains/hyperlane-cosmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async-trait = { workspace = true }
base64 = { workspace = true }
bech32 = { workspace = true }
cosmrs = { workspace = true, features = ["cosmwasm", "tokio", "grpc", "rpc"] }
cosmwasm-std = { workspace = true }
derive-new = { workspace = true }
hex = { workspace = true }
hpl-interface.workspace = true
Expand Down
56 changes: 17 additions & 39 deletions rust/chains/hyperlane-cosmos/src/merkle_tree_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{fmt::Debug, num::NonZeroU64, ops::RangeInclusive, str::FromStr};
use async_trait::async_trait;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use cosmrs::tendermint::abci::EventAttribute;
use hpl_interface::hook::merkle::{MerkleHookQueryMsg, QueryMsg, TreeResponse};
use hyperlane_core::{
accumulator::incremental::IncrementalMerkle, ChainCommunicationError, ChainResult, Checkpoint,
ContractLocator, HyperlaneChain, HyperlaneContract, HyperlaneDomain, HyperlaneProvider,
Expand All @@ -13,16 +14,18 @@ use tracing::instrument;

use crate::{
grpc::{WasmGrpcProvider, WasmProvider},
payloads::{
general::{self},
merkle_tree_hook,
},
// payloads::{
// general::{self},
// merkle_tree_hook,
// },
rpc::{CosmosWasmIndexer, ParsedEvent, WasmIndexer},
utils::{
get_block_height_for_lag, CONTRACT_ADDRESS_ATTRIBUTE_KEY,
CONTRACT_ADDRESS_ATTRIBUTE_KEY_BASE64,
},
ConnectionConf, CosmosProvider, Signer,
ConnectionConf,
CosmosProvider,
Signer,
};

#[derive(Debug)]
Expand Down Expand Up @@ -74,27 +77,17 @@ impl MerkleTreeHook for CosmosMerkleTreeHook {
/// Return the incremental merkle tree in storage
#[instrument(level = "debug", err, ret, skip(self))]
async fn tree(&self, lag: Option<NonZeroU64>) -> ChainResult<IncrementalMerkle> {
let payload = merkle_tree_hook::MerkleTreeRequest {
tree: general::EmptyStruct {},
};
let payload = QueryMsg::MerkleHook(MerkleHookQueryMsg::Tree {});

let block_height = get_block_height_for_lag(&self.provider, lag).await?;

let data = self
.provider
.wasm_query(
merkle_tree_hook::MerkleTreeGenericRequest {
merkle_hook: payload,
},
block_height,
)
.await?;
let response: merkle_tree_hook::MerkleTreeResponse = serde_json::from_slice(&data)?;
let data = self.provider.wasm_query(payload, block_height).await?;
let response: TreeResponse = serde_json::from_slice(&data)?;

let branch = response
.branch
.iter()
.map(|s| s.as_str())
.map(|s| s.to_string().as_str())
.map(H256::from_str)
.collect::<Result<Vec<H256>, _>>()?;

Expand All @@ -107,18 +100,14 @@ impl MerkleTreeHook for CosmosMerkleTreeHook {

/// Gets the current leaf count of the merkle tree
async fn count(&self, lag: Option<NonZeroU64>) -> ChainResult<u32> {
let payload = merkle_tree_hook::MerkleTreeCountRequest {
count: general::EmptyStruct {},
};

let block_height = get_block_height_for_lag(&self.provider, lag).await?;

self.count_at_block(block_height).await
}

#[instrument(level = "debug", err, ret, skip(self))]
async fn latest_checkpoint(&self, lag: Option<NonZeroU64>) -> ChainResult<Checkpoint> {
let payload = merkle_tree_hook::CheckPointRequest {
let payload = merkle_tree_hook::requests::CheckPoint {
check_point: general::EmptyStruct {},
};

Expand All @@ -133,7 +122,7 @@ impl MerkleTreeHook for CosmosMerkleTreeHook {
block_height,
)
.await?;
let response: merkle_tree_hook::CheckPointResponse = serde_json::from_slice(&data)?;
let response: merkle_tree_hook::responses::CheckPoint = serde_json::from_slice(&data)?;

Ok(Checkpoint {
merkle_tree_hook_address: self.address,
Expand All @@ -147,20 +136,9 @@ impl MerkleTreeHook for CosmosMerkleTreeHook {
impl CosmosMerkleTreeHook {
#[instrument(level = "debug", err, ret, skip(self))]
async fn count_at_block(&self, block_height: Option<u64>) -> ChainResult<u32> {
let payload = merkle_tree_hook::MerkleTreeCountRequest {
count: general::EmptyStruct {},
};

let data = self
.provider
.wasm_query(
merkle_tree_hook::MerkleTreeGenericRequest {
merkle_hook: payload,
},
block_height,
)
.await?;
let response: merkle_tree_hook::MerkleTreeCountResponse = serde_json::from_slice(&data)?;
let payload = QueryMsg::MerkleHook(MerkleHookQueryMsg::Count {});
let data = self.provider.wasm_query(payload, block_height).await?;
let response: merkle_tree_hook::responses::MerkleTreeCount = serde_json::from_slice(&data)?;

Ok(response.count)
}
Expand Down
25 changes: 12 additions & 13 deletions rust/chains/hyperlane-cosmos/src/multisig_ism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use std::str::FromStr;

use crate::{
grpc::{WasmGrpcProvider, WasmProvider},
payloads::ism_routes::QueryIsmGeneralRequest,
signers::Signer,
ConnectionConf, CosmosProvider,
};
use async_trait::async_trait;
use cosmwasm_std::HexBinary;
use hpl_interface::ism::IsmQueryMsg;
use hpl_interface::ism::VerifyInfoResponse;
use hyperlane_core::{
ChainResult, ContractLocator, HyperlaneChain, HyperlaneContract, HyperlaneDomain,
HyperlaneMessage, HyperlaneProvider, MultisigIsm, RawHyperlaneMessage, H160, H256,
};

use crate::payloads::multisig_ism::{self, VerifyInfoRequest, VerifyInfoRequestInner};

/// A reference to a MultisigIsm contract on some Cosmos chain
#[derive(Debug)]
pub struct CosmosMultisigIsm {
Expand Down Expand Up @@ -62,22 +62,21 @@ impl MultisigIsm for CosmosMultisigIsm {
&self,
message: &HyperlaneMessage,
) -> ChainResult<(Vec<H256>, u8)> {
let payload = VerifyInfoRequest {
verify_info: VerifyInfoRequestInner {
message: hex::encode(RawHyperlaneMessage::from(message)),
},
let payload = IsmQueryMsg::VerifyInfo {
message: HexBinary::from(RawHyperlaneMessage::from(message)),
};

let data = self
.provider
.wasm_query(QueryIsmGeneralRequest { ism: payload }, None)
.await?;
let response: multisig_ism::VerifyInfoResponse = serde_json::from_slice(&data)?;
let data = self.provider.wasm_query(payload.wrap(), None).await?;
let response: VerifyInfoResponse = serde_json::from_slice(&data)?;

let validators: ChainResult<Vec<H256>> = response
.validators
.iter()
.map(|v| H160::from_str(v).map(H256::from).map_err(Into::into))
.map(|v| {
H160::from_str(&v.to_string())
.map(H256::from)
.map_err(Into::into)
})
.collect();

Ok((validators?, response.threshold))
Expand Down
68 changes: 36 additions & 32 deletions rust/chains/hyperlane-cosmos/src/payloads/merkle_tree_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,42 @@ pub struct MerkleTreeGenericRequest<T> {
pub merkle_hook: T,
}

// --------- Requests ---------

#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleTreeRequest {
pub tree: EmptyStruct,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleTreeCountRequest {
pub count: EmptyStruct,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CheckPointRequest {
pub check_point: EmptyStruct,
pub mod requests {
use super::*;

#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleTree {
pub tree: EmptyStruct,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleTreeCount {
pub count: EmptyStruct,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CheckPoint {
pub check_point: EmptyStruct,
}
}

// --------- Responses ---------

#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleTreeResponse {
pub branch: [String; TREE_DEPTH],
pub count: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleTreeCountResponse {
pub count: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CheckPointResponse {
pub root: String,
pub count: u32,
pub mod responses {
use super::*;

#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleTree {
pub branch: [String; TREE_DEPTH],
pub count: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MerkleTreeCount {
pub count: u32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct CheckPoint {
pub root: String,
pub count: u32,
}
}
14 changes: 7 additions & 7 deletions rust/chains/hyperlane-cosmos/src/payloads/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod aggregate_ism;
pub mod general;
pub mod ism_routes;
pub mod mailbox;
pub mod merkle_tree_hook;
pub mod multisig_ism;
pub mod validator_announce;
// pub mod aggregate_ism;
// pub mod general;
// pub mod ism_routes;
// pub mod mailbox;
// pub mod merkle_tree_hook;
// pub mod multisig_ism;
// pub mod validator_announce;
6 changes: 0 additions & 6 deletions rust/chains/hyperlane-cosmos/src/payloads/multisig_ism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ pub struct VerifyInfoRequest {
pub struct VerifyInfoRequestInner {
pub message: String, // hexbinary
}

#[derive(Serialize, Deserialize, Debug)]
pub struct VerifyInfoResponse {
pub threshold: u8,
pub validators: Vec<String>,
}
26 changes: 7 additions & 19 deletions rust/chains/hyperlane-cosmos/src/routing_ism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::str::FromStr;

use async_trait::async_trait;

use cosmwasm_std::HexBinary;
use hpl_interface::ism::routing::{QueryMsg, RouteResponse, RoutingIsmQueryMsg};
use hyperlane_core::{
ChainResult, ContractLocator, HyperlaneChain, HyperlaneContract, HyperlaneDomain,
HyperlaneMessage, HyperlaneProvider, RawHyperlaneMessage, RoutingIsm, H256,
Expand All @@ -10,9 +12,6 @@ use hyperlane_core::{
use crate::{
address::CosmosAddress,
grpc::{WasmGrpcProvider, WasmProvider},
payloads::ism_routes::{
IsmRouteRequest, IsmRouteRequestInner, IsmRouteRespnose, QueryRoutingIsmGeneralRequest,
},
signers::Signer,
ConnectionConf, CosmosProvider,
};
Expand Down Expand Up @@ -61,22 +60,11 @@ impl HyperlaneChain for CosmosRoutingIsm {
#[async_trait]
impl RoutingIsm for CosmosRoutingIsm {
async fn route(&self, message: &HyperlaneMessage) -> ChainResult<H256> {
let payload = IsmRouteRequest {
route: IsmRouteRequestInner {
message: hex::encode(RawHyperlaneMessage::from(message)),
},
};

let data = self
.provider
.wasm_query(
QueryRoutingIsmGeneralRequest {
routing_ism: payload,
},
None,
)
.await?;
let response: IsmRouteRespnose = serde_json::from_slice(&data)?;
let payload = QueryMsg::RoutingIsm(RoutingIsmQueryMsg::Route {
message: HexBinary::from(RawHyperlaneMessage::from(message)),
});
let data = self.provider.wasm_query(payload, None).await?;
let response: RouteResponse = serde_json::from_slice(&data)?;

Ok(CosmosAddress::from_str(&response.ism)?.digest())
}
Expand Down
2 changes: 1 addition & 1 deletion rust/hyperlane-base/src/settings/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use self::json_value_parser::ValueParser;
pub use super::envs::*;
use crate::settings::{
chains::IndexSettings, parser::connection_parser::build_connection_conf, trace::TracingConfig,
ChainConf, ChainConnectionConf, CoreContractAddresses, Settings, SignerConf,
ChainConf, CoreContractAddresses, Settings, SignerConf,
};

mod connection_parser;
Expand Down

0 comments on commit 3148a0d

Please sign in to comment.