以太坊私有链搭建
本文根据汪晓明的视频资料整理,ubuntu16.04测试正确。
代码块
安装go-ethereum客户端
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo add-apt-repository -y ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install ethereum
指定工作目录,以防止跟共有链混淆,进入开发模式
geth --datadir "~/pengfan/eth" --dev
另外重新启动一个geth控制台,将日志文件输出到制定文件中,
geth --dev console 2>>file_to_log_output
Welcome to the Geth JavaScript console!
instance: Geth/v1.5.5-stable-ff07d548/linux/go1.7.3
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0
重新启动一个窗口,输出日志文件
tail -f file_to_log_output
添加用户,指定密码
> eth.accounts
[]
> personal.newAccount('123456')
"0x3daef47c6c40b25ebb720ab6313947b069568b54"
> eth.accounts
["0x3daef47c6c40b25ebb720ab6313947b069568b54"]
> personal.newAccount('123456')
"0x51366d4f2d9209c0d776df6ae10dff051d6a1acb"
> user1=eth.accounts[0]
"0x3daef47c6c40b25ebb720ab6313947b069568b54"
> user1
"0x3daef47c6c40b25ebb720ab6313947b069568b54"
> user2=eth.accounts[1]
"0x51366d4f2d9209c0d776df6ae10dff051d6a1acb"
> user2
"0x51366d4f2d9209c0d776df6ae10dff051d6a1acb"
查询余额和区块号
> eth.getBalance(user1)
0
> eth.getBalance(user2)
0
> eth.blockNumber
0
开始挖矿
> miner.start()
true
> eth.getBalance(user1)
15000000000000000000
> eth.getBalance(user2)
0
> miner.stop()
true
> eth.blockNumber
3
解锁账户
>eth.sendTransaction({from:user1,to:user2,value:web3.toWei(3,"ether")})
Error: account is locked
at web3.js:3119:20
at web3.js:6023:15
at web3.js:4995:36
at <anonymous>:1:1
> eth.accounts
["0x3daef47c6c40b25ebb720ab6313947b069568b54", "0x51366d4f2d9209c0d776df6ae10dff051d6a1acb"]
>personal.unlockAccount("0x3daef47c6c40b25ebb720ab6313947b069568b54","123456")
true
转账,挖矿确认
>eth.sendTransaction({from:user1,to:user2,value:web3.toWei(3,"ether")})
"0xec8c7a510c217ff2b58a00b0f842e075551d19b615373bccaa61cfeacd3de27b"
> eth.getBalance(user2)
0
> miner.start()
true
> miner.stop()
true
> eth.getBalance(user2)
0
> eth.blockNumber
3
> miner.start()
true
> eth.getBalance(user2)
3000000000000000000
> miner.stop()
true
> eth.blockNumber
4