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

cosmwasm_std::entry_point parses data for disabled feature #2271

Open
Buckram123 opened this issue Sep 25, 2024 · 4 comments
Open

cosmwasm_std::entry_point parses data for disabled feature #2271

Buckram123 opened this issue Sep 25, 2024 · 4 comments
Labels
wontfix This will not be worked on

Comments

@Buckram123
Copy link
Contributor

Buckram123 commented Sep 25, 2024

cosmwasm_std::entry_point forward all arguments to the export function from the entry point function, even if this argument should not be included in this feature.
I think it's best to see as minimal example

pub struct InstantiateMsg<N> {
    pub number: N,
}

#[cosmwasm_std::entry_point]
pub fn instantiate(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    #[cfg(feature = "u32")] msg: InstantiateMsg<u32>,
    #[cfg(not(feature = "u32"))] msg: InstantiateMsg<u64>,
) -> Result<Response, Never> {
    Ok(Response::new())
}

Compiling it with "u32" or without "u32" feature enabled will error with:

error[E0061]: this function takes 4 arguments but 5 arguments were supplied
   --> contracts/cw-blob/src/lib.rs:8:1
    |
8   | #[cosmwasm_std::entry_point]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected argument of type `u32`
    |
note: function defined here
   --> /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/cosmwasm-std-2.1.3/src/exports.rs:115:8
    |
115 | pub fn do_instantiate<Q, M, C, E>(
    |        ^^^^^^^^^^^^^^
    = note: this error originates in the attribute macro `cosmwasm_std::entry_point` (in Nightly builds, run with -Z macro-backtrace for more info)
@Buckram123
Copy link
Contributor Author

Buckram123 commented Sep 25, 2024

Working workaround is making wrapper around instantiate function and having both instantiate functions included

Based on the example from the issue description:

#[cfg(feature = "u32")]
#[cosmwasm_std::entry_point]
pub fn instantiate(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: InstantiateMsg<u32>,
) -> Result<Response, Never> {
    _instantiate(deps, env, info, msg)
}

#[cfg(not(feature = "u32"))]
#[cosmwasm_std::entry_point]
pub fn instantiate(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: InstantiateMsg<u64>,
) -> Result<Response, Never> {
    _instantiate(deps, env, info, msg)
}

pub fn _instantiate(
    _deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    #[cfg(feature = "u32")] _msg: InstantiateMsg<u32>,
    #[cfg(not(feature = "u32"))] _msg: InstantiateMsg<u64>,
) -> Result<Response, Never> {
    Ok(Response::new())
}

@chipshort
Copy link
Collaborator

You have a point here. This is a result of the way the entry_point macro is implemented.
The problem is that we go through all the parameters of the entrypoint function and create a pointer for each one that is passed to the actual entrypoint function. The problem is that the conditional compilation happens after the macro runs and the macro ignores all attributes. With your example, it basically generates code like this:

mod __wasm_export_instantiate {
    #[no_mangle]
    extern "C" fn instantiate(env_ptr: u32, info_ptr: u32, msg_ptr: u32, msg_ptr2: u32) -> u32 {
        cosmwasm_std::do_instantiate(&super::instantiate, env_ptr, info_ptr, msg_ptr, msg_ptr2)
    }
}

In this specific case, the only fix I can think of would be to keep the cfg attribute on the ptr parameter and argument, but I don't know if that's fine for all attributes.

@Buckram123
Copy link
Contributor Author

Buckram123 commented Sep 27, 2024

You have a point here. This is a result of the way the entry_point macro is implemented. The problem is that we go through all the parameters of the entrypoint function and create a pointer for each one that is passed to the actual entrypoint function. The problem is that the conditional compilation happens after the macro runs and the macro ignores all attributes. With your example, it basically generates code like this:

mod __wasm_export_instantiate {
    #[no_mangle]
    extern "C" fn instantiate(env_ptr: u32, info_ptr: u32, msg_ptr: u32, msg_ptr2: u32) -> u32 {
        cosmwasm_std::do_instantiate(&super::instantiate, env_ptr, info_ptr, msg_ptr, msg_ptr2)
    }
}

In this specific case, the only fix I can think of would be to keep the cfg attribute on the ptr parameter and argument, but I don't know if that's fine for all attributes.

Won't it be better then to just ignore how many arguments entry point have and just generate always the same extern function? So instead of counting arguments, instantiate will have always 3 pointers

@chipshort
Copy link
Collaborator

That already doesn't work as a full solution because we just introduced an additional version of migrate with 3 arguments (instead of the 2 it had until now).

@chipshort chipshort added the wontfix This will not be worked on label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants