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

  • 木头
  • 更新于 2022-08-11 15:00
  • 阅读 3038

执行事务脚本时,通常需要调用不同的 Move 模块,之前的模块示例所示。可以将新模块添加到调用 CLI 的包中的目录中。您还可以添加对其他包的依赖项,以访问它们定义的模块(就像我们对之前的模块所做的那样)。在运行给定脚本之前,该命令将编译并发布包中的每个模块以及包的每个可传递依赖项。

新建模块

// 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));
  }
}

编译模块

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

$ move build
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING my-move

编译并发布模块

在沙盒环境发布模块

$ 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

查询发布的模块

$ ls storage/0x00000000000000000000000000000002/modules
Test.mv

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

$ 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)
        1: LdU64(10)
        2: Pack[0](Resource)
        3: MoveTo[0](Resource)
        4: Ret
}
public unpublish(Arg0: &signer) {
B0:
        0: MoveLoc[0](Arg0: &signer)
        1: Call[3](address_of(&signer): address)
        2: MoveFrom[0](Resource)
        3: Unpack[0](Resource)
        4: Pop
        5: Ret
}
public write(Arg0: &signer, Arg1: u64) {
B0:
        0: MoveLoc[1](Arg1: u64)
        1: MoveLoc[0](Arg0: &signer)
        2: Call[3](address_of(&signer): address)
        3: MutBorrowGlobal[0](Resource)
        4: MutBorrowField[0](Resource.i: u64)
        5: WriteRef
        6: Ret
}
}

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

$ 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)
        1: LdU64(10)
        2: Pack[0](Resource)
        3: MoveTo[0](Resource)
        4: Ret
}
public unpublish(account: &signer) {
B0:
        0: MoveLoc[0](account: &signer)
        1: Call[3](address_of(&signer): address)
        2: MoveFrom[0](Resource)
        3: Unpack[0](Resource)
        4: Pop
        5: Ret
}
public write(account: &signer, i: u64) {
B0:
        0: MoveLoc[1](i: u64)
        1: MoveLoc[0](account: &signer)
        2: Call[3](address_of(&signer): address)
        3: MutBorrowGlobal[0](Resource)
        4: MutBorrowField[0](Resource.i: u64)
        5: WriteRef
        6: Ret
}
}

脚本调用模块

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

// sources/test_script.move
script {
    use 0x2::Test;
    fun test_script(account: signer) {
        Test::publish(&account)
    }
}

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

$ 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

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

$ move sandbox view storage/0x0000000000000000000000000000000F/resources/0x00000000000000000000000000000002::Test::Resource.bcs
key 0x2::Test::Resource {
    i: 10
}

清理沙盒环境数据

$ 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
点赞 2
收藏 3
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

3 条评论

请先 登录 后评论
木头
木头
0xC020...10cf
江湖只有他的大名,没有他的介绍。