區塊鏈:單位(Units) 和 全局變量(Globally Available Variables)

Ether Units

一個整數的後面可以跟一個單位,etherfinneyszabo或者wei

他們的單位換算如下:

1 ether = 1000 finney
1 ether = 1000000 szabo 1
ether = 10 ** 18 wei

Time Units

時間的單位有seconds, minutes, hours, days, weeksyears。換算如下:

1 == 1 seconds
1 minutes == 60 seconds
1 hours == 60 minutes
1 days == 24 hours
1 weeks == 7 days
1 years == 365 days

特殊的變量和函數和函數

有一些特殊的變量和函數存在於全局的命名空間以提供區塊相關信息。

區塊和交易屬性

  • block.blockhash(uint blockNumber) returns (bytes32): 某個區塊的區塊鏈hash值
  • block.coinbase (address): 當前區塊的挖礦地址
  • block.difficulty (uint): 當前區塊的難度
  • block.gaslimit (uint): 當前區塊的gaslimit
  • block.number (uint): 當前區塊編號
  • block.timestamp (uint): 當前區塊時間戳
  • msg.data (bytes): 參數
  • msg.gas (uint): 剩餘的gas
  • msg.sender (address): 當前發送消息的地址
  • msg.sig (bytes4): 方法ID
  • msg.value (uint): 伴隨消息附帶的以太幣數量
  • now (uint): 時間戳,等價於block.timestamp (uint)
  • tx.gasprice (uint): 交易的gas單價
  • tx.origin (address):交易發送地址

    錯誤處理

  • assert(bool condition):不滿足條件,將拋出異常

  • require(bool condition):不滿足條件,將拋出異常
  • revert() 拋出異常

在Solidity 0.4.10版本之前,使用throw來處理異常。如下所示:

contract HasAnOwner {

    address owner;

    function useSuperPowers(){ 
        if (msg.sender != owner) { 
            throw; 
        }
    }
}

Solidity 0.4.10版本之後,我們通常如下使用:

  • if(msg.sender != owner) { revert(); }
  • assert(msg.sender == owner);
  • require(msg.sender == owner);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章