From 824dfa3d6d5dd2fe163e8493ca580bdfcaf83277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 4 Jan 2024 16:12:16 +0000 Subject: [PATCH] added cw2 interface to dkg contract --- contracts/Cargo.lock | 6 ++-- contracts/Cargo.toml | 1 + contracts/coconut-dkg/Cargo.toml | 2 ++ contracts/coconut-dkg/src/contract.rs | 30 +++++++++++++++++-- contracts/coconut-dkg/src/error.rs | 6 ++++ contracts/mixnet/Cargo.toml | 2 +- contracts/name-service/Cargo.toml | 2 +- .../service-provider-directory/Cargo.toml | 2 +- contracts/vesting/Cargo.toml | 2 +- 9 files changed, 45 insertions(+), 8 deletions(-) diff --git a/contracts/Cargo.lock b/contracts/Cargo.lock index 8307dbe37f..f9ac94de48 100644 --- a/contracts/Cargo.lock +++ b/contracts/Cargo.lock @@ -1220,11 +1220,13 @@ dependencies = [ "cw-controllers", "cw-multi-test", "cw-storage-plus", + "cw2", "cw4", "cw4-group", "nym-coconut-dkg-common", "nym-group-contract-common", "rusty-fork", + "semver", "serde", "thiserror", ] @@ -1879,9 +1881,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.17" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" +checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" [[package]] name = "serde" diff --git a/contracts/Cargo.toml b/contracts/Cargo.toml index effb7d9df7..83f0a46531 100644 --- a/contracts/Cargo.toml +++ b/contracts/Cargo.toml @@ -48,5 +48,6 @@ cw3 = "=1.1.0" cw3-fixed-multisig = "=1.1.0" cw4 = "=1.1.0" cw20 = "=1.1.0" +semver = "1.0.21" thiserror = "1.0.48" diff --git a/contracts/coconut-dkg/Cargo.toml b/contracts/coconut-dkg/Cargo.toml index 8c62629309..13937bcb3a 100644 --- a/contracts/coconut-dkg/Cargo.toml +++ b/contracts/coconut-dkg/Cargo.toml @@ -20,8 +20,10 @@ cosmwasm-std = { workspace = true } cosmwasm-storage = { workspace = true } cw-storage-plus = { workspace = true } cw-controllers = { workspace = true } +cw2 = { workspace = true } cw4 = { workspace = true } serde = { version = "1.0.103", default-features = false, features = ["derive"] } +semver = { workspace = true, default-features = false } thiserror = { workspace = true } [dev-dependencies] diff --git a/contracts/coconut-dkg/src/contract.rs b/contracts/coconut-dkg/src/contract.rs index aa6b700486..022da6552f 100644 --- a/contracts/coconut-dkg/src/contract.rs +++ b/contracts/coconut-dkg/src/contract.rs @@ -24,6 +24,10 @@ use cosmwasm_std::{ use cw4::Cw4Contract; use nym_coconut_dkg_common::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}; use nym_coconut_dkg_common::types::{Epoch, EpochState, State}; +use semver::Version; + +const CONTRACT_NAME: &str = "crate:nym-coconut-dkg"; +const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); /// Instantiate the contract. /// @@ -64,6 +68,8 @@ pub fn instantiate( ), )?; + cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + Ok(Response::default()) } @@ -159,8 +165,28 @@ pub fn query(deps: Deps<'_>, _env: Env, msg: QueryMsg) -> Result, _env: Env, _msg: MigrateMsg) -> Result { - Ok(Default::default()) +pub fn migrate(deps: DepsMut<'_>, _env: Env, _msg: MigrateMsg) -> Result { + fn parse_semver(raw: &str) -> Result { + raw.parse() + .map_err(|error: semver::Error| ContractError::SemVerFailure { + value: CONTRACT_VERSION.to_string(), + error_message: error.to_string(), + }) + } + + // Note: don't remove this particular bit of code as we have to ALWAYS check whether we have to + // update the stored version + let build_version: Version = parse_semver(CONTRACT_VERSION)?; + let stored_version: Version = parse_semver(&cw2::get_contract_version(deps.storage)?.version)?; + + if stored_version < build_version { + cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + + // If state structure changed in any contract version in the way migration is needed, it + // should occur here, for example anything from `crate::queued_migrations::` + } + + Ok(Response::new()) } #[cfg(test)] diff --git a/contracts/coconut-dkg/src/error.rs b/contracts/coconut-dkg/src/error.rs index 3a00ba4b62..e302e1c3ab 100644 --- a/contracts/coconut-dkg/src/error.rs +++ b/contracts/coconut-dkg/src/error.rs @@ -68,4 +68,10 @@ pub enum ContractError { #[error("No verification key committed for owner {owner}")] NoCommitForOwner { owner: String }, + + #[error("failed to parse {value} into a valid SemVer version: {error_message}")] + SemVerFailure { + value: String, + error_message: String, + }, } diff --git a/contracts/mixnet/Cargo.toml b/contracts/mixnet/Cargo.toml index a6f1ba5d0e..c365dda0c6 100644 --- a/contracts/mixnet/Cargo.toml +++ b/contracts/mixnet/Cargo.toml @@ -41,7 +41,7 @@ bs58 = "0.4.0" serde = { version = "1.0.103", default-features = false, features = ["derive"] } thiserror = { workspace = true } time = { version = "0.3", features = ["macros"] } -semver = { version = "1.0.16", default-features = false } +semver = { workspace = true, default-features = false } [dev-dependencies] rand_chacha = "0.2" diff --git a/contracts/name-service/Cargo.toml b/contracts/name-service/Cargo.toml index e2b5ddb753..13cbb7a6c0 100644 --- a/contracts/name-service/Cargo.toml +++ b/contracts/name-service/Cargo.toml @@ -20,7 +20,7 @@ cw-utils = { workspace = true } cw2 = { workspace = true } nym-contracts-common = { path = "../../common/cosmwasm-smart-contracts/contracts-common", version = "0.5.0" } nym-name-service-common = { path = "../../common/cosmwasm-smart-contracts/name-service" } -semver = { version = "1.0.16", default-features = false } +semver = { workspace = true, default-features = false } serde = { version = "1.0.155", default-features = false, features = ["derive"] } thiserror = { workspace = true } diff --git a/contracts/service-provider-directory/Cargo.toml b/contracts/service-provider-directory/Cargo.toml index e6b36d3346..4acd39aa09 100644 --- a/contracts/service-provider-directory/Cargo.toml +++ b/contracts/service-provider-directory/Cargo.toml @@ -20,7 +20,7 @@ cw-utils = { workspace = true } cw2 = { workspace = true } nym-contracts-common = { path = "../../common/cosmwasm-smart-contracts/contracts-common", version = "0.5.0" } nym-service-provider-directory-common = { path = "../../common/cosmwasm-smart-contracts/service-provider-directory" } -semver = { version = "1.0.16", default-features = false } +semver = { workspace = true, default-features = false } serde = { version = "1.0.155", default-features = false, features = ["derive"] } thiserror = { workspace = true } diff --git a/contracts/vesting/Cargo.toml b/contracts/vesting/Cargo.toml index efd3f73006..28f3120c17 100644 --- a/contracts/vesting/Cargo.toml +++ b/contracts/vesting/Cargo.toml @@ -36,7 +36,7 @@ cw-storage-plus = { workspace = true, features = ["iterator"] } serde = { version = "1.0", default-features = false, features = ["derive"] } thiserror ={ workspace = true } -semver = { version = "1.0.16", default-features = false } +semver = { workspace = true, default-features = false } [dev-dependencies] rand_chacha = "0.3.1"