Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_wasm_version! to create custom Wasm section #1946

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion contracts/cyberpunk/Cargo.lock

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

3 changes: 2 additions & 1 deletion contracts/cyberpunk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cyberpunk"
version = "0.0.0"
version = "0.12.345"
authors = ["Tomasz Kurcz <[email protected]>"]
edition = "2021"
publish = false
Expand Down Expand Up @@ -33,6 +33,7 @@ cosmwasm-schema = { path = "../../packages/schema" }
cosmwasm-std = { path = "../../packages/std", default-features = false, features = ["abort", "cosmwasm_1_3"] }
rust-argon2 = "0.8"
thiserror = "1.0.26"
const-str = "0.5.6"

[dev-dependencies]
cosmwasm-vm = { path = "../../packages/vm", default-features = false }
Expand Down
6 changes: 6 additions & 0 deletions contracts/cyberpunk/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use cosmwasm_std::{

use crate::errors::ContractError;
use crate::msg::{ExecuteMsg, QueryMsg};
use crate::set_wasm_version;

const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

set_wasm_version!(CONTRACT_NAME, CONTRACT_VERSION);

#[entry_point]
pub fn instantiate(
Expand Down
2 changes: 2 additions & 0 deletions contracts/cyberpunk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod contract;
pub mod errors;
pub mod msg;

mod set_version;
15 changes: 15 additions & 0 deletions contracts/cyberpunk/src/set_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[macro_export]
macro_rules! set_wasm_version {
($s1: expr, $s2: expr) => {
/// Private module with no stability guarantees
mod __cw5 {
use super::*;

const FULL: &str = const_str::concat!("/", $s1, "/", $s2);

#[link_section = "cw5"]
#[allow(unused)]
static AS_BYTES: [u8; FULL.len()] = const_str::to_byte_array!(FULL);
Comment on lines +10 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As simple as that. I like it.

Copy link
Contributor

@maurolacy maurolacy Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The #[used] attribute can also be useful, to prevent this from disappearing after optimisation: https://doc.rust-lang.org/beta/reference/abi.html#the-used-attribute

Copy link
Member Author

@webmaster128 webmaster128 Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not removed in the custom Wasm section in any case. I guess link_section ensures that. Other than that in the Wasm program it is not needed to have a static address. So I'm happy if it's optimized out.

}
};
}