Blockchain in 100 lines of code

Written by stremann | Published 2017/10/15
Tech Story Tags: blockchain | javascript | programming | in-100-lines-of-code | blockchain-code

TLDRvia the TL;DR App

Everything should be made as simple as possible, but not simpler

What is blokchain?

From technical point of view, this is a distributed database with special features like validation, storage and data synchronization.

From business perspective, this is a concept which allows efficient and reliable peer-to-peer transactions without requiring central point of control.

There are some differences in blockchains comparing to distributed data base. Blockchain has “append only” operations, where data is validated and signed.

Players and frameworks

It is easy to get mixed up as usually when talk about blockchains we also talk about the problems we are trying to solve with them. This is the case in the popular blockchain-based projects such as Bitcoin and Ethereum. The term “blockchain” is usually strongly tied to concepts like transactions, smart contractors or cryptocurrencies.

This makes understanding blockchains a necessarily harder task, than it must be. This is why I will go through a simple blockchain just in 100 lines of code called Chainpro. I usually do that to get better understanding how things work under the hood, so feel free to check the “Flux architecture in 30 lines of code” called Flypro and “Virtual DOM in 50 lines of code” which is Dompro as well.

Architecture

Blockchain is a chain of blocks. To make things as simple as possible, but not simpler, every block includes the most necessary: index, timestamp, data, hash and previous hash. And each new block which is about to be added to a chain should be validated as well.

A chain is a singleton implementation which you allow to extend it with a new valid block, and replace a chain in case of multiple peers connection choosing the one that has the longest number of blocks in it.

An essential part of a node is to share and sync the chain with other nodes. When a node generates a new block it broadcasts it to the network. When a node connects to a new peer it queries for the latest block. And when a node encounters a block that has an index larger than the current known block, it either adds the block its current chain or queries for the full blockchain. No automatic peer discovery. The location (=URLs) of peers manually added.

The user is able to control the node using an HTTP server to list all blocks in a chain, create a new one with a content given by the user, list and add peers. It should be noted that node exposes two web servers: one for the user to qpcontrol the node (HTTP server) and one for peer-to-peer communication between the nodes (Websocket P2P server). For more details check GitHub.

Use cases and benefits

From IT point of view, blockhain is transparent and immutable distributed high available database with crypto enabled data consistency validation, smart contracts and atomic transactions.

From business perspective, blockchain is all about operational efficiency (cost efficiency, time efficiency, additional trust for multiparty collaboration) and new operational models, new ways of doing business (assets digitization).

This is why it could be used in huge variety of industries, such as:

Finance

  • Clearing and settlement (retail and corp. syndicated loans)
  • P2P loans + PSD 2.0
  • KYC — Know Your Client
  • Assets (stocks, bonds, etc.) issuance (e.g. book building)and management
  • Digital currencies
  • Collateral management

Media

  • Primary and secondary market for digital assets
  • Crowdfunding and royalty distribution
  • Media distribution network
  • Gaming/Gambling (settlement, open betting network)

Government

  • Public and private registries (land, auto, etc.)
  • Identity management (anti-fraud protection, global encrypted store of personal data)
  • legal and notary (smart contracts, proof of existence and ownership)
  • Voting system

Insurance

  • Claims management
  • KYC
  • Extended warranty
  • Mobility insurance (compensation for trip delay)

Healthcare

  • EMR — electronic medical records
  • Claims management
  • Prescription management and medication adherence
  • Clinical trial results
  • Counterfeit drug prevention

Others

  • IoT — internet of things. Execution of contractual agreement (car and apartment rental, etc.)
  • Loyalty bonus network
  • Intellectual property (patents, rights, authorship, proof of authenticity)

Blockchain roadblocks

Well, the blockchain is still an early days technology. It needs to be very carefully applied considering these roadblocks: performance, latency, scalability, security, privacy, waste of resources and maturity.

If this article helped you, please help others by clapping your hands below — thanks!

Roman Stremedlovskyi is the Senior Software Engineer at EPAM Systems. Passion about Software Engineering, Public Speaking and Social Architecture. Connect with me on Twitter, Facebook, GitHub and LinkedIn.


Published by HackerNoon on 2017/10/15