美文网首页Dapp开发
以太坊私链搭建及智能合约部署

以太坊私链搭建及智能合约部署

作者: droid_zf | 来源:发表于2018-10-15 16:26 被阅读71次

    Ubuntu

    安装客户端

    1.源码安装(需go环境支持)
    git clone https://github.com/ethereum/go-ethereum.git
    cd  go-ethereum
    git checkout v1.7.2
    make geth
    make all
    
    2.直接安装
    apt-get update
    apt-get install software-properties-common
    add-apt-repository -y ppa:ethereum/ethereum
    apt-get update
    apt-get install ethereum
    

    安装Solidity 编译器

    apt-get install npm
    npm install -g solc
    

    私链配置及初始化

    新建genesis.json文件并添加如下内容

    {
    "config": {
        "chainId": 10,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
        },
    "nonce":"0x0000000000000042",
    "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
    "difficulty": "0x4000",
    "alloc": {},
    "coinbase":"0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x88",
    "gasLimit":"0xffffffff"
    }
    
    geth  --datadir "$basepath/chain" init genesis.json
    #成功提示Successfully wrote genesis...
    geth --identity "0x88" --rpc --rpccorsdomain "*" --datadir "$basepath/chain" --port "30303" --rpcapi "db,eth,net,web3" --networkid 95518 console  
    #进入geth交互控制台,后台运行geth attach http://127.0.0.1:8545进入控制台
    > personal.newAccount()
    #生成新账户
    > miner.start()
    #开始挖矿
    

    部署合约

    新建合约testContract.sol添加测试代码

    pragma solidity ^0.4.0;
    contract test
    {
       uint storedData;
       function set(uint x) public {
            storedData = x;
        }
    
        function get() public view returns (uint) {
            return storedData;
        }
    }
    
    #生成bin文件
    solcjs --bin testContract.sol
    #生成abi文件
    solcjs --abi testContract.sol
    

    记下bin和abi文件内容,返回geth控制台

    > code = "0x608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058202d0f5da7a2fe6b2ea8e06f9b43e4f23466d7f5475b52559b863b3a381cdd23450029"
    > abi = [{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]
    #解锁账户
    > personal.unlockAccount(eth.accounts[0])
    U
    #发送合约
    > myContract = eth.contract(abi)
    > contract = myContract.new({from:eth.accounts[0],data:code,gas:1000000})
    #如果没挖矿txpool.status查看是否有待确认交易,eth.getBlock("pending", true).transactions查看信息,启动挖矿后交易确认可以执行合约
    

    调用合约

    > contract.set(123,{from:eth.accounts[0]})
    > contract.get({from:eth.accounts[0]})
    #调用get可以看到返回123
    

    相关文章

      网友评论

        本文标题:以太坊私链搭建及智能合约部署

        本文链接:https://www.haomeiwen.com/subject/qbwfzftx.html