Skip to content

Commit

Permalink
Add legacy upgrade support (#1289)
Browse files Browse the repository at this point in the history
* Add legacy upgrade support

* Fix check for legacy

* Update .changelog

* Add TODO

* Apply suggestion

Co-authored-by: Adi Seredinschi <[email protected]>

Co-authored-by: Adi Seredinschi <[email protected]>
  • Loading branch information
hu55a1n1 and adizere authored Aug 17, 2021
1 parent 7e38aff commit 4bf511c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/unreleased/features/1287-upgrade-legacy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Add `--legacy | -l` flag to support upgrades for chains built with Cosmos SDK < v0.43.0 ([#1287])

[#1287]: https://github.com/informalsystems/ibc-rs/issues/1287
7 changes: 7 additions & 0 deletions relayer-cli/src/commands/tx/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ pub struct TxIbcUpgradeChainCmd {
help = "a string to name the upgrade proposal plan (default: 'plan')"
)]
upgrade_name: Option<String>,

#[options(
help = "use legacy upgrade proposal constructs (for chains built with Cosmos SDK < v0.43.0)",
short = "l"
)]
legacy: bool,
}

impl TxIbcUpgradeChainCmd {
Expand Down Expand Up @@ -88,6 +94,7 @@ impl TxIbcUpgradeChainCmd {
.upgrade_name
.clone()
.unwrap_or_else(|| "plan".to_string()),
legacy: self.legacy,
};

Ok(opts)
Expand Down
64 changes: 60 additions & 4 deletions relayer/src/upgrade_chain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Chain upgrade plans for triggering IBC-breaking upgrades.
#![allow(deprecated)] // TODO(hu55a1n1): remove this when we don't need legacy upgrade support

use std::time::Duration;

use bytes::BufMut;
use flex_error::define_error;
use prost_types::Any;

Expand All @@ -10,7 +12,7 @@ use ibc::ics02_client::height::Height;
use ibc::ics24_host::identifier::{ChainId, ClientId};
use ibc::{events::IbcEvent, ics07_tendermint::client_state::ClientState};
use ibc_proto::cosmos::gov::v1beta1::MsgSubmitProposal;
use ibc_proto::cosmos::upgrade::v1beta1::Plan;
use ibc_proto::cosmos::upgrade::v1beta1::{Plan, SoftwareUpgradeProposal};
use ibc_proto::ibc::core::client::v1::UpgradeProposal;

use crate::chain::{ChainEndpoint, CosmosSdkChain};
Expand Down Expand Up @@ -55,6 +57,7 @@ pub struct UpgradePlanOptions {
pub upgraded_chain_id: ChainId,
pub upgraded_unbonding_period: Option<Duration>,
pub upgrade_plan_name: String,
pub legacy: bool,
}

pub fn build_and_send_ibc_upgrade_proposal(
Expand Down Expand Up @@ -94,11 +97,16 @@ pub fn build_and_send_ibc_upgrade_proposal(
}),
};

let mut buf_proposal = Vec::new();
prost::Message::encode(&proposal, &mut buf_proposal).unwrap();
let proposal = if opts.legacy {
Proposal::Legacy(proposal.into())
} else {
Proposal::Default(proposal)
};

let mut buf_proposal = Vec::new();
proposal.encode(&mut buf_proposal);
let any_proposal = Any {
type_url: "/ibc.core.client.v1.UpgradeProposal".to_string(),
type_url: proposal.type_url(),
value: buf_proposal,
};

Expand Down Expand Up @@ -138,3 +146,51 @@ pub fn build_and_send_ibc_upgrade_proposal(
Some(reason) => Err(UpgradeChainError::tx_response(reason)),
}
}

enum Proposal {
Default(UpgradeProposal),
Legacy(LegacyProposal),
}

impl Proposal {
fn encode(&self, buf: &mut impl BufMut) {
match self {
Proposal::Default(p) => prost::Message::encode(p, buf),
Proposal::Legacy(p) => prost::Message::encode(&p.0, buf),
}
.unwrap()
}

fn type_url(&self) -> String {
match self {
Proposal::Default(_) => "/ibc.core.client.v1.UpgradeProposal",
Proposal::Legacy(_) => "/cosmos.upgrade.v1beta1.SoftwareUpgradeProposal",
}
.to_owned()
}
}

struct LegacyProposal(SoftwareUpgradeProposal);

impl From<UpgradeProposal> for LegacyProposal {
fn from(v: UpgradeProposal) -> Self {
let plan = {
if let Some(plan) = v.plan {
Some(Plan {
name: plan.name,
height: plan.height,
info: plan.info,
time: None,
upgraded_client_state: v.upgraded_client_state,
})
} else {
None
}
};
Self(SoftwareUpgradeProposal {
title: v.title,
description: v.description,
plan,
})
}
}

0 comments on commit 4bf511c

Please sign in to comment.