Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor registry module, add IBC path cache, paths API #63

Merged
merged 15 commits into from
Jul 11, 2022
Merged
777 changes: 38 additions & 739 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions ocular/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ license = "Apache-2.0"
repository = "https://github.com/PeggyJV/ocular"
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[package.metadata.cargo-udeps.ignore]
normal = ["assay"]

[dependencies]
assay = "0.1"
Expand All @@ -20,12 +21,11 @@ bip32 = "0.3"
cosmos-sdk-proto = "0.12.0"
cosmrs = {version = "0.7.0", features = ["dev"] }
dirs = "1.0.4"
eyre = "0.6.8"
futures = { version = "0.3.19", features = ["executor"] }
hex = "0.3.1"
keyring = "1.1.2"
k256 = { version = "0.10.2", features = ["pem"] }
http = "0.2"
octocrab = "0.15.3"
pkcs8 = "0.8"
rand = "0.8.4"
rand_core = "0.6.3"
Expand All @@ -36,15 +36,16 @@ signatory = "0.24"
thiserror = "1.0"
tendermint-pre = { package = "tendermint", git = "https://github.com/informalsystems/tendermint-rs" }
tendermint-rpc = { version = "0.23.7", features = ["websocket-client"] }
tiny-bip39 = "0.8"
tonic = { version = "0.7.2", features = ["transport"] }
url = "2.2.2"
tendermint-proto = "0.23.5"
tokio = "1.17.0"
prost-types = "0.10.1"
prost = "0.10.4"
toml = "0.5.8"
chrono = "0.4.0"
uuid = { version = "0.8.2", features = ["serde", "v4"] }
eyre = "0.6.8"
async-trait = "0.1.56"

[features]
default = ["registry-cache"]
registry-cache = []
2 changes: 0 additions & 2 deletions ocular/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use tendermint_pre;

pub mod client;
pub mod config;
pub mod info;
pub mod registry;

pub type ChainName = tendermint_pre::chain::Id;

Expand Down
29 changes: 25 additions & 4 deletions ocular/src/chain/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![warn(unused_qualifications)]

use crate::{
chain::{client::cache::Cache, config::ChainClientConfig, registry::get_chain},
error::{ChainClientError, RpcError},
chain::{client::cache::Cache, config::ChainClientConfig},
error::{ChainClientError, ChainRegistryError, RpcError},
keyring::Keyring,
registry::get_chain,
};
use futures::executor;
use tendermint_rpc::{self, WebSocketClient, WebSocketClientDriver};
Expand Down Expand Up @@ -71,7 +72,17 @@ impl ChainClientBuilder {
}

pub async fn build(self) -> Result<ChainClient, ChainClientError> {
let info = get_chain(self.chain_name.as_str()).await?;
let info = match get_chain(self.chain_name.as_str()).await? {
Some(c) => c,
None => {
return Err(ChainRegistryError::UnsupportedChain(format!(
"chain info for {} not found (no chain.json present)",
&self.chain_name
))
.into())
}
};

let mut config = info.get_chain_config()?;

if self.grpc_endpoint.is_some() {
Expand Down Expand Up @@ -118,7 +129,17 @@ impl ChainClientBuilder {
}

fn get_client(chain_name: &str) -> Result<ChainClient, ChainClientError> {
let chain = executor::block_on(async { get_chain(chain_name).await })?;
let chain = match executor::block_on(async { get_chain(chain_name).await })? {
Some(c) => c,
None => {
return Err(ChainRegistryError::UnsupportedChain(format!(
"chain info for {} not found (no chain.json present)",
chain_name
))
.into())
}
};

// Default to in memory cache
let cache = Cache::create_memory_cache(None, 3)?;
let config = chain.get_chain_config()?;
Expand Down
4 changes: 2 additions & 2 deletions ocular/src/chain/client/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ impl Cache {
///
/// ```toml
/// [[endpoints]]
/// address = "35.230.37.28:9090"
/// connsecutive_failed_connections = 0
/// address = "35.230.37.28:9090"
/// connsecutive_failed_connections = 0
/// ```
pub fn create_file_cache(
file_path: Option<&str>,
Expand Down
8 changes: 3 additions & 5 deletions ocular/src/chain/client/grpc.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
chain::{
client::{query::bank::BankQueryClient, ChainClient},
registry::{self},
},
chain::client::{query::BankQueryClient, ChainClient},
error::{ChainInfoError, GrpcError, RpcError},
utils,
registry, utils,
};
use rand::prelude::SliceRandom;
use rand::thread_rng;
Expand Down Expand Up @@ -89,6 +86,7 @@ impl ChainClient {
async fn get_all_grpc_endpoints(&self) -> Vec<String> {
let info = registry::get_chain(&self.config.chain_name)
.await
.expect("Could not get chain info.")
.expect("Could not get chain info.");

info.apis
Expand Down
111 changes: 0 additions & 111 deletions ocular/src/chain/registry.rs

This file was deleted.

4 changes: 2 additions & 2 deletions ocular/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub enum ChainInfoError {
pub enum ChainRegistryError {
#[error("error parsing chain info: {0}")]
InvalidChainInfo(#[from] serde_json::error::Error),
#[error("error during chain registry request: {0}")]
Request(#[from] octocrab::Error),
#[error("error furing content get request")]
GetRequest(#[from] reqwest::Error),
#[error("{0}")]
UnsupportedChain(String),
}
Expand Down
32 changes: 32 additions & 0 deletions ocular/src/github.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! Types for deserializing github repos API content
use serde::Deserialize;
use serde::Serialize;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Content {
pub name: String,
pub path: String,
pub sha: String,
pub size: i64,
pub url: String,
#[serde(rename = "html_url")]
pub html_url: String,
#[serde(rename = "git_url")]
pub git_url: String,
#[serde(rename = "download_url")]
pub download_url: Option<String>,
#[serde(rename = "type")]
pub type_field: String,
#[serde(rename = "_links")]
pub links: Links,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Links {
#[serde(rename = "self")]
pub self_field: String,
pub git: String,
pub html: String,
}
4 changes: 2 additions & 2 deletions ocular/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![allow(clippy::too_many_arguments)]

pub use error::ChainRegistryError;

pub mod account;
pub mod chain;
pub mod cosmos_modules {
Expand All @@ -20,6 +18,8 @@ pub mod cosmos_modules {
pub use cosmrs::proto::cosmos::staking::v1beta1 as staking;
}
pub mod error;
pub(crate) mod github;
pub mod keyring;
pub mod registry;
pub mod tx;
pub mod utils;
Loading