I can actually look up how long I have by logging into my Coinbase account, looking at the history of the Bitcoin wallet, and watching this transaction I got back ter 2012 after signing up for Coinbase. Bitcoin wasgoed trading at about $6.50 vanaf. If I still had that 0.1 BTC, that’d be worth overheen $500 at the time of this writing. Te case people are wondering, I ended up selling that when a Bitcoin wasgoed worth $2000. So I only made $200 out of it rather than the $550 now. Should have held on.
Thank you Brian.
Despite knowing about Bitcoin’s existence, I never got much involved. I spotted the rises and falls of the $/BTC ratio. I’ve seen people talk about how much of the future it is, and seen a few articles about how pointless BTC is. I never had an opinion on that, only somewhat followed along.
Similarly, I have scarcely followed blockchains themselves. Recently, my dad has brought up numerous times how the CNBC and Bloomberg stations he observes ter the mornings bring up blockchains often, and he doesn’t know what it means at all.
And then abruptly, I figured I should attempt to learn about the blockchain more than the top level information I had. I embarked by doing a loterijlot of “research”, which means I would search all around the internet attempting to find other articles explaining the blockchain. Some were good, some were bad, some were dense, some were super upper level.
Reading only goes so far, and if there’s one thing I know, it’s that reading to learn doesn’t get you even close to the skill you get from programming to learn. So I figured I should go through and attempt to write my own basic local blockchain.
A big thing to mention here is that there are differences te a basic blockchain like I’m describing here and a ‘professional’ blockchain. This chain will not create a crypto currency. Blockchains do not require producing coins that can be traded and exchanged for physical money. Blockchains are used to store and verify information. Coins help incentive knots to participate te validation but don’t need to exist.
The reason I’m writing this postbode is 1) so people reading this can learn more about blockchains themselves, and Two) so I can attempt to learn more by explaining the code and not just writing it.
Te this postbode, I’ll showcase the way I want to store the blockchain gegevens and generate an initial block, how a knot can sync up with the local blockchain gegevens, how to display the blockchain (which will be used te the future to sync with other knots), and then how to go through and mine and create valid fresh blocks. For this very first postbode, there are no other knots. There are no wallets, no peers, no significant gegevens. Information on those will come zometeen.
Other Posts te This Series
If you don’t want to get into specifics and read the code, or if you came across this postbode while searching for an article that describes blockchains understandably, I’ll attempt to write a summary about how a blockchains work.
At a super high level, a blockchain is a database where everyone participating te the blockchain is able to store, view, confirm, and never delete the gegevens.
On a somewhat lower level, the gegevens ter thesis blocks can be anything spil long spil that specific blockchain permits it. For example, the gegevens te the Bitcoin blockchain is only transactions of Bitcoins inbetween accounts. The Ethereum blockchain permits similar transactions of Ether’s, but also transactions that are used to run code.
Slightly more downward, before a block is created and linked into the blockchain, it is validated by a majority of people working on the blockchain, referred to spil knots. The true blockchain is the chain containing the greatest number of blocks that is correctly verified by the majority of the knots. That means if a knot attempts to switch the gegevens te a previous block, the newer blocks will not be valid and knots will not trust the gegevens from the incorrect block.
Don’t worry if this is all confusing. It took mij a while to figure that out myself and a much longer time to be able to write this te a way that my sister (who has no background ter anything blockchain) understands.
If you want to look at the code, check out the part 1 branch on Github. Anyone with questions, comments, corrections, or praise (if you feel like being super nice!), get te voeling, or let mij know on twitter.
Step 1 – Classes and Files
Step 1 for mij is to write a class that treats the blocks when a knot is running. I’ll call this class Block . Frankly, there isn’t much to do with this class. Te the __init__ function, wij’re going to trust that all the required information is provided ter a dictionary. If I were writing a production blockchain, this wouldn’t be brainy, but it’s fine for the example where I’m the only one writing all the code. I also want to write a method that vleugelspeler out the significant block information into a dict, and then have a nicer way to display block information if I print a block to the terminal.
When wij’re looking to create a very first block, wij can run the elementary code.
Nice. The final question of this section is where to store the gegevens ter the verkeersopstopping system. Wij want this so wij don’t lose our local block gegevens if wij turn off the knot.
Ter an attempt to somewhat copy the Etherium Nevel folder scheme, I’m going to name the folder with the gegevens ‘chaindata’. Each block will be permitted its own verkeersopstopping for now where it’s named based on its index. Wij need to make sure that the filename commences with slew of leading zeros so the blocks are te numerical order.
With the code above, this is what I need to create the very first block.
Step Two – Syncing the blockchain, locally
When you embark a knot, before you’re able to begin mining, interpreting the gegevens, or send / create fresh gegevens for the chain, you need to sync the knot. Since there are no other knots, I’m only talking about reading the blocks from the local files. Te the future, reading from files will be part of syncing, but also talking to peers to gather the blocks that were generated while you weren’t running your own knot.
Nice and plain, for now. Reading strings from a folder and loading them into gegevens structures doesn’t require super complicated code. For now this works. But ter future posts when I write the capability for different knots to communicate, this sync function is going to get a loterijlot more complicated.
Step Trio – Displaying the blockchain
Now that wij have the blockchain ter memory, I want to begin being able to voorstelling the chain ter a browser. Two reasons for doing this now. Very first is to validate ter a browser that things have switched. And then also I’ll want to use the browser te the future to view and act on the blockchain. Like sending transactions or managing wallets.
I use Flask here since it’s impressively effortless to embark, and also since I’m ter control.
Here’s the code to voorstelling the blockchain json. I’ll overlook the invoer requirements to save space here.
Run this code, visit localhost:3000/blockchain.json, and you’ll see the current blocks drool out.
Part Four – “Mining”, also known spil block creation
Wij only have that one genesis block, and if wij have more gegevens wij want to store and distribute, wij need a way to include that into a fresh block. The question is how to create a fresh block while linking back to a previous one.
Te the Bitcoin whitepaper, Satoshi describes it spil the following. Note that ‘timestamp server’ is referred to spil a ‘knot’:
The solution wij propose embarks with a timestamp server. A timestamp server works by taking a hash of a block of items to be timestamped and widely publishing the hash.. . The timestamp proves that the gegevens vereiste have existed at the time, obviously, te order to get into the hash. Each timestamp includes the previous timestamp ter its hash, forming a chain, with each extra timestamp reinforcing the ones before it.
Here’s a screenshot of the picture below the description.
A summary of that section is that te order to verbinding the blocks together, wij create a hash of the information of a fresh block that includes the time of block creation, the hash of the previous block, and the information te the block. I’ll refer to this group of information spil the block’s ‘header’. Ter this way, wij’re able to verify a block’s truthfulness by running through all the hashes before a block and validating the sequence.
For my header the case here the header I’m creating is adding the string values together into a giant string. The gegevens I’m including is:
- Index, meaning which number of block this will be
- Previous block’s hash
- the gegevens, te this case is just random strings. For bitcoin, this is referred to spil the Merkle root, which is informatie about the transactions
- The timestamp of when wij’re mining the block
Before getting confused, adding the strings of information together isn’t required to create a header. The requirement is that everyone knows how to generate a block’s header, and within the header is the previous block’s hash. This is so everyone can confirm the juist hash for the fresh block, and validate the verbinding inbetween the two blocks .
The Bitcoin header is much more ingewikkeld than combining strings. It uses hashes of gegevens, times, and deals with how the bytes are stored ter pc memory. But for now, adding strings suffices.
Once wij have the header, wij want to go through and calculate the validated hash, and by calculating the hash. Te my hash calculation, I’m going to be doing something slightly different than Bitcoin’s method, but I’m still running the block header through the sha256 function.
Eventually, to mine the block wij use the functions above to get a hash for the fresh block, store the hash te the fresh block, and then save that block to the chaindata directory.
Tada! Tho’ with this type of block creation, whoever has the fastest CPU is able to create a chain that’s the longest which other knots would conceive spil true. Wij need some way to slow down block creation and confirm each other before moving towards the next block.
Part Five – Proof-of-Work
Ter order to do the slowdown, I’m throwing ter Proof-of-Work spil Bitcoin does. Proof-of-Stake is another way you’ll see blockchains use to get overeenstemming, but for this I’ll go with work.
The way to do this is to adjust the requirement that a block’s hash has certain properties. Like bitcoin, I’m going to make sure that the hash embarks with a certain number of zeros before you can budge on to the next one. The way to do this is to throw on one more lump of information into the header – a nonce.
Now the mining function is adjusted to create the hash, but if the block’s hash doesn’t lead with enough zeros, wij increment the nonce value, create the fresh header, calculate the fresh hash and check to see if that leads with enough zeros.
Excellent. This fresh block contains the valid nonce value so other knots can validate the hash. Wij can generate, save, and distribute this fresh block to the surplus.
And that’s it! For now. There are tons of questions and features for this blockchain that I toevluchthaven’t included.
For example, how do other knots become involved? How would knots transfer gegevens that they want included te a block? How do wij store the information ter the block other than just a giant string Is there a better type of header that doesn’t include that giant gegevens string?
There will be more parts of the series coming where I’ll stir forward with solving thesis questions. So if you have suggestions of what parts you want to see, let mij know on twitter, comment on this postbode, or get te voeling!
Thanks to my sister Sara for reading through this for edits, and asking questions about blockchains so I had to rewrite to clarify.