5 u3 O3 M! E4 r' g0 O
维基上说智能合约(英语:Smartcontract)是一种旨在以信息化方式传播、验证或执行合同的计算机协议。智能合约允许在没有第三方的情况下进行可信交易。这些交易可追踪且不可逆转。智能合约概念于1994年由NickSzabo首次提出。智能合同的目的是提供优于传统合同方法的安全,并减少与合同相关的其他交易成本。/ L4 |. \9 U( u9 j/ `4 r
由于区块链上的所有用户都可以看到基于区块链的智能合约。这也会导致包括安全漏洞在内的所有漏洞都可见,并且可能无法迅速修复。这样的攻击难以迅速解决。
+ {3 a4 k, B% d$ Y' |
插曲,2016年6月TheDAOEther的漏洞造成损失5000万美元,而开发者试图达成共识的解决方案。DAO的程序在黑客删除资金之前有一段时间的延迟。以太坊软件的一个硬分叉在时限到期之前完成了攻击者的资金回收工作。以太坊智能合约中的问题包括合约编程Solidity、编译器错误、以太坊虚拟机错误、对区块链网络的攻击、程序错误的不变性以及其他尚无文档记录的攻击。
}+ T; P ~: c
部署智能合约的经典案例有:
以太坊在其区块链上实施了一种近乎图灵完备的语言,这是一个突出的智能合约框架。RootStock(RSK)是一个智能合约平台,通过侧链技术连接到比特币区块链。RSK兼容为以太坊创造的智能合约。
/ a! f) G! l+ ^/ |- D( K" w8 j
一个典型的智能合约的solidity语言编程示例或者叫实例如下一个委托投票系统,做了一些备注:3 l! c% J. ~' r7 ]+ q+ R
官网示例原文
( T$ L0 a9 _: H$ l4 ?
这个例子是最新的,主要用到了以太坊编程语言Solidity的一些特性。例子实现了一个投票智能合约即电子投票系统。解决的主要问题是如何分配合理的权限给正确的人,并且要防止被篡改。这个例子实现了如何去委托投票,整个投票计数过程是自动而且完全透明。
- Q( V* E* `+ s( }, W
功能上它首先为投票创建一个合约,发起者作为所谓的chairperson姑且叫主席来给每一个独立的地址分配相应权限。每一个参与投票者可以自己投票或者委托自己信任的人。这段代码最后运行结果会返回得票数最多的那个议案或者叫倡议。" p' ~- I* ~. C$ M! ^
4 t. T; \& x2 m, V' r
- pragma solidity ^0.4.22;3 t: m& | M% f, y5 \. U3 w0 e
- /// @title Voting with delegation.一个有委托功能的投票系统
- contract Ballot {* U- \- i" H( e" X4 f- N
- // This declares a new complex type which will 定义一个复杂类型6 s* U6 u8 E; ~" b9 ^+ ]
- // be used for variables later. 后面作为变量来使用
- // It will represent a single voter.代表一个投票人
- struct Voter {6 K K' Z q. F
- uint weight; // weight is accumulated by delegation weight在代表投票过程中会累积
- bool voted; // if true, that person already voted 如果值为true,代表这个投票人已经投过票7 \& N* a# {3 y% V+ v
- address delegate; // person delegated to 投票人地址& [( }. k# o5 X. C3 R; D
- uint vote; // index of the voted proposal 当前投票的索引, r4 P6 t" x. {, F: T$ l; k. l8 d
- }# `) P( l$ Y, X* A) _ j' }$ t
- // This is a type for a single proposal.代表一份议案的数据结构
- struct Proposal {) K9 ~) C# b$ E a& p2 o* Z* q
- bytes32 name; // short name (up to 32 bytes) 议案的名称
- uint voteCount; // number of accumulated votes 议案接受的投票数8 j1 E. [) Y. M0 x& l! _
- }
- address public chairperson; // 定义投票发起人
- // This declares a state variable that
- // stores a `Voter` struct for each possible address. 这个状态变量存储了所有潜在投票人
- mapping(address => Voter) public voters; N/ y5 ? F- J4 j J
- // A dynamically-sized array of `Proposal` structs. 定义动态数组存储议案
- Proposal[] public proposals;
- /// Create a new ballot to choose one of `proposalNames`. 传入议案名称来定义一个投票对象. x5 h. ~1 D _" U3 d M; g4 Z
- function Ballot(bytes32[] proposalNames) public {) O( I& z; y4 T8 `% m, f
- chairperson = msg.sender;
- voters[chairperson].weight = 1;
- // For each of the provided proposal names,
- // create a new proposal object and add it( h* e) |9 s- t" t% |4 U9 b
- // to the end of the array. 按传入的议案名称创建一个议案,并加入到前面定义的议案数组) _; A) c& I7 l8 K: B
- for (uint i = 0; i winningVoteCount) {* H" H- S! n7 Y2 \# {: M% J
- winningVoteCount = proposals.voteCount;
- winningProposal_ = p;7 t6 }; E0 h1 x1 ]+ f+ c# I$ O
- }
- }9 N# c8 p$ S, @: J9 O+ b
- }
- // Calls winningProposal() function to get the index
- // of the winner contained in the proposals array and then2 u/ @0 \" c2 z3 l; h! o# p8 F
- // returns the name of the winner; z( M) t: Y! w4 F6 o( x7 l
- function winnerName() public view
- returns (bytes32 winnerName_)4 [% x* H& c- _1 c, p7 C( }
- {
- winnerName_ = proposals[winningProposal()].name; G" Q, r+ _2 f" p( ^) A7 U5 [
- }
- }