智能合约开发用solidity编程语言部署在以太坊这个区块链平台,本文提供一个官方实战示例快速入门,用例子深入浅出智能合约开发,体会以太坊构建去中心化可信交易技术魅力。智能合约其实是“执行合约条款的计算机交易协议”。区块链上的所有用户都可以看到基于区块链的智能合约。

维基上说智能合约(英语:Smart contract )是一种旨在以信息化方式传播、验证或执行合同的计算机协议。智能合约允许在没有第三方的情况下进行可信交易。这些交易可追踪且不可逆转。智能合约概念于1994年由Nick Szabo首次提出。智能合同的目的是提供优于传统合同方法的安全,并减少与合同相关的其他交易成本。

由于区块链上的所有用户都可以看到基于区块链的智能合约。这也会导致包括安全漏洞在内的所有漏洞都可见,并且可能无法迅速修复。这样的攻击难以迅速解决。

插曲,2016年6月The DAOEther的漏洞造成损失5000万美元,而开发者试图达成共识的解决方案。DAO的程序在黑客删除资金之前有一段时间的延迟。以太坊软件的一个硬分叉在时限到期之前完成了攻击者的资金回收工作。以太坊智能合约中的问题包括合约编程Solidity、编译器错误、以太坊虚拟机错误、对区块链网络的攻击、程序错误的不变性以及其他尚无文档记录的攻击。

部署智能合约的经典案例有:

1. 以太坊在其区块链上实施了一种近乎图灵完备的语言,这是一个突出的智能合约框架。
2. RootStock (RSK) 是一个智能合约平台,通过侧链技术连接到比特币区块链。 RSK兼容为以太坊创造的智能合约。

一个典型的智能合约的solidity语言编程示例或者叫实例如下一个委托投票系统,做了一些备注:

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

这个例子是最新的,主要用到了以太坊编程语言Solidity的一些特性。例子实现了一个投票智能合约即电子投票系统。解决的主要问题是如何分配合理的权限给正确的人,并且要防止被篡改。这个例子实现了如何去委托投票,整个投票计数过程是自动而且完全透明。

功能上它首先为投票创建一个合约,发起者作为所谓的chairperson姑且叫主席来给每一个独立的地址分配相应权限。每一个参与投票者可以自己投票或者委托自己信任的人。这段代码最后运行结果会返回得票数最多的那个议案或者叫倡议。

  1. pragma solidity ^0.4.;
  2.  
  3. /// @title Voting with delegation.一个有委托功能的投票系统
  4. contract Ballot {
  5. // This declares a new complex type which will 定义一个复杂类型
  6. // be used for variables later. 后面作为变量来使用
  7. // It will represent a single voter.代表一个投票人
  8. struct Voter {
  9. uint weight; // weight is accumulated by delegation weight在代表投票过程中会累积
  10. bool voted; // if true, that person already voted 如果值为true,代表这个投票人已经投过票
  11. address delegate; // person delegated to 投票人地址
  12. uint vote; // index of the voted proposal 当前投票的索引
  13. }
  14.  
  15. // This is a type for a single proposal.代表一份议案的数据结构
  16. struct Proposal {
  17. bytes32 name; // short name (up to 32 bytes) 议案的名称
  18. uint voteCount; // number of accumulated votes 议案接受的投票数
  19. }
  20.  
  21. address public chairperson; // 定义投票发起人
  22.  
  23. // This declares a state variable that
  24. // stores a `Voter` struct for each possible address. 这个状态变量存储了所有潜在投票人
  25. mapping(address => Voter) public voters;
  26.  
  27. // A dynamically-sized array of `Proposal` structs. 定义动态数组存储议案
  28. Proposal[] public proposals;
  29.  
  30. /// Create a new ballot to choose one of `proposalNames`. 传入议案名称来定义一个投票对象
  31. function Ballot(bytes32[] proposalNames) public {
  32. chairperson = msg.sender;
  33. voters[chairperson].weight = ;
  34.  
  35. // For each of the provided proposal names,
  36. // create a new proposal object and add it
  37. // to the end of the array. 按传入的议案名称创建一个议案,并加入到前面定义的议案数组
  38. for (uint i = ; i < proposalNames.length; i++) {
  39. // `Proposal({...})` creates a temporary
  40. // Proposal object and `proposals.push(...)`
  41. // appends it to the end of `proposals`.创建一个临时议案对象,加入议案数组
  42. proposals.push(Proposal({
  43. name: proposalNames[i],
  44. voteCount:
  45. }));
  46. }
  47. }
  48.  
  49. // Give `voter` the right to vote on this ballot.
  50. // May only be called by `chairperson`. 给投票人分配投票权限,这个操作只有主席才可以
  51. function giveRightToVote(address voter) public {
  52. // If the first argument of `require` evaluates
  53. // to `false`, execution terminates and all
  54. // changes to the state and to Ether balances
  55. // are reverted.
  56. // This used to consume all gas in old EVM versions, but
  57. // not anymore.
  58. // It is often a good idea to use `require` to check if
  59. // functions are called correctly.
  60. // As a second argument, you can also provide an
  61. // explanation about what went wrong.
  62. require(
  63. msg.sender == chairperson,
  64. "Only chairperson can give right to vote."
  65. );
  66. require(
  67. !voters[voter].voted,
  68. "The voter already voted."
  69. );
  70. require(voters[voter].weight == );
  71. voters[voter].weight = ;
  72. }
  73.  
  74. /// Delegate your vote to the voter `to`. 委托投票给另外一个投票人
  75. function delegate(address to) public {
  76. // assigns reference 找出委托发起人,如果已经投票,终止程序
  77. Voter storage sender = voters[msg.sender];
  78. require(!sender.voted, "You already voted.");
  79.  
  80. require(to != msg.sender, "Self-delegation is disallowed.");
  81.  
  82. // Forward the delegation as long as
  83. // `to` also delegated.
  84. // In general, such loops are very dangerous,
  85. // because if they run too long, they might
  86. // need more gas than is available in a block.
  87. // In this case, the delegation will not be executed,
  88. // but in other situations, such loops might
  89. // cause a contract to get "stuck" completely.
  90. while (voters[to].delegate != address()) {
  91. to = voters[to].delegate;
  92.  
  93. // We found a loop in the delegation, not allowed. 发起人、委托人不能是同一个,否则终止程序
  94. require(to != msg.sender, "Found loop in delegation.");
  95. }
  96.  
  97. // Since `sender` is a reference, this
  98. // modifies `voters[msg.sender].voted` 标识发起人已经投过票
  99. sender.voted = true;
  100. sender.delegate = to;
  101. Voter storage delegate_ = voters[to];
  102. if (delegate_.voted) {
  103. // If the delegate already voted,
  104. // directly add to the number of votes 投票成功,投票总数加上相应的weight
  105. proposals[delegate_.vote].voteCount += sender.weight;
  106. } else {
  107. // If the delegate did not vote yet,
  108. // add to her weight. 如果还没投票,发起人weight赋值给委托人
  109. delegate_.weight += sender.weight;
  110. }
  111. }
  112.  
  113. /// Give your vote (including votes delegated to you)
  114. /// to proposal `proposals[proposal].name`.投票给某个议案
  115. function vote(uint proposal) public {
  116. Voter storage sender = voters[msg.sender];
  117. require(!sender.voted, "Already voted.");
  118. sender.voted = true;
  119. sender.vote = proposal;
  120.  
  121. // If `proposal` is out of the range of the array,
  122. // this will throw automatically and revert all
  123. // changes.
  124. proposals[proposal].voteCount += sender.weight;
  125. }
  126.  
  127. /// @dev Computes the winning proposal taking all
  128. /// previous votes into account.找出投票数最多的议案
  129. function winningProposal() public view
  130. returns (uint winningProposal_)
  131. {
  132. uint winningVoteCount = ;
  133. for (uint p = ; p < proposals.length; p++) {
  134. if (proposals[p].voteCount > winningVoteCount) {
  135. winningVoteCount = proposals[p].voteCount;
  136. winningProposal_ = p;
  137. }
  138. }
  139. }
  140.  
  141. // Calls winningProposal() function to get the index
  142. // of the winner contained in the proposals array and then
  143. // returns the name of the winner
  144. function winnerName() public view
  145. returns (bytes32 winnerName_)
  146. {
  147. winnerName_ = proposals[winningProposal()].name;
  148. }
  149. }

如果这个代码基本能够看明白,那应该是可以直接实战开启以太坊区块链的学习进程了。

安利个教程可以通过在线编程环境实战学习:以太坊DApp实战开发

智能合约开发solidity编程语言开发一个以太坊应用区块链投票实例的更多相关文章

  1. 以太坊geth区块链私链建立

      以太坊geth区块链私链建立 geth的github https://github.com/ethereum/go-ethereum 下载最新(1.8)的geth,windows下安装很简单 关于 ...

  2. 智能合约语言 Solidity 教程系列7 - 以太单位及时间单位

    这是Solidity教程系列文章第7篇介绍以太单位及时间单位,系列带你全面深入理解Solidity语言. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所 ...

  3. 智能合约语言Solidity教程系列2 - 地址类型介绍

    智能合约语言Solidity教程系列第二篇 - Solidity地址类型介绍. 写在前面 Solidity是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你 ...

  4. 智能合约语言 Solidity 教程系列3 - 函数类型

    Solidity 教程系列第三篇 - Solidity 函数类型介绍. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你先看以 ...

  5. 智能合约语言 Solidity 教程系列4 - 数据存储位置分析

    写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 这部分的内容官方英文文档讲的不是很透,因此我在参考Soli ...

  6. 智能合约语言 Solidity 教程系列6 - 结构体与映射

    写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 本系列文章一部分是参考Solidity官方文档(当前最新版 ...

  7. 智能合约语言 Solidity 教程系列5 - 数组介绍

    写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解, 如果你还不了解,建议你先看以太坊是什么 本文前半部分是参考Solidity官方文档(当前最新版本: ...

  8. 智能合约语言 Solidity 教程系列8 - Solidity API

    这是Solidity教程系列文章第8篇介绍Solidity API,它们主要表现为内置的特殊的变量及函数,存在于全局命名空间里. 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应 ...

  9. 智能合约语言 Solidity 教程系列2 - 地址类型介绍

    Solidity教程系列第二篇 - Solidity地址类型介绍. 写在前面 Solidity是以太坊智能合约编程语言,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你先看以太坊是 ...

随机推荐

  1. 跟着大神学zookeeper分布式锁实现-----来自Ruthless

    前几天分享了@Ruthless大神的Redis锁,发现和大家都学习了很多东西.因为分布式锁里面,最好的实现是zookeeper的分布式锁.所以在这里把实现方式和大家分享一下. zookeeper分布式 ...

  2. zabbix通过SNMP监控服务器硬件及构建触发器

    公司的服务器没装系统无法使用IPMI协议来监控服务器硬件信息,所以我们使用SNMP来获取,下面介绍如何通过SNMP监控服务器硬件信息. 1.HP服务器进入iLO开启SNMP协议. 2.查看服务器温度信 ...

  3. 走进webpack(2)--第三方框架(类库)的引入及抽离

    在当代的前端开发中,很少会用原生JS来开发页面,最基本的都会使用jQuery来节省我们开发的时间和效率,而angular,vue,react的出现更是为前端开发者带来了福音.那么这篇文章就说说如何用w ...

  4. C#编程语言之委托与事件(一)—— C/C++函数指针和C#委托初步

    相信正在学习C#的人都有学习过C或C++的经验,本文要讲的第一个要点是C#中的委托(delegate,有些资料也叫代表).什么是委托,很多人都能自然而然地想到C/C++中的函数指针,事实上很多书和资料 ...

  5. 启动tomcat时jmx port被占用

    一.问题描述 今天一来公司,在IntelliJ IDEA 中启动Tomcat服务器时就出现了如下图所示的错误: 错误: 代理抛出异常错误: java.rmi.server.ExportExceptio ...

  6. 漫谈Java IO之 Netty与NIO服务器

    前面介绍了基本的网络模型以及IO与NIO,那么有了NIO来开发非阻塞服务器,大家就满足了吗?有了技术支持,就回去追求效率,因此就产生了很多NIO的框架对NIO进行封装--这就是大名鼎鼎的Netty. ...

  7. 201621123040《Java程序设计》第七周学习总结

    1.本周学习总结 1.1思维导图:Java图形界面总结 2.书面作业 2.1GUI中的事件处理 2.1.1写出事件处理模型中最重要的几个关键词. 关键词:事件 事件源 事件监听器 2.1.2任意编写事 ...

  8. 学号:201621123032 《Java程序设计》第7周学习总结

    1:本周学习总结 1.1:思维导图:Java图形界面总结 2:书面作业 2.1: GUI中的事件处理 2.1.1: 写出事件处理模型中最重要的几个关键词 事件:如鼠标单击,滑动,输入汉字等. 事件源: ...

  9. android批量打包

    http://blog.csdn.net/johnny901114/article/details/48714849

  10. 坑爹了多少年的html元素垂直居中问题

    原文章:https://www.w3cplus.com/css3/a-guide-to-flexbox.html 如果你的元素有固定高度的话 父元素用display: flex;height:100p ...