Skip to content

Commit

Permalink
migrate version only if newer pattern, and prevent migrating from oth…
Browse files Browse the repository at this point in the history
…er contracts
  • Loading branch information
NoahSaso committed Aug 2, 2024
1 parent 5b6093e commit 89bd350
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/distribution/dao-rewards-distributor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cw-utils = { workspace = true }
dao-hooks = { workspace = true }
dao-interface = { workspace = true }
dao-voting = { workspace = true }
semver = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
Expand Down
18 changes: 17 additions & 1 deletion contracts/distribution/dao-rewards-distributor/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cw20::{Cw20ReceiveMsg, Denom};
use cw_storage_plus::Bound;
use cw_utils::{must_pay, nonpayable, Duration, Expiration};
use dao_interface::voting::InfoResponse;
use semver::Version;

use std::ops::Add;

Expand Down Expand Up @@ -592,6 +593,21 @@ fn query_distributions(

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
let contract_version = get_contract_version(deps.storage)?;

Check warning on line 596 in contracts/distribution/dao-rewards-distributor/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/distribution/dao-rewards-distributor/src/contract.rs#L596

Added line #L596 was not covered by tests

if contract_version.contract != CONTRACT_NAME {
return Err(ContractError::MigrationErrorIncorrectContract {
expected: CONTRACT_NAME.to_string(),
actual: contract_version.contract,
});
}

Check warning on line 603 in contracts/distribution/dao-rewards-distributor/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/distribution/dao-rewards-distributor/src/contract.rs#L598-L603

Added lines #L598 - L603 were not covered by tests

let version: Version = CONTRACT_VERSION.parse()?;
let storage_version: Version = contract_version.version.parse()?;

Check warning on line 606 in contracts/distribution/dao-rewards-distributor/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/distribution/dao-rewards-distributor/src/contract.rs#L605-L606

Added lines #L605 - L606 were not covered by tests

if storage_version < version {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
}

Check warning on line 610 in contracts/distribution/dao-rewards-distributor/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/distribution/dao-rewards-distributor/src/contract.rs#L608-L610

Added lines #L608 - L610 were not covered by tests

Ok(Response::default())
}
12 changes: 12 additions & 0 deletions contracts/distribution/dao-rewards-distributor/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum ContractError {
#[error(transparent)]
Payment(#[from] PaymentError),

#[error("semver parsing error: {0}")]
SemVer(String),

#[error("Invalid CW20")]
InvalidCw20 {},

Expand Down Expand Up @@ -54,4 +57,13 @@ pub enum ContractError {

#[error("Cannot update emission rate because this distribution has accumulated the maximum rewards. Start a new distribution with the new emission rate instead. (Overflow: {err})")]
DistributionHistoryTooLarge { err: String },

#[error("Expected to migrate from contract {expected}. Got {actual}")]
MigrationErrorIncorrectContract { expected: String, actual: String },
}

impl From<semver::Error> for ContractError {
fn from(err: semver::Error) -> Self {
Self::SemVer(err.to_string())
}

Check warning on line 68 in contracts/distribution/dao-rewards-distributor/src/error.rs

View check run for this annotation

Codecov / codecov/patch

contracts/distribution/dao-rewards-distributor/src/error.rs#L66-L68

Added lines #L66 - L68 were not covered by tests
}

0 comments on commit 89bd350

Please sign in to comment.