以太坊ETH开发1( Go-Ethereum搭建以太坊私链搭建)

Windown

1.首先到geth-windows-amd64-1.8.11-dea1ce05.exe 下载安装包。
2.初始化创世区块(私链才需要这一步,公链不需要)
 初始化创世区块时,要在geth根目录先创建一个genesis.json文件,内容如下:

{
  "config": {
        "chainId": 16,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc": {
  "0x83fd95f8e41f6afedd08dd6ae11db607a7a3c60c": {"balance": "666666666"},
  "0x0000000000000000000000000000000000000002": {"balance": "222222222"}
	},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

执行初始化命令(datadir为自定义的数据目录)

 geth init genesis.json --datadir=D:\ETH\data

3.执行启动命令:

geth --datadir=D:\ETH\data --rpc --rpcport 8545 --rpcaddr "0.0.0.0"  --rpcapi "personal,db,eth,net,web3" console

4.一些常用命令:

  • 查看账户列表:
web3.eth.accounts
  • 创建账户:
web3.personal.newAccount("123456789")
  • 开启挖矿:
miner.start(1)
  • 停止挖矿:
miner.stop()
  • 查询余额
 web3.eth.getBalance("0x0000000000000000000000000000000000")
  • 解锁账户
     web3.personal.unlockAccount(acc0,"123456")
  • 转账(解锁之后才能转账,挖矿才能到账)

acc0 = web3.eth.accounts[0]
“0xbe323cc4fde114269a9513a27d3e985f82b9e25d”
acc1 = web3.eth.accounts[1]
“0x3b0ec02b4193d14abdc9cc5b264b5e3f39624d70”
web3.eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(3,“ether”)})


Linux

1.下载geth源码,下载zip版本。
2.安装go语言环境:

rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm
yum install golang

中间有一步需要确认,输入小写的y,然后回车即可
3.解压第一步的安装包
进入解压目录执行make编译代码:

make

在go-ethereum-xx/build/bin目录下创建genesis.json的文本文件,内容如下:
{
“config”: {
“chainId”: 16,
“homesteadBlock”: 0,
“eip155Block”: 0,
“eip158Block”: 0
},
“alloc”: {
“0x83fd95f8e41f6afedd08dd6ae11db607a7a3c60c”: {“balance”: “666666666”},
“0x0000000000000000000000000000000000000002”: {“balance”: “222222222”}
},
“coinbase” : “0x0000000000000000000000000000000000000000”,
“difficulty” : “0x20000”,
“extraData” : “”,
“gasLimit” : “0x2fefd8”,
“nonce” : “0x0000000000000042”,
“mixhash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“parentHash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“timestamp” : “0x00”
}

4.执行初始化命令(datadir为自定义的数据目录):

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

5.启动节点:

./geth --datadir=/opt/eth/go-ethereum-1.8.11/build/bin/data --rpc --rpcport 8545 --rpcaddr 0.0.0.0  --rpcapi personal,db,eth,net,web3 console

Mac

Mac下的安装方式跟Linux一摸一样,只是genesis.json文件的内容要替换如下:

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

后台启动

在命令前后加上nohup … &,
打开控制台:./geth attach /datadir/geth.ipc

你可能感兴趣的:(区块链开发)