Skip to content

Commit

Permalink
Support octopus 2.0 (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
en authored Dec 13, 2023
1 parent e529d25 commit 3b1332e
Show file tree
Hide file tree
Showing 73 changed files with 10,365 additions and 333 deletions.
2,619 changes: 2,475 additions & 144 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ overflow-checks = true
# tendermint-light-client-verifier = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
# tendermint-light-client-detector = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
# tendermint-testgen = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
ibc = { git = "https://github.com/octopus-network/ibc-rs.git", branch = "v0.48.1-octopus" }
ibc-proto = { git = "https://github.com/octopus-network/ibc-proto-rs.git", branch = "v0.38.0-octopus" }
6 changes: 6 additions & 0 deletions crates/relayer-cli/src/chain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::collections::HashMap;
use std::fmt::Display;
use std::marker::Send;
use std::path::PathBuf;

use futures::future::join_all;
use http::Uri;
Expand All @@ -19,6 +20,7 @@ use ibc_chain_registry::querier::*;
use ibc_relayer::config::filter::{FilterPattern, PacketFilter};
use ibc_relayer::config::gas_multiplier::GasMultiplier;
use ibc_relayer::config::types::{MaxMsgNum, MaxTxSize, Memo};
use ibc_relayer::config::CanisterIdConfig;
use ibc_relayer::config::{default, AddressType, ChainConfig, EventSourceMode, GasPrice};
use ibc_relayer::keyring::Store;

Expand Down Expand Up @@ -122,6 +124,10 @@ where

Ok(ChainConfig {
id: chain_data.chain_id,
ic_endpoint: String::new(),
canister_id: CanisterIdConfig::default(),
canister_pem: PathBuf::new(),
near_ibc_address: ibc_relayer::config::NearIbcContractAddress::default(),
r#type: default::chain_type(),
rpc_addr: rpc_data.rpc_address,
grpc_addr: grpc_address,
Expand Down
4 changes: 4 additions & 0 deletions crates/relayer-cli/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ use crate::commands::tx::client::TxCreateClientCmd;

mod channel;
mod connection;
mod vp_client;

/// `create` subcommands
#[derive(Command, Debug, Parser, Runnable)]
pub enum CreateCmds {
/// Create a new IBC client
Client(TxCreateClientCmd),

/// Create a vp IBC client
VpClient(vp_client::TxCreateVpClientCmd),

/// Create a new connection between two chains
Connection(CreateConnectionCommand),

Expand Down
111 changes: 111 additions & 0 deletions crates/relayer-cli/src/commands/create/vp_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use abscissa_core::clap::Parser;
use abscissa_core::{Command, Runnable};

use ibc_relayer::event::IbcEventWithHeight;
use ibc_relayer::foreign_client::{CreateOptions, ForeignClient};

use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ClientId};
use tendermint_light_client_verifier::types::TrustThreshold;

use crate::application::app_config;
use crate::cli_utils::ChainHandlePair;
use crate::conclude::Output;
use crate::error::Error;

#[derive(Clone, Command, Debug, Parser, PartialEq, Eq)]
pub struct TxCreateVpClientCmd {
#[clap(
long = "host-chain",
required = true,
value_name = "HOST_CHAIN_ID",
help_heading = "REQUIRED",
help = "Identifier of the chain that hosts the client"
)]
dst_chain_id: ChainId,

#[clap(
long = "reference-chain",
required = true,
value_name = "REFERENCE_CHAIN_ID",
help_heading = "REQUIRED",
help = "Identifier of the chain targeted by the client"
)]
src_chain_id: ChainId,

/// The maximum allowed clock drift for this client.
///
/// The clock drift is a correction parameter. It helps deal with clocks
/// that are only approximately synchronized between the source and destination chains
/// of this client.
/// The destination chain for this client uses the clock drift parameter when deciding
/// to accept or reject a new header (originating from the source chain) for this client.
/// If this option is not specified, a suitable clock drift value is derived from the chain
/// configurations.
#[clap(long = "clock-drift", value_name = "CLOCK_DRIFT")]
clock_drift: Option<humantime::Duration>,

/// Override the trusting period specified in the config.
///
/// The trusting period specifies how long a validator set is trusted for
/// (must be shorter than the chain's unbonding period).
#[clap(long = "trusting-period", value_name = "TRUSTING_PERIOD")]
trusting_period: Option<humantime::Duration>,

/// Override the trust threshold specified in the configuration.
///
/// The trust threshold defines what fraction of the total voting power of a known
/// and trusted validator set is sufficient for a commit to be accepted going forward.
#[clap(long = "trust-threshold", value_name = "TRUST_THRESHOLD", parse(try_from_str = parse_trust_threshold))]
trust_threshold: Option<TrustThreshold>,
}

/// Sample to run this tx:
/// `hermes create client --host-chain ibc-0 --reference-chain ibc-1`
impl Runnable for TxCreateVpClientCmd {
fn run(&self) {
let config = app_config();

if self.src_chain_id == self.dst_chain_id {
Output::error("source and destination chains must be different".to_string()).exit()
}

let chains = match ChainHandlePair::spawn(&config, &self.src_chain_id, &self.dst_chain_id) {
Ok(chains) => chains,
Err(e) => Output::error(e).exit(),
};

let client = ForeignClient::restore(ClientId::default(), chains.dst, chains.src);

let options = CreateOptions {
max_clock_drift: self.clock_drift.map(Into::into),
trusting_period: self.trusting_period.map(Into::into),
trust_threshold: self.trust_threshold.map(Into::into),
};

// Trigger client creation via the "build" interface, so that we obtain the resulting event
let res: Result<IbcEventWithHeight, Error> = client
.build_create_client_and_send(options)
.map_err(Error::foreign_client);

match res {
Ok(receipt) => Output::success(receipt.event).exit(),
Err(e) => Output::error(e).exit(),
}
}
}

fn parse_trust_threshold(input: &str) -> Result<TrustThreshold, Error> {
let (num_part, denom_part) = input.split_once('/').ok_or_else(|| {
Error::cli_arg("expected a fractional argument, two numbers separated by '/'".into())
})?;
let numerator = num_part
.trim()
.parse()
.map_err(|_| Error::cli_arg("invalid numerator for the fraction".into()))?;
let denominator = denom_part
.trim()
.parse()
.map_err(|_| Error::cli_arg("invalid denominator for the fraction".into()))?;
TrustThreshold::new(numerator, denominator)
.map_err(|e| Error::cli_arg(format!("invalid trust threshold fraction: {e}")))
}
45 changes: 42 additions & 3 deletions crates/relayer-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use ibc_relayer::{
chain::ChainType,
config::{ChainConfig, Config},
keyring::{
AnySigningKeyPair, KeyRing, Secp256k1KeyPair, SigningKeyPair, SigningKeyPairSized, Store,
AnySigningKeyPair, KeyRing, NearKeyPair, Secp256k1KeyPair, SigningKeyPair,
SigningKeyPairSized, Store,
},
};
use ibc_relayer_types::core::ics24_host::identifier::ChainId;
Expand Down Expand Up @@ -204,7 +205,7 @@ pub fn add_key(
overwrite: bool,
) -> eyre::Result<AnySigningKeyPair> {
let key_pair = match config.r#type {
ChainType::CosmosSdk => {
ChainType::CosmosSdk | ChainType::Vp => {
let mut keyring = KeyRing::new_secp256k1(
Store::Test,
&config.account_prefix,
Expand All @@ -218,6 +219,24 @@ pub fn add_key(
fs::read_to_string(file).map_err(|_| eyre!("error reading the key file"))?;
let key_pair = Secp256k1KeyPair::from_seed_file(&key_contents, hd_path)?;

keyring.add_key(key_name, key_pair.clone())?;
key_pair.into()
}
ChainType::Near => {
let mut keyring = KeyRing::new_near_keypair(
Store::Test,
&config.account_prefix,
&config.id,
&config.key_store_folder,
)?;

check_key_exists(&keyring, key_name, overwrite);

let key_contents =
fs::read_to_string(file).map_err(|_| eyre!("error reading the key file"))?;

let key_pair = NearKeyPair::from_seed_file(&key_contents, hd_path)?;

keyring.add_key(key_name, key_pair.clone())?;
key_pair.into()
}
Expand All @@ -237,7 +256,7 @@ pub fn restore_key(
fs::read_to_string(mnemonic).map_err(|_| eyre!("error reading the mnemonic file"))?;

let key_pair = match config.r#type {
ChainType::CosmosSdk => {
ChainType::CosmosSdk | ChainType::Vp => {
let mut keyring = KeyRing::new_secp256k1(
Store::Test,
&config.account_prefix,
Expand All @@ -254,6 +273,26 @@ pub fn restore_key(
keyring.account_prefix(),
)?;

keyring.add_key(key_name, key_pair.clone())?;
key_pair.into()
}
ChainType::Near => {
let mut keyring = KeyRing::new_near_keypair(
Store::Test,
&config.account_prefix,
&config.id,
&config.key_store_folder,
)?;

check_key_exists(&keyring, key_name, overwrite);

let key_pair = NearKeyPair::from_mnemonic(
&mnemonic_content,
hdpath,
&config.address_type,
keyring.account_prefix(),
)?;

keyring.add_key(key_name, key_pair.clone())?;
key_pair.into()
}
Expand Down
25 changes: 23 additions & 2 deletions crates/relayer-cli/src/commands/keys/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Runnable for KeysDeleteCmd {

pub fn delete_key(config: &ChainConfig, key_name: &str) -> eyre::Result<()> {
match config.r#type {
ChainType::CosmosSdk => {
ChainType::CosmosSdk | ChainType::Vp => {
let mut keyring = KeyRing::new_secp256k1(
Store::Test,
&config.account_prefix,
Expand All @@ -123,13 +123,22 @@ pub fn delete_key(config: &ChainConfig, key_name: &str) -> eyre::Result<()> {
)?;
keyring.remove_key(key_name)?;
}
ChainType::Near => {
let mut keyring = KeyRing::new_near_keypair(
Store::Test,
&config.account_prefix,
&config.id,
&config.key_store_folder,
)?;
keyring.remove_key(key_name)?;
}
}
Ok(())
}

pub fn delete_all_keys(config: &ChainConfig) -> eyre::Result<()> {
match config.r#type {
ChainType::CosmosSdk => {
ChainType::CosmosSdk | ChainType::Vp => {
let mut keyring = KeyRing::new_secp256k1(
Store::Test,
&config.account_prefix,
Expand All @@ -141,6 +150,18 @@ pub fn delete_all_keys(config: &ChainConfig) -> eyre::Result<()> {
keyring.remove_key(&key_name)?;
}
}
ChainType::Near => {
let mut keyring = KeyRing::new_near_keypair(
Store::Test,
&config.account_prefix,
&config.id,
&config.key_store_folder,
)?;
let keys = keyring.keys()?;
for (key_name, _) in keys {
keyring.remove_key(&key_name)?;
}
}
}
Ok(())
}
Expand Down
10 changes: 10 additions & 0 deletions crates/relayer-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ primitive-types = { version = "0.12.1", default-features = false, features = ["s
num-rational = "0.4.1"
regex = "1"

# for ics06 solomachine client
eyre = "0.6"

# for ics12 near client
ed25519-dalek = {version = "1", features = ["default", "serde"]}
borsh = { version = "1.0", default-features = false, features = ["derive"] }
bs58 = "0.4"
sha2 = { version = "0.10.8", default-features = false }
ics12-proto = { version = "0.1.3", default-features = false }

[dependencies.tendermint]
version = "0.34.0"
features = ["clock"]
Expand Down
Loading

0 comments on commit 3b1332e

Please sign in to comment.