前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >5.MOVE从入门到实战-发布新模块

5.MOVE从入门到实战-发布新模块

作者头像
Tiny熊
发布2022-11-07 11:11:55
3250
发布2022-11-07 11:11:55
举报
文章被收录于专栏:深入浅出区块链技术

本文作者:木头[1]

新建模块

代码语言:javascript
复制
// sources/Test.move
module 0x2::Test {
    use std::signer;

    struct Resource has key { i: u64 }

    public fun publish(account: &signer) {
        move_to(account, Resource { i: 10 })
    }

    public fun write(account: &signer, i: u64) acquires Resource {
        borrow_global_mut<Resource>(signer::address_of(account)).i = i;
    }

    public fun unpublish(account: &signer) acquires Resource {
        let Resource { i: _ } = move_from(signer::address_of(account));
  }
}

编译模块

编译并检查模块,但不会发布模块字节码

代码语言:javascript
复制
$ move build
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING my-move

编译并发布模块

在沙盒环境发布模块

代码语言:javascript
复制
$ move sandbox publish -v
Found 1 modules
Publishing a new module 00000000000000000000000000000002::Test (wrote 253 bytes)
Wrote 253 bytes of module ID's and code

查询发布的模块

代码语言:javascript
复制
$ ls storage/0x00000000000000000000000000000002/modules
Test.mv

我们还可以使用以下命令检查存储中已编译的字节码:move sandbox view

代码语言:javascript
复制
$ move sandbox view storage/0x00000000000000000000000000000002/modules/Test.mv
// Move bytecode v5
module 2.Test {
struct Resource has key {
        i: u64
}

public publish(Arg0: &signer) {
B0:
        0: MoveLoc[0](Arg0: &signer "0")
        1: LdU64(10)
        2: Pack[0](Resource "0")
        3: MoveTo[0](Resource "0")
        4: Ret
}
public unpublish(Arg0: &signer) {
B0:
        0: MoveLoc[0](Arg0: &signer "0")
        1: Call[3](address_of(&signer "3"): address)
        2: MoveFrom[0](Resource "0")
        3: Unpack[0](Resource "0")
        4: Pop
        5: Ret
}
public write(Arg0: &signer, Arg1: u64) {
B0:
        0: MoveLoc[1](Arg1: u64 "1")
        1: MoveLoc[0](Arg0: &signer "0")
        2: Call[3](address_of(&signer "3"): address)
        3: MutBorrowGlobal[0](Resource "0")
        4: MutBorrowField[0](Resource.i: u64 "0")
        5: WriteRef
        6: Ret
}
}

还可以在发布到之前通过运行来查看已编译的字节码,或者以交互方式检查字节码及其与 Move[2] 源代码的关系:storage move disassemble --name <module_name> move disassemble --name <module_name> --interactive

代码语言:javascript
复制
$ move disassemble --name Test --interactive # You can quit by pressing "q"
$ move disassemble --name Test
// Move bytecode v5
module 2.Test {
struct Resource has key {
        i: u64
}

public publish(account: &signer) {
B0:
        0: MoveLoc[0](account: &signer "0")
        1: LdU64(10)
        2: Pack[0](Resource "0")
        3: MoveTo[0](Resource "0")
        4: Ret
}
public unpublish(account: &signer) {
B0:
        0: MoveLoc[0](account: &signer "0")
        1: Call[3](address_of(&signer "3"): address)
        2: MoveFrom[0](Resource "0")
        3: Unpack[0](Resource "0")
        4: Pop
        5: Ret
}
public write(account: &signer, i: u64) {
B0:
        0: MoveLoc[1](i: u64 "1")
        1: MoveLoc[0](account: &signer "0")
        2: Call[3](address_of(&signer "3"): address)
        3: MutBorrowGlobal[0](Resource "0")
        4: MutBorrowField[0](Resource.i: u64 "0")
        5: WriteRef
        6: Ret
}
}

脚本调用模块

我们通过运行以下脚本来练习我们的新模块

代码语言:javascript
复制
// sources/test_script.move
script {
    use 0x2::Test;
    fun test_script(account: signer) {
        Test::publish(&account)
    }
}

此脚本调用我们模块的函数,该函数将在签名者的帐户下发布类型资源。让我们先看看如果不先提交这些更改,此脚本将更改什么。我们可以通过传递标志来做到这一点:--dry-run

代码语言:javascript
复制
$ move sandbox run sources/test_script.move --signers 0xf -v --dry-run
Changed resource(s) under 1 address(es):
  Changed 1 resource(s) under address 0000000000000000000000000000000F:
    Added type 0x2::Test::Resource: [10, 0, 0, 0, 0, 0, 0, 0] (wrote 40 bytes)
      key 0x2::Test::Resource {
          i: 10
      }
Wrote 40 bytes of resource ID's and data
Discarding changes; re-run without --dry-run if you would like to keep them.

一切看起来都很好,所以我们可以再次运行它,但这次通过删除标志来提交更改:--dry-run

代码语言:javascript
复制
move sandbox run sources/test_script.move --signers 0xf -v
Changed resource(s) under 1 address(es):
  Changed 1 resource(s) under address 0000000000000000000000000000000F:
    Added type 0x2::Test::Resource: [10, 0, 0, 0, 0, 0, 0, 0] (wrote 40 bytes)
      key 0x2::Test::Resource {
          i: 10
      }
Wrote 40 bytes of resource ID's and data

我们可以使用自更改提交以来检查新发布的资源:-v move sandbox view

代码语言:javascript
复制
$ move sandbox view storage/0x0000000000000000000000000000000F/resources/0x00000000000000000000000000000002::Test::Resource.bcs
key 0x2::Test::Resource {
    i: 10
}

清理沙盒环境数据

代码语言:javascript
复制
$ move sandbox clean
$ move sandbox view  storage/0x0000000000000000000000000000000F/resources/0x00000000000000000000000000000002::Test::Resource.bcs
Error: `move view <file>` must point to a valid file under storage

参考资料

[1]

木头: https://learnblockchain.cn/people/3015

[2]

Move: https://learnblockchain.cn/article/4700

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-10-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 深入浅出区块链技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 新建模块
    • 编译模块
      • 编译并发布模块
      • 脚本调用模块
        • 清理沙盒环境数据
          • 参考资料
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档