This is the 10th article in the Solidity Tutorial series, with a full understanding of solidity's function modifier.
For a complete list of articles in the solidity series, see Category-solidity.
Write in front
Solidity is the Ethereum Smart Contract programming language, you should know about Ethereum and smart contracts before reading this article.
If you don't understand, it's recommended that you look at Ethereum first.
Functions modifier (function Modifiers)
The function modifier (Modifiers) can be used to change the behavior of a function. For example, to check for a pre-condition before the function executes.
If you are familiar with Python, you will find that the function modifier is similar to the Python decorator.
A modifier is a contract override (override) that can be inherited from a contract property and can also be inherited. Let's look at a sample code:
pragma solidity ^0.4.11;contract owned {function owned () public {owner = Msg.sender;} Address owner; When a function modifier is defined that can be inherited//modified, the function body is inserted into the "_;"//when the condition is not met, an exception is thrown modifier Onlyowner {require (Msg.sender = = Owner ); _; }}contract Mortal is owned {//use inherited ' Onlyowner ' function close () public Onlyowner {selfdestruct (owner); }}contract priced {//function modifier can receive parameters modifier costs (uint price) {if (Msg.value >= price) {_; }}}contract Register is priced, owned {mapping (address = bool) registeredaddresses; UINT Price; function Register (UINT initialprice) Public {price = Initialprice;} A payable is required to accept the Ether function register () public payable costs (price) {Registeredaddresses[msg.sender] = true; } function Changeprice (UINT _price) public Onlyowner {price = _price; }}
The above onlyowner is the definition of a function modifier, when using this modifier area to decorate a function, the function must meet the conditions of Onlyowner to run, the condition is: The contract must be created to call the function, otherwise throw an exception.
We have used this function modifier in a token article that implements advanced functions such as managing, offering, redeeming, and freezing.
Multiple modifiers
If there are multiple modifiers for the same function, they are separated by a space, and the decorator checks execution in turn.
An explicit return statement in a modifier or within a function simply jumps out of the current modifier or function. The returned variable is assigned a value, but the execution flow continues after the "_" defined after the previous modifier, such as:
contract Mutex { bool locked; modifier noReentrancy() { require(!locked); locked = true; _; locked = false; } // 防止递归调用 // return 7 之后,locked = false 依然会执行 function f() public noReentrancy returns (uint) { require(msg.sender.call()); return 7; }}
The modifier's parameters can be any expression. In this context, all the symbols introduced in the function are visible in the modifier. However, the symbols introduced in the modifier are not visible in the function because they may be overridden.
In-depth understanding of the execution order of modifiers
Take a closer look at a more complicated example to understand the modifier:
pragma solidity ^0.4.11;contract modifysample { uint a = 10; modifier mf1 (uint b) { uint c = b; _; c = a; a = 11; } modifier mf2 () { uint c = a; _; } modifier mf3() { a = 12; return ; _; a = 13; } function test1() mf1(a) mf2 mf3 public { a = 1; } function test2() public constant returns (uint) { return a; } }
When the above smart contract runs Test1 (), what is the value of the state variable A, 1, 11, 12, or 13?
The answer is 11, you can run the next test2 to get the next a value.
Let's analyze test1, which expands after this:
uint c = b; uint c = a; a = 12; return ; _; a = 13;c = a;a = 11;
At this point at a glance, the last A is 11, note that the 5th and 6th lines are not executed.
Reference documents
Official Document-function Modifiers
In-depth blockchain-the system learns blockchain to create the best blockchain technology blog.
Smart Contract Language Solidity tutorial Series 10-Full understanding of function modifiers