アプリケーション開発ポータルサイト
ServerNote.NET
Amazon.co.jpでPC関連商品タイムセール開催中!
カテゴリー【仮想通貨UbuntuDebian
Ubuntu Server 21.10でイーサリアムブロックチェーン【その4】
POSTED BY
2023-03-25

Ubuntu Server 21.10でイーサリアムブロックチェーン【その1】
Ubuntu Server 21.10でイーサリアムブロックチェーン【その2】
Ubuntu Server 21.10でイーサリアムブロックチェーン【その3】

続きです。今回はgethを外部から使う用の様々な立ち上げかたをメモしてみます。

1、最初からアカウントのアンロックがされた状態で起動する

毎回personal.unlockAccountするのが面倒な場合有用です。

パスワードファイルの作成

任意のファイルに1行1パスワードな形で書いて保存しておきます。ここではeth_test/.gethpassとします。

vi eth_test/.gethpass

hoge1
hoge2

1行目がアカウント1のパスワード、2行目がアカウント2のパスワードです。
本人のみ読み取り可にします。

cat eth_test/.gethpass
hoge1
hoge2
chmod go-rwx eth_test/.gethpass

対応アカウントとパスワードファイルを指定して起動

geth --networkid "15" --nodiscover --rpc.allow-unprotected-txs  --allow-insecure-unlock --unlock 0,1 --password "/home/hogeuser/eth_test/.gethpass" --datadir "/home/hogeuser/eth_test" console 2>> /home/hogeuser/eth_test/geth_err.log

2、デーモンプロセスとしてバックグラウンド起動しコンソールは別途接続

サーバープロセスとしてバックグラウンド実行するには、consoleを外して&をつけ、cacheを指定し起動する。SSH接続が切れても継続するようにnohupをつけている。

nohup geth --networkid "15" --nodiscover --rpc.allow-unprotected-txs --cache 2048 --datadir "/home/hogeuser/eth_test" 2>> /home/hogeuser/eth_test/geth_err.log &

別途コンソール接続

eth_test/geth.ipcというUNIXドメインソケットファイルがあるので、それを指定して接続。

geth --datadir "/home/hogeuser/eth_test" attach ipc:/home/hogeuser/eth_test/geth.ipc

なお、デーモンプロセスを終了するには

ps aux | grep hogeuser

などとして、gethのプロセス群を発見してひたすらkillする必要がある。

3、HTTP/Web API接続可能にして起動する

ポート番号は自分で決める。ここでは8575。

geth --http --http.addr "0.0.0.0" --http.port 8575 --http.api "web3,eth,net,personal" --http.corsdomain "*" --rpc.allow-unprotected-txs --datadir "/home/hogeuser/eth_test" --nodiscover --networkid 15 console 2>> /home/hogeuser/eth_test/geth_err.log

外部からWeb API経由で接続テストをする

curlでバージョン情報取得APIを打ってみる。

curl -X POST http://127.0.0.1:8575/  --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":1}' -H "Content-Type: application/json"

{"jsonrpc":"2.0","id":1,"result":"Geth/v1.10.17-unstable-2352c722/linux-amd64/go1.17.5"}

4、マイニングを開始した状態で起動する

※マイニングにはPCの大量の電力を消費します。自己責任注意※

geth --mine --miner.threads 4 --identity "sampleNode" --rpc.allow-unprotected-txs --datadir "/home/hogeuser/eth_test" --nodiscover --networkid 15 console 2>> /home/hogeuser/eth_test/geth_err.log

eth.mining
true

miner.stop()
null

5、まとめの一括起動

1~4すべてを組み合わせたバックグラウンド起動は以下のようになる。

nohup geth --mine --miner.threads 4 --identity "sampleNode" --http --http.addr "0.0.0.0" --http.port 8575 --http.api "web3,eth,net,personal" --http.corsdomain "*" --networkid "15" --nodiscover --rpc.allow-unprotected-txs --cache 2048 --allow-insecure-unlock --unlock 0,1 --password "/home/hogeuser/eth_test/.gethpass" --datadir "/home/hogeuser/eth_test" 2>> /home/hogeuser/eth_test/geth_err.log &

次回はブロックチェーン上でプログラムを実行できるスマートコントラクトを作成してみます。

Ubuntu Server 21.10でイーサリアムブロックチェーン【その5】

※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

☆ServerNote.NETショッピング↓
ShoppingNote / Amazon.co.jp
☆お仲間ブログ↓
一人社長の不動産業務日誌
【キーワード検索】