美文网首页程序员以太坊炸鸡
Solidity之Modifier(还有那个酷酷的 _ )

Solidity之Modifier(还有那个酷酷的 _ )

作者: charlieyan | 来源:发表于2018-01-24 10:42 被阅读0次
pragma solidity ^0.4.11;

contract owned {
    function owned() public { owner = msg.sender; }
    address owner;

    // This contract only defines a modifier but does not use
    // it: it will be used in derived contracts.
    // The function body is inserted where the special symbol
    // `_;` in the definition of a modifier appears.
    // This means that if the owner calls this function, the
    // function is executed and otherwise, an exception is
    // thrown.
    定义了一个modifier但是没有使用,将在继承的合约中使用
    函数体将在特殊符号 _ 出现的位置被插入
    这里代表的是只有Owner调用这个方法时才会被执行,否则报错
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
}

contract mortal is owned {
    // This contract inherits the `onlyOwner` modifier from
    // `owned` and applies it to the `close` function, which
    // causes that calls to `close` only have an effect if
    // they are made by the stored owner.
    这个合约从owned继承了onlyOwner的modifier,应用在close方法上
    这将造成只有Owner才能调用close方法
    function close() public onlyOwner {
        selfdestruct(owner);
    }
}

contract priced {
    // Modifiers can receive arguments:
    modifiers可以接受参数
    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; }

    // It is important to also provide the
    // `payable` keyword here, otherwise the function will
    // automatically reject all Ether sent to it.
    function register() public payable costs(price) {
        registeredAddresses[msg.sender] = true;
    }

    function changePrice(uint _price) public onlyOwner {
        price = _price;
    }
}

contract Mutex {
    bool locked;
    modifier noReentrancy() {
        require(!locked);
        locked = true;
        _;
        locked = false;
    }

    /// This function is protected by a mutex, which means that
    /// reentrant calls from within `msg.sender.call` cannot call `f` again.
    /// The `return 7` statement assigns 7 to the return value but still
    /// executes the statement `locked = false` in the modifier.
    function f() public noReentrancy returns (uint) {
        require(msg.sender.call());
        return 7;
    }
}

那么modifier在solidity中做的是什么工作呢?
答案就是给继承这个modifier修饰的function加上一个特定的约束,比如

  modifier isOwner() {
     if (msg.sender != owner) {
          throw;
      }
      _; // 继续执行余下的代码体(其实就是isOwner里的实际代码)
  }
  
  doSomething() isOwner {
    // 将会检查调用者是不是Owner
  
    // code
  }

相关文章

  • Solidity之Modifier(还有那个酷酷的 _ )

    那么modifier在solidity中做的是什么工作呢?答案就是给继承这个modifier修饰的function...

  • 浅谈Solidity: 13. 继承

    solidity中的继承(inheritance),包括简单继承,多重继承,以及修饰器(modifier)和构造函...

  • 那个酷酷的女孩

    小W是个从小就酷酷的女孩子,小学的时候就留着寸头,戴着耳钉,拽的二五八百的样子,还喜欢斜眼看人,动不动翻白眼,除了...

  • 罗韭菜的超详细dapp从零实战(二)-- 编写智能合约与truf

    使用solidity语言撰写智能合约 Ethereum上的智能合约需要使用solidity语言来撰写。虽然还有其他...

  • 小小的我

    我可以不酷, 但我心中有个酷酷的世界, 里面有毛绒玩具、小汽车, 还有那个小小的我

  • Modifier

    item类型font原地 modifierforegroundColor原地 modifierframe原地 mo...

  • SwiftUI 学习笔记-01 自定义Modifier开始

    自定义Modifier开始 新建 SwiftUI File -> Modifier.swift 使用

  • 那个酷酷的女孩(1)

    至于是如何和小眉认识的,已经有些记不清楚了,只记得那天太阳当空照,花儿对我笑,小鸟说『操』『操』『操』。——喔,不...

  • 那个酷酷的女孩(5)

    我目瞪口呆地看着她,实未料到美女也会说粗话。 “喂!别这么看着我。”她一边从红樱桃之唇内吐出丁香之舌品味着如雪美味...

  • 那个酷酷的女孩(2)

    我的第二个反应是,赶忙把第二个扣子给扣了起来。 我的第三个反应是,脑袋嗡地一声……世界果然大不同,居然有女子主动找...

网友评论

    本文标题:Solidity之Modifier(还有那个酷酷的 _ )

    本文链接:https://www.haomeiwen.com/subject/hjgyaxtx.html