Solidity之Modifier(還有那個(gè)酷酷的 _ )

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.
    定義了一個(gè)modifier但是沒(méi)有使用肃拜,將在繼承的合約中使用
    函數(shù)體將在特殊符號(hào) _ 出現(xiàn)的位置被插入
    這里代表的是只有Owner調(diào)用這個(gè)方法時(shí)才會(huì)被執(zhí)行晃听,否則報(bào)錯(cuò)
    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.
    這個(gè)合約從owned繼承了onlyOwner的modifier鲜侥,應(yīng)用在close方法上
    這將造成只有Owner才能調(diào)用close方法
    function close() public onlyOwner {
        selfdestruct(owner);
    }
}

contract priced {
    // Modifiers can receive arguments:
    modifiers可以接受參數(shù)
    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中做的是什么工作呢涯雅?
答案就是給繼承這個(gè)modifier修飾的function加上一個(gè)特定的約束外构,比如

  modifier isOwner() {
     if (msg.sender != owner) {
          throw;
      }
      _; // 繼續(xù)執(zhí)行余下的代碼體(其實(shí)就是isOwner里的實(shí)際代碼)
  }
  
  doSomething() isOwner {
    // 將會(huì)檢查調(diào)用者是不是Owner
  
    // code
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末帆吻,一起剝皮案震驚了整個(gè)濱河市域那,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌猜煮,老刑警劉巖次员,帶你破解...
    沈念sama閱讀 221,888評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異王带,居然都是意外死亡淑蔚,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門愕撰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)刹衫,“玉大人,你說(shuō)我怎么就攤上這事搞挣〈伲” “怎么了?”我有些...
    開封第一講書人閱讀 168,386評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵囱桨,是天一觀的道長(zhǎng)仓犬。 經(jīng)常有香客問(wèn)我,道長(zhǎng)舍肠,這世上最難降的妖魔是什么搀继? 我笑而不...
    開封第一講書人閱讀 59,726評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮貌夕,結(jié)果婚禮上律歼,老公的妹妹穿的比我還像新娘。我一直安慰自己啡专,他們只是感情好险毁,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般畔况。 火紅的嫁衣襯著肌膚如雪鲸鹦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,337評(píng)論 1 310
  • 那天跷跪,我揣著相機(jī)與錄音馋嗜,去河邊找鬼。 笑死吵瞻,一個(gè)胖子當(dāng)著我的面吹牛葛菇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播橡羞,決...
    沈念sama閱讀 40,902評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼眯停,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了卿泽?” 一聲冷哼從身側(cè)響起莺债,我...
    開封第一講書人閱讀 39,807評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎签夭,沒(méi)想到半個(gè)月后齐邦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,349評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡第租,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評(píng)論 3 340
  • 正文 我和宋清朗相戀三年措拇,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片煌妈。...
    茶點(diǎn)故事閱讀 40,567評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡儡羔,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出璧诵,到底是詐尸還是另有隱情,我是刑警寧澤仇冯,帶...
    沈念sama閱讀 36,242評(píng)論 5 350
  • 正文 年R本政府宣布之宿,位于F島的核電站,受9級(jí)特大地震影響苛坚,放射性物質(zhì)發(fā)生泄漏比被。R本人自食惡果不足惜章姓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評(píng)論 3 334
  • 文/蒙蒙 一备籽、第九天 我趴在偏房一處隱蔽的房頂上張望原环。 院中可真熱鬧蚕钦,春花似錦风宁、人聲如沸旁钧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至噪裕,卻和暖如春蹲盘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背膳音。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工召衔, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人祭陷。 一個(gè)月前我還...
    沈念sama閱讀 48,995評(píng)論 3 377
  • 正文 我出身青樓苍凛,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親兵志。 傳聞我的和親對(duì)象是個(gè)殘疾皇子毫深,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評(píng)論 2 359