don't stop believing

Geth 백그라운드 실행과 JSON-RPC 서버 실행 본문

Ethereum

Geth 백그라운드 실행과 JSON-RPC 서버 실행

Tongchun 2018. 8. 25. 13:40

Geth를 백그라운드에서 계속 채굴하돌록 하겠습니다.

방법은 Linux 계열에서 사용하는 nohup 을 사용합니다.


이전 post에서 사용했던 geth 실행 설정입니다.

geth --networkid 45 --nodiscover --maxpeers 0 --datadir /Users/tongchunkim/Documents/Test_Ethereum/data_testnet console 2>> /Users/tongchunkim/Documents/Test_Ethereum/data_testnet/geth.log


여기에 몇 가지 추가하겠습니다.

nohup geth --networkid 45 --nodiscover --maxpeers 0 --datadir /Users/tongchunkim/Documents/Test_Ethereum/data_testnet/ --mine --minerthreads 1 --rpc  2>> /Users/tongchunkim/Documents/Test_Ethereum/data_testnet/geth.log &


백그라운드 실행을 위해 명령문의 제일 앞에 nohup과 마지막에 & 를 추가했습니다.

그리고 백그라운드 실행이기 때문에 console 옵션을 제거했습니다.

백그라운드에서 채굴을 시키기 위해 --mine --minerthreads 1 옵션을 주고 HPPT-RPC 서버를 활성화 하기 위해--rpc를 추가했습니다.


백그라운드로 실행이 잘 되고 있는지 Geth rpc로 접속해 봅시다.

$ geth attach rpc:http://localhost:8545
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.14-stable/darwin-amd64/go1.10.3
coinbase: 0x78ce083531ff41d26c7002efcf4eec1fd11deaa0
at block: 837 (Sat, 25 Aug 2018 16:00:39 KST)
 modules: eth:1.0 net:1.0 rpc:1.0 web3:1.0

>

채굴이 잘 되고 있는지도 확인해 봅니다.

> eth.mining
true
> eth.blockNumber
919
> eth.blockNumber
920
> eth.blockNumber
921

백그라운드로 돌아가는 프로세스를 확인하려면 ps aux | grep geth 명령으로 확인할 수 있습니다.

프로세스를 종료할때는 kill 명령을 사용합니다.


이제 JSON-RPC 서버를 실행해 보겠습니다.

현재 백그라운드로 실행되고 있는 geth를 종료하고 아래와 같이 geth를 실행합니다.

nohup geth --networkid 45 --nodiscover --maxpeers 0 --datadir /Users/tongchunkim/Documents/Test_Ethereum/data_testnet/ --mine --minerthreads 1 --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpccorsdomain "*" --rpcapi "admin, db, eth, debug, miner, net, shh, txpool, personal, web3" 2>> /Users/tongchunkim/Documents/Test_Ethereum/data_testnet/geth.log &


--rpc 옵션으로 RPC 서버를 활성화 시킵니다.

--rpcaddr "0.0.0.0"은 HTTP-RPC 서버의 수신 IP를 지정합니다. 기본값은 localhost이며 "0.0.0.0"을 지정하면 외부 인터페이스에 대한 접근도 가능합니다.

--rpccorsdomain "*" 자신의 노드에 RPC로 접속할 IP 주소를 지정합니다. 쉼표로 구분해서 여러 개를 지정할 수도 있고 "*"로 지정하면 모든 IP에서 접속을 허용합니다.

--rpcapi "admin, db, eth, debug, miner, net, shh, txpool, personal, web3" RPC를 허가할 명령을 지정합니다. 기본값은 "eth, net, web3"입니다.


RPC 서버를 실행했다면 curl 명령이나 REST API를 확인할 수 있는 툴을 사용해 json 형태의 데이터를 주고받을 수 있습니다.

RPC API 정보는 아래 url에서 확인할 수 있습니다.

https://github.com/ethereum/go-ethereum/wiki/Management-APIs


먼저 새로운 계정을 만들어 보겠습니다. Geth의 personal.newAccount에 해당하는 API인 personal_newAccount를 입력하고 params에 페스워드를 지정합니다.

id는 임의의 숫자를 지정합니다.

{
	"jsonrpc": "2.0",
	"id": 10,
	"method": "personal_newAccount",
	"params": ["air1"]
}


curl로 사용할 경우 아래와 같이 사용합니다.

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 10, "method": "personal_newAccount", "params": ["air1"]}' localhost:8545
{"jsonrpc":"2.0","id":10,"result":"0xf47f59e83cdec183666305e3ed4c5eef4539907f"}

API 호출 시 Header의 Content-Type은 application/json으로 지정해 줘야 합니다.

id가 10인 요청의 결과(result)로 생성된 계정의 주소가 반환됩니다.


계정 리스트를 불러와 봅시다. method는 personal_listAccounts 입니다.

요청하는 json data는 아래와 같습니다.

{
	"jsonrpc": "2.0",
	"id": 10,
	"method": "personal_listAccounts"
}


채굴이 진행되는지 확인해 봅시다.

이번엔 curl로 확인해 봅시다. method는 eth_mining 입니다.

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 10, "method": "eth_mining", "params": []}' localhost:8545
{"jsonrpc":"2.0","id":10,"result":true}

블록이 얼마나 되는지 확인해 봅시다. method는 eth_getBalance, params 확인하려는 계정 주소와 latest를 입력합니다.

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 10, "method": "eth_getBalance", "params": ["0x78ce083531ff41d26c7002efcf4eec1fd11deaa0", "latest"]}' localhost:8545
{"jsonrpc":"2.0","id":10,"result":"0x23e5b2ac512bb65a000"}

리턴 데이터의 result는 16진수로 된 잔고입니다.

16진수를 10진수로 확인하려면 printf 명령을 사용합니다.

$ printf  '%d\n' 0x23e5b2ac512bb65a000
-bash: printf: warning: 0x23e5b2ac512bb65a000: Result too large
9223372036854775807

정수로 표현하기에는 너무 많네요. float 타입으로 보면 보이네요.

$ printf  '%f\n' 0x23e5b2ac512bb65a000
10595000377999999631360.000000

이제 accounts[2]번이 accounts[3]에게 송금해 보겠습니다.

accounts[2]는 10 Ether를 가지고 있고 accounts[3]은 0 입니다. accounts[2]가 5 Ether를 송금하겠습니다.

5 Ether는 5000000000000000000 wei 이고 16진수로는 0x4563918244f40000 입니다.

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 10, "method": "eth_sendTransaction", "params": [{"from":"0xa6e12c605be72b4e201a94e56346d5d6463b366e", "to":"0xb398be73eb30de89d121e4a1bb2c219c40252c9e", "value":"0x4563918244f40000"}]}' localhost:8545
{"jsonrpc":"2.0","id":10,"error":{"code":-32000,"message":"authentication needed: password or unlock"}}

리턴 메시지를 보니 accounts[2]를 unlock해야 합니다.

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 10, "method": "personal_unlockAccount", "params": ["0xa6e12c605be72b4e201a94e56346d5d6463b366e", "tongchun1234", 300]}' localhost:8545
{"jsonrpc":"2.0","id":10,"result":true}

이제 다시 송금해 보겠습니다.

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 10, "method": "eth_sendTransaction", "params": [{"from":"0xa6e12c605be72b4e201a94e56346d5d6463b366e", "to":"0xb398be73eb30de89d121e4a1bb2c219c40252c9e", "value":"0x4563918244f40000"}]}' localhost:8545
{"jsonrpc":"2.0","id":10,"result":"0x443a3f534a790be9943a5cfc9cf558883361eecec31c3e0149c1b68092e743ca"}

송금이 정상적으로 처리되고 Transaction ID가 리턴되었습니다.

geth는 계속 mining하고 있으니 바로 송금이 되었을 것입니다. accounts[2]의 잔고를 확인해 보겠습니다.

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 10, "method": "eth_getBalance", "params": ["0xa6e12c605be72b4e201a94e56346d5d6463b366e", "latest"]}' localhost:8545
{"jsonrpc":"2.0","id":10,"result":"0x456239b8493a6000"}

잔고가 16진수로 0x456239b8493a6000입니다. 10진수로 확인하니 4999622000000000000 wei 입니다.

수수로가 나갔네요.

accounts[3]의 잔고도 확인해 보겠습니다.

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "id": 10, "method": "eth_getBalance", "params": ["0xb398be73eb30de89d121e4a1bb2c219c40252c9e", "latest"]}' localhost:8545
{"jsonrpc":"2.0","id":10,"result":"0x4563918244f40000"}

잔고가 16ㄹ진수로 0x4563918244f40000입니다. 10진수로 확인하면 5000000000000000000입니다.

5000000000000000000 wei가 정상적으로 입금 되었습니다.


Comments