ICP链motoko升级canister

当我们的canister比较多时,一个一个升级比较麻烦。
或者如果我们canister是由root canister创建,我们不是controller时。
以及我们需要分散权限,不想由某个人本地身份直接部署时。
可以考虑使用该方法进行版本管理通过motoko语言

我们需要 目标canister对应模块的最新wasm文件,并且canister创建时需要指定 调用升级api的模块为它的 controller.

首先需要将ic-manage 声明复制到本地
`https://github.com/johnxiaohe/ICP-Spark/blob/dev/src/spark_backend/management.mo`

然后在负责升级的模块 声明IC管理模块客户端 和 升级方法
```
actor{
    type Management = management.Management;
    let mng : Management = actor("aaaaa-aa");
    public shared func updatecodeWithoutArgs(wasm: [Nat8], cids:[Text]): async(Bool){
        for(cid in Iter.fromArray(cids)){
            await mng.install_code({
                arg = to_candid();
                wasm_module = wasm;
                mode = #upgrade;
                canister_id = Principal.fromText(cid);
            });
        };
        return true;
    };
    public shared func updatecodeWithArgs(wasm: [Nat8], cids:[Text]): async(Bool){
        for(cid in Iter.fromArray(cids)){
            await mng.install_code({
                arg = to_candid('arg1','arg2','arg3','arg4');
                wasm_module = wasm;
                mode = #upgrade;
                canister_id = Principal.fromText(cid);
            });
        };
        return true;
    };
}
```

wasm文件流可以通过js客户端获取wasm文件生成流,调用motoko canister来升级我们的 主canister 、 child canister

当我们的child canister有很多动态的init参数,这时更新代码需要同样的init参数作为arg,可以使用下面的方法将参数按照顺序传入生成blob,
```
to_candid('arg1','arg2','arg3','arg4')
```

因为child的init参数是动态的,不可预知。建议由child canister提供 argsblob 接口,升级时通过接口获取该canister的init args blob信息

感谢这些作者提供的思路
https://forum.dfinity.org/t/encode-principal-to-candid-arguments/10465
https://github.com/peterpeterparker/motoko_to_rust_migration/blob/1d51608bbea4f7257d6ed0916a5238d4427ec13b/src/motoko_to_rust_migration_backend/main.mo
https://forum.dfinity.org/t/need-help-implementing-install-code-canister-method/18313
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-15 17:46
暑期就挂了,秋招还有机会吗
大聪明777:研发提前批,14号刚开的,官网上面的配图上有写。提前批没过的话,秋招还可以投,不过前面的笔试/面试记录会被保留,供秋招参考
26届校招投递进展
点赞 评论 收藏
分享
求offer的大角牛:简历写的第一乱,没有突出重点,第二项目太多太杂看不出来有啥核心技术,第三自我评价太多了,第四获得的荣誉没啥含金量,可以不写,反正问题不少
点赞 评论 收藏
分享
机械打工仔:有说的你怀疑一下就行了,直接问也太实诚了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务