How do I upgrade child canisters that were dynamically created from a parent canister of which I am the controlller (In Motoko)?

Not sure but I would say probably not.

Did you select the main.mo or the Journal.mo wasm code to install the code (wasmModule)?

Good question. I wasn’t aware that the Journal.mo had its own wasm file. Where do i find it?

In the .dfx/ic/canisters Directory, i see three folders: one for my backend, one for my front end and one titled “idl”. The one titled is empty, and the the folder for my backend has only one wasm file. That’s the wasm file i used

also in .dfx in a folder next to your main.mo wasm code

however it might not always present and not always up-to-date with the last build depending of the settings. there is probably a nice way to generate it with a dfx command line but I am not big with the command line, so I generate it with a workaround.

  1. temporarly, in dfx.json add the Journal.mo
  2. dfx build or dfx deploy
  3. above command error will ends in error but it’s alright
  4. you have the wasm code

don’t forget to revert the change in dfx.json when finished.

with 1. I mean for example following. this is my original config

"canisters": {
    "manager": {
      "main": "canisters/src/manager/manager.mo",
      "type": "motoko"
    }
  }

then I add my child canister just to generate the wasm code

"canisters": {
    "manager": {
      "main": "canisters/src/manager/manager.mo",
      "type": "motoko"
    },
    "child": {
      "main": "canisters/src/child/child.mo",
      "type": "motoko"
    }
  }

again there is probably a way to do this by dfx but above generate the wasm code but also the did files what is often handy.

1 Like

Gonna give this a try now

@peterparker , you’re my hero. It worked! i can finally go to bed. its 3:30 AM where I am :smiling_face_with_tear:

2 Likes

awesome, happy to hear that :+1:

3:30 am, such a dedication! sleep well

1 Like

Just to clary, the wasm for an imported actor class is actually embedded in the wasm of the importing code.

Adding the additional compilation target to the dfx.json, (thus compiling the imported class on its own) has the side-effect of producing the wasm of the imported class as a separate, non-embedded thing.

As far as I know, that’s the only way to do this just now.

I’ve considered making the wasm of an imported class programmatically accessible as a blob somehow, or, alternatively, providing additional functions to perform manual installation/upgrade with all IC parameters exposed in the imported library (in addition to the class constructor), but they all seem like hacky solutions and are difficult to make type safe.

4 Likes

Thanks for the explanation Claudio :+1:

About the “hacky solutions”, no rush for me, I can live with my current “hacky” solution :wink:.

1 Like

Are there any breaking/differing behavior or “got-yas” once a canister is upgraded in the way @peterparker has outlined?

For example, if I would imagine that if I upgrade each of the canisters independently in this way, I would also need to redeploy the main canister as well so that it also has the new correct child canister wasm embedded in it in order to allow the main canister to continue dynamically spawning more child canisters.

Are there any additional side effects to keep in mind when updating settings, uninstalling, stopping, or deleting canisters through the main canister after such an upgrade of all the child canisters?

Hello Claudio!
I was blocked when installing wasm module.
If I install wasm file using ic-install_code interface, such as the following

let install_result = await IC_actor.install_code({
            arg = Blob.toArray(some_args));
            wasm_module = Blob.toArray(wasm);
            mode = #reinstall;
            canister_id = new_canister_id;
        });

This canister will trapped

trapped explicitly: Custom(Trailing value after finishing deserialization

when i call a function which inputs is an nested data.

Well,if i install this wasm and specific a candid file using dfx command with dfx.json config, it works fine.
What should i do when using ic-install_code interface?
Seems it’s a type problem.

Not sure, but can you show me:

  • the Candid or Motoko type of the argument to the installed code?
  • the way in which you obtain the blob for the arguments?
  • the dfx command that works?

Here I have collect something:

  1. the Candid or Motoko type of the argument to the installed code
    just normal init arguments, it can be null type. It does not affect the call operation.

  2. the way in which you obtain the blob for the arguments
    wasm load function with node js:

import {readFileSync} from 'fs';
​
const loadWasm = () => {
  const localPath = 
     `${process.cwd()}/.dfx/local/canisters/bucket/bucket.wasm`;
  const buffer = readFileSync(localPath);
  return [...new Uint8Array(buffer)];
};
  1. the dfx command that works
    The succeed command
dfx --identity NFT_trader_E canister call tlwi3-3aaaa-aaaaa-aaapq-cai mint '(principal "ome-principal",    7328:nat, vec { 
    record { "image"; variant { TextContent = "0.jpg" } }; 
    record { "tokenId"; variant { TextContent = "0" } }; 
    record { "name"; variant { TextContent = "Corn #0" } }; 
    record { "description"; variant { TextContent = "Corn #0" } } 
} )'

The failed command

dfx --identity NFT_trader_E canister call tlwi3-3aaaa-aaaaa-aaapq-cai mint '(principal "some-principal",    7326:nat, vec { 
    record { "image"; variant { TextContent = "0.jpg"}   };
    record { "tokenId"; variant { TextContent = "0"}   };
    record { "name"; variant { TextContent = "Corn #0"}   };
    record { "description"; variant { TextContent = "Corn #0"}   };
    record { "attributes"; 
        variant { 
            NestedContent = vec { 
                record { "Background"; variant { TextContent = "light"}   };
                record { "Name"; variant { TextContent = "Chon"}   } 
            } 
        } 
    } 
} )'
  1. The type about mint:
type GenericValue = variant {
  Nat64Content : nat64;
  Nat32Content : nat32;
  BoolContent : bool;
  Nat8Content : nat8;
  Int64Content : int64;
  IntContent : int;
  NatContent : nat;
  Nat16Content : nat16;
  Int32Content : int32;
  Int8Content : int8;
  FloatContent : float64;
  Int16Content : int16;
  BlobContent : vec nat8;
  NestedContent : Vec;
  Principal : principal;
  TextContent : text;
};

type Vec = vec record {
  text;
  variant {
    Nat64Content : nat64;
    Nat32Content : nat32;
    BoolContent : bool;
    Nat8Content : nat8;
    Int64Content : int64;
    IntContent : int;
    NatContent : nat;
    Nat16Content : nat16;
    Int32Content : int32;
    Int8Content : int8;
    FloatContent : float64;
    Int16Content : int16;
    BlobContent : vec nat8;
    NestedContent : Vec;
    Principal : principal;
    TextContent : text;
  };
};

mint : (principal, nat, vec record { text; GenericValue }) -> (Result);
  1. The error info
Error: Invalid vec record {text; variant {Nat64Content:nat64; Nat32Content:nat32; BoolContent:bool; Nat8Content:nat8; Int64Content:int64; IntContent:int; NatContent:nat; Nat16Content:nat16; Int32Content:int32; Int8Content:int8; FloatContent:float64; Int16Content:int16; BlobContent:vec nat8; NestedContent:μrec_2.vec record {_0_:text; _1_:variant {Nat64Content:nat64; Nat32Content:nat32; BoolContent:bool; Nat8Content:nat8; Int64Content:int64; IntContent:int; NatContent:nat; Nat16Content:nat16; Int32Content:int32; Int8Content:int8; FloatContent:float64; Int16Content:int16; BlobContent:vec nat8; NestedContent:rec_2; Principal:principal; TextContent:text}}; Principal:principal; TextContent:text}} argument: "vec { record { \"image\"; variant { TextContent = \"1.jpg\" } }; record { \"tokenId\"; variant { TextContent = \"1\" } }; record { \"name\"; variant { TextContent = \"Corn #1\" } }; record { \"description\"; variant { TextContent = \"Corn #1\" } }; }"

I can’t see the error, but spacing out your error message I see:

Error: Invalid
vec record {
    text;
    variant {
       Nat64Content:nat64;
       Nat32Content:nat32;
       BoolContent:bool;
       Nat8Content:nat8;
       Int64Content:int64;
       IntContent:int;
       NatContent:nat;
       Nat16Content:nat16;
       Int32Content:int32;
       Int8Content:int8;
       FloatContent:float64;
       Int16Content:int16;
       BlobContent:vec nat8;
       NestedContent:μrec_2.vec record {
         _0_: text;
         _1_: variant {
	     Nat64Content:nat64; Nat32Content:nat32;
	     BoolContent:bool; Nat8Content:nat8; Int64Content:int64;
	     IntContent:int; NatContent:nat; Nat16Content:nat16; Int32Content:int32; Int8Content:int8;
	     FloatContent:float64; Int16Content:int16; BlobContent:vec nat8; NestedContent:rec_2;
	     Principal:principal; TextContent:text}
       };
       Principal:principal;
       TextContent:text
   }
} argument:
"vec { record { \"image\"; variant { TextContent = \"1.jpg\" } }; 
       record { \"tokenId\"; variant { TextContent = \"1\" } }; 
       record { \"name\"; variant { TextContent = \"Corn #1\" } };
       record { \"description\"; variant { TextContent = \"Corn #1\" } }; 
}"

@chenyan can you see anything wrong with this? I rarely use candid values directly.

So the install_code call does succeed, and the problem is only when calling the mint method, right?

I think what’s happening is that dfx call with canister_id (instead of a project name specified in dfx.json) doesn’t fetch the candid file when making calls. Therefore, some of the type isn’t inferred properly. Can you try dfx canister call tlwi3-3aaaa-aaaaa-aaapq-cai mint '...' --candid your_candid_file.did and see if it works?

1 Like