LoginSignup
4
6

More than 5 years have passed since last update.

Ethereumノウハウ

Last updated at Posted at 2018-04-14

Ethereumについてのメモ

gethに接続

$ geth --testnet --fast --networkid 12345 --datadir ~/eth_data --nodiscover --rpc --rpccorsdomain "*" --rpcport 8545 --rpcaddr "localhost" --rpcapi "web3,eth,net,personal" console 2>> ~/eth_data/geth_err.log

バックグラウンド起動でgethに接続

最後に&をつける

既に起動しているgethプロンプトを開く

$ geth --datadir "~/eth_data" attach ipc:~/eth_data/geth.ipc

アカウント

EOA(Externally Owned Account)とContact Accountの2種類がある

EOA

任意に操作するユーザー用アカウント

Contact Account

トランザクションに応じて処理が行われるアカウント。EOAによりContractの処理が呼ばれ、保持データが変わる。

アカウントの作成

> personal.newAccount("hogehoge1")

パスワードを指定。パスワードを忘れると復元する手段はない。

アカウント一覧

> eth.accounts
["0x95d0ab3bcb76b3a56d4cd0c5289cccdf972c3766", "0xc39e22c8907ef0982753f0d0ed47c9abd1c4679f"]`

コインベース

> eth.coinbase

1つ目のアカウント。マイニングで報酬を受取るアカウント。

採掘開始

> miner.start()

採掘中止

> miner.stop()

採掘状況の確認

> eth.blockNumber
118

採掘したブロック数が返される

採掘中かどうか

> eth.mining
true

ブロック情報

> eth.getBlock(20)
{
  difficulty: 131968,
  extraData: "0xd783010802846765746887676f312e392e34856c696e7578",
  gasLimit: 131620495,
  gasUsed: 0,
  hash: "0xbc4a240d033051bc26f1f756b847fa93daa5d5e920b57f40d723b5473163002a",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x95d0ab3bcb76b3a56d4cd0c5289cccdf972c3766",
  mixHash: "0x9b7924b0733ef8eff23e283c04ddd17dbb76284476b00978014d18b6d597e9dc",
  nonce: "0x33d81415781d1db1",
  number: 20,
  parentHash: "0x1eb4061cc665ae248b3968a15d9b58bf8ba36ad676c8223ff9f4f7db2c4d9873",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 536,
  stateRoot: "0x4eef1c1ed228cd3647ac5803e516c950966b1637e6d6de66cfeaf389131edd0c",
  timestamp: 1523721735,
  totalDifficulty: 2646336,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

残高

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

口座のアンロック

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

アンロックの時間のデフォルトは300秒。0を指定すると無限になる。

> personal.unlockAccount(アドレス, "パスワード", "アンロックの時間(秒)")

API経由でもアンロックする場合は、personalを–rpcapiに入れる

$ geth --unlock 0 --rpc --rpcapi "admin,eth,personal"

送金

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

トランザクションIDが返される
採掘を実行中でないとトランザクションが実行されない

トランザクション情報

> eth.getTransaction('0x9deec8faff406b7ae0142032a367fe72572449e58ddf85b597c035041ca7ac23')
{
  blockHash: "0x4d5808eab4f5c5a24bc93c5ae3c59d74554f265b5500991b7d6b3c94941ab51d",
  blockNumber: 759,
  from: "0x95d0ab3bcb76b3a56d4cd0c5289cccdf972c3766",
  gas: 90000,
  gasPrice: 18000000000,
  hash: "0x9deec8faff406b7ae0142032a367fe72572449e58ddf85b597c035041ca7ac23",
  input: "0x",
  nonce: 0,
  r: "0x198b0598cc2768eadd7a4cbe2b23c18d1e9c3ba6cfe913033fbdfdd6db438892",
  s: "0x45b196f32354c65263b2479fb1b9ae8435f4aaa9fb6a2ddb78fbe80e5b623c56",
  to: "0xc39e22c8907ef0982753f0d0ed47c9abd1c4679f",
  transactionIndex: 0,
  v: "0x1c",
  value: 3500000000000000000
}

JavaScript

JSでイーサリアムネットワークに接続するには、イーサリアムが開発しているweb3jsを使う。
2018年5月1日で1.0はβ版。安定版は0.20.X。1.0はpromiseベースで開発されており、ブラウザではFirefoxくらいでしかまだ対応されていないawaitなどの最新JSを使う想定でつくられている。

安定版をの0.20.0をインストールする場合

$ npm install web3@0.20.0

最新版をインストールする場合

$ npm install web3

test.js

var Web3 = require('Web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var _account = web3.personal.newAccount("mysecret");
console.log(_account)
$ node test.js 
0x9419ac384e8980a8a7e361dbc95c7da7620a408b

Solidity

イーサリアムプラットフォーム用の独自言語。
イーサリアムネットワークに送受信されるEthereum Virtual Machine Code(EVM Code)に変換される。

Solidityコンパイラのインストール

ubuntu

$ sudo add-apt-repository ppa:ethereum/ethereum
$ sudo apt-get update
$ sudo apt-get install solc

Mac

brew update
brew upgrade
brew tap ethereum/ethereum
brew install solidity
brew linkapps solidity

プラズマ・キャッシュ

取引所がこの技術を導入している場合、取引所がハッキングされてもユーザーの資産が保護される。
ユーザーがイーサを取引所に預けると、取引所は同じ価値を持つプラズマのコインを生成する。
このプラズマを他の所有者が引き出そうとすると、所有者に警告がいく。
ユーザーが取引履歴を証明できれば、不正な取引を防止できる。

ABI

Application Binary Interfaceの略。solidtyで記述されたsolファイルをコンパイルしたJSONのコード。

etherの単位

単位 ether
wei(最小) 0.000000000000000001ether
kwei 0.000000000000001ether
mwei 0.000000000001ether
gwei(shannon) 0.000000001ether
szabo 0.000001ether
finney 0.001ether
ether 1ether
kether 1000ether
mether 1000000ether
gether 1000000000ether
tether 1000000000000ether

トラブルシューティング

マイニングが進まない

eth.miningがtrueになってない場合はマイニング開始をされていません。miner.start()をするかgeth起動時にマイニング指定が必要です。
gethをテストネット指定して起動しないとマイニングがなかなか進みません。テストネット指定するにはgeth起動時に--testnetのオプションを指定します。

4
6
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
4
6