gary115 / Blockchain

P2P payment system with a cryptocurrency wallet built on a blockchain like distributed ledger with shared transaction pool and shared blockchain across all nodes in the network

Home Page:https://bitcoin.org/bitcoin.pdf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cryptocurrency built on a distributed ledger

Maintenance License: MIT

alt text

Downloading

$ git clone https://github.com/mayankamencherla/Blockchain.git

This app is a backend that mimics a peer to peer payment system (like Bitcoin), with a fully functional wallet, attached to the local miner, but with transactions and the blockchain synced across the distributed system of nodes

Pre-requisities:

Some key things to set up / understand to use this app:

Setup Locally

To get the app working locally, or to run test cases, follow the instructions below. After setting up the app, details on each API and how to use it can be found below in the API's available on this app section. If any of the commands below are denied due to a permission error, please prepend a sudo to the command.

  1. Navigate to the app's root directory

  2. Run the following command to install all the dependencies:

$ npm install
  1. Open the first node on the network:
$ npm run dev

By default, the first node's HTTP server is hosted on port 3001, and the P2P server is hosted on port 5001

  1. Open the second node on the network:
$ HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001 npm run dev

This opens a new node, with an HTTP server hosted on port 3002, and the P2P server hosted on 5002, and it connects to the peers in the network (In this case, only 1 hosted on port 5001)

  1. To open the nth node on the network:
  a. Choose 2 open ports, 1 for the HTTP server, and 1 for the P2P server of the node
  b. PEERS = n-1 P2P servers that were created before this node in the form: ws://localhost:<port>, seperated by commas
$ HTTP_PORT=<HTTP PORT> P2P_PORT=<P2P PORT> PEERS=<ws://localhost:<PORT1>,ws://localhost:<PORT2>...> npm run dev

Run test cases:

$ npm run test

API's available on this app

This app supports 6 API's currently: (3001 can be changed to any of the other node's HTTP server's port number)

  1. GET /blocks

    • Fetch the blocks in the blockchain saved locally on the node whose HTTP server is running on port 3001
  2. POST /mine

    • Mines a new block containing the data field in the post request
    • The block is then added to the blockchain locally
    • The updated blockchain is broadcasted across the network so that other nodes can update their blockchains
  3. GET /transactions

    • Fetch all transactions in the transactions pool saved locally in the node
    • The transaction pool is the same and is saved across all nodes in the network
  4. POST /transact

    • Takes in recipient and amount as post parameters
    • This endpoint is used to send amount to recipient from wallet at 3001
    • Creates a new transaction and adds it to the transaction pool
    • Wallet at 3001 has amount subtracted, and recipient wallet gets amount added to balance
  5. GET /public-key

    • This endpoint is used to retrieve public key for the wallet at port 3001
    • This public key must be used as recipient to send wallet at 3001 currency
  6. GET /mine-transactions

    • Mines a new block containing all the transactions in the transaction pool shared across all nodes in the network
    • The block is mined based on the proof of work mechanism also used in Bitcoin.
    • The block is then added to the blockchain locally
    • The updated blockchain is broadcasted across the network so that other nodes can update their blockchains

Proof of work

The miner creats a new block using the proof of work mechanism outlined below

  1. When the miner decides to mine a new block, he does so with all the transactions in the transaction pool
  2. The initial block is a genesis block, and a new block is mined based on the following parameters:
    a. block of the last hash
    b. nonce
    c. hash of the current block
    d. timestamp
    e. difficulty
  1. The proof of work mechanism iteratively increases nonce and alters difficulty, until the number of leadings 0's in the block's hash are equal to the difficulty in the current iteration
  2. This is done to ensure that each block is added once every MINE_RATE of the blockchain
  3. The algorithm can be found in mine block

About

P2P payment system with a cryptocurrency wallet built on a blockchain like distributed ledger with shared transaction pool and shared blockchain across all nodes in the network

https://bitcoin.org/bitcoin.pdf

License:MIT License


Languages

Language:JavaScript 100.0%