table of Contents
Two, View function to save Gas
Third, the declaration of an array in memory
I. Introduction
Read some block chain of tutorials, papers, on the Internet just to find a project combat, CryptoZombies.
Earlier we talked about Gas, today we'll look at in terms of how to conserve Gas.
If you want to learn more about machine learning, deep learning, block chain, computer vision and other related technologies, to communicate with more chiefs, then scan the next Fanger Wei code to join us now!
Two, View function to save Gas
1, to explain
When a player from the outside to call a view
function that is not required to pay a minute of gas . For the following reasons:
view
Function does not actually change any data on the block chain - they just read. So with theview
mark of a function, which means tellweb3.js
, run this function only need to check your local Ethernet nodes Square, without the need to create a transaction on Block Chaining (transaction needs to be run on each node, and therefore spend Gas) .
So that "read-only" "in the function can be a read-only marks external view
declarations, you can reduce the amount of gas in the DApp.
But we should pay attention to a situation:
If a
view
function within another function is called, and call the function andview
the function does not belong to the same contract, also have call costs . This is because if the calling function in an Ethernet Square creates a transaction, it still needs to verify node to node. So markedview
function only when an external call is free .
2, combat
1. Requirements
Let's write a "return to a player's entire zombie army" function. From when we web3.js
call it, which you display a player's profile page.
1. Create a file called
getZombiesByOwner
new function. It has called_owner
theaddress
parameter type.
2.
It is declared asexternal view
a function, so that when a player fromweb3.js
when you call it, it is unnecessary to spend any gas.3. The need to return a function
uint []
(uint
array).
2. Code
pragma solidity >=0.5.0 <0.6.0;
import "./zombiefeeding.sol";
contract ZombieHelper is ZombieFeeding {
modifier aboveLevel(uint _level, uint _zombieId) {
require(zombies[_zombieId].level >= _level);
_;
}
function changeName(uint _zombieId, string calldata _newName) external aboveLevel(2, _zombieId) {
require(msg.sender == zombieToOwner[_zombieId]);
zombies[_zombieId].name = _newName;
}
function changeDna(uint _zombieId, uint _newDna) external aboveLevel(20, _zombieId) {
require(msg.sender == zombieToOwner[_zombieId]);
zombies[_zombieId].dna = _newDna;
}
// Create your function here
function getZombiesByOwner(address _owner) external view returns(uint[] memory) {
}
}
Third, the declaration of an array in memory
1, to explain
Before, we had to find out about entry function modifier.
Solidity use storage
(storage) is quite expensive, "write" operation You Qigui . This is because:
Whether written or change a piece of data, which will be permanently written to the block chain. This requires that data stored on the hard disk of the world thousands of nodes, with the growth of chain blocks, number of copies more, greater storage capacity. This is a cost !
To reduce costs, not a last resort, to avoid writing data to memory. Of course, this can lead to inefficient programming logic - for example, each call to a function, you need to memory
rebuild an array (memory), rather than simply the last calculation to the storage array down for quick lookup.
In the back of the array plus memory
keyword, indicating the array is created in memory only, no need to write external storage, and at the end of the function call it disbanded. At the end of the program and the data stored into the storage
comparison of practice, memory computing can greatly reduce the cost of gas - in this array view
in use, do not need to spend money .
function getArray() external pure returns(uint[]) {
// 初始化一个长度为3的内存数组
uint[] memory values = new uint[](3);
// 赋值
values.push(1);
values.push(2);
values.push(3);
// 返回数组
return values;
}
In the course to be noted: a memory array have a length parameter (in this case 3
created). Does not currently support array.push()
such a method to adjust the array size, in future versions may support length changes.
This is also well understood, such as C ++, if we do not have the new keyword to create an array, the array length in the definition process, must be a constant. It can not be a variable.
2, combat
1. Requirements
The definition of a modifier, by passing the level
limiting parameters of zombies using some special features.
1. The statement called
result
theuint [] memory'
(memory variable array).2. set it to a new
uint
type of array. Length of the array for the_owner
number of zombies have, which can be obtained by callingownerZombieCount [_ owner]
acquire.3. The function ends and returns
result
. Currently it is just an empty columns。
2. Code
pragma solidity >=0.5.0 <0.6.0;
import "./zombiefeeding.sol";
contract ZombieHelper is ZombieFeeding {
modifier aboveLevel(uint _level, uint _zombieId) {
require(zombies[_zombieId].level >= _level);
_;
}
function changeName(uint _zombieId, string calldata _newName) external aboveLevel(2, _zombieId) {
require(msg.sender == zombieToOwner[_zombieId]);
zombies[_zombieId].name = _newName;
}
function changeDna(uint _zombieId, uint _newDna) external aboveLevel(20, _zombieId) {
require(msg.sender == zombieToOwner[_zombieId]);
zombies[_zombieId].dna = _newDna;
}
function getZombiesByOwner(address _owner) external view returns(uint[] memory) {
// Start here
uint[] memory result = new uint[](ownerZombieCount[_owner]);
return result;
}
}