$ node dev/test.js
One block has:
- index (block number)
- timestamp
- pending transactions (wait for verify). It will be pushed into next new block
- nonce (proof)
- hash
- previous block's hash
Genesic block does not have hash and previous block's hash
Using sha256 to generate block's hash
Proof of Work:
- Repeat hash block until it finds correct hash => '0000WRKWER9803KJLJBA'
- Use previousBlockHash, currentBlockData and nonce for generate hash
- Continue changes nonce until it finds correct hash
- Return nonce value that creates the correct hash
npm i express
$ node dev/api.js
http://localhost:3000/
nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.
npm i express
"start": "nodemon --watch dev -e js dev/api.js"
- watch any file changes in dev folder
- e keep an eyes file is javascript (.js)
- automatically restarting dev/api.js file
npm start
Node.js body parsing middleware.
Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
npm i body-parser
APIs
- GET/ blockchain: get entire blockchain
- POST/ transaction: receive new transaction information and add to pendingTransactions array
- GET/ mine: mining a new block
Test
- Run web to mine new block: http://localhost:3000/mine
- Run http://localhost:3000/blockchain to check add new block into blockchain
- Run postman POST/ transaction add some new pending transasction
- Run /blockchain to see new pending transaction
- Run mine to see new pending transaction is pushed into new block
Summary
- Create some new transaction -> add into pending transaction
- Mine new block -> some pending transaction will be pushed into new block
- Check entire blockchain -> new block will be pushed into chain and pendingTransaction array is empty
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"node_1": "nodemon --watch dev -e js dev/networkNode.js 3001 http://localhost:3001/transaction",
"node_2": "nodemon --watch dev -e js dev/networkNode.js 3002 http://localhost:3002/transaction",
"node_3": "nodemon --watch dev -e js dev/networkNode.js 3003 http://localhost:3003/transaction",
"node_4": "nodemon --watch dev -e js dev/networkNode.js 3004 http://localhost:3004/transaction",
"node_5": "nodemon --watch dev -e js dev/networkNode.js 3005 http://localhost:3005/transaction"
},
Get argument port in javascript at index 2
const port = process.argv[2]
Run in multi cmd windows
npm run node_1
npm run node_2
npm run node_3
...
Create new pending transaction at Postman in node 1 (http://localhost:3001/transaction), node 3 (http://localhost:3003/transaction)
These transactions is in private node, must decentralized by every node aware all transaction of all node
The simplified HTTP request client 'request' with Promise support. Powered by Bluebird.
npm install request --save
Existing node: Node 1
, Node 2
, Node 4
New Node: Node 3
Node 1
callsregister-and-broadcast-node
for newNode 3
(Node 1
has awared about newNode 3
)Node 2
andNode 4
callsregister-node
for awaring about newNode 3
- After all,
Node 3
callsregister-nodes-bulk
for awaring all existing nodes in network
All request options and uri is built in
Node 1
All existing nodes need to aware new pending transaction and new mine block
- Aware new pending transaction
- Current node add new transaction into pending transaction array
- Then current node will broadcast new transaction to all existing nodes in network
- All existing nodes will call
/transaction
POST to add new transation into pending transaction array - All nodes have same new pending transaction datas.
- Aware new block is mined
- Node will create new block if it finds correct hash (proof of Work)
- Then node will request to all network nodes for receiving new block
- All existing nodes will call
/receive-new-block
to push new block intothis.chain
- Then node winner will call
/transaction/broadcast
to broadcast for all network nodes about mining reward transaction to current node Address. But this transaction is into pending transaction array (memory pool) - This transaction will be added
transactions
array into new another block
Consensus
is a way for all of the nodes inside of our network to agree upon what the correct data inside of the blockchain is.
So, for example, in the real world, when a block chain is totally built out, it is running across hundreds or thousands of nodes and every transaction and every block that's being created, it's all broadcast to the entire block chain network
.
And there's a possibility that during these broadcasts, a hiccup could occur and maybe a certain node doesn't receive a piece of information
or a transaction that took place.
Or even maybe there is a bad actor inside of the block chain network
who is sending out false information
or creating fraudulent transactions
on their copy of the block chain and trying to broadcast them to the whole network and convince everybody that they are legitimate transactions.
So how do we solve for this problem?
Our consensus algorithm will provide us with a way to compare one node to all the other nodes inside of the network
to confirm that we have the correct data on the specific node.
We are going to create a consensus algorithm that implements the longest chain rule
.
So what is this longest chain rule?
It will simply compare the chain on the chosen node with all the other chains inside of our network, and if one of the other chains has a longer length than the current node that we're on
, then we are simply going to replace the chain of the node we are on with the longest chain in the network
.
The longest chain has the most blocks
in it and each of those blocks was mined by using a proof of work
.
Check chain is valid
- Check each block has
hash
begin with0000
- Check
previousBlockHash
in current block is equal withhash of previous block
- Check data of
genesic block
is correct
Test
- Create test data (some block, some transactions)
- Paste test data in dev/test.js
- Run
node dev/test.js
- Attemp change data of
hash
,address of recipient
,genesic block
- Run test again
node dev/test.js
Check consensus
- Node 1, 2, 3, 4 are awaring in
networkNodes
- Mine some block and create some transactions
- Synchonize all datas in network
- Register and broadcast Node 5 in network
- Run
/consensus
to update blockchain in Node 5 - Run
/consensus
again, this chain of Node 5 has not been replaced
- Write
getBlock()
receivesblockHash
param to return correct block - Write
getTransaction()
receivestransactionId
param to return correct transaction and correct block - Write
getAddressData()
receivesaddress
param to return transactions and balace of this address
Get Block by hash
Get Transaction by Id
Get address data by address
Front End
- Get block
- Get transactions
- Get address's data