3.22 以太貓原始碼分析2

尹成發表於2018-11-09

00A. Ownable 合約:提供基本的認證控制

 // 提供基本的認證控制
contract Ownable {
  address public owner; 
  /**
   * @dev The Ownable constructor sets the original `owner` of the contract to the sender
   * account.
   */
  function Ownable() {
    owner = msg.sender;
  }
  /**
   * @dev Throws if called by any account other than the owner.
   */
   // 修改器 合約所有所有者控制
  modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }
  /**
   * @dev Allows the current owner to transfer control of the contract to a newOwner.
   * @param newOwner The address to transfer ownership to.
   */
   // 轉移控制權給一個新的地址
  function transferOwnership(address newOwner) onlyOwner {
    if (newOwner != address(0)) { // 新地址不能是空
      owner = newOwner;
    }
  }
}

00B. 基因介面(這是唯一一個沒有開源的地方)

// 基因介面
contract GeneScienceInterface {
    /// @dev simply a boolean to indicate this is the contract we expect to be
    function isGeneScience() public pure returns (bool); // 是否是我們所期望的合約
    /// @dev given genes of kitten 1 & 2, return a genetic combination - may have a random factor
    /// @param genes1 genes of mom
    /// @param genes2 genes of sire
    /// @return the genes that are supposed to be passed down the child
    // 通過給定的小貓1和小貓2的基因返回他們的遺傳組合 
    function mixGenes(uint256 genes1, uint256 genes2, uint256 targetBlock) public returns (uint256);
}

00C. 訪問控制合約

contract KittyAccessControl {
    // cryptoKitties的訪問控制
     event ContractUpgrade(address newContract); // 合約更新時觸發的事件
    // The addresses of the accounts (or contracts) that can execute actions within each roles.
    address public ceoAddress;
    address public cfoAddress;
    address public cooAddress; 
    // @dev Keeps track whether the contract is paused. When that is true, most actions are blocked
    // 合約暫停狀態,暫停之後大多數行為都會被鎖定
    bool public paused = false;
    /// @dev Access modifier for CEO-only functionality
    modifier onlyCEO() { // 限定為被修飾的函式只有CEO能操作
        require(msg.sender == ceoAddress);
        _;
    }
    /// @dev Access modifier for CFO-only functionality
    modifier onlyCFO() { // 限定為被修飾的函式只有CFO能操作
        require(msg.sender == cfoAddress);
        _;
    }
    /// @dev Access modifier for COO-only functionality
    modifier onlyCOO() { // 限定為被修飾的函式只有COO能操作
        require(msg.sender == cooAddress);
        _;
    }
    // 限定為被修飾的函式只有CEO/CFO/COO中的一種角色能操作
    modifier onlyCLevel() {
        require(
            msg.sender == cooAddress ||
            msg.sender == ceoAddress ||
            msg.sender == cfoAddress
        );
        _;
    }
    /// @dev Assigns a new address to act as the CEO. Only available to the current CEO.
    /// @param _newCEO The address of the new CEO
    // 註冊新的CEO 只有當前的CEO可以操作
    function setCEO(address _newCEO) external onlyCEO {
        require(_newCEO != address(0));
        ceoAddress = _newCEO;
    }
    // 設定CFO
    function setCFO(address _newCFO) external onlyCEO {
        require(_newCFO != address(0));
        cfoAddress = _newCFO;
    }
    // 設定COO
    function setCOO(address _newCOO) external onlyCEO {
        require(_newCOO != address(0));
        cooAddress = _newCOO;
    }
    // 限定被該修飾符修飾的函式只能在非暫停狀態下才能執行
    modifier whenNotPaused() {
        require(!paused);
        _;
    }
    // // 限定被該修飾符修飾的函式只能在暫停狀態下才能執行
    modifier whenPaused {
        require(paused);
        _;
    }
    /// @dev Called by any "C-level" role to pause the contract. Used only when
    ///  a bug or exploit is detected and we need to limit damage.
    // 暫停
    function pause() external onlyCLevel whenNotPaused {
        paused = true;
    }
    // 恢復
    function unpause() public onlyCEO whenPaused {
        // can't unpause if contract was upgraded
        paused = false;
    }
}

 

相關文章