Fabric Samples made easier with Hurley the Development Environment Manager

Walter Montes
Jan 31 · 5 min read

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.

Install Hurley

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

Start your environment

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.

Installing and testing examples

chaincode_example02 (click to see the code)

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 example02 golang --ctor '{"Args":["Init","walter","100","diego","200"]}'

Check if the user received the initial units:

$ hurl invoke example02 --ctor '{"Args":["query","walter"]}'# If you are working with Hurley v0.5.1
$ hurl invoke example02 query "walter"

Let’s make an invoke to transfer units:

$ hurl invoke example02 --ctor '{"Args":["invoke","walter","diego","99"]}'# If you are working with Hurley v0.5.1
$ hurl invoke example02 invoke "walter" "diego" "99"

Now let’s see if the transfer actually happened.

$ hurl invoke example02 --ctor '{"Args":["query","walter"]}'# If you are working with Hurley >= v0.5.1
$ hurl invoke example02 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 --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"]}'
# If you are working with Hurley v0.5.1
$ hurl invoke example02_node invoke "walter" "diego" "99"
$ hurl invoke example02_node query "walter"

marbels02 (click to see the code)

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.

# If you are working with Hurley >= v0.5.1$ hurl invoke marbles initMarble "marble1" "blue" "35" "tom"$ hurl invoke marbles initMarble "marble2" "red" "50" "tom"$ hurl invoke marbles initMarble "marble3" "blue" "70" "tom"$ hurl invoke marbles transferMarble "marble2" "jerry"$ hurl invoke marbles transferMarblesBasedOnColor "blue" "jerry"$ hurl invoke marbles delete "marble1"$ hurl invoke marbles readMarble "marble1"$ hurl invoke marbles getMarblesByRange "marble1" "marble3"$ hurl invoke marbles getHistoryForMarble "marble1"$ hurl invoke marbles queryMarblesByOwner "tom"$ hurl invoke marbels queryMarbles "{\"selector\":{\"owner\":\"tom\"}}"$ hurl invoke marbles  queryMarblesWithPagination "{\"selector\":{\"owner\":\"tom\"}}" "3" ""# If you are working with Hurley < v0.5.1$ 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",""]}'

sacc

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.

# If you are working with Hurley >= v0.5.1$ hurl invoke sacc set"  "1" "gold"
$ hurl invoke sacc "get" "1"
# If you are working with Hurley < v0.5.1$ hurl invoke sacc --ctor '{"Args":["set", "1", "gold"]}'
$ hurl invoke sacc --ctor '{"Args":["get", "1"]}'

fabcar (click to see the code)

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:

# If you are working with Hurley >= v0.5.1
$ hurl invoke fabcar initLedger
# If you are working with Hurley < v0.5.1
$ hurl invoke fabcar --ctor '{"Args":["initLedger"]}'

Query all the items:

# If you are working with Hurley >= v0.5.1
$ hurl invoke fabcar queryAllCars
# If you are working with Hurley < v0.5.1
$ hurl invoke fabcar --ctor '{"Args":["queryAllCars"]}'

And transfer one car to another person:

# If you are working with Hurley >= v0.5.1
$ hurl invoke fabcar changeCarOwner "CAR0" "Walter"
# If you are working with Hurley < v0.5.1
$ hurl invoke fabcar changeCarOwner "CAR0" "Walter"

And confirm transferral:

# If you are working with Hurley >= v0.5.1
hurl invoke fabcar queryCar "CAR0"
# If you are working with Hurley < v0.5.1
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.


Covalent

A unified development platform to create and deploy enterprise smart contract systems.

Walter Montes

Written by

CEO @ Covalent — https://covalentx.com

Covalent

Covalent

A unified development platform to create and deploy enterprise smart contract systems.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade