本文節選自電子書《Netkiller Blockchain 手劄》
Netkiller Blockchain 手劄
本文作者最近在找工作,有意向緻電 13113668890
Mr. Neo Chan, 陳景峯(BG7NYT)
中國廣東省深圳市龍華新區民治街道溪山美地
518131
+86 13113668890
文檔始創于2018-02-10
版權 © 2018 Netkiller(Neo Chan). All rights reserved.
版權聲明
轉載請與作者聯系,轉載時請務必标明文章原始出處和作者資訊及本聲明。
http://www.netkiller.cnhttp://netkiller.github.iohttp://netkiller.sourceforge.net | http://www.netkiller.cn | http://netkiller.github.io | http://netkiller.sourceforge.net | 微信訂閱号 netkiller-ebook (微信掃描二維碼)QQ:13721218 請注明“讀者”QQ群:128659835 請注明“讀者” | 微信訂閱号 netkiller-ebook (微信掃描二維碼) | QQ:13721218 請注明“讀者” | QQ群:128659835 請注明“讀者” |
---|---|---|---|---|---|---|---|
$Data$
内容摘要
這一部關于區塊鍊開發及運維的電子書。
為什麼會寫區塊鍊電子書?因為2018年是區塊鍊年。
這本電子書是否會出版(紙質圖書)? 不會,因為網際網路技術更疊太快,紙質書籍的内容無法實時更新,一本書動辄百元,很快就成為垃圾,你會發現目前市面的上區塊鍊書籍至少是一年前寫的,内容已經過時,很多例子無法正确運作。是以我不會出版,電子書的内容會追逐技術發展,及時跟進軟體版本的更新,做到内容最新,至少是主流。
這本電子書與其他區塊鍊書籍有什麼不同?市面上大部分區塊鍊書籍都是用2/3去講區塊鍊原理,隻要不到 1/3 的幹貨,幹貨不夠理論來湊,通篇将理論或是大談特談區塊鍊行業,這些内容更多是頭腦風暴,展望區塊鍊,均無法落地實施。本書與那些書籍完全不同,不講理論和原理,面向應用落地,注重例子,均是幹貨。
電子書更新頻率?每天都會有新内容加入,更新頻率最遲不會超過一周,更新内容請關注 https://github.com/netkiller/netkiller.github.io/commits/master
本文采用碎片化寫作,原文會不定期更新,請盡量閱讀原文。
http://www.netkiller.cn/blockchain/index.html
您的打賞是我的寫作動力:http://www.netkiller.cn/blockchain/donations.html
-------------------------
33.4. 遊戲領域區塊鍊探索
如何将區塊鍊嫁接到遊戲領域,我做了很多思考,經過分析總結,發現下面幾項内容非常适合上鍊。
上鍊内容
-
積分代币
如果說區塊鍊應用于遊戲領域,可能99%的人首先會想到是代币,的确遊戲領域實施區塊鍊連,代币必不可少。但是區塊鍊不等于代币。
- 遊戲裝備
- 人物屬性
- 關卡任務
下面我們要思考為什麼需要将遊戲資料放到區塊鍊上,玩遊戲的人都知道私服,私人架設遊戲伺服器,私服上玩遊戲遇到最大的問題就是公平性。管理者可以随意調整伺服器參數。
私服存在哪些問題呢?
- 修改遊戲裝備屬性
- 修改生命與魔法值
- 關卡參數
- 随意封賬号
這是我們在私服上遇到的最大問題,那麼官方伺服器就公平嗎?不一定,對于弱勢的玩家隻能相信遊戲公司的承諾。
有了區塊鍊技術,我們能做什麼呢?例如我們将使用者裝備資料等資料上鍊,這樣保證了裝備永遠屬于玩家
區塊鍊能做什麼?
- “點” 獎勵采用代币實作,可以實作流通,兌換,消費等等......
- 爆出裝備立即上鍊
- 使用者等級屬性上鍊
- 使用者狀态上鍊
- 關卡資料上鍊
了凸顯公平性,我們采用公鍊,查詢使用者資料可以使用接口,也可以直接到公鍊上查詢。
下面詳細講解具體怎麼實作。
33.4.1. 遊戲代币
傳統币 Point (點) 僅僅是一個數字,數字存在資料庫中,例如
Username | Point (Integer) ----------------------- Neo | 1000 Jam | 500
因為僅僅是一個數字,管理者可以随意修改,黑客也可随意修改,例如
update member set point = 1000000000000 where username = 'Neo'
瞬間就有 1000000000000 點。由于是在資料庫中修改,沒有日志,不知道誰操作的,可能是開發人員,可以是管理者,也可能是黑客。
如何消費“點呢”,例如消費 100 個點:
update member set point = point - 100 where username = 'Neo'
傳統币“點”,隻是一個數字做加法和減法運算,安全性主要依賴于開發團隊的能(期望别出BUG),運維團隊的能力(被别黑客攻擊),以及DBA(資料庫管理者)的節操。
審計全靠開發人員列印出的日志。
現在我們再看看數字貨币,跟很多朋友聊天中發現,他們還沒有了解什麼是币,他們認為數字代币花掉就沒了(消失了),然後通過挖礦不停的産生新币,這種了解完全錯誤。
數字币是這樣運作的,首先發行時設定了币的總量例如 1000000,然後将所有代币存入發行者賬号,例如 account 1
account | coin --------------------------------- account1 | 1000000 account2 | 0 account3 | 0 account4 | 0 account5 | 0
現在 account2 遊戲線上1小時獎勵 10 個币,系統從賬号account1轉賬5個币給 account2
account | coin --------------------------------- account1 | 999990 account2 | 10 account3 | 0 account4 | 0 account5 | 0
以此類推,從 account1 轉賬給其他賬号。
account | coin --------------------------------- account1 | 999960 account2 | 10 account3 | 10 account4 | 10 account5 | 10
現在 account3 消費 5個币買了裝備。從 account3 轉 5 個币到 account1
account | coin --------------------------------- account1 | 999965 account2 | 10 account3 | 5 account4 | 10 account5 | 10
現在你應該看懂了把,代币是流通的,總量是不變的。account1 賬号負責币的發行,回收等等工作。
同時任何轉賬将産生區塊,曆史資料永久記錄。
下面是一個進階代币合約,位址
https://github.com/ibook/NetkillerAdvancedTokenpragma solidity ^0.4.20; /******************************************/ /* Netkiller ADVANCED TOKEN */ /******************************************/ /* Author netkiller <[email protected]> */ /* Home http://www.netkiller.cn */ /* Version 2018-03-05 */ /* Version 2018-03-06 - Add Global lock */ /******************************************/ interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; } contract NetkillerAdvancedToken { address public owner; // Public variables of the token string public name; string public symbol; uint8 public decimals = 2; // 18 decimals is the strongly suggested default, avoid changing it uint256 public totalSupply; uint256 public sellPrice; uint256 public buyPrice; // This creates an array with all balances mapping (address => uint256) public balanceOf; mapping (address => mapping (address => uint256)) public allowance; // This generates a public event on the blockchain that will notify clients event Transfer(address indexed from, address indexed to, uint256 value); // This notifies clients about the amount burnt event Burn(address indexed from, uint256 value); event Approval(address indexed owner, address indexed spender, uint256 value); mapping (address => bool) public frozenAccount; /* This generates a public event on the blockchain that will notify clients */ event FrozenFunds(address target, bool frozen); bool lock = true; /** * Constrctor function * * Initializes contract with initial supply tokens to the creator of the contract */ function NetkillerAdvancedToken( uint256 initialSupply, string tokenName, string tokenSymbol ) public { owner = msg.sender; totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens name = tokenName; // Set the name for display purposes symbol = tokenSymbol; // Set the symbol for display purposes } modifier onlyOwner { require(msg.sender == owner); _; } modifier isLock { require(!lock); _; } function setLock(bool _lock) onlyOwner { lock = _lock; } function transferOwnership(address newOwner) onlyOwner public { owner = newOwner; } /* Internal transfer, only can be called by this contract */ function _transfer(address _from, address _to, uint _value) isLock internal { require (_to != 0x0); // Prevent transfer to 0x0 address. Use burn() instead require (balanceOf[_from] >= _value); // Check if the sender has enough require (balanceOf[_to] + _value > balanceOf[_to]); // Check for overflows require(!frozenAccount[_from]); // Check if sender is frozen require(!frozenAccount[_to]); // Check if recipient is frozen balanceOf[_from] -= _value; // Subtract from the sender balanceOf[_to] += _value; // Add the same to the recipient Transfer(_from, _to, _value); } /** * Transfer tokens * * Send `_value` tokens to `_to` from your account * * @param _to The address of the recipient * @param _value the amount to send */ function transfer(address _to, uint256 _value) public { _transfer(msg.sender, _to, _value); } /** * Transfer tokens from other address * * Send `_value` tokens to `_to` in behalf of `_from` * * @param _from The address of the sender * @param _to The address of the recipient * @param _value the amount to send */ function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { require(_value <= allowance[_from][msg.sender]); // Check allowance allowance[_from][msg.sender] -= _value; _transfer(_from, _to, _value); return true; } /** * Set allowance for other address * * Allows `_spender` to spend no more than `_value` tokens in your behalf * * @param _spender The address authorized to spend * @param _value the max amount they can spend */ function approve(address _spender, uint256 _value) public returns (bool success) { allowance[msg.sender][_spender] = _value; Approval(msg.sender, _spender, _value); return true; } /** * Set allowance for other address and notify * * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it * * @param _spender The address authorized to spend * @param _value the max amount they can spend * @param _extraData some extra information to send to the approved contract */ function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) { tokenRecipient spender = tokenRecipient(_spender); if (approve(_spender, _value)) { spender.receiveApproval(msg.sender, _value, this, _extraData); return true; } } /** * Destroy tokens * * Remove `_value` tokens from the system irreversibly * * @param _value the amount of money to burn */ function burn(uint256 _value) onlyOwner public returns (bool success) { require(balanceOf[msg.sender] >= _value); // Check if the sender has enough balanceOf[msg.sender] -= _value; // Subtract from the sender totalSupply -= _value; // Updates totalSupply Burn(msg.sender, _value); return true; } /** * Destroy tokens from other account * * Remove `_value` tokens from the system irreversibly on behalf of `_from`. * * @param _from the address of the sender * @param _value the amount of money to burn */ function burnFrom(address _from, uint256 _value) onlyOwner public returns (bool success) { require(balanceOf[_from] >= _value); // Check if the targeted balance is enough require(_value <= allowance[_from][msg.sender]); // Check allowance balanceOf[_from] -= _value; // Subtract from the targeted balance allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance totalSupply -= _value; // Update totalSupply Burn(_from, _value); return true; } /// @notice Create `mintedAmount` tokens and send it to `target` /// @param target Address to receive the tokens /// @param mintedAmount the amount of tokens it will receive function mintToken(address target, uint256 mintedAmount) onlyOwner public { balanceOf[target] += mintedAmount; totalSupply += mintedAmount; Transfer(0, this, mintedAmount); Transfer(this, target, mintedAmount); } /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens /// @param target Address to be frozen /// @param freeze either to freeze it or not function freezeAccount(address target, bool freeze) onlyOwner public { frozenAccount[target] = freeze; FrozenFunds(target, freeze); } /// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth /// @param newSellPrice Price the users can sell to the contract /// @param newBuyPrice Price users can buy from the contract function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public { sellPrice = newSellPrice; buyPrice = newBuyPrice; } /// @notice Buy tokens from contract by sending ether function buy() payable public { uint amount = msg.value / buyPrice; // calculates the amount _transfer(this, msg.sender, amount); // makes the transfers } /// @notice Sell `amount` tokens to contract /// @param amount amount of tokens to be sold function sell(uint256 amount) public { require(this.balance >= amount * sellPrice); // checks if the contract has enough ether to buy _transfer(msg.sender, this, amount); // makes the transfers msg.sender.transfer(amount * sellPrice); // sends ether to the seller. It's important to do this last to avoid recursion attacks } function transfer(address _to, uint256 _value, bytes _data) public returns (bool) { require(_to != address(this)); transfer(_to, _value); require(_to.call(_data)); return true; } function transferFrom(address _from, address _to, uint256 _value, bytes _data) public returns (bool) { require(_to != address(this)); transferFrom(_from, _to, _value); require(_to.call(_data)); return true; } function approve(address _spender, uint256 _value, bytes _data) public returns (bool) { require(_spender != address(this)); approve(_spender, _value); require(_spender.call(_data)); return true; } }
這個代币合約實作了,增發,減持,全局鎖,賬号當機/解凍 等等功能。
33.4.2. 玩家屬性與遊戲裝備
下面的合約實作了遊戲玩家上鍊,上鍊資訊有玩家屬性,例如膚色,眼睛,頭發,血統等等。身上的穿戴物品包括武器等等。
pragma solidity ^0.4.20; contract Player { address public owner; string name; bool lock = false; //合約鎖 uint number = 1; uint attr_number = 1; mapping (address => string) guestbook; //客戶留言本 struct Attribute { string key; // 屬性的名字 string value; // 屬性值 } mapping (uint => Attribute) attribute; struct Wear { string name; // 裝備名 string desciption; // 資訊 string attribute; // 屬性,存儲json 資料。例如生命+10,魔法+5,冰凍系... } mapping (uint => Wear) wear; function Player(string _name) public { name = _name; } modifier onlyOwner { require(msg.sender == owner); _; } // 名稱 function getName() public view returns(string){ return name; } function setLock(bool _lock) onlyOwner public { lock = _lock; } // 增加人物屬性,例如膚色,眼睛,頭發等等 function putAttribute(string _key, string _value) onlyOwner public{ if(lock == false){ Attribute memory item = Attribute(_key, _value); attribute[attr_number] = item; attr_number = attr_number + 1; } } // 獲得屬性 function getAttribute(uint _attr_number) public view returns(string, string) { require(_attr_number < attr_number); Attribute memory item = attribute[_attr_number]; return (item.key, item.value); } // 增加裝備資訊,穿戴物品,武器, function putWear(string _name, string _description, string _attribute ) onlyOwner public{ if(lock == false){ Wear memory node = Wear(_name,_description,_attribute); wear[number] = node; number = number + 1; lock = true; } } // 獲得資訊 function getWear(uint _number) public view returns(string, string, string) { require(_number < number); Wear memory item = wear[_number]; return (item.name, item.desciption, item.attribute); } // 數量 function getWearCount() public view returns(uint){ return number; } // 客戶留言 function addGuestbook(address _owner, string message) onlyOwner public{ guestbook[_owner] = message; } }
33.4.3. 物品合成計算
區塊鍊還可用于物品合成計算或者叫煉金術等等
很早的時候玩《暗黑破壞神III》 裡面已一個盒子,放入符文,可以根據公式合成其他屬性的符文,我任務這個需求可以使用區塊鍊來完成。
另外在我玩XBOX遊戲《巫師3》 中的煉金術,鑄造,藥水合成等等,我逗人都可以使用區塊鍊完成。
33.4.4. 實施步驟
如果着手一個遊戲項目上鍊,我們需要怎麼做呢?
上鍊步驟
-
收集需求,收集公司的内部上鍊需求,聽取所有部門的建議和訴求。
收集内容例如,代币發行量多少?代币小數位數,代币名稱,是否會增發,是否需要當機,代币怎樣流通,怎樣回收
Dapp 的 UI 設計,各種功能流程
- 分析需求,因為需求來自各種部門,各種崗位等等,他們不一定從事需求分析工作,是以我們需求對他們的需求過濾,分析,然後給出初步的PRD文檔(産品需求文檔)
-
根據收集的需求設計合約
根據需求設計Dapp
系統架構設計,軟體架構設計,技術選型;需要考慮擴充性,靈活性,并發設計,資料安全,部署,後期監控,各種埋點(統計、監控)
- 準備環境,我們需要三個環境,開發,測試,生産(營運)。
-
項目啟動
運維部門準備環境
開發部門開發合約和Dapp
測試部門準備測試用例,測試環境
-
測試
Alpha 階段,将合約部署到測試環境,測試合約的每個函數的工作邏輯,確定無誤。因為合約一旦部署将不能更改,隻能廢棄使用新的合約,是以這個測試步驟必須重視。
Beta 階段,将測試合約部署到測試網,例如 Ropsten ,可以邀請公司内部測試
-
部署生産環境
部署合約,将合約部署到主網
Dapp 部署到生産環境。
- 驗收測試,在生産環境做最後驗收測試
原文釋出時間為:2018-03-10
本文作者:netkiller
本文來源:
騰訊雲 雲+社群,如需轉載請聯系原作者。