智能合约开发用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姑且叫主席来给每一个独立的地址分配相应权限。每一个参与投票者可以自己投票或者委托自己信任的人。这段代码最后运行结果会返回得票数最多的那个议案或者叫倡议。

pragma solidity ^0.4.;

/// @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 = ; // For each of the provided proposal names,
// create a new proposal object and add it
// to the end of the array. 按传入的议案名称创建一个议案,并加入到前面定义的议案数组
for (uint i = ; 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:
}));
}
} // 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 == );
voters[voter].weight = ;
} /// 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()) {
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 = ;
for (uint p = ; 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实战开发

智能合约开发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. Linux中SVN的备份与恢复

    linux中SVN备份有三种方式 1.svnadmin dump 是官方推荐的备份方式,优点是比较灵活,可以全量备份也可以增量备份,并提供版本恢复机制. 缺点是版本数过大,增长到数万以上,那么dump ...

  2. java基础笔记(6)----面向对象的三大特性

    简介:面向对象的三大特性就是封装,继承,多态,是面向对象的核心. 封装 简介:封装是类的边界,可以对数据起到保护作用 特性:属性私有,提供公开的get/set方法 属性私有:private 数据类型 ...

  3. [UWP]创建一个ProgressControl

    1. 前言 博客园终于新增了UWP的分类,我来为这个分类贡献第一篇博客吧. UWP有很多问题,先不说生态的事情,表单.验证.输入.设计等等一堆基本问题缠身.但我觉得最应该首先解决的绝对是Blend,那 ...

  4. 记录一个古老的Sql分页过程

    /* 根据单位ID获取排班信息 For:WXX TIme:2017-11-22 */ ALTER proc [dbo].[proc_ScheduleInfo] )='', --单位ID )='', - ...

  5. Linux下导入SQL文件

    导入数据库 一.首先建空数据库 格式: mysql>create database 数据库名;举例: mysql>create database abc; 二.导入数据库 方法一: 选择数 ...

  6. 工作流Activiti5.13学习笔记(一)

    了解工作流 1.工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某 ...

  7. Java基础学习笔记十四 常用API之基本类型包装类

    基本类型包装类 Java中有8种基本的数据类型,可是这些数据是基本数据,想对其进行复杂操作,变的很难.怎么办呢?在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们 ...

  8. Ubuntu安装MariaDB教程

    一.环境 服务器:Ubuntu 16.04.1 LTS(GUN/Linux 4.4.0-91-generic x86_64) 数据库版本:MariaDB 10.3 二.安装流程 2.1 进入Maria ...

  9. [福大软工] W班 团队第一次作业—团队展示成绩公布

    作业地址 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/906 作业要求 根据已经组队的队伍组成, 每 ...

  10. 第二次作业-Steam软件分析

    1 .介绍产品相关信息 随着电子音频游戏产业的发展以及正版意识的崛起,Steam已经成为大部分游戏爱好者必备的一款游戏下载平台.这款软件也使得Valve公司从一个游戏制作公司成功扩展业务到一个承揽众多 ...