Simple Full-stack Blockchain Test Network with Angular, NodeJs EP1: Project Organisation and Architecting Back-end

This blog is basically a recap of my journey in architecture very basic blockchain network project.

For those who want to explore my source code, the link is at Github repo. The project is highly inspired by following:

  1. https://lhartikk.github.io/
  2. https://github.com/15Dkatz/cryptochain

However, I note that the source code is for educational purpose only, NOT in production. There is concern about security, needed to be reconsidered again.

Blockchain System Architecture: Overview

The basic blockchain should contain at least followings:

This image has an empty alt attribute; its file name is Blockchain-Architecture.png
Basic Blockchain Network Architecture

1) Node

Blockchain Network is one type of peer-to-peer network (P2P). It employs its nature of decentralisation. In simple words, no single entity has full control in the network. In particular, all Nodes (or Peers ) create, read, update, and save the database as the same copy for all nodes.

The killer advantages of decentralisation feature are outlined as following:

  1. Immutability -The data is immutable, because all parties hold the same set of database. These copies will be verified and endorsed by everyone in the network. If someone tries to manipulate the data, this fault will be detected then rejected by others in the network
  2. Fault tolerance -There is low probability that the network will be down, since there is no centralised entity which can be easily attacked. The network is live as long as there is running node in the network.
  3. Attack Resistance – The system is designed for attackers to use extremely high cost in order to has a right to manipulate the data. it can be said that this use the principal of economics to solve the problem.

Why Immutability? The definition behind blockchain

before moving on, let see and understand these following blockchain vocabulary lists:

Blockchain Network is peer-to-peer network (P2P) which stores database as Blockchain

Blockchain is a Data Structure, which includes and connects many Blocks with Hash Function below:

This image has an empty alt attribute; its file name is blockchain-data.png
credit: blockgeeks.com

It can be noticeable that every Block has its own hash, which is hashed from all data. The data, in this case, are index, timestamp, data and previousHash. Plus, previousHash has the same value of hash from the previous block. It can be seen these links between block as “chain”, so it is called “Blockchain”.

Hash is one-way encryption function, and the good hash function should have these five properties:

1.Deterministic – The same set of data gives the same hash.


2.Quick The function use small amount of time to run.


3. Collision Resistance There is extremely low probability or impossible that the hashes from different data have the same value. For example, hash from “BLOCKCHAIN” is different from “blockchain”

It is hard to find a pair of messages 𝑥1≠𝑥2
with 𝐻(𝑥1)=𝐻(𝑥2)

4. Pre-image Resistance The original data can not be generate from its hash. In other words, it is irreversible.

In mathematics, For a given ℎ
h in the output space of the hash function, it is hard to find any message 𝑥
x with 𝐻(𝑥)=ℎ


5. Second Pre-image Resistance

Giver one message, there is extremely low probability to find the second message, that generate the same hash .

In mathematics: a given message X1, it is hard to find a second message that X2 ≠ X1
with 𝐻(X1) = 𝐻(X2).

Due to the property of Hash function, when there is any change of the data in the block, its resulting hash also changes. If someone manipulates the current block data, its hash will have the different value, compared to previousHash in next blocks. As a result, other nodes, with this method, can verify and reject this kind of fault. Thereby, this Immutability lead to better security.

2) Miner 

Again, let understand the definitions:

Why Fault tolerance? The concept behind Miner

As described before, the decentralised system has no single entity to control the whole network. But Who are responsible for running the network? The answer is any of nodes in the network. However, the running node will periodically selected and changed by the mechanism. This agreement is called “Consensus algorithm“.

1.1) Byzantine Fault Tolerance(BFT)

has following properties:

  • The communication across Network is safe.
  • The network can reach consensus despite any faulty Nodes.

1.2) Attack Resistance

  • Honest Nodes should always win.
  • It is cheap to verify the correctness.
  • It is expensive to attack.
  • It should not slow the network.

In this project, I use Proof of Work Consensus.

Proof-of-Work is a piece of data, which is difficult to produce(by someone) and easy to verify(by all others)

Proof of Work Consensus(PoW) defines that the first one, who can figure out Proof-of-Work, gain the right to endorse and add block to the blockchain. When adding is completed, the adding node will get either reward or fee. This type of process is called “Mine”.

Miner is node that mines. Since mining is hard, it has cost of computing power ,then electric utility fees.

This image has an empty alt attribute; its file name is bitcoin-network-2.png

It is certain that without miner, the network could not be run. So, the most crucial part of the network is the miner. The interesting concern is that the greedy make the network live, as all the nodes, who become miners, expect to earn the reward generating from the mining process. If the network has great decentralisation, the network also has Fault tolerance. In other words, it is hard for network to be dead.

Why Attack Resistance? The concept behind Proof of Work

The idea behind Proof of Work Consensus is:

the first miner, who can solve the mathematic puzzle, gain the right to endorse and add block to the blockchain. When adding is completed, the adding miner will get either reward or fee.

Since the puzzle is extremely hard to solve, so the producing answer( Proof-of-Work) can be a random guesting process. Practically, this can be done via computer only in order to prove that those miners have already done the hard work.

From an economic perspective:

Network must increase the cost for middle man or miner, so that the cost is higher than the value of advantage the faulty miner could cheat from the network. Thereby, middle man dare not risk cheating the network, because although the miner can cheat, the return is not worth cheating.

When someone tries to manipulate the past block data, that person needs to entirely solve the puzzle from that past block to the current block. Moreover, that faulty party needs to increase the proportion of faulty miners to be greater than 50% of the total. All of these needs to be done in limited time, before the other miners add the data to the block. Then, it is practically impossible to cheat highly decentralised blockchain system because of highly computing cost.

The main purpose of blockchain is to solve the problem that people do not trust one another. So, the blockchain become more trustful.

3) (Crypto) Wallet

The main duty is to receive from and send to other wallet as wallet owner want. However, the crypto wallet is different from the traditional ones:

The bank stores only total balance for each wallet

to

The wallet is a software that store private and public keys. These keys are known as “Public Key Cryptography” or “Asymmetric Cryptography”

The principal is that public key can be generated from private key, but not vice versa.

Private Key can be considered as password. Private Key owner can send the value to the others

Public Key can be considered as username. If we let others know our public key, they can send the money back to us.

Private Key allow us to sign the transaction and store the signed one into the blockchain

sign is a function that takes Private Key and transaction as inputs and return signature. The signature can indicate that where the transaction comes from via verification process.

Or, sign(private_key, transaction) => signature

verify is a function that takes transaction, Public key, paired with Private key used to sign transaction and resulting signature as parameters. It can confirm whether that transaction is sent from wallet that hold that public key or not. As a result, everyone in the network can verify the transaction without disclose any information about their private keys.

or verify(public_key, transaction, signature) => true/false

As a result, private key must be stored in a secure place. This is because any ones who know private key have a full controls over wallet, namely creating new wallet with same private key, recovering wallet, and creating new transaction (transferring money out).

It is clear that decentralises system does not store total balance, but only transactions e g

1) Tom deposits $1000

2) Tom transfers $500 to Tim (Tom has now $500)

3) Jo-anne transfers $200 to Tom (Tom has total $700)

if you want to know the total money , you just add and subtract all transactions.

Transaction Model( UTXO )

Since decentralised network has many miners which compete each other in order to mine transactions, add them to block and send the data to other mode. These leads to:

“Block time” is time interval between blocks, or time it takes to mine a block

In reality, the financial transaction occurs in real-time manner. So, the transaction which has occurred but not been added is called Unspent transaction output( UTXO )

This image has an empty alt attribute; its file name is UTXO-flow-model-1024x530.png
Transactions Flow in the UTXO Model

It can be seen that transactions consume the previous transactions and UTXO will be created in order to be paid as transaction in the future.

Math behind Private and Public keys: Elliptic Curve Cryptography ( ECC )

Elliptical curves is set of points {x, y} of y^2=x^3+ax+b

This image has an empty alt attribute; its file name is elliptic-curves.png

Every Elliptical curve has following properties:

  • Graph is symmetric with respect to x-axis.
  • if a line intersects any point in the curve it will always intersect a second.
  •  if a line intersects two points in the curve it will always intersect a third.

Due to above properties, the new property of point can be newly defined as :

Point Addiction: P + Q = R – addition is defined as  negation of the point resulting from the intersection of the curve and the straight line defined by the points P and Q, giving the point, R.

This image has an empty alt attribute; its file name is ECC-addition-3.png

Note that we’re not talking about addition as 1+1 , it’s a different kind of operation. Nevertheless, both addition operations share the same following properties:

  1. Closure Property
  2. Associativity
  3. Identity
  4. Inverse
  5. Commutativity

Point Doubling : P+P=2P  or adding a point to itself  but now we have to find the equation of the line that goes through P and P. There are infinite such lines. In this special case, we opt for the tangent line instead as below:

This image has an empty alt attribute; its file name is ECC-doubling-1.png
Generating Private Public Keys for Elliptical Curve

Generating Private Public Keys for Elliptical Curve

  1. Randomly generate point G

  2. Suppose that we need to find point P = k*G where k = random 256-bit integer (between2^0 and 2^256) and P is point (x,y)

    Since k is known, we just perform point addition for k times until we get P= k*G.

    However, we still can reduce the number of addition operation. For example:


    Supposed that k = 126 and we want to find P= 126*G
    This can be rewritten as:
    2^6*G +2^5*G+2^4*G+2^3*G+2^2*G+2*G
    =126*G
    It is clear that there are now 5 operations.

    G+G=2G
    2G+2G=4G=2^2G
    4G+4G=8G=2^3G
    8G+8G=16G=2^4G
    16G+16G=32G=2^5G
    32G+32G=2^6G

    And, there is more 6 operations, then the total is 11.
    This is significantly faster than adding G to itself for 126 operations.

  3. Suppose that we need to find point k = P/G given that we know P


    In this case, we are asked “How many point addition G to itself it takes” and it is clear that we can not reduce the operation as above step does. So, finding k consume greater time than P.

However, point addition sometimes leads to point at nearly infinity bound, so this can not be calculated because of the limitation of hardware. Accordingly, the bound, in cryptography, is limited as following:

Elliptical Curve Cryptography

From:

y^2=x^3+ax+b

TO:
( y^2 )mod p =(x^3+ax+b) mod p where p is prime number

Now, the range of point is between 0 and P

As P can be easily found and it shows identity of k

But it is extremely hard to find k despite knowing P

P can be seen as username or Public key 

Also, k is password or Private key

4) Faucet app

Generally, it is a web application. It hold a great amount of money which comes from genesis transaction( from block 0) or donator.

The role of faucet is to send some amount of money to wallet that requested. In other words, it simply creates transaction from faucet app to the public key.

Faucet app is simply a wallet.

The main purpose is to use the request money to test the blockchain network.

5) Explorer app

It is a web application that help user to explore information about the network such as:

  • Blocks
  • Transactions
  • Address(Public Key) and Balance

Tool Implementation

This project is full-stack web application. For simplicity, I implemented RESTful web API as backend and these are tools and libraries I used:

Backend

  • Express.js or NodeJs Framework for RESTful web API
  • Redis – create channel so that the Node can publish and subscribe ,leading to synchronisation between Nodes
  • Socket.io for Real-time feature Frontend
  • Jest for Test Driven Development (TDD)

Frontend

  • Angular 7 Typescript Framework
  • Angular Material for UI Design

Nonetheless, this EP focuses on the backend.

Project Organisation and File Structure

In this project, I use npm as package manager and Angular-cli command in order to scaffold the project.

ng new full-stack-blockchain-network

According to Separation of concerns , frontend and backend are separated as:

cd full-stack-blockchain-network/
mkdir backend
This image has an empty alt attribute; its file name is Project-Structure.png
File Structure

Separation is a design principle for separating a computer program into distinct sections, so that each section addresses a separate concern. As a result code is easier readable and maintainable.

Let Code!

1) Designing Blockchain Network: RESTful API Requirement

Users can interact the app though 3 features, which are wallet, explorer and faucet.

  • GET:http://{host}:port/
  • GET:http://{host}:port/wallet-info
  • POST:http://{host}:port/wallet/recover
  • POST:http://{host}:port/wallet/create
  • POST:http://{host}:port/wallet/transact
  • GET:http://{host}:wallet/mine-transactions/
  • GET:http://{host}:port/wallet/start-mining-transactions
  • GET:http://{host}:port/wallet/stop-mining
  • GET:http://{host}:port/explorer/blocks
  • GET:http://{host}:port/explorer/blocks/:blockId
  • GET:http://{host}:port/explorer/transactions
  • GET:http://{host}:port/explorer/transaction-pool-map
  • GET:http://{host}:port/accounts
  • GET:http://{host}:port/faucet/request

So, all of blockchain services are separated into three modules as below:

This image has an empty alt attribute; its file name is organise-restful-1.png

According to MVC pattern, the two folders of model and controller are created above.

Model represents an object storing data. It can also have logic to update controller if the data updates. In our case, the data is blockchain.


View represents the visualisation of the data that model contains to the users. Also, users can interact with View. But, in our case, this is regarding to Angular.


Controllers acts on both model and view. It controls the data flow into model t and updates the view whenever the data changes.

Then, Express can be used to write RESTful Web app as follow:

This image has an empty alt attribute; its file name is backend-1-1.png
backend/index.js

2) Building the Blockchain Node Requirement

This syncs with other Nodes to store and update the data as the same copy, which are:

  • Block Data( many blocks together as blockchain)
  • Unconfirmed Transaction(หรือ Transaction Pool)
This image has an empty alt attribute; its file name is backend-2-2.png
backend/index.js

In order to run the first and main Node:

npm run dev

To run next Nodes (or Peer Nodes) for synchronisation with main Node :

npm run dev-peer

Synchronisation

This image has an empty alt attribute; its file name is synchronisation.png

The below class pubsub is for channel creation and methods for publish , subscribe and broadcast.

This image has an empty alt attribute; its file name is pubsub.png
pubsub.js

Block and Blockchain Data Structure

This image has an empty alt attribute; its file name is block_logic.png
Block

Data is Property which stores array of Transaction data structure.
SHA256 is a hash function.

This image has an empty alt attribute; its file name is block.png
block.js

Nonce or “n(umber used)once” is a parameter in the block. It serves to be modified so that we can change its value until the block is mined or the puzzle answer (Proof-of-Work ) can be found. ไ

and the condition is:
SHA256(all properties in Block) contains leading 0s at certain amount. The amount depends on the difficulty value at that time. The higher difficulty value , the lower probability of finding the right answer is. For instance, the difficulty is 2, the answer is:

005603f7bfb22139228fa5149495661ac70fcf10cd6cf31bbbe007cf16e945bb

The mining logic can be implemented as:

block.js

In the case that node competes each other, the first node, who can find the solution first, broadcast chain. After that, Peer Node, who subscribe the channel call method replaceChain in order to sync chain.

This image has an empty alt attribute; its file name is replace-chain.png
blockchain.js

3) Wallet Requirement

  • create and store Private key
  • Recover the lost Wallet with Private key
  • calculate Balance
  • create Transaction
  • sign Transaction

In the wallet module, file is organised as:

This image has an empty alt attribute; its file name is wallet-structure.png
wallet/index.js

As described before, all about keys use the principle of Elliptic Curve CryptographyIn javascript, there is library https://github.com/indutny/elliptic , and it can be used as below:

This image has an empty alt attribute; its file name is wallet-1.png
wallet/index.js

transaction.js

Transaction must include:

  • id which is unique with library called uuid
  • outputMap when it is mapped with receiver’s public key, it will return amount of money
  • input composes timestamp, remaining balance, sender’s public key and signature for that transaction
This image has an empty alt attribute; its file name is transaction.png
transaction.js

Apart from Transaction logic, method update which aggregate many Transactions which has same publicKey of input . In other words, one transaction can have many outputMap.

This image has an empty alt attribute; its file name is update.png
transaction.js

transaction-pool.js

or Unspent transaction output( UTXO )

This image has an empty alt attribute; its file name is transaction-pool.png
transaction-pool.js

4) Miner Requirement

or a wallet with mining features:

  • mine Transaction pool
  • then store as confirmed transaction into Block , then linked with previous Blocks, result in Blockchain
This image has an empty alt attribute; its file name is transaction-miner.png
transaction-miner.js

Method startMing infinitely loops and calls method mineTransaction , leading to competition between miner nodes.

This image has an empty alt attribute; its file name is start-mining.png
transaction-miner.js

4) Faucet app Requirement

  • create Transaction sent to wallet who request
  • contains a great amount of money

I hardcoded that there is transaction sent to Faucet’s public key.

This image has an empty alt attribute; its file name is genesis-block.png
model

The faucet wallet will be recovered with private key as below:

This image has an empty alt attribute; its file name is faucet.png
network/index.js

5) Explorer App Requirement

  • explore confirmed transaction
  • explore mined Block
  • explore address(Public key) and balance

Model can be gotten via Controller with GET method as below:

This image has an empty alt attribute; its file name is explorer.png
routes/explorer.js

Conclusion and my Thought

As aforementioned, I personally believe that the most important part is to deeply understand the business logic, then simplify that understanding into the code.

The good code is one that are easily readable by everyone.

In the next EP, we will continue talking about How I architect Angular for front-end part.

Related posts