Skip to content

Build a Blockchain and a Cryptocurrency from Scratch

Notifications You must be signed in to change notification settings

SuNR0N/build-blockchain

Repository files navigation

build-blockchain

Build a Blockchain and a Cryptocurrency from Scratch

Table of Contents

Terminology

Bitcoin

The first decentralized cryptocurrency in 2009 developed by Satoshi Nakamoto.

Blockchain

The blockchain is a distributed and decentralized ledger that stores data such as transactions, and that is publicly shared across all the nodes of its network.

Cryptocurrency

A cryptocurrency is a digital medium of exchange which leverages the blockchain and uses cryptography to generate digital signatures.

Digital Signature

The digital signature is the hash value of the combination of the transactional data and the sender's private key.

The public key of the sender can be used to verify the signature.

Genesis Block

Hardcoded block which serves as the origin of the blockchain.

Mining

Temporarily unconfirmed blocks of transactions can be added to the blockchain by solving a computationally expensive proof of work algorithm. Once solved, the miner can add the block and the other miners will verify it. Miners are rewarded for adding a new block to the chain.

Proof of Work System

A system that requires miners to do computational work to add blocks to the chain and which makes it expensive to generate corrupt chains.

Based on a predefined difficulty level, which sets the rate of mining, the system generates hashes until one with the matching criteria is found. A nonce value is incremented within the block which facilitates the generation of a new hash.

Transaction

An object which captures the information behind the exchange of currency between two individuals. Its input fields (timestamp, balance, signature, sender's public key) provides details about the original sender while its output fields (amount, address) describes the balance change of each individual.

Transaction Pool

An object that contains all new and therefore unconfirmed transactions submitted by individuals.

Wallet

Object that stores the balance of an individual alongside with a private and public key pair. The private key is used to generate digital signatures, while the public key is used to verify signatures and it also serves as the address of the wallet.

Install

yarn

Run

yarn start

With multiple connected peers:

# Terminal #1
yarn start

# Terminal #2
PORT=3002 WS_PORT=5002 PEERS=ws://localhost:5001 yarn start

# Terminal #3
PORT=3003 WS_PORT=5003 PEERS=ws://localhost:5001,ws://localhost:5002 yarn start

Environment Variables

# The HTTP port on which the Express server will listen
PORT=3001

# The WebSocket port on which the P2P server will listen
WS_PORT=5001

# The comma separated list of WebSocket addresses to which the P2P server will join on start
PEERS=

# The initial diffculty of the proof-of-work algorithm which sets how many prefixing zeros the block hash must have  
DIFFICULTY=

# The average amount of time (ms) required to successfully mine a new block
MINE_RATE=

Test

yarn test

With coverage report:

yarn test:coverage

Debug

If you're using VS Code then you can set up your debug configurations within your launch.json file based on the following code snippet to be able to easily debug a particular TypeScript / Jest file or the whole application:

{
    "name": "Current TS File",
    "type": "node",
    "request": "launch",
    "args": [
        "${relativeFile}"
    ],
    "runtimeArgs": [
        "-r",
        "ts-node/register"
    ],
    "cwd": "${workspaceRoot}",
    "protocol": "inspector",
    "internalConsoleOptions": "openOnSessionStart"
},
{
    "name": "Current Jest Test",
    "type": "node",
    "request": "launch",
    "program": "${workspaceFolder}/node_modules/.bin/jest",
    "args": [
        "${relativeFile}"
    ],
    "console": "integratedTerminal",
    "internalConsoleOptions": "neverOpen"
},
{
    "name": "Application",
    "type": "node",
    "request": "launch",
    "args": [
        "src/app.ts"
    ],
    "runtimeArgs": [
        "-r",
        "ts-node/register"
    ],
    "cwd": "${workspaceRoot}",
    "protocol": "inspector",
    "internalConsoleOptions": "openOnSessionStart"
}

Docker

The provided docker-compose.yml file spins up 5 interconnected miner nodes and exposes their public APIs on your localhost on ports ranging from 3001 to 3005.

# Start peer-to-peer network
docker-compose up

# Stop peer-to-peer network
docker-compose down

TODO

  • Reach 100% code coverage
  • Implement endpoint which returns the address book
  • Set up docker compose for the p2p nodes
  • Reduce the runtime of unit tests
  • Implement transaction fees

About

Build a Blockchain and a Cryptocurrency from Scratch

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published