Skip to content

Commit

Permalink
Merge pull request #2166 from CosmWasm/chipshort/rename-state-version
Browse files Browse the repository at this point in the history
Rename `state_version` to `migrate_version`
  • Loading branch information
chipshort authored Jun 3, 2024
2 parents 76479b3 + ea886d4 commit 3a6613f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ and this project adheres to
`Uint64::strict_add`/`::strict_sub` and document overflows. ([#2098], [#2107])
- cosmwasm-std: Add `QuerierWrapper::query_grpc` helper for gRPC queries.
([#2120])
- cosmwasm-derive: Add `state_version` attribute for `migrate` entrypoints
([#2124])
- cosmwasm-vm: Read the state version from Wasm modules and return them as part
of `AnalyzeReport` ([#2129])
- cosmwasm-derive: Add `migrate_version` attribute for `migrate` entrypoints
([#2124], [#2166])
- cosmwasm-vm: Read the migrate version from Wasm modules and return them as
part of `AnalyzeReport` ([#2129], [#2166])
- cosmwasm-vm: Add `bls12_381_aggregate_g1`, `bls12_381_aggregate_g2`,
`bls12_381_pairing_equality`, `bls12_381_hash_to_g1`, and
`bls12_381_hash_to_g2` to enable BLS12-381 curve operations, such as verifying
Expand All @@ -63,6 +63,7 @@ and this project adheres to
[#2120]: https://github.com/CosmWasm/cosmwasm/pull/2120
[#2124]: https://github.com/CosmWasm/cosmwasm/pull/2124
[#2129]: https://github.com/CosmWasm/cosmwasm/pull/2129
[#2166]: https://github.com/CosmWasm/cosmwasm/pull/2166

### Changed

Expand Down
30 changes: 15 additions & 15 deletions packages/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Parse for Options {
/// #
/// # type MigrateMsg = ();
/// #[entry_point]
/// #[state_version(2)]
/// #[migrate_version(2)]
/// pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> StdResult<Response> {
/// todo!();
/// }
Expand All @@ -123,7 +123,7 @@ fn expand_attributes(func: &mut ItemFn) -> syn::Result<TokenStream> {
let attributes = std::mem::take(&mut func.attrs);
let mut stream = TokenStream::new();
for attribute in attributes {
if !attribute.path().is_ident("state_version") {
if !attribute.path().is_ident("migrate_version") {
func.attrs.push(attribute);
continue;
}
Expand Down Expand Up @@ -152,10 +152,10 @@ fn expand_attributes(func: &mut ItemFn) -> syn::Result<TokenStream> {
#[allow(unused)]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
#[link_section = "cw_state_version"]
/// This is an internal constant exported as a custom section denoting the contract state version.
#[link_section = "cw_migrate_version"]
/// This is an internal constant exported as a custom section denoting the contract migrate version.
/// The format and even the existence of this value is an implementation detail, DO NOT RELY ON THIS!
static __CW_STATE_VERSION: &str = #version;
static __CW_MIGRATE_VERSION: &str = #version;
};
}

Expand Down Expand Up @@ -202,7 +202,7 @@ mod test {
#[test]
fn contract_state_zero_not_allowed() {
let code = quote! {
#[state_version(0)]
#[migrate_version(0)]
fn migrate() -> Response {
// Logic here
}
Expand All @@ -217,9 +217,9 @@ mod test {
}

#[test]
fn contract_state_version_on_non_migrate() {
fn contract_migrate_version_on_non_migrate() {
let code = quote! {
#[state_version(42)]
#[migrate_version(42)]
fn anything_else() -> Response {
// Logic here
}
Expand All @@ -234,9 +234,9 @@ mod test {
}

#[test]
fn contract_state_version_in_u64() {
fn contract_migrate_version_in_u64() {
let code = quote! {
#[state_version(0xDEAD_BEEF_FFFF_DEAD_2BAD)]
#[migrate_version(0xDEAD_BEEF_FFFF_DEAD_2BAD)]
fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> Response {
// Logic here
}
Expand All @@ -251,9 +251,9 @@ mod test {
}

#[test]
fn contract_state_version_expansion() {
fn contract_migrate_version_expansion() {
let code = quote! {
#[state_version(2)]
#[migrate_version(2)]
fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> Response {
// Logic here
}
Expand All @@ -264,10 +264,10 @@ mod test {
#[allow(unused)]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
#[link_section = "cw_state_version"]
/// This is an internal constant exported as a custom section denoting the contract state version.
#[link_section = "cw_migrate_version"]
/// This is an internal constant exported as a custom section denoting the contract migrate version.
/// The format and even the existence of this value is an implementation detail, DO NOT RELY ON THIS!
static __CW_STATE_VERSION: &str = "2";
static __CW_MIGRATE_VERSION: &str = "2";

fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> Response {
// Logic here
Expand Down
16 changes: 8 additions & 8 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ pub struct AnalysisReport {
pub entrypoints: BTreeSet<Entrypoint>,
/// The set of capabilities the contract requires.
pub required_capabilities: BTreeSet<String>,
/// The contract state version exported set by the contract developer
pub contract_state_version: Option<u64>,
/// The contract migrate version exported set by the contract developer
pub contract_migrate_version: Option<u64>,
}

impl<A, S, Q> Cache<A, S, Q>
Expand Down Expand Up @@ -323,7 +323,7 @@ where
required_capabilities: required_capabilities_from_module(&module)
.into_iter()
.collect(),
contract_state_version: module.contract_state_version,
contract_migrate_version: module.contract_migrate_version,
})
}

Expand Down Expand Up @@ -1416,7 +1416,7 @@ mod tests {
E::Query
]),
required_capabilities: BTreeSet::new(),
contract_state_version: None,
contract_migrate_version: None,
}
);

Expand All @@ -1434,7 +1434,7 @@ mod tests {
"iterator".to_string(),
"stargate".to_string()
]),
contract_state_version: None,
contract_migrate_version: None,
}
);

Expand All @@ -1446,13 +1446,13 @@ mod tests {
has_ibc_entry_points: false,
entrypoints: BTreeSet::new(),
required_capabilities: BTreeSet::from(["iterator".to_string()]),
contract_state_version: None,
contract_migrate_version: None,
}
);

let mut wasm_with_version = EMPTY_CONTRACT.to_vec();
let custom_section = wasm_encoder::CustomSection {
name: Cow::Borrowed("cw_state_version"),
name: Cow::Borrowed("cw_migrate_version"),
data: Cow::Borrowed(b"21"),
};
custom_section.append_to_component(&mut wasm_with_version);
Expand All @@ -1465,7 +1465,7 @@ mod tests {
has_ibc_entry_points: false,
entrypoints: BTreeSet::new(),
required_capabilities: BTreeSet::from(["iterator".to_string()]),
contract_state_version: Some(21),
contract_migrate_version: Some(21),
}
);
}
Expand Down
21 changes: 11 additions & 10 deletions packages/vm/src/parsed_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ pub struct ParsedWasm<'a> {
pub total_func_params: usize,
/// Collections of functions that are potentially pending validation
pub func_validator: FunctionValidator<'a>,
/// Contract state version as defined in a custom section
pub contract_state_version: Option<u64>,
/// Contract migrate version as defined in a custom section
pub contract_migrate_version: Option<u64>,
}

impl<'a> ParsedWasm<'a> {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl<'a> ParsedWasm<'a> {
max_func_results: 0,
total_func_params: 0,
func_validator: FunctionValidator::Pending(OpaqueDebug::default()),
contract_state_version: None,
contract_migrate_version: None,
};

for p in Parser::new(0).parse_all(wasm) {
Expand Down Expand Up @@ -182,12 +182,12 @@ impl<'a> ParsedWasm<'a> {
Payload::ExportSection(e) => {
this.exports = e.into_iter().collect::<Result<Vec<_>, _>>()?;
}
Payload::CustomSection(reader) if reader.name() == "cw_state_version" => {
Payload::CustomSection(reader) if reader.name() == "cw_migrate_version" => {
// This is supposed to be valid UTF-8
let raw_version = str::from_utf8(reader.data())
.map_err(|err| VmError::static_validation_err(err.to_string()))?;

this.contract_state_version = Some(
this.contract_migrate_version = Some(
raw_version
.parse::<u64>()
.map_err(|err| VmError::static_validation_err(err.to_string()))?,
Expand Down Expand Up @@ -234,18 +234,19 @@ mod test {
use super::ParsedWasm;

#[test]
fn read_state_version() {
fn read_migrate_version() {
let wasm_data =
wat::parse_str(r#"( module ( @custom "cw_state_version" "42" ) )"#).unwrap();
wat::parse_str(r#"( module ( @custom "cw_migrate_version" "42" ) )"#).unwrap();
let parsed = ParsedWasm::parse(&wasm_data).unwrap();

assert_eq!(parsed.contract_state_version, Some(42));
assert_eq!(parsed.contract_migrate_version, Some(42));
}

#[test]
fn read_state_version_fails() {
fn read_migrate_version_fails() {
let wasm_data =
wat::parse_str(r#"( module ( @custom "cw_state_version" "not a number" ) )"#).unwrap();
wat::parse_str(r#"( module ( @custom "cw_migrate_version" "not a number" ) )"#)
.unwrap();
assert!(ParsedWasm::parse(&wasm_data).is_err());
}
}

0 comments on commit 3a6613f

Please sign in to comment.