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

下载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

你可能感兴趣的:(以太坊智能合约开发(1))