目的
ubuntuでプライベートネットを構築し、コンソール上からマイニングと送金を行う
環境
aws
Ubuntu 16.04.3 LTS
geth 1.7.3-stable-4bb3c89d
インストール
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
# 確認
geth --help
初期設定
mkdir eth_private_net
vim eth_private_net/genesis.json
{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x600",
"alloc": {},
"coinbase": "0x3333333333333333333333333333333333333333",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000",
"config": {
"chainId": 13
}
}
# 初期化
geth --datadir /home/ubuntu/eth_private_net init /home/ubuntu/eth_private_net/genesis.json
動作確認
# コンソール起動
geth --networkid "13" --nodiscover --datadir "/home/ubuntu/eth_private_net" console 2>> /home/ubuntu/eth_private_net/geth_err.log
# 初期状態の確認(変なとこ繋がってないかの確認)
eth.getBlock(0)
> {
difficulty: 1536,
...
uncles: []
}
eth.accounts
> []
eth.blockNumber
> 0
# アカウント作成。引数はパスワードで名前ではない
personal.newAccount("hogehoge01")
personal.newAccount("hogehoge02")
eth.accounts
>['一人目のID','二人目のID']
# 採掘者。デフォルトは一人目
eth.coinbase
>'一人目のID'
# マイニングするアカウントの変更
miner.setEtherbase(eth.accounts[1])
eth.coinbase
>'二人目のID'
# 採掘
miner.start()
> null
# 採掘状況確認
eth.hashrate
> 0 #まだ掘っていない。初回だけ15分くらいかかる
> 1325 #何か値が返ってくれば掘れている
# 採掘終了
miner.stop()
# 現在の最新ブロック
eth.blockNumber
> 15
# ブロックの調査
eth.getBlock(10)
>{ブロック情報}
# 残高確認
eth.getBalance(eth.accounts[0])
> 0
eth.getBalance(eth.accounts[1])
> 800000000000000000
# 送金者のアカウントをロック解除
personal.unlockAccount(eth.accounts[1])
# 送金
eth.sendTransaction({from: eth.accounts[1], to: eth.accounts[0], value: web3.toWei(1, "ether")})
# 送金されたかな
eth.getBalance(eth.accounts[1])
> 800000000000000000 #されてない
# 送金するために採掘
miner.start()
> null
eth.hashrate
>4022
miner.stop()
# 送金されたかな
eth.getBalance(eth.accounts[1])
> 799900000000000000
eth.getBalance(eth.accounts[0])
>100000000000000 #された
exit
つづく
メモ
genesis.json
# 平たく言うと設定ファイル。
# peer接続を行う場合、これが同じでないとだめ
{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x600",
"alloc": {},
"coinbase": "0x3333333333333333333333333333333333333333",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000",
"config": {
"chainId": 13
}
}
- nonce
- 識別コード。他の環境と混じらないように任意の値にするのがいい
- difficulty
- miningの難易度。でかいほど難しい(時間がかかる)
- config
- geth1.6以降必須になった。書くことなくても
config: {}
でキーだけは定義する
- geth1.6以降必須になった。書くことなくても
- chainId
- ネットワークID。被らないように任意の値で
- 1〜4あたりは特に避けた方がいい
gethのパラメータ
接続先のID。chainIDと同じにすること
--networkid "13"
自動で接続先を見つける機能をoffにする
--nodiscover
参考
ethereum入門
https://www.gitbook.com/book/a-mitani/mastering-ethereum
Installation Instructions for Ubuntu
https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu