智能合約開發solidity編程語言實例

 智能合約開發用solidity編程語言部署在以太坊這個區塊鏈平臺,本文提供一個官方實戰示例快速入門,用例子深刻淺出智能合約開發,體會以太坊構建去中心化可信交易技術魅力。智能合約實際上是「執行合約條款的計算機交易協議」。區塊鏈上的全部用戶均可以看到基於區塊鏈的智能合約。html

維基上說智能合約(英語:Smart contract )是一種旨在以信息化方式傳播、驗證或執行合同的計算機協議。智能合約容許在沒有第三方的狀況下進行可信交易。這些交易可追蹤且不可逆轉。智能合約概念於1994年由Nick Szabo首次提出。智能合同的目的是提供優於傳統合同方法的安全,並減小與合同相關的其餘交易成本。編程

因爲區塊鏈上的全部用戶均可以看到基於區塊鏈的智能合約。這也會致使包括安全漏洞在內的全部漏洞均可見,而且可能沒法迅速修復。這樣的攻擊難以迅速解決。數組

插曲,2016年6月The DAOEther的漏洞形成損失5000萬美圓,而開發者試圖達成共識的解決方案。DAO的程序在黑客刪除資金以前有一段時間的延遲。以太坊軟件的一個硬分叉在時限到期以前完成了攻擊者的資金回收工做。以太坊智能合約中的問題包括合約編程Solidity、編譯器錯誤、以太坊虛擬機錯誤、對區塊鏈網絡的攻擊、程序錯誤的不變性以及其餘尚無文檔記錄的攻擊。安全

部署智能合約的經典案例有:網絡

1. 以太坊在其區塊鏈上實施了一種近乎圖靈完備的語言,這是一個突出的智能合約框架。
2. RootStock (RSK) 是一個智能合約平臺,經過側鏈技術鏈接到比特幣區塊鏈。 RSK兼容爲以太坊創造的智能合約。數據結構

一個典型的智能合約的solidity語言編程示例或者叫實例以下一個委託投票系統,作了一些備註:app

官網示例原文:https://solidity.readthedocs.io/en/develop/solidity-by-example.html#possible-improvements框架

這個例子是最新的,主要用到了以太坊編程語言Solidity的一些特性。例子實現了一個投票智能合約即電子投票系統。解決的主要問題是如何分配合理的權限給正確的人,而且要防止被篡改。這個例子實現瞭如何去委託投票,整個投票計數過程是自動並且徹底透明。編程語言

功能上它首先爲投票建立一個合約,發起者做爲所謂的chairperson姑且叫主席來給每個獨立的地址分配相應權限。每個參與投票者能夠本身投票或者委託本身信任的人。這段代碼最後運行結果會返回得票數最多的那個議案或者叫倡議。ide

pragma solidity ^0.4.22;

/// @title Voting with delegation.一個有委託功能的投票系統
contract Ballot {
    // This declares a new complex type which will 定義一個複雜類型
    // be used for variables later. 後面做爲變量來使用
    // It will represent a single voter.表明一個投票人
    struct Voter {
        uint weight; // weight is accumulated by delegation weight在表明投票過程當中會累積
        bool voted;  // if true, that person already voted 若是值爲true,表明這個投票人已經投過票
        address delegate; // person delegated to 投票人地址
        uint vote;   // index of the voted proposal 當前投票的索引
    }

    // This is a type for a single proposal.表明一份議案的數據結構 
    struct Proposal {
        bytes32 name;   // short name (up to 32 bytes) 議案的名稱
        uint voteCount; // number of accumulated votes 議案接受的投票數
    }

    address public chairperson;  // 定義投票發起人

    // This declares a state variable that 
    // stores a `Voter` struct for each possible address. 這個狀態變量存儲了全部潛在投票人
    mapping(address => Voter) public voters;

    // A dynamically-sized array of `Proposal` structs. 定義動態數組存儲議案
    Proposal[] public proposals;

    /// Create a new ballot to choose one of `proposalNames`. 傳入議案名稱來定義一個投票對象
    function Ballot(bytes32[] proposalNames) public {
        chairperson = msg.sender;
        voters[chairperson].weight = 1;

        // For each of the provided proposal names,
        // create a new proposal object and add it
        // to the end of the array. 按傳入的議案名稱建立一個議案,並加入到前面定義的議案數組
        for (uint i = 0; i < proposalNames.length; i++) {
            // `Proposal({...})` creates a temporary
            // Proposal object and `proposals.push(...)`
            // appends it to the end of `proposals`.建立一個臨時議案對象,加入議案數組
            proposals.push(Proposal({
                name: proposalNames[i],
                voteCount: 0
            }));
        }
    }

    // Give `voter` the right to vote on this ballot.
    // May only be called by `chairperson`. 給投票人分配投票權限,這個操做只有主席才能夠
    function giveRightToVote(address voter) public {
        // If the first argument of `require` evaluates
        // to `false`, execution terminates and all
        // changes to the state and to Ether balances
        // are reverted.
        // This used to consume all gas in old EVM versions, but
        // not anymore.
        // It is often a good idea to use `require` to check if
        // functions are called correctly.
        // As a second argument, you can also provide an
        // explanation about what went wrong.
        require(
            msg.sender == chairperson,
            "Only chairperson can give right to vote."
        );
        require(
            !voters[voter].voted,
            "The voter already voted."
        );
        require(voters[voter].weight == 0);
        voters[voter].weight = 1;
    }

    /// Delegate your vote to the voter `to`. 委託投票給另一個投票人
    function delegate(address to) public {
        // assigns reference 找出委託發起人,若是已經投票,終止程序
        Voter storage sender = voters[msg.sender];
        require(!sender.voted, "You already voted.");

        require(to != msg.sender, "Self-delegation is disallowed.");

        // Forward the delegation as long as
        // `to` also delegated.
        // In general, such loops are very dangerous,
        // because if they run too long, they might
        // need more gas than is available in a block.
        // In this case, the delegation will not be executed,
        // but in other situations, such loops might
        // cause a contract to get "stuck" completely.
        while (voters[to].delegate != address(0)) {
            to = voters[to].delegate;

            // We found a loop in the delegation, not allowed. 發起人、委託人不能是同一個,不然終止程序
            require(to != msg.sender, "Found loop in delegation.");
        }

        // Since `sender` is a reference, this
        // modifies `voters[msg.sender].voted`   標識發起人已經投過票
        sender.voted = true;
        sender.delegate = to;
        Voter storage delegate_ = voters[to];
        if (delegate_.voted) {
            // If the delegate already voted,
            // directly add to the number of votes 投票成功,投票總數加上相應的weight
            proposals[delegate_.vote].voteCount += sender.weight;
        } else {
            // If the delegate did not vote yet,
            // add to her weight. 若是還沒投票,發起人weight賦值給委託人
            delegate_.weight += sender.weight;
        }
    }

    /// Give your vote (including votes delegated to you)
    /// to proposal `proposals[proposal].name`.投票給某個議案
    function vote(uint proposal) public {
        Voter storage sender = voters[msg.sender];
        require(!sender.voted, "Already voted.");
        sender.voted = true;
        sender.vote = proposal;

        // If `proposal` is out of the range of the array,
        // this will throw automatically and revert all
        // changes.
        proposals[proposal].voteCount += sender.weight;
    }

    /// @dev Computes the winning proposal taking all
    /// previous votes into account.找出投票數最多的議案
    function winningProposal() public view
            returns (uint winningProposal_)
    {
        uint winningVoteCount = 0;
        for (uint p = 0; p < proposals.length; p++) {
            if (proposals[p].voteCount > winningVoteCount) {
                winningVoteCount = proposals[p].voteCount;
                winningProposal_ = p;
            }
        }
    }

    // Calls winningProposal() function to get the index
    // of the winner contained in the proposals array and then
    // returns the name of the winner
    function winnerName() public view
            returns (bytes32 winnerName_)
    {
        winnerName_ = proposals[winningProposal()].name;
    }
}

 

若是這個代碼基本可以看明白,那應該是能夠直接實戰開啓以太坊區塊鏈的學習進程了。

安利個教程能夠經過在線編程環境實戰學習:以太坊DApp實戰開發

相關文章
相關標籤/搜索