From ed7a84a1cebc97821290e2b9c9c760906b4232fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Wed, 4 Sep 2024 09:55:23 +0100 Subject: [PATCH] made 'owner' field optional to prepare for its future removal --- .../cosmwasm-smart-contracts/mixnet-contract/src/types.rs | 3 ++- contracts/mixnet/schema/nym-mixnet-contract.json | 7 +++++-- contracts/mixnet/schema/raw/response_to_get_state.json | 7 +++++-- contracts/mixnet/src/contract.rs | 4 ++-- contracts/mixnet/src/mixnet_contract_settings/queries.rs | 2 +- .../mixnet/src/mixnet_contract_settings/transactions.rs | 2 +- contracts/mixnet/src/queued_migrations.rs | 6 +++++- 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs b/common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs index 749de1cc7e..f95c15eb34 100644 --- a/common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs +++ b/common/cosmwasm-smart-contracts/mixnet-contract/src/types.rs @@ -190,7 +190,8 @@ pub struct ContractState { #[deprecated( note = "use explicit ADMIN instead. this field will be removed in future release" )] - pub owner: Addr, + #[serde(default)] + pub owner: Option, /// Address of "rewarding validator" (nym-api) that's allowed to send any rewarding-related transactions. pub rewarding_validator_address: Addr, diff --git a/contracts/mixnet/schema/nym-mixnet-contract.json b/contracts/mixnet/schema/nym-mixnet-contract.json index 772b9b6068..c754e65fdb 100644 --- a/contracts/mixnet/schema/nym-mixnet-contract.json +++ b/contracts/mixnet/schema/nym-mixnet-contract.json @@ -8190,7 +8190,6 @@ "description": "The current state of the mixnet contract.", "type": "object", "required": [ - "owner", "params", "rewarding_denom", "rewarding_validator_address", @@ -8199,10 +8198,14 @@ "properties": { "owner": { "description": "Address of the contract owner.", + "default": null, "deprecated": true, - "allOf": [ + "anyOf": [ { "$ref": "#/definitions/Addr" + }, + { + "type": "null" } ] }, diff --git a/contracts/mixnet/schema/raw/response_to_get_state.json b/contracts/mixnet/schema/raw/response_to_get_state.json index 616f678dbd..9103e87b72 100644 --- a/contracts/mixnet/schema/raw/response_to_get_state.json +++ b/contracts/mixnet/schema/raw/response_to_get_state.json @@ -4,7 +4,6 @@ "description": "The current state of the mixnet contract.", "type": "object", "required": [ - "owner", "params", "rewarding_denom", "rewarding_validator_address", @@ -13,10 +12,14 @@ "properties": { "owner": { "description": "Address of the contract owner.", + "default": null, "deprecated": true, - "allOf": [ + "anyOf": [ { "$ref": "#/definitions/Addr" + }, + { + "type": "null" } ] }, diff --git a/contracts/mixnet/src/contract.rs b/contracts/mixnet/src/contract.rs index 5e7cacccd7..f56af17cd7 100644 --- a/contracts/mixnet/src/contract.rs +++ b/contracts/mixnet/src/contract.rs @@ -31,7 +31,7 @@ fn default_initial_state( // we have to temporarily preserve this functionalities until it can be removed #[allow(deprecated)] ContractState { - owner, + owner: Some(owner), rewarding_validator_address, vesting_contract_address, rewarding_denom: rewarding_denom.clone(), @@ -607,7 +607,7 @@ mod tests { #[allow(deprecated)] let expected_state = ContractState { - owner: Addr::unchecked("sender"), + owner: Some(Addr::unchecked("sender")), rewarding_validator_address: Addr::unchecked("foomp123"), vesting_contract_address: Addr::unchecked("bar456"), rewarding_denom: "uatom".into(), diff --git a/contracts/mixnet/src/mixnet_contract_settings/queries.rs b/contracts/mixnet/src/mixnet_contract_settings/queries.rs index d8844c3adb..5d80fec78a 100644 --- a/contracts/mixnet/src/mixnet_contract_settings/queries.rs +++ b/contracts/mixnet/src/mixnet_contract_settings/queries.rs @@ -44,7 +44,7 @@ pub(crate) mod tests { #[allow(deprecated)] let dummy_state = ContractState { - owner: Addr::unchecked("foomp"), + owner: Some(Addr::unchecked("foomp")), rewarding_validator_address: Addr::unchecked("monitor"), vesting_contract_address: Addr::unchecked("foomp"), rewarding_denom: "unym".to_string(), diff --git a/contracts/mixnet/src/mixnet_contract_settings/transactions.rs b/contracts/mixnet/src/mixnet_contract_settings/transactions.rs index 00da2ae97a..b458f6fbbc 100644 --- a/contracts/mixnet/src/mixnet_contract_settings/transactions.rs +++ b/contracts/mixnet/src/mixnet_contract_settings/transactions.rs @@ -25,7 +25,7 @@ pub fn try_update_contract_admin( // during 'execute_update_admin' call #[allow(deprecated)] storage::CONTRACT_STATE.update(deps.storage, |mut state| -> StdResult<_> { - state.owner = new_admin; + state.owner = Some(new_admin); Ok(state) })?; diff --git a/contracts/mixnet/src/queued_migrations.rs b/contracts/mixnet/src/queued_migrations.rs index 68ee265f3b..248a33d30e 100644 --- a/contracts/mixnet/src/queued_migrations.rs +++ b/contracts/mixnet/src/queued_migrations.rs @@ -54,9 +54,13 @@ pub(crate) fn vesting_purge(deps: DepsMut) -> Result<(), MixnetContractError> { pub(crate) fn explicit_contract_admin(deps: DepsMut) -> Result<(), MixnetContractError> { // we need to read the deprecated field to migrate it over #[allow(deprecated)] + // SAFETY: this value should ALWAYS exist on the first execution of this migration; + // as a matter of fact, it should ALWAYS continue existing until another migration + #[allow(clippy::expect_used)] let existing_admin = mixnet_params_storage::CONTRACT_STATE .load(deps.storage)? - .owner; + .owner + .expect("the contract state is corrupt - there's no admin set"); mixnet_params_storage::ADMIN.set(deps, Some(existing_admin))?; Ok(()) }