25 个版本 (稳定版)

2.0.0 2023 年 7 月 4 日
1.6.0 2023 年 5 月 15 日
1.5.1 2023 年 3 月 8 日
1.5.0 2022 年 5 月 13 日
0.1.0 2020 年 11 月 11 日

#1600 in 神奇豆

Apache-2.0

535KB
11K SLoC

casper-client

用于与 Casper 网络交互的客户端库和二进制文件。

运行客户端

客户端以多种模式之一运行,每种模式执行单个操作。要查看所有可用命令

cargo run --release -- help
示例输出
Casper client 2.0.0
A client for interacting with the Casper network

USAGE:
    casper-client [SUBCOMMAND]

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

SUBCOMMANDS:
    put-deploy               Create a deploy and send it to the network for execution
    make-deploy              Create a deploy and output it to a file or stdout. As a file, the
                                 deploy can subsequently be signed by other parties using the
                                 'sign-deploy' subcommand and then sent to the network for execution
                                 using the 'send-deploy' subcommand
    sign-deploy              Read a previously-saved deploy from a file, cryptographically sign
                                 it, and output it to a file or stdout
    send-deploy              Read a previously-saved deploy from a file and send it to the
                                 network for execution
    transfer                 Transfer funds between purses
    make-transfer            Create a transfer deploy and output it to a file or stdout. As a
                                 file, the deploy can subsequently be signed by other parties using
                                 the 'sign-deploy' subcommand and then sent to the network for
                                 execution using the 'send-deploy' subcommand
    get-deploy               Retrieve a deploy from the network
    get-block                Retrieve a block from the network
    get-block-transfers      Retrieve all transfers for a block from the network
    list-deploys             Retrieve the list of all deploy hashes in a given block
    get-state-root-hash      Retrieve a state root hash at a given block
    get-era-info             Retrieve era information from the network
    query-global-state       Retrieve a stored value from the network
    query-balance            Retrieve a purse's balance from the network
    get-dictionary-item      Retrieve a stored value from a dictionary
    get-account              Retrieve account information from the network
    get-auction-info         Retrieve the bids and validators as of either a specific block (by
                                 height or hash), or the most recently added block
    get-validator-changes    Retrieve status changes of active validators
    get-peers                Retrieve network identity and address of each of the specified
                                 node's peers
    get-node-status          Retrieve status of the specified node
    get-chainspec            Retrieve the chainspec of the network (to print the full TOML, run
                                 with '-vv')
    list-rpcs                List all currently supported RPCs
    keygen                   Generate account key files in the given directory
    account-address          Generate an account hash from a given public key
    generate-completion      Generate a shell completion script
    help                     Print this message or the help of the given subcommand(s)

要获取有关任何命令的更多信息,运行 help 后跟子命令,例如

cargo run --release -- help keygen
示例输出
casper-client-keygen 
Generates account key files in the given directory. Creates ["secret_key.pem", "public_key.pem", "public_key_hex"].
"public_key_hex" contains the hex-encoded key's bytes with the hex-encoded algorithm tag prefixed

USAGE:
    casper-client keygen [FLAGS] [OPTIONS] [PATH]

FLAGS:
    -f, --force      If this flag is passed, any existing output files will be overwritten. Without this flag, if any
                     output file exists, no output files will be generated and the command will fail
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -a, --algorithm <STRING>    The type of keys to generate [default: Ed25519]  [possible values: Ed25519, secp256k1]

ARGS:
    <PATH>    Path to output directory where key files will be created. If the path doesn't exist, it will be
              created. If not set, the current working directory will be used

生成非对称签名密钥

某些命令需要使用秘密密钥对数据进行签名。要生成一对秘密和公钥

cargo run --release -- keygen $HOME/.client_keys

与本地节点交互

许多客户端命令需要发送 HTTP 请求并接收响应。要与本机上的本地节点进行此操作,请按照 nctl README 中的说明设置本地测试网络。

在运行客户端命令之前,请确保网络已完全启动。这可以通过运行 nctl-view-node-peers 并检查每个节点是否与其他所有节点建立连接来确定。

对于需要节点地址的客户端命令(通过--node-address-n参数指定),默认值是https://127.0.0.1:7777,这是实际网络节点的地址。对于通过nctl启动的测试网的第一个节点,必须包含--node-address=https://127.0.0.1:11101参数。

在钱包之间转移资金

测试网将被设置为每个节点在其主钱包中拥有初始代币余额。假设我们想在“生成非对称签名密钥”部分创建的新公钥下创建一个新钱包。我们可以通过创建一个新的部署来实现,该部署执行后将资金在两个钱包之间进行转移。最简单的方法是通过transfer子命令。

首先,将public_key_hex文件的内容设置为变量。我们将将其用作目标账户

PUBLIC_KEY=$(cat $HOME/.client_keys/public_key_hex)

然后执行transfer子命令。我们指定要从节点3的主钱包中转移123,456,7个代币,并且为了执行此部署,我们将支付最多10,000个代币

cargo run --release -- transfer \
    --node-address=https://127.0.0.1:11101 \
    --secret-key=../casper-node/utils/nctl/assets/net-1/nodes/node-3/keys/secret_key.pem \
    --amount=1234567 \
    --target-account=$PUBLIC_KEY \
    --chain-name=casper-net-1 \
    --payment-amount=3000000000
示例输出
{
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.0.0",
    "deploy_hash": "c42210759368a07a1b1ff4f019f7e77e7c9eaf2961b8c9dfc4237ea2218246c9"
  },
  "id": 2564730065
}

响应中的deploy_hash值得关注,因为它可以用来识别此部署。

获取部署的详细信息

要查看通过transferput-deploysend-deploy发送到网络的部署的信息,可以使用get-deploy,同时提供执行这些子命令之一后打印的部署哈希。

例如,查看我们之前执行的transfer命令是否生成了一个由网络执行的部署

cargo run --release -- get-deploy --node-address=https://127.0.0.1:11101 c42210759368a07a1b1ff4f019f7e77e7c9eaf2961b8c9dfc4237ea2218246c9
示例输出
{
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.0.0",
    "deploy": {
      "approvals": [
        {
          "signature": "0140850c4f74aaad24894ce2d0e3efb64f599633fad4e280f39529dbd062ab49ca6a1f0bd6f20a8cddeab68e95ae5ea416a5b2ae3a02a0bc7a714c2915106e1c09",
          "signer": "015b7723f1d9499fa02bd17dfe4e1315cfe1660a071e27ab1f29d6ceb6e2abcd73"
        }
      ],
      "hash": "c42210759368a07a1b1ff4f019f7e77e7c9eaf2961b8c9dfc4237ea2218246c9",
      "header": {
        "account": "015b7723f1d9499fa02bd17dfe4e1315cfe1660a071e27ab1f29d6ceb6e2abcd73",
        "body_hash": "c66f1040f8f2aeafee73b7c0811e00fd6eb63a6a5992d7cc0f967e14704dd35b",
        "chain_name": "casper-net-1",
        "dependencies": [],
        "gas_price": 10,
        "timestamp": "2020-10-15T13:23:45.355Z",
        "ttl": "1h"
      },
      "payment": {
        "ModuleBytes": {
          "args": "0100000006000000616d6f756e740300000002102708",
          "module_bytes": ""
        }
      },
      "session": {
        "Transfer": {
          "args": "0200000006000000616d6f756e74040000000387d612080600000074617267657420000000018189fd2d42c36d951f9803e595795a3a0fc07aa999c88a28d286c7cbf338940f0320000000"
        }
      }
    },
    "execution_results": [
      {
        "block_hash": "80a09df67f45bfb290c8f36021daf2fb898587a48fa0e4f7c506202ae8f791b8",
        "result": {
          "cost": "0",
          "effect": {
            "operations": {
              "account-hash-018189fd2d42c36d951f9803e595795a3a0fc07aa999c88a28d286c7cbf33894": "Write",
              "hash-09480c3248ef76b603d386f3f4f8a5f87f597d4eaffd475433f861af187ab5db": "Write",
              "hash-d46e35465520ef9f868be3f26eaded1585dd66ac410706bab4b7adf92bdf528a": "Read",
              "hash-ea274222cc975e4daec2cced17a0270df7c282e865115d98f544a35877af5271": "Add",
              "uref-09480c3248ef76b603d386f3f4f8a5f87f597d4eaffd475433f861af187ab5db-000": "Write",
              "uref-8e7893be4b33bc5eacde4dd684b030593200364a211b8566ed9458ccbafbcde9-000": "Write",
              "uref-b645152645faa6c3f7708fd362a118296f7f4d39dc065c120877d13b6981cd67-000": "Write"
            },
            "transforms": {
              "account-hash-018189fd2d42c36d951f9803e595795a3a0fc07aa999c88a28d286c7cbf33894": "WriteAccount",
              "hash-09480c3248ef76b603d386f3f4f8a5f87f597d4eaffd475433f861af187ab5db": {
                "WriteCLValue": {
                  "bytes": "02b645152645faa6c3f7708fd362a118296f7f4d39dc065c120877d13b6981cd6707",
                  "cl_type": "Key"
                }
              },
              "hash-d46e35465520ef9f868be3f26eaded1585dd66ac410706bab4b7adf92bdf528a": "Identity",
              "hash-ea274222cc975e4daec2cced17a0270df7c282e865115d98f544a35877af5271": {
                "AddKeys": {
                  "uref-09480c3248ef76b603d386f3f4f8a5f87f597d4eaffd475433f861af187ab5db-000": "uref-b645152645faa6c3f7708fd362a118296f7f4d39dc065c120877d13b6981cd67-007"
                }
              },
              "uref-09480c3248ef76b603d386f3f4f8a5f87f597d4eaffd475433f861af187ab5db-000": {
                "WriteCLValue": {
                  "bytes": "",
                  "cl_type": "Unit"
                }
              },
              "uref-8e7893be4b33bc5eacde4dd684b030593200364a211b8566ed9458ccbafbcde9-000": {
                "WriteCLValue": {
                  "bytes": "087929775d78456301",
                  "cl_type": "U512"
                }
              },
              "uref-b645152645faa6c3f7708fd362a118296f7f4d39dc065c120877d13b6981cd67-000": {
                "WriteCLValue": {
                  "bytes": "0387d612",
                  "cl_type": "U512"
                }
              }
            }
          },
          "error_message": null
        }
      }
    ]
  },
  "id": 592430140
}

响应的execution_results中的block_hash值得关注,因为它可以用来识别包含部署的区块。如果部署被节点成功接收并解析,但执行失败,execution_results中的error_message可能提供有用的信息。

获取区块的详细信息

要查看网络创建的Block的信息,可以使用get-block。例如

cargo run --release -- get-block \
    --node-address=https://127.0.0.1:11101 \
    --block-hash=80a09df67f45bfb290c8f36021daf2fb898587a48fa0e4f7c506202ae8f791b8
示例输出
{
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.0.0",
    "block": {
      "body": null,
      "hash": "80a09df67f45bfb290c8f36021daf2fb898587a48fa0e4f7c506202ae8f791b8",
      "header": {
        "accumulated_seed": "e8c65524331dc950d9065c289deb05458d3f9d8beba15e663a5418f5a6c7bed5",
        "body_hash": "0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8",
        "deploy_hashes": [
          "c42210759368a07a1b1ff4f019f7e77e7c9eaf2961b8c9dfc4237ea2218246c9"
        ],
        "era_end": null,
        "era_id": 89,
        "state_root_hash": "c79f4c9a017532fe265593d86d3917581479fd1601093e16d17ec90aeaa63b83",
        "height": 987,
        "parent_hash": "ffb95eac42eae1112d37797a1ecc67860e88a9364c44845cb7a96eb426dca502",
        "proposer": "015b7723f1d9499fa02bd17dfe4e1315cfe1660a071e27ab1f29d6ceb6e2abcd73",
        "random_bit": true,
        "timestamp": "2020-10-15T13:23:48.352Z"
      },
      "proofs": [
        "0104df3fe39567d22a48b68c4b046dadf5af6552c45b1a93613c89a65caa98b12a4564ba1a794e77787eb3d37c19617ca344f2a304387a0364fee0e8f89da2da0d"
      ]
    }
  },
  "id": 3484548969
}

响应的header中的state_root_hash值得关注,因为它可以用来查询全局状态的状态根哈希。

获取包含在区块中的所有Transfers

要检索由网络创建的Block中处理的所有Transfer交易,可以使用get-block-transfers。例如

cargo run --release -- get-block-transfers \
    --node-address=https://127.0.0.1:11101 \
    --block-hash=80a09df67f45bfb290c8f36021daf2fb898587a48fa0e4f7c506202ae8f791b8
示例输出
{
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.0.0",
    "block_hash": "80a09df67f45bfb290c8f36021daf2fb898587a48fa0e4f7c506202ae8f791b8",
    "transfers": [
      {
        "amount": "100000000",
        "deploy_hash": "ab87c5f2c0f6f331bf488703676fb0c68f897282dfbb8e085752f220a3dfc25e",
        "from": "account-hash-1ace33e66142d5a0679ba5507ef75b9c09888d1567e86100d1db535fa819a962",
        "gas": "0",
        "id": null,
        "source": "uref-21f7316e72d1baa7b706a9083077d643665ad3a56673c594db9762ceac4f3788-007",
        "target": "uref-c5eb9788156b53c9a599dfb5e591c6399580b491c72086a6bc028dd18fdfcb2d-004"
      }
    ]
  },
  "id": 7229488934468542904
}

查询全局状态

要查看在执行部署后存储到全局状态的数据,可以使用query-global-state。例如,要查看存储在我们新账户公钥下的值

cargo run --release -- query-global-state \
    --node-address=https://127.0.0.1:11101 \
    --state-root-hash=242666f5959e6a51b7a75c23264f3cb326eecd6bec6dbab147f5801ec23daed6 \
    --key=$PUBLIC_KEY
示例输出
{
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.0.0",
    "stored_value": {
      "Account": {
        "account_hash": "018189fd2d42c36d951f9803e595795a3a0fc07aa999c88a28d286c7cbf33894",
        "action_thresholds": {
          "deployment": 1,
          "key_management": 1
        },
        "associated_keys": [
          {
            "account_hash": "018189fd2d42c36d951f9803e595795a3a0fc07aa999c88a28d286c7cbf33894",
            "weight": 1
          }
        ],
        "main_purse": "uref-09480c3248ef76b603d386f3f4f8a5f87f597d4eaffd475433f861af187ab5db-007",
        "named_keys": {}
      }
    }
  },
  "id": 3649040235
}

这会显示新创建的账户对象的详细信息,包括账户主钱包的URef

查询钱包余额

这可以通过query-balance来完成。例如,要获取我们新创建账户的主钱包的余额

cargo run --release -- query-balance \
    --node-address=https://127.0.0.1:11101 \
    --state-root-hash=242666f5959e6a51b7a75c23264f3cb326eecd6bec6dbab147f5801ec23daed6 \
    --purse-identifier=account-hash-f7196ed0f4a4aa2aae02daa8f0bdad39ed3098c028979db4f96c3c25670a8240
示例输出
{
  "jsonrpc": "2.0",
  "result": {
    "api_version": "1.0.0",
    "balance_value": "1234567"
  },
  "id": 4193583276
}

请注意,系统铸造合约是检索任何给定钱包余额所必需的。如果您执行一个指定钱包URef作为--key参数的query-global-state,您会发现实际上存储在那里的是一个单元值()。这使得get-balance子命令特别有用。


客户端库

lib 目录包含客户端库的源代码,可以直接调用,而不是通过 CLI 二进制文件。CLI 应用 casper-client 使用这个库来实现其功能。


许可证

遵循 Apache License Version 2.0 许可。

依赖

~18–37MB
~608K SLoC