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;
    }
}

 

3.22 以太猫源码分析2

相关文章:

  • 2021-04-26
  • 2021-08-07
  • 2021-08-07
  • 2021-09-06
  • 2021-04-27
  • 2021-08-13
  • 2021-10-11
  • 2021-08-18
猜你喜欢
  • 2021-04-06
  • 2021-05-27
  • 2021-07-17
  • 2021-11-04
  • 2021-12-16
  • 2022-01-16
  • 2021-10-27
相关资源
相似解决方案