Cryptokitties (ether square cloud cat) is a recent game on the chain of Etheric square blocks. Super cute cat image, and then with the breeding, breeding, auction such a rich game, all of a sudden detonated the block chain of the etheric square. The core of this game is the intelligent contract based on Ethernet, and the first intelligent contract based game. In addition to the game in the breeding of genetic engineering contract, other contracts are open source, and her source for the study of intelligent contract writing has great significance. Here is a little bit of my understanding of the smart contract and Cryptokitties source, to share with you.
(This article assumes that you already have some programming experience and a certain understanding of the Cryptokitties game rules.)
I am a small Siamese, sold only 0.3ETH, welcome to contact Intelligent Contract Brief
What a smart contract is. A smart contract is a piece of code that is placed on a chain of etheric square blocks.
How the smart contract is executed. The smart contract was executed when the money was punched into the smart contract and the code that invoked the smart contract was added to the notes. (All smart function calls are essentially transfers)
Where the smart contract is executed. The intelligent contract is performed by the miners and the execution results are recorded on the block chain.
What is the difference between the contract keyword and the class keyword in other languages. In fact, the two concepts are similar, the difference is that when using contract to create a smart contract, you only create a contract instance, the block chain returns the address of this instance. Like one of Cryptokitties's contract addresses 0X06012C8CF97BEAD5DEAE237070F9587F8E7A266D
Using pseudo Chinese code to understand is the block chain of Etheric Square. Create a smart contract (' Smart Contract Code ') return to the contract address 0x06012c8cf97bead5deae237070f9587f8e7a266d
What is the event keyword. The event keyword defines an incident type in which an external program can listen for an event to get the change on the smart contract (The Observer model), and the Word event keyword gets through the channel of the Intelligent contract State and the external program.
What is the View keyword. Beginners should remember that from the smart contract to read the data is not money and do not send transactions, you are essentially reading your computer or other people on the machine block chain data, do not modify the block chain. The View keyword tells you that this function is only used to read data without modifying the block chain.
What is a Msg object. Every time you call a contract, there is a Msg object, which is placed, the sender's address (sender attribute), how much money is sent (the value attribute), and so on. This object is predefined for each function. If you don't understand, you may be confused by the object that comes out of nowhere.
More details can be read on the website of the etheric square. ownable Contract
This is a very simple smart contract that defines ownership, and her role resembles an abstract base class, and subsequent classes simplify the code by inheriting her.
Modifier is also the ether keyword, defining a modifier that she writes at the end of the function, and executes the code in the modifier before the function executes. General modifier codes are permission checks, and so on.
For example, this code, used to transfer the ownership of the contract, Onlyowner modifier will be executed first, because only the person who really owns the contract has the right to hand over the contract. ERC721 Contract
ERC is the abbreviation of the Ethernet square token, ERC721 is one of the standard of the block chain in the ether square. The more common is ERC20. This standard is equivalent to defining an interface, and when your contract implements the interface, the wallet software can manipulate your ERC721 tokens by calling these interfaces, listening to events, and so on. In other programming languages, such as the Java Struts Framework, which defines the action interface, you access the framework of struts by implementing the action interface. or Linux development, you have to write a driver, you have to implement file_operations in the various reading and writing functions.
What's the difference between ERC721 and ERC20 tokens?
The core of ERC721 is "non-fungible tokens", not interchangeable tokens. How to understand "not interchangeable". For example you have 2 cats (cat A and Cat B), your token number is 2, but cat A and cat B are different, when you sell your cat, you must specify that the cat is not sold, because Cat A and cat B are not interchangeable. Analogy ERC20, just like you have 2 dollars, these two dollars, you spend any one of the money, do not affect the result, as long as the account is deducted a piece of money can be. ERC721 each token has a separate unique Tokenid, this ID in this cryptokitties is the cat ID, such as the following link "103646" is actually the cat's Tokenid.
https://www.cryptokitties.co/kitty/103646
From this transfer function, it can be seen that each transfer can only be transferred to a specific cat (no number). genescienceinterface Contract
The contract has no source code, and it is clear from the name that the cat's genetic engineering is used to determine the genes of the new born cat. Here only defines an interface, no concrete implementation, subsequent calls see subsequent code analysis.
Kittyaccesscontrol Contract
The Require keyword (function), which resembles an assert in many programming languages, terminates the execution of a contract if the condition in parentheses is false.
Equivalent to if state = = False:exit (), before the ether to exit the process will take away your handling fees, now the hard part of the upgrade, just charge to run to the handling fee here, thus greatly saving the expenses of the fees.
The contract is also a similar abstract base class contract that defines the cat's access control, which defines 3 roles, Ceo,cfo,coo.
Defines some modifiers, such as Onlyceo only the CEO operation method, ONLYCFO is only the CFO operation method.
The game itself can be paused
Game operators can suspend the game, many of the following game methods have the whennotpaused modifier, this modifier in a very concise way to deal with the game pause, avoid a lot of code duplication.
It wrote a lot of the topic of getting into cryptokitties at last. kittybase Contract
First, it inherits Kittyaccesscontrol so that it can directly use a bunch of control modifiers previously defined above. Then take a look at two events:
Birth event external program by focusing on the event, when a cat is born, it can be notified from the block chain. Cryptokitties's external program focuses on this event, and then sends a message to you when a new cat is born. The parameters on the birth event are for external use, and the block chain will tell you who owner, who has been the new Cat (Kittyid), the cat's father mother who (Sireid,matronid), the gene genes (what)
The transfer event is used to inform the cat of the hands. The core data structure of cats
The data structure of the cat
The kitty structure is a data representation of a cat stored on a block chain. Cryptokitties is to do all kinds of cat-related business by manipulating the properties of this data structure.
Generation Cat algebra, the system-generated cat is Gen 0 for the No. 0 generation. Later generations, algebra will grow slowly.
Genes cat's genes determine the cat's appearance, but it should be noted that the chain is stored in the Ethernet block is only a genetic data, the specific display, itself or by the Cryptokitties server to provide data, that is, if the cryptokitties shut down the server, Your cat is just this 256-digit number.
Birthtime Birth time, this attribute is not useful
How long Cooldownendblock can be bred, on the Etheric Square, the operation of time, most of which are expressed in chunks. For example, when the block number is 1000, 1 block time is 15 seconds, then I said, the cat to wait 1 minutes after the breeding, then in fact, is to wait for 4 blocks (60 seconds/15) After, that is, 1004th blocks, in the number of 1005th out, you can carry out breeding.
Matronid,sireid Matronid says the cat's cat's mother's Id,sireid represents the cat's father ID. Since the first generation of cats is no cat mom and Dad cat (is the development team to come out) so the first generation ID is 0.
Siringwithid Spouse ID, this game, the cat is not gender, when you participate in mating, play the side of the cat to take the birth of kittens, playing the industry and trade party to take the breeding fee. Siringwithid!=0 said the cat was breeding, and Siringwithid==0 said the cat was empty-nest.
Cooldownindex mating cooling time when a cat was clicked, this attribute determines its remaining speed level.
It is used in conjunction with this table below.
Cooldownindex = 0 for 1 minutes is the fast,1 on behalf of Swift.
The following code defines the core data of the cat business.
Mapping is a data structure of the etheric square intelligent contract, it is the same as the dict in Hashmap,python in Java, it is a hash table. This hash table is stored in the etheric square intelligent contract.
Kittes Array, all cats will be stored in the kitties array, this array will grow, Kittyid is actually the kitty object in this array subscript. And Kittyid is actually erc721 's tokenid. The kitties array establishes the mapping relationship between the erc721 token and the CAT data structure. The Kitty object data can be obtained through the erc721 Tokenid.
Kittyindextoowner table, through this mapping, you can quickly know, who is the cat. So why is the owner attribute not in Kitty's data structure? The author speculated that many scenes do not need to obtain the specific data on Kitty, if the first to find the cat to obtain the owner, the efficiency may be lower, after all, the implementation of intelligent contract is to be money.
Ownershiptokencount the table and recorded who had several cats.
kittyindextoapproved table, recording a licensing operation, you can authorize your cat to an address, such as your auction, your cat is actually authorized to the auction contract, the contract has the right to transfer your cat to others.
Sireallowedtoaddress Watch, you can take your cat to the breeding, the other side and your cat breeding, must get your authorization, then this table records, you have your kind of cat to allow who breeding.
_transfer function This function describes the core operation of a cat assignment. _from is the original holder, _to is the one you want to transfer to WHO, Tokenid represents what cat. First, the number of the other cats to increase by 1, the Second Amendment Kittyindextoowner in the owner of the cat assets. This function also determines whether the From address is 0 because the cat may have been created by the system, and that from is 0. If from is a user, then the number of his cats should be reduced by 1, and then the relevant breeding power and transfer authority will be cleared. Finally through Transfer (_from, _to, _tokenid); Send Cat transfer events, let the external system to notify and so on.
_createkitty to create a cat's core function, the first interesting thing is Cooldownindex's handling, that is, the new cat's breeding speed is directly related to his first generation of cats, the earlier generation of cat breeding cooling events lower. The newly created Kitty object is then put into the kitty array, and its kittyid is obtained. Simultaneously sends the birth event, the system listens to this event, sends you Your the Kitty is born mail. At the same time, call the _transfer internal function, assign the ownership of this cat, because it is not transferable between the user and the user, so _from is 0 to express the system transfer to you.
kittyownership Contract
Several of the above contracts, in fact, are abstract base classes, and not deployed in the ether, they are in order to save the code to draw out. The kittyownership contract is a contract that is really deployed on the etheric Square, and he defines a ERC721 token. Kittyownership the Kittybase and ERC721 interface Bridge. Inside a lot of code, basically are Kittybase contract interface conversion, and there is no very special logic, we can read.
Just pick this method and talk about it, this method describes how many cats you have, he goes through all the current cats, and then kittyindextoowner to see if the cat is yours, because you have a few cats before, so find all your cats and stop without walking through all the cat collections. But this function is still very expensive, notes authors write, this function should not be called by the intelligent contract, this function should be used locally to find the block chain. His effect is the page's my kitties button.
The author writes here has collapsed, the cat breeding and the auction, next time talks again, welcome likes the programming and the block chain friend pays attention to me.
Turn from: http://www.icokan.com/portal.php?mod=view&aid=6052