⛓️

gethをローカルDevモードで立ち上げRemixでスマコンをデプロイをしてみる

2024/10/06に公開

目的

gethを弄りたいのでやったことをまとめておく。

前提

go-ethereumのgithubをcloneし、そのソースコードでbuildしたgethを利用します。
https://github.com/ethereum/go-ethereum

  • mac: M3 Pro
  • go version go1.22.4 darwin/arm64

手順

buildしgethを作成する

https://geth.ethereum.org/docs/getting-started/installing-geth

make geth

...
Run "./build/bin/geth" to launch geth.

./build/bin/にgethが作成されます。
この後はこれを使います。

devモードで立ち上げ

ほぼここをやるだけ。
https://geth.ethereum.org/docs/developers/dapp-developer/dev-mode

./build/bin/geth --dev --http --http.api eth,web3,net --http.corsdomain "https://remix.ethereum.org"
http.apiとは

eth, web3, net は、Geth(Go Ethereum)で利用可能なJSON-RPC APIの名前空間で、Ethereumノードと外部アプリケーションの間で通信するためのインターフェースです。

  1. eth (Ethereum API):
    Ethereumブロックチェーンに関する主要な操作やデータ取得に使用します。
    ・トランザクションの送信 (eth_sendTransaction)
    ・アカウントの残高確認 (eth_getBalance)
    ・ブロック情報の取得 (eth_getBlockByNumber)
    ・スマートコントラクトの呼び出し (eth_call)

  2. web3 (Web3 API):
    ノードに関するメタデータや、低レベルの操作に使用されます。
    ・ノードのバージョン確認 (web3_clientVersion)
    ・ノードのSHA3ハッシュ計算 (web3_sha3)
    ・主にノードの状態やメタ情報にアクセスするためのAPIです。

  3. net (Network API):
    Ethereumネットワークの状態に関する情報を提供します。
    ・ネットワークIDの取得 (net_version)
    ・現在接続しているピアの数の確認 (net_peerCount)
    ・ネットワークの状態確認 (net_listening)

http.corsdomain とは

CORSは、ブラウザがリクエストを送信する際に、現在アクセスしているページと異なるドメインへのリクエスト(クロスオリジンリクエスト)を制限する仕組みです。

ここではRemixからのアクセスを許可するために "https://remix.ethereum.org" を指定しています。

起動ログ
$ ./build/bin/geth --dev --http --http.api eth,web3,net --http.corsdomain "https://remix.ethereum.org"

INFO [10-06|10:12:26.626] Starting Geth in ephemeral dev mode...
WARN [10-06|10:12:26.626] You are running Geth in --dev mode. Please note the following:

  1. This mode is only intended for fast, iterative development without assumptions on
     security or persistence.
  2. The database is created in memory unless specified otherwise. Therefore, shutting down
     your computer or losing power will wipe your entire block data and chain state for
     your dev environment.
  3. A random, pre-allocated developer account will be available and unlocked as
     eth.coinbase, which can be used for testing. The random dev account is temporary,
     stored on a ramdisk, and will be lost if your machine is restarted.
  4. Mining is enabled by default. However, the client will only seal blocks if transactions
     are pending in the mempool. The miner's minimum accepted gas price is 1.
  5. Networking is disabled; there is no listen-address, the maximum number of peers is set
     to 0, and discovery is disabled.

INFO [10-06|10:12:26.628] Maximum peer count                       ETH=50 total=50
INFO [10-06|10:12:26.630] Set global gas cap                       cap=50,000,000
INFO [10-06|10:12:26.719] Using developer account                  address=0xAd7b5A64490b941F098E42fD696Ba086a9d25AF8
INFO [10-06|10:12:26.719] Initializing the KZG library             backend=gokzg
INFO [10-06|10:12:26.733] Allocated trie memory caches             clean=154.00MiB dirty=256.00MiB
INFO [10-06|10:12:26.733] State schema set to default              scheme=path
ERROR[10-06|10:12:26.733] Head block is not reachable
INFO [10-06|10:12:26.733] Initialising Ethereum protocol           network=1337 dbversion=<nil>
INFO [10-06|10:12:26.733] Writing custom genesis block
INFO [10-06|10:12:26.735] 
INFO [10-06|10:12:26.735] ---------------------------------------------------------------------------------------------------------------------------------------------------------
INFO [10-06|10:12:26.735] Chain ID:  1337 (unknown)
INFO [10-06|10:12:26.735] Consensus: unknown
INFO [10-06|10:12:26.735] 
INFO [10-06|10:12:26.735] Pre-Merge hard forks (block based):
INFO [10-06|10:12:26.735]  - Homestead:                   #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/homestead.md)
INFO [10-06|10:12:26.735]  - Tangerine Whistle (EIP 150): #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/tangerine-whistle.md)
INFO [10-06|10:12:26.735]  - Spurious Dragon/1 (EIP 155): #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/spurious-dragon.md)
INFO [10-06|10:12:26.735]  - Spurious Dragon/2 (EIP 158): #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/spurious-dragon.md)
INFO [10-06|10:12:26.735]  - Byzantium:                   #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/byzantium.md)
INFO [10-06|10:12:26.735]  - Constantinople:              #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/constantinople.md)
INFO [10-06|10:12:26.735]  - Petersburg:                  #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/petersburg.md)
INFO [10-06|10:12:26.735]  - Istanbul:                    #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/istanbul.md)
INFO [10-06|10:12:26.735]  - Muir Glacier:                #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/muir-glacier.md)
INFO [10-06|10:12:26.735]  - Berlin:                      #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/berlin.md)
INFO [10-06|10:12:26.735]  - London:                      #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/london.md)
INFO [10-06|10:12:26.735]  - Arrow Glacier:               #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/arrow-glacier.md)
INFO [10-06|10:12:26.735]  - Gray Glacier:                #0        (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/gray-glacier.md)
INFO [10-06|10:12:26.735] 
INFO [10-06|10:12:26.735] Merge configured:
INFO [10-06|10:12:26.735]  - Hard-fork specification:    https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/paris.md
INFO [10-06|10:12:26.735]  - Network known to be merged: true
INFO [10-06|10:12:26.735]  - Total terminal difficulty:  0
INFO [10-06|10:12:26.735] 
INFO [10-06|10:12:26.735] Post-Merge hard forks (timestamp based):
INFO [10-06|10:12:26.735]  - Shanghai:                    @0          (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/shanghai.md)
INFO [10-06|10:12:26.735]  - Cancun:                      @0          (https://github.com/ethereum/execution-specs/blob/master/network-upgrades/mainnet-upgrades/cancun.md)
INFO [10-06|10:12:26.735] 
INFO [10-06|10:12:26.735] ---------------------------------------------------------------------------------------------------------------------------------------------------------
INFO [10-06|10:12:26.735] 
INFO [10-06|10:12:26.735] Loaded most recent local block           number=0 hash=da4dd8..55415c td=0 age=55y6mo3w
WARN [10-06|10:12:26.735] Failed to load snapshot                  err="missing or corrupted snapshot"
INFO [10-06|10:12:26.735] Rebuilding state snapshot
INFO [10-06|10:12:26.735] Initialized transaction indexer          range="last 2350000 blocks"
INFO [10-06|10:12:26.735] Resuming state snapshot generation       root=326e96..9f9894 accounts=0 slots=0 storage=0.00B dangling=0 elapsed="123.167µs"
INFO [10-06|10:12:26.735] Generated state snapshot                 accounts=12 slots=0 storage=552.00B dangling=0 elapsed="238.708µs"
INFO [10-06|10:12:26.738] Gasprice oracle is ignoring threshold set threshold=2
INFO [10-06|10:12:26.739] Starting peer-to-peer node               instance=Geth/v1.14.11-unstable-e9647202-20240930/darwin-arm64/go1.22.4
WARN [10-06|10:12:26.739] P2P server will be useless, neither dialing nor listening
INFO [10-06|10:12:26.740] IPC endpoint opened                      url=/var/folders/np/_mlkq0s564301z4585lk47180000gn/T/geth.ipc
INFO [10-06|10:12:26.741] HTTP server started                      endpoint=127.0.0.1:8545 auth=false prefix= cors=https://remix.ethereum.org vhosts=localhost
INFO [10-06|10:12:26.741] New local node record                    seq=1,728,177,146,740 id=28d16f4bc3a0f872 ip=127.0.0.1 udp=0 tcp=0
INFO [10-06|10:12:26.741] Started P2P networking                   self=enode://0d187f6f42423d97ec2d9dfb7c9cd91f376c0503d6d70b164cda8703f162255631aeac1f08482dc04adb9b2605334de6798b7f34fa4fe2e18fa00d17b751eb51@127.0.0.1:0

ローカルdevノードに接続

geth attach <datadir>/geth.ipc

これでローカルのGethノードに接続しJavaScriptコンソールを使用して操作します。

<datadir>は起動ログに表示されているのでそれを指定します。

./build/bin/geth attach /var/folders/np/_mlkq0s564301z4585lk47180000gn/T/geth.ipc

繋がりました。

$ ./build/bin/geth attach /var/folders/np/_mlkq0s564301z4585lk47180000gn/T/geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.14.11-unstable-e9647202-20240930/darwin-arm64/go1.22.4
at block: 0 (Thu Jan 01 1970 09:00:00 GMT+0900 (JST))
 datadir: 
 modules: admin:1.0 debug:1.0 dev:1.0 eth:1.0 miner:1.0 net:1.0 rpc:1.0 txpool:1.0 web3:1.0

To exit, press ctrl-d or type exit
>

試しにアカウントを取得してみます。

> eth.accounts
["0xad7b5a64490b941f098e42fd696ba086a9d25af8"]
> eth.getBalance(eth.accounts[0])/1e18
1.157920892373162e+59
>

RemixIDEからスマコンをデプロイ

openzeppelinのERC20をデプロイしてみます。
https://wizard.openzeppelin.com/

Remixを開いたらSolidityをコンパイルします。
Compile contract~

Custom - External Http Providerを選択します。

portはデフォルトで8545で起動しているためこのままOKで接続します。

Deployを押してデプロイ完了です。

contract address : 0xEFdfb1ed225A255a9eB2C31E6f0aC5B03C833D0E

console側でも確認してみます。
デプロイされていることが確認できました。

> eth.getTransactionReceipt(eth.getBlock("latest").transactions[0])

{
  blockHash: "0xfa4304c58d7b972b5193e1a8c6568231266247eb8bfb7e54539a36aa407539b9",
  blockNumber: 2,
  contractAddress: "0xefdfb1ed225a255a9eb2c31e6f0ac5b03c833d0e",
  cumulativeGasUsed: 1635010,
  effectiveGasPrice: 796695400,
  from: "0xad7b5a64490b941f098e42fd696ba086a9d25af8",
  gasUsed: 1635010,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  status: "0x1",
  to: null,
  transactionHash: "0x21c49eb2cb0eb63ecd0ef693286271e8c00693244aa96dc441deb1be5bea824b",
  transactionIndex: 0,
  type: "0x2"
}

nameを呼び出してみます。

var contractAddress = "0xEFdfb1ed225A255a9eB2C31E6f0aC5B03C833D0E"
var abi=[{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}];
var contract = eth.contract(abi).at(contractAddress);
contract.name.call(function(error,result){console.log(result);});

MyToken

問題なし。

最後に

次はexplorerとしてblockscoutを繋げてみようかなと思います。

https://delt.co.jp/article

Discussion