Deep-Dive into FabCar: A Complete Application Example on Hyperledger Fabric (Part 2)

5. Understanding FabCar Chaincode

Now it is the right time to take a look the FabCar chaincode. As a recap, chaincode is executed inside the Fabric network (on endorsing peer). It is where the business logic, agreed by all participants, resides and will be executed when conditions are met. In production, the ledger is only updated when chaincode is invoked.

It is where the Chaincode resides.

Data Structure

As FabCar is a “database” in the ledger, here is how the data is structured.

Init()

This is a specific function required in Hyperledger Fabric chaincode. The Init() is executed when the chaincode is instantiated in the Fabric network.

Invoke()

This is another specific function required in Hyperledger Fabric chaincode. The Invoke() usually defines some further actions (functions) when the chaincode is invoked. It usually follows the pattern like this.

  • queryCar
  • initLedger
  • createCar
  • queryAllCars
  • changeCarOwner

6. Demonstration of Chaincode Functions using CLI

Here we begin to demonstrate how the chaincode is working, or more specifically, what happens when the chaincode is queried or invoked.

  1. Query All Cars in the ledger
  2. Query the specific car by CarID
  3. Add a new car with detail provided
  4. Query All Cars in the ledger again and we see a new car is in the ledger
  5. Change the owner of a car to someone else
  6. Query that car again and see the change is done
Make sure the nodes and chaincode container are running
$ docker exec cli peer chaincode invoke -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
$ docker exec cli peer chaincode invoke -C mychannel -n fabcar -c '{"Args":["queryCar","CAR4"]}'
$ docker exec cli peer chaincode invoke -C mychannel -n fabcar -c '{"Args":["createCar", "CAR12", "Honda", "Accord", "black", "Tom"]}'
$ docker exec cli peer chaincode invoke -C mychannel -n fabcar -c ‘{“Args”:[“queryAllCars”]}’
$ docker exec cli peer chaincode invoke -C mychannel -n fabcar -c ‘{“Args”:[“changeCarOwner”, “CAR4”, “KC”]}’
$ docker exec cli peer chaincode invoke -C mychannel -n fabcar -c ‘{“Args”:[“queryCar”,”CAR4"]}’

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store