title: How to update the chaincode in Hyperledger Fabric?
body: <p>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 <a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html" rel="nofollow noreferrer">this tutorial</a> 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. </p>
  
  <p>My question is similar to <a href="https://stackoverflow.com/questions/49855723/hyperledger-fabric-chaincode-not-updated">this question</a> but even following the answer in this question does not help me.</p>
  
  <p>When I start up my network I use the following commands:</p>
  
  <pre><code>cd .../fabric-samples/first-network
  </code></pre>
  
  <p>Change directory to the first-network within the fabric-samples folder.</p>
  
  <pre><code>docker stop $(docker ps -a -q)
  docker rm $(docker ps -a -q)
  docker rmi $(docker images -a -q)
  </code></pre>
  
  <p>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 <code>docker images</code> it is only Hyperledger images.</p>
  
  <pre><code>./byfn.sh down
  ./byfn.sh up -c mychannel -s couchdb
  docker exec -it cli bash
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>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')"
  </code></pre>
  
  <p>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. </p>
  
  <pre><code>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"]}'
  </code></pre>
  
  <p>An marble is created through the initMarble.</p>
  
  <pre><code>peer chaincode query -C $CHANNEL_NAME -n marblestest -c '{"Args":["queryMarbles", "{\"selector\":{\"docType\":\"marble\",\"owner\":\"tom\"}, \"use_index\":[\"_design/indexOwnerDoc\", \"indexOwner\"]}"]}'
  </code></pre>
  
  <p>Here using the modified query function, which works just as the original function and printing the created marble record. </p>
  
  <pre><code>exit
  </code></pre>
  
  <p>Lastly exiting the program before it can be ran again. </p>
  
  <p>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:</p>
  
  <pre><code>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
  }
  
  </code></pre>
  
  <p>I would have expected a print with <code>counter = 1</code>, but I got the original print <code>{"color":"blue","docType":"marble","name":"marble1","owner":"tom","size":35}</code></p>
  
  <p>Do anybody know why my chaincode is not being updating when I redeploy it?</p>