Learning block Chains (eight)--creating the Zombie Corps advanced Ⅲ Onlyowner modifier __ block Chain

Source: Internet
Author: User
Tags modifier rand

Before this chapter, I have to say that running in the ether Dapp and our ordinary app is a great difference, that is, the persistence of intelligent protocol , we compile the program will be permanent, immutable existence on the Ethernet, there is no like our centralized system on the possibility of continuous version of the iteration. In a way, this is extremely unfriendly to developers, and in our past development experience we've always been iterating our systems to make them more robust, and Dapp has only one chance, and it's a great test of the developer's code skills and Carefulness, So some people will say that the block chain to some extent and the Internet is mirrored relationship, but it is precisely because of this that the unique advantages of DAPP, once the contract is deployed to confirm that anyone will not have the right to change it, the program will be in accordance with the original code meticulous execution, if your contract created loopholes, I'm sorry, You only have to move your users to a new fixed contract, but the user's approximate rate is unlikely to buy two of the Dapp team that developed the vulnerability, and the famous DAO event is still vivid.

Remember before we used the zombiefeeding contract in the Kittycontract interface, will address "hard code" to our contract, if one day, Kittycontract does not exist, then our contract will not be used, The Dapp of giving birth to others is clearly fatal.

So in the beginning, we don't write the address through the variable to the contract, but through the function of the way to implement injection. Of course, it is impossible for anyone to modify the address, we should only allow the owner of the contract address to modify the address.
We can do this through the Openzeppelin Library's ownable contract, so let's take a look at Ownable.sol's source code, and with the previous basics, I think it looks pretty simple:

/** * @title ownable * @dev The ownable contract has an owner, and provides basic authorization control * FUNCT
 Ions, this simplifies the implementation of "User permissions".
   * * Contract Ownable {address public owner;

  Here is an event for the front-end monitor event ownershiptransferred (address indexed Previousowner, address indexed newowner); /** * @dev The Ownable constructor sets the original ' owner ' contract to the sender * account. This is a constructor that starts at the time of the contract
  Run once and assign the address of the contract to the address owner/function ownable () public {owner = Msg.sender; }/** * @dev Throws if called by any account than the owner.
    Here is the modifier of the modifier onlyowner, which is used to determine whether the contract Publisher */modifier Onlyowner () {require (Msg.sender = owner);
  _;
   /** * @dev allows the current owner to transfer control of the contract to a newowner.
   * @param newowner to transfer ownership.
* Allow the contract owner to modify the designation of the new contract owner and invoke the event to monitor/function transferownership (address Newowner) public Onlyowner {    Require (Newowner!= address (0));
    Ownershiptransferred (owner, Newowner);
  Owner = Newowner;
 }

}

OK, so let's introduce a function to specify the address of the kitten, to refer to the ownable contract in the Zombiefactory, to create the Setkittycontractaddress function in the zombiefeeding contract, and to modify it with Onlyowner:

pragma solidity ^0.4.19;

Import "./ownable.sol";

    Contract Zombiefactory is ownable {event Newzombie (UINT Zombieid, string name, uint DNA);
    UINT dnadigits = 16;

    UINT Dnamodulus = dnadigits;
        struct Zombie {string name;
        UINT DNA;

    ADD new data here} zombie[] public zombies;
    Mapping (UINT => address) public zombietoowner;

    Mapping (address => uint) Ownerzombiecount;
        function _createzombie (string _name, uint _dna) Internal {UINT ID = Zombies.push (Zombie (_name, _dna))-1;
        Zombietoowner[id] = Msg.sender;
        ownerzombiecount[msg.sender]++;
    Newzombie (ID, _name, _dna);
        function _generaterandomdna (string _str) Private View Returns (UINT) {UINT Rand = UINT (keccak256 (_STR));
    Return to Rand% Dnamodulus;
        function Createrandomzombie (string _name) public {require (ownerzombiecount[msg.sender] = = 0); UINT Randdna = _generaterandomdna (_name);
        Randdna = randdna-randdna% 100;
    _createzombie (_name, Randdna); }

}
pragma solidity ^0.4.19;

Import "./zombiefactory.sol"; 
    Contract Kittyinterface {function Getkitty (uint256 _id) external view Returns (bool isgestating, BOOL IsReady, uint256 Cooldownindex, uint256 Nextactionat, uint256 Siringwithid, uint256 birthtime, uint256 Matroni
D, uint256 Sireid, uint256 generation, uint256 genes);

  } contract zombiefeeding is zombiefactory {kittyinterface kittycontract;
  function setkittycontractaddress (address _address) external Onlyowner {kittycontract = Kittyinterface (_address); function feedandmultiply (UINT _zombieid, uint _targetdna, string species) public {require (Msg.sender = = Zombieto
    Owner[_zombieid]);
    Zombie storage Myzombie = Zombies[_zombieid];
    _targetdna = _targetdna% Dnamodulus;
    UINT Newdna = (Myzombie.dna + _targetdna)/2;
    if (keccak256 (species) = = keccak256 ("kitty")) {Newdna = Newdna-newdna% 100 + 99;
  } _createzombie ("NoName", Newdna);

 } function Feedonkitty (UINT _zombieid, uint _kittyid) public {uint Kittydna;
    (,,,,,,,,, Kittydna) = Kittycontract.getkitty (_kittyid);
  Feedandmultiply (_zombieid, Kittydna, "Kitty"); }

}

Don't tell me you forgot external keyword is used to do, it means that this function can only be called by contract outside of the contract oh ...

There is no feeling that this onlyowner is the back door feeling, so you can only contract to be recognized by the public, these backdoor are necessary, and remember to have to open the GitHub on their ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.