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;
}
}
-
学院Go语言视频主页
https://edu.csdn.net/lecturer/1928 -
扫码获取海量视频及源码 QQ群:721929980