세션3. geth 클라이언트 실습 및...

13
Geth 클라이언트 실습 / 시각화 이재진/김현욱 [email protected] [email protected] Byzantium Megara Geth1.7 ethereum 연구회

Upload: jay-jh-park

Post on 22-Jan-2018

3.499 views

Category:

Software


0 download

TRANSCRIPT

Page 1: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth 클라이언트실습 / 시각화이재진/김현욱

[email protected]@live.co.kr

Byzantium Megara Geth1.7

ethereum 연구회

Page 2: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth시작하기

ethereum 연구회

Page 3: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

실습 시나리오 및 환경 설명

3

Geth 설치부터채굴까지 ~

Alice

Bob

Miner

: Bob 에게 5 이더송금

: Alice 로부터 5 이더 수금

: 트랜잭션확인(채굴)

ethereum 연구회

Page 4: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth & Eth

- 이더리움의전체기능을 사용할수있는 풀클라이언트로서다중인터페이스제공

: 커맨드라인 모드의부가 명령어로대화형자바스크립트콘솔과 JSON-RPC 서버

- Geth 커맨드라인 : geth [options] command [--command options] [arguments...]

$ geth --identity "JayChain" --rpc --rpcport "8800" --rpccorsdomain "*" --datadir "/Users/jay/privatechain" --port

"30303" --nodiscover --rpcapi "db,eth,net,web3" --networkid 2017 console--identity "JayChain" //내프라이빗노드식별자.--rpc // RPC 인터페이스가능하게함.--rpcport "8800" // RPC포트지정--rpccorsdomain "*" //접속가능한RPC클라이언트URL지정 , 가능한 *(전체허용) 보다는URL을지정하는게보안상좋음.--datadir "/Users/jay/privatechain" //커스텀디렉토리지정--port "30303" //네트웍 Listening Port지정--nodiscover //같은제네시스블록과네트웍ID에있는블록에연결방지--rpcapi "db,eth,net,web3" // RPC에의해서접근을허락할 API--networkid 2017

console //출력을콘솔로함. 4

이더리움 CLI 클라이언트

Page 5: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth & Eth

- 대화형자바스크립트옵션 ( console , attach , js )

- 이더리움은자바스크립트런타임환경(JSRE)을대화형콘솔또는비대화형스크립트모드로제공

$ geth console // 콘솔에서커맨드라인모드로 JSRE 사용

$ geth attach [ipc:/some/custom/path | rpc:http://127:0.0.1:8545] // 작동중인 geth 노드의콘솔에연결후사용

$ geth console 2 >> /dev/null // 화면에 log를출력하지않는다.

$ geth js demo.js 2 >> geth.log // 비대화형모드 , demo.js 파일을실행한다.

- 커맨드라인모드 : JSON-RPC 서버옵션

- JSON-RPC는경량RPC 프로토콜을통해이더리움을호출하고그결과를 JSON 포맷으로받음.

- 이더리움RPC 인터페이스를제공하는Web3.js 라이브러리를사용하여자바스크립트응용프로그램내에서이더리

움노드호출

- 기본 JSON-RPC 기본엔드포인트 : http://localhost:8545

- Geth 구동 : $ geth –rpc –rpcaddr <IP주소 > --rpcport <포트번호>

- Geth 콘솔: >> admin.startRPC(IP주소 , 포트번호) 5

이더리움 CLI 클라이언트

Page 6: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth & Eth

1. 커스템제네시스파일 생성 , CustomGenesis.json{

"config": {"chainId": 2017,"homesteadBlock": 0,"eip155Block": 0,"eip158Block": 0

},"difficulty": "200000000","gasLimit": "2100000","alloc": {

"0x81162835c5164e390c714a6e213e64c6b4f00132": { "balance": "400000000000000000000" },"0x29d5b5488fe14e36dba76f5d667448bc9e13312d": { "balance": "300000000000000000000" }}

}2. 네트워크 ID 지정 ( 메인넷 1번 )geth --datadir /privatechain init CustomGenesis.json --networkid 2017

6

이더리움 CLI 클라이언트 – 개인네트워크구축

Page 7: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth & Eth

1. 어카운트생성 - Alice , > personal.newAccount(“Alice”)2. 어카운트의목록조회 , > eth.accounts

3. 마이닝후보상을 받을이더베이스(etherbase)를지정 - Alice어카운트.> miner.setEtherbase(personal.listAccounts[0])> miner.setEtherbase("0x81162835c5164e390c714a6e213e64c6b4f00132")

4. 마이닝실행, 마이닝후 리워드보상은앞서지정한 Alice 어카운트로보내짐> miner.start()> miner.start(2) // 마이닝쓰레드를 2개로지정

5. 계정잔액조회(Wei 단위로 표시되기때문에 1/1018로계산 )> eth.getBalance(eth.accounts[0]) // eth.getBalance(eth.coinbase) > web3.fromWei(eth.getBalance(eth.coinbase),"ether") > web3.fromWei(eth.getBalance(eth.coinbase),"shannon")

6. 생성된블록수 조회. > eth.blockNumber 7

Geth CLI 사용 – Alice가Bob에게 5이더송금하기.

Page 8: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth & Eth

7. > personal.newAccount("Bob") // 송금을위해 Bob 라는계정을 추가생성

8. Alice -> Bob 로이더송금 (21,000 gas is the minimum for sending a transaction )> eth.sendTransaction({from: eth.coinbase, to: "0x29d5b5488fe14e36dba76f5d667448bc9e13312d", value: web3.toWei(5, "ether")})

> web3.eth.sendTransaction({from:'0x81162835c5164e390c714a6e213e64c6b4f00132' , to:'0x29d5b5488fe14e36dba76f5d667448bc9e13312d', value:web3.toWei(5,'ether'), gasLimit: 21000, gasPrice: 20000000000}) //gasLimit: 21000, gasPrice: 20000000000} ->옵션 사항임.

> acct1 = web3.eth.accounts[0]> acct2 = web3.eth.accounts[1]> web3.eth.sendTransaction({from: acct1, to: acct2, value: web3.toWei(5, 'ether'), gasLimit: 21000, gasPrice: 20000000000})

➔위의 트렌젝션을실행하면 Alice 계정에서돈을옮겨야 하는데 LOCK되어있으니 Unlock 시키라고 함.

8

Geth CLI 사용 – Alice가Bob에게 5이더송금하기.

ethereum 연구회

Page 9: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth & Eth

9. Alice계정을 UNLOCK을시킴> personal.listWallets[0].status // 어카운트의상태 확인”> web3.personal.unlockAccount(eth.coinbase) // 어카운트락 해제> web3.personal.unlockAccount("0x81162835c5164e390c714a6e213e64c6b4f00132”) > web3.personal.unlockAccount(eth.coinbase, 'Jay') > personal.lockAccount(eth.coinbase) // 어카운트락

➔패스워드에는 Alice 라고 계정이름을넣으면됨( 대소문자구별함). ➔트렌젝션이즉시 수행되지않고수행 대기상태가됨(pending). 마이닝작업을수행해야 함.

10. 수행대기중인 트랜잭션확인> eth.pendingTransactions // 수행대기중인미확정트렌젝션 조회

11. 마이닝 수행. > miner.start()> eth.pendingTransactions // 미확정트렌젝션 재확인> eth.getBalance(eth.accounts[1]) //다음으로 Bob의 Account에 Ether가송금됨을 확인할수있음

9

Geth CLI 사용 – Alice가Bob에게 5이더송금하기.

ethereum 연구회

Page 10: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth상태모니터링및시각화Metrics and Monitoring

ethereum 연구회

Page 11: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Metrics

Metrics 시스템의목표는로그와비슷하지만, 코드사이에분석(카운터변수, 공용인터페이스, API, console hook, 기타등)을위한복잡한구조를만들어야할필요없이임의의Metric 모음을추가하기만하면되는것이다.대신, 필요할때마다Metric을업데이트해주어야하고, 자동으로수집되어야한다, API를통해드러나고, 질의가가능해야하며, 분석을위해시각화할수있어야한다.

11

Geth Metrics 시스템

•Meters : 물리적인 계량기(전기, 수도등)와 비슷하게, 통과하는 무언가의 양과 속도를측정할수 있다. Meter에는 특정측정 단위(바이트, 블록, malloc 등)가없으며, 임의의 이벤트만센다 .•Timers : 이벤트의 발생이 측정 될뿐만아니라, 기간도 수집되는 Meter의 확장. Meter와마찬가지로 Timer도 임의의 이벤트를 측정 할수있지만 각각의 시간은 개별적으로할당해야 한다.

Metrics 종류

Metrics : Geth 의로깅 시스템

ethereum 연구회

Page 12: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth 모니터링

var (headerInMeter =

metrics.NewMeter("eth/downloader/headers/in")headerReqTimer =

metrics.NewTimer("eth/downloader/headers/req")headerDropMeter =

metrics.NewMeter("eth/downloader/headers/drop")headerTimeoutMeter =

metrics.NewMeter("eth/downloader/headers/timeout")

bodyInMeter = metrics.NewMeter("eth/downloader/bodies/in")bodyReqTimer =

metrics.NewTimer("eth/downloader/bodies/req")

12

metrics.go

meter := metrics.NewMeter("system/memory/allocs")timer := metrics.NewTimer("chain/inserts")meter.Mark(n) // Record the occurrence of ̀ n` eventstimer.Update(duration) // Record an event that took ̀ duration`timer.UpdateSince(time) // Record an event that started at `time`timer.Time(function) // Measure and record the execution of `function`

case packet := <-d.headerCh:// Make sure the active peer is giving us the skeleton

headersif packet.PeerId() != p.id {

log.Debug("Received skeleton from incorrect peer", "peer", packet.PeerId())

break}headerReqTimer.UpdateSince(request)timeout.Stop()

Page 13: 세션3. geth 클라이언트 실습 및 모니터링과 시각화

Geth 모니터링

13

https://github.com/ethereum/go-ethereum/wiki/Metrics-and-Monitoring

./geth monitor

ethereum 연구회