geth 명령어 팁

in #blockchain4 years ago (edited)

1. Geth 옵션

제가 geth를 시작할때 자주 쓰는 옵션을 정리해보겠습니다.

❯ geth --networkid 4693 --rpc --rpcport 8545 --rpccorsdomain "*" --rpcaddr "0.0.0.0" --rpcapi "admin,eth,debug,miner,net,txpool,personal,web3" --ws --wsport 8546 --wsorigins "*" --wsapi "admin,eth,debug,miner,net,txpool,personal,web3" console --allow-insecure-unlock --mine --miner.threads 1 --unlock 0 --password ./password

--networkid value
: Network identifier (integer, 1=Frontier, 2=Morden (disused), 3=Ropsten, 4=Rinkeby) (default: 1)

geth로 생성된 블록체인네트워크의 id를 지정해주는 옵션입니다. 1,2,3,4번은 미리 정해진 숫자이므로 다른 숫자를 입력해줍니다.

--rpc               Enable the HTTP-RPC server
--rpcport           HTTP-RPC server listening port (default: 8545)
--rpccorsdomain     Comma separated list of domains from which to accept cross origin requests (browser enforced)
--rpcaddr           HTTP-RPC server listening interface (default: "localhost")
--rpcapi            API's offered over the HTTP-RPC interface

http-rpc server와 관련된 옵션입니다. rpc를 사용하면 외부에서 geth에 접근할 수 있습니다. --rpcport로 포트를 지정해주고 --rpcapi로 어떤 모듈을 사용하게 해줄 것인지 정할 수 있습니다.

--ws                                Enable the WS-RPC server
--wsaddr value                      WS-RPC server listening interface (default: "localhost")
--wsport value                      WS-RPC server listening port (default: 8546)
--wsapi value                       API's offered over the WS-RPC interface
--wsorigins value                   Origins from which to accept websockets requests

geth는 websocket을 지원합니다. rpc옵션과 사용법은 비슷합니다.

--allow-insecure-unlock             Allow insecure account unlocking when account-related RPCs are exposed by http

--unlock value                      Comma separated list of accounts to unlock
--password ./password

geth가 업데이트 되면서 보안상의 이유로 rpc 사용할때 외부에서 계정을 unlock하는 것을 금지하고 있습니다. 개발할때 외부에서 unlock하는 것이 종종 필요하기에 이 옵션을 사용합니다.

--unlock value를 사용하여 어떤 계정을 unlock할지 정해줍니다. --unlock "0"은 0번째 계정을 unlock하는 것이고, 콤마로 구분하여 --unlock "0, 1"을 사용하여 0번째와 1번째 계정을 unlock할 수 있습니다.
unlocking에는 패스워드가 필요합니다. --password옵션을 사용하여 각 계정의 패스워드를 입력할 수 있습니다. 현재 폴더에서 password파일을 생성하고 파일안에 각 계정의 패스워드를 입력합니다.

password file

pass00
pass01

password파일에 위에서부터 차례대로 0번째, 1번째 계정의 패스워드를 입력합니다.

--mine                              Enable mining
--miner.threads value               Number of CPU threads to use for mining (default: 0)

--mine은 geth를 시작할때 mining을 자동으로 시작해주는 옵션입니다. miner.threads 1을 통해 mining을 할 때 사용할 thread의 수를 1로 지정해 주었습니다.

이 옵션을 alias로 간편하게 사용할 수 있습니다. 저는 zsh셸을 사용하므로 zshrc파일을 열어 alias를 설정합니다.

vim ~/.zshrc

alias gethRPCWS='geth --networkid 4693 --rpc --rpcport 8545 --rpccorsdomain "*" --rpcaddr "0.0.0.0" --rpcapi "admin,eth,debug,miner,net,txpool,personal,web3" --ws --wsport 8546 --wsorigins "*" --wsapi "admin,eth,debug,miner,net,txpool,personal,web3" console --allow-insecure-unlock --mine --miner.threads 1 --unlock 0 --password ./password'

source ~/.zshrc

이제 터미널에서 다음과 같이 사용하면 됩니다.

gethRPCWS --datadir data

2. geth attach

위의 명령어로 geth를 구동시키면 rpc와 ws를 통해 attach 명령어로 geth에 접근할 수 있습니다. rpcport는 8545, wsport는 8546입니다.

❯ geth attach http://127.0.0.1:8545

Welcome to the Geth JavaScript console!
instance: Geth/v1.9.12-unstable-556888c4-20200303/darwin-amd64/go1.14.6
coinbase: 0x4c8d62c6341a6be819cffa294b81d69b083ded7f
at block: 1826 (Sat Jul 25 2020 16:56:06 GMT+0900 (KST))
datadir: /Users/thkim/workspace/KSABlockchain/DappFriday/data2
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

❯ geth attach ws://127.0.0.1:8546

Welcome to the Geth JavaScript console!
instance: Geth/v1.9.12-unstable-556888c4-20200303/darwin-amd64/go1.14.6
coinbase: 0x4c8d62c6341a6be819cffa294b81d69b083ded7f
at block: 1812 (Sat Jul 25 2020 16:53:59 GMT+0900 (KST))
datadir: /Users/thkim/workspace/KSABlockchain/DappFriday/data2
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

rpc, ws 각각 사용하여 geth에 접근한 모습입니다. --rpcapi, --wsapi로 지정해준 모듈만 사용할 수 있으며 위의 modules에 사용할 수 있는 모듈이 적혀 있습니다.

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.029
BTC 63441.26
ETH 2477.91
USDT 1.00
SBD 2.64