Hyperledger Fabric smart contract

Victor Yeo
2 min readFeb 19, 2021

Smart contract is a program that runs in the context of blockchain. In Hyperledger Fabric, smart contract is called chaincode. Chaincode can be written in golang, nodejs or typescript. Chaincode runs in docker container isolated from endorsing peers. The web/mobile applications submit chaincode to hyperledger by calling commands such as “peer lifecycle install …”. After that, applications can invoke or query the chaincode. For example, one use case would be to get/update the value of the object in blockchain.

The diagram above shows how an application interacts with hyperledger fabric chaincode. In one such use case, the chaincode would be created in advance and stored in cloud store (AWS S3). The users would select the chaincode from the application UI, and submit the chaincode to the application backend. The backend would then call hyperledger fabric commands to interact (deploy, invoke, query) with the chaincode in the context of hyperledger blockchain.

Any member of hyperledger network can activate the chaincode by submitting instantiation transactions to the network. Any chaincode transactions that are validated are appended to the blockchain. Chaincode usually handles business logic that members of hyperledger network have agreed to.

To get/update value of the object in blockchain, applications use supported SDK to generate transaction proposal. One example of supported SDK is fabric-network (https://www.npmjs.com/package/fabric-network). The proposal is a request to invoke a chaincode function with certain input parameters, with the intent of reading and/or updating the object in blockchain. The SDK will receive a “proposal response” from peers. After proposal response is inspected, the SDK assembles a transaction.

After transaction is validated and committed, these transactions can then modify the blockchain world state accordingly. Hyperledger blockchain world state holds the current value of the attributes of business object as a unique blockchain state. That is useful because programs usually require the current value of an object. It would be cumbersome to traverse the entire hyperledger blockchain to calculate an object’s current value. With the world state, you just get the value directly from the world state.

A ledger world state containing two states. The first state is: key=CAR1 and value=Audi. The second state has a more complex value: key=CAR2 and value={model:BMW, color=red, owner=Jane}. Both states are at version 0.

The world state is implemented as a database. In Hyperledger Fabric, there are options of CouchDB or LevelDB for implementing world state database.

--

--