Bluemix Group

In previous blogs –

  1. http://ibm.biz/setupbc , we created Blockchain (Hyperlegder fabric) dev environment on Ubuntu 16.04.1 LTS.
  2. http://ibm.biz/securebc , we configured security, privacy & logging levels.

Next in the series, we’ll work with chaincode. Chaincode is usually Go (GoLang) code (alternatively Java code) , deployed into a network of Hyperledger fabric peer knots that enables interaction with that network's collective ledger. Whenever you 'invoke' a transaction on the network, you are calling a function in chaincode that reads and writes values to the ledger. In this blog, we'll keep Java chaincode out of scope and concentrate only on Go based code. For more details, please visit – https://github.com/IBM-Blockchain/learn-chaincode

  • If you are fresh to Blockchain, please visit – https://www.ibm.com/blockchain/what-is-blockchain.html and https://www.hyperledger.org/.
  • If you are a developer and looking for a systematic free online self-learning course, please visit – https://developer.ibm.com/courses/all-courses/blockchain-for-developers/
  • If you want to use blockchain without installing & managing it, IBM Bluemix platform as a service (PaaS) provides convenient ways to test an IBM Blockchain network on the cloud – https://www.ibm.com/blockchain/bluemix.html .
  • IBM Bluemix garages for blockchain help businesses accelerate the design and development of blockchain applications, For more info – https://www.ibm.com/blockchain/garage.html.

Let’s get began. As you can see from below snapshot, I am logging into the same machine using putty which we used in previous blog (http://ibm.biz/securebc).

You can test if everything is running and their respective ports –

We’ll use a chaincode example (chaincode_example02.go) for banking screenplay. You can download it from https://github.com/hyperledger/fabric/blob/v0.6/examples/chaincode/go/chaincode_example02/chaincode_example02.go. It’s written in GO language and you must be familiar with Go in order to write the similar code. Please refer https://golang.org/ for more info about Go.

Let’s open and understand code given in chaincode_example02.go file. The import statement lists a few dependencies that you would need in order to compile/build your chaincode successfully.

  • errors – standard go error format.
  • fmt – contains Println for debugging/logging.
  • strconv – implements conversions to and from string representations of basic data types.
  • github.com/hyperledger/fabric/core/chaincode/shim – the code that interfaces your golang code with a peer.

We must implement the chaincode shim interface ("github.com/hyperledger/fabric/core/chaincode/shim") in our chaincode. To do that we must write three core functions (Init, Invoke & Query). All three functions have the same prototype. They take in a function name and an array of strings. The main difference inbetween the functions is when they will be called. Let’s have a closer look at these functions –

  1. Init – Init is called when you very first deploy your chaincode. As the name implies, this function should be used to do any initialization your chaincode needs. In our example, we use Init to configure the initial state of variables on the ledger. As you can see below, our Init implementation accepts four parameters as input and writes validated values on ledger by using stub.PutState (<key>,<value>) function.
    • 1) Very first account name, lets say ‘a’
    • Two) Initial amount (integer) in very first account, lets say 1000
    • Three) 2nd account name, lets say ‘b’
    • Four) Initial amount (integer) in 2nd account, lets say 2000
  1. Invoke – Invoke is called when you want to call chaincode functions to do real transaction work. Invocation transactions will be captured as blocks on the chain. In this example, Invoke accepts three parameters as input and transfers amount from very first parameter to 2nd parameter (account). Note that few lines of code are hidden to demonstrate only relevant code.
    • 1) Very first account name, lets say ‘a’
    • Two) 2nd account name, lets say ‘b’
    • Three) Amount (integer) to be deducted from ‘a’ and deposited in ‘b’
  1. Query . As the name implies, Query is called whenever you query your chaincode state. You will use Query to read the value of your chaincode state's key/value pairs. In this example, Query takes one parameter, account as input and shows the balance in that account.

Now that we have our code ready, let’s build (compile) the chaincode_example02 code. It is also provided in the Hyperledger fabric source code repository.

As we are dealing with secured blockchain environment, we’ll use the Registrar APIs (http://hyperledger-fabric.readthedocs.io/en/latest/API/CoreAPI/#registrar) to manage end user registration with the CA. Below screen shows how we register ‘WebAppAdmin’ user with a POST rest API call. However please note 'WebAppAdmin' is just a sample and not a good choice for a secureContext. Your secureContext should be complicated and hard to guess, like a password, since it secures your transactions after you have enrolled. Please refer our previous blog (http://ibm.biz/securebc) to learn about securing blockchain setup and getting the list of default users & credentials.

Prepare the JSON as shown in the snapshot and make the POST call. You can any Rest client from browser like I am using RESTClient from Mozilla Firefox.

Have a look at the log for this rest service call. Make sure there is no error.

Write, deploy, invoke and query Chaincode on Blockchain (Hyperlegder) network – Bluemix Group Blog – Bluemix Group

Bluemix Group

In previous blogs –

  1. http://ibm.biz/setupbc , we created Blockchain (Hyperlegder fabric) dev environment on Ubuntu 16.04.1 LTS.
  2. http://ibm.biz/securebc , we configured security, privacy & logging levels.

Next in the series, we’ll work with chaincode. Chaincode is usually Go (GoLang) code (alternatively Java code) , deployed into a network of Hyperledger fabric peer knots that enables interaction with that network's collective ledger. Whenever you 'invoke' a transaction on the network, you are calling a function in chaincode that reads and writes values to the ledger. In this blog, we'll keep Java chaincode out of scope and concentrate only on Go based code. For more details, please visit – https://github.com/IBM-Blockchain/learn-chaincode

  • If you are fresh to Blockchain, please visit – https://www.ibm.com/blockchain/what-is-blockchain.html and https://www.hyperledger.org/.
  • If you are a developer and looking for a systematic free online self-learning course, please visit – https://developer.ibm.com/courses/all-courses/blockchain-for-developers/
  • If you want to use blockchain without installing & managing it, IBM Bluemix platform as a service (PaaS) provides convenient ways to test an IBM Blockchain network on the cloud – https://www.ibm.com/blockchain/bluemix.html .
  • IBM Bluemix garages for blockchain help businesses accelerate the design and development of blockchain applications, For more info – https://www.ibm.com/blockchain/garage.html.

Let’s get embarked. As you can see from below snapshot, I am logging into the same machine using putty which we used in previous blog (http://ibm.biz/securebc).

You can test if everything is running and their respective ports –

We’ll use a chaincode example (chaincode_example02.go) for banking script. You can download it from https://github.com/hyperledger/fabric/blob/v0.6/examples/chaincode/go/chaincode_example02/chaincode_example02.go. It’s written in GO language and you must be familiar with Go in order to write the similar code. Please refer https://golang.org/ for more info about Go.

Let’s open and understand code given in chaincode_example02.go file. The import statement lists a few dependencies that you would need in order to compile/build your chaincode successfully.

  • errors – standard go error format.
  • fmt – contains Println for debugging/logging.
  • strconv – implements conversions to and from string representations of basic data types.
  • github.com/hyperledger/fabric/core/chaincode/shim – the code that interfaces your golang code with a peer.

We must implement the chaincode shim interface ("github.com/hyperledger/fabric/core/chaincode/shim") in our chaincode. To do that we must write three core functions (Init, Invoke & Query). All three functions have the same prototype. They take in a function name and an array of strings. The main difference inbetween the functions is when they will be called. Let’s have a closer look at these functions –

  1. Init – Init is called when you very first deploy your chaincode. As the name implies, this function should be used to do any initialization your chaincode needs. In our example, we use Init to configure the initial state of variables on the ledger. As you can see below, our Init implementation accepts four parameters as input and writes validated values on ledger by using stub.PutState (<key>,<value>) function.
    • 1) Very first account name, lets say ‘a’
    • Two) Initial amount (integer) in very first account, lets say 1000
    • Trio) 2nd account name, lets say ‘b’
    • Four) Initial amount (integer) in 2nd account, lets say 2000
  1. Invoke – Invoke is called when you want to call chaincode functions to do real transaction work. Invocation transactions will be captured as blocks on the chain. In this example, Invoke accepts three parameters as input and transfers amount from very first parameter to 2nd parameter (account). Note that few lines of code are hidden to showcase only relevant code.
    • 1) Very first account name, lets say ‘a’
    • Two) 2nd account name, lets say ‘b’
    • Trio) Amount (integer) to be deducted from ‘a’ and deposited in ‘b’
  1. Query . As the name implies, Query is called whenever you query your chaincode state. You will use Query to read the value of your chaincode state's key/value pairs. In this example, Query takes one parameter, account as input and shows the balance in that account.

Now that we have our code ready, let’s build (compile) the chaincode_example02 code. It is also provided in the Hyperledger fabric source code repository.

As we are dealing with secured blockchain environment, we’ll use the Registrar APIs (http://hyperledger-fabric.readthedocs.io/en/latest/API/CoreAPI/#registrar) to manage end user registration with the CA. Below screen shows how we register ‘WebAppAdmin’ user with a POST rest API call. However please note 'WebAppAdmin' is just a sample and not a good choice for a secureContext. Your secureContext should be sophisticated and hard to guess, like a password, since it secures your transactions after you have enrolled. Please refer our previous blog (http://ibm.biz/securebc) to learn about securing blockchain setup and getting the list of default users & credentials.

Prepare the JSON as shown in the snapshot and make the POST call. You can any Rest client from browser like I am using RESTClient from Mozilla Firefox.

Have a look at the log for this rest service call. Make sure there is no error.

Related video:

admin_en | 1@1.com

Related Posts

How to invest in Bitcoin decently. Blockchain and other cryptocurrencies People keep asking me how to decently invest into the crypto-economy. What do I need to know? How not to lose money? How should I choose the right cryptocurrency for my portfolio, which will skyrocket in the future? In this guide you will find an […]

The Fresh York Times June 29, 2016 A delegation of American executives flew to Beijing in April for a secret meeting just blocks from Tiananmen Square. They had come to court the fresh kingmakers in one of the strangest experiments in money the world has seen: the virtual currency known as Bitcoin. Against long odds, […]

How Blockchain Can Bring Financial Services to the Poor by Elizabeth Woyke April Legitimate, 2017 Two billion people worldwide don’t have bank accounts and must conduct their transactions in cash―which can be difficult to manage and presents safety issues. Could blockchain, the technology underlying the digital currency Bitcoin, give them access to financial services?

Leave a Reply

Your email address will not be published. Required fields are marked *