基于EOSIO的区块链应用三种系统资源:RAM、CPU、NET。区块链账户须要足够的系统资源,能力与部署在区块链上的智能合约进行交互。本教程具体介绍了eosio.system智能合约中的系统资源模块,实用于EOS智能合约的高级开发人员,相熟如何进行RAM的购买与发售、CPU的质押与勾销质押、NET的质押与勾销质押等。
01
概述
(一)RAM资源简介
- RAM是内存资源,区块链存储数据的中央
智能合约在区块链上存储数据的形式,相似于在数据库的操作,能够应用多索引表或单例将其存储在区块链的RAM中。基于EOSIO的区块链应用RAM作为存储介质,领有其余区块链无奈达到的高性能,因而拜访区块链数据十分快。
- RAM是一种无限的资源,区块链能够有不同的RAM政策和规定
例如,公共EOS区块链以64GB的RAM开始,之后区块生产者决定将内存减少到每个区块1KB,从而一直减少RAM的供给,以使其价格不会因区块链应用程序需要的减少而增长过高。
- RAM用于执行发送到区块链的许多操作
例如,创立一个新账户操作,须要将新账户的信息存储在区块链内存中;同样,当一个帐户接管一种新型代币时,必须在区块链内存中创立一个新记录,该记录保留接管的新型代币余额,并且区块链上的这块存储空间,必须被转移代币的帐户或接管新型代币的帐户购买。
- 如果智能合约耗费了所有调配的RAM,则无奈存储任何附加信息
如果智能合约耗费了所有调配的RAM,要在区块链数据库中持续保留数据,必须满足以下两个条件之一:智能合约开释了一部分占用的RAM;通过RAM购买过程调配给智能合约账户。
(二)CPU和NET资源简介
- CPU为区块链账户提供解决能力
帐户领有的CPU数量以微秒为单位,在cleos get account命令输入中称为cpu带宽。cpu带宽示意当发送到合约的操作由区块链执行时,帐户可摆布的解决工夫。区块链执行交易时会耗费CPU,因而必须抵押足够的CPU能力实现交易。
- NET为交易提供网络带宽
帐户领有的NET数量以字节为单位,在cleos get account命令输入中称为net带宽。NET是一种以空间计价的资源,用于掂量交易在P2P层传输过程中耗费的网络份额。区块链执行交易时会耗费NET,因而必须抵押足够的NET能力实现交易。
- CPU和NET资源由帐户所有者通过质押机制调配
当您为CPU和NET抵押代币时,您能够拜访与所有其余用户在同一时间为同一系统资源抵押的代币总量成比例的系统资源。这意味着您能够收费执行交易,但要在质押代币的限度范畴内。无论自由市场有什么变动,质押的代币都能保障资源的比例。如果帐户耗费了所有调配的CPU和NET资源,此时能够有两个抉择:期待区块链补充耗费的资源;通过质押机制调配更多的资源。
- 区块链主动补充耗费的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-