区块链搭建以及智能合约的运行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LNZ001/article/details/72374982

( 强烈建议所有操作在root下搭建环境和运行. )

安装环境ethereum&solc

(此处参考:区块链技术-智能合约-以太坊(译文) , smartsponsor-github )

  • 安装ethereum,solc

    sudo apt-get install software-properties-common
    
    sudo add-apt-repository -y ppa:ethereum/ethereum
    
    sudo apt-get update
    
    sudo apt-get install ethereum
    
    sudo apt-get install solc
    

配置创世块

  • 创建genesis.json,内容如下,复制至/root/.ethereum下

    {

    “nonce”: “0x0000000000000042”,

    “timestamp”: “0x0”,

    “parentHash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,

    “extraData”: “0x0”,

    “gasLimit”: “0x8000000”,

    “difficulty”: “0x400”,

    “mixhash”: “0x0000000000000000000000000000000000000000000000000000000000000000”,

    “coinbase”: “0x3333333333333333333333333333333333333333”,

    “alloc”: { }

    }

  • 执行初始化:

    geth --nodiscover --maxpeers 0 init genesis.json
    
  • 打开私有链方法:

    geth --datadir "~/ethdev" --dev console 2>> geth.log
    
  • 另:

    geth --rpc --rpcport 8000 --rpccorsdomain '"*"' --mine --minerthreads 1 --maxpeers 0 --nodiscover --networkid 3301 console

安装node

(此处参考:https://bitshuo.com/topic/58ce7b2d0a3de8932e6f75ba)

  • 此处需要先安装nodejs. ubuntu直接安装的node版本过低,选择直接在官网上下载:https://nodejs.org/en/download/,注意应下载源码.将其复制到/root目录下

    cd node
    
    ./configure
    
    make -j4
    
    sudo make install
    

安装testrpc

(此处参考:http://blog.csdn.net/wo541075754/article/details/53155578)

  • 执行命令安装testrpc:

    sudo npm install -g ethereumjs-testrpc
    
  • 执行testrpc测试,报错:

    Secp256k1 bindings are not compiled. Pure JS implementation will be used.

    ……

    Error: Module version mismatch. Expected 48, got 46.

    at Error (native)

    at Object.Module._extensions..node (module.js:597:18)

    at Module.load (module.js:487:32)

    at tryModuleLoad (module.js:446:12)

    at Function.Module._load (module.js:438:3)

    at Module.require (module.js:497:17)

    at require (internal/module.js:20:19)

    at Object. (/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/scrypt/index.js:3:20)

    at Module._compile (module.js:570:32)

    at Object.Module._extensions..js (module.js:579:10)

  • 解决办法: 打开报错文件/usr/local/lib/node_modules/ethereumjs-testrpc/node_modules/scrypt/index.js:存在

    “use strict”;

    var scryptNative = require(“./build/scrypt”)

    , Crypto = require(“crypto”)

    , Os = require(“os”);

  • 实际上./build/scrypt不存在,而同级目录下有scrypt,修改为:

    “use strict”;

    var scryptNative = require(“scrypt”)

    , Crypto = require(“crypto”)

    , Os = require(“os”);

  • 再次testrpc,通过.

    这里写图片描述


安装truffle

  • 执行命令安装truffle

    sudo npm install -g truffle
    

1. eth.getCompilers()为空问题?

  • 解决办法:
    这里写图片描述

    被某文章误导,将安装的solc和solcjs删除(/usr/local/bin/和/usr/local/lib/node_modules/下)
    这里写图片描述

    sudo apt-get remove solc, sudo apt-get install solc

    手动添加 admin.setSolc(“”)

    即可.

2. info和code找不到?

  • 解决办法:

    srcCompiled.smartSponsor
    
    改为
    
    srcCompiled["<stdin>:smartSponsor"]
    

3. loadScript(“./smartsponsor.js”)只有信息true,而没有log的问题?

  • 由于4个账户没有解锁,解决办法:

    personal.unlockAccount(eth.accounts[0],"123456",10000)
    
    personal.unlockAccount(eth.accounts[1],"123456",10000)
    
    personal.unlockAccount(eth.accounts[2],"123456",10000)
    
    personal.unlockAccount(eth.accounts[3],"123456",10000)
    
    > loadScript("./smartsponsor.js")
    
    Contract transaction send: TransactionHash: 0x6cccd9aff2063101500b9e400ac43955dd1f58cde63142f038b6337cb30d37e8 waiting to be mined...
    
    true
    
    
    
    > Contract mined! Address: 0xc9274287d6365cb235ffbbc871ae75972677a090
    
    [object Object]
    
    
    
    

4. 支付问题?

  • Error: Cannot send value to non-payable function.——无法支付到非支付功能.(问题未解决.考虑到可能为个例,最好再尝试其他合约.)

    这里写图片描述
    解决办法,需要在合约源码中修改,function pledge添加”payable”:
    function pledge(bytes32 _message) payable { … }


实例测试完成.

猜你喜欢

转载自blog.csdn.net/LNZ001/article/details/72374982
今日推荐