Ethereum: Repetition of Guardians and CEI – Understanding the role of both modifiers
In Ethereum, modifiers are a key aspect of the intelligent development of the contract. Two specific modifiers playing a significant role in ensuring the integrity and safety of the contract are Reenrancydaard (RGC) modifier and the control effect model (CEI). In this article, we will peek into the meaning of these two modifiers and give examples that illustrate their use.
What is the Modifier of Releas Protection?
Reenrancyguard modifier is a built Ethereum decorator that prevents a contract or other function from re -call after calling the same instruction. This ensures that the contract cannot perform an endless loop, which can lead to serious locks in safety and even failure.
** Is there a need to re -use the protection modifier if the contract follows the template
No, you do not necessarily use the Reentrancygard modifier if the contract observes the control of the interaction of the control effect (CEI). The CEI model is a more advanced and elegant approach to ensuring that the impact of interaction with function has no side effects on other functions under the same contract.
In fact, the CEI sample provides a comprehensive set of guarantees of the integrity and security of the contract, which often exceeds the need for Reentrancyduard modifier. The CEI pattern contains several important rules, such as:
* Lack of only -teem : The contract cannot be called.
* No recursive calls : The contract cannot repeat the second function.
* There is no common state
: the lack of a condition is available between functions.
What is the interactions with the control effect (CEI)?
The interactions of effect control (CEI) are an interaction model that allows you to create complex contracts with many effects on other functions. CEI provides a way to express these interactions in a more elegant and more readable way, at the same time ensuring the integrity and security of the contract.
Example: Use Reenrancyguard Modifier
Consider an example of a simple contract followed by the CEI model:
`Strength
Pragma Solidity ^0.8.0;
Simplelette {contract {
// Reenrancyduard modifier is not used here.
Dosometering () public function {{
// This function cannot be called.
Demand (! Is self (), "re -eneran");
// Call another function that affects this contract.
Interaction interaction = new functioning ();
interaction.dosomethinglse ();
Iche
Isself Function () Internal View Returns (Bool) {
Return Msg.sener! = address (0);
Iche
Iche
In this example, we define a simple "simplelett" contract with two functions:dosometching ()and
Dosometching () causes a different function of “dosometchilsels (), which affects the contract.
`Strength
Pragma Solidity ^0.8.0;
Simplelette {contract {
// Use the Reenrancyguard modifier.
Dosometering () public function {{
Demand (! Is self (), "re -eneran");
// Call another function that affects this contract.
Interaction interaction = new functioning ();
interaction.dosomethinglse ();
// You can now safely call "dosometling" because we have prevented re -operation.
Iche
Isself Function () Internal View Returns (Bool) {
Return Msg.sener! = address (0);
Iche
Iche
`
app
In short, although the reenrancyguard modifier can be used to prevent infinite loops in the contract followed by the CEI model, it is not always necessary.