Hyperledger Fabric—Maven way to upgrade contracts

1.1 Before upgrading the contract, the modified code needs to be uploaded to the remote warehouse

First enter the chaincode directory under the fabric-samples directory

cd /root/fabric-samples/chaincode

Then enter the source code directory of your own project, execute the git pull command, and you will be asked to enter the git account passwordinsert image description here

1.2 Return to the directory where test-network is located

Return to the directory where the test-network is located so that the chaincode can be packaged with other network components.

cd ../../test-network

1.3 Add the binary files in the bin directory to the CLI path

Chaincode packages in the desired format can be created using the peer CLI, add these binaries to your CLI path using the following commands.

export PATH=${PWD}/../bin:$PATH

1.4 Set FABRIC_CFG_PATH to point to the core.yaml file in fabric-samples

export FABRIC_CFG_PATH=$PWD/../config/

1.5 Create chain code package

peer lifecycle chaincode package hyperledger-fabric-contract-java-demo.tar.gz --path ../chaincode/hyperledger-fabric-contract-java-demo/ --lang java --label hyperledger-fabric-contract-java-demo_2

insert image description here

2 Install the chaincode package

After packaging the hyperledger-fabric-contract-java-demo smart contract, we can install the chaincode on the peer node. Chaincode needs to be installed on every peer that will approve transactions. Because we will set the endorsement policy to require endorsements from both Org1 and Org2, we need to install the chaincode on the peer nodes of both organizations: peer0.org1.example.com and peer0.org2.example.com

2.1 Org1 peer node installation chain code

Set the following environment variables to operate the peer CLI as an Org1 administrator.

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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

Use the peer lifecycle chaincode install command to install the chaincode on the peer node.

peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz

insert image description here

You can open another window during installation to view the installation log of the chaincode package

// 查看所有容器
docker ps

// 查看最新容器的日志
docker logs -f  容器id

insert image description here
To install the contract in the Maven way, you need to build the maven environment every time and download the dependencies required for the program to run, so you need to wait for a while
before executing the installation command

peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz

Failed to invoke backing implementation of 'InstallChaincode': chaincode already successfully installed appears, indicating that the installation was successful
insert image description here

2.2 Org2 peer node installation chain code

Set the following environment variables to operate the peer CLI as an Org2 administrator.

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_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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051

Use the peer lifecycle chaincode install command to install the chaincode on the peer node.

peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz
Note: When installing the chain code, the chain code is built by the peer node. If there is a problem with the smart contract code, the install command will return all build errors from the chaincode. Because the process of installing java chaincode needs to go through the process of maven building and downloading dependent packages, this process may be slow, so the install command may return a timeout error:. But in fact, the build task is still being executed in the docker container of the chaincode at this time and has not been completed. When the build is successful, the chaincode package will be installed successfully. # 3 Defined by chaincode

After installing the chaincode package, you need to pass the organization's chaincode definition. This definition includes important parameters for chaincode management, such as name, version, and chaincode approval policy.

If the organization has chaincode installed on their peer nodes, they need to include the package ID in the chaincode definition passed by their organization. The package ID is used to associate the chaincode installed on the peer with the passed chaincode definition and allows organizations to use the chaincode to endorse transactions.

3.1 Query package ID

peer lifecycle chaincode queryinstalled

The queryinstalled command will return a list of chaincodes installed on the peer node:

Installed chaincodes on peer:
Package ID: hyperledger-fabric-contract-java-demo_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506, Label: hyperledger-fabric-contract-java-demo_1
Package ID: hyperledger-fabric-contract-java-demo_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506, Label: hyperledger-fabric-contract-java-demo_2

We will use the bundle ID when going through the chaincode, so, save the bundle ID as an environment variable. Paste the returned bundle ID into the command below.


NOTE: Bundle IDs are different for all users, so this step needs to be done using the bundle ID returned from the command window in the previous step. Instead of copying the command directly! ! !

Use the package tag to find the package ID of the new chaincode and save it as a new environment variable:

export NEW_CC_PACKAGE_ID=hyperledger-fabric-contract-java-demo_4:993171856031ede4117405448c930c57ef99b4d54623d9b7247ee6f7152d8b71

3.2 Org2 defined by chain code

Because the environment variable has been set to peer CLI to operate as an Orig2 administrator, we can pass the chaincode definition of hyperledger-fabric-contract-java-demo at the Org2 organization level. Use the peer lifecycle chaincode approveformyorg command to define through the chain code:
here you need to pay attention to modify the version and serial number: version 2.0 and sequence 2

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 2.0 --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

3.2 Org1 defined by chain code

Set the following environment variables to run as Org1 administrator:

export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051

Through the chain code definition with the peer lifecycle chaincode approveformyorg command

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 2.0 --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

4 Commit the chaincode definition to the channel

Use the peer lifecycle chaincode checkcommitreadiness command to check that channel members have approved the same chaincode definition:

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 2.0 --sequence 2 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json

If the command returns the following JSON, the chaincode is ready to be upgraded:

{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": true
	}
}

insert image description here

Upgrade chaincode:

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 2.0 --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

The peer lifecycle chaincode querycommitted command can be used to confirm that the chaincode definition has been committed to the channel.

peer lifecycle chaincode querycommitted --channelID mychannel --name hyperledger-fabric-contract-java-demo --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

You can see that the version is 2.0 and the serial number is 2, indicating that the submission was successful
insert image description here

important point:

Pay attention to modifying the version number and serial number when defining the chain code and upgrading and installing the chain code

Guess you like

Origin blog.csdn.net/qq_44154912/article/details/124859101