1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![allow(deprecated)] use core::time::Duration;
use bytes::BufMut;
use flex_error::define_error;
use tendermint::abci::transaction::Hash as TxHash;
use ibc::clients::ics07_tendermint::client_state::UpgradeOptions;
use ibc::core::ics02_client::client_state::{AnyClientState, ClientState};
use ibc::core::ics24_host::identifier::{ChainId, ClientId};
use ibc::downcast;
use ibc_proto::cosmos::gov::v1beta1::MsgSubmitProposal;
use ibc_proto::cosmos::upgrade::v1beta1::Plan;
use ibc_proto::google::protobuf::Any;
use ibc_proto::ibc::core::client::v1::UpgradeProposal;
use crate::chain::handle::ChainHandle;
use crate::chain::requests::{IncludeProof, QueryClientStateRequest, QueryHeight};
use crate::chain::tracking::TrackedMsgs;
use crate::config::ChainConfig;
use crate::error::Error;
define_error! {
UpgradeChainError {
Query
[ Error ]
|_| { "error during a query" },
Key
[ Error ]
|_| { "key error" },
Submit
{ chain_id: ChainId }
[ Error ]
|e| {
format!("failed while submitting the Transfer message to chain {0}", e.chain_id)
},
TxResponse
{ event: String }
|e| {
format!("tx response event consists of an error: {}", e.event)
},
TendermintOnly
|_| { "only Tendermint clients can be upgraded" }
}
}
#[derive(Clone, Debug)]
pub struct UpgradePlanOptions {
pub src_chain_config: ChainConfig,
pub dst_chain_config: ChainConfig,
pub src_client_id: ClientId,
pub amount: u64,
pub denom: String,
pub height_offset: u64,
pub upgraded_chain_id: ChainId,
pub upgraded_unbonding_period: Option<Duration>,
pub upgrade_plan_name: String,
}
pub fn build_and_send_ibc_upgrade_proposal(
dst_chain: impl ChainHandle, src_chain: impl ChainHandle, opts: &UpgradePlanOptions,
) -> Result<TxHash, UpgradeChainError> {
let upgrade_height = dst_chain
.query_latest_height() .map_err(UpgradeChainError::query)?
.add(opts.height_offset);
let (client_state, _) = src_chain
.query_client_state(
QueryClientStateRequest {
client_id: opts.src_client_id.clone(),
height: QueryHeight::Latest,
},
IncludeProof::No,
)
.map_err(UpgradeChainError::query)?;
let client_state = downcast!(client_state => AnyClientState::Tendermint)
.ok_or_else(UpgradeChainError::tendermint_only)?;
let upgrade_options = UpgradeOptions {
unbonding_period: opts
.upgraded_unbonding_period
.unwrap_or(client_state.unbonding_period),
};
let upgraded_client_state = client_state.upgrade(
upgrade_height.increment(),
upgrade_options,
opts.upgraded_chain_id.clone(),
);
let proposal = UpgradeProposal {
title: "proposal 0".to_string(),
description: "upgrade the chain software and unbonding period".to_string(),
upgraded_client_state: Some(Any::from(upgraded_client_state.wrap_any())),
plan: Some(Plan {
name: opts.upgrade_plan_name.clone(),
height: upgrade_height.revision_height() as i64,
info: "".to_string(),
..Default::default() }),
};
let proposal = Proposal::Default(proposal);
let mut buf_proposal = Vec::new();
proposal.encode(&mut buf_proposal);
let any_proposal = Any {
type_url: proposal.type_url(),
value: buf_proposal,
};
let proposer = dst_chain.get_signer().map_err(UpgradeChainError::key)?;
let coins = ibc_proto::cosmos::base::v1beta1::Coin {
denom: opts.denom.clone(),
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 responses = dst_chain
.send_messages_and_wait_check_tx(TrackedMsgs::new_single(any_msg, "upgrade"))
.map_err(|e| UpgradeChainError::submit(dst_chain.id(), e))?;
Ok(responses[0].hash)
}
enum Proposal {
Default(UpgradeProposal),
}
impl Proposal {
fn encode(&self, buf: &mut impl BufMut) {
match self {
Proposal::Default(p) => prost::Message::encode(p, buf),
}
.unwrap()
}
fn type_url(&self) -> String {
match self {
Proposal::Default(_) => "/ibc.core.client.v1.UpgradeProposal",
}
.to_owned()
}
}