Dapp开发我爱编程EOS学习资料

【EOS开发学习笔记】程序和工具(一)cleos之get用法

2018-04-11  本文已影响310人  面壁者Z

先啰嗦几句:本人区块链小白一枚,正在自学EOS应用开发(当然目前也没有培训班培训),我的这个系列文章是我的学习笔记,因此,鉴于水平有限,难免有许多错误之处,还希望各位读者海涵,若能留言勘误,更是感激不尽。同时也欢迎热爱EOS开发的朋友加我微信(微信号:361757),暗号EOS,我已经创建了一个交流群,发布本文时(2018年4月11日),群人数已经达到99人。好了,系好安全带,我们的EOS应用开发之旅,马上开始!

本系列文章共分4篇,学习的内容为EOS.IO Programs & Tools

本文为第一部分,除了简单介绍nodeos/keosd/launcher/snapshot外,重点探索cleos的各个用法中的第一部分,get的用法。

在学习本文前,您需要先部署好EOS.IO运行环境,测试一下智能合约,然后再开始学习。

001 nodeos

nodeos是EOS.IO的核心守护进程,可以使用插件配置以运行节点。用途是块的生产,专用API端点和本地开发。当我们在部署了EOS.IO的服务器上运行nodeos时,nodeos会开始不断的生产区块,目前为0.5s/个。

在linux系统下,nodeos的配置文档位于root用户主目录下的隐藏文件夹.local内,具体路径如下:

cd ~./local/share/eosio/nodeos/config

这个文件夹下,有一个名为config.ini的文件,里边是对nodeos的一些配置文档。

002 keosd

keosd是一个钱包驻留守护程序,例如http接口和RPC API。

该程序位于 /home/username/eos/build/programs。

003 launcher

launcher简化了多个节点在局域网或更广泛的网络中的分布。它可以通过CLI进行配置以组成每个节点的配置文件,在对等主机之间安全地分发这些文件,然后启动多个nodeos实例。

在单节点的情况下,应该是不需要启动的。

004 snapshot

快照程序引用自EOSIO/genesis存储库子模块,其中包含用于从众包协议生成快找的nodejs应用程序,用于配置生成块或其他生成相关工具的Web GUI。

005 cleos

上面的程序基本都是一些基础进程,cleos才是这篇文章的重头戏,因为它可以做的事情太多了。现在跟随我一起,来探索一下吧!

cleos是一个命令行工具,它需要启动nodeos才可以使用。要想使用它,你需要知道节点的IP和端口号,并且这个节点还必须配置加载了eosio::chain_api_plugin。通过直接运行cleos,可以查看其支持的各种命令和简介。

程序位于:/home/username/eos/build/programs

这里有一个小技巧,就是如果你想了解某个子命令的帮助信息,只要不带参数的运行它就好了。

先看一下基本的命令有哪些:

$ cleos

ERROR: RequiredError: Subcommand required

Command Line Interface to EOSIO Client

Usage: ./cleos

[OPTIONS] SUBCOMMANDOptions: 

-h,--help                   Print this help message and exit 

-H,--host TEXT=localhost    the host where nodeos is running 

-p,--port UINT=8888         the port where nodeos is running 

--wallet-host TEXT=localhost                              the host where keosd is running 

--wallet-port UINT=8888     the port where keosd is running 

-v,--verbose                output verbose messages on error

Subcommands: 

version                     Retrieve version information 

create                      Create various items, on and off the blockchain 

get                         Retrieve various items and information from the blockchain 

set                         Set or update blockchain state 

transfer                    Transfer EOS from account to account 

net                         Interact with local p2p network connections 

wallet                      Interact with local wallet 

benchmark                   Configure and execute benchmarks  

push                        Push arbitrary transactions to the blockchain

1) Options

-h,--help,获取帮助信息,效果与直接运行cleos一样。

-H,--host TEXT=localhost,获取nodeos运行的主机名/ip地址

-p,--port UINT=8888,获取nodeos运行的主机端口

--wallet-host TEXT=localhost 获取keosd运行的主机名/ip地址

--wallet-port UINT=8888,获取keosd运行的主机端口

-v,--verbose,输出错误的详细信息

2) subcommands

2-1  version

获取版本信息,仅包含一个子命令:client(获取客户端版本信息)

使用方法:

$ cleos version client

Build version: 124c62d0

2-2 create

创建项目,在区块链上面或下面。

包含以下子命令:

key,创建一对新的key,并打印出来,包括私钥和公钥

$ cleos create key

Private key: 5JimvGoUyEK7Sfs773eFMyVMwySPig7qLruMDyX6rhEzr4Puw46

Public key: EOS8NftRSw6u2X65SUg2u6GG1mgpwYztaBAa2U56RGAqiVrX4uhko

account,在区块链上创建一个新的账户(这个相对复杂一点,需要的参数也比较多)

先来试一下:

$ cleos create account

ERROR: RequiredError: creator

Create a new account on the blockchain

Usage: cleos create account [OPTIONS] creator name OwnerKey ActiveKey

# 创建账户的格式,cleos create account [options] <创建者> <账户名> <ownerkey> <activekey>,这里的ownerkey和activekey是前面通过cleos create key生成的两组key的public key

Positionals:

  creator TEXT                The name of the account creating the new account

  name TEXT                  The name of the new account

  OwnerKey TEXT              The owner public key for the new account

  ActiveKey TEXT              The active public key for the new account

Options:

  -x,--expiration            set the time in seconds before a transaction expires, defaults to 30s

# 设置交易到期的时间,单位为秒,默认为30s

  -f,--force-unique          force the transaction to be unique. this will consume extra bandwidth and remove any protections against accidently issuing the same transaction multiple times

# 强制设置事务唯一。这可以避免额外的带宽和意外的重复交易。

  -s,--skip-sign              Specify if unlocked wallet keys should be used to sign transaction

# 指定是否需要使用未打开的钱包密钥来签署事务

  -j,--json                  print result as json

# 是否打印json结果

  -d,--dont-broadcast        don't broadcast transaction to the network (just print to stdout)

# 不要广播到网络,只打印到stdout

  -p,--permission TEXT ...    An account and permission level to authorize, as in 'account@permission' (defaults to 'creator@active')

# 授权账户和权限级别,如 account@permission,默认为cteator@active

  --max-cpu-usage UINT        set an upper limit on the cpu usage budget, in instructions-retired, for the execution of the transaction (defaults to 0 which means no limit)

# 设置CPU预算上限默认为0,即不限制

  --max-net-usage UINT        set an upper limit on the net usage budget, in bytes, for the transaction (defaults to 0 which means no limit)

# 为事务设置净使用预算的上限,单位字节,默认为0即不限制

现在我们按照要求,尝试创建一个账户:

$ cleos create key # 创建OwnerKey

$ cleos create key # 创建ActiveKey

$ cleos create wallet unlock # 解锁钱包

$ cleos create account eosio bob2 <public-OwnerKey> <public-ActiveKey>

executed transaction: 08fe8f8c9010949c1a4e8ec09d806f93962ca8105563285e79c5090f9c5a0162  352 bytes  102400 cycles

#        eosio <= eosio::newaccount            {"creator":"eosio","name":"bob2","owner":{"threshold":1,"keys":[{"key":"EOS5sfiYLq8fEDoq7iWRNKDWCHJZ...

这样我们就成功的创建了一个叫做bob2的账户,它的creator是eosio。

3) get

老样子,先试试

$ cleos get

ERROR: RequiredError: Subcommand required

Retrieve various items and information from the blockchain

Usage: cleos get SUBCOMMAND

Subcommands:

  info                        Get current blockchain information

# 获取当前区块链信息

  block                      Retrieve a full block from the blockchain

# 检出一个完整的区块

  account                    Retrieve an account from the blockchain

# 检出一个区块链上的账户

  code                        Retrieve the code and ABI for an account

# 检出代码和ABI

  table                      Retrieve the contents of a database table

# 检出数据表的内容

  currency                    Retrieve information related to standard currencies

# 检出与标准货币相关的信息

  accounts                    Retrieve accounts associated with a public key

# 检出与公钥关联的账户

  servants                    Retrieve accounts which are servants of a given account 

# 检出给定账户的子账户

  transaction                Retrieve a transaction from the blockchain

# 检出一条区块链上的交易

  transactions                Retrieve all transactions with specific account name referenced in their scope

# 检出在其范围内引用的特定账户名称的所有事务

好了,我们一个一个试试

3-1 info

$ cleos get info

{

  "server_version": "124c62d0",  # 服务端版本

  "head_block_num": 277162, #块高度

  "last_irreversible_block_num": 277161, # 最后一个确认块

  "head_block_id": "00043aaa09080cf8ca265d4d1c1cd310578b0baee68959da490cfe99769d278a", # 头文件ID

  "head_block_time": "2018-04-11T03:29:39", # 头文件时间

  "head_block_producer": "eosio" # 区块生产者名称

}

3-2 block

$ cleos get block  # 会报错,提示需要输入块ID

$ cleos get block 277161 # 最后这个ID我们取刚才获取到的任意一个ID即可

{

  "previous": "00043aa89db2e1cd1bb0963100d4f2f8d509994c0e7111cb2c34a91051ad49fd",

  "timestamp": "2018-04-11T03:29:39.000",

  "transaction_mroot": "644f83cb15c5c968bbbac40a2d0e830a27c15a318cb2ec6d67b5647069b4f765",

  "action_mroot": "9d83071ec793e7cc1f1c1d164f5bcb5bb25b153865b2c64a5eb07abb75934195",

  "block_mroot": "0d662207da7e67f09c4472907c44c5364fcc2f9ba33505674341bd8e2be71ac3",

  "producer": "eosio",

  "schedule_version": 0,

  "new_producers": null,

  "producer_signature": "EOSK7aQgderqj2t68SZc3rk2P3eMyZmutBUBXqhEfsTb43PzrEU7xdfvYy1D8w2LuZ648vpZMqmgqvsSY6reoBdA9j1cMuc8W",

  "regions": [{

      "region": 0,

      "cycles_summary": [[{

            "read_locks": [],

            "write_locks": [],

            "transactions": [{

                "status": "executed",

                "kcpu_usage": 2,

                "net_usage_words": 38,

                "id": "4099d5ee4afe48d33ec28e883742b19505ea3fb07c9f7cdddd9f4988a43d66a0"

              }

            ]

          }

        ]

      ]

    }

  ],

  "input_transactions": [],

  "id": "00043aa9252e8e04ed618aba50879fa48d53e6d1300c3ff0504ed15b345e94e1",

  "block_num": 277161,

  "ref_block_prefix": 3129631213

}

3-3 account

$ cleos get account #报错,需要账户名

$ cleos get account bob2 #用我们刚才创建的账户名

{

  "account_name": "bob2",

  "permissions": [{

      "perm_name": "active",

      "parent": "owner",

      "required_auth": {

        "threshold": 1,

        "keys": [{

            "key": "EOS8Aw3TYtBY1b3qJmP1tJqEsGzDaKL1gzbTuzfYuVHxkUFF8LDeZ",

            "weight": 1

          }

        ],

        "accounts": []

      }

    },{

      "perm_name": "owner",

      "parent": "",

      "required_auth": {

        "threshold": 1,

        "keys": [{

            "key": "EOS5sfiYLq8fEDoq7iWRNKDWCHJZvnj4kA2YBoo72bRLoJXwgZWZW",

            "weight": 1

          }

        ],

        "accounts": []

      }

    }

  ]

}

我们看到返回了一组json数据,其中包含了Owner和Active的publick-Key。

3-4 code

$ cleos get code # 提示报错,需要code name

ERROR: RequiredError: name

Retrieve the code and ABI for an account

Usage: cleos get code [OPTIONS] name

Positionals:

  name TEXT                  The name of the account whose code should be retrieved

Options:

  -c,--code TEXT              The name of the file to save the contract .wast to

  -a,--abi TEXT              The name of the file to save the contract .abi to

$ cleos get code bob2  # 依旧用bob2做为测试

code hash: 0000000000000000000000000000000000000000000000000000000000000000

3-5 table

$ cleos get table # 提示我们需要输入contract scope table参数

ERROR: RequiredError: contract

Retrieve the contents of a database table

Usage: cleos get table [OPTIONS] contract scope table

Positionals:

  contract TEXT              The contract who owns the table

# 拥有表的合约

  scope TEXT                  The scope within the contract in which the table is found

# 表的作用域

  table TEXT                  The name of the table as specified by the contract abi

# 表的名称

Options:

  -b,--binary UINT            Return the value as BINARY rather than using abi to interpret as JSON

  -l,--limit UINT            The maximum number of rows to return

  -k,--key TEXT              The name of the key to index by as defined by the abi, defaults to primary key

  -L,--lower TEXT            JSON representation of lower bound value of key, defaults to first

  -U,--upper TEXT            JSON representation of upper bound value value of key, defaults to last

由于我们目前没有创建过表,所以这里暂时没办法演示。

3-6 currency 

$ cleos get currency # 报错,提示了两个参数,balance(余额)和stats(状态)

$ cleos get currency balance #尝试获取余额,提示要输入合约和账户

$ cleos get currency balance currency eosio #查询合约“currency”(我们在配置环境时部署过的合约),账户eosio(我们曾经转过20个MGD进去)

20.0000 MGD

3-7 accounts

$ cleos get accounts # 要输入public key

$ cleos get accounts EOS5sfiYLq8fEDoq7iWRNKDWCHJZvnj4kA2YBoo72bRLoJXwgZWZW # 从前面随便取一个bob2的publickey试试

{

  "account_names": [

    "bob2"

  ]

}

3-8 servants

$ cleos get servants #报错,提示输入账户名

$ cleos get servants eosio #我们输入最高级别账户

{

  "controlled_accounts": [

    "bob1",

    "bob2",

    "currency",

    "zilly"

  ]

}

# 返回了该账户下的所有子账户

3-9 transaction

$ cleos get transaction # 报错,提示需要id,但我们暂时没有id,所以暂时没办法演示。

3-10 transactions

获取某个账户下的所有交易

$ cleos get transaction -j eosio # 获取eosio账户下的交易,并打印

{

  "transactions": []

}

# 因为没有,所以为空。

至此,cleos get部分我们基本都尝试过了,由于条件限制,有些操作可能无法演示,不过后面我们都会用到。

下一篇将继续探索 cleos下的set子命令的各个用法。

我是王越,EOS应用开发小白一枚,渴望与你链接,我已经建立了一个交流开发技术的微信群,期待你的加入!请加我微信 361757,暗号EOS

上一篇下一篇

猜你喜欢

热点阅读