Deploy and invoke the chain code

Here is out main method which will be starting the chaincode when we deploy it to fabric runtime on peer nodes

main.go

func main() {
   voteContract := new(vote.VoteContract)
   chaincode, err := contractapi.NewChaincode(voteContract)
   if err != nil {
      panic(err)
   }

   err = chaincode.Start()
   if err != nil {
      panic(err.Error())
   }
}

Waltkthrough of deployment script

To deploy a chaincode is no trivial task :). It has to go through a set of peer lifecycle stages before it can run on peer nodes . This is intentional so that all parties (investorOrg and managementOrg in this case) approve the definition of chaincode. Lets walk through the script used for deploying which does that voter-net chaincode(voternet-deployChainCode.sh) .

Step 1 - First we compile and package the golang code into a tar.gz file . Apart from name of chain code we have to give version and sequence of chaincode , which we have to increment we edit and redeploy the chaincode again. version is a string we use for our tacking purposes (eg 1.0 or 1.0.1) , sequence is an int used by fabric to track how many times it has been redefined

Step 2 - install chain code on both peers ( to do so we need to set the right env variables - look at setInvestorVars and setManagementVars methods)

Step 3 - approve the chaincode with approveformyorg sub-command . However we need to get the package id for the installed chaincode , which we get by queryinstalled sub command .

Step 4 - check if the chaincode has been approved by all orgs as per the policy in the channel (in this case both orgs need to approve it before it can be activated)

Step 5 - Finally send the commit transaction to the order node (abbreviated explanation - it goes through the whole transaction flow actually).(for fabric, chaincode installation is also a transaction) This finishes the "installation" process .

Also verify if the commit succeeded

Step 6 - Finally we invoke a dummy chaincode method to see if the chaincode is accessible . This of it like a liveliness test for microservices.

Lets now invoke the deploy script

As usual we run the script from inside the setup directory

If chain code is deployed successfully , it will print the repsonse from LiveTest function

Also please check you docker containers now . You can see a docker container for the chaincode for each peer starting with dev-peer0*.For each chaincode deployed the fabric peers creates a docker container which execute chaincode contracts

Invoking the chaincode contract

Execute the above command to cast a vote for user . Lets now deconstruct the command

There are couple of env vars need for the peer invoke subcommand to work , (Worth taking a look at the tls configuration document here.

  • CORE_PEER_TLS_ENABLED - Indicate peer node needs tls for connection

  • CORE_PEER_TLS_ROOTCERT_FILE - path of tls cert for peer

  • CORE_PEER_MSPCONFIGPATH - path of msp folder - (note we are pointing to User1 , but any other user like admin will also work)

  • ORDERER_CA - path of cert for orderer

  • FABRIC_CFG_PATH - default config directory for running peer cli command

Now lets look at the peer chaincode invoke subcommand arguments

  • -o - Ordering service endpoint

  • ordererTLSHostnameOverride -The hostname override to use when validating the TLS connection to the orderer

  • cafile - cert for orderer node

  • C - the channel id

  • n - chaincode id

  • peerAddresses - address of peer node (note we supply both the peer nodes since our chaincode needs to be endorsed by both orgs)

  • tlsRootCertFiles - certfiles for peers

  • c - the command string in json format - (note how we supply the function name and arguments

if the invocation is successful you can see an output like this : If you see an error ( use monitordocker.sh in another terminal to get the peer logs and triage as explained earlier)

if you try the same command again , you can see an error this time as expected

Error: endorsement failure during invoke. response: status:500 message:"voter already voted"

Try and invoke the GetVoteCountForCand contract to get the vote count for the candidateid

Last updated

Was this helpful?