搭建本地以太坊私链
访问量:
macOS以太坊安装
$ brew tap ethereum/ethereum
$ brew install ethereum
创建ethereum_home
$ cd ~
$ mkdir ethereum
$ echo 'export ethereum_home=$HOME/ethereum' >> ~/.zshrc
$ source ~/.zshrc
创建创世纪(Genesis)文件
创世纪区块是区块链上的第一个区块,Genesis文件是一个JSON文件,描述了初始区块的特性。
$ cd $ethereum_home
$ touch genesis.json
genesis.json内容如下:
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000",
"difficulty": "0x400",
"config":{},
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {}
}
启动以太坊Node
$ geth --datadir "$ethereum_home/eth1" init "$ethereum_home/genesis.json"
$ geth --datadir "$ethereum_home/eth1" --networkid 1234 console 2>>eth1.log
通过geth.ipc可以将多个终端attach同一个console
$ geth attach ipc:$ethereum_home/eth1/geth.ipc
启动Peer Node
采用同样的genesis.json
$ geth --datadir "$ethereum_home/eth2" init "$ethereum_home/genesis.json"
$ geth --datadir "$ethereum_home/eth2" --port 30304 --nodiscover --networkid 1234 console 2>>eth2.log
通过admin.nodeInfo获取enode
> admin.nodeInfo
{
enode: "enode://30312c1a5e16a1698c3b901fec03d5ae81a094dd913f1a480d5d82fad0be26911b2d1abe2b63f0b21948aa0ab4815a70880ca7d8ac19c2abd047d2e75e77552a@[::]:30304?discport=0",
id: "30312c1a5e16a1698c3b901fec03d5ae81a094dd913f1a480d5d82fad0be26911b2d1abe2b63f0b21948aa0ab4815a70880ca7d8ac19c2abd047d2e75e77552a",
ip: "::",
listenAddr: "[::]:30304",
name: "Geth/v1.8.11-stable/darwin-amd64/go1.10.3",
ports: {
discovery: 0,
listener: 30304
},
protocols: {
eth: {
config: {
chainId: null,
eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
},
difficulty: 1024,
genesis: "0x6231b02ac967dff9b0c799e956094408959de862d720d08776302ffefba0300b",
head: "0x6231b02ac967dff9b0c799e956094408959de862d720d08776302ffefba0300b",
network: 1234
}
}
}
切换到console 1, admin.addPeer
> admin.addPeer("enode://30312c1a5e16a1698c3b901fec03d5ae81a094dd913f1a480d5d82fad0be26911b2d1abe2b63f0b21948aa0ab4815a70880ca7d8ac19c2abd047d2e75e77552a@[::]:30304?discport=0")
> addmin.peers
运行admin.peers,我们可以看到peer的信息了
启动挖矿
> eth.blockNumber
0
这里我们看到区块数目是0,因为还没有开始挖矿。挖矿前需要建立account,否则执行挖矿会报错(Error: etherbase missing)
> personal.newAccount('123456')
"0x9fc496826bc0745af978c7cad7d90ddee06a6293"
> miner.start(1)
> eth.blockNumber
4
> eth.blockNumber
6
我们可以在两个Node的Console上都可以看到blockNumber在不断增加,虽然只是在一个Node上进行挖矿。start(1)这里的1代表一个线程挖矿。 通过下面命令可以获取block信息和最新的block对应的miner
> eth.getBlock(5)
> eth.getBlock(eth.blockNumber).miner
停止挖矿
> miner.stop()
测试交易
新建一个账户,查看其余额
> personal.newAccount("123456)
0xd870d9a0cbfa37a53563f12a783fb697027f5668
> eth.getBalance("0xd870d9a0cbfa37a53563f12a783fb697027f5668")
0
查看之前挖矿的账户余额,用之前账户转20 Wei
> eth.getBalance("0x9fc496826bc0745af978c7cad7d90ddee06a6293")
1.01968750000000000002e+21
> eth.sendTransaction({from:"0x9fc496826bc0745af978c7cad7d90ddee06a6293", to:"0xd870d9a0cbfa37a53563f12a783fb697027f5668",value:20})
Error: authentication needed: password or unlock
at web3.js:3143:20
at web3.js:6347:15
at web3.js:5081:36
at <anonymous>:1:1
unlockAccount
> personal.unlockAccount("0x9fc496826bc0745af978c7cad7d90ddee06a6293")
> eth.sendTransaction({from:"0x9fc496826bc0745af978c7cad7d90ddee06a6293", to:"0xd870d9a0cbfa37a53563f12a783fb697027f5668",value:20})
这时候我们再次查看新账号余额会发现仍旧是0,这是因为我们之前停止了挖矿,重新开启挖矿,过几秒之后我们发现新账号余额增加了20 wei
> eth.getBalance("0xd870d9a0cbfa37a53563f12a783fb697027f5668")
0
> miner.start(1)
> eth.getBalance("0xd870d9a0cbfa37a53563f12a783fb697027f5668")
20
API
- admin.*
- personal.*
- eth.*
> admin.nodeInfo > personal.newAccount() > personal.listAccounts > eth.blockNumber