Skip to content

Commit 8e66724

Browse files
committed
refactor: improve migration code
1 parent c7305be commit 8e66724

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/contract.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
8989
#[allow(clippy::pedantic)]
9090
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
9191
migrate::validate_semver(deps.as_ref())?;
92-
93-
// Reject the migration if the channel encoding is not protobuf
94-
if let Some(ica_info) = state::STATE.load(deps.storage)?.ica_info {
95-
if !matches!(
96-
ica_info.encoding,
97-
crate::ibc::types::metadata::TxEncoding::Protobuf
98-
) {
99-
return Err(ContractError::UnsupportedPacketEncoding(
100-
ica_info.encoding.to_string(),
101-
));
102-
}
103-
}
92+
migrate::validate_channel_encoding(deps.as_ref())?;
10493

10594
cw2::set_contract_version(deps.storage, keys::CONTRACT_NAME, keys::CONTRACT_VERSION)?;
10695
// If state structure changed in any contract version in the way migration is needed, it
@@ -255,8 +244,10 @@ mod query {
255244
}
256245

257246
mod migrate {
258-
use super::{keys, ContractError, Deps};
247+
use super::{keys, state, ContractError, Deps};
259248

249+
/// Validate that the contract version is semver compliant
250+
/// and greater than the previous version.
260251
pub fn validate_semver(deps: Deps) -> Result<(), ContractError> {
261252
let prev_cw2_version = cw2::get_contract_version(deps.storage)?;
262253
if prev_cw2_version.contract != keys::CONTRACT_NAME {
@@ -276,6 +267,23 @@ mod migrate {
276267
}
277268
Ok(())
278269
}
270+
271+
/// Validate that the channel encoding is protobuf if set.
272+
pub fn validate_channel_encoding(deps: Deps) -> Result<(), ContractError> {
273+
// Reject the migration if the channel encoding is not protobuf
274+
if let Some(ica_info) = state::STATE.load(deps.storage)?.ica_info {
275+
if !matches!(
276+
ica_info.encoding,
277+
crate::ibc::types::metadata::TxEncoding::Protobuf
278+
) {
279+
return Err(ContractError::UnsupportedPacketEncoding(
280+
ica_info.encoding.to_string(),
281+
));
282+
}
283+
}
284+
285+
Ok(())
286+
}
279287
}
280288

281289
#[cfg(test)]
@@ -495,7 +503,14 @@ mod tests {
495503
.unwrap();
496504

497505
// Migration should fail because the encoding is not protobuf
498-
let _err = migrate(deps.as_mut(), mock_env(), MigrateMsg {}).unwrap_err();
506+
let err = migrate(deps.as_mut(), mock_env(), MigrateMsg {}).unwrap_err();
507+
assert_eq!(
508+
err.to_string(),
509+
ContractError::UnsupportedPacketEncoding(
510+
crate::ibc::types::metadata::TxEncoding::Proto3Json.to_string()
511+
)
512+
.to_string()
513+
);
499514

500515
// Set the encoding to protobuf
501516
state::STATE

0 commit comments

Comments
 (0)