Geth 초기화 및 Dapp 개발을 위한 준비

in geth •  6 years ago 

[간단한 Geth 설치 및 환경설정은 이전 포스트 참고
https://steemit.com/gcp/@bluezoid/gcp-geth]

Geth 초기화 및 계정생성등 Dapp 개발을 위한 기본 셋팅

1 개발 디렉토리 생성
$mkdir ~/data_testnet

2 genesis.json 파일 생성
$cd ~/data_testnet
$vi genesis.json

저는 아래와 같은 내용의 genesis.json 파일 생성함

{
  "config": {
    "chainId": 33,
    "homesteadBlock": 0,
    "eip155Block": 0,
    "eip158Block": 0
  },
  "nonce": "0x0000000000000033",
  "timestamp": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x8000000",
  "difficulty": "0x100",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x3333333333333333333333333333333333333333",
  "alloc": {}
}



3 geth 초기화
$geth --datadir /home/devno3kaul/data_testnet init /home/devno3kaul/data_testnet/genesis.json

4 geth 구동(실행)
$geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/devno3kaul/data_testnet console 2>> /home/devno3kaul/data_testnet/geth.log

  • networkid 는 4649 사용
  • nodiscover : 생성자의 노드를 다른노드에서 검색할 수 없게 하는 옵션
  • maxpeers : 생성자의 노드에 연결할 수 있는 노드의 수를 지정, 0은 다른노드와 연결하지 않는다.
  • datadir : 데이터 디렉토리 지정
  • console : 대화형 자바스크립트 콘솔 기동
  • 2>> /home/devno3kaul/data_testnet/geth.log : 에러를 해당 경로의 파일로 저장, 리눅스 쉘명령임

다음과 같이 실행됨

5 EOA(Externally Owned Account):송금및계약실행가능계정 생성

  • 이더리움에는 두가지 종류의 계정이 있다, 하나는 EOA, 다른하나는 Contract 계정
  • EOA는 일반 사용자 계정, 비밀키로 관리
  • Contract 계정은 계약을 블록체인에 배포할 때 만들어지는 계정으로 블록체인에 존재한다.

>personal.newAccount("pass0")
"0xead06f7ce504d82837e3241394f5498cfa3df00b"

>personal.newAccount("pass1")
"0x24e73a3457a05b6910cd6078828d916ee159cc95"

==> 패스워드 꼭 기억할 것, 위에서는 두계의 EOA계정을 생성함

6 geth 종료
>exit

7 쉘에서 EOA 생성
$geth --datadir /home/devno3kaul/data_testnet account new
INFO [04-24|05:14:05] Maximum peer count ETH=25 LES=0 total=25
Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase: pass2
Repeat passphrase: pass2
Address: {79e122c5ade724ceeeba1c58eb2b403f39f5484b}

8 쉘에서 EOA 확인
$geth --datadir /home/devno3kaul/data_testnet account list

9 송금 테스트를 위해 Ether가 필요하고 Ether를 얻기 위해 채굴이 필요함, 채굴을 해보자

  • geth 기동
    $geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/devno3kaul/data_testnet console 2>> /home/devno3kaul/data_testnet/geth.log
  • 채굴 시작 , 별도의 콘솔에서 tail -100f ~/data_testnet/geth.log 로 진행사항 확인
    >miner.start(1)
    처음 채굴을 하는 경우 DAG 생성으로 약간 시간이 걸린다.
  • 채굴과 관련된 기본적인 명령어들
    >eth.coinbase ⇒ 현재 채굴보상 계정 확인
    >miner.setEtherbase(eth.accounts[1]) 명령어로 Etherbase를 변경할 수 있음
    >eth.getBalance(eth.coinbase) 채굴보상계정인 Etherbase계정의 잔고확인
    >eth.getBalance(eth.accounts[0]) 첫번째 계정의 잔고확인
    >web3.fromWei(eth.getBalance(eth.accounts[0]), "ether") ⇒ 웨이를 이더로 표시

10 송금을 위해 계정 unlock
>personal.unlockAccount(eth.accounts[0])
>personal.unlockAccount(eth.accounts[0], "pass0") : 패스워드 함께
>personal.unlockAccount(eth.accounts[0], "pass0", 0) : unlock시간, 0인경우 Geth 종료시까지

11 Ether 송금
>eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:web3.toWei(10,"ether")})
>eth.sendTransaction({from:eth.accounts[1] to:eth.accounts[2], value:web3.toWei(5,"ether")})

12 Geth 기동시 HTTP-RPC 활성화 및 백그라운드 실행
$nohup geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/devno3kaul/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>> /home/devno3kaul/data_testnet/geth.log &

  • --mine 옵션은 geth기동시 mining 실행함, 웬만하면 개발시에는 하지말자 쓸데없이 컴퓨팅 자원만 낭비됨 특히나 구글 GCP에서 할 경우 구글이 정책위반이라고 경고와 함께 VM 정지 당하는 경우가 생김
  • & 백그라운드 실행(리눅스)

12-1 Geth 기동시 HTTP-RPC 활성화 및 백그라운드 실행 마이닝옵션 빼고
$nohup geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/devno3kaul/data_testnet --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpccorsdomain "*" --rpcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" 2>> /home/devno3kaul/data_testnet/geth.log &

13 json-rpc 명령 수행 , 원격접속해서 명령어 실행할 수 있음
$curl -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"personal_listAccounts","params":[],"id":10}' localhost:8545

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

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

14 실행된 Geth에 rpc를 통해 Geth 콘솔에 접근하기
$geth attach rpc:http://localhost:8545

간단하게 Geth 초기화 및 계정생성 등 Dapp 개발을 위한 준비가 끝났다. 다음번 포스팅엔 간단한 Dapp을 개발해보자

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!