📜 ⬆️ ⬇️

Exonum first buyding: why we chose Java

The core of our framework for developing Exonum private blockchains is written in Rust, since this PL is focused on memory security. However, along with many advantages, Rust has a number of features that complicate the “interaction” with it: its syntax is unusual for many developers, and the threshold for entry is rather high.

To make it easier to work with the Exonum platform and make it more accessible to the audience, we decided to write a banding library. The language for Binding was Java.

Why we chose Java, we tell under cat.
')

/ Exonum

A couple of words about Exonum


Exonum is our open source framework for developing private blockchains. The blockchain on Exonum is much faster than public blockchains and is capable of processing up to 5 thousand transactions per second. For comparison, in Ethereum, this indicator equals several dozens , while bitcoin is even smaller .

In doing so, Exonum uses the Byzantine consensus algorithm to protect data. It does not require mining and guarantees the correct execution of transactions, even if a third of the network nodes will be compromised.

The Exonum platform can be used in any area: financial, technological, legal. In particular, the framework is suitable for creating digital rights management systems (the demo can be found on the official website ) and voting organization ( demo ). In both cases, all occurring processes are as transparent as possible and protected by cryptographic mechanisms.

Last year, the State Land Cadastre of Ukraine was implemented using the Exonum platform. And before that, Exonum launched a land management project in Georgia. We also negotiate with dozens of companies from the Fortune 500 and the European Union about the implementation of our system in their business processes.

The core Exonum is written in Rust. The choice is justified by the fact that this PL focuses on security and speed — on some tasks it works faster than Java, Go, C, and C ++. At the same time, Rust guarantees memory safety and prevents races when two threads try to access one data.

The compiler Rust is designed to minimize the number of bugs caused by the human factor. For example, it eliminates several classes of errors at the expense of the concept of time of life and ownership.

All values ​​in Rust have a “domain of ownership”. When a name goes outside this area, the associated resource is released. Here is one example of the code that is provided in the official Rust documentation:

fn use_vec() { let vec = make_vec(); //   print_vec(vec); //   print_vec for i in vec.iter() { //   vec println!("{}", i * 2) } } 

If you feed it to the compiler, it will generate an error:

 error: use of moved value: `vec` for i in vec.iter() { ^~~ 

This suggests that vec is unavailable because its domain has changed. Thus, “shooting yourself a leg” during the development process becomes much more difficult.

Why we decided to create binding


"Noisy" syntax

Rust language offers a convenient and wide range of data types that can be combined with each other. This makes it possible to organize the sets of values ​​in the code and restrict access to data, protecting them from unauthorized access.

These features are very important when working with smart contracts in Exonum. Thanks to them, the smart contracts of our framework have greater performance and security of access to memory than, for example, Ethereum solutions.

In general, Rust is similar to other imperative languages ​​(in particular, the Rust syntax resembles C / C ++), but represents a large number of innovative concepts. It has cycles, conditions, functions, but at the same time there are areas of ownership and types. Therefore, those who have never worked with this PL can be difficult to read programs on it.

At first they seem to be "foreign." "Pain" adds an unusual memory management (compared to other languages), which makes Rust so safe. Last fall, the creators of Rust published the results of a survey among 5 thousand members of the community. Almost a quarter of respondents noted that it is difficult to work with Rust.

Too "demanding" compiler

As we have already noted, the task of the Rust compiler is to reduce the number of bugs in the code. The compiler is strict with the text of the program, but at the same time displays options for eliminating errors. In this case, the compiler even shows warnings related to the programming style.

This approach allows you to write reliable code (which is important when working with blockchains in general), but it also has the opposite side of the coin. Sometimes you have to write programs on Rust so that the compiler "understands" that you are not performing prohibited memory operations. And since the language is still young and continues to evolve, there may not be any established practices. Because, as Exonum developer Ilya Bogdanov says, many patterns have to be found using the scientific method.

Small community

The third reason for the creation of Binding was a small Rust-community. Although the community of this PL is quite friendly, and its members are always ready to answer questions, the language “suffers” from a small amount of literature and libraries. However, it will be fair to note here that this problem is being gradually solved.

In recent years, Rust has been actively promoting Mozilla and Samsung , which has a positive effect on the number of libraries being developed and new “wrappers” for existing solutions from the world of C / C ++. "Textbooks" on the language also begin to appear gradually. From those that already exist, Ivo Balbaert's Basics of Rust (Ivo Balbaert), the online guide on the official website and the recent book of Rust programming language, one of the Rust project developers Steve Klabnik, are worth highlighting.

Why choose Java


One of the main reasons that determined the choice was the largest community of this PL. According to a study conducted on the Stack Overflow site last year, Java is in third place in popularity (it was bypassed only by JavaScript and SQL). Of the 64 thousand developers polled, almost 40% write in Java.

Due to the size of the community, this PL has acquired an extensive set of tools. These include IDE, analytical solutions, benchmark frameworks, etc. There are so many of them that some companies oblige developers to use only certain IDE and frameworks to avoid “splitting” the work environment.

At the same time, Java has a simple syntax and Java Native Interface (JNI), which can work with the C Application Binary Interface (ABI). In addition, Java allows you to use other languages ​​on the JVM stack: Scala, Kotlin, Clojure.

And finally, the Java machine is cross-platform: Java code is executed in byte-code, which is interpreted and run on Windows, MacOS, and Linux-based platforms. In this case, the Java language is more tied to open source (compared to, for example, C #). Java developer tools are mostly free: this is the JDK and the integrated development environments based on it - JDeveloper, NetBeans, Eclipse, etc. At the same time, you can find a huge number of open source projects (for example, on GitHub ) on specialized resources. There are also many manuals on working with open source technologies.

The main challenges in developing Java Binding


The development of Java Binding was long and complicated (and it is still underway). We needed to take into account all the features that make the Rust and Java languages ​​so different.

For example, one of the difficulties was the organization of resource management. The fact is that in Java there is a Garbage Collector, but in Rust it is not. It was removed in one of the earlier versions, because the developers came to the conclusion that they could provide the same level of reliability with the help of the type system.

Java GC, although it has an increased resource consumption (it makes all functions give it unused objects to avoid potential memory leaks), is quite convenient. Therefore, we needed to implement a resource cleanup mechanism that Java developers would like.

Another complication was related to the specific data structures presented in Exonum, the Merkle trees. Exonum uses them to combine blockchain states into a single hash. This makes it possible to prove the authenticity of transactions without the need to communicate with several complete network nodes. This functionality is important for the work of our light clients, because it also needed to be interpreted in Java.

Java API almost completely repeats Rust API. This is done to make it easier for us to adapt the documentation and simplify the work of users. We have prepared a separate tutorial on setting up and running the Exonum node with the Java Binding App.

To create Java services , you can use a template project generator. You need to install Maven 3 and run the command:

 $ mvn archetype:generate \ -DinteractiveMode=false \ -DarchetypeGroupId=com.exonum.binding \ -DarchetypeArtifactId=exonum-java-binding-service-archetype \ -DgroupId=com.example.myservice \ -DartifactId=my-service \ -Dversion=1.0 

You can use the interactive mode:

 $ mvn archetype:generate \ -DarchetypeGroupId=com.exonum.binding \ -DarchetypeArtifactId=exonum-java-binding-service-archetype 

You can find a complete guide with examples on setting up a Java service in the documentation on the official Exonum project website. Recommendations for running the Exonum node are in the repository on GitHub .


/ Exonum

Future plans


While Java Binding is in alpha. We plan to release it as a complete and ready feature in the near future. Now we are collecting feedback from users to track potential problems in the library and make corrections.

We are also working on documentation, writing example projects, SDKs to simplify integration with the application on the blockchain and improve the UX as a whole. You can find the complete project roadmap in the repository on GitHub .

There you can also take all the sources to try Java Binding and write your Java service for Exonum. If you have any questions in the process, contact our Gitter development team . Tell and help as much as possible.

Source: https://habr.com/ru/post/423441/


All Articles