2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

macにgethをインストールして、private netでコマンドを実行してみる。

Last updated at Posted at 2018-12-23

この記事では、macOS High Sierra 10.13.6を使っています。
Ethereumの環境構築をして、プライベートネットで動かす方法についてまとめてみます。
今回はクライアントとして、gethの1.8.20-stableを利用してます。

1. macにgethをインストール

brew tap ethereum/ethereum
brew install ethereum

// バージョン確認
geth version

Geth
Version: 1.8.20-stable
〜省略〜

2. データ用のディレクトリを作成する

mkdir /path/to/home/eth_private_net

3. genesis.json(初期ブロック)を作成

cd /path/to/home/eth_private_net
vim genesis.json 
genesis.json
{
    "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "nonce": "0x0000000000000042",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "difficulty": "0x00",
    "alloc": {},
    "coinbase": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x00",
    "gasLimit": "0x1312d00"
}

4. 初期化処理を実行

geth --datadir /path/to/home/eth_private_net init /path/to/home/eth_private_net/genesis.json

5. geth起動

geth --networkid "10" --nodiscover --datadir "/path/to/home/eth_private_net" --rpc --rpcaddr "localhost" --rpcport "8545" --rpccorsdomain "*" --rpcapi "eth,net,web3,personal" --targetgaslimit "20000000" console 2>> /path/to/home/eth_private_net/geth_err.log


// 下記が出力されたらOK
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.20-stable/darwin-amd64/go1.11.2
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

>

6. コマンド実行

アカウント作成

> personal.newAccount("test")
"0x1c5dcf3fb4614a82adcf09bc194365a127ff272a"

アカウントリスト確認

> eth.accounts
["0x1c5dcf3fb4614a82adcf09bc194365a127ff272a"]

index指定でアカウント確認

> eth.accounts[0]
"0x1c5dcf3fb4614a82adcf09bc194365a127ff272a"

coinbaseアカウント(mining報酬を受取るアカウント)の確認

> eth.coinbase
"0x1c5dcf3fb4614a82adcf09bc194365a127ff272a"

genesisブロックの確認

> eth.getBlock(0)
{
  difficulty: 0,
  extraData: "0x00",
  gasLimit: 20000000,
  gasUsed: 0,
  hash: "0x76d747ec34337ec5677b1aba554769485e160663eee3c63486400bddc21a5e65",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x0000000000000000000000000000000000000000",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000042",
  number: 0,
  parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 505,
  stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  timestamp: 0,
  totalDifficulty: 0,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

マイニング開始

> miner.start(2)
null

マイニングステータス確認

> eth.mining
true

アカウント残高確認

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

// 少し待つ
> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
80

送金テスト用にアカウントを作成

> personal.newAccount("test")
0x581206828d6faf24fc8b842d0d3544da5c180210

送金

// 送金元のアカウントロックを解除
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x1c5dcf3fb4614a82adcf09bc194365a127ff272a
Passphrase: // personal.newAccountで作成した際のパスワードを指定。この例では、「test」
true

// 送金
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(5, "ether")})
"0x906ccd91621ac73f6221a2a8ffe825c18199ca6708d4ee5fa5f73b4c9c9ca779" //txid

// 少し待つとアカウント残高が反映される
> web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
5

トランザクション確認

> eth.getTransactionReceipt("0x906ccd91621ac73f6221a2a8ffe825c18199ca6708d4ee5fa5f73b4c9c9ca779")
{
  blockHash: "0x86279ca24cd8b163ebdf22cf7ae656389278c0dcd133bc99c0ffe4558b7e9cc2",
  blockNumber: 55,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  from: "0x1c5dcf3fb4614a82adcf09bc194365a127ff272a",
  gasUsed: 21000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0x810497dd7957105d8262f21a71af0d84e30d72bd6c267946d44b7519242e34a9",
  to: "0x581206828d6faf24fc8b842d0d3544da5c180210",
  transactionHash: "0x906ccd91621ac73f6221a2a8ffe825c18199ca6708d4ee5fa5f73b4c9c9ca779",
  transactionIndex: 0
}

マイニング停止

> miner.stop()

参考書籍

参考サイト

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?