首页
学习
活动
专区
工具
TVP
发布

全栈开发以太坊Dapp 投票系统HELLO WORLD教程-第三节

在本教程的第1-2节中,我们使用ganache在我们的开发环境中构建了一个简单的投票应用程序。现在,让我们将这个应用程序放在真正的区块链上。以太坊有一些公共测试区块链和一个主要区块链。

测试网:有一些测试区块链,如Ropsten,Rinkeby,Kovan。将它们视为QA或登台服务器,它们仅用于测试目的。

在本教程中,我们将完成以下操作:

安装geth - 用于下载区块链并在本地计算机上运行以太坊节点的客户端软件。

安装名为Truffle的以太坊dapp框架,该框架将用于编译和部署我们的合同。

使用truffle对我们的投票应用程序进行小的更新。

编译并将合同部署到Rinkeby testnet。

通过网页truffle控制台与智能合同互动。

1.安装geth并同步区块链

我在MacOS和Ubuntu上安装并测试了所有内容,安装非常简单:

On Mac:

mahesh@projectblockchain:~$ brew tap ethereum/ethereum

mahesh@projectblockchain:~$ brew install ethereum

On Ubuntu:

mahesh@projectblockchain:~$ sudo apt-get install software-properties-common

mahesh@projectblockchain:~$ sudo add-apt-repository -y ppa:ethereum/ethereum

mahesh@projectblockchain:~$ sudo apt-get update

mahesh@projectblockchain:~$ sudo apt-get install ethereum

安装geth后,在命令行控制台中运行以下命令:

mahesh@projectblockchain:~$ geth --rinkeby --syncmode"fast"--rpc --rpcapidb,eth,net,web3,personal --cache=1024--rpcport8545--rpcaddr127.0.0.1--rpccorsdomain"*"

这将启动以太坊节点,连接到其他对等节点并开始下载区块链。 下载区块链所需的时间取决于各种因素,如您的互联网连接速度,计算机上的RAM,硬盘驱动器的类型等。在一台具有8GB RAM和50Mbps连接的机器上花了30-45分钟。

在运行geth的控制台中,您将看到如下所示的输出。 查找以粗体显示的块编号。 当您的区块链完全同步时,您看到的区块编号将接近此页面上的区块编号:https://rinkeby.etherscan.io/

I013022:18:15.116332core/blockchain.go:1064] imported32blocks,49txs (6.256Mg) in185.716ms (33.688Mg/s). #445097[e1199364… / bce20913…]

I013022:18:20.267142core/blockchain.go:1064] imported1blocks,1txs (0.239Mg) in11.379ms (20.963Mg/s). #445097[b4d77c46…]

I013022:18:21.059414core/blockchain.go:1064] imported1blocks,txs (0.000Mg) in7.807ms (0.000Mg/s). #445098[f990e694…]

I013022:18:34.367485core/blockchain.go:1064] imported1blocks,txs (0.000Mg) in4.599ms (0.000Mg/s). #445099[86b4f29a…]

I013022:18:42.953523core/blockchain.go:1064] imported1blocks,2txs (0.294Mg) in9.149ms (32.136Mg/s). #445100[3572f223…]

2.安装truffle框架

使用npm安装truffle 。 本教程中使用的truffle 版本是3.1.1

npm install -g truffle

3.设定投票合同

第一步是建立truffle 项目:

mahesh@projectblockchain:~$ mkdir voting

mahesh@projectblockchain:~$ cd voting

mahesh@projectblockchain:~/voting$ npm install -g webpack

mahesh@projectblockchain:~/voting$ truffle unbox webpack

mahesh@projectblockchain:~/voting$ ls

README.md contracts node_modules test webpack.config.js truffle.js

app migrationspackage.json

mahesh@projectblockchain:~/voting$ ls app/

index.html javascripts stylesheets

mahesh@projectblockchain:~/voting$ ls contracts/

ConvertLib.sol MetaCoin.sol Migrations.sol

mahesh@projectblockchain:~/voting$ ls migrations/

1_initial_migration.js2_deploy_contracts.js

如上所示,truffle创建运行完整堆栈dapp所需的必要文件和目录。 Truffle还创建了一个示例应用程序来帮助您入门(我们将不会在本教程中使用它)。您可以安全地删除此项目的contracts目录中的ConvertLib.sol和MetaCoin.sol文件。

了解迁移目录的内容非常重要。这些迁移文件用于将合同部署到区块链。 (如果你还记得,在上一篇文章中,我们使用VotingContract.new将合同部署到区块链,我们不再需要这样做了)。第一次迁移1_initial_migration.js将名为Migrations的合同部署到区块链,用于存储您已部署的最新合同。每次运行迁移时,truffle都会查询区块链以获取已部署的最后一个合同,然后部署尚未部署的任何合同。然后,它会更新迁移合同中的last_completed_migration字段,以指示已部署的最新合同。您可以简单地将其视为名为Migration的数据库表,其中包含名为last_completed_migration的列,该列始终保持最新。您可以在truffle文档页面上找到更多详细信息。

现在让我们使用我们在上一个教程中编写的所有代码更新项目,下面将对几个更改进行解释。

首先,将Voting.sol从上一个教程复制到contract目录(此文件没有更改)。

pragma solidity ^0.4.18;

// We have to specify what version of compiler this code will compile with

contract Voting {

/* mapping field below is equivalent to an associative array or hash.

The key of the mapping is candidate name stored as type bytes32 and value is

an unsigned integer to store the vote count

*/

mapping (bytes32 =>uint8) public votesReceived;

/* Solidity doesn't let you pass in an array of strings in the constructor (yet).

We will use an array of bytes32 instead to store the list of candidates

*/

bytes32[] public candidateList;

/* This is the constructor which will be called once when you

deploy the contract to the blockchain. When we deploy the contract,

we will pass an array of candidates who will be contesting in the election

*/

function Voting(bytes32[] candidateNames) public {

candidateList = candidateNames;

}

// This function returns the total votes a candidate has received so far

function totalVotesFor(bytes32 candidate) view public returns (uint8) {

require(validCandidate(candidate));

returnvotesReceived[candidate];

}

// This function increments the vote count for the specified candidate. This

// is equivalent to casting a vote

function voteForCandidate(bytes32 candidate) public {

require(validCandidate(candidate));

votesReceived[candidate] +=1;

}

function validCandidate(bytes32 candidate) view public returns (bool) {

for(uinti =; i

if(candidateList[i] == candidate) {

returntrue;

}

}

returnfalse;

}

}

mahesh@projectblockchain:~/voting$ ls contracts/

Migrations.sol Voting.sol

接下来,使用以下内容替换迁移目录中的2_deploy_contracts.js的内容:

varVoting = artifacts.require("./Voting.sol");

module.exports = function(deployer) {

deployer.deploy(Voting, ['Rama','Nick','Jose'], );

};

/* As you can see above, the deployer expects the first argument to be the name of the contract followed by constructor arguments. In our case, there is only one argument which is an array of

candidates. The third argument is a hash where we specify the gas required to deploy our code. The gas amount varies depending on the size of your contract.

*/

您还可以将gas值设置为truffle.js中的全局设置。 继续添加如下所示的gas选项,以便将来如果忘记在特定的迁移文件中设置gas,它将默认使用全局值。

require('babel-register')

module.exports = {

networks: {

development: {

host:'localhost',

port:8545,

network_id:'*',

gas:470000

}

}

}

好,第三节到此结束!!

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180730A08HMO00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券