Night Protocol Documentation

Hello I'm ujjwal-kr and interested in computer science. I'm working on a long term research project related to blockchain and this project exists to help me gain some experience, and also to share my journey so that other people may learn about it and contribute their ideas as well. If you are interested to work with me on my research, hit me up on epicujjwal@protonmail.com

Night is a blockchain protocol written in the rust programming language and is created to give people insights about the internal structure of a blockchain and help me with further research into this topic. To be noted that this is v1 of the project and does not implement the distributed natute of blockchain, and also dosen't have any multiple wallet system. Those features will be availabe soon with v2 and this paper will be updated accordingly.

Prerequisites

  • Basic rust, the rust book is a good resource to learn it
  • Javascript & React
  • No knowledge of blockchain is required

Structure

  • The night_protocol folder contains the rust backend and blockchain source.
  • The client folder contains a client for the blockchain written in nextjs and typescript.

In this document I'm not going to talk about the client folder much as it is not included in the core structure of the chain. But to summarize, it is a REST client interacting with the exposed blockchain REST API, the calls can be seen in the services folder. Here are some useful routes:

  • GET /transactions?page=<number> Sends paginated list of transactions done
  • POST /gamble?amount=<amount> Gambles with the amount
  • GET /balance returns the balance of user

Terms used here will be explained ahead in the documentation, this is for client API reference.

night_protocol

If we look into the /src of the folder, we'll find main.rs and blockchain folder. The main file defines the program initiation and has http routes and functions to let the client interact with the chain. The blockchain folder has 2 files, blockchain.rs and master_chain.rs. They are the blueprints and interfaces of the project.

What is a Block?

Block is a data structure containing some properties. Here the block contains properties like transaction and a unique identifier. There are also some properties in the blocks which makes them connected to each other, hence forming a blockchain. A blockchain is basically just connected blocks, or rather "chained" blocks. Here is an illustration of the blockchain:

Here all the properties of the blocks are shown. Each block contains an id, previous_hash, transaction & block_hash. I use the SHA256 algorithm to hash the blocks. Hash is a fingerprint of the input data, which can be used for verification as a hash is alaways unique for each input. Here the input is the entire block data (id, previous_hash and transaction). previous_hash is the hash of a block which is prior to it, this is what makes them connected, and allows the validation function to make sure nothing has been altered. The first block has no prior block therefore the previous_hash is set to genesis. When a new block is created, hash of the previous block is taken as stored in the previous_hash field of the new block.

Transaction

As said earlier, this project has got no multiple wallet system. So transactions are either from the network to the user or from the user to the network. For each transaction made, a new block is created. A transaction has amount, sender and reciever. The balance is not stored anywhere in the chain as it's not practical to update the chain, the blockchain is meant to be immutable by natute. The balance is calculated on the fly by scanning the entire blockchain and you can find the function in blockchain.rs file. A transaction looks like {sender: "net", reciever: "user", amount: 500} For now, the only way to do a transaction is to use the gamble method.

Validation

Whenever a transaction is made, a validation function is fired to ensure that all the blocks before the arriving blocks are valid. The validation function takes two blocks at once, and hashes the first block and compares the hash to the previous_hash field of the second block. This is done for the entire chain, each time a new block is added. If the validation fails, no new transactions can be performed, and the balance returns 0. It also means that as the number of blocks increases, the time taken for the validation function to run also increases. We have addressed this issue by adding something called a master block.

Master Block

The master block contains a blockchain. The chain formed by the master block is known as the "master_chain", and whenever the blockchain exeedes 100 blocks, the entire blockchain is validated, compressed and sent to the master_chain, as a master block. And then the blockchain is deleted. This way, one master_chain can hold 20 transactions. master_chain.rs has all the methods related to the master_chain.

Master_chain has all the properties of a normal blockchain, And it makes it easy to validate all the transactions since now instead of validating each transaction, we only need to validate a master_block and the speed of the validation algorithm is increased by 10000% (100 times). The validation function to validate the master_chain is same as the blockchain and works on hash comparison.

Improvements

Usage of this chain in production is not practical yet, because it still requires trust of the user who is hosting it. In future, we can make it distributed by using p2p technology and also make multiple wallet system using cryptographic signing. Thanks for reading the paper. If you have any ideas to discuss with me, you can join my discord server.