美文网首页Dapp开发以太坊ethereum开发
以太坊智能合约开发(1)

以太坊智能合约开发(1)

作者: 糖果果老师 | 来源:发表于2018-05-30 11:59 被阅读46次

    下载go-ethereum编译安装

    git clone https://github.com/ethereum/go-ethereum.git
    make all
    

    创建data目录存区块数据

    mkdir data
    

    创建初始账户

    ./go-ethereum/build/bin/geth  -datadir ./data/ account new
    //bd307b93ea8adf8136ffbb8eee9d20464ce94d2e   123456
    "enode://d664accf24fad9eb0e7fe5a46209f7af369d9ed0a302a6b03f42883b97bf5f97581eba1edf98629bf016b3cdf652045a04b716af69c8be0ee0153e7160b5f034@[::]:30303"
    

    创建创世区块 genesis.json

    {
      "config": {
            "chainId": 10,
            "homesteadBlock": 0,
            "eip155Block": 0,
            "eip158Block": 0
        },
      "alloc"      : {
      "0xbd307b93ea8adf8136ffbb8eee9d20464ce94d2e":{"balance":"100000000000000"}
      },
      "coinbase"   : "0x0000000000000000000000000000000000000000",
      "difficulty" : "0x02000000",
      "extraData"  : "",
      "gasLimit"   : "0x2fefd8",
      "nonce"      : "0x0000000000000042",
      "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
      "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
      "timestamp"  : "0x00"
    }
    
    

    初始化创世区块

    ./go-ethereum/build/bin/geth -datadir ./data/ init ./genesis.json
    
    

    启动以太坊网络 指定RPC通讯端口

    ./go-ethereum/build/bin/geth -datadir ./data/ --networkid 10  --rpc --rpcaddr localhost --rpcport "8545" console
    
    

    设置挖矿账户 开始挖矿

    miner.setEtherbase(eth.accounts[0])
    miner.start(1)//设置了挖矿的线程数目
    
    

    利用web3测试

    package.json
    
    {
      "name": "contract",
      "version": "1.0.0",
      "description": "a contract of eth",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [
        "eth"
      ],
      "author": "llG",
      "license": "ISC",
      "dependencies": {
        "solc": "^0.4.19",
        "web3": "^0.20.4"
      }
    }
    
    

    安装依赖

    npm install
    
    

    编写测试脚本

    index.js
    
    
    const fs = require("fs");
    const solc = require('solc')
    const net = require('net')
    
    
    /// 引入web3 ,版本 0.20.4
    
    let Web3 = require('web3');
    let web3;
    
    if (typeof web3 !== 'undefined') {
      web3 = new Web3(web3.currentProvider);
    } else {
      // set the provider you want from Web3.providers
      web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    }
    
    //区块查询
    console.log(web3.eth.accounts[0])
    web3.eth.getBlock(1, function(error, result){
        if(!error){
            console.log(result)
            }
        else{
           console.log(error);
           }
    })
    
    

    停止挖矿

    miner.stop()
    
    

    查询网络端口

    netstat -nlp | grep eth
    
    

    相关文章

      网友评论

      • 8337ea5e8883:您好,看到您的文章质量非常高,想邀请您成为虫洞社区的首批优质内容签约作者。虫洞社区是专业的区块链技术学习社区。虫洞社区鼓励内容生产者产生高质量内容,并给予合理的回报,也希望能帮助内容消费者获得高质量的区块链内容,并让数字货币投资者获得有价值的投资洞见。同时,虫洞社区已经积累了大量的区块链深度从业者,便于作者建立个人品牌。不知道是否方便加您微信细聊?

      本文标题:以太坊智能合约开发(1)

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