简体   繁体   中英

How to update the chaincode in Hyperledger Fabric?

I'm have been having some troubles updating my Hyperledger chaincode and even if I comment a function out, it is still possible to run this function. I'm using the Build Your First Network sample network from this tutorial together with the marble chaincode as also mentioned in the tutorial. I have no troubles using the original chaincode, but when I start making changes, it is not always these is changes is shown, when I re-deploy my network.

My question is similar to this question but even following the answer in this question does not help me.

When I start up my network I use the following commands:

cd .../fabric-samples/first-network

Change directory to the first-network within the fabric-samples folder.

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -a -q)

This is done in order first stop all containers, remove all containers and remove all images. This does not remove the Hyperledger Fabric images, but only all the chaincodes. This is so I ensure no old chaincode is still active. Even if I check with the docker images it is only Hyperledger images.

./byfn.sh down
./byfn.sh up -c mychannel -s couchdb
docker exec -it cli bash

Here I shutting down the existing Build Your First Network (byfn). After I deploy the network again with the channel mychannel and the couchdb as the couchdb as the state database. Lastly entering the docker CLI container.

peer chaincode install -n marblestest -v 3.1 -p github.com/chaincode/marblestest/go
export CHANNEL_NAME=mychannel
peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n marblestest -v 3.1 -c '{"Args":["init"]}' -P "OR ('Org0MSP.peer','Org1MSP.peer')"

As you can see I have tried using different versions and is not operating with version 3.1. The chaincode is installed, the channel name is set to mychannel and the chaincode is instantiated without any troubles.

peer chaincode invoke -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n marblestest -c '{"Args":["initMarble","marble1","blue","35","tom"]}'

An marble is created through the initMarble.

peer chaincode query -C $CHANNEL_NAME -n marblestest -c '{"Args":["queryMarbles", "{\"selector\":{\"docType\":\"marble\",\"owner\":\"tom\"}, \"use_index\":[\"_design/indexOwnerDoc\", \"indexOwner\"]}"]}'

Here using the modified query function, which works just as the original function and printing the created marble record.

exit

Lastly exiting the program before it can be ran again.

The modifications I have made to the query function is I have commented out the two printing lines and added a counter to count the number of records from the for loop:

func getQueryResultForQueryString(stub shim.ChaincodeStubInterface, queryString string) ([]byte, error) {

    //fmt.Printf("- getQueryResultForQueryString queryString:\n%s\n", queryString)

    resultsIterator, err := stub.GetQueryResult(queryString)
    if err != nil {
        return nil, err
    }
    defer resultsIterator.Close()

    buffer, counter, err := constructQueryResponseFromIterator(resultsIterator)
    if err != nil {
        return nil, err
    }

    //fmt.Printf("- getQueryResultForQueryString queryResult:\n%s\n", buffer.String())
    fmt.Printf("counter =", counter)
    return buffer.Bytes(), nil
}

I would have expected a print with counter = 1 , but I got the original print {"color":"blue","docType":"marble","name":"marble1","owner":"tom","size":35}

Do anybody know why my chaincode is not being updating when I redeploy it?

For executing your changes you must:

  1. Install the new chaincode version on the required endorsers.
  2. Upgrade the chaincode by invoking the peer chaincode upgrade command.

Below the two command examples:

  1. peer chaincode install -n mycc -v 1.1 -p your_path
  2. peer chaincode upgrade -o orderer.example.com:7050 -C mychannel -n mycc -v 1.1 -c '{"Args":["init","a","100","b","200","c","300"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"

Official HL Fabric documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM