L1 Minimal Viable Blockchain

Minimally Viable Blockchain

 

Before you get proof-of-work or transactions working, you need to build the underlying blockchain layer and its block-swapping P2P network. Make a working copy of the L1/stubs folder located in the project files folder.

main.js

Running node main.js starts the main entry point for the Minicoin program. Here, you have a pre-built command line REPL user interface. It’s up to you to fill in the details in the Blocks, Miner and P2P module files before anything actually works (see below)!

blocks.js

This file is a module. In Node, anything called exports.* is accessible from other source code files by require(‘./blocks.js’) ing this file.

 

Here you work with blocks and blockchains. A basic block is made like this:

 

prevBlock = chain[chain.length – 1];

block = {

time: Math.max(prevBlock.time + 1, Math.ceil(Date.now() / 1000)),

txs: [‘tx1 string’, ‘tx2 string’]

};

block.hash = SHA256().update(prevBlock.hash + JSON.stringify(block)).digest(‘hex’);

 

A valid bock time is a UNIX timestamp between 1 second after the previous block and 10 seconds in the future (to allow slightly inaccurate computer clocks).

 

There are function stubs to add a block to the local chain and potentially swap to a forked blockchain from the P2P network.

 

addBlock(block, chain) takes a blockchain array that’s passed by reference. In the function, you append the block in-place using chain.push(block) so that you don’t need to copy the entire blockchain array in memory every time.

 

swapChains(chain) takes a blockchain array you got from the P2P network. It makes the swap if the chain is valid and longer. It returns the difference in length so that the P2P algorithm knows how to respond to the peer.

miner.js

This is for making the next block for the blockchain. It instamines, so main limits ourselves to one call per 10 seconds.

 

mineBlock(txs, chain) takes txs and that’s just an array of strings for now. It returns a block that will fit on the end of the chain.

p2p.js

This is where things get interesting. You are provided with the boilerplate about making and receiving connections with peers. You need port 3151 open to receive incoming connections.

 

Minicoin has a purely push-based messaging system. At this stage it can send and receive LATESTBLOCK and BLOCKCHAIN messages that you have to handle.

 

The peer making a connection to a server starts by sending their latest block and the server peer goes to step 1:

 

  1. LATESTBLOCK: The other peer sends their latest block to you.
    1. If the block fits as the next block on our blockchain, sit it onto your blockchain and rebroadcast it to your other peers…
    2. If the block is the same as our latest block, do nothing.
    3. If the block doesn’t fit, it’s either too old/new to add to our chain, or it’s from a fork. Send your blockchain back to the peer…(switch roles and go to step 2)

 

  1. BLOCKCHAIN: The other peer sends their blockchain to you.
    1. If their chain is longer than yours, swap it out and send your new latest block to your other peers…
    2. If the chain is the same length, do nothing.
    3. If the chain is shorter, respond with your blockchain…

 

This algorithm is wasteful in that it sends the entire blockchain in case of a conflict. However, it’s really simple and it’s similar to the Bitcoin network algorithm.

 

When you’re done, run node main.js. Run a couple peers and test the .mine and .add functions. Try forking some chains!