Geth搭建私有链

geth是go版本的客户端,是目前主流的链接以太坊网络的客户端

创建

1
geth --datadir node1 init genesis.json

启动私有链节点

1
geth --datadir "./node1" --networkid 72 --port 3100

交互连接节点

1
geth attach ipc:\\.\pipe\geth.ipc

创建账户

1
2
3
4
> personal.newAccount("ck1")
"0x1850e5e25c4698dc45a176cebc55491740d96a76"
> personal.newAccount("ck2")
"0xc86f6ef05ff2a98f7fed3dffb7fbd11cc9cd3ab1"

查看挖矿人。默认第一个账户

1
2
> eth.coinbase
"0x1850e5e25c4698dc45a176cebc55491740d96a76"

设置挖矿人

1
2
> miner.setEtherbase(eth.accounts[1])
true

查看账户余额

1
2
> eth.getBalance(eth.accounts[0])
0

开始挖矿

1
2
> miner.start()
null

结束挖矿

1
2
> miner.stop()
null

查看余额,返回单位为wei

1
2
> eth.getBalance(eth.accounts[1])
285000000000000000000

查看余额,返回单位为ether

1
2
> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')
285

查看挖矿数,每挖一个矿奖励5个ether

1
2
> eth.blockNumber
57

手动设置防止其他人连接,重新启动节点

1
geth --datadir "./node1" --networkid 72 --port 3100 --nodiscover

创建另一个节点,进行交互

1
geth --datadir node2 init genesis.json

启动节点并交互

1
geth --datadir "./node2" --networkid 72 --nodiscover --ipcdisable console --port 3200

查看当前节点信息

1
admin.nodeInfo

复制enode的信息,并在第一个节点进行连接

1
admin.addPeer("enode://8db28a590067d581faf750fee75fb0b8113d72e4ee7a262e4ba0a3d45ba21bdb169106fd0b1e93addcd445c61f4e6da3c6ed57a15d429ec21f09836f626494dc@127.0.0.1:3200?discport=0")

目前,节点1的accounts[1]有币。我们向另一个节点的accounts[0]转账

解锁账户

1
personal.unlockAccount(eth.accounts[1])

发起转账

1
eth.sendTransaction({from:eth.accounts[1],to:"0xa1ed106e83f678aa82b22e1331cbb9956605f463",value:web3.toWei(15,'ether')})

查看交易

1
txpool.status

这时候交易还没有打包,因为没有进行挖矿

1
2
miner.start()
miner.stop()

挖矿后,交易被完成,显示交易得到15个ether

1
> eth.getBalance(eth.accounts[0])

与Mist进行连接,首先要重新启动节点,设置ipcpath

1
geth --datadir "./node1" --networkid 72 --port 30301 --ipcpath \\.\pipe\geth.ipc console

geth –datadir “./node1” –ipcpath \.\pipe\geth.ipc –networkid 72 –rpc –rpcaddr “0.0.0.0” console