LoginSignup
2
3

More than 5 years have passed since last update.

go-ethereum (geth) で送金してみる

Last updated at Posted at 2018-03-19

前回 につづき、
渡辺・松本・西村・清水(2017)『はじめてのブロックチェーンアプリケーション』翔泳社
を参考に、ブロックチェーンの実装を試している。

概要

前回 立ち上げた geth で以下の操作を行う

  • アカウントを作成
  • マイニング
  • 送金

geth を起動する

フォアグラウンドで起動した

$ geth --networkid 4649 --nodiscover --maxpeers 0 --datadir ~/eth/data_testnet console 2>> ~/eth/data_testnet/geth.log
>

アカウント(EOA)を作成する

送金用のアカウント(EOA)を作成する。
A さんから B さんに送金するので、アカウントを2つ作成する。

> personal.newAccount("pass0")
> personal.newAccount("pass1")

確認する

> eth.accounts
["0x9243861e2eaf17d07a4b7d3cea8b6d36cafdc107", "0xf0e88a84e2ec8cc0e97df28442c508fd8cad7ce2"]

マイニングする

送金の原資として、マイニング報酬を利用するために、まずマイニングを行う。

マイニング報酬を受け取るアカウントを確認する

マイニング成功時に報酬を受け取るアカウントを確認する。最初に作成したEOAが報酬を得られるようだ。

> eth.coinbase
"0x9243861e2eaf17d07a4b7d3cea8b6d36cafdc107"

アカウントの残高を確認する

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

マイニングを開始する

> miner.start(1)

注)

  • パラメータはマイニングを行うスレッド数。
  • 初回はDAG( Direct acyclic grapf )の生成が行われるため、マイニングが行われるまで時間がかかる  DAG ファイルは、ASIC耐性のために作成される1GBのファイルで、3万ブロック(約125時間)ごとに再作成される。  ~/.ethash/full-R* として作成される  

マイニングの状況を確認する

マイニング中か

> eth.mining
true

ハッシュレート(マイニングの計算力)を調べる。
なぜ0なんだろう。。

> eth.hashrate
0

ブロック高*を確認する。
*ブロック高とは、ジェネシスブロックを 0 とした時にそのブロックが何番目のブロックにあたるかを指す。

> eth.blockNumber
418

マイニングを止める

> miner.stop()
true

マイニング報酬を確認する

ブロック高を確認

456ブロック

> eth.blockNumber
456

残高を確認

■wei
2.28e+21 wei

> eth.getBalance(eth.coinbase)
2.28e+21

■ether
weiをetherに変換して表示する

> web3.fromWei(eth.getBalance(eth.coinbase), "ether")
2280

1wei = 0.000000000000000001ether
http://www.tottemoyasashiibitcoin.net/entry/2017/10/01/090847

■円?

感覚がわからないので、余興としてパブリックな ethereum の場合の円貨を求めてみた。

2280 ether = 126,675,842.4 JPY

送金する

ロックを解除する

送金元のアカウントに手数料が発生するので、デフォルト状態ではロックされているらしい。ロックを解除する。

> personal.unlockAccount(eth.accounts[0], "pass0")
true

アカウント1からアカウント2へ送金する。

> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(10, "ether")})
"0xc848cb5724a49d89e882139ffe86ae1901d2d39e16603b3f4ce3a65445801ce3"

トランザクションを確認する。

> eth.getTransaction("0xc848cb5724a49d89e882139ffe86ae1901d2d39e16603b3f4ce3a65445801ce3")
{
  blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  blockNumber: null,
  from: "0x9243861e2eaf17d07a4b7d3cea8b6d36cafdc107",
  gas: 90000,
  gasPrice: 18000000000,
  hash: "0xc848cb5724a49d89e882139ffe86ae1901d2d39e16603b3f4ce3a65445801ce3",
  input: "0x",
  nonce: 0,
  r: "0x8347332a80a4009c3763169df01a6c7b99fdfe88f8f246335f948f7b0fb446aa",
  s: "0x67118045bbe425bcc0412baaab48031934d27b79d1ba970c2e27a991385d254b",
  to: "0xf0e88a84e2ec8cc0e97df28442c508fd8cad7ce2",
  transactionIndex: 0,
  v: "0x41",
  value: 10000000000000000000
}

→blockNumber: nullとなっている。
送金したが、マイニングされていないので、pending 状態である。

マイニングする

ペンディングのトランザクションがあるか確認する

> eth.pendingTransactions
[{
    blockHash: null,
    blockNumber: null,
    from: "0x9243861e2eaf17d07a4b7d3cea8b6d36cafdc107",
    gas: 90000,
    gasPrice: 18000000000,
    hash: "0xc848cb5724a49d89e882139ffe86ae1901d2d39e16603b3f4ce3a65445801ce3",
    input: "0x",
    nonce: 0,
    r: "0x8347332a80a4009c3763169df01a6c7b99fdfe88f8f246335f948f7b0fb446aa",
    s: "0x67118045bbe425bcc0412baaab48031934d27b79d1ba970c2e27a991385d254b",
    to: "0xf0e88a84e2ec8cc0e97df28442c508fd8cad7ce2",
    transactionIndex: 0,
    v: "0x41",
    value: 10000000000000000000
}]

マイニングする

マイニングを開始する。ペンディング中のトランザクションがなくなったらマイニングを終了する。

> miner.start(1)
null
> eth.pendingTransactions
[]
> miner.stop()
true

残高を確認する

送金先の残高

> eth.getBalance(eth.accounts[1])
10000000000000000000

送金元の残高

> eth.getBalance(eth.accounts[0])
2.285e+21

補足

今回は geth をフォアグラウンドで起動し、コンソールでオペレーションしました。
JSON-RPC で接続し、HTTP でリクエストするほうが普通なんでしょうね。

以上

2
3
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
3