BUILDING BLOCKCHAIN Revanth Kumar Co-director @ StartupGrind Senior Freelance R & D Engineer at Anvira Edustation Mobile: 7093-113-123 FB: revanth.kumar.connect | IGTV: rave.kumr | Twitter: TweetRevanth Step1: Create new block and test the block ● Write create new block method in blockchain.js Export the blockchain method to use it in test.js In dev folder run the command node <space> test.js CREATE NEW BLOCK WITH DUMMY DATA OUTPUT OF CREATE NEW BLOCK IS THIS – run the node test.js command again crosscheck ● Create more blocks with different values ● Create three different blocks Step 2: Get last block ● Returns last block in our block chain Step 3: create new transaction ● ● All new transactions are pending transactions until a new block is created. Change the param – new trsanscations to pending transactions ● They get recorded when a new block is created ● All new trs are pending trs ● ● Change the variable name to pending trs Return index of last block Step 4: Test create new block ● CREATE NEW BLOCK ● CREATE NEW TRANSACTION ● ● ● ● O/P should be one block created and one pending tr in the pendingTrs array NOTE: We did not create new block after creating transaction (new) Test output should be ● ● ● But the pending transaction is not added to a block Bcoz we did not create a new block after doing the transaction Now, create new block after creating new tr ● ● A new array is created in second block Now access the new array that is created and check Create new multiple trs ● Create multiple trs ● Create three blocks ● ● All the pending trs must be stored in last block chain Pending trs array must be empty Look at third block Hash block ● ● ● Take in a block and hash the block in to fixed length string Use hashing function SHA256: converts string in to random long digit string – very secured – change input then the entire op changes ● Show in realtime website ● Same hash for any specific string Search sha256 npm ● Be in blockchain directory ● Execute sha256 --save Create hash function ● Generate hash for blocks Creating hash func Testing hash Hash to be created ● Any part of data changes, ● The whole hash would change ● This adds lot of security Proof of work ● Very essential ● High security ● Make sure every block is legitimate ● Every time we create a new block, we need to make sure it is legitimate ● Takes current block data and previous block hash ● Get hash that have four zeros in beginning ● Its creates hash until you get specific string code Test proof of work Output nonce value Print all hash values until we see the actual value we need Proof of work should be very difficult to calculate check block is valid or not by using hadblock method and passing correct nonce Creating genesis block ● First block in a BC ● Use createnew block method Section 2 Building APIs to interact with BC create new file api.js Install expressjs ● npm install express --save Start server End point – 1 - blockchain Run api.js and see it is listening at port 3000 Making dev easy ● ● ● ● npm i nodemon –save --> in bc directory When ever we make change it will automatically restart the server Blockchain end point ● Get previously created blockchain by hitting the URL Transaction endpoint CHECK BC AGAIN – NEW TR CREATED Mining blocks via API Send bitcoins to whomever mines the blocks Install uuid for unique network nodes ● Npm i uuid –save ->> blockchain dir ● Hit blockchain endpoint in browser ● Localhost:3000/blockchain ● hit mine endpoint ● Check blockchain endpoint again ● Mine again ● Check block endpoint ● Repeat.... creating blocks..