LoginSignup
2
0

More than 5 years have passed since last update.

Gethのインストールと採掘・送金〜mac編〜

Last updated at Posted at 2018-04-01

やること

スマートコントラクトに必要なEthereumクライアントであるgethをローカルにインストールしてHello worldまでいってみます。

Gethの発音

Go言語で作られていて、Ethereumを扱うってことで、Go Ethereumの省略らしいですが、発音気になりませんか?
ゲース?
ギース?
ゴース?(goっていってるし)
ゲート?
ゴート?

正解発表

Youtubeより
IMAGE ALT TEXT HERE

というわけで
ゲース なのですが思ったイントネーションと違いました。

インストール

homebrewを使ってMac OSにインストールします

$ brew tap ethereum/ethereum
$ brew install ethereum

プライベートネットの作成

Ethereum入門ではGenesisファイルを作成していましたが、--devを使うと簡単らしいのでこちらを採用

今回、ホームディレクトリ下のworkmフォルダにethtestをつくってここをデータに入れるところにします。

$ cd ~/work
$ mkdir ethtest
$ cd ethtest 
$ geth --dev --datadir .

これでチェーンが立ち上がりました。
この画面はプライベートチェーンがひたすら動いてるだけのものになるので、新しいターミナルウィンドウを開いて、
gethのコンソールを起動します。

$ cd work/ethtest
$ geth attach ipc:geth.ipc

これでコンソールが立ち上がりましたのでアカウントを作成します。

> personal.newAccount("PW")
"0x699a41fb91251f35149d6c4d1bf6b4589a0c6e45"

PWがパスワードですが、空白でも可能みたいなので空白でも作ってみます

> personal.newAccount()
Passphrase: 
Repeat passphrase: 
"0xd77bfea42da2508a0d4b06a4d457b2eb7a9b18df"

確認

> eth.accounts
["0x94da4fca6188bf0510fc8b6b2ccfbc8f4ecc8527", "0x699a41fb91251f35149d6c4d1bf6b4589a0c6e45", "0xd77bfea42da2508a0d4b06a4d457b2eb7a9b18df"]
> 
> eth.coinbase
"0x94da4fca6188bf0510fc8b6b2ccfbc8f4ecc8527"

なにやら作った覚えがない0x94da4fca6188bf0510fc8b6b2ccfbc8f4ecc8527というアカウントがあるが、--devを使ったからなのか、developer accountらしい

アンロック
いろんなところで使えるように全部ロックを解除しておきます。

> personal.unlockAccount(eth.accounts[0])
Unlock account 0x94da4fca6188bf0510fc8b6b2ccfbc8f4ecc8527
Passphrase: 
true

> personal.unlockAccount(eth.accounts[1])
Unlock account 0x699a41fb91251f35149d6c4d1bf6b4589a0c6e45
Passphrase: 
true

> personal.unlockAccount(eth.accounts[2])
Unlock account 0xd77bfea42da2508a0d4b06a4d457b2eb7a9b18df
Passphrase: 
true

マイニング開始

> miner.start()
null

スタートしたはいいものの、ぜんぜん採掘されている気がしない
(チェーンのコンソール画面もぜんぜん動かない)

> eth.blockNumber
0
> eth.mining
true
> eth.hashrate
0

うーん
動いてはいるらしいけど何も採掘されない。
genesisブロックはあってもその先がなにもない。

> eth.getBlock(0)
{
  difficulty: 1,
  extraData: "0x000000000000000000000000000000000000000000000000000000000000000094da4fca6188bf0510fc8b6b2ccfbc8f4ecc85270000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  gasLimit: 6283185,
  gasUsed: 0,
  hash: "0x7400cf60082e03e0cb6b6aec88def6a3fc0adb83381ce559b8995bcdb2092abb",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x0000000000000000000000000000000000000000",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000000",
  number: 0,
  parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 622,
  stateRoot: "0x095f9fff627b81c2338bbbc850cf0f992f7a2bf14629d88e01a4cf98bd4507f4",
  timestamp: 0,
  totalDifficulty: 1,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
> eth.getBlock(1)
null

調べてみると、--devを使った場合はtransactionがないとマイニングしないとかなんとか
あちこちの記事では--devを紹介してるのに結局はGenesisファイル使ってやってるのか、、、、

送金

残高確認

> eth.getBalance(eth.accounts[0])
1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+77
> eth.getBalance(eth.accounts[1])
0
> eth.getBalance(eth.accounts[2])
0

めちゃくちゃいっぱい入ってた、、、、、
約10の77乗weiで、etherはweiの10の18乗倍なので、10の55etherくらい。とにかく大金

ケチなので1wei送ります。

> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:"1"})
"0x3d0f79a51bc273d266ca51a6c0d74c768d9291b93175277d2a0dba0d8f927d6e"
> eth.getBalance(eth.accounts[1])
1

1weiが入りました。新たなブロックができてることも確認

> eth.getBlock(1)
{
  difficulty: 2,
  extraData: "0xd983010803846765746888676f312e31302e318664617277696e00000000000042050f8fbe7beff3c52c837bbaac4165581e900efb0866d1085872717bc7b04e75a697a3c5bb0f0d42c272c01a97d0c0791f60e846f904fe9f1a064d84d97d5e01",
  gasLimit: 6277051,
  gasUsed: 21000,
  hash: "0xcf16179e579c8051710093924bd74b1cae338c7bb87f10bd7ad6756f6153bd2d",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x0000000000000000000000000000000000000000",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000000",
  number: 1,
  parentHash: "0x7400cf60082e03e0cb6b6aec88def6a3fc0adb83381ce559b8995bcdb2092abb",
  receiptsRoot: "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 709,
  stateRoot: "0xe541a1749a31bc99c9ac3001c21f08a69444e75138b79182fb0ed5509b9c8d02",
  timestamp: 1522575943,
  totalDifficulty: 3,
  transactions: ["0x3d0f79a51bc273d266ca51a6c0d74c768d9291b93175277d2a0dba0d8f927d6e"],
  transactionsRoot: "0xef936d5e25d692542d4e4546971e807c0f057659f68ccb95e9447f911cfdfeca",
  uncles: []

調子出てきたのでもっと大量に送ってみます。

> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value: web3.toWei(1000, "ether")})
"0x1d77b0f001575c6a97bdcd543e302cf7f80061b223c7ab03392ab00e1b226377"
> eth.getBalance(eth.accounts[1])
1.000000000000000000001e+21

いっぱい送れました

> eth.getTransaction("0x1d77b0f001575c6a97bdcd543e302cf7f80061b223c7ab03392ab00e1b226377")
{
  blockHash: "0x5af8f20225ac903132fbc9b3c1e83df5b339de50b03faa373e22adea290fd14f",
  blockNumber: 2,
  from: "0x94da4fca6188bf0510fc8b6b2ccfbc8f4ecc8527",
  gas: 90000,
  gasPrice: 1,
  hash: "0x1d77b0f001575c6a97bdcd543e302cf7f80061b223c7ab03392ab00e1b226377",
  input: "0x",
  nonce: 1,
  r: "0x4394335e5a41f157c25089f209f74ec9b776b2ad43183e3ce83980ba64624f22",
  s: "0x298217349eaea647f03eaf84ec5b7be42270906f2e127dbb5ac71103be25acb4",
  to: "0x699a41fb91251f35149d6c4d1bf6b4589a0c6e45",
  transactionIndex: 0,
  v: "0xa96",
  value: 1e+21
}

9000gas取られてるっぽいですね。わかりやすく1→2で送金

> eth.sendTransaction({from:eth.accounts[1], to:eth.accounts[2], value: web3.toWei(500, "ether")})
"0x938d0ecc3b6c966409b43784e89c263fc335cfc5ae1f384b3000a5fde94a8f2d"
> eth.getBalance(eth.accounts[1])
499999999999999958001

gas取られてますね

続いてsolidityを使用したHello worldをしてみようと思います

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