Skip to content

Commit

Permalink
feat: Conditional migrate call with extra MigrateInfo argument after …
Browse files Browse the repository at this point in the history
…contract update
  • Loading branch information
kulikthebird committed Sep 4, 2024
1 parent 33ba7c9 commit c09d19e
Show file tree
Hide file tree
Showing 17 changed files with 420 additions and 87 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ jobs:
- cargocache-v2-contract_hackatom-rust:1.74-{{ checksum "Cargo.lock" }}
- check_contract:
min_version: "1.4"
skip_cosmwasm_check: true
- save_cache:
paths:
- /usr/local/cargo/registry
Expand Down
2 changes: 1 addition & 1 deletion contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Introducing the development contracts in the order they were created.
[mask contract](https://medium.com/cosmwasm/introducing-the-mask-41d11e51bccf),
which allows the user to send messages to the contract which are then emitted
with the contract as the sender. It later got support to handle sub messages
and replys ([#796](https://github.com/CosmWasm/cosmwasm/pull/796)).
and replies ([#796](https://github.com/CosmWasm/cosmwasm/pull/796)).
4. **staking** is a staking derivatives example showing how the contract itself
can be a delegator.
5. **burner** shows how contract migrations work, which were added in CosmWasm
Expand Down
2 changes: 1 addition & 1 deletion contracts/hackatom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cranelift = ["cosmwasm-vm/cranelift"]

[dependencies]
cosmwasm-schema = { path = "../../packages/schema" }
cosmwasm-std = { path = "../../packages/std", default-features = false, features = ["std", "abort"] }
cosmwasm-std = { path = "../../packages/std", default-features = false, features = ["std", "abort", "cosmwasm_2_2"] }
schemars = "0.8.12"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
sha2 = "0.10"
Expand Down
26 changes: 21 additions & 5 deletions contracts/hackatom/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use sha2::{Digest, Sha256};

use cosmwasm_std::{
entry_point, from_json, to_json_binary, to_json_vec, Addr, AllBalanceResponse, Api, BankMsg,
BankQuery, CanonicalAddr, Deps, DepsMut, Env, Event, MessageInfo, QueryRequest, QueryResponse,
Response, StdError, StdResult, WasmMsg, WasmQuery,
BankQuery, CanonicalAddr, Deps, DepsMut, Env, Event, MessageInfo, MigrateInfo, QueryRequest,
QueryResponse, Response, StdError, StdResult, WasmMsg, WasmQuery,
};

use crate::errors::HackError;
Expand Down Expand Up @@ -35,9 +35,21 @@ pub fn instantiate(
Ok(Response::new().add_attribute("Let the", "hacking begin"))
}

const CONTRACT_MIGRATE_VERSION: u64 = 420;

#[entry_point]
#[migrate_version(42)]
pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, HackError> {
#[migrate_version(CONTRACT_MIGRATE_VERSION)]
pub fn migrate(
deps: DepsMut,
_env: Env,
msg: MigrateMsg,
migrate_info: MigrateInfo,
) -> Result<Response, HackError> {
if let Some(old_version) = migrate_info.old_migrate_version {
if CONTRACT_MIGRATE_VERSION <= old_version {
return Err(HackError::Downgrade);
}
}
let data = deps
.storage
.get(CONFIG_KEY)
Expand Down Expand Up @@ -379,7 +391,11 @@ mod tests {
let msg = MigrateMsg {
verifier: new_verifier.clone(),
};
let res = migrate(deps.as_mut(), mock_env(), msg).unwrap();
let migrate_info = MigrateInfo {
sender: creator,
old_migrate_version: None,
};
let res = migrate(deps.as_mut(), mock_env(), msg, migrate_info).unwrap();
assert_eq!(0, res.messages.len());

// check it is 'someone else'
Expand Down
3 changes: 3 additions & 0 deletions contracts/hackatom/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ pub enum HackError {
// this is whatever we want
#[error("Unauthorized")]
Unauthorized {},
// this is whatever we want
#[error("Downgrade is not supported")]
Downgrade,
}
12 changes: 8 additions & 4 deletions contracts/hackatom/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
use cosmwasm_std::{
assert_approx_eq, coins, from_json, to_json_vec, Addr, AllBalanceResponse, BankMsg, Binary,
ContractResult, Empty, Response, SubMsg,
ContractResult, Empty, MigrateInfo, Response, SubMsg,
};
use cosmwasm_vm::{
call_execute, from_slice,
testing::{
execute, instantiate, migrate, mock_env, mock_info, mock_instance,
execute, instantiate, migrate_with_info, mock_env, mock_info, mock_instance,
mock_instance_with_balances, query, sudo, test_io, MockApi, MOCK_CONTRACT_ADDR,
},
Storage, VmError,
Expand Down Expand Up @@ -53,7 +53,7 @@ fn make_init_msg(api: &MockApi) -> (InstantiateMsg, String) {
#[test]
fn proper_initialization() {
let mut deps = mock_instance(WASM, &[]);
assert_eq!(deps.required_capabilities().len(), 0);
assert_eq!(deps.required_capabilities().len(), 7);

let verifier = deps.api().addr_make("verifies");
let beneficiary = deps.api().addr_make("benefits");
Expand Down Expand Up @@ -142,7 +142,11 @@ fn migrate_verifier() {
let msg = MigrateMsg {
verifier: someone_else.clone(),
};
let res: Response = migrate(&mut deps, mock_env(), msg).unwrap();
let migrate_info = MigrateInfo {
sender: Addr::unchecked(creator),
old_migrate_version: None,
};
let res: Response = migrate_with_info(&mut deps, mock_env(), msg, migrate_info).unwrap();
assert_eq!(0, res.messages.len());

// check it is 'someone else'
Expand Down
2 changes: 1 addition & 1 deletion packages/check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use cosmwasm_vm::internals::{check_wasm, compile, make_compiling_engine, LogOutp
use cosmwasm_vm::{capabilities_from_csv, WasmLimits};

const DEFAULT_AVAILABLE_CAPABILITIES: &str =
"iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3,cosmwasm_1_4,cosmwasm_2_0,cosmwasm_2_1";
"iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3,cosmwasm_1_4,cosmwasm_2_0,cosmwasm_2_1,cosmwasm_2_2";

pub fn main() {
let matches = Command::new("Contract checking")
Expand Down
Loading

0 comments on commit c09d19e

Please sign in to comment.