Hope is a Dream. Dream is a Hope.

非公開ブログは再開しました。

UbuntuでEthereum その1(2017年6月版)

UbuntuでEthereum その1(2017年6月版)

参考 http://qiita.com/hshimo/items/8b69975d40466022f278

ポイント –devオプションを使い開発用gethを立ち上げる.(DAG等の大きなファイル作成の必要がなくなる)

gethを使うまでには

  1. Ubuntuにgethをインストールする
  2. gestion.jsonファイルを作成する
  3. gethを初期化する
  4. personを作成する
  5. personをunlockする
  6. マイニングする
  7. 送金する
  8. トランザクションの確認

記事執筆時点でのバージョン

geth 1.6

1. ubuntuにgethをインストールする

ubuntuの場合はapt-getでインストール可能

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

2.1. 環境を作る

まずはイーサリアムの環境を作るディレクトリを作成する. ここではhome/fifi/ethereumとする.

mkdir /home/fifi/ethereum

2.2. gestion.jsonファイルを作成する

genesis.json genesis.jsonファイルを作業スペース(/home/fifi/ethereum/genesis.json)下に格納する.

{
    "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "difficulty": "200000000",
    "gasLimit": "2100000",
    "alloc": {
        "7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
        "f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
    }
}

2.3 プライベートネットワークを初期化

geth --datadir /home/fifi/ethereum init genesis.json

// 下でも初期化できた
cd /home/fifi/ethereum
geth --datadir ./ init ./genesis.json

// 開発用でgethを立ち上げたい場合は、--devオプションをつける.
geth --dev --datadir ./ init ./genesis.json

初期化した後にこのデータディレクトリにアクセスするには以下のコマンドを実行する

geth --datadir ./ console

起動には複数のオプションがある

 --datadir /home/fifi/ethereum/ : data-dir-path
 --networkid 15 : 仮想ネットワークのID(適当) (例)1, 12, 100
 --bootnodes <bootnode-enode-url-from-above> :
--mine: mining enable
--minerthreads=1
--etherbase=0x0000000000 : etherbaseとなるアカウントの指定

3.1. personを作成する

mining book.ethereum-jp.net

> personal.newAccount("user1")
INFO [06-26|14:07:03] New wallet appeared                      url=keystore:///home/fifi/.ethereum… status=Locked
"0x3260c0b494dd9b8715d02bbbfdc6527135ad97b6"

指定する文字列(例では"user1")がパスワードとなるので覚えておく.(Unlock時に使用する)

3.2 personのUnlock

> personal.unlockAccount(eth.accounts[0])
Unlock account 0x3260c0b494dd9b8715d02bbbfdc6527135ad97b6
Passphrase:  (パスワードを入れる)
true

3.3 etherbaseを設定する

マイニングした報酬をあるアカウントに紐つけます

> miner.setEtherbae(eth.acthcounts[0])
> eth.coinbase
'0x59c444d6c4f4187d1dd1875ad74a558a2a3e20b6'

4. 送金する

etherを送金する

> eth.accounts // 登録されている2つのアカウントを表示。
['0x24afe6c0c64821349bc1bfa73110512b33fa18e1', '0x59c444d6c4f4187d1dd1875ad74a558a2a3e20b6' ]
> eth.getBalance(eth.accounts[0]) // etherbaseである1番目のアカウントにetherの持ち高がある(採掘の報酬)。
'72500000000000000000'
> eth.getBalance(eth.accounts[1]) // 2番目のアカウントにはetherの持ち高はない。
'0'

トランザクションを作成する

> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(5, "ether")}) //送金の実行。アカウントのパスワードの入力を求められるので従う。実行結果としてトランザクションIDが返される。
Please unlock account 24afe6c0c64821349bc1bfa73110512b33fa18e1.
Passphrase:
Account is now unlocked for this session.
'0xc86c2a5bdf651f54095eca87e487d4f68f12030dd559f0377e9e7bf1566b9b28' →トランザクションの番号

トランザクションの情報を調べる

先に送信したトランザクションの番号を控えておき,情報を調べる

> eth.getTransaction('0x5fd0bdcccb379a8b4034668464ad9a499a8a6b7801ed66ac23e4df3d67ec64a5')
{
  blockHash: '0xeef0f74bc51ecb9f3d64099fa4f3c1651af36a632380d41dd987e8e7064a5276',
  blockNumber: 11076,
  from: '0x868d840e872df5134a3be6f7b68e52cb680fe3ac',
  gas: 90000,
  gasPrice: '55928534329',
  hash: '0x5fd0bdcccb379a8b4034668464ad9a499a8a6b7801ed66ac23e4df3d67ec64a5',
  input: '0x',
  nonce: 0,
  to: '0x2efbdc840746c862b63077643e5b7dd8bebb8448',
  transactionIndex: 0,
  value: '3000000000000000000'
}

トランザクションを送っただけでは,送金はされない.だれかがマイニングする必要がある.

自分でマイニングする.

miner.start()

適当なところでストップ

miner.stop()
> eth.getTransaction("0x0f61531af0dd600fd37cf0eb85cb131fc87d37d06c4e00b95cf1c847e612a640")
{
  blockHash: "0x936cd2d85afc1eefa6c0f4f81555c75a0c9e62b4f3780f2dafe769d33cd93948",
  blockNumber: 208,
  from: "0xdce41c68345bf699aa82ca2cce29fdf58e4b3b45",
  gas: 90000,
  gasPrice: 0,
  hash: "0x0f61531af0dd600fd37cf0eb85cb131fc87d37d06c4e00b95cf1c847e612a640",
  input: "0x",
  nonce: 0,
  r: "0x285a39079b67c3e3a509bdba4746342accb96093d3810419c9a2681c39c7ead5",
  s: "0x619519553f55c555acfcbfdba69a251446bc3baf1f00c51783de3c3fd6c61b1d",
  to: "0xffa054f546f2edf37d0515f531421ffecbb55ef8",
  transactionIndex: 0,
  v: "0xa96",
  value: 3000000000000000000
}

内容の詳細はチュートリアルを参照 https://book.ethereum-jp.net/first_use/sending_ether.html

> eth.getBalance(eth.accounts[1])
3000000000000000000
etherが増えていることを確認

リファレンス

コマンド一覧


- 起動
geth console
geth --bootnodes "enode://pubkey1@ip1:port1 enode://pubkey2@ip2:port2 enode://pubkey3@ip3:port3"
geth --dev console

- personal
personal.newAccount();
personal.unlockAccount(eth.account[0])

- eth
eth.getBalance(eth.accounts[0])
eth.sendTransaction({from: eth.accounts[1], to: eth.accounts[0], value: web3.toWei(3, "ether")})

- admin
admin.nodeInfo
admin.addPeer(enodeUrlOfFirstInstance)

-miner
miner.start()
miner.stop()
miner.setEtherbae(eth.accounts[0])

'''

簡単安心!ビットコイン取引所 coincheck