2018年03月13日 09:20:54 思无邪-machengyu 阅读数 2683
 
版权声明:本文为博主原创文章,转载请务必注明出处,否则追究法律责任 https://blog.csdn.net/pony_maggie/article/details/79535896

环境

ubuntu 16.04, 64位


运行testrpc

安装过程参考以前的文章。

开启一个终端,输入testrpc运行测试节点。testrpc是一个完整的在内存中的区块链仅仅存在于你开发的设备上。相对于 Geth私有链环境,TestRPC 它在执行交易时是实时返回,而不等待默认的出块时间,这样你可以快速验证你新写的代码,当出现错误时,也能即时反馈给你。

启动 testrpc 经后,会默认创建10个帐号,Available Accounts是帐号列表,Private Keys是相对应的帐号密钥。

在当前home目录下新建testproject目录,进入目录执行

truffle init
 

等待一会发现会多出几个目录和文件:

  • contracts/: 智能合约存放的目录,默认情况下已经帮你创建 Migrations.sol合约。

  • migrations/: 存放部署脚本

  • test/: 存放测试脚本
  • truffle.js: truffle的配置文件

首先修改truffle.js文件,改成如下:

module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // 匹配任何network id
}
}
};
 

这里是设置我们稍后要部署智能合约的位置, 否则会报网络错误。

编写合约

进入contracts目录,这里是存放合约代码的地方。我们可以使用sublime等工具编写测试合约代码,然后保存MyTest.sol文件。

pragma solidity ^0.4.4;
contract MyTest {
function multiply(uint a) public pure returns(uint d) {
return a * 7;
} function say() public pure returns (string) {
return "Hello Contract";
}
}
 

合约内容很简单,就是输入一个整数,返回它乘以7的结果。然后有一个函数say()输出一串字符。函数声明为pure,在这种情况下,它们承诺不会从该状态中读取或修改该状态

编译部署

修改migrations下的1_initial_migration.js文件,改成如下:

var Migrations = artifacts.require("./Migrations.sol");
var MyTest = artifacts.require("./MyTest.sol"); module.exports = function(deployer) {
deployer.deploy(Migrations);
deployer.deploy(MyTest);
};
 

原来的部署不动,只是新增一个我们自己写的合约(MyTest)。

开始编译,

pony@pony-virtual-machine:~/solidity/testproject/migrations$ sudo truffle compile --compile-all
Compiling ./contracts/Migrations.sol...
Compiling ./contracts/MyTest.sol... Compilation warnings encountered: /home/pony/solidity/testproject/contracts/MyTest.sol:3:5: Warning: No visibility specified. Defaulting to "public".
function multiply(uint a) returns(uint d) {
^
Spanning multiple lines.
,/home/pony/solidity/testproject/contracts/MyTest.sol:3:5: Warning: Function state mutability can be restricted to pure
function multiply(uint a) returns(uint d) {
^
Spanning multiple lines. Writing artifacts to ./build/contracts
 

Truffle仅默认编译自上次编译后被修改过的文件,来减少不必要的编译。如果你想编译全部文件,可以使用–compile-all选项。

然后会多出一个build目录,该目录下的文件都不要做任何的修改。

部署,

pony@pony-virtual-machine:~/solidity/testproject$ sudo truffle migrate --reset

Using network 'development'.

Running migration: 1_initial_migration.js
Deploying Migrations...
... 0x65b047027d1fac616d3e87dba99d8a971873a001753ea1e0c834599710c4a4d4
Migrations: 0x01f6ddb59ead2e893504830c4bc0bba9845b8104
Deploying MyTest...
... 0x92358d38b5cde5d89d0133e19082fae422f581aad05a1fcdd146bc1f4ae181c9
MyTest: 0x2b4c4ba201ada20fe5983a4e52b983858f39bb8e
Saving successful migration to network...
... 0xb3767703f49b42a3a3bc3d0fe193a431b01d8600a0f8bc6d24a926a23f16ba8a
Saving artifacts...
 

这个命令会执行所有migrations目录下的js文件。如果之前执行过truffle migrate命令,再次执行,只会部署新的js文件,如果没有新的js文件,不会起任何作用。如果使用–reset参数,则会重新的执行所有脚本的部署。

执行

truffle migrate
 

如果以前有编译过别的乱七八糟的合约,怕环境出问题,可以使用

truffle migrate –reset。
 

我自己就是执行migrate一直不成功,带上reset就可以了。

如果要部署到指定的网络,可以使用–network参数,例如:

truffle migrate --network live
 

多个网络的配置格式如下:

networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // match any network
},
live: {
host: "178.25.19.88", // Random IP for example purposes (do not use)
port: 80,
network_id: 1, // Ethereum public network
// optional config values:
// gas Gas limit used for deploys. Default is 4712388
// gasPrice Gas price used for deploys. Default is 100000000000 (100 Shannon).
// from - default address to use for any transaction Truffle makes during migrations
// provider - web3 provider instance Truffle should use to talk to the Ethereum network.
// - if specified, host and port are ignored.
}
}
 

简单测试下, 进入truffle控制台,

$ truffle console
truffle(development)> var contract;
undefined
truffle(development)> MyTest.deployed().then(function(instance){contract= instance;});
undefined
truffle(development)>
undefined
truffle(development)>
undefined
truffle(development)> contract.multiply(3)
{ [String: '21'] s: 1, e: 1, c: [ 21 ] }
truffle(development)>
undefined
truffle(development)> contract.say()
'Hello Contract'
 

输入.exit可以退出控制台。

自动化测试

上面只是简单测试了下,那么如何编写测试代码进行自动化测试呢?

项目目录下的test子目录用了存放测试代码文件,我们可以编写测试代码然后保存在改目录下,这样当执行

truffle test

的时候,测试代码就会被执行。

编写测试代码,保存为TestMyTest.sol,代码如下:

pragma solidity ^0.4.0;  

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/MyTest.sol"; contract TestMyTest {
uint someValue;
uint value;
function testmultiply(){
someValue=3;
MyTest aaa=MyTest(DeployedAddresses.MyTest());
value = aaa.multiply(someValue);
uint expected = 21;
Assert.equal(value, expected, "should 3*7=21");
}
}
 

执行测试,

pony@pony-virtual-machine:~/solidity/testproject$ sudo truffle test
Using network 'development'. Compiling ./contracts/MyTest.sol...
Compiling ./test/TestMyTest.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol... TestMyTest
✓ testmultiply (83ms) 1 passing (548ms)
 

在testrpc以太坊测试环境部署智能合约的更多相关文章

  1. 区块链入门到实战(18)之以太坊(Ethereum) – 什么是智能合约

    作用:提供优于传统合约的安全方法,并减少与合约相关的其他交易成本. 以太坊网络基石:以太坊虚拟币和智能合约. 智能合约(Smart contract )是一种旨在以信息化方式传播.验证或执行合同的计算 ...

  2. [币严区块链]简单易懂的以太坊(ETH)智能合约开发入门教程

    以太坊(Ethereum)是一提供个智能合约(smart contract)功能的公共区块链(BlockChain)平台. 本文介绍了一个简单的以太坊智能合约的开发过程. 开发环境 在以太坊上开发应用 ...

  3. 以太坊开发教程(二) 利用truffle发布宠物商店 DAPP 到 以太坊测试环境Ropsten

    1.环境安装 1) node安装 设置镜像地址: curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -下载安装 ...

  4. 从零构建以太坊(Ethereum)智能合约到项目实战——第23章 从零构建和部署去中心化投票App,decentralization Voting Dapp

    P90 .1-从零构建和部署去中心化投票App-01 P91 .2-从零构建和部署去中心化投票App-02 P92 .3-从零构建和部署去中心化投票App-03 参考博文:http://liyuech ...

  5. 从零构建以太坊(Ethereum)智能合约到项目实战——第20章 搭建自己的私有链网络

    P75 .1-以太坊私网建立 .合约编译.部署完全教程(1) 使用此博文进行安装配置:https://blog.csdn.net/w88193363/article/details/79402074 ...

  6. 从零构建以太坊(Ethereum)智能合约到项目实战——第22章 玩转truffle framework 、Web3.js 框架

    P84 .1-玩转truffle framework.Web3.js 框架 内容介绍 truffle官方网站:https://truffleframework.com/ P85 .2-truffle ...

  7. 从零构建以太坊(Ethereum)智能合约到项目实战——学习笔记10

    P57 .1-Solidity Types - 玩转 Solidity 数组 (Arrays) 学习目标 1.掌握Arrays的可变不可变的创建 2.深度理解可变数组和不可变数组之间的区别 3.二维数 ...

  8. 从零构建以太坊(Ethereum)智能合约到项目实战——第21章 搭建联盟链

    P78 .1-内容介绍 什么情况下建立自己测试用的PoA chain? 公司内网或无对外网络,无法同步区块 降低测试时等待区块的时间 不想碰到testrpc各种雷 PoA chain特点有 有别于Po ...

  9. 从零构建以太坊(Ethereum)智能合约到项目实战——第24章 IPFS + 区块链

    P93 .1-IPFS环境配置P94 .2-IPFS+P .IPNS+P .个人博客搭建 - 如何在IPFS新增一个文件P95 .3-IPFS+P .IPNS+P .个人博客搭建 - 通过ipfs创建 ...

随机推荐

  1. django-两种方式对单表的操作

    单表操作的内容 我们这里对数据库单表的操作包含增删改查四部分 具体链接数据库的方式我们是通过pymysql,当然你也可以用其他的. 两种方式的概念与区别 1.新url的方式 主要就是我们每一次向后台提 ...

  2. CVE-2018-2879 - anniversary

    For the anniversary of the discovery of CVE-2018-2879 by Sec Consult (https://sec-consult.com/en/blo ...

  3. curl函数错误码对照信息表

  4. 【leetcode】566. Reshape the Matrix

    原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...

  5. Python asyncio 模块

    Python 3.4 asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLo ...

  6. win10锁屏壁纸文件夹Assets中无文件问题的解决方法

    一.前言 win10在锁屏时会有很多精美的壁纸,在网上查找到win10锁屏壁纸存放目录为 : C:\Users\你的用户名\AppData\Local\Packages\Microsoft.Windo ...

  7. Excel 教程一

    俗话说,工欲善其事,必先利其器,那么我们今天就先来看一下这个excel软件的一些主要功能菜单. 一.功能区菜单 功能区菜单主要包括: 文件菜单:  主要用于新建文件,保存文件,另存为文件,打开文件,打 ...

  8. MongoDB C#samus驱动

    MongoDB的c#驱动有两种,官方驱动和samus驱动,不过我更喜欢samus驱动,因为samus驱动提供了丰富的linq操作. 官方驱动:https://github.com/mongodb/mo ...

  9. Red Hat Enterprise Linux 7.0

    简介 Red Hat Enterprise Linux是Red Hat公司的Linux发行版,面向商业市场,包括大型机.红帽公司从Red Hat Enterprise Linux 5开始对企业版LIN ...

  10. 在stm32开发可以调用c标准库的排序和查找 qsort bsearch

    在嵌入式开发中,可以使用c标准库自带的库函数,而不用自己去早轮子,qsort 和bsearch就是其中的两个比较好用的 二分法查找,前提是已经排序好的数据.下面的代码, 如果数据为排序,则要进行排序后 ...