LoginSignup
0
4

More than 5 years have passed since last update.

Cloud9上でEthereumコマンドを学ぶ1 インストールからマイニング~送金編

Last updated at Posted at 2018-01-15

Ethereumの勉強のために、cloud9上でインストールとコマンドを色々と弄ってみました(Eheriumインストール、アカウント作成、送金、マイニングまで)

まずはインストールはこのあたりを参考にしました。

まず、cloud9のworkspaceの作成はpythonで行いました。
cloud9はターミナルを複数作成できるので、gethのコマンド入力用とubuntuのコマンド入力用と2つ立ち上げておくと便利です。

cloud9のコマンド上で下記のコマンドをたたきます
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo add-apt-repository -y ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install ethereum

Gethの起動

参考サイト
https://book.ethereum-jp.net/first_use/connect_to_private_net.html

Genesisファイルを作成する

./workspace配下に作業用ディレクトリを作成する。以下、デフォルト作業ディレクトリは /hoem/ubuntu/workspace とする。

cd /hoem/ubuntu/workspace
mkdir ./eth_private

次に上記ディレクトリ内にjson形式の下記の内容を記述したmyGenesis.jsonファイルを配置します。

しかし、参考サイトのjsonファイルではエラー(cannot unmarshal hex string of odd length into Go struct field Genesis.extraData of type hexutil.Bytes)となったので、別サイトを参考にして設定しました。
別サイトURL https://qiita.com/oggata/items/eea4d5e37f38785f6079

cd eth_private
vi genesis.json

genesis.jsonの内容
{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x400",
"alloc": {},
"coinbase": "0x3333333333333333333333333333333333333333",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000",
"config": {}
}

プライベートネットを起動

ブロックチェーン情報の初期化

geth --datadir /home/ubuntu/workspace/eth_private/ init /home/ubuntu/workspace/eth_private/genesis.json

下記のように表示されますが、次に進んでいきます
WARN [01-15|21:18:28] No etherbase set and no accounts found as default
INFO [01-15|21:18:28] Allocated cache and file handles database=/home/ubuntu/workspace/eth_private/geth/chaindata cache=16 handles=16
INFO [01-15|21:18:28] Writing custom genesis block
INFO [01-15|21:18:28] Successfully wrote genesis state database=chaindata hash=6231b0…a0300b
INFO [01-15|21:18:28] Allocated cache and file handles database=/home/ubuntu/workspace/eth_private/geth/lightchaindata cache=16 handles=16
INFO [01-15|21:18:28] Writing custom genesis block
INFO [01-15|21:18:28] Successfully wrote genesis state database=lightchaindata hash=6231b0…a0300b

プライベートネットワークにてgethの起動

geth --networkid "10" --nodiscover --datadir "/home/ubuntu/workspace/eth_private" console 2>> /home/ubuntu/workspace/eth_private/geth_err.log

下記のように表示されて無事 プライベートネットワークが起動されました
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9
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

ブロック情報の表示

>eth.getBlock(0)

下記のようにブロック情報が表示されました。
{
difficulty: 1024,
extraData: "0x",
gasLimit: 134217728,
gasUsed: 0,
hash: "0x6231b02ac967dff9b0c799e956094408959de862d720d08776302ffefba0300b",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x3333333333333333333333333333333333333333",
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
nonce: "0x0000000000000042",
number: 0,
parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 507,
stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
timestamp: 0,
totalDifficulty: 1024,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}

アカウント作成から採掘,送金まで

注意点  miner.start()  でマイニングを開始してもすぐにマイニングは行われない場合がある。
その時は、gethを再起動して数分待ってからハッシュレートを確認すること。

> eth.accounts //アカウント参照
[] //アカウトはまだ無い
> personal.newAccount("hogehoge01")  //アカウント1つ目作成
"0x1263708f626d608dda736b63d4a1f891cfffa344"  //アカウントアドレスが表示される
> eth.accounts //アカウントの確認
["0x1263708f626d608dda736b63d4a1f891cfffa344"]
> personal.newAccount("hogehoge02")   //アカウント2つ目作成
"0x422c402e8fc22d0ba73e6531f9ae70f8b292a9cb"
> eth.accounts //アカウントチェック
["0x1263708f626d608dda736b63d4a1f891cfffa344", "0x422c402e8fc22d0ba73e6531f9ae70f8b292a9cb"]
> eth.coinbase //コインベースの確認 マイニングで得たコインはこのアドレスに入る(最初に作ったアドレス)
"0x1263708f626d608dda736b63d4a1f891cfffa344"
>
> miner.start()  //マイニングスタート
null
> eth.hashrate  //ハッシュレート確認 0でなければ採掘中
ハッシュレートの確認で何度コマンド入力しても0になる場合がある


> eth.getBalance(eth.accounts[0])  //残高確認
70000000000000000000
> eth.getBalance(eth.accounts[0])
90000000000000000000
> miner.stop()  //マイニングストップ
true
> eth.getBalance(eth.accounts[0]) //残高確認 マイニングにより残高が増えています
110000000000000000000
> 
> personal.unlockAccount("0xbf682f19e76a5f063bea220db6ea3a386482fccd", "hogehoge01") //口座をアンロックしないと送金ができないので、2口座分アンロックする
true
> personal.unlockAccount("0x7e2cbd73bac19146b356019911f5305e46482637", "hogehoge02") //口座アンロック
true
> eth.getBalance(eth.accounts[0])
110000000000000000000
> eth.getBalance(eth.accounts[1])
0
//口座から口座へ送金
> eth.sendTransaction({from: "0xbf682f19e76a5f063bea220db6ea3a386482fccd", to: "0x7e2cbd73bac19146b356019911f5305e46482637", value: web3.toWei(1, "ether")})
"0xf08efbca66117c59820dd909e5fc8cd368802aa01ab34ca388e9702158f52624" //←上記のコマンドを入力するとトランザクションIDが表示される。後でトランザクションを参照して処理状況を確認できる。この状態では、まだマイニングされていないのでマイニングをしてトランザクションを送信する必要がある


> eth.pendingTransactions //ペンディングトランザクション参照
[{
    blockHash: null,
    blockNumber: null,
    from: "0xbf682f19e76a5f063bea220db6ea3a386482fccd",
    gas: 90000,
    gasPrice: 18000000000,
    hash: "0xa5ea4b219c11d78c3a16cc764b406ba86691ebc5845b88776c8f60a0f673c90c",
    input: "0x",
    nonce: 0,
    r: "0xd597de1a228c3650abb90ee7593284021097de75dcb3c85fe9551942955821c6",
    s: "0x6153de9aba94edee4c958ba7c7333b1ce4997bb06ebadef0a6f7a6a569c8899b",
    to: "0x7e2cbd73bac19146b356019911f5305e46482637",
    transactionIndex: 0,
    v: "0x1c",
    value: 1000000000000000000
}]
> 
> miner.start() //マイニングスタート マイニングをすることでブロックが作成されて送金が行われる
null
> eth.hashrate //ハッシュレート確認 マイニングされているか確認
794
> eth.getBalance(eth.accounts[0]) //残高を確認して、マイニングの報酬をもらっていることがわかる。
134000000000000000000
> eth.pendingTransactions
[]
> eth.getBalance(eth.accounts[1]) //送信先の口座確認 送金されている
1000000000000000000
> miner.stop()
true
>
> web3.fromWei(eth.getBalance(eth.accounts[0]),"ether") //wei単位からイーサ単位に変換
324
0
4
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
0
4