Skip to content

Commit

Permalink
Workaround: disabled chain upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
adizere committed May 10, 2021
1 parent fc0eab5 commit 30688a4
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 156 deletions.
169 changes: 88 additions & 81 deletions relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_imports)]

use std::{
convert::TryFrom, convert::TryInto, future::Future, str::FromStr, sync::Arc, thread,
time::Duration,
Expand Down Expand Up @@ -287,6 +289,7 @@ impl CosmosSdkChain {
}

// Perform an ABCI query against the client upgrade sub-store to fetch a proof.
#[allow(dead_code)]
fn query_client_upgrade_proof(
&self,
data: ClientUpgradePath,
Expand Down Expand Up @@ -532,97 +535,101 @@ impl Chain for CosmosSdkChain {
Ok(client_state)
}

#[allow(unused_variables, dead_code)]
fn query_upgraded_client_state(
&self,
height: ICSHeight,
) -> Result<(Self::ClientState, MerkleProof), Error> {
crate::time!("query_upgraded_client_state");

let mut client = self
.block_on(
ibc_proto::cosmos::upgrade::v1beta1::query_client::QueryClient::connect(
self.grpc_addr.clone(),
),
)
.map_err(|e| Kind::Grpc.context(e))?;

let req = tonic::Request::new(QueryCurrentPlanRequest {});
let response = self
.block_on(client.current_plan(req))
.map_err(|e| Kind::Grpc.context(e))?;

let upgraded_client_state_raw = response
.into_inner()
.plan
.ok_or(Kind::EmptyResponseValue)?
.upgraded_client_state
.ok_or(Kind::EmptyUpgradedClientState)?;
let client_state = AnyClientState::try_from(upgraded_client_state_raw)
.map_err(|e| Kind::Grpc.context(e))?;

// TODO: Better error kinds here.
let tm_client_state =
downcast!(client_state => AnyClientState::Tendermint).ok_or_else(|| {
Kind::Query("upgraded client state".into()).context("unexpected client state type")
})?;

// Query for the proof.
let tm_height =
Height::try_from(height.revision_height).map_err(|e| Kind::InvalidHeight.context(e))?;
let (proof, _proof_height) = self.query_client_upgrade_proof(
ClientUpgradePath::UpgradedClientState(height.revision_height),
tm_height,
)?;

Ok((tm_client_state, proof))
todo!()
// crate::time!("query_upgraded_client_state");
//
// let mut client = self
// .block_on(
// ibc_proto::cosmos::upgrade::v1beta1::query_client::QueryClient::connect(
// self.grpc_addr.clone(),
// ),
// )
// .map_err(|e| Kind::Grpc.context(e))?;
//
// let req = tonic::Request::new(QueryCurrentPlanRequest {});
// let response = self
// .block_on(client.current_plan(req))
// .map_err(|e| Kind::Grpc.context(e))?;
//
// let upgraded_client_state_raw = response
// .into_inner()
// .plan
// .ok_or(Kind::EmptyResponseValue)?
// .upgraded_client_state
// .ok_or(Kind::EmptyUpgradedClientState)?;
// let client_state = AnyClientState::try_from(upgraded_client_state_raw)
// .map_err(|e| Kind::Grpc.context(e))?;
//
// // TODO: Better error kinds here.
// let tm_client_state =
// downcast!(client_state => AnyClientState::Tendermint).ok_or_else(|| {
// Kind::Query("upgraded client state".into()).context("unexpected client state type")
// })?;
//
// // Query for the proof.
// let tm_height =
// Height::try_from(height.revision_height).map_err(|e| Kind::InvalidHeight.context(e))?;
// let (proof, _proof_height) = self.query_client_upgrade_proof(
// ClientUpgradePath::UpgradedClientState(height.revision_height),
// tm_height,
// )?;
//
// Ok((tm_client_state, proof))
}

#[allow(unused_variables, dead_code)]
fn query_upgraded_consensus_state(
&self,
height: ICSHeight,
) -> Result<(Self::ConsensusState, MerkleProof), Error> {
crate::time!("query_upgraded_consensus_state");

let tm_height =
Height::try_from(height.revision_height).map_err(|e| Kind::InvalidHeight.context(e))?;

let mut client = self
.block_on(
ibc_proto::cosmos::upgrade::v1beta1::query_client::QueryClient::connect(
self.grpc_addr.clone(),
),
)
.map_err(|e| Kind::Grpc.context(e))?;

let req = tonic::Request::new(QueryUpgradedConsensusStateRequest {
last_height: tm_height.into(),
});
let response = self
.block_on(client.upgraded_consensus_state(req))
.map_err(|e| Kind::Grpc.context(e))?;

let upgraded_consensus_state_raw = response
.into_inner()
.upgraded_consensus_state
.ok_or(Kind::EmptyResponseValue)?;

// TODO: More explicit error kinds (should not reuse Grpc all over the place)
let consensus_state = AnyConsensusState::try_from(upgraded_consensus_state_raw)
.map_err(|e| Kind::Grpc.context(e))?;

let tm_consensus_state = downcast!(consensus_state => AnyConsensusState::Tendermint)
.ok_or_else(|| {
Kind::Query("upgraded consensus state".into())
.context("unexpected consensus state type")
})?;

// Fetch the proof.
let (proof, _proof_height) = self.query_client_upgrade_proof(
ClientUpgradePath::UpgradedClientConsensusState(height.revision_height),
tm_height,
)?;

Ok((tm_consensus_state, proof))
todo!()
// crate::time!("query_upgraded_consensus_state");
//
// let tm_height =
// Height::try_from(height.revision_height).map_err(|e| Kind::InvalidHeight.context(e))?;
//
// let mut client = self
// .block_on(
// ibc_proto::cosmos::upgrade::v1beta1::query_client::QueryClient::connect(
// self.grpc_addr.clone(),
// ),
// )
// .map_err(|e| Kind::Grpc.context(e))?;
//
// let req = tonic::Request::new(QueryUpgradedConsensusStateRequest {
// last_height: tm_height.into(),
// });
// let response = self
// .block_on(client.upgraded_consensus_state(req))
// .map_err(|e| Kind::Grpc.context(e))?;
//
// let upgraded_consensus_state_raw = response
// .into_inner()
// .upgraded_consensus_state
// .ok_or(Kind::EmptyResponseValue)?;
//
// // TODO: More explicit error kinds (should not reuse Grpc all over the place)
// let consensus_state = AnyConsensusState::try_from(upgraded_consensus_state_raw)
// .map_err(|e| Kind::Grpc.context(e))?;
//
// let tm_consensus_state = downcast!(consensus_state => AnyConsensusState::Tendermint)
// .ok_or_else(|| {
// Kind::Query("upgraded consensus state".into())
// .context("unexpected consensus state type")
// })?;
//
// // Fetch the proof.
// let (proof, _proof_height) = self.query_client_upgrade_proof(
// ClientUpgradePath::UpgradedClientConsensusState(height.revision_height),
// tm_height,
// )?;
//
// Ok((tm_consensus_state, proof))
}

/// Performs a query to retrieve the identifiers of all connections.
Expand Down
154 changes: 79 additions & 75 deletions relayer/src/upgrade_chain.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused_imports)]

use std::time::Duration;

use prost_types::Any;
Expand Down Expand Up @@ -38,84 +40,86 @@ pub struct UpdatePlanOptions {
pub height_offset: u64,
}

#[allow(unused_mut, unused_variables)]
pub fn build_and_send_upgrade_chain_message(
mut dst_chain: CosmosSdkChain, // the chain whose account is debited
src_chain: CosmosSdkChain, // the chain where the transfer is sent
opts: &UpdatePlanOptions,
) -> Result<Vec<IbcEvent>, UpgradeChainError> {
// build a proposal Plan
let upgrade_height = dst_chain
.query_latest_height()
.unwrap()
.add(opts.height_offset);

let client_state = src_chain
.query_client_state(&opts.src_client_id, Height::zero())
.unwrap();

let mut upgraded_client_state = ClientState::zero_custom_fields(client_state);
upgraded_client_state.latest_height = upgrade_height.increment();
upgraded_client_state.unbonding_period = Duration::from_secs(400 * 3600);

let raw_client_state = AnyClientState::Tendermint(upgraded_client_state);
let plan = Plan {
name: "test".to_string(),
time: None,
height: upgrade_height.revision_height as i64,
info: "upgrade the chain software and unbonding period".to_string(),
upgraded_client_state: Some(Any::from(raw_client_state)),
};

// build the proposal
let proposal = SoftwareUpgradeProposal {
title: "upgrade_ibc_clients".to_string(),
description: "upgrade the chain software and unbonding period".to_string(),
plan: Some(plan),
};

let mut buf_proposal = Vec::new();
prost::Message::encode(&proposal, &mut buf_proposal).unwrap();

let any_proposal = Any {
type_url: "/cosmos.upgrade.v1beta1.SoftwareUpgradeProposal".to_string(),
value: buf_proposal,
};

// build the msg submit proposal
let proposer = dst_chain
.get_signer()
.map_err(UpgradeChainError::KeyError)?;

let coins = ibc_proto::cosmos::base::v1beta1::Coin {
denom: "stake".to_string(),
amount: opts.amount.to_string(),
};

let msg = MsgSubmitProposal {
content: Some(any_proposal),
initial_deposit: vec![coins],
proposer: proposer.to_string(),
};

let mut buf_msg = Vec::new();
prost::Message::encode(&msg, &mut buf_msg).unwrap();
let any_msg = Any {
type_url: "/cosmos.gov.v1beta1.MsgSubmitProposal".to_string(),
value: buf_msg,
};

let events = dst_chain
.send_msgs(vec![any_msg])
.map_err(|e| UpgradeChainError::SubmitError(dst_chain.id().clone(), e))?;

// Check if the chain rejected the transaction
let result = events.iter().find_map(|event| match event {
IbcEvent::ChainError(reason) => Some(reason.clone()),
_ => None,
});

match result {
None => Ok(events),
Some(reason) => Err(UpgradeChainError::Failed(reason)),
}
todo!()
// // build a proposal Plan
// let upgrade_height = dst_chain
// .query_latest_height()
// .unwrap()
// .add(opts.height_offset);
//
// let client_state = src_chain
// .query_client_state(&opts.src_client_id, Height::zero())
// .unwrap();
//
// let mut upgraded_client_state = ClientState::zero_custom_fields(client_state);
// upgraded_client_state.latest_height = upgrade_height.increment();
// upgraded_client_state.unbonding_period = Duration::from_secs(400 * 3600);
//
// let raw_client_state = AnyClientState::Tendermint(upgraded_client_state);
// let plan = Plan {
// name: "test".to_string(),
// time: None,
// height: upgrade_height.revision_height as i64,
// info: "upgrade the chain software and unbonding period".to_string(),
// upgraded_client_state: Some(Any::from(raw_client_state)),
// };
//
// // build the proposal
// let proposal = SoftwareUpgradeProposal {
// title: "upgrade_ibc_clients".to_string(),
// description: "upgrade the chain software and unbonding period".to_string(),
// plan: Some(plan),
// };
//
// let mut buf_proposal = Vec::new();
// prost::Message::encode(&proposal, &mut buf_proposal).unwrap();
//
// let any_proposal = Any {
// type_url: "/cosmos.upgrade.v1beta1.SoftwareUpgradeProposal".to_string(),
// value: buf_proposal,
// };
//
// // build the msg submit proposal
// let proposer = dst_chain
// .get_signer()
// .map_err(UpgradeChainError::KeyError)?;
//
// let coins = ibc_proto::cosmos::base::v1beta1::Coin {
// denom: "stake".to_string(),
// amount: opts.amount.to_string(),
// };
//
// let msg = MsgSubmitProposal {
// content: Some(any_proposal),
// initial_deposit: vec![coins],
// proposer: proposer.to_string(),
// };
//
// let mut buf_msg = Vec::new();
// prost::Message::encode(&msg, &mut buf_msg).unwrap();
// let any_msg = Any {
// type_url: "/cosmos.gov.v1beta1.MsgSubmitProposal".to_string(),
// value: buf_msg,
// };
//
// let events = dst_chain
// .send_msgs(vec![any_msg])
// .map_err(|e| UpgradeChainError::SubmitError(dst_chain.id().clone(), e))?;
//
// // Check if the chain rejected the transaction
// let result = events.iter().find_map(|event| match event {
// IbcEvent::ChainError(reason) => Some(reason.clone()),
// _ => None,
// });
//
// match result {
// None => Ok(events),
// Some(reason) => Err(UpgradeChainError::Failed(reason)),
// }
}

0 comments on commit 30688a4

Please sign in to comment.