Blockchain
As part of the Financial Engineering capstone project, I built a basic blockchain and illustrate its impact on the financial sector. I tried to clone the basic technology behind Bitcoin: the nodes network, Byzantine Fault Tolerance (BFT), Consensus.
Technology is playing a major role in advancing the financial sector. Rather than a black box, blockchain and other technologies must be understood in order to recognize how it works and how it affects our Decision-Making process.
You can find the project report in french: Blockchain the new era of Finance
Installation & Prerequisites
Make sure you have Node.js & Postman installed in your computer to follow the guide.
Then download the files, extract the files at a directory blockchain, and install the dependencies.
$ git clone https://github.com/unes07/Blockchain.git
$ cd blockchain
$ npm install
Guide
Setup Nodes
If you take a look at the package.json file you will find the node configurations at the scripts. There's 5 nodes you can add other ones or delete some.
To run a node, run the command below at the terminal. At each terminal run a node. for the five nodes
<addr>
x: number of the node, like run node_1
$ npm run node_x
You can visit at your browser for seeing node 1 http://localhost:3001/blockchain, and change the 1 by the numbers of the nodes [2:5].
Network
The blockchain network will make the nodes connected & will share data together.
To create the network:
- Open Postman
- Change to POST request
- Click on body
- Then raw
- Make sure is JSON
- Enter the following JSON script:
{
"newNodeUrl": "http://localhost:3002"
}
- Then make a POST request to
http://localhost:3001/register-and-broadcast-node
-By making this request the second node and the first one are now connected you can check your browser to verfiy the Node Url of the seconde node is added at the networkNodes array of the first node.
Now you can added the other nodes to the network by yourself, following the same steps changing just the body of the request: change the Node Url (number).
-Note: we make a request to the first node but you can make it to any of the running nodes.
Now you network is ready we can start making some transactions.
Transactions
To make a transaction:
- Open Postman
- Change to POST request
- Click on body
- Then raw
- Make sure is JSON
- Enter the following JSON script:
{
"amount": 14,
"sender": "JEAKJDBSCSDBQSHBCHQB42",
"recipient": "FCSDBQSHBCHQB4254JHGH56"
}
- amount: the amount of the transaction
- sender: the sender public key, for our case is a random Id
- recipient: the recipient public key, for our case is a random Id
- Then make a POST request to
http://localhost:3001/transaction/broadcast
-Note: you can send the request for any of the nodes, the consensus algorithm will broadcast the transaction to all the other nodes at the network.
-You can make as many transactions as you want.
The transaction is in pendingTransactions, to add this block of transactions to the chain it should be mined.
Mining
To mine a block you should make a request to /mine to any of the running nodes. for example:
http://localhost:3001/mine
If you check now the blockchain API page http://localhost:3001/mine you will notice another transaction of the amount of 12 is being added to the pendingTransactions sent by "00" to a random Id. This is the reward of the miner sent by the blockchain to the miner wallet (public key).
Consensus
In our guide, we added all the nodes to the network before adding any of the data to the blockchain, but if a node joins later the network will not have the previous data. for that he should send a request to /consensus to get the right data:
http://localhost:3001/consensus
Block Explorer
If you visited http://localhost:3001/block-explorer you will find an explorer of your blockchain.
you can search by:
- a Block Hash
- a Transaction Id
- an Address¹
Address¹: is the wallet Id of a sender or a recipient
You can find your data at your API, the enter it at the first input, then choose from the dropdown the type of the search. An example of a searsh by Block Hash