In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the Solidity function modifier". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the Solidity function modifier".
Function modifier (Function Modifiers)
The function modifier (Modifiers) can be used to change the behavior of a function. For example, it is used to check certain preconditions before the function is executed.
If you are familiar with Python, you will find that the function modifier is very similar to Python's decorator.
A modifier is a contract attribute that can be inherited as well as contract rewriting (override). Let's take a look at a sample code:
Pragma solidity ^ 0.4.11 X contract owned {function owned () public {owner = msg.sender;} address owner; / / defines a function modifier that can be inherited / / modified when the function body is inserted at "_;" / / does not meet the condition, the exception modifier onlyOwner {require (msg.sender = = owner) will be thrown; }} 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 Ethernet function register () public payable costs (price) {registeredAddresses [msg.sender] = true;} function changePrice (uint _ price) public onlyOwner {price = _ price;}}
OnlyOwner is a function modifier defined above. When you modify a function with this modifier area, the function must meet the conditions of onlyOwner before it can be run. The condition here is that the function can be called only when the contract is created, otherwise an exception is thrown. We use this function modifier in a token article that implements advanced functions such as manageability, additional issuance, exchange, freeze, and so on.
Multiple modifier
If there are multiple modifiers for the same function, they are separated by spaces, and the modifier checks and executes in turn.
An explicit return statement in a modifier or function that simply jumps out of the current modifier or function. The returned variable is assigned, 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;} / / prevent recursive calls to / / return 7, locked = false will still execute function f () public noReentrancy returns (uint) {require (msg.sender.call ()); return 7;}}
The parameters of the modifier can be any expression. In this context, the symbols introduced in all functions are visible in the modifier. However, symbols introduced in modifiers are not visible in functions because they may be overridden.
In-depth understanding of the order in which modifiers are executed
Let's look at a more complex example to gain an in-depth understanding of modifiers:
Pragma solidity ^ 0.4.11 modifier mf3 contract {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;}}
After 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 test2 to get the next a value.
Let's take a look at test1. When extended, it looks like this:
Uint c = b; uint c = a Tinca = 12; return; _; a = 13 Ten c = a Tian a = 11
At this time, it is clear at a glance, and the final an is 11. Note whether lines 5 and 6 are executed.
Thank you for your reading, the above is the content of "how to use the Solidity function modifier". After the study of this article, I believe you have a deeper understanding of how to use the Solidity function modifier, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.