Giả sử Org1 và Org2 muốn sử dụng chaincode ở một ngôn ngữ khác, chẳng hạn Golang. Các tổ chức sẽ cần phải upgrade chaincode.
Để upgrade chaincode, ta cần phải đóng gói chaincode mới và cài đặt lên các peer. Sau đó, ta cũng cần phải approve lại chaincode package với package ID mới, phiên bản chaincode mới và sequence number tăng lên một giá trị1.
Package the Chaincode
Thư mục chứa mã nguồn của chaincode là fabric-samples/asset-transfer-basic/chaincode-go
. Truy cập vào thư mục này và chạy lệnh sau để cài đặt các dependency của chaincode:
GO111MODULE=on go mod vendor
Nếu thành công, các dependency sẽ được cài đặt vào thư mục vendor
.
Sau đó, quay về thư mục fabric-samples/test-network
và thiết lập các biến môi trường cần thiết để chạy chương trình peer
:
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
Chạy câu lệnh sau để đóng gói chaincode với version mới:
peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_1.0.2
Chaincode được đóng gói sẽ có tên là basic.tar.gz
và được lưu ở thư mục hiện tại.
Install the Chaincode Package
Tiếp theo, ta thiết lập biến môi trường để chạy peer
dưới danh nghĩa của Org1 để cài đặt chaincode package lên peer của tổ chức này.
peer lifecycle chaincode install basic.tar.gz
Output khi cài đặt thành công:
2024-01-23 11:25:25.140 +07 0001 INFO [cli.lifecycle.chaincode] submitInstallProposal -> Installed remotely: response:<status:200 payload:"\nLbasic_1.0.2:d4d93369f6ae4a030d59265cd888b5e039211fdbd2cfbe8895926c73cffd8d01\022\013basic_1.0.2" >
2024-01-23 11:25:25.140 +07 0002 INFO [cli.lifecycle.chaincode] submitInstallProposal -> Chaincode code package identifier: basic_1.0.2:d4d93369f6ae4a030d59265cd888b5e039211fdbd2cfbe8895926c73cffd8d01
Package ID của chaincode mới là:
basic_1.0.2:d4d93369f6ae4a030d59265cd888b5e039211fdbd2cfbe8895926c73cffd8d01
Cài đặt tương tự ở trên peer của Org2.
Sau khi cài đặt thì ta có thể truy vấn thông tin của chaincode package như sau:
peer lifecycle chaincode queryinstalled
Kết quả có thể là:
Installed chaincodes on peer:
Package ID: basic_1.0.1:9fc1aea9e8fb20b27ddb041e705e0e31fd6ccc036868263db24c457ca1eb02cd, Label: basic_1.0.1
Package ID: basic_1.0.2:d4d93369f6ae4a030d59265cd888b5e039211fdbd2cfbe8895926c73cffd8d01, Label: basic_1.0.2
Có thể thấy, trên peer hiện tại đã có hai phiên bản của chaincode.
Approve a Chaincode Definition
Thiết lập biến môi trường cho package ID mới:
export NEW_CC_PACKAGE_ID=basic_1.0.2:d4d93369f6ae4a030d59265cd888b5e039211fdbd2cfbe8895926c73cffd8d01
Tiếp theo, ta approve chaincode definition ở trên Org2 như sau:
peer lifecycle chaincode approveformyorg \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID mychannel \
--name basic \
--version 1.0.2 \
--package-id $NEW_CC_PACKAGE_ID \
--sequence 2 \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
Chú ý version và sequence đã được thay đổi để target đến chaincode mới.
Thực hiện tương tự cho Org1.
Truy vấn xem hai tổ chức đã approve hay chưa:
peer lifecycle chaincode checkcommitreadiness \
--channelID mychannel \
--name basic \
--version 1.0.2 \
--sequence 2 \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \
--output json
Kết quả:
{
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}
Committing the Chaincode Definition to the Channel
Cuối cùng, ta thực hiện commit chaincode definition xuống ledger như sau:
peer lifecycle chaincode commit \
-o localhost:7050 \
--ordererTLSHostnameOverride orderer.example.com \
--channelID mychannel \
--name basic \
--version 1.0.2 \
--sequence 2 \
--tls \
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" \
--peerAddresses localhost:7051 \
--tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" \
--peerAddresses localhost:9051 \
--tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt"
Related
list
from [[Hyperledger Fabric - Upgrading a Chaincode]]
sort file.ctime asc
Resources
- https://hyperledger-fabric.readthedocs.io/en/latest/deploy_chaincode.html#upgrading-a-smart-contract
Footnotes
-
các bước thực hiện tương tự như Hyperledger Fabric - Deploying a Chaincode to a Channel ↩