Skip to content

Commit

Permalink
chore: Add new functions for migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Aug 20, 2024
1 parent ef3da24 commit d0356bc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub use crate::stdack::StdAck;
pub use crate::storage::MemoryStorage;
pub use crate::timestamp::Timestamp;
pub use crate::traits::{Api, HashFunction, Querier, QuerierResult, QuerierWrapper, Storage};
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, TransactionInfo};
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, MigrateInfo, TransactionInfo};

// Exposed in wasm build only

Expand Down
11 changes: 11 additions & 0 deletions packages/std/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,14 @@ pub struct MessageInfo {
pub struct ContractInfo {
pub address: Addr,
}

/// The structure contains additional information related to the
/// contract's state migration procedure - the sender address and
/// the contract's state version currently stored on the blockchain.
/// The `old_state_version` is optional, since there is no guarantee
/// that the currently stored contract's binary contains that information.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct MigrateInfo {
pub sender: Addr,
pub old_state_version: Option<u64>,
}
47 changes: 46 additions & 1 deletion packages/vm/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use wasmer::Value;

use cosmwasm_std::{
ContractResult, CustomMsg, Env, IbcBasicResponse, IbcDestinationCallbackMsg,
IbcSourceCallbackMsg, MessageInfo, QueryResponse, Reply, Response,
IbcSourceCallbackMsg, MessageInfo, MigrateInfo, QueryResponse, Reply, Response,
};
#[cfg(feature = "stargate")]
use cosmwasm_std::{
Expand Down Expand Up @@ -163,6 +163,26 @@ where
Ok(result)
}

pub fn call_migrate2<A, S, Q, U>(
instance: &mut Instance<A, S, Q>,
env: &Env,
msg: &[u8],
migrate_info: &MigrateInfo,
) -> VmResult<ContractResult<Response<U>>>
where
A: BackendApi + 'static,
S: Storage + 'static,
Q: Querier + 'static,
U: DeserializeOwned + CustomMsg,
{
let env = to_vec(env)?;
let migrate_info = to_vec(migrate_info)?;
let data = call_migrate2_raw(instance, &env, msg, &migrate_info)?;
let result: ContractResult<Response<U>> =
from_slice(&data, deserialization_limits::RESULT_MIGRATE)?;
Ok(result)
}

pub fn call_sudo<A, S, Q, U>(
instance: &mut Instance<A, S, Q>,
env: &Env,
Expand Down Expand Up @@ -441,6 +461,31 @@ where
)
}

/// Calls Wasm export "migrate" and returns raw data from the contract.
/// The result is length limited to prevent abuse but otherwise unchecked.
/// The difference between this function and [call_migrate_raw] is the
/// additional argument - `migrate_info`. It contains additional data
/// related to the contract's state migration procedure.
pub fn call_migrate2_raw<A, S, Q>(
instance: &mut Instance<A, S, Q>,
env: &[u8],
msg: &[u8],
migrate_info: &[u8],
) -> VmResult<Vec<u8>>
where
A: BackendApi + 'static,
S: Storage + 'static,
Q: Querier + 'static,
{
instance.set_storage_readonly(false);
call_raw(
instance,
"migrate",
&[env, migrate_info, msg],
read_limits::RESULT_MIGRATE,
)
}

/// Calls Wasm export "sudo" and returns raw data from the contract.
/// The result is length limited to prevent abuse but otherwise unchecked.
pub fn call_sudo_raw<A, S, Q>(
Expand Down
1 change: 0 additions & 1 deletion packages/vm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ impl<A: BackendApi, S: Storage, Q: Querier> Environment<A, S, Q> {
})?;
let function_arity = func.param_arity(store);
if args.len() != function_arity {
// TODO tkulik: when return error, try to call it with different args vector
return Err(VmError::func_arity_mismatch(function_arity));
};
self.increment_call_depth()?;
Expand Down
5 changes: 3 additions & 2 deletions packages/vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ pub use crate::cache::{
pub use crate::calls::{
call_execute, call_execute_raw, call_ibc_destination_callback,
call_ibc_destination_callback_raw, call_ibc_source_callback, call_ibc_source_callback_raw,
call_instantiate, call_instantiate_raw, call_migrate, call_migrate_raw, call_query,
call_query_raw, call_reply, call_reply_raw, call_sudo, call_sudo_raw,
call_instantiate, call_instantiate_raw, call_migrate, call_migrate2, call_migrate2_raw,
call_migrate_raw, call_query, call_query_raw, call_reply, call_reply_raw, call_sudo,
call_sudo_raw,
};
#[cfg(feature = "stargate")]
pub use crate::calls::{
Expand Down

0 comments on commit d0356bc

Please sign in to comment.