使用 Aptos CLI

aptos 工具是一个命令行界面(CLI),用于在 Aptos 区块链上进行开发,调试和节点操作。本文档介绍了如何使用 Aptos CLI 工具。要安装 CLI,请参阅安装 Aptos CLI

命令行帮助

命令行帮助可用。键入 aptos helpaptos --help 以查看可用的命令选项。请参阅下面 aptos --help 的用法输出:

USAGE:
    aptos <SUBCOMMAND>

OPTIONS:
    -h, --help       Print help information
    -V, --version    Print version information

SUBCOMMANDS:
    account       Toolfor interacting with accounts
    config        Toolfor interacting with configuration of the Aptos CLI tool
    genesis       Toolfor setting up an Aptos chain Genesis transaction
    governance    Toolfor on-chain governance
    help          Print this message or the help of the given subcommand(s)
    info          Show build information about the CLI
    init          Tool to initialize current directoryfor the aptos tool
    key           Toolfor generating, inspecting, and interacting with keys
    move          Toolfor Move related operations
    node          Toolfor operations related to nodes
    stake         Toolfor manipulating stake

特定于命令的帮助

还提供了特定于命令的帮助。例如,请参阅下面的 aptos move --help 的用法输出:

USAGE:
    aptos move <SUBCOMMAND>

OPTIONS:
    -h, --help       Print help information
    -V, --version    Print version information

SUBCOMMANDS:
    clean
            Cleans derived artifacts of a package
    compile
            Compiles a package and returns the [`ModuleId`]s
    download
            Downloads a package and stores it ina directory named after the package
    help
            Print this message or the help of the given subcommand(s)
    init
            Creates a new Move package at the given location
    list
            Lists information about packages and modules on-chain
    prove
            Proves the Move package
    publish
            Publishes the modules ina Move package to the Aptos blockchain
    run
            Run a Move function    test
            Runs Move unit tests fora package
    transactional-test
            Run Move transactional tests

子命令帮助

还提供了子命令的帮助。例如,请参阅下面的 aptos move compile --help的用法输出:

USAGE:
    aptos move compile [OPTIONS]

OPTIONS:
    -h, --help
            Print help information

        --named-addresses <NAMED_ADDRESSES>
            Named addresses forthe move binary

            Example: alice=0x1234, bob=0x5678

            Note: This will fail ifthere are duplicates inthe Move.toml file remove those first.

            [default: ]

        --output-dir <OUTPUT_DIR>
            Path to save the compiled move package

            Defaults to `<package_dir>/build`

        --package-dir <PACKAGE_DIR>
            Path to a move package (the folder with a Move.toml file)

    -V, --version
            Print version information

CLI 信息

运行 aptos info命令以获取 CLI 信息以进行调试。请参阅 aptos info命令的示例输出:

{
  "Result": {
    "build_branch": "testnet",
    "build_cargo_version": "cargo 1.62.1 (a748cf5a3 2022-06-08)",
    "build_commit_hash": "f8bf8fdeec33c8c6ff3d1cbaf4990b9e54c2176a",
    "build_os": "macos-x86_64",
    "build_pkg_version": "0.3.2",
    "build_rust_channel": "1.62.1-x86_64-apple-darwin",
    "build_rust_version": "rustc 1.62.1 (e092d0b6b 2022-07-16)",
    "build_tag": "",
    "build_time": "2022-08-26 22:27:31 +00:00"
  }
}

配置示例

CLI 的配置的工作方式如下:

在本地运行的当前工作目录中

  1. 您的配置位于本地 YAML 配置文件 .aptos/config.yaml中,即位于运行 CLI 的当前工作目录中。在这种情况下,您必须从当前工作目录运行 CLI 命令,才能使用此配置。

  2. 您可以通过以下命令验证 CLI 是否设置为使用此本地配置 YAML 文件:

    aptos config show-global-config

    您应该看到以下输出:

    {
      "Result": {
        "config_type": "Workspace"
      }
    }
  3. config_typeWorkspace值指示 .aptos/config.yaml文件用于 CLI 配置。

在全局运行的主目录中

  1. 您的配置位于全局 YAML 配置文件 ~/.aptos/global_config.yaml 中,即位于您的主目录中。

  2. 通过运行以下命令将 CLI 设置为使用此全局配置 YAML 文件:

    aptos config set-global-config --config-type global
  3. You will see the below output:

  4. 你将看到如下输出:

    {
      "Result": {
        "config_type": "Global"
      }
    }
  5. 您也可以使用 show-global-config 命令显示全局配置。

$ aptos config show-global-config
{
  "Result": {
    "config_type": "Global"
  }
}

默认配置

如果您没有设置任何全局配置,则使用当前工作目录中的 ./.aptos/config.yaml 进行配置。

设置 shell 补全

您可以使用 generate-shell-completions 命令设置 shell 完成。 您可以查找特定 shell 的配置。 支持的 shell 是 [bash、zsh、fish、powershell、elvish]。 下面是 oh my zsh 的示例。

aptos config generate-shell-completions --shell zsh --output-file ~/.oh-my-zsh/completions/_aptos

初始化本地配置并创建账户

将使用配置 config.yaml 创建一个名为 .aptos/ 的本地文件夹,该配置可用于在 CLI 运行之间存储配置。 这是您运行的本地文件,因此您需要继续从该文件夹运行 CLI,或在另一个文件夹中重新初始化。

第 1 步:运行 Aptos 初始化

aptos init 命令将使用您提供的私钥初始化配置。

$ aptos init
Configuringfor profile default
Enter your rest endpoint [Current: None | No input: <https://fullnode.devnet.aptoslabs.com>]

No rest url given, using <https://fullnode.devnet.aptoslabs.com>...
Enter your faucet endpoint [Current: None | No input: <https://faucet.devnet.aptoslabs.com>]

No faucet url given, using <https://faucet.devnet.aptoslabs.com>...
Enter your private key as a hex literal (0x...) [Current: None | No input: Generate new key (or keep oneif present)]

No key given, generating key...
Account 00f1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696 doesn't exist, creating it and funding it with 10000 coins
Aptos is now set upfor account 00f1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696!  Run`aptos help`for more information about commands

{
  "Result": "Success"
}

第 2 步:更改配置

要更改配置,您可以运行命令 aptos init 或手动编辑当前工作目录中的 .aptos/config.yaml

创建其他配置文件

您还可以为不同的端点和不同的密钥创建其他配置文件。 这些可以通过添加 --profile 参数来实现,并且可以在大多数其他命令中用于替换命令行参数。

$ aptos init --profile superuser
Configuringfor profile superuser
Enter your rest endpoint [Current: None | No input: <https://fullnode.devnet.aptoslabs.com>]

No rest url given, using <https://fullnode.devnet.aptoslabs.com>...
Enter your faucet endpoint [Current: None | No input: <https://faucet.devnet.aptoslabs.com>]

No faucet url given, using <https://faucet.devnet.aptoslabs.com>...
Enter your private key as a hex literal (0x...) [Current: None | No input: Generate new key (or keep oneif present)]

No key given, generating key...
Account 18B61497FD290B02BB0751F44381CADA1657C2B3AA6194A00D9BC9A85FAD3B04 doesn't exist, creating it and funding it with 10000 coins
Aptos is now set upfor account 18B61497FD290B02BB0751F44381CADA1657C2B3AA6194A00D9BC9A85FAD3B04!  Run`aptos help`for more information about commands
{
  "Result": "Success"
}

帐户示例

用水龙头为账户注资

您可以通过 CLI 使用帐户地址或 default(默认为使用 aptos init 创建的账户地址)使用水龙头为账户注资。

例如,要为上面使用 aptos init 命令创建的帐户 00f1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696 提供资金:

$ aptos account fund-with-faucet --account 00f1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696
{
  "Result": "Added 10000 coins to account 00f1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696"
}
$ aptos account fund-with-faucet --account default
{
  "Result": "Added 10000 coins to account 00f1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696"
}

查看账户的余额和转账事件

您可以通过明确指定账户地址来查看余额和转账事件(存款和取款),如下所示:

$ aptos account list --query balance --account 00f1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696

或通过如下指定默认值:

$ aptos account list --query balance --account default

上述两个命令都将在您的终端上生成以下信息:

{
  "Result": [
    {
      "coin": {
        "value": "110000"
      },
      "deposit_events": {
        "counter": "3",
        "guid": {
          "id": {
            "addr": "0xf1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696",
            "creation_num": "2"
          }
        }
      },
      "frozen": false,
      "withdraw_events": {
        "counter": "0",
        "guid": {
          "id": {
            "addr": "0xf1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696",
            "creation_num": "3"
          }
        }
      }
    }
  ]
}

列出帐户中的资源

您可以从命令行列出帐户中的资源。 例如,请参阅下文,了解如何列出您刚刚在上面创建的帐户中的资源:

$ aptos account list --query resources --account default

或者

$ aptos account list --query resources --account 0xf1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696

上述两个命令都会在您的终端上生成以下资源列表信息:

{
  "Result": [
    {
      "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>": {
        "coin": {
          "value": "110000"
        },
        "deposit_events": {
          "counter": "3",
          "guid": {
            "id": {
              "addr": "0xf1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696",
              "creation_num": "2"
            }
          }
        },
        "frozen": false,
        "withdraw_events": {
          "counter": "0",
          "guid": {
            "id": {
              "addr": "0xf1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696",
              "creation_num": "3"
            }
          }
        }
      }
    },
    {
      "0x1::account::Account": {
        "authentication_key": "0x00f1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696",
        "coin_register_events": {
          "counter": "1",
          "guid": {
            "id": {
              "addr": "0xf1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696",
              "creation_num": "0"
            }
          }
        },
        "guid_creation_num": "4",
        "key_rotation_events": {
          "counter": "0",
          "guid": {
            "id": {
              "addr": "0xf1f20ddd0b0dd2291b6e42c97274668c479bca70f07c6b6a80b99720779696",
              "creation_num": "1"
            }
          }
        },
        "rotation_capability_offer": {
          "for": {
            "vec": []
          }
        },
        "sequence_number": "0",
        "signer_capability_offer": {
          "for": {
            "vec": []
          }
        }
      }
    }
  ]
}

列出默认配置文件

您还可以从配置中列出未指定帐户的默认配置文件。

<aside> 💡 提示

在本节中,帐户地址可能因示例而异。

</aside>

$ aptos account list
{
  "Result": [
    {
      "coin": {
        "value": "10000"
      },
      "deposit_events": {
        "counter": "1",
        "guid": {
          "id": {
            "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
            "creation_num": "1"
          }
        }
      },
      "withdraw_events": {
        "counter": "0",
        "guid": {
          "id": {
            "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
            "creation_num": "2"
          }
        }
      }
    },
    {
      "register_events": {
        "counter": "1",
        "guid": {
          "id": {
            "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
            "creation_num": "0"
          }
        }
      }
    },
    {
      "counter": "3"
    },
    {
      "authentication_key": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
      "self_address": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
      "sequence_number": "0"
    }
  ]
}

使用配置文件的名称

此外,任何需要帐户的地方都可以使用个人资料的名称:

$ aptos account list --query resources --account superuser
{
  "Result": [
    {
      "coin": {
        "value": "10000"
      },
      "deposit_events": {
        "counter": "1",
        "guid": {
          "id": {
            "addr": "0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc",
            "creation_num": "1"
          }
        }
      },
      "withdraw_events": {
        "counter": "0",
        "guid": {
          "id": {
            "addr": "0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc",
            "creation_num": "2"
          }
        }
      }
    },
    {
      "register_events": {
        "counter": "1",
        "guid": {
          "id": {
            "addr": "0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc",
            "creation_num": "0"
          }
        }
      }
    },
    {
      "counter": "3"
    },
    {
      "authentication_key": "0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc",
      "self_address": "0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc",
      "sequence_number": "0"
    }
  ]
}

列出帐户中的模块

您可以通过不同类型的查询来查看帐户下的不同项目。 目前,支持“资源”和“模块”,但即将推出更多查询类型。 例如,要获取模块:

$ aptos account list --query modules
{
  "Result": [
    {
      "bytecode": "0xa11ceb0b050000000b01000a020a12031c2504410405452d0772da0108cc0240068c030a0a9603150cab03650d90040400000101010201030104000506000006080004070700020e0401060100080001000009020300010f0404000410060100031107000002120709010602130a030106050806080105010802020c0a02000103040508020802070801010a0201060c010800010b0301090002070b030109000900074d657373616765056572726f72056576656e74067369676e657206737472696e67124d6573736167654368616e67654576656e740d4d657373616765486f6c64657206537472696e670b6765745f6d6573736167650b7365745f6d6573736167650c66726f6d5f6d6573736167650a746f5f6d657373616765076d657373616765156d6573736167655f6368616e67655f6576656e74730b4576656e7448616e646c65096e6f745f666f756e6404757466380a616464726573735f6f66106e65775f6576656e745f68616e646c650a656d69745f6576656e74b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb0000000000000000000000000000000000000000000000000000000000000001030800000000000000000002020a08020b08020102020c08020d0b030108000001000101030b0a002901030607001102270b002b0110001402010104010105240b0111030c040e0011040c020a02290120030b05120e000b040e00380012012d0105230b022a010c050a051000140c030a050f010b030a04120038010b040b050f0015020100010100",
      "abi": {
        "address": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "name": "Message",
        "friends": [],
        "exposed_functions": [
          {
            "name": "get_message",
            "visibility": "public",
            "is_entry": false,
            "generic_type_params": [],
            "params": [
              "address"
            ],
            "return": [
              "0x1::string::String"
            ]
          },
          {
            "name": "set_message",
            "visibility": "public",
            "is_entry": true,
            "generic_type_params": [],
            "params": [
              "signer",
              "vector<u8>"
            ],
            "return": []
          }
        ],
        "structs": [
          {
            "name": "MessageChangeEvent",
            "is_native": false,
            "abilities": [
              "drop",
              "store"
            ],
            "generic_type_params": [],
            "fields": [
              {
                "name": "from_message",
                "type": "0x1::string::String"
              },
              {
                "name": "to_message",
                "type": "0x1::string::String"
              }
            ]
          },
          {
            "name": "MessageHolder",
            "is_native": false,
            "abilities": [
              "key"
            ],
            "generic_type_params": [],
            "fields": [
              {
                "name": "message",
                "type": "0x1::string::String"
              },
              {
                "name": "message_change_events",
                "type": "0x1::event::EventHandle<0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb::Message::MessageChangeEvent>"
              }
            ]
          }
        ]
      }
    }
  ]
}

转币

Aptos CLI 也是一个简单的钱包,可以在帐户之间转移代币。

$ aptos account transfer --account superuser --amount 100
{
  "Result": {
    "gas_used": 73,
    "balance_changes": {
      "742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc": {
        "coin": {
          "value": "10100"
        },
        "deposit_events": {
          "counter": "2",
          "guid": {
            "id": {
              "addr": "0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc",
              "creation_num": "1"
            }
          }
        },
        "withdraw_events": {
          "counter": "0",
          "guid": {
            "id": {
              "addr": "0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc",
              "creation_num": "2"
            }
          }
        }
      },
      "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb": {
        "coin": {
          "value": "9827"
        },
        "deposit_events": {
          "counter": "1",
          "guid": {
            "id": {
              "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
              "creation_num": "1"
            }
          }
        },
        "withdraw_events": {
          "counter": "1",
          "guid": {
            "id": {
              "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
              "creation_num": "2"
            }
          }
        }
      }
    },
    "sender": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
    "success": true,
    "version": 1139,
    "vm_status": "Executed successfully"
  }
}

关键示例

生成密钥

要允许生成私钥,您可以使用 aptos key generate command。 您可以生成 x25519ed25519 密钥。

$ aptos key generate --key-type ed25519 --output-file output.key
{
  "Result": {
    "PrivateKey Path": "output.key",
    "PublicKey Path": "output.key.pub"
  }
}

生成对等配置

要允许其他人连接到您的节点,您需要生成对等配置。 下面的命令显示了如何使用 aptos CLI 生成对等配置并将其写入名为 peer_config.yaml 的文件中。

$ aptos key extract-peer --output-file peer_config.yaml

上述命令将生成以下终端输出:

{
  "Result": {
    "8cfb85603080b13013b57e2e80887c695cfecd7ad8217d1cac22fa6f3b0b5752": {
      "addresses": [],
      "keys": [
        "0x8cfb85603080b13013b57e2e80887c695cfecd7ad8217d1cac22fa6f3b0b5752"
      ],
      "role": "Upstream"
    }
  }
}

peer_config.yaml 文件将在您当前的工作目录中创建,其内容如下例所示:

---
8cfb85603080b13013b57e2e80887c695cfecd7ad8217d1cac22fa6f3b0b5752:
  addresses: []
  keys:
    - "0x8cfb85603080b13013b57e2e80887c695cfecd7ad8217d1cac22fa6f3b0b5752"
  role: Upstream

**注意:**在地址键中,您应该填写您的地址。

Move 示例

编译 Move

aptos CLI 可用于在本地编译 Move 包。 下面的示例使用 HelloBlockchainin Move 示例

命名地址可以是帐户地址或配置文件名称。

$ aptos move compile --package-dir aptos-move/move-examples/hello_blockchain/ --named-addresseshello_blockchain=superuser

上述命令将生成以下终端输出:

{
  "Result": [
    "742854F7DCA56EA6309B51E8CEBB830B12623F9C9D76C72C3242E4CAD353DEDC::Message"
  ]
}

编译和单元测试 Move

aptos CLI 也可用于在本地编译和运行单元测试。 在本例中,我们将在 move-examples 中使用 HelloBlockchain

$ aptos move test --package-dir aptos-move/move-examples/hello_blockchain/ --named-addresseshello_blockchain=superuser

上述命令将生成以下终端输出:

INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING Examples
Running Move unit tests
[ PASS    ] 0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc::MessageTests::sender_can_set_message
[ PASS    ] 0x742854f7dca56ea6309b51e8cebb830b12623f9c9d76c72c3242e4cad353dedc::Message::sender_can_set_message
Test result: OK. Total tests: 2; passed: 2; failed: 0
{
  "Result": "Success"
}

证明 Move

aptos CLI 可用于运行 Move Prover,它是 Move 语言的正式验证工具。 下面的例子证明了 move-examples 中的 hello_prover 包。

aptos move prove --package-dir aptos-move/move-examples/hello_prover/

上述命令将生成以下终端输出:

SUCCESS proving 1 modules from package`hello_prover`in 1.649s
{
  "Result": "Success"
}

如果未正确安装和设置依赖项,Move Prover 可能会失败并显示以下终端输出:

FAILURE proving 1 modules from package`hello_prover`in 0.067s
{
  "Error": "Move Prover failed: No boogie executable set.  Please set BOOGIE_EXE"
}

在这种情况下,请参阅安装 Move Prover 的依赖项

Debug 和打印堆栈跟踪

在本例中,我们将在 debug-move-example 中使用 DebugDemo

首先,您需要在 Move toml 文件中包含 Move Nursery。

现在,您可以在 DebugDemo Move 文件中使用 Debug::print 和 Debug::print_stack_trace

您可以运行以下命令:

$ aptos move test --package-dir crates/aptos/debug-move-example

该命令将生成以下输出:

Running Move unit tests
[debug] 0000000000000000000000000000000000000000000000000000000000000001
Call Stack:
    [0] 0000000000000000000000000000000000000000000000000000000000000001::Message::sender_can_set_message

        Code:
            [4] CallGeneric(0)
            [5] MoveLoc(0)
            [6] LdConst(0)
          > [7] Call(1)
            [8] Ret

        Locals:
            [0] -
            [1] 0000000000000000000000000000000000000000000000000000000000000001

Operand Stack:

发布具有命名地址的 Move 包

在本例中,我们将在 move-examples 中使用 HelloBlockchain

使用您为 HelloBlockchain 设置的帐户地址发布包。

在这里,您需要将 8946741e5c907c43c9e042b3739993f32904723f8e2d1491564d38959b59ac71 更改为您的帐户地址。

$ aptos move publish --package-dir aptos-move/move-examples/hello_blockchain/ --named-addressesHelloBlockchain=8946741e5c907c43c9e042b3739993f32904723f8e2d1491564d38959b59ac71

您还可以对地址使用命名配置文件。 第一个占位符是 default

$ aptos move publish --package-dir aptos-move/move-examples/hello_blockchain/ --named-addressesHelloBlockchain=default

<aside> 💡 提示

发布 Move 模块时,如果多个模块在一个包中,则该包中的所有模块必须具有相同的帐户。 如果他们有不同的帐户,那么发布将在交易级别失败。

</aside>

运行 Move 函数

现在您已经发布了上面的函数,您可以运行它了。

必须给参数一个类型,用冒号分隔它。 在此示例中,我们希望将输入解析为字符串,因此我们输入 string:Hello!

$ aptos move run --function-id 0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb::message::set_message --args string:hello!
{
  "Result": {
    "changes": [
      {
        "address": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "data": {
          "authentication_key": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
          "self_address": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
          "sequence_number": "3"
        },
        "event": "write_resource",
        "resource": "0x1::account::Account"
      },
      {
        "address": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "data": {
          "coin": {
            "value": "9777"
          },
          "deposit_events": {
            "counter": "1",
            "guid": {
              "id": {
                "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
                "creation_num": "1"
              }
            }
          },
          "withdraw_events": {
            "counter": "1",
            "guid": {
              "id": {
                "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
                "creation_num": "2"
              }
            }
          }
        },
        "event": "write_resource",
        "resource": "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>"
      },
      {
        "address": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "data": {
          "counter": "4"
        },
        "event": "write_resource",
        "resource": "0x1::guid::Generator"
      },
      {
        "address": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "data": {
          "message": "hello!",
          "message_change_events": {
            "counter": "0",
            "guid": {
              "id": {
                "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
                "creation_num": "3"
              }
            }
          }
        },
        "event": "write_resource",
        "resource": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb::Message::MessageHolder"
      }
    ],
    "gas_used": 41,
    "success": true,
    "version": 3488,
    "vm_status": "Executed successfully"
  }
}

此外,配置文件可以替换函数 ID 中的地址。

$ aptos move run --function-id default::message::set_message --args string:hello!
{
  "Result": {
    "changes": [
      {
        "address": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "data": {
          "authentication_key": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
          "self_address": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
          "sequence_number": "3"
        },
        "event": "write_resource",
        "resource": "0x1::account::Account"
      },
      {
        "address": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "data": {
          "coin": {
            "value": "9777"
          },
          "deposit_events": {
            "counter": "1",
            "guid": {
              "id": {
                "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
                "creation_num": "1"
              }
            }
          },
          "withdraw_events": {
            "counter": "1",
            "guid": {
              "id": {
                "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
                "creation_num": "2"
              }
            }
          }
        },
        "event": "write_resource",
        "resource": "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>"
      },
      {
        "address": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "data": {
          "counter": "4"
        },
        "event": "write_resource",
        "resource": "0x1::guid::Generator"
      },
      {
        "address": "b9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
        "data": {
          "message": "hello!",
          "message_change_events": {
            "counter": "0",
            "guid": {
              "id": {
                "addr": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb",
                "creation_num": "3"
              }
            }
          }
        },
        "event": "write_resource",
        "resource": "0xb9bd2cfa58ca29bce1d7add25fce5c62220604cd0236fe3f90d9de91ed9fb8cb::Message::MessageHolder"
      }
    ],
    "gas_used": 41,
    "success": true,
    "version": 3488,
    "vm_status": "Executed successfully"
  }
}

节点命令示例

运行本地测试网

您可以从 aptos CLI 运行本地测试网,这将与构建它的版本相匹配。 此外,它可以与本地单节点测试网并排运行水龙头。

$ aptos node run-local-testnet --with-faucet
Completed generating configuration:
        Log file: "/Users/greg/.aptos/testnet/validator.log"
        Test dir: "/Users/greg/.aptos/testnet"
        Aptos root key path: "/Users/greg/.aptos/testnet/mint.key"
        Waypoint: 0:d302c6b10e0fa68bfec9cdb383f24ef1189d8850d50b832365eea21ae52d8101
        ChainId: TESTING
        REST API endpoint: 0.0.0.0:8080
        Fullnode network: /ip4/0.0.0.0/tcp/6181

Aptos is running, press ctrl-c to exit

如果节点关闭,这将具有一致的状态,它将从以前的状态开始。 如果你想从创世重启链,你可以添加 --force-restart 标志。

$ aptos node run-local-testnet --with-faucet --force-restart
Are you sure you want to delete the existing chain? [yes/no] >
yes
Completed generating configuration:
        Log file: "/Users/greg/.aptos/testnet/validator.log"
        Test dir: "/Users/greg/.aptos/testnet"
        Aptos root key path: "/Users/greg/.aptos/testnet/mint.key"
        Waypoint: 0:649efc34c813d0db8db6fa5b1ffc9cc62f726bb5168e7f4b8730bb155d6213ea
        ChainId: TESTING
        REST API endpoint: 0.0.0.0:8080
        Fullnode network: /ip4/0.0.0.0/tcp/6181

Aptos is running, press ctrl-c to exit

创世仪式

aptos 工具支持通过所谓的创世仪式引导新的区块链。 创世仪式的输出是为在线操作准备区块链的移动指令的输出。 输入包括:

  • 一组验证器及其配置

  • 最初的 Move 模块集,称为框架

  • 一个独特的 ChainId (u8),将其与其他部署区分开来

  • 对于测试链,还有一个管理 AptosCoin 铸币的账户

产生创世

  • 创世组织者构建一个 Layout 并分发它。

  • 创世组织者准备 Aptos 框架的字节码并分发它。

  • 每个参与者生成他们的 ValidatorConfiguration 并分发它。

  • 每个参与者从产生的贡献中生成一个 genesis.blob

  • 创世组织者执行 genesis.blob 以导出初始航路点并分发它。

  • 每个参与者都开始他们的 aptos-nodeaptos-node 在启动时验证 genesis.blob 与 创世组织者提供的航点。

  • 区块链将在达到法定人数后开始达成共识。

准备 aptos-core

以下指南假定您有权访问 Aptos-core 存储库或相关工具。 您可以从 GitHub 下载并准备 Aptos-core:

git clone <https://github.com/aptos-labs/aptos-core.git>
cd aptos-core
git checkout --track origin/testnet
./scripts/dev_setup.sh
source ~/.cargo/env

layout 文件

该文件包含了:

  • root_key:用于 AptosCoin 管理的 Ed25519 公钥。

  • users:参与者的集合

  • chain_idChainId 或唯一的整数,用于区分此部署与其他 Aptos 网络

示例:

root_key: "0xca3579457555c80fc7bb39964eb298c414fd60f81a2f8eedb0244ec07a26e575"
users:
  - alice
  - bob
chain_id: 8

构建 Aptos 框架

从您的 Aptos-core 存储库中,构建框架并将其打包:

cargo run --package framework
mkdir aptos-framework-release
cp aptos-framework/releases/artifacts/current/build/**/bytecode_modules/* aptos-framework-release

该框架将存储在 aptos-framework-release 目录中。

ValidatorConfiguration 文件

ValidatorConfiguration 文件包含:

  • account_address:管理此验证器的帐户。这必须从 ValidatorConfiguration 文件中提供的 account_key 派生。

  • consensus_key:用于验证来自验证者的共识消息的公钥

  • account_key:管理此验证器的帐户的公钥。这用于派生 account_address

  • network_key:验证者和全节点网络认证和加密的公钥。

  • validator_host:验证者所在的网络地址。这包含一个主机和端口字段。主机应该是 DNS 名称或 IP 地址。目前仅支持 IPv4。

  • full_node_host:全节点所在的可选网络地址。这包含一个主机和端口字段。主机应该是 DNS 名称或 IP 地址。目前仅支持 IPv4。

  • stake_amount:该节点质押的硬币数量。预计为 1,如果不同,则配置将被视为无效。

示例:

account_address: ccd49f3ea764365ac21e99f029ca63a9b0fbfab1c8d8d5482900e4fa32c5448a
consensus_key: "0xa05b8f41057ac72f9ca99f5e3b1b787930f03ba5e448661f2a1fac98371775ee"
account_key: "0x3d15ab64c8b14c9aab95287fd0eb894aad0b4bd929a5581bcc8225b5688f053b"
network_key: "0x43ce1a4ac031b98bb1ee4a5cd72a4cca0fd72933d64b22cef4f1a61895c2e544"
validator_host:
  host: bobs_host
  port: 6180
full_node_host:
  host: bobs_host
  port: 6182
stake_amount: 1

使用 aptos CLI来生成:

  1. 生成验证者密钥:

    cargo run --package aptos -- genesis generate-keys --output-dir bobs
  2. 生成ValidatorConfiguration

    cargo run --package aptos -- \\\\
        genesis set-validator-configuration \\\\
        --keys-dir bobs \\\\
        --username bob \\\\
        --validator-host bobs_host:6180 \\\\
        --full-node-host bobs_host:6180 \\\\
        --local-repository-dir .
  3. 最后一条命令将生成一个 bob.yaml 文件,该文件应分发给其他参与者以生成 genesis.blob

创建一个 genesis 和路标

在获得 Layout 文件、各个 ValidatorConfiguration 文件和框架发布后,可以生成 genesis.blob 和路标。 验证早期提供的 ValidatorConfiguration 是否与生成 genesis.blob 的发行版中的相同,这一点很重要。 如果有不匹配,通知所有参与者。

生成 genesis.blob 和路标

  • Layout 文件放在一个目录中,例如 genesis

  • 将所有 ValidatorConfiguration 文件放入 genesis 目录。

  • 确保 ValidatorConfiguration 文件列在 Layout 文件中的用户集下。

  • genesis 目录中创建一个框架目录,并将框架发布 .mv 文件放入框架目录。

  • 使用 aptos CLI 生成 genesis 和航路点:

cargo run --package aptos -- genesis generate-genesis --local-repository-dir genesis

开始一个 aptos-node

生成 genesis.blob 和路标后,将它们放入验证器和全节点的配置目录中,然后启动验证器和全节点。

Last updated