하이퍼레저 패브릭 - 채널에 조직 추가하기

choko's avatar
Jun 29, 2024
하이퍼레저 패브릭 - 채널에 조직 추가하기

ORG1_CERTIFICATE, ORG1_PRIVATE_KEY

  • ORG1_CERTIFICATE 예시:
    • peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem
    • peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/signcerts/cert.pem
  • ORG1_PRIVATE_KEY 예시:
    • peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/keystore/6297d56c78be7eabf5adb67403afd32936591af2bcc9a7114d39458d1d442d90_sk
    •  
       

Peer간 연결이 안될 때

export CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp export CORE_PEER_ADDRESS=localhost:7051 peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n ledger -c '{"Args":["CreateAsset","asset1","blue","5","tom","35"]}' ## 로그 Error: error getting endorser client for invoke: endorser client failed to connect to 0.0.0.0:7051: failed to create new connection: context deadline exceeded ## 해결 CORE_PEER_TLS_ENABLED = true # 위 환경변수를 설정해 준 후 다시 체인코드 invoke를 하면 에러가 해결된다.
 
 

새 Organization 추가하기

기존 network.sh로 만든 Fabirc network는 Org가 2로 구성되어 있었다. 채널에 새 Org(3)를 추가하는 방법을 알아보자.
 

Org3 암호화 자료 생성

org3-crypto.yaml의 구성으로, Org3 Peer에 대한 인증서 및 키를 생성한다.
cd ./test-network ./network.sh down # channel1 이름으로 채널 생성 ./network.sh up createChannel -c channel1 # addOrg3 폴더로 이동 cd addOrg3 # cryptogen generate로 org3-crypto.yaml 구성으로 Org를 만든다. cryptogen generate --config=org3-crypto.yaml --output="../organizations" # org3-crypto.yaml 구성은 아래와 같다.
test-network/organizations/peerOrganizations 에서 Org3의 암호화 자료를 찾을 수 있다.
org3-crypto.yaml
# Copyright IBM Corp. All Rights Reserved. # # SPDX-License-Identifier: Apache-2.0 # # --------------------------------------------------------------------------- # "PeerOrgs" - Definition of organizations managing peer nodes # --------------------------------------------------------------------------- PeerOrgs: # --------------------------------------------------------------------------- # Org3 # --------------------------------------------------------------------------- - Name: Org3 Domain: org3.example.com EnableNodeOUs: true Template: Count: 1 SANS: - localhost Users: Count: 1
 
# org3.json에 다음 내용을 저장한다. # 1. Org의 CA 루트 인증서 # 2. Org3를 식별하기 위해 GOSSIP 프로토콜에서 사용하는 tls 루트 인증서 export FABRIC_CFG_PATH=$PWD configtxgen -printOrg Org3MSP > ../organizations/peerOrganizations/org3.example.com/org3.json
 

Org3 구성요소 가져오기

docker-compose를 이용하여 Org3의 Peer을 불러온다. j
docker-compose-org3.yaml
# Copyright IBM Corp. All Rights Reserved. # # SPDX-License-Identifier: Apache-2.0 # version: '2.1' volumes: peer0.org3.example.com: networks: test: name: fabric_test services: peer0.org3.example.com: container_name: peer0.org3.example.com image: hyperledger/fabric-peer:latest environment: #Generic peer variables - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric_test - FABRIC_LOGGING_SPEC=INFO #- FABRIC_LOGGING_SPEC=DEBUG - CORE_PEER_TLS_ENABLED=true - CORE_PEER_PROFILE_ENABLED=true - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt # Peer specific variabes - CORE_PEER_ID=peer0.org3.example.com - CORE_PEER_ADDRESS=peer0.org3.example.com:11051 - CORE_PEER_LISTENADDRESS=0.0.0.0:11051 - CORE_PEER_CHAINCODEADDRESS=peer0.org3.example.com:11052 - CORE_PEER_CHAINCODELISTENADDRESS=0.0.0.0:11052 - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org3.example.com:11051 - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org3.example.com:11051 - CORE_PEER_LOCALMSPID=Org3MSP volumes: - /var/run/docker.sock:/host/var/run/docker.sock - ../../organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/msp:/etc/hyperled> - ../../organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls:/etc/hyperled> - peer0.org3.example.com:/var/hyperledger/production working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer command: peer node start ports: - 11051:11051 networks: - test
 
# addOrg3 폴더에서 실행 docker-compose -f docker/docker-compose-org3.yaml up -d ## 결과 로그 ## Creating peer0.org3.example.com ... done
 

Configuration 가져오기

채널의 최신 버전 구성을 가져와야 한다. 아직 Org3은 채널(mychannel)의 구성원이 아니므로 Org1의 admin 설정으로 환경변수를 등록한 후 채널 구성을 가져올 수 있다.
# fabric-samples/test-network 폴더에서 # Org1의 admin으로 환경변수를 정해준다. export PATH=${PWD}/../bin:$PATH export FABRIC_CFG_PATH=${PWD}/../config/ export CORE_PEER_TLS_ENABLED=true export CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp export CORE_PEER_ADDRESS=localhost:7051 # 채널의 최신 구성 블록을 가져온다. # 바이너리 protobuf 채널 구성 블록을 config_block.pb 이름으로 저장한다. peer channel fetch config channel-artifacts/config_block.pb -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com -c channel1 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" ## 결과 로그 ## 2022-08-08 13:51:26.315 KST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2022-08-08 13:51:26.316 KST [cli.common] readBlock -> INFO 002 Received block: 2 cd channel-artifacts # configtxlator를 사용하여 채널 구성 블록을 JSON 형식으로 디코딩 configtxlator proto_decode --input config_block.pb --type common.Block --output config_block.json # pb -> json jq .data.data[0].payload.data.config config_block.json > config.json # 필요한 부분만 저장
 

Org3 암호화 자료 추가

# config.json에서 jq를 사용하여 org3.json의 Application 부분을 추가하여 modified_config.json 생성 jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ../organizations/peerOrganizations/org3.example.com/org3.json > modified_config.json # config.json 파일을 다시 config.pb라는 이름의 protobuf로 변환 configtxlator proto_encode --input config.json --type common.Config --output config.pb # modified_config.json를 modified_config.pb라는 이름의 protobuf로 변환 configtxlator proto_encode --input modified_config.json --type common.Config --output modified_config.pb # config.pb와 modified_config.pb간의 delta(?) 계산 # org3_update.pb는 Org3의 정의와 Org1, Org2에 대한 포인터가 포함되어 있음. configtxlator compute_update --channel_id channel1 --original config.pb --updated modified_config.pb --output org3_update.pb # org3_update.pb를 편집 가능한 JSON 형식으로 디코딩 configtxlator proto_decode --input org3_update.pb --type common.ConfigUpdate --output org3_update.json # 생성된 org3_update.json에 헤더를 달아준다. echo '{"payload":{"header":{"channel_header":{"channel_id":"'channel1'", "type":2}},"data":{"config_update":'$(cat org3_update.json)'}}}' | jq . > org3_update_in_envelope.json # 생성된 org3_update.json를 org3_update_in_envelope.json로 인코딩한다. configtxlator proto_encode --input org3_update_in_envelope.json --type common.Envelope --output org3_update_in_envelope.pb
 

Config 업데이트 전송 및 서명

  • 이전 작업으로 protobuf 바이너리인 org3_update_in_envelope.pb가 생성되었다. 그러나 구성을 Ledger에 쓰기 전에 우선 관리자의 서명이 필요하다.
  • 현재 채널 애플리케이션 수정 정책(mod_policy)은 기본값인 MAJORITY로 설정되어 있다. 이는 조직 관리자의 대다수가 서명해야 함을 의미한다. → 따라서 Org1, Org2 둘 다 서명해야 한다.
  • peer channel signconfigtx 를 통해 서명할 수 있다.
# test-network 폴더에서 실행 peer channel signconfigtx -f channel-artifacts/org3_update_in_envelope.pb #Org2에서도 서명 export CORE_PEER_TLS_ENABLED=true export CORE_PEER_LOCALMSPID="Org2MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp export CORE_PEER_ADDRESS=localhost:9051 # 서명 완료시 업데이트 peer channel update -f channel-artifacts/org3_update_in_envelope.pb -c channel1 -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" ## 결과 로그 ## 2021-01-07 18:51:48.015 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
 

Org3를 채널에 Join

# Org3 환경 변수 설정 export CORE_PEER_TLS_ENABLED=true export CORE_PEER_LOCALMSPID="Org3MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp export CORE_PEER_ADDRESS=localhost:11051 # Org3이 제네시스 블록을 가져와 채널에 참여할 수 있는지 확인 # 채널 Ledger의 첫 번째 블록(제네시스 블록)을 찾기 위해 0 명시 # 성공시 channel1.block 파일이 생성된다. peer channel fetch 0 channel-artifacts/channel1.block -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com -c channel1 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" # 생성된 제네시스 블록을 사용하여 Peer를 채널에 연결 peer channel join -b channel-artifacts/channel1.block ## 결과 로그 ## 2022-08-08 15:26:39.480 KST [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2022-08-08 15:26:39.535 KST [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
Share article

Tom의 TIL 정리방