Can not upgrade a canister's code

Steps:

  1. deploy examples/rust/basic_bitcoin at master · dfinity/examples · GitHub on ICP
  2. Add the following function in,
#[ic_cdk_macros::query]
fn greet(name: String) -> String {
    format!("Hello, {}!", name)
}
  1. run “dfx build”
  2. run “dfx canister install basic_bitcoin --network ic --mode upgrade”
  3. it always reports “Module hashxxx is already installed”.

My question is how to upgrade a canister code?

Because there is no changes, the IC tells you that no upgrade is required - “hash of the wasm is the same as the once already installed”. If you modify the code, build and deploy again, it should work out.

I add greet() function in to basic_bitcoin’s lib.rs, and build again. I think the wasm is changed.

Maybe it was not build? Either way, don’t really know another reason to “Module hashxxx is already installed” than trying to install the same version of the code.

After several times test, I believe the code is not rebuilt.
Another interesting thing, after rebuild

  1. if I call “dfx canister install basic_bitcoin --network ic --mode upgrade” it reports same code.
  2. if I call “dfx deploy basic_bitcoin --network ic”, it can upgrade the code on ICP.

What’s the difference between these 2 commands in upgrading canister?

1 Like
  • dfx install is installing a pre build wasm code.
  • dfx deploy is a command which includes a couple of commands (create canisters if needed, build the wasm and installs the wasm into the canister.

You can try dfx install —mode reinstall if you want reinstall your code completely.

vs

dfx build is shorthand for dfx build --network local. It does not affect the wasm built for --network ic. Therefore dfx canister install --network ic will not see any updates to the wasm

Either use dfx build --network ic (or shorthand dfx build --ic) before dfx canister install or use dfx deploy --ic, which will build and install automatically