Java and Blockchain – how to combine these two in business application?

Java and Blockchain – how to combine these two in business application?

The following article is the continuation of the topic related to blockchain technology which we started from presenting basic concepts of the blockchain itself. Moving on, I would like to show how to develop an application using blockchain technology in Java, which solves the real business issue.

 

Business issue

Your company expands, more and more people work there. So far, many different requests (for leave, for new hardware or changing already existing one) have been handled in writing, which often led to unacceptable abuses and forgeries. Specific parties stopped trusting each other during the process. Last meeting of management ended with taking the decision to change this state, namely digitalization of the processes, in order to increase the control over them and transparency. They decided to develop an application, which handles requests, for office use.

Basic assumptions:

  • each employee may file a request,
  • an employee with a special permission may accept requests,
  • each application must be accepted before it is processed further,
  • requests and decisions must be indisputable and unchangeable,
  • every user sees all requests (full transparency).

Sample test scenario:

  1. Create “standard” users: user1, user2.
  2. Create managers: manager1, manager2.
  3. User1 writes a request for leave.
  4. Manager1 accepts a request of User1 (gives a positive decision).
  5. User2 writes a request for leave.
  6. Manager2 refuses the request of  User2 (gives a negative decision).

Head of IT stated that basic assumptions, namely indisputability, changelessness, and auditability perfectly fit into blockchain technology. He made a decision that it is blockchain which will be basic database in terms of information about requests and decisions.

 

Technologies

While wanting to meet business requirements, we rejected all public blockchains right from the start. Why? I recommend you to read our previous article Blockchain-fundamenty, where we tried to explain the differences. It is also worth to take a closer look at the prezentation.

Thus, we limited our search to private blockchains/private DLT. The conclusion from our search was that the most recommended platforms are Hyperledger and MultiChain.

Hyperledger is strongly supported and promoted by IBM. CEO CEE Karolina Marzantanowicz mentioned at one of the meetings with Aula Polska that the company had already introduced over 500 projects based on the platform. That’s impressive.

After the meeting I was (and in fact still am) fascinated by  Hyperledger, however, eventually we decided to use MultiChain platform in our project.

Why? Mainly because of the fact that there are often comments on the Internet saying that it is easier to begin with, if we mainly care about time series database. Is it so? Nowadays we cannot confirm it, because we haven’t compared both technologies “on the battlefield”. I hope that we will manage to come back to the topic and check how  Hyperledger works.

Apart from Multichain in application published on GitHubie we used:

  • Java 8 + Spring (Boot, Security) + MultiChainJavaAPI + H2 – backend
  • Angular + Semantic UI – frontend
  • Docker + Kubernetes – containerization of a solution and  deployment in cloud

MultiChainIndividual system components

Installing MultiChain

The first step is of course, installing  MultiChain on my local machine. It can be done without any problem using the guide from MultiChain official website. I won’t be describing here the process in detail, I refer you to linked instruction.

An optional solution is starting everything with help of Docker, with one command, using this repository.

Since linked repository has not been updated for quite a while, we decided to copy it and updated the version of libraries used inside. Hence, we suggest using what is within multichain-cluster folder in our repository.

We can start it by using Docker Compose. While being at the folder with scripts at the console, all you need to do is type docker-compose up or initiate run.bat (for Windows) or run.sh (for Linux).
Created cluster consists of three combined nodes: master node, slave node and explorer node.

run.sh

Starting docker-compose by run.sh script

After a few minutes the whole MultiChain environment will rise. The logs from explorer should appear at the console:

Java i Blockchain run.sh

 

Now you may go to MultiChain Explorer and look at first transaction created automatically:

MultiChain Explorer

MultiChain CLI

Since we haven’t dealt with applications using MultiChain before, we decided to check at the beginning if our created scenario can be implemented using CLI.

What was the result? Positive, we’ve made it!

First steps have been described in detail in README, and run.sh file in multichain-cli folder includes all commands which have to be ran to fulfill steps of  scenario.

To implement the above scenario, we used  MultiChain Streams. What are these streams? Let me quote CEO MultiChain:

Gideon Greenspan

Streams provide a natural abstraction for blockchain use cases which focus on general data retrieval, timestamping and archiving, rather than the transfer of assets between participants. Streams can be used to implement three different types of databases on a chain:

  1. A key-value database or document store, in the style of NoSQL.
  2. A time series database, which focuses on the ordering of entries.
  3. An identity-driven database where entries are classified according to their author.

These can be considered as the ‘what’, ‘when’ and ‘who’ of a shared database.

 

All we needed were two streams. One for requests and another for storing decisions. Due to such approach we could control permissions at  MultiChain level (who can save to what stream), thanks to which standard users couldn’t accept requests.

 

MultiChain + Java

Speaking about integrating MultiChain and Java, we used MultiChainJavaAPI project. API is a bit limited, because it was being created to manage already created chain, due to which there is a  lack of “make a chain” methods. We assumed that our application operates on the blockchain developed by other method (for example by CLI), hence we were not bothered by these shortages.

Let’s take a look at packages which have been formed during developing an application:

MultiChain server

What does particular package consist of?

  • api – REST Endpoints, which frontend uses,
  • blockchaindomain –  classes related to blockchain itself (having no reference to business domain), due to which you may look at basic information about the chain itself and blocks,
  • businessdomain – classes pursuing the logic of business application
  • config – configuring security, CORS, JSON
  • init – initiating in-memory bases by sample users
  • mockchain –  mocked implementations of interfaces responsible for operating on Since during development rarely did we want to have MultiChain on, two profiles of applications, which we may operate in application.properties, had been created.

 

mockchain

Thanks to @Profile annotation over the class we may indicate to Spring, which Beans it is supposed to inject depending on the profile set.

Java i Blockchain @Profile

Bean MockChainExplorer will be injected only if the application is started with dev profile.

MockChainExplorer service

Bean MultichainExplorer will be injected only if the application is started with the different profile than dev:

  • multichain – integration with MultiChain,
  • security – it doesn’t need an explanation, I guess,
  • primitives – simple classes putting server responses into Client application (ErrorResponse, OkResponse).

The most important place for application, business-wise, is Ledger interface. The interface is a sort of abstraction of  decision and conclusion register (adding/deleting/reviewing). At the moment it has two implementations – one based on MultiChain, another is mock, which I have mentioned before while discussing structure of packages. Thanks to such approach you may very easily add another implementations based for example on Hyperledger or traditional database.

Java i Blockchain Ledger

The interface is implemented by MultichainLedger class. Here is where integration with MultiChain takes place.

In class constructor we subscribe streams or create them, provided they don’t exist yet. As I’ve mentioned before, one stream is used to store requests  (Request), another – decisions (Decision).

Java i Blockchain Request

MultichainLedger class constructor and subscribe support method.

Adding something to  stream (which is similar to adding a new request or decision) takes place by using publishFrom method of API MultiChain. Don’t forget that data has to be converted into  HEX form beforehand.

Java i Blockchain HEX

Creating a new request.

Apart from creating a request/decision, a function of downloading a whole list or list of requests filed by a particular user. It is about indicating the stream from which we would like to download data and convert them from HEX into our objects. An example is below. In the example the data is also being  filtered and sorted.

HEX Java i Blockchain

 

Frontend – Angular 6

The Client has been written in Angular 6 using Semantic UI library.

multichain Angular 6

Application allows:

  • to create a new request and assign it to your manager,
  • to accept or reject a request (making a decision) only by the people with the role of the manager,
  • looking at the list of requests,
  • looking at the list of decisions,
  • looking at the list of users,
  • looking at basic information about the blockchain itself and last transactions

More screens from the application itself are available at README on GitHub.

 

Summary

Developing an application in Java, which uses blockchain, is easy, if you know basic assumptions related to blockchain, differences between public and private version, as well as limitations of each one.

Let’s not forget that not everybody needs blockchain. In our previous article we placed a diagram which answers the following question  Do I need blockchain? It is worth to acquaint yourself with it and remember it.

 

Java i Blockchain

 

We hope that a sample application published by us will be helpful for everyone who wants to get into the subject.

Author: Robert Witkowski, Senior Software Engineer, ASC LAB