LoginSignup
1
2

More than 5 years have passed since last update.

テストネットワークで、Ethereumを使ってみる。(ethereum2)

Last updated at Posted at 2017-12-20

前回はこちら。Ethereumの環境構築

Create account

This is EOA (Externally Owned Account)
"path0"は任意の値で、本来はもっと複雑な値にすべきです。

geth_console
personal.newAccount("pass0")
shell-session
Welcome to the Geth JavaScript console!

instance: Geth/v1.5.5-stable-ff07d548/linux/go1.6.2
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> personal.newAccount("pass0")
"0x5746ffe765c507eb13f0cf34e7fad2bbef7b9f46"
> 

この値はアカウントのアドレス

shell-session
0x5746ffe765c507eb13f0cf34e7fad2bbef7b9f46

別のユーザのアドレスも作成

geth_console
personal.newAccount("test_user")

Ethereumノードで管理しているアカウントのアドレスを確認

geth_console
eth.accounts

2つの値があることを確認

shell-session
["0x5746ffe765c507eb13f0cf34e7fad2bbef7b9f46", "0xb208341b8e7b98070a22f3099e4d1cd719073c0d"]

2つめの値を取得

geth_console
eth.accounts[1]

終了

geth_console
exit

コマンド上での、ユーザーの作成方法

geth --datadir ~/data_testnet account new
shell-session
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: 
Repeat passphrase: 
Address: {d043182856c38652d99f8cb20107264036178e8e}

アカウントの確認

geth --datadir ~/data_testnet account list
shell-session
Account #0: {5746ffe765c507eb13f0cf34e7fad2bbef7b9f46} /home/ubuntu/data_testnet/keystore/UTC--2017-12-19T08-58-39.992054458Z--5746ffe765c507eb13f0cf34e7fad2bbef7b9f46
Account #1: {b208341b8e7b98070a22f3099e4d1cd719073c0d} /home/ubuntu/data_testnet/keystore/UTC--2017-12-19T09-04-54.664337554Z--b208341b8e7b98070a22f3099e4d1cd719073c0d
Account #2: {d043182856c38652d99f8cb20107264036178e8e} /home/ubuntu/data_testnet/keystore/UTC--2017-12-19T09-28-31.058060534Z--d043182856c38652d99f8cb20107264036178e8e

treeコマンドで確認

tree
shell-session
.
├── genesis.json
├── geth
│   ├── chaindata
│   │   ├── 000004.ldb
│   │   ├── 000007.log
│   │   ├── CURRENT
│   │   ├── LOCK
│   │   ├── LOG
│   │   └── MANIFEST-000008
│   ├── LOCK
│   └── nodekey
├── geth.log
├── history
└── keystore
    ├── UTC--2017-12-19T08-58-39.992054458Z--5746ffe765c507eb13f0cf34e7fad2bbef7b9f46
    ├── UTC--2017-12-19T09-04-54.664337554Z--b208341b8e7b98070a22f3099e4d1cd719073c0d
    └── UTC--2017-12-19T09-28-31.058060534Z--d043182856c38652d99f8cb20107264036178e8e

3 directories, 14 files

マイニングを行って、他人に譲渡する

test1アカウントを作る

personal.newAccount("test1")
shell-session
"0x127a0614d4f606460f3cb7e05f1b9f79fdf9191b"
personal.newAccount("test2")
shell-session
"0x32a39ca8fd074a9c1e49b77b6613ca5287592305"

残高を確認

eth.getBalance(eth.accounts[0])
shell-session
0
eth.getBalance(eth.accounts[1])
shell-session
0

マイニング開始
1は、マイニングを行うスレッド数です

miner.start(1)

別ターミナルで、ログファイルを確認できます

tail -f geth.log
I1220 08:01:31.896619 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 0%
I1220 08:01:38.721520 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 1%
I1220 08:01:45.193384 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 2%
I1220 08:01:51.505296 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 3%
I1220 08:01:58.154409 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 4%
I1220 08:02:04.670121 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 5%
I1220 08:02:10.557139 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 6%
I1220 08:12:08.259019 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 97%
I1220 08:12:14.538737 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 98%
I1220 08:12:20.874793 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 99%
I1220 08:12:27.316451 vendor/github.com/ethereum/ethash/ethash.go:291] Generating DAG: 100%
I1220 08:12:27.319713 vendor/github.com/ethereum/ethash/ethash.go:276] Done generating DAG for epoch 0, it took 10m56.334516065s
I1220 08:12:28.523751 miner/unconfirmed.go:83] 🔨  mined potential block #1 [0966b92a…], waiting for 5 blocks to confirm
I1220 08:12:28.524502 miner/worker.go:516] commit new work on block 2 with 0 txs & 0 uncles. Took 409.923µs
I1220 08:12:38.540692 miner/unconfirmed.go:83] 🔨  mined potential block #2 [33e1a281…], waiting for 5 blocks to confirm
I1220 08:12:38.541027 miner/worker.go:516] commit new work on block 3 with 0 txs & 0 uncles. Took 197.657µs
I1220 08:12:47.436003 miner/unconfirmed.go:83] 🔨  mined potential block #3 [c48c98cb…], waiting for 5 blocks to confirm
I1220 08:12:47.436235 miner/worker.go:516] commit new work on block 4 with 0 txs & 0 uncles. Took 156.045µs

ハッシュレートの見方です。

eth.hashrate

ハッシュレートはマイニングの計算力を表す値で、単位はhash/s です。例えばハッシュレート140,956hash/s の場合、1 秒あたり140,956回ハッシュの計算ができることになります。

72584

これでマイニングを停止します。

miner.stop()

残高を確認します。

eth.getBalance(eth.accounts[0])

このコマンドで表示される数値の単位はweiです

1.02e+21

ethに変換します。

web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
1020

生成されたブロックの数は204

eth.blockNumber
204

現在のマイニングの報酬は、1ブロックで5etherです。

1020 = 204 * 5で計算が合います。

アカウントを変更して、マイニングしてみます。

eth.accounts
["0x127a0614d4f606460f3cb7e05f1b9f79fdf9191b", "0x32a39ca8fd074a9c1e49b77b6613ca5287592305"]

Etherbaseを確認
Ethereumでは、マイニン グ成功時に報酬を受け取るアカウントをEtherbaseと言います。

eth.coinbase
"0x127a0614d4f606460f3cb7e05f1b9f79fdf9191b"

Etherbaseを変更してマイニングをやり直す

miner.setEtherbase(eth.accounts[1])
true

Etherbaseが変更されたことを確認

eth.coinbase
"0x32a39ca8fd074a9c1e49b77b6613ca5287592305"
web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
120
eth.blockNumber
228
web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
1020

計算は合ってる
228 * 5 = 1020 + 120

Etherの送金

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

エラーが出る

Error: account is locked
    at web3.js:3119:20
    at web3.js:6023:15
    at web3.js:4995:36
    at <anonymous>:1:1

トランザクションの発行は有料(fromに指定したアドレスが手 数料を払う。)になるため、誤って実行できないよう通常はロックされており、使用時にロックを解除 (アンロック)する必要があります。

ロックを解除

personal.unlockAccount(eth.accounts[0])

"test1"を入力

Unlock account 0x127a0614d4f606460f3cb7e05f1b9f79fdf9191b
Passphrase: 
true

改めて送金

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

再確認

web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
120

まだ、届いていない。
実は sendTransaction でトランザクションを発行しただけでは、処理は実行されません。ブロックチェーンでは、ブロックの中にそのトランザクションが取り込まれるときに、トランザクションの内容 が実行されます。

トランザクションの中身をチェック

eth.getTransaction("0xe677f6a75b953f106200dff21b163241af64ab2273091024b9ccb45386d34ead")
{
  blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  blockNumber: null,
  from: "0x127a0614d4f606460f3cb7e05f1b9f79fdf9191b",
  gas: 90000,
  gasPrice: 20000000000,
  hash: "0xe677f6a75b953f106200dff21b163241af64ab2273091024b9ccb45386d34ead",
  input: "0x",
  nonce: 0,
  r: "0x153232820cb6d3062f4f3855cc5e3ca887dff189671583a15f5c30424df6f3d2",
  s: "0x31cb1e320fc5ebda5bddc7ceec16238a00bee2c9f4a497565a4be3145cb788c6",
  to: "0x32a39ca8fd074a9c1e49b77b6613ca5287592305",
  transactionIndex: null,
  v: "0x1c",
  value: 10000000000000000000
}

blockNumber がnull になっています。nullは、ブロックに入っていない(=未処理、ペンディング されている)ことを表します。

ペンディングされているトランザクションを確認する。

eth.pendingTransactions
[{
    blockHash: null,
    blockNumber: null,
    from: "0x127a0614d4f606460f3cb7e05f1b9f79fdf9191b",
    gas: 90000,
    gasPrice: 20000000000,
    hash: "0xe677f6a75b953f106200dff21b163241af64ab2273091024b9ccb45386d34ead",
    input: "0x",
    nonce: 0,
    r: "0x153232820cb6d3062f4f3855cc5e3ca887dff189671583a15f5c30424df6f3d2",
    s: "0x31cb1e320fc5ebda5bddc7ceec16238a00bee2c9f4a497565a4be3145cb788c6",
    to: "0x32a39ca8fd074a9c1e49b77b6613ca5287592305",
    transactionIndex: null,
    v: "0x1c",
    value: 10000000000000000000
}]

マイニングを再開する

miner.start(1)

これが空になるまで確認

eth.pendingTransactions
miner.stop()

トランザクションを確認

eth.getTransaction("0xe677f6a75b953f106200dff21b163241af64ab2273091024b9ccb45386d34ead")
{
  blockHash: "0x138b447d0f59ed304aab0a2883e35f6c7010c4f975c84d8e3f880eb3d755c13f",
  blockNumber: 229,
  from: "0x127a0614d4f606460f3cb7e05f1b9f79fdf9191b",
  gas: 90000,
  gasPrice: 20000000000,
  hash: "0xe677f6a75b953f106200dff21b163241af64ab2273091024b9ccb45386d34ead",
  input: "0x",
  nonce: 0,
  r: "0x153232820cb6d3062f4f3855cc5e3ca887dff189671583a15f5c30424df6f3d2",
  s: "0x31cb1e320fc5ebda5bddc7ceec16238a00bee2c9f4a497565a4be3145cb788c6",
  to: "0x32a39ca8fd074a9c1e49b77b6613ca5287592305",
  transactionIndex: 0,
  v: "0x1c",
  value: 10000000000000000000
}

blockNumberが229になっています。

ブロックを確認

eth.getBlock(229)
{
  difficulty: 145323,
  extraData: "0xd783010505846765746887676f312e362e32856c696e7578",
  gasLimit: 107310241,
  gasUsed: 21000,
  hash: "0x138b447d0f59ed304aab0a2883e35f6c7010c4f975c84d8e3f880eb3d755c13f",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x32a39ca8fd074a9c1e49b77b6613ca5287592305",
  mixHash: "0xb185673ee917a94b59b1159d5f9e747c8915938e5d8198fb90c701021fba93a3",
  nonce: "0x0c26985dd6772893",
  number: 229,
  parentHash: "0x8560cdc36d48a296a3b14e2855d1c5e427c633617da9f2aa125cae98c398724a",
  receiptsRoot: "0xce5422d31cecf3fe1dae36dcd66bd81ab1a9197f5a843e344fd4b73596eeba08",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 651,
  stateRoot: "0x869098ec4f9d6c5e08c203f209525d237d9cedda488065709ebe39d4afe59789",
  timestamp: 1513768485,
  totalDifficulty: 31667187,
  transactions: ["0xe677f6a75b953f106200dff21b163241af64ab2273091024b9ccb45386d34ead"],
  transactionsRoot: "0xd3496926fe4729cf162bbbbda9bd7d88af6415abd1648f12196b6660fa024896",
  uncles: []
}

改めて残高を確認

増えてる

eth.getBalance(eth.accounts[1])
145000420000000000000

減ってる

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

マイニングした時に報酬をaccount[1]に向けていたので、正確に移動した値がわからなくなりました。
eth.sendTransactionをする前に、eth.coinbaseでアカウントをチェックしておくといいとですね。

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