geth puppethを使った プライベート・ネットワーク立ち上げ
Ethereumクライアントのgethを使って、プライベート・ネットワークを作成します。
プライベート・ネットワークを作成する際、本来自分でgenesisファイルを作成しなければならないのですが、gethに搭載されているpuppethというツールを使うことで、簡単にgenesisファイルが作成できます。
(puppethの解説⬇️)
https://blog.ethereum.org/2017/04/14/geth-1-6-puppeth-master/
https://modalduality.org/posts/puppeth/
❶puppethでgenesisファイル作成
private-netというフォルダをホームディレクトリ配下に用意しました。以降、このディレクトリでコマンドを実行します。
$ puppeth +-----------------------------------------------------------+ | Welcome to puppeth, your Ethereum private network manager | | | | This tool lets you create a new Ethereum network down to | | the genesis block, bootnodes, miners and ethstats servers | | without the hassle that it would normally entail. | | | | Puppeth uses SSH to dial in to remote servers, and builds | | its network components out of Docker containers using the | | docker-compose toolset. | +-----------------------------------------------------------+
まず、ネットワークに名前をつけます。
Please specify a network name to administer (no spaces or hyphens, please) > privateNetwork
2番を選択します。
What would you like to do? (default = stats) 1. Show network stats 2. Configure new genesis 3. Track new remote server 4. Deploy network components > 2
コンセンサスアルゴリズムを選びます。今回は、1番のproof-of-workを選択します。
Which consensus engine to use? (default = clique) 1. Ethash - proof-of-work 2. Clique - proof-of-authority > 1
今回は、prefundしないので0xのままenter。
Which accounts should be pre-funded? (advisable at least one) > 0x
ネットワークIDを指定します。今回は4224を指定します。
Specify your chain/network ID if you want an explicit one (default = random) > 4224 INFO [04-08|22:33:57] Configured new genesis block
What would you like to do? (default = stats) 1. Show network stats 2. Manage existing genesis 3. Track new remote server 4. Deploy network components > INFO [04-08|22:34:14] No remote machines to gather stats from
2番を選択し、genesisブロックを操作します。
What would you like to do? (default = stats) 1. Show network stats 2. Manage existing genesis 3. Track new remote server 4. Deploy network components > 2
2番を選択し、genesisファイルをエクスポートします。
1. Modify existing fork rules 2. Export genesis configuration 3. Remove genesis configuration > 2
enterをクリックします。
Which file to save the genesis into? (default = privateNetwork.json) > INFO [04-08|22:34:35] Exported existing genesis block What would you like to do? (default = stats) 1. Show network stats 2. Manage existing genesis 3. Track new remote server 4. Deploy network components > ^C
❷プライベートインスタンスの初期化
$ geth --datadir ~/private-net init privateNetwork.json
⇨geth, keystoreファイルが作成されます。
また、アカウントを作成することができます。(パスワードを設定してください)
$ geth --datadir . account new
❹startnode.shファイル作成
startnode.sh geth --networkid 4224 --mine --minerthreads 1 --datadir "~/private-net" --nodiscover --rpc --rpcport "8545" --port "30303" --rpccorsdomain "*" --nat "any" --rpcapi eth,web3,personal,net --unlock 0 --password ~/private-net/password.sec --ipcpath "~/Library/Ethereum/geth.ipc"
注)private-net部分は、自分が作成したディレクトリ名に変更してください。
各オプションの意味は、下のリンクをみてください。
https://book.ethereum-jp.net/first_use/connect_to_private_net.html
次に、アカウントロック解除用の、パスワードファイルpassword.secを作成します。
****
❺プライベートネットワーク立ち上げ
先ほど作成したstartnode.shのアクセス権を変更します。
$ chmod +x startnode.sh
startnode.shを実行します。
$ ./startnode.sh
⇨プライベートネットワークが立ち上がります。
❻gethコンソールの操作
geth attach > eth.accounts ["0x4563747acfa25bbca20438a81bdd516529670dc7", "0x540e5d6b883c345120bc244ab1194fe69e799ac1", "0xf68778cf200251c46d047f05eca60cb9fd4c63ef"] > eth.getBalance(eth.accounts[0]) 5.991e+21
などなど。
gethコンソールの操作は以下のリンクをみると良いと思います。
https://qiita.com/amachino/items/b59ec8e46863ce2ebd4a