In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In my previous article Hyperledger Fabric on SAP Cloud Platform, my colleague Aviva gave you an overview of blockchain technology based Hyperledger. Fabric is one of the five parallel projects of Hyperledger, and because it is mature, SAP Cloud Platform also provides good support for Fabric.
After learning the theoretical knowledge of the previous article, today we will practice it.
The exercise we did was narrow in scope and was to learn how to use go to develop a set of microservices that included read and write APIs that could write data to a hyperledger service hosted on SAP Cloud.
Do you remember Aviva's Smart Contract?
Simply put, applications read and write to hyperledgers through smart contract interfaces. We will develop a Hello World smart contract and deploy it to SAP Cloud Platform. For simplicity's sake, instead of developing an app, we simply consume this Hello World smart contract directly on the SAP cloud platform API console, reading and writing to the hyperledger on the cloud platform.
Open the github repository address of the Hyperledger project Fabric:
https://github.com/hyperledger/fabric
The Discovery Fabric project was developed in Google's programming language GoLang, so this exercise also uses Go for smart contract development.
1. Download Go version 1.11 binaries locally from Google and extract them to/usr/local:
sudo tar -C /usr/local -xzf /home/vagrant/Downloads/go1.11.linux-amd64.tar.gz
Configure this directory in the PATH environment variable:
2. The Fabric project has encapsulated the communication between smart contracts and hyperledgers into an interface called shim, which we just need to call directly in the smart contract code we write.
We introduce this shim interface dependency using import, defining a struct at line 14 that contains two fields, ID and Value. This structure is the data structure to be written to the hyperledger, and ABAP consultants can think of it as a structure defined in the ABAP data dictionary.
The method Invoke defined in line 46 is the core code of this simplest smart contract. cc MessageStore is similar to C language syntax and defines a pointer variable cc of type MessageStore. This pointer variable acts like the C++ this pointer and ABAP me reference *, pointing to the caller of the method when it is invoked.
The stub shim.ChaincodeStubInterface in parentheses after Invoke defines the input parameter stub of the method, which is of type shim.ChaincodeStubInterface.
This Invoke method is not invoked explicitly by the application, but rather by the hyperledger program callback: when the method is invoked, the pointer cc and the input parameter stub are automatically assigned values by the Fabric framework. In the context in which the Invoke method runs, determine whether the scene being called back is read or write by entering the parameter stub, and then enter the corresponding branch. The branch internally calls our own write and read methods to interact with the hyperledger. The source code is on my github:
https://github.com/i042416/KnowlegeRepository/blob/master/ABAP/blockchain/message_store_chaincode.go
ABAP and Java developers should be familiar with this approach of handling multiple scenarios through switch cases within the same callback function. For example, the following figure is an example of implementing Java dynamic agents through InvocationHandler, where the logical structure of the invoke method is very similar to the structure of the smart contract code in this article.
For ABAP and Java static and dynamic proxy writing, please refer to my blog:
Various Proxy Design Pattern implementation variants in Java, ABAP and JavaScript
https://blogs.sap.com/2017/04/17/various-proxy-design-pattern-implementation-variants-in-java-and-abap/
3. Build the developed smart contract source file into an executable file. This step ensures that all potential errors are detected and fixed in the local development environment before deploying smart contracts to the SAP cloud platform.
4. Log in to SAP Cloud Platform and click Hyperledger Fabric hyperlink in Service Marketplace:
Create a new instance of Service:
During the creation process, you need to fill in the ID and key of the channel.
Remember the previous article Aviva mentioned that blockchain is divided into public chain, private chain and federated chain, and hyperledger belongs to federated chain? In the alliance chain, there is a special module called MSP(Membership Service Provider), which provides member management services. Only authorized users can access the blockchain network. Here I created and authenticated a channel on SAP Cloud beforehand, so I entered a legitimate channel ID and key directly here. For steps to create a Hyperledger channel on SAP Cloud Platform and access member authorization, please refer to SAP Help documentation:
https://help.sap.com/viewer/p/HYPERLEDGER_FABRIC
After the Service instance is created, click the Create Service Key button to create a key, in order to generate clientId and clientSecret for OAuth authentication, which is convenient for the next API call.
Click on the Referencing Apps panel of the Service instance and click on the button Open Dashboard:
Click Deploy Chaincode, select the locally built zip package, upload and deploy it. This button has the same logic for deploying on-premises apps in SAP Cloud Platform Neo and CloudFoundry environments.
After successful deployment, click the Test Chaincode hyperlink to enter the API console.
The console integrates Swagger framework. Before calling post request for writing operation and get request for reading operation of hyperledger, click Authorize button for identity authentication:
Enter the clientID and clientSecret generated after creating the Service Key in Step 4 for authentication:
After authentication, you can call post and get requests in Swagger's console.
First send a post request, the request payload is a simple json object, id is i042416, value is Hello World:
The post request is successfully executed on the SAP cloud platform, and the response code 200 is returned:
Immediately after executing the get request, enter the data id: i042416:
The get request can successfully read data previously written to the ledger through the post request:
Log in to SAP Cloud Platform Hyperledger 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 on the block to view the data breakdown:
Details of each read and write to the Hyperledger can also be seen in the API Calls and Logs panels of the Hyperledger console.
To summarize, SAP Cloud's Hyperledger service has successfully helped businesses wishing to use blockchain technology avoid hardware infrastructure investment while shielding most of the underlying details of Hyperledger platform management. Through the console provided by SAP cloud platform, you can realize device access, access control, service monitoring and other management functions for hyperledger. At the same time, smart contracts written in Go, once deployed to the SAP cloud platform, generate Restful APIs that can be easily consumed by other programming languages. Data written to the Hyperledger blockchain by calling these APIs cannot be tampered with anymore. Using SAP Cloud Platform's Hyperledger service, application developers can focus on writing application logic without spending too much effort on the Hyperledger architecture itself.
During the writing process of this article, I received great help from my colleague Aviva, and I would like to thank him here.
more reading
Hyperledger Fabric on SAP Cloud Platform
300 lines of ABAP code implement one of the simplest blockchain prototypes
To get more Jerry's original articles, please pay attention to the public account "Wang Zixi":
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.