eosio.system智能合约介绍(二)系统资源

基于EOSIO的区块链使用三种系统资源:RAM、CPU、NET。区块链账户需要足够的系统资源,才能与部署在区块链上的智能合约进行交互。本教程详细介绍了eosio.system智能合约中的系统资源模块,适用于EOS智能合约的初级开发人员,熟悉如何进行RAM的购买与出售、CPU的质押与取消质押、NET的质押与取消质押等。

01

概述

(一)RAM资源简介

1. RAM是内存资源,区块链存储数据的地方

智能合约在区块链上存储数据的方式,类似于在数据库的操作,可以使用多索引表或单例将其存储在区块链的RAM中。基于EOSIO的区块链使用RAM作为存储介质,拥有其他区块链无法达到的高性能,因此访问区块链数据非常快。

2. RAM是一种有限的资源,区块链可以有不同的RAM政策和规则

例如,公共EOS区块链以64GB的RAM开始,之后区块生产者决定将内存增加到每个区块1KB,从而不断增加RAM的供应,以使其价格不会因区块链应用程序需求的增加而增长过高。

3. RAM用于执行发送到区块链的许多操作

例如,创建一个新账户操作,需要将新账户的信息存储在区块链内存中;同样,当一个帐户接收一种新型代币时,必须在区块链内存中创建一个新记录,该记录保存接收的新型代币余额,并且区块链上的这块存储空间,必须被转移代币的帐户或接收新型代币的帐户购买。

4. 如果智能合约消耗了所有分配的RAM,则无法存储任何附加信息

如果智能合约消耗了所有分配的RAM,要在区块链数据库中继续保存数据,必须满足以下两个条件之一:智能合约释放了一部分占用的RAM;通过RAM购买过程分配给智能合约账户。

(二)CPU和NET资源简介

1. CPU为区块链账户提供处理能力

帐户拥有的CPU数量以微秒为单位,在cleos get account命令输出中称为cpu带宽。cpu带宽表示当发送到合约的操作由区块链执行时,帐户可支配的处理时间。区块链执行交易时会消耗CPU,因此必须抵押足够的CPU才能完成交易。

2. NET为交易提供网络带宽

帐户拥有的NET数量以字节为单位,在cleos get account命令输出中称为net带宽。NET是一种以空间计价的资源,用于衡量交易在P2P层传输过程中消耗的网络份额。区块链执行交易时会消耗NET,因此必须抵押足够的NET才能完成交易。

3. CPU和NET资源由帐户所有者通过质押机制分配

当您为CPU和NET抵押代币时,您可以访问与所有其他用户在同一时间为同一系统资源抵押的代币总量成比例的系统资源。这意味着您可以免费执行交易,但要在质押代币的限制范围内。无论自由市场有什么变化,质押的代币都能保证资源的比例。如果帐户消耗了所有分配的CPU和NET资源,此时可以有两个选择:等待区块链补充消耗的资源;通过质押机制分配更多的资源。

4. 区块链自动补充消耗的CPU 和NET资源

在执行交易之前,区块链首先计算执行交易的账户可以消耗多少资源。当数据丢失时,该计算使用带有线性外推的指数移动平均值,并将当前累积平均值乘以(窗口中的块数-自上次更新以来的块数) / (窗口中的块数)。窗口设置为24小时窗口。

02

环境准备

(一)一条正在运行且可访问的区块链

中移链(基于EOS)测试环境搭建:

https://mp.weixin.qq.com/s/NBNFk9Xk9FCukMVgl0tfHA

(二)确保本地钱包已打开并解锁

如何创建钱包:

https://developers.eos.io/manuals/eos/latest/cleos/how-to-guides/how-to-create-a-wallet

(三)已完成eosio.contracts的构建和部署

如何构建eosio.contracts:

https://developers.eos.io/manuals/eosio.contracts/latest/build-and-deploy

(四)已完成token的创建、发行和转移

如何创建、发行和转移token:

https://developers.eos.io/manuals/eosio.contracts/latest/guides/how-to-create-issue-and-transfer-a-token

03

RAM的操作

(一)购买RAM

运行以下命令为testaccount1账户购买价值0.1 SYS的RAM资源,其中eosio=支付RAM的账户,testaccount1=接收购买RAM的账户,0.1 SYS=为RAM支付的代币数量,-p eosio@active=用于授权购买的权限,在本例中为active权限。

cleos system buyram eosio testaccount1 "0.1 SYS" -p eosio@active

# 示例输出:
executed transaction: e669b474e985c4346fddcbd98344580d56683a3856874e529d6bff065615c155  128 bytes  466 us
#         eosio <= eosio::buyram                {"payer":"eosio","receiver":"testaccount1","quant":"0.1000 SYS"}
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"eosio.ram","quantity":"0.0995 SYS","memo":"buy ram"}
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"eosio.ramfee","quantity":"0.0005 SYS","memo":"ram fee"}
#         eosio <= eosio.token::transfer        {"from":"eosio","to":"eosio.ram","quantity":"0.0995 SYS","memo":"buy ram"}
#     eosio.ram <= eosio.token::transfer        {"from":"eosio","to":"eosio.ram","quantity":"0.0995 SYS","memo":"buy ram"}
#         eosio <= eosio.token::transfer        {"from":"eosio","to":"eosio.ramfee","quantity":"0.0005 SYS","memo":"ram fee"}
#  eosio.ramfee <= eosio.token::transfer        {"from":"eosio","to":"eosio.ramfee","quantity":"0.0005 SYS","memo":"ram fee"}
warning: transaction executed locally, but may not be confirmed by the network yet         ]

(二)出售RAM

运行以下命令为testaccount1账户出售1024 Bytes的RAM资源,其中testaccount1=出售RAM资源的账户,1024=出售RAM的字节数量,-p testaccount1@active=用于授权出售的权限,在本例中为active权限。

cleos system sellram testaccount1 1024 -p testaccount1@active

# 示例输出:
executed transaction: 9a4d3cc802a152cc26ce897571cf95b506cd56aa254becde5efc24f3304cd779  112 bytes  467 us
#         eosio <= eosio::sellram               {"account":"testaccount1","bytes":1024}
#   eosio.token <= eosio.token::transfer        {"from":"eosio.ram","to":"testaccount1","quantity":"0.0149 SYS","memo":"sell ram"}
#   eosio.token <= eosio.token::transfer        {"from":"testaccount1","to":"eosio.ramfee","quantity":"0.0001 SYS","memo":"sell ram fee"}
#     eosio.ram <= eosio.token::transfer        {"from":"eosio.ram","to":"testaccount1","quantity":"0.0149 SYS","memo":"sell ram"}
#  testaccount1 <= eosio.token::transfer        {"from":"eosio.ram","to":"testaccount1","quantity":"0.0149 SYS","memo":"sell ram"}
#  testaccount1 <= eosio.token::transfer        {"from":"testaccount1","to":"eosio.ramfee","quantity":"0.0001 SYS","memo":"sell ram fee"}
#  eosio.ramfee <= eosio.token::transfer        {"from":"testaccount1","to":"eosio.ramfee","quantity":"0.0001 SYS","memo":"sell ram fee"}
warning: transaction executed locally, but may not be confirmed by the network yet         ]

04

CPU的操作

(一)质押CPU

运行以下命令为testaccount1账户质押0.01 SYS的CPU资源,其中eosio=为质押CPU带宽支付0.01 SYS的账户,testaccount1=质押CPU带宽的账户,0 SYS=为质押NET带宽分配的SYS代币数量,0.01 SYS=为质押CPU带宽分配的SYS代币数量,-p eosio@active=用于授权质押的权限,在本例中为active权限。

cleos system sellram testaccount1 1024 -p testaccount1@active

# 示例输出:
executed transaction: 9a4d3cc802a152cc26ce897571cf95b506cd56aa254becde5efc24f3304cd779  112 bytes  467 us
#         eosio <= eosio::sellram               {"account":"testaccount1","bytes":1024}
#   eosio.token <= eosio.token::transfer        {"from":"eosio.ram","to":"testaccount1","quantity":"0.0149 SYS","memo":"sell ram"}
#   eosio.token <= eosio.token::transfer        {"from":"testaccount1","to":"eosio.ramfee","quantity":"0.0001 SYS","memo":"sell ram fee"}
#     eosio.ram <= eosio.token::transfer        {"from":"eosio.ram","to":"testaccount1","quantity":"0.0149 SYS","memo":"sell ram"}
#  testaccount1 <= eosio.token::transfer        {"from":"eosio.ram","to":"testaccount1","quantity":"0.0149 SYS","memo":"sell ram"}
#  testaccount1 <= eosio.token::transfer        {"from":"testaccount1","to":"eosio.ramfee","quantity":"0.0001 SYS","memo":"sell ram fee"}
#  eosio.ramfee <= eosio.token::transfer        {"from":"testaccount1","to":"eosio.ramfee","quantity":"0.0001 SYS","memo":"sell ram fee"}
warning: transaction executed locally, but may not be confirmed by the network yet         ]

(二)取消质押CPU

运行以下命令为 testaccount1 账户取消质押 0.01 SYS 的 CPU 资源,其中 eosio=为质押 CPU 带宽支付 0.01 SYS 的账户,testaccount1=质押 CPU 带宽的账户,0 SYS=为质押 NET 带宽分配的SYS 代币数量,0.01 SYS=为质押 CPU 带宽分配的 SYS 代币数量,-p eosio@active=用于授权取消质押的权限,在本例中为 active 权限。

cleos system undelegatebw eosio testaccount1 "0 SYS" "0.01 SYS" -p eosio@active

# 示例输出:
executed transaction: e7e7edb6c5556de933f9d663fea8b4a9cd56ece6ff2cebf056ddd0835efa6606  184 bytes  452 us
#         eosio <= eosio::undelegatebw          {"from":"eosio","receiver":"testaccount1","unstake_net_quantity":"0.0000 SYS","unstake_cpu_qu...
warning: transaction executed locally, but may not be confirmed by the network yet         ]

05

NET的操作

(一)质押NET

运行以下命令为testaccount1账户质押0.01 SYS的NET资源,其中eosio=为质押NET带宽支付0.01 SYS的账户,testaccount1=质押NET带宽的账户,0 SYS=为质押NET带宽分配的SYS代币数量,0.01 SYS=为质押CPU带宽分配的SYS代币数量,-p eosio@active=用于授权质押的权限,在本例中为active权限。

cleos system delegatebw eosio testaccount1 "0.01 SYS" "0 SYS" -p eosio@active

# 示例输出:
executed transaction: d25c7deff42f3a420081b8b717820a78a01cf42fc173fe57d305e7ddaebebc12  144 bytes  422 us
#         eosio <= eosio::delegatebw            {"from":"eosio","receiver":"testaccount1","stake_net_quantity":"0.0100 SYS","stake_cpu_quantity":"0....
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"eosio.stake","quantity":"0.0100 SYS","memo":"stake bandwidth"}
#         eosio <= eosio.token::transfer        {"from":"eosio","to":"eosio.stake","quantity":"0.0100 SYS","memo":"stake bandwidth"}
#   eosio.stake <= eosio.token::transfer        {"from":"eosio","to":"eosio.stake","quantity":"0.0100 SYS","memo":"stake bandwidth"}
warning: transaction executed locally, but may not be confirmed by the network yet         ]

(二)取消质押NET

运行以下命令为testaccount1账户取消质押0.01 SYS的NET资源,其中eosio=为质押NET带宽支付0.01 SYS的账户,testaccount1=质押NET带宽的账户,0 SYS=为质押NET带宽分配的SYS代币数量,0.01 SYS=为质押CPU带宽分配的SYS代币数量,-p eosio@active=用于授权取消质押的权限,在本例中为active权限。

cleos system undelegatebw eosio testaccount1 "0.01 SYS" "0 SYS" -p eosio@active

# 示例输出:
executed transaction: e7e7edb6c5556de933f9d663fea8b4a9cd56ece6ff2cebf056ddd0835efa6606  184 bytes  452 us
#         eosio <= eosio::undelegatebw          {"from":"eosio","receiver":"testaccount1","unstake_net_quantity":"0.01 SYS","unstake_cpu_qu...
warning: transaction executed locally, but may not be confirmed by the network yet         ]

06

常见问题

(一)执行sellram方法时报错:eosio_assert_message assertion failure?

解答:出售的RAM字节数太少了,没有达到0.0001 SYS对应的最小字节数,需要多出售一些字节数量。

(二)质押CPU和NET资源的代币单位是啥,如何查询呢?

解答:质押CPU和NET资源的代币单位一般是SYS,这是通过系统合约定义的,可以通过cleos get account查询到CPU、NET用量的代币单位。

(三)关于EOS的三种资源:RAM、NET、CPU,数量单位分别是什么,以及如何计算剩余数量?

解答:可以cleos get account查询到剩余资源信息,其中RAM资源的单位是byte,剩余RAM数量对应字段:quota - used;NET资源的单位也是byte,剩余NET数量对应字段:available;CPU资源的单位是us,剩余CPU数量对应字段:available。

(四)关于一次性初始化足够多的RAM、CPU、NET资源,为啥不建议这么操作?

解答:RAM对应服务器的内存资源,分配过多可能影响其他服务运行。通过计算一万份DDC所消耗的RAM,可以预估出每一份DDC所需的RAM,从而根据业务需要一段时间充值一次就行。CPU和NET资源是质押机制的,交易完成后会在24小时内会线性返还,因此充值一天所需的CPU和NET资源就行。

-END-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值