Week 3: OOP And Proof Of Stake
April 14, 2023
Hello everybody, and welcome back to my blog!
My third week has been a continuation of my coding project. As usual, you can watch the evolution of my code here. This week, my code has been focused on first modularizing the code using classes and inheritance, which I will describe, and, more importantly, implementing the proof of stake consensus mechanism I have been modularizing my code using a type of coding conduct/etiquette called Object Oriented Programming (OOP).
This is an approach to programming that is centered around objects and classes. You can think about these classes as blueprints of code that contain certain functionality, but they are only a blueprint. When you instantiate a class, that is to make a functional instance of, they are then called objects. Consider a class that is meant to represent a dog. The class could have attributes (or variables) that track the dog’s color, height, hunger, etc. The class could also contain functions such as feeding the dog, washing it, etc. However, these are simply empty variables that represent data. Once we instantiate the dog class and make it into an object we could then fill in these attributes. These classes can then be expanded upon using inheritance. We could inherit all the variables and functions of the general dog class blueprint and extend functionality, say, for a specific dog breed. This approach to programming is very useful in my project because I am able to make both a general block class and a blockchain class. These classes would contain the functions that I need all blockchains to have, for example, each blockchain needs to have a dictionary that records all blocks added to the chain as I talked about in my previous blogs. Then I would use programming inheritance to build a proof of work blockchain class that has the same functions a general blockchain would have plus a few extras like a function to mine a block until you find a proper hash and then add that block to the chain. I have tended to avoid OOP in my coding career because it can be quite intricate, but this is a perfect use case for it so it’s been wonderful for expanding my skills. You can learn more about OOP for non-developers here.
The most important part of my work this week has been implementing the proof of stake consensus algorithm. Proof of stake is an alternative to proof of work. Instead of computers proving their commitment to the chain by doing work through computational input to add blocks like in proof of work, proof of stake works by users staking (or putting up as collateral) their crypto tokens and in turn getting the chance of being chosen to be a block approver who verifies the information within a block and adds it to chain. Stakers, those who stake their coins, are chosen in a sort of weighted lottery. The more coins your stake the better chance you have of being chosen to verify block info. The key, like in any proof system, is that by purchasing and staking your coins you have inputted capital into the chain and have an incentive not to lie about any information. Again, complicated stuff: learn more here.