关于智能合约:eosiosystem智能合约介绍二系统资源

2次阅读

共计 7318 个字符,预计需要花费 19 分钟才能阅读完成。

基于 EOSIO 的区块链应用三种系统资源:RAM、CPU、NET。区块链账户须要足够的系统资源,能力与部署在区块链上的智能合约进行交互。本教程具体介绍了 eosio.system 智能合约中的系统资源模块,实用于 EOS 智能合约的高级开发人员,相熟如何进行 RAM 的购买与发售、CPU 的质押与勾销质押、NET 的质押与勾销质押等。

01

概述

(一)RAM 资源简介

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

智能合约在区块链上存储数据的形式,相似于在数据库的操作,能够应用多索引表或单例将其存储在区块链的 RAM 中。基于 EOSIO 的区块链应用 RAM 作为存储介质,领有其余区块链无奈达到的高性能,因而拜访区块链数据十分快。

  1. RAM 是一种无限的资源,区块链能够有不同的 RAM 政策和规定

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

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

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

  1. 如果智能合约耗费了所有调配的 RAM,则无奈存储任何附加信息

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

(二)CPU 和 NET 资源简介

  1. CPU 为区块链账户提供解决能力

帐户领有的 CPU 数量以微秒为单位,在 cleos get account 命令输入中称为 cpu 带宽。cpu 带宽示意当发送到合约的操作由区块链执行时,帐户可摆布的解决工夫。区块链执行交易时会耗费 CPU,因而必须抵押足够的 CPU 能力实现交易。

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

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

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

当您为 CPU 和 NET 抵押代币时,您能够拜访与所有其余用户在同一时间为同一系统资源抵押的代币总量成比例的系统资源。这意味着您能够收费执行交易,但要在质押代币的限度范畴内。无论自由市场有什么变动,质押的代币都能保障资源的比例。如果帐户耗费了所有调配的 CPU 和 NET 资源,此时能够有两个抉择:期待区块链补充耗费的资源;通过质押机制调配更多的资源。

  1. 区块链主动补充耗费的 CPU 和 NET 资源

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

02

环境筹备

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

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

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

(二)确保本地钱包已关上并解锁

如何创立钱包:

https://developers.eos.io/manuals/eos/latest/cleos/how-to-gui…

(三)已实现 eosio.contracts 的构建和部署

如何构建 eosio.contracts:

https://developers.eos.io/manuals/eosio.contracts/latest/buil…

(四)已实现 token 的创立、发行和转移

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

https://developers.eos.io/manuals/eosio.contracts/latest/guid…

03

RAM 的操作

(一)购买 RAM

运行以下命令为 testaccount1 账户购买价值 0.1 SYS 的 RAM 资源,其中 eosio= 领取 RAM 的账户,testaccount1= 接管购买 RAM 的账户,0.1 SYS= 为 RAM 领取的代币数量,-p eosio@active= 用于受权购买的权限,在本例中为 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 权限。


# 示例输入: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 权限。


# 示例输入: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 权限。


# 示例输入: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 权限。


# 示例输入: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 权限。


# 示例输入: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