In a previous article, Hyperledger Fabric on SAP Cloud Platform, my colleague Aviva gave you an overview of Hyperledger based on blockchain technology. Fabric is one of five parallel projects in the Super Ledger, and because of its maturity, Fabric is well supported by the SAP cloud platform.

After learning the theory of the previous article, let’s put it into practice today.

The scope of this exercise is very narrow. It is to learn how to use GO to develop a set of microservices that contain both read and write apis that can write data to a super ledger service on the SAP cloud platform.

Remember Aviva’s Smart Contract?

Simply put, the application reads and writes to the superledger through the smart contract interface. We will develop a Hello World smart contract and deploy it on the SAP cloud platform. For simplicity, instead of developing an app, we simply consumed the Hello World smart contract directly on the API console of the SAP cloud platform, reading and writing the super ledger on the cloud platform.

Open the Github repository address for the Super ledger project Fabric:

Github.com/hyperledger…

It turns out that the Fabric project is developed by Google’s programming language GoLang, so this exercise also uses Go for smart contract development.

1. Download the binary package of Go 1.11 from Google and decompress it to /usr/local:

Sudo tar – C/usr/local – XZF/home/vagrant/Downloads/go1.11 Linux – amd64. Tar. Gz

Configure this directory to the environment variable PATH:

2. The Fabric project has encapsulated the communication between the smart contract and the hyperledger into an interface called shiM, which we simply call directly from the smart contract code we wrote.

We introduce this shiM interface dependency with import, defining a structure in line 14 that contains the ID and Value fields. This structure is the data structure to be written to the hyperledger, and the ABAP consultant can treat it as a structure defined in the ABAP data dictionary.

The Invoke method defined in line 46 is the core code of the simplest smart contract. Cc *MessageStore, which has a syntax similar to C, defines a pointer variable cc of type MessageStore. This pointer is similar to the C++ this pointer and ABAP’s me reference, pointing to the method caller when the method is called.

Invoke the stub in the parentheses shim. ChaincodeStubInterface defines the method of input parameters (parameters) stub, type of shim. ChaincodeStubInterface.

The Invoke method is not called explicitly by the application, but is called back by the hyperledger program: when the method is called, the pointer CC and the input parameter stub have been automatically assigned corresponding values by the Fabric framework. In the context in which the Invoke method runs, the input parameter stub determines whether the current callback scenario is read or write, and then enters the corresponding branch. The branch internally calls our own write and read methods to interact with the hyperledger. The source code is available on github:

Github.com/i042416/Kno…

The practice of handling multiple scenarios through a switch case inside the same callback function should be familiar to ABAP and Java developers alike. For example, here is an example of a Java dynamic proxy implemented through InvocationHandler, where the logical structure of the Invoke method is very similar to that of the smart contract code in this article.

For more information on static and dynamic proxies in ABAP and Java, see my blog:

Various Proxy Design Pattern implementation variants in Java, ABAP and JavaScript

Blogs.sap.com/2017/04/17/…

3. Build the developed source files of smart contracts into executable files. This step ensures that all potential bugs are detected and fixed in the local development environment before deploying smart contracts to the SAP cloud platform.

4. Log in to the SAP cloud platform and click the Hyperledger Fabric link in the Service Marketplace:

Create a new Service instance:

Enter the ID and key of a channel during creation.

Remember when Aviva mentioned in the last article that blockchains are divided into public, private and federated chains, and that hyperledgers are federated chains? In the alliance chain, there is a special module called Membership Service Provider (MSP) to provide Membership management services, and only authorized users can access the blockchain network. Here, I created a channel on SAP cloud platform in advance and authenticated it, so I directly entered a legitimate channel ID and key here. For details on how to create the super ledger channel on THE SAP cloud platform and how to authorize member access, please refer to the SAP help document:

Help.sap.com/viewer/p/HY…

After the Service instance is created, click the Create Service Key button to Create the Key. The purpose is to generate clientId and clientSecret for OAuth authentication for subsequent API calls.

Take the Referencing Apps panel of the Service instance and click the Open Dashboard:

Click Deploy Chaincode, select the locally built ZIP package, upload and Deploy. This button is the same logic used to deploy native applications in SAP cloud platform Neo and CloudFoundry environments.

Once deployed, click the Test Chaincode hyperlink to go to the API console.

The console integrates Swagger framework. Before invoking POST request for write operation of super ledger and GET request for read operation, click Authorize button for identity authentication:

Enter clientID and clientSecret generated after step 4 to create the Service Key for authentication:

After successful authentication, post and GET requests can be called in Swagger’s console.

A POST request is sent. The payload is a simple JSON object with id i042416 and value Hello World:

The post request was successfully executed on the SAP cloud platform’s super ledger, returning 200 response code:

Then execute the get request and enter the data id: i042416:

The get request successfully reads the data that was previously written to the ledger by the POST request:

Log in to the SAP Cloud Platform super Ledger console, and you can see that the data previously written through POST has been added to the block at the end of the blockchain. Click the block to view the details of the data:

Details of each read and write to the super ledger can also be seen in the API Calls and Logs panels of the Super Ledger console.

To summarize, the SAP Cloud platform’s super ledger service successfully helps businesses that want to use this blockchain technology avoid hardware infrastructure investment, while shielding most of the low-level details of super ledger platform management. Through the console provided by SAP cloud platform, the super ledger can be used for device access, access control, service monitoring and other management functions. At the same time, once smart contracts written in Go are deployed to SAP cloud platform, the generated Restful apis can be easily consumed by other programming languages. Calling these apis to write data into the superledger blockchain can no longer be tampered with. With the SAP Cloud platform’s super Ledger service, application developers can focus on writing application logic without having to spend too much time on the super ledger architecture itself.

In the process of writing this paper, I have received great help from my colleague Aviva. Thank you here.

Read more

  • Hyperledger Fabric on SAP Cloud Platform

  • Three hundred lines of ABAP code implement a simplest blockchain prototype

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: