본문 바로가기

옥탑방주인/Hyperledger

Hyperledger Sawtooth on Ubuntu 16.04 (Sawtooth v1.0.2)

Using Sawtooth on ubuntu 16.04




이 절차는 Ubuntu에서 응용 프로그램 개발을 위해 Hyperledger Sawtooth를 설정하는 과정을 안내하고 응용 프로그램 개발에 필요한 기본적인 Sawtooth 개념을 소개하며 다음 작업을 수행하는 과정을 안내합니다 :



  • 우분투 16.04에 소투스(Sawtooth)를 설치하는 법
  • Sawtooth validator와 관련된 REST API와 두개의 트랜잭션 프로세서 컴포넌트를 시작하는 법
  • transaction family 셋팅 구성하는 법
  • transaction을 REST API에 제출(submitting)
  • sawtooth command를 사용하여 블록, 트랜잭션, 및 state 보기.

이 섹션이 완료되면, Sawtooth의 REST API를 사용하는 트랜잭션 패밀리 및 작성 클라이언트로 비즈니스 로직을 구현하는 것과 같은 애플리케이션 개발 주제를 설명하는 후속 섹션을 준비 할 것입니다.




Overview of Sawtooth Components 



실행중인 Sawtooth 네트워크는 다음과 같은 응용 프로그램 또는 프로세스로 구성됩니다.


이 다이어그램은 두개의 validator와 두개의 transaction processor가 있는 간단한 네트워크를 보여준다.



Installing Sawtooth


Getting the Sawtooth Packages for Ubuntu


소투스 패키지 저장소는 두가지 타입(stable, nightly)의 우분투 패키지를 제공한다. stable repository를 사용해서 설치하는것을 추천한다.


stable repository를 추가하려면, 호스트 시스템 터미널에 아래의 명령어를 입력해라


$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 8AA7AF1F1091A5FD

$ sudo add-apt-repository 'deb http://repo.sawtooth.me/ubuntu/1.0/stable xenial universe'

$ sudo apt-get update



nightly repository를 사용하려면, 아래의 커맨드를 터미널에 입력해라.

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 44FC67F19B2466EA

$ sudo apt-add-repository "deb http://repo.sawtooth.me/ubuntu/nightly xenial universe"

$ sudo apt-get update



주의사항

nightly build는 설명서와 동기화되지 않을 수 있으며 장기간 실행되는 네트워크 테스트를 거치지 않았습니다. stable repository를 사용하는 것이 좋습니다.




Installing the Sawtooth Packages


Sawtooth는 sawtooth 메타 패키지를 사용하여 함께 설치할 수있는 여러 개의 우분투 패키지로 구성됩니다. 동일한 호스트 터미널 창에서 다음 명령을 실행하십시오.


$ sudo apt-get install -y sawtooth 



설치 후 언제든지 다음 명령을 사용하여 설치된 sawtooth 패키지를 볼 수 있습니다.


$ dpkg -l '*sawtooth*' 





Creating the Genesis Block


대부분의 경우, validator를 시작하면 Genesis block을 만들 필요가 없습니다. 왜냐하면 validator가 기존의 분산 원장 네트워크(distributed ledger network)에 연결하기 때문입니다. 그러나 개발자는 short-lived test network를 만들어야 할 수 있습니다. 이 경우 새 네트워크를 인스턴스화 할 때 genesis block을 만들어야합니다.


Genesis block에는 sawtooth의 원장을 만들고 처음 사용할 때 필요한 초기 값이 포함되어 있습니다. 설정되어야하는 genesis block의 설정 중 하나는 sawset genesis 명령을 사용하여 아래에 표시된대로 구성 설정을 설정하고 변경하도록 허가 된 키입니다.

Genesis block을 만들려면, 터미널을 열어서 아래의 명령어를 입력해서 실행해라:


$ sawtooth keygen

$ sawset genesis

$ sudo -u sawtooth sawadm genesis config-genesis.batch 



마지막 명령어를 입력한 후에는, 아래와 같은 결과가 나타날 것이다.

Processing config-genesis.batch...

Generating /var/lib/sawtooth/genesis.batch



팁 

validator를 실행하기 전에 기존 블록 체인 데이터를 삭제해야하는 경우 /var/lib/sawtooth에서 모든 파일을 제거하십시오.




Starting the Validator


기본 포트에서 로컬로 수신하는 validator를 시작하려면 다음 명령을 실행합니다.


$ sudo sawadm keygen

$ sudo -u sawtooth sawtooth-validator -vv


로깅 출력은 검사기 터미널 창에 표시됩니다. 출력은 아래와 비슷하게 나온다.



팁 

validator를 멈추길 원하면 CTRL-c(컨트롤+C)를 몇차례 눌러라.



Other Start-up Options for the Validator


위의 sawtooth-validator 명령에서 -vv 플래그는 DEBUG 메시지를 포함하도록 로그 수준을 설정한다. 로깅 출력을 줄이면서 validator를 실행하려면 -v를 사용하거나 플래그를 생략하십시오.


기본적으로 validator는 네트워크 및 구성 요소 통신에 대한 루프백 인터페이스를 수신합니다. 사용 된 인터페이스와 포트를 변경하기 위해 -bind 플래그를 사용할 수 있습니다. 사용 가능한 플래그에 대한 자세한 정보는 sawtooth-validator를 참조하십시오. 다음 명령은 기본 동작과 동일합니다.


sudo -u sawtooth sawtooth-validator -vv --bind network:tcp://127.0.0.1:8800 --bind component:tcp://127.0.0.1:4004 


validator는 생성 된 상태의 차이없이 직렬(serial) (기본값) 또는 병렬(parallel)로 트랜잭션을 처리 할 수 ​​있습니다. 병렬로 처리하려면 --scheduler parallel 옵션을 사용하십시오. 병렬 옵션을 최대한 활용하려면 예상되는 높은 볼륨이있는 트랜잭션 유형에 대해 여러 트랜잭션 프로세서를 시작하십시오.



Starting the REST API


실행중인 validator를 구성하고 일괄 처리(batch)를 제출하고 원장의 상태를 쿼리하려면 REST API 응용 프로그램을 시작해야합니다.


터미널을 열고 로컬 validator에 접속하는법과 REST API를 실행하는 명령어는 아래와 같다.


$ sudo -u sawtooth sawtooth-rest-api -v




Starting and Configuration the Transaction Processors


이 섹션에서는 IntegerKey 및 family transaction processors 셋팅을 시작하고 REST API가 실행 중인지 확인한 다음 validator 또는 validator network에 이러한 transaction family의 트랜잭션을 수락하도록 지시하는 방법에 대해 설명합니다. 


transaction processor는 validator가 시작되기 전 또는 후에 시작될 수 있다.



Starting the IntegerKey Transaction Processor


IntegerKey transaction processor는 테스트용으로 사용될 수 있는 transaction family의 간단한 예제로 제공된다.


IntegerKey transaction processor를 시작하려면, 새 터미널을 열고 아래의 명령어를 입력해라.


$ sudo -u sawtooth intkey-tp-python -v 




노트 

 기본적으로 트랜잭션 프로세서는 포트 4004의 local validator에 연결을 시도합니다.이 매개 변수는 다른 끝점을 인수로 전달하여 수정할 수 있습니다. 다음 엔드 포인트 인수는 기본값과 같습니다. intkey-tp-python -v tcp : //127.0.0.1 : 4004


이 명령어는 IntegerKey transaction family로부터 트랜잭션의 이해하고 처리 할 수 있는 Intkey handler를 사용해서 트랜잭션 프로세서를 시작한다. 


트랜잭션 프로세서는 아래와 같은 결과를 만들어낸다.


[23:07:57 INFO core] register attempt: OK 



Starting the Settings Family Transaction Processor


Sawtooth는 Python으로 작성된 family transaction processor 설정과 함께 On-Chain 설정을 저장하는 transaction family 구성을 제공합니다.


family transaction processor 셋팅을 하려면 아래와 같은 명령어를 입력하여라.


 $ sudo -u sawtooth settings-tp -v


transaction processor가 validator에 등록되었는지 확인해봐라. 만약 성공적으로 등록되었다면 아래와 같은 출력이 나왔을 것이다.


 [21:03:55.955 INFO processor_handlers] registered transaction processor: identity=b'6d2d80275ae280ea', family=sawtooth_settings, version=1.0, namespaces=<google.protobuf.pyext._message.RepeatedScalarContainer object at 0x7e1ff042f6c0>

[21:03:55.956 DEBUG   interconnect] ServerThread sending TP_REGISTER_RESPONSE to b'6d2d80275ae280ea'


Verifying that the REST API is Running


실행중인 validator 구성하려면, REST API는 반드시 실행중이여야 한다. 아래와 같은 명령어를 입력해서 확인해봐라.



 $ ps aux | grep sawtooth-rest-api

sawtooth  2829  0.0  0.3  55756  3980 pts/0    S+   19:36   0:00 sudo -u sawtooth sawtooth-rest-api -v
sawtooth  2830  0.0  3.6 221164 37520 pts/0    Sl+  19:36   0:00 /usr/bin/python3 /usr/bin/sawtooth-rest-api -v
ubuntu    3004  0.0  0.0  12944   928 pts/4    S+   19:54   0:00 grep -E --color=auto sawtooth-rest-api


만약 REST API가 실행중이지 않다면 아래와 같은 명령어를 입력해라.


$ sudo -u sawtooth sawtooth-rest-api -v



Configuration the List of Transaction Families (Ubuntu version)


on-chain 셋팅중 하나는 지원되는 transaction family의 리스트중 하나이다. 이 셋팅을 구성하려면, 다음 단계에 따라 일괄 처리(batch)를 생성(create)하고 제출(submit)하여 transaction family 설정을 변경해라. 


이 예제에서는, sawset 명령은 설정 변경을 포함하는 트랜잭션 배치를 생성(create)하고 제출(submit)하는 JSON array을 명시한다. 이 JSON array는 validator또는 validator network에 다음 유형의 트랜잭션을 허용하도록 지시한다:


  • intkey (IntegerKey transcation family)
  • sawtooth_settings (Settings transaction family)


새로운 세팅을 포함하는 배치를 생성하거나 제출하려면, 아래의 커맨드를 입력해라.


$ sawset proposal create sawtooth.validator.transaction_families='[{"family": "intkey", "version": "1.0"}, {"family":"sawtooth_settings", "version":"1.0"}]'

validator terminal 창에서 출력은 TP_PROCESS_REQUEST message, 허가된 키(authorized key)의 정보, transaction family를 포함하고 있다.



Creating and Submitting Transactions


intkey 명령어는 테스트 목적을 위한 샘플 intkey (IntegerKey) transcation을 생성한다.


이 색션은 다음의 작업을 가이드해준다:


  1. 임의의 값의 키를 가지고있는 intkey transaction의 batch를 준비해라
  2. 블록체인에 저장된 기존 state에 적용할 inc (increment)와 dec (decrement) transaction을 생성한다.
  3. validator에 이러한 transaction들을 제출(submitting)한다.


아래의 명령어를 입력해라:


$ intkey create_batch

Writing to batches.intkey...


$ intkey load

batches: 2 batch/sec: 135.96900883377907


intkey transaction processor의 출력 로깅을 관찰하여 transaction 처리 과정을 볼 수 있다.



Viewing Blocks and State


sawtooth bloack 명령어를 사용해서 Merkle tree의 노드와 블록체인에 저장된 블록들을 볼 수 있다.


노트 

sawtooth 명령어는 모든 하위 명령어에 대한 도움말을 제공한다. 예를들어, block 하위 커맨드에 대한 도움을 얻고싶다면, sawtooth block -h 명령어를 입력해봐라.



Viewing the List of Blocks


state에 저장된 블록을 보려면 sawtooth block list 명령어를 입력해라.


$ sawtooth block list 



팁 

모든 출력을 보려면 터미널 윈도우 창을 확장하면 최대157 글자(character)를 볼 수 있다.



Viewing a Particular Block


위의 sawtooth 목록의 출력을 사용하여 보려는 블록 ID를 복사 한 후 다음 sawtooth block show 명령의 {BLOCK_ID} 자리에 붙여 넣습니다.


$ sawtooth block show {BLOCK_ID} 



위의 명령어의 출력은 아래와 같이 비슷하게 나올 것이다.


 batches:

- header:

    signer_public_key: 0380be3421629849b1d03af520d7fa2cdc24c2d2611771ddf946ef3aaae216be84

    transaction_ids:

    - c498c916da09450597053ada1938858a11d94e2ed5c18f92cd7d34b865af646144d180bdc121a48eb753b4abd326baa3ea26ee8a29b07119052320370d24ab84

    - c68de164421bbcfcc9ea60b725bae289aecd02ddde6f520e6e85b3227337e2971e89bbff468bdebe408e0facc343c612a32db98e5ac4da2296a7acf4033073cd

    - faf9121f9744716363253cb0ff4b6011093ada6e19dae63ae04a58a1fca25424779a13628a047c009d2e73d0e7baddc95b428b4a22cf1c60961d6dcae8ee60fa

  header_signature: 2ff874edfa80a8e6b718e7d10e91970150fcc3fcfd46d38eb18f356e7a733baa40d9e816247985d7ea7ef2492c09cd9c1830267471c6e35dca0d19f5c6d2b61e

  transactions:

  - header:

      batcher_public_key: 0380be3421629849b1d03af520d7fa2cdc24c2d2611771ddf946ef3aaae216be84

      dependencies:

      - 19ad647bd292c980e00f05eed6078b471ca2d603b842bc4eaecf301d61f15c0d3705a4ec8d915ceb646f35d443da43569f58c906faf3713853fe638c7a0ea410

      family_name: intkey

      family_version: '1.0'

      inputs:

      - 1cf126c15b04cb20206d45c4d0e432d036420401dbd90f064683399fae55b99af1a543f7de79cfafa4f220a22fa248f8346fb1ad0343fcf8d7708565ebb8a3deaac09d

      nonce: 0x1.63021cad39ceep+30

      outputs:

      - 1cf126c15b04cb20206d45c4d0e432d036420401dbd90f064683399fae55b99af1a543f7de79cfafa4f220a22fa248f8346fb1ad0343fcf8d7708565ebb8a3deaac09d

      payload_sha512: 942a09c0254c4a5712ffd152dc6218fc5453451726d935ac1ba67de93147b5e7be605da7ab91245f48029b41f493a1cc8dfc45bb090ac97420580eb1bdded01f

      signer_public_key: 0380be3421629849b1d03af520d7fa2cdc24c2d2611771ddf946ef3aaae216be84

    header_signature: c498c916da09450597053ada1938858a11d94e2ed5c18f92cd7d34b865af646144d180bdc121a48eb753b4abd326baa3ea26ee8a29b07119052320370d24ab84

    payload: o2ROYW1lZnFrbGR1emVWYWx1ZQFkVmVyYmNpbmM=



Viewing Global State


sawtooth state list 명령어를 사용해서 Merkle tree에 있는 노드 리스트를 확인할 수 있다.


$ sawtooth state list 


팁 

주소는 node id와 동일하다. 



Viewing Data in a Node


특정 노드에 데이터를 보려면 sawtooth state show 명령어를 사용하면 된다. 위의 sawtooth state list 명령의 출력을 사용하여 보려는 노드 ID를 복사 한 후 다음 명령의 {NODE_ID} 자리에 붙여 넣으십시오.


$ sawtooth state show {NODE_ID} 


출력은 아래와 비슷하게 나올것이다.


DATA: "b'\xa1fcCTdcH\x192B'"

HEAD: "0c4364c6d5181282a1c7653038ec9515cb0530c6bfcb46f16e79b77cb524491676638339e8ff8e3cc57155c6d920e6a4d1f53947a31dc02908bcf68a91315ad5"



Stopping Sawtooth Components


validator를 멈추고싶으면, 컨트롤-C (CTRL-C)를 몇차례 눌러라.


REST API와 transaction processor에도 한번의 컨트롤-C를 눌르면 종료된다.