Nimiq [1] is a block chain project, launched in 2017, its application scenario goal to pay (lightning network, P2P currency exchange, etc.), feature is only through the browser to open a Web page can be involved in the block chain network to transfer and dig, equivalent to the Web version of the COINS, greatly reduces the average user to participate in the threshold of the chain block.

Unlike Menlo Coin web mining, which treats the browser as a worker, Nimiq can actually participate in the blockchain network as a node through web pages [2].

Click on me to experience nimiq.com

Note: The purpose of this article is not to promote the project, but to talk about some advantages of Node in the development of the blockchain project.


Clever tradeoff – Nimiq’s three clients

Nimiq has three client sets Full, Light, and Nano, designed for the server, desktop browser, and mobile browser respectively. Of course, it’s not limited to these scenarios. Browsers can also run Full clients, and servers can run Nano clients. The three apps run on all major browsers, including Chrome, Firefox, Safari, and even the Built-in wechat browser.

The Full client needs to synchronize all blockchain data, similar to bitcoin and Ethereum clients. The nodes run by the Full client are also called backbone nodes, which can complete functions including query, mining and transfer.

The Light client needs to synchronize some blockchain data and relies on backbone node to compress block data. The Light client aims to lower the threshold for ordinary users to participate in mining by opening a browser. The Demo on Nimiq website runs the Light client.

The Nano client is designed to make payments in a mobile browser, requiring only less than 1MB of data to synchronize.


Code reuse – a single copy of JavaScript runs in multiple scenarios

Write once, run everywhere

This sentence was originally used to describe the Java virtual machine as an intermediary between code and system, so that Java programs can run independently of the operating system. After the advent of Android, this sentence has been verified to some extent.

Now, in 2018, Node.js is also written once for JavaScript, running everywhere.

Nimiq’s blockchain system code is implemented using JavaScript, except for the implementation of the hashing algorithm (discussed in the next section). In terms of JavaScript code, only a very small part of the browser and Node side implementation for performance considerations (about 5% of the total), most of the code is shared.

Statistics were made under nimIQ-Core [3] SRC /main/generic and SRC /main/platform respectively:

find . -name '*.js' | xargs wc -l
Copy the code

The results are 22182 and 1332 lines, respectively.

Nimiq implemented a blockchain system that runs both on a Linux server with dozens of cores and in the wechat browser, and they run the same set of applications.



Hash implementation of polymorphism – WebAssembly and Node-Addon

In the previous section, Nimiq’s hash algorithm was not implemented using JavaScript.

One way to create blocks on a blockchain is called Proof of Work, commonly known as mining. The program takes some data and uses a hash algorithm to produce a new string of data. If the new string of data matches a certain rule, it is “mined”. The hash algorithm in the middle is the calculation needed to “mine”, the faster the calculation, the higher the mining efficiency.

The Nimiq team chose a variant of the Argon2 [4] algorithm, which they call Argon2D, and they are also doing some anti-ASIC work to prevent a mining market dominated by mining machines like Bitcoin.

One objection to node.js backend development is that JavaScript is not very efficient. With V8 and Libuv, Node.js is actually an order of magnitude more efficient than Java and far more efficient than Python and PHP. In order to better perform CPU-intensive tasks, Node.js provides Native Addon [5] function, which enables Node.js JavaScript code to call C/C++ compiled code and better utilize multiple cores to perform CPU-intensive tasks.

In order to pursue the ultimate efficiency, Argon2 naturally uses C to implement, and runs perfectly on node.js platform with Node.js Native Addon.

But what about the browser platform? The answer is WebAssembly [6]! A technique that enables browsers to run native efficiency programs. Therefore, in addition to JavaScript code, C code can be reused as well!

As a practical matter, not all browsers currently support WebAssembly and not all operating systems support Node.js Native Addon, so Nimiq also implements the JavaScript version of Argon2d.

Based on the results of multiple cpus, Nimiq’s mining efficiency (Argon2d execution speed) in JavaScript, WebAssembly, and Native Addon implementations is approximately 1:2:6.


Polymorphic communication – WebSocket and WebRTC

Nimiq also provides two implementations for node communication: WebSocket and WebRTC.

Using WebSocket on Node.js platform to achieve communication, browser platform compatibility is complex, WebRTC can cover more browsers, such as Safari on iPad.

Node.js is developed as a server and is naturally friendly to WebRTC communication with the browser.


Off-topic — Node packing


I recently implemented a set of mining pools around Nimiq to solve the problem of small retail investors having difficulty generating revenue from small amounts of computing power.

The desktop mining client of mining pool is developed by Node.js and Native Addon. Through the tool Zeit/PKG and the author’s debugging in one night, the executable file packaging of Windows, MacOS and Linux platforms is easily completed. At present, the system compatibility is excellent.

Node.js really implements, write once, run everywhere.


reference

[1] Nimiq official website: Nimiq [2] Nimiq Node program: Nimiq Testnet Luna [3] Nimiq Core code: NimIQ-Network/Core [4] Argon2: Argon2 – Wikipedia [5] Node.js Native Addon: Node.js v9.4.0 Documentation [6] WebAssembly: WebAssembly [7] WebSocket: www.npmjs.com/package/ws [8] WebRTC: webrtc.org/ [9] Tianchi Nimiq: Tianchi Nimiq Mine Pool [10] Tianchi desktop client: skypool-org/skypool- nimiQ-miner