Introduction to eosio.system Smart Contract (2) System Resources

EOSIO-based blockchains use three system resources: RAM, CPU, NET. Blockchain accounts require sufficient system resources to interact with smart contracts deployed on the blockchain. This tutorial introduces the system resource module in the eosio.system smart contract in detail. It is suitable for junior developers of EOS smart contracts. They are familiar with how to buy and sell RAM, pledge and cancel pledge of CPU, pledge and cancel pledge of NET, etc.

01

overview

(1) Introduction to RAM resources

1. RAM is the memory resource, where the blockchain stores data

The way smart contracts store data on the blockchain is similar to the operation in the database, which can be stored in the RAM of the blockchain using multi-index tables or singletons. The EOSIO-based blockchain uses RAM as a storage medium and has high performance that other blockchains cannot achieve, so accessing blockchain data is very fast.

2. RAM is a limited resource, blockchains can have different RAM policies and rules

For example, the public EOS blockchain started with 64GB of RAM, after which the block producers decided to increase the memory to 1KB per block, thus continuously increasing the supply of RAM so that its price does not vary depending on the demand of the blockchain application. increase and grow too high.

3. RAM is used to perform many operations sent to the blockchain

For example, to create a new account operation, the information of the new account needs to be stored in the blockchain memory; similarly, when an account receives a new type of token, a new record must be created in the blockchain memory, and the record is saved The balance of the new type of tokens received, and this storage space on the blockchain, must be purchased by the account transferring the tokens or the account receiving the new type of tokens.

4. If a smart contract consumes all allocated RAM, no additional information can be stored

If the smart contract consumes all the allocated RAM, to continue saving data in the blockchain database, one of the following two conditions must be met: the smart contract releases a part of the occupied RAM; it is allocated to the smart contract account through the RAM purchase process.

(2) Introduction to CPU and NET resources

1. CPU provides processing power for blockchain accounts

The amount of CPU owned by an account is measured in microseconds and is referred to as cpu bandwidth in the cleos get account command output. The cpu bandwidth represents the processing time at the account's disposal when the operations sent to the contract are executed by the blockchain. The blockchain consumes CPU when executing transactions, so enough CPU must be mortgaged to complete the transaction.

2. NET provides network bandwidth for transactions

The amount of NET owned by the account is measured in bytes and is called net bandwidth in the cleos get account command output. NET is a resource denominated in space, which is used to measure the share of the network consumed by the transaction during the transmission of the P2P layer. NET is consumed when the blockchain executes a transaction, so enough NET must be mortgaged to complete the transaction.

3. CPU and NET resources are allocated by the account owner through the pledge mechanism

When you stake CPU and NET, you gain access to system resources proportional to the total amount of tokens staked by all other users for the same system resource at the same time. This means you can execute transactions for free, but within the limits of your staked tokens. No matter what changes in the free market, the pledged tokens can guarantee the proportion of resources. If the account consumes all allocated CPU and NET resources, there are two options at this point: wait for the blockchain to replenish the consumed resources; allocate more resources through the pledge mechanism.

4. The blockchain automatically replenishes the consumed CPU and NET resources

Before executing a transaction, the blockchain first calculates how much resources the account performing the transaction can consume. When data is missing, the calculation uses an exponential moving average with linear extrapolation and multiplies the current cumulative average by (number of blocks in window - number of blocks since last update) / (number of blocks in window ). The window is set to a 24 hour window.

02

Environmental preparation

(1) A running and accessible blockchain

China Mobile Chain (based on EOS) test environment setup:

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

(2) Make sure the local wallet is opened and unlocked

How to create a wallet:

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

(3) The construction and deployment of eosio.contracts has been completed

How to build eosio.contracts:

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

(4) The creation, issuance and transfer of tokens have been completed

How to create, issue and transfer tokens:

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

03

RAM operation

(1) Purchase RAM

Run the following command to purchase RAM resources worth 0.1 SYS for account testaccount1, where eosio=account paying for RAM, testaccount1=account receiving purchase of RAM, 0.1 SYS=number of tokens paid for RAM, -p eosio@active=for authorization Purchased permissions, in this case active permissions.

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         ]

(2) Sell RAM

Run the following command to sell 1024 Bytes of RAM resources for the testaccount1 account, where testaccount1=the account that sells RAM resources, 1024=the number of bytes of RAM sold, -p testaccount1@active=the permission used to authorize the sale, in this case it is active permission.

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 operation

(1) Pledge CPU

Run the following command to pledge 0.01 SYS of CPU resources for the testaccount1 account, where eosio=the account that pays 0.01 SYS for pledged CPU bandwidth, testaccount1=the account that pledges CPU bandwidth, 0 SYS=the amount of SYS tokens allocated for pledged NET bandwidth, 0.01 SYS = amount of SYS tokens allocated for staking CPU bandwidth, -p eosio@active = permission to authorize staking, in this case the active permission.

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         ]

(2) Cancel the pledge of CPU

Run the following command to cancel the pledge of 0.01 SYS of CPU resources for the testaccount1 account, where eosio=the account that paid 0.01 SYS for pledged CPU bandwidth, testaccount1=the account that pledged CPU bandwidth, 0 SYS=the amount of SYS tokens allocated for pledged NET bandwidth, 0.01 SYS=Amount of SYS tokens allocated for staking CPU bandwidth, -p eosio@active=Permission to authorize unstaking, in this case the active permission.

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 operation

(1) Pledge NET

Run the following command to pledge 0.01 SYS of NET resources for the testaccount1 account, where eosio=the account that paid 0.01 SYS for pledged NET bandwidth, testaccount1=the account that pledged NET bandwidth, 0 SYS=the amount of SYS tokens allocated for pledged NET bandwidth, 0.01 SYS = amount of SYS tokens allocated for staking CPU bandwidth, -p eosio@active = permission to authorize staking, in this case the active permission.

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         ]

(2) Cancel the pledge of NET

Run the following command to cancel the pledge of 0.01 SYS of NET resources for account testaccount1, where eosio=the account that paid 0.01 SYS for pledged NET bandwidth, testaccount1=the account that pledged NET bandwidth, 0 SYS=the amount of SYS tokens allocated for pledged NET bandwidth, 0.01 SYS = the amount of SYS tokens allocated for staking CPU bandwidth, -p eosio@active = permission to authorize unstaking, in this case the active permission.

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

common problem

(1) An error is reported when executing the sellram method: eosio_assert_message assertion failure?

Answer: The number of RAM bytes sold is too small, and the minimum number of bytes corresponding to 0.0001 SYS has not been reached, and more bytes need to be sold.

(2) What is the token unit for pledged CPU and NET resources, and how to check?

Answer: The token unit of pledged CPU and NET resources is generally SYS, which is defined by the system contract, and the token unit of CPU and NET usage can be queried through cleos get account.

(3) About the three resources of EOS: RAM, NET, CPU, what are the units of quantity, and how to calculate the remaining quantity?

Answer: You can query the remaining resource information by cleos get account. The unit of RAM resource is byte, and the corresponding field of remaining RAM quantity is: quota - used; the unit of NET resource is also byte, and the corresponding field of remaining NET quantity is available; the unit of CPU resource is us, the field corresponding to the number of remaining CPUs: available.

(4) Why is it not recommended to initialize enough RAM, CPU, and NET resources at one time?

Answer: RAM corresponds to the memory resources of the server, and excessive allocation may affect the operation of other services. By calculating the RAM consumed by 10,000 copies of DDC, the RAM required for each DDC can be estimated, so that it can be recharged once in a period of time according to business needs. CPU and NET resources are pledged, and will be returned linearly within 24 hours after the transaction is completed, so it is enough to recharge the CPU and NET resources required for one day.

-END-

Guess you like

Origin blog.csdn.net/BSN_yanxishe/article/details/131375757