Block-Chain (part 2)

袁晗 | Luo, Yuan Han
4 min readSep 10, 2021
img source: https://www.linkedin.com/pulse/keeping-cool-over-bitcoin-how-crypto-miners-moving-when-beresford/

Ok, let’s finish up Block-Chain by picking up where we left off last time: Blockchain and Transaction. If you are new to programming or did not read the part 1, I suggest you to go over it. As a refresher, last time we left at how the method mineBlock provides security to Block-Chain. And I told you I will discuss mineBlock’s parameter “difficulty” in part 2 so lets dive right in.

I always think the best way to understand a class is to look at its constructor first. In the case of Blockchain, I want to bring your focus to “difficulty” first, I will go over the others momentarily. This is where we set how much computational power, hence time, it will take to mine a block. Cool, we found out where we set difficulty, let’s find how this single parameter can drive the most advanced computers to a melting point. Recall the while loop from mineBlock method in the Block class:

The substring method starts from the 0th index of the hash and grabs “difficulty” numbers of digits. In our case the “difficulty” is 1, which means we grab the first digit of the hash. The Array class creates an array of 1+“difficulty” number of empty elements inside. In this case, it’s 2, hence [1, 2]. And the “join(“0”)” method combines the empty arrays with “0”. Since the array is empty, all we see is what the “join” method joins the array with, “0”. To generate a hash with 1 leading zero isn’t that hard, but the computing power and time increase dramatically if we set to 2. This is proof-of-work. Now with that in the bag lets talk about the rest of the constructor.

If I wasn’t preaching to the void, you must realize there is a paradox: if the formation of every hash requires the previous hash, then there can never be the 1st hash, because it will always require the hash before that. This is where the “createGenesisBlock” method comes in.

If you never liked programming, I hope this will change your mind. We literally end the century old question of whether chicken or egg came first by one line of code: we just invented the first block with current date, empty array, and 0. That’s right, you are looking at the first block, with its previous hash as, 0, because we said so, move on.

pendingTransaction is an array of the class “Transaction”.

This class along with the “minedReward” object variable is very self-explanatory, so I won’t go into it. What you have to remember is that a block does not contain only 1 transaction. Rather, it consist of many transactions. The quantity is subjective and flexible. So let’s move on to the most important method of the Blockchain class: “minePendingTransaction”

The only method I have not go over in this snip is “getLastBlock”, because it’s not exciting at all: it just returns the last block of the chain. This method is very straight forward, it uses the “Block” class to construct a new block by passing in date, pending transactions, last block hash. Then it uses the “mineBlock” method in the “Block” class to go through proof of work. And after it’s done, it pushes the block into the object variable “chain” within the Blockchain class. To finish, we empty out the pendingTransactions and call it a day!

Conclusion

I hope you can see that the idea of Block-Chain isn’t complicated, its like a English word play for nerds. Except, instead of words, programming geeks use classes to interact with each other. I have a link down below to my github repository for you to test it out. However, I did shove the Block-Chain code within the react-redux architecture, so you will have to npm install a bunch of things to make it work. If you just want to go straight to the codes, the files are inside back_end_logic file, path: blockChain/src/back_end_logic/. I am also planning on writing a series about react/redux architecture, so stay tuned if you are interested.

--

--