Skip to content

Call migrate method only when there is a change in the contract's state required #2087

Closed
@kulikthebird

Description

@kulikthebird

Contract's state version in the wasm binary

Currently each time a new contract binary version is uploaded to Cosmwasm it is required to call a migrate entry point method. The purpose of this entry point is to let the contract to align the state stored on the blockchain to the new version of the contract. Lack of the migrate entry point in the contract results in an error.

However there are significantly many cases when the binary is uploaded but it doesn't change the state in the process, for e.g. bug fixing, new implementation basing on the current version of the state etc. It is a problem, because each call of a migrate method (even the one that does almost nothing) costs gas. In a big scale it might increase the costs quite significantly.

Required steps

There are two sub-issues to be resolved for this issue:

Result

CosmWasm will provide the on-chain state version of the contract in the new message:

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct MigrateInfo {
    pub sender: Addr,
    pub state_version_on_chain: Option<u64>,
}

Once the current version is provided, developers could implement the migration procedure in a new way. The example of the new approach:

/// This method is called only when the current state version is different
/// than the new contract's one after the contract's binary update procedure.
/// The current version can be found in `info.state_version_on_chain`
///
#[entry_point]
#[set_contract_state_version(3)]  // the new macro call here
pub fn migrate(deps: DepsMut, env: Env, info: MigrateInfo, _msg: MigrateContract) -> StdResult<Response> {
    match info.state_version_on_chain {
        Some(0) => {},
        Some(1) => {},
        Some(2) => {},
        None => {
            // Fallback to the previous method of migrating contract's state.
        }
    }
    Ok(Response::default())
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions