W3LCOM3 TO

W3LCOM3 TO
           ╔╦╗┬ ┬┌─┐     
            ║ ├─┤├┤      
            ╩ ┴ ┴└─┘ 
      ╔╗ ╦╔═╗╦  ╦╦  ╦╔═╗
      ╠╩╗║║ ║║  ║╚╗╔╝║╣ 
      ╚═╝╩╚═╝╩═╝╩ ╚╝ ╚═╝

      

Sunday, November 24, 2019

Create a simple Blockchain in Go

Better way to understand blockchain is to create own!

At the end you will able to:-
# create your own block chain.
# understand how hashing works in maintaining integrity of the block chain.
# see how new block get added.
# see how tiebreaker get resolved when multiple nodes generate block.
# view your blockchain in web browser.
# write new block.
# get foundational understanding of the blockchain.

Let's GO 👊

Setup
          After installing and configuring Go, grab the following packages

          go get github.com/davecgh/go-spew/spew
          go-spew/spew

Spew allow us to view structs and slices cleanly formatted in our console.Then grab...

          go get github.com/joho/godotenv
          godotenv

Godotenv let us read from .env file that we keep in the root of our directory.

Let's create a .env file in the riot of our directory defining the port that will serve http requests.
Just add one line to this file:
          PORT=8080

Create a main.go file. Everything from now on will be written to this file.

Import
     Here are the imports we'll need, along with our package declaration. Let's write these to main.go
Data model
     Let's define the struct of each of our blocks that will make up the blockchain.

Each Block contains data that will be written to the to the blockchain, and represents each case when you took your pulse rate.

*Index is the position of the data record in the blockchain
*Timestamp is automatically determined and is the time the data is written
*BPM or beats per minute, is your pulse rate
*Hash is a SHA256 identifier representing this data record
*PrevHash is the SHA256 identifier of the previous record in the chain

Let's also model out the blockchain iself, which is simply a slice of Block:

So how does hashing fit into blocks and blockchain? We use hashes to identify and keep the block in the right order. By ensuring the PrevHash in each Block is identical to Hash in the previous Block we know the proper order of the block that make up the chain.

Hashing and Generating New Blocks

So why do we need hashing? We hash data for 2 main reasons:

(1) To save space. Hashes are derived from all the data that is on the block. In our case, we only have a few data points but imagine we have data from hundreds, thousands or millions of previous blocks. It’s much more efficient to hash that data into a single SHA256 string or hash the hashes than to copy all the data in preceding blocks over and over again.
(2) Preserve integrity of the blockchain. By storing previous hashes like we do in the diagram above, we’re able to ensure the blocks in the blockchain are in the right order. If a malicious party were to come in and try to manipulate the data (for example, to change our heart rate to fix life insurance prices), the hashes would change quickly and the chain would “break”, and everyone would know to not trust that malicious chain.

Let’s write a function that takes our Block data and creates a SHA256 hash of it.

This calculateHash function concatenates Index, Timestamp, BPM, PrevHash of the Block we provide as an argument and returns the SHA256 hash as a string. Now we can generate a new Block with all the elements we need with a new generateBlock function. We’ll need to supply it the previous block so we can get its hash and our pulse rate in BPM. Don’t worry about the BPM int argument that’s passed in. We’ll address that later.
Notice that the current time is automatically written in the block with time.Now(). Also notice that our prior calculateHash function was called. PrevHash is copied over from the hash of the previous block. Index is incremented from the Index of the previous block.

Block Validation

Now we need to write some functions to make sure the blocks haven’t been tampered with. We do this by checking Index to make sure they’ve incremented as expected. We also check to make sure our PrevHash is indeed the same as the Hash of the previous block. Lastly, we want to double check the hash of the current block by running the calculateHash function again on the current block. Let’s write a isBlockValid function that does all these things and returns a bool. It’ll return true if it passes all our checks:
What if we run into an issue where two nodes of our blockchain ecosystem both added blocks to their chains and we received them both. Which one do we pick as the source of truth? We choose the longest chain. This is a classic blockchain issue and has nothing to do with nefarious actors. Two well meaning nodes may simply have different chain lengths, so naturally the longer one will be the most up to date and have the latest blocks. So let’s make sure the new chain we’re taking in is longer than the current chain we have. If it is, we can overwrite our chain with the new one that has the new block(s).

No comments: