智能合約開發用solidity程式設計語言部署在以太坊這個區塊鍊平台,本文提供一個官方實戰示例快速入門,用例子深入淺出智能合約開發,體會以太坊建構去中心化可信交易技術魅力。智能合約其實是“執行合約條款的計算機交易協定”。區塊鍊上的所有使用者都可以看到基于區塊鍊的智能合約。
維基上說智能合約(英語:Smart contract )是一種旨在以資訊化方式傳播、驗證或執行合同的計算機協定。智能合約允許在沒有第三方的情況下進行可信交易。這些交易可追蹤且不可逆轉。智能合約概念于1994年由Nick Szabo首次提出。智能合同的目的是提供優于傳統合同方法的安全,并減少與合同相關的其他交易成本。
由于區塊鍊上的所有使用者都可以看到基于區塊鍊的智能合約。這也會導緻包括安全漏洞在内的所有漏洞都可見,并且可能無法迅速修複。這樣的攻擊難以迅速解決。
插曲,2016年6月The DAOEther的漏洞造成損失5000萬美元,而開發者試圖達成共識的解決方案。DAO的程式在黑客删除資金之前有一段時間的延遲。以太坊軟體的一個硬分叉在時限到期之前完成了攻擊者的資金回收工作。以太坊智能合約中的問題包括合約程式設計Solidity、編譯器錯誤、以太坊虛拟機錯誤、對區塊鍊網絡的攻擊、程式錯誤的不變性以及其他尚無文檔記錄的攻擊。
部署智能合約的經典案例有:
1. 以太坊在其區塊鍊上實施了一種近乎圖靈完備的語言,這是一個突出的智能合約架構。
2. RootStock (RSK) 是一個智能合約平台,通過側鍊技術連接配接到比特币區塊鍊。 RSK相容為以太坊創造的智能合約。
如果你是區塊鍊開發的小白那建議你看這個教程:
以太坊DApp入門實戰一個典型的智能合約的solidity語言程式設計示例或者叫執行個體如下一個委托投票系統,做了一些備注:
官網示例原文:https://solidity.readthedocs.io/en/develop/solidity-by-example.html#possible-improvements
這個例子是最新的,主要用到了以太坊程式設計語言Solidity的一些特性。例子實作了一個投票智能合約即電子投票系統。解決的主要問題是如何配置設定合理的權限給正确的人,并且要防止被篡改。這個例子實作了如何去委托投票,整個投票計數過程是自動而且完全透明。
功能上它首先為投票建立一個合約,發起者作為所謂的chairperson姑且叫主席來給每一個獨立的位址配置設定相應權限。每一個參與投票者可以自己投票或者委托自己信任的人。這段代碼最後運作結果會傳回得票數最多的那個議案或者叫倡議。
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;
}
}
如果這個代碼基本能夠看明白,那應該是可以直接實戰開啟以太坊區塊鍊的學習程序了,分享兩個教程,都可以通過線上程式設計環境實戰學習:
1. 适合區塊鍊新手的以太坊DApp開發:
http://xc.hubwiz.com/course/5a952991adb3847553d205d12. 用區塊鍊、星際檔案系統(IPFS)、Node.js和MongoDB來建構以太坊DApp電商平台:
http://xc.hubwiz.com/course/5abbb7acc02e6b6a59171dd6如果想加入以太坊技術開發群可以加微信:cuixuebin2,拉你入群。