SAP Cloud Platform, BlockChain, Hyperledger Fabric and Smart Contract
My colleague Wen, Aviva has posted an article in Wechat《Hyperledger Fabric on SAP Cloud Platform》which gives a brief introduction about Hyperledger Fabric and the support of SAP Cloud Platform on it ( Sorry the article is written in Chinese, as its main target are local partners in China ).
Fabric is one of the five projects which are running in parallel. Due to its maturity, SAP Cloud Platform has also provided productive support on it.
After going through the theory on Hyperledger Fabric explained by Aviva, now it’s time for practice.
The scope of our practice today is limited: only learn to develop a set of micro-services, consisting of read and write API, enabling the data to be written into Hyperledger Fabric service instance hosted in SAP Cloud Platform and read out.
Still remember the meaning of Smart Contract ( also named as chaincode ) in Hyperledger Fabric?
Simply speaking, smart contract is used as an interface to interact with Hyperledger by application. In this blog we will develop a “Hello World” smart contract and deploy it to SAP Cloud Platform. For simplicity reason, I don’t plan to develop an application to consume this “Hello World” smart contract. Instead, we can simply consume it in API console provided by SAP Cloud Platform to perform read and write on Hyperledger in SAP Cloud Platform.
Open github url for Hyperledger Fabric, and we can find it is developed by GoLang programming language invented by Google. As a result in this practice we will program with Go as well.
Detail development steps are listed below:
1. Download binary package of Go language and extract the content into /usr/local:
sudo tar -C /usr/local -xzf /home/vagrant/Downloads/go1.11.linux-amd64.tar.gz
Add /usr/local into environment variable PATH:
2. The communication routine code has already been encapsulated into an interface named “shim”. We can call this shim interface in our smart contract.
As commonly used in other language, we use keyword import to introduce the dependency of shim interface, so that we can use it in our implementation. In line 14 we define a structure named “Message” containing two fields ID and Value. Later on, we will write data with the type of this defined structure into Hyperledger soon. ABAPers can simply treat this structure as DDIC structure created in tcode SE11.
In line 46, a method Invoke is defined which contains the core logic of the smart contract to be developed. The grammar “cc *MessageStore” has great similarity with C programming language – a pointer variable cc is declared which points to a data type “MessageStore”.
This cc variable acts as the “this” pointer in C++ and “me” reference in ABAP, pointing to the method caller when a method is executed.
The “stub shim.ChaincodeStubInterface” in the parenthese after Invoke method represents the importing parameter stub with data type shim.ChaincodeStubInterface. In the implementation of Invoke method, we can leverage stub to utilize all its public API.
Our application code will not call this Invoke method explicitly. Instead, it works as a callback triggered by Hyperledger Fabric. When triggered, the cc pointer and importing parameter stub have already been initialized with corresponding value. In Invoke method implementation, we determine what scenario current is, read or write. In each read and write branch, we call read and write function implemented by ourselves to communicate with Hyperledger using shim.
The complete code of this smart contract could be found from my github.
Both ABAPers and Javaers would be familar with the coding style that different branches in a single method are used to handle different scenarios. For example, if we would like to implement dynamic proxy using InvocationHandler in Java, we will program with very similar code with our smart contract in this article. Just compare:
3. perform local build to generate a local executable file. This step is not mandatory, as there is still a mandatory build when you deploy your local smart contract to SAP Cloud Platform. Nevertheless it helps you identify potential errors as early as possible.
4. Log on SAP Cloud Platform, click Hyperledger Fabric hyperlink in Service Marketplace tab:
create a new Service instance：
channel ID and secret are required during service creation.
Still remember the last article by Aviva, that blockchain could be grouped into three categories: public, private and consortium, and Hyperledger Fabric belongs to Consortium chain, where a dedicated module named MSP(Membership Service Provider) is used to provide membership management, only authorized user can gain access to blockchain network. I create a channel in SAP Cloud Platform beforehand. Regarding more detail of this step, please refer to SAP help.
After service instance is created, press button “Create Service Key” to generate clientId and clientSecret needed for OAuth. Write down both for later use.
Press Open Dashboard button:
Press Deploy Chaincode button, select zip package
(containing both smart contract source code and yaml file) built locally and deploy it to SAP Cloud Platform.
This deployment process is exactly the same as what you have done in Neo or CloudFoundry environment.
After deployment is done successfully, press Test Chaincode to enter API console.
This console is integrated with Swagger framework. Before post(write) or get(read) request is sent to Hyperledger, authorization must be gained first by pressing Authorize button.
Use clientID and clientSecret gained in step 4 for authorization:
After authorization is done, now we can test post and get request in console:
First send a post request, the payload is a simple json object with id field as i042416, and value field as “Hello World”：
This post request is successfully handled in SAP Cloud Platform, return a HTTP 200 response code, which means the data is written to Hyperledger now.
And perform get request to read the data written by post request just now. The input for read operation: i042416.
This get request can successfully read out the data:
Open Hyperledger dashboard and we can find the data written by post request has been added into the tail of blockchain. Click the last block to review detail.
In the tab API Calls and Logs, detail information of each read and write on Hyperledger could be found as well.
In summary, Hyberledger Fabric service in SAP Cloud Platform can help those company who would like to leverage blockchain in their business to quickly setup Hyperledger in their company without knowing too much technical details about Hyperledger Fabric.