Fabric Samples made easier with Hurley the Development Environment Manager

In case you didn’t know there are a lot of great chaincode examples here from the very own Hyperledger Fabric team. These help us understand better how to build correct Fabric code and also get tips and tricks that the docs may not include.

There are really simple and basic examples like fabcar and a bit more complex like the commercial-paper.

Something that is not great is how all of these examples manage the development environment differently. Maybe because these were created by different teams or in different points in time. It may be confusing.

A development environment is usually something that is better if it is mutation-resistant. So immutable setups like the one that Hurley provides is a simple and straight forward way to deal with it without much hustle.

Hurley is part of the Convector Suite but independent from Convector Smart Contracts. Precisely in this example, I will show how to use it with non-Convector smart contracts.

You want to start today and just run hurl new to create your dev env and go ahead and focus on the code. You don’t really want to debug or read how to install, instantiate, and upgrade each sample you’d like to use as inspiration.

I want to show you how easy you can start with Hurley here.

These are all the chaincode samples in the fabric-samples repo.

There are go and node chaincodes, but most of them are really simple and easy to read. Though, maybe not as great as I think that Convector Smart Contracts are 😁.

So let’s get to it. We will need first to start our dev env, and then install the chaincode, and make a few invoke tasks.

npm i -g @worldsibu/hurley

You will also need NPM and Docker CE.

If you had a previous version of Hurley, upgrade first.

npm upgrade -g @worldsibu/hurley

This will create a development blockchain with 2 organizations and one channel, plus one user per organization.

$ hurl new

More details for more complex scenarios here.

This is a simple example of how to create units accounting. The example doesn’t prevent transferrals that exceed what one user has, but it’s an easy way to understand go easily.

First get to the right folder inside the fabric-samples folder.

# From the fabric-samples/chaincode folder
$ cd chaincode_example02/go/

We need to first install the chaincode. It requires 4 properties, 2 users and their units, passed as params:

$ hurl install abac1 golang --ctor '{"Args":["Init","walter","100","diego","200"]}'

Check if the user received the initial units:

$ hurl invoke example02 --ctor '{"Args":["query","walter"]}'

Let’s make an invoke to transfer units:

$ hurl invoke example02 --ctor '{"Args":["invoke","walter","diego","99"]}'

Now let’s see if the transfer actually happened.

$ hurl invoke example02 --ctor '{"Args":["query","walter"]}'

That’s it! 🙂 As you can see it’s really simple to install and invoke your chaincode.

If you’d like to test the node version of the code, go back to the root folder of this example cd .. and run:

$ hurl install example02_node node --ctor '{"Args":["Init","walter","100","diego","200"]}'

And you can repeat previous steps but referencing this new chaincode.

$ hurl invoke example02_node --ctor '{"Args":["invoke","walter","diego","99"]}'
$ hurl invoke example02_node --ctor '{"Args":["query","walter"]}'

This is a more complex example, and you can still do all sorts of things with Hurley to interact with this smart contract.

First, install it.

# From the fabric-samples/chaincode folder
cd marbles02/go/
$ hurl install marbles golang

Here are a lot of invokes you can try, from simple transactions to simple and complex queries.

$ hurl invoke marbles -c '{"Args":["initMarble","marble1","blue","35","tom"]}'
$ hurl invoke marbles -c '{"Args":["initMarble","marble2","red","50","tom"]}'
$ hurl invoke marbles -c '{"Args":["initMarble","marble3","blue","70","tom"]}'
$ hurl invoke marbles -c '{"Args":["transferMarble","marble2","jerry"]}'
$ hurl invoke marbles -c '{"Args":["transferMarblesBasedOnColor","blue","jerry"]}'
$ hurl invoke marbles -c '{"Args":["delete","marble1"]}'
$ hurl invoke marbles -c '{"Args":["readMarble","marble1"]}'
$ hurl invoke marbles -c '{"Args":["getMarblesByRange","marble1","marble3"]}'
$ hurl invoke marbles -c '{"Args":["getHistoryForMarble","marble1"]}'
$ hurl invoke marbles -c '{"Args":["queryMarblesByOwner","tom"]}'
$ hurl invoke marbels -c '{"Args":["queryMarbles","{\"selector\":{\"owner\":\"tom\"}}"]}'
$ hurl invoke marbles -c '{"Args":["queryMarblesWithPagination","{\"selector\":{\"owner\":\"tom\"}}","3",""]}'

Another example is the SimpleAsset chaincode.

Go to the chaincode folder and install the chaincode.

# From the fabric-samples/chaincode folder
$ cd saac
$ hurl install sacc golang

Then you can add and query assets.

$ hurl invoke sacc --ctor '{"Args":["set", "1", "gold"]}'
$ hurl invoke sacc --ctor '{"Args":["get", "1"]}'

Last but not least, the fabcar example I’ve seen really used by devs.

# From the fabric-samples/chaincode folder
cd fabcar/go
$ hurl install fabcar golang

It has a self-generation function to create some mock data:

$ hurl invoke fabcar --ctor '{"Args":["initLedger"]}'

Query all the items:

$ hurl invoke fabcar --ctor '{"Args":["queryAllCars"]}'

And transfer one car to another person:

$ hurl invoke fabcar --ctor '{"Args":["changeCarOwner","CAR0", "Walter"]}'

And confirm transferral:

hurl invoke fabcar --ctor '{"Args":["queryCar","CAR0" ]}'

I hope this post has helped you and to see you around in the Convector Suite community on Discord and Twitter.