(Fabric Learning 5) Update the fabcar chain code and upgrade the contract

1. Modify the fabcar chaincode in the official example

Add two functions to delete car and check whether car exists

// DeleteCar deletes an given asset from the world state.
func (s *SmartContract) DeleteCar(ctx contractapi.TransactionContextInterface, carNumber string) error {
    exists, err := s.CarExists(ctx, carNumber)
    if err != nil {
        return err
    }
    if !exists {
        return fmt.Errorf("the asset %s does not exist", carNumber)
    }
 
    return ctx.GetStub().DelState(carNumber)
}


// CarExists returns true when asset with given ID exists in world state
func (s *SmartContract) CarExists(ctx contractapi.TransactionContextInterface, carNumber string) (bool, error) {
    assetJSON, err := ctx.GetStub().GetState(carNumber)
    if err != nil {
        return false, fmt.Errorf("failed to read from world state: %v", err)
    }

    return assetJSON != nil, nil
}

save and exit

2. Update the chaincode

2.1 Create chain code package

Because the fabcar in the official example is running the test-network network, so go to the test-network network to operate.

First configure environment variables

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

export FABRIC_CFG_PATH=$PWD/../config/

Create chaincode package

peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_2

Note that creating a chaincode package label here can be modified according to the version you uploaded

2.2 Install the chaincode package on the org1 node

First initialize the member org1

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

Install the chaincode package on org1

peer lifecycle chaincode install fabcar.tar.gz

result:

If you use the chain code written in java, it is very likely that you cannot configure the environment

In this way, we need to check the docker container and log the container ID of javaenv

docker ps 
docker logs -f javaenv的那个容器id

2.3 Install the chaincode package on the org2 node

Repeat the operation of 2.2

Member org2:

export PATH=${PWD}/../bin:$PATH
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051

Node installation on org2

peer lifecycle chaincode install fabcar.tar.gz

result:

Of course, you can also check whether the package is successfully installed on the node by querying the package id

peer lifecycle chaincode queryinstalled

result:

 

2.4 Approve the chaincode

Here you need to modify its version and serial number .
The following operations must be performed in the virtual machines of Org1 and Org2 . The ID of the chaincode should be replaced in the following command according to the result of the above query.

export CC_PACKAGE_ID=fabcar_2:ed7196cb77fa3fc8c927a825bf03a83cde6addf785a03b030e7ae7c71c36f346

export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile "$ORDERER_CA" --channelID mychannel --name fabcar --version 2.0 --package-id $CC_PACKAGE_ID --sequence 2

result:

2.5 Check whether the chain code is ready

This corresponds to the version number and serial number of the approved chaincode above

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 2.0  --sequence 2 --tls true --cafile "$ORDERER_CA" --output json

result:

2.6 Submit the chain code (org1 or org2 submit once)

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 2.0 --sequence 2 --tls true --cafile "$ORDERER_CA" --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 operation of upgrading the chain code contract is completed! 

 

Guess you like

Origin blog.csdn.net/Wannabe_hacker/article/details/124298759