• 在testrpc以太坊测试环境部署智能合约


     

    版权声明:本文为博主原创文章,转载请务必注明出处,否则追究法律责任 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)
     
  • 相关阅读:
    Vue的条件渲染详解
    Vue的style绑定
    Vue的class绑定总结
    v-model详解
    MUI手势锁
    mysql主从搭建
    处理绿盟科技安全评估的系统漏洞
    微信小程序发送ajax
    微信小程序上拉下拉刷新
    微信小程序覆盖自定义组件样式
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/11200614.html
Copyright © 2020-2023  润新知