The Managers Guide to Coding on the Blockchain — Part I

This article was originally published as part of a multi-part Medium series and has been modified slightly to suit this medium.

This is my journey of discovering how to ‘do’ again. I’m a recovering manager who lost his engineering roots and found himself in the seedy underbelly of the world of people managers (Don’t get me wrong, I had a wonderful team. In fact they have been a large part of the inspiration to leave a great job and rediscovering how to do). Having left my job as a director at a Silicon Valley Fortune 500 company, I decided to take some time out to get back into coding. This is intended to be a brain dump of my day to day experiments, frustrations, wins and hopefully progress as I endeavor to teach myself javascript, node, jQuery, blockchains, Ethereum, solidity and anything else that gets in my way. I’m not entirely sure where I’m going or what the end goal looks like but I figure it’ll start to become clear as we go along.


Where to start

“Wait, is this a wheat beer, I can’t drink this without a lemon” — Dinesh, Silicon Valley

For anyone reading this who is still getting their feet under them when it comes to Blockchain, cryptocurrencies, smart contracts, ICOs, it can feel a little daunting separating the important stuff from the noise, and its a noisy noisy world out there right now when it comes to cryptocurrencies.

There are now over 1,000 different cryptocurrencies, altcoins and tokens out there and every day it feels like some new company is raising millions of dollars in an ICO for a new token.

When faced with a seemingly insurmountable amount of information the only way to proceed is to chunk it down into more manageable bites and start from there. For me, I’ve decided to start with Ethereum and focus on smart contracts. I’ll go a little more into what Ethereum and smart contracts are a little later but in a nutshell I chose this for 3 main reasons 1) Ethereum lets you build apps (distributed applications or dApps) on top of a blockchain including but not limited to new cryptocurrencies 2) Even though Ethereum isn’t the only option its arguably the most mature and already has a reasonably strong developer ecosystem 3) Smart contracts allow you to experiment easily with a variety of different approaches to using blockchains.

But, before we start to get under the hood on blockchain and get our hands dirty, its worth having a little context around what bitcoin is, how it relates to blockchain and why any of this is worth even thinking about given all the other exciting new technologies that are currently fighting for mindshare.

I’d like to address any core crypto geeks who have been in this space since the beginning that this is not intended for you. I fully acknowledge that Bitcoin has been around for years and there are complexities and philosophies going on behind the scenes and under the hood that I will breeze right past. At times I may generalize and simplify, but I’m not doing that in an attempt to downplay the subtletees and nuances but rather to address this to a more mainstream audience.


The Birth of Bitcoin

Lets start with Bitcoin as it’s the most well known. Bitcoin came to be “popularized” by the media in large part thanks to its use on silk road for various illegal activities… because we all love scandalous stories that sound like they’re right out of a movie. Who doesn’t want to hear about a new sub-culture of “un-tracable” digital currency being used to purchase contracts with professional killers. It’s right out of John Wick. Note I put un-tracable in quotes as Bitcoin is in fact very tracable, and is actually designed to be so, thanks specifically to the underlying blockchain technology which we will get to later, however the hype and mis-information surrounding bitcoin has led many people to believe its a fully anonymous digital currency.

Who doesn’t want to hear about a new sub-culture of “un-tracable” digital currency being used to purchase contracts with professional killers

In any case, think of Bitcoin as an application sitting on top of a new web platform called blockchain. Blockchain technology is not a cryptocurrency but instead a mechanism for sharing information between two parties in a secure way without the risk of double spending. What this essentailly means is that if I give you something that I have, at the end of the transaction you should now have it and I shouldn’t. This type of transaction could relate to all sorts of things including money. This was previously difficult to do without a trusted third party and is even more difficult when dealing with purely digital artifacts or information. Lets imagine a situation where I have created a new digital currency and I now hold 10 dollars of this new currency. Who will guarantee that when I spend those 10 dollars or transfer some of those dollars to you that I’m not just duplicating them and giving you a copy. The way that is currrently handled in the real world is that a third party such as PayPal, the bank, or some service is handling both sides of the transaction and keep track of who has what. If I send $10 with PayPal to a friend, PayPal debits that amount from my account and credits it to my friend. This works well in practice, but it does require one authority to hold all of the information about everyone. This does actually become a problem as more and more highly sensitive information is being stored in these central servers. Just within the last couple of months we saw Equifax security get breached and over 145 million accounts get compromised including social security numbers, which in America at least is tied to almost every major transaction we make. The entire US economy seems to revolve around your credit score which is tied to your social security number so this one security breach on its own is fairly devastaing.


What’s a Blockchain?

Remember I said previously that PayPal keeps track of each user’s balance and records who owes what to who. That’s just a giant accounting ledger and at the heart of blockchain, that’s just what it is. Except in the case of blockchain, the ledger is stored out in the open for everyone to see and instead of one party owning or holding the ledger, many copies of it are shared out across the web and continuously synchronized with each other. Each account is debited and credited out in the open. Now, clearly people wouldn’t be happy if I could just go onto the web and look up your bank account and see how much much you have and what you spend your money on, so instead each account is represented as a string of numbers and letters, which is your account ID.

It’s often easy to get confused at this point and be lead to thinking that there is one universal blockchain. Blockchain is just a concept, but it can and has been implemented in a variety of ways for the various applications that are starting to come to market. Some blockchains are public and others are private and not visible to the public. A company or a group of companies for example may decide that transacting between them could be done more efficiently using a blockchain but have no intention of letting that data be accessible to the outside world. Bitcoin has a public blockchain, Ethereum has a different public blockchain. You can think of it a bit like cloud computing. Even though we talk about “the cloud” there isn’t really one cloud but rather different companies have instances of remote hosted servers that implement the concept of cloud based computing and do so in a variety of different ways. Amazon’s hosted service is different to Google’s hosted service and each have their own independent set of APIs to access it.

Blockchain is similar in that bitcoin’s blockchain and Ethereum’s blockchain are both publicly accessible via APIs but each API is unique and implements slightly different functionality. Some companies are now working on various methods to bridge between different blockchains.

At it’s simplest, in order to conduct a transaction on a blockchain you just make a couple of simple API calls just as you would if you were accessing any online web service. If you are transferring money on bitcoin, you will use your private key (more on that later) to sign the transaction and provide the ID of the account that the bitcoin is being transferred to.

The big difference between the cloud computing model and a blockchain is that with cloud computing the database is stored on servers controlled by an entity (such as Google or Facebook), whereas in blockchain the database (the blockchain) is stored and duplicated on nodes that are hosted by anyone. Its a fully decentralized network of interconnected nodes. This means that the data has redundant backup through duplication and no single point of failure as there could many thousands or tens of thousands of nodes hosting the data. It also helps to prevent against attacks like the one on Equifax as the data is public by default and encrypted.

Blockchain isn’t a replacement for centralized architectures as there are also tradeoffs that have to be made, however for some specific applications and for some applications that couldn’t previously even exist using a centralized architecture, a blockchain may be the better solution.


Picks and Shovels

Now comes the interesting and unique part of blockchains. Each of these transactions needs to be processed, which in the past has been done by the bank or PayPal or some other intermemdiary. So, who processes the transaction and how can we eliminate the intermediary party? This is where the concept of mining comes in and by now most people have at least heard of bitcoin mining, however many people either don’t understand what it is or don’t really understand how it works. Miners are just a community of people who are willing to commit their compute power to process the transactions. Each transaction is broadcast to the network of miners and each miner choses a group of recent transactions to include in a block of transactions that they hope to process. Now comes the slightly complicated part. In order to add a block to the public record and make it official, the miner needs to complete a cryptographic puzzle and have it validated by the community of miners. This cryptographic puzzle is built around computing the hash of some value. This just means that there is a function that takes some input number and always generates the exact same output, however there isn’t any known way of figuring out what the input number was given the output. In the case of the cryptographic puzzle, the miners are actually asked to do just that, figure out the input given the output. The only way to do this is by brute force trial and error. They simply try millions of numbers until they find an input number that produces the given output. It’s simplified for them a little bit because they don’t have to find the exact match, they only need to match the first 10 or 11 digits of the output number, which in the case of bitcoin has to be all zeros. So each miner is furiously generating numbers (which is referred to as hashes/second) until they generate an output with, say 10 leading zeros (but they don’t have to care what the numbers after the zeros are). The algorithm defining Bitcoin’s blockchain can automatically adjust the number of zeros that need to be matched to adjust the difficulty of finding a solution. This way depending on how many miners are trying to solve the problems the blockchain can self-regulate to keep the number of blocks produced per minute at a fairly constant rate. After the miner finally figures out a number that creates the right match they can get it validated by the community of miners and once validated it is officially added to the blockchain.

You may be still wondering why its called a chain. The number that the miners are trying to guess is actually made up of three parts: 1) a number that is a hash of the data contained in the current block that the miner is trying to add 2) the hash of the most recent block that was added to the blockchain 3) a nonce, which is just a fancy way of saying the number that the miner needs to guess. Those 3 numbers are added together and the hash of the sum of those numbers is the what needs to produce the output with a sequence of zeroes. In this way each block is linked to the block before it going all the way back to the first block in the chain, hence a block-chain.

In order to change a transaction in any one block in the chain, you would be changing the data contained in that block, which means that the hash value of that block would change, which means that the sum of the three numbers would have changed, which means that the hash would no longer output to a number with the correct number of zeroes. You would need to calculate a new nonce in order to make the output correct. The problem is that the next block in the chain uses the hash of the curent block in its calculation so you would need to update that block’s nonce as well and as you can see it carries all the way down the chain. In order to change any one block you would have to calcualte a new nonce for every single block following it until the most recent block. Each time you do that you have to guess that number again by brute force, but simultaneously the blockchain is continuing to expand one block at a time. This means you would need more compute power than the rest of the network as you would need to compute nonces at a rate many many many times faster than the current fastest miner who’s just trying to only calculate the next block. This is the very very simplified version of the theory behind blockchains and why they are considered immutable or unchangable and hence a reliable record of transactions.

But this still doesn’t answer the questions of why we call them Miners or even why these Miners would be interested in spending their time and resources on solving these cryptographic puzzles. Every transaction can, but doesn’t have to, include a fee. This fee is intended to be a small incentive for the miner to include the transaction in their block. The miner gets to keep the fee from each of the transactions within the block if their block is validated to be included into the blockchain. The larger the fee the more likely they will be to add the transaction but, at least in theory, even transactions without fees will be included eventually but may have a small delay, usually around a couple of hours at most.Basically its a pay for service type of model. If you want it done instantly then you pay for it. It’s an interesting alternative to the usual percentage based fee model of banks. With bitcoin, you could transfer a million dollars worth of bitcoin from one side of the world to the other without paying any fee at all, so long as you’re aren’t too worried about when it will arrive.

The reason, however, that Miners are called as such, is actually becuase of an additional compensation mechanism. Every time a Miner has one of their blocks approved for the blockchain, they get a reward in the form of bitcoin. The difference is that this bitcoin is actually created from nowhere. This might seem odd at first until you realize that this is not so different to what the Federal Reserve does when they adjust the money supply to prevent inflation getting out of control. Another way of thinking about this is to relate it to the process of mining gold. We know there is a fixed amount of gold on or within planet earth. We don’t know exactly how much but we know that the longer we mine it the harder it will be to find the remaining gold. Because there is a limited supply of gold it has become a good store of value because we know that its impossible for someone to just create more gold out of thin air so if demand for it increases so will the price that someone is willing to pay for it.

The algorithm behind Bitcoin’s blockchain has capped the eventual supply of Bitcoin at 22 Million coins (which will take around 150 years to reach). Everytime a block is validated, new Bitcoin is released into the market from this fixed eventual supply. Early on in the history of Bitcoin the reward was high and it was relatively easy to validate blocks so Bitcoin was being released at a faster rate. Over time that rate has slowed down and therefore the reward per block is lower. Therefore, each miner is “doing work” (to solve the cryptographic problem) in order to “mine” (process transactions and create new blocks) and some of these miners will “find Bitcoin” (new Bitcoin is released from a fixed supply, just like a piece of gold being discovered that was previously trapped underground).

The Mining example above is specific to Bitcoin, however Miners can decide what they want to apply their compute resources to and could chose to mine Bitcoin, Ethereum, ZCash or one of the many other currencies. Of course, they will likely mine where they feel they can make the most money. Value of the currency and difficulty in solving the cryptographic problem are two key factors in determining what to mine.

New methods for validating blockchains are also in development that move away from the “proof of work” model above. One of the more popular alternatives is “proof of stake” but possibly we can get into that in another post.


Keys to your own Kingdom

The account ID for a blockchain account, mentioned earlier, is often referred to (slightly incorrectly) as the public key of the account. Public/Private keys are part of the science of public key cryptography otherwise known as asymmetric cryptography. This approach is not unique to Blockchain and in fact is used widely throughout the web — you’re most likely using it now on this webpage through “https://medium.com” (secure HTTP). I won’t go into the details behind Asymmetric cryptography but the nuts and bolts of it is based around having a public key (that can be shared publicly) and a private key (that can never be shared and is held under close guard). A message can be encrypted using the private key or the public key and then decrypted using the opposite key. In this way you could supply a public key to someone and ask them to encrypt a message and send it to you. The only person that can decrypt that message is anyone holding the private key (hence why its important to never let it out). In much the same way, you can think of the account ID similar to your public key (basically a shortened version of the public key) and you have a matching private key that you can use to prove ownership of the account and anything contained within.

In order to keep track of your cryptocurrency most people use a cryptocurrency wallet. There are many different wallets, some purely web based, some desktop or mobile apps and others phsyical secure memory storage that usually connects by USB. Regardless of the type of wallet, they all essentially serve the same function to hold your private key safe. As a result of holding your private key safe they also can access your account and show your current balance and provide a simple interface for conducting transactions like sending and receiving cryptocurrency. Despite having many shared features with a bank account, a wallet doesn’t actually store any cryptocurrency. It simply provides an interface for you to view the blockchain and, using your private key, access the data on the blockchain related to your specific private key. You may have heard stories of people losing hard drives containing thousands of bitcoins that would now be worth millions. The harddrive doesn’t actually contain any bitcoins, in fact all of those bitcoins still exist on the blockchain, but no one will ever be able to access them because the only private key was stored on that hard drive that got lost. This is why its so important to keep your private key safe and backed up in a safe way.

This might sound a little alarming that you could lose millions of dollars just by misplacing a single number and you would be right. However, newer wallets and services are developing better user experience around this security mechanism to remove some of the burden from the end user, who ultimately shouldn’t really need to know anything about public and private keys. Its about finding the balance between perfect security and good user experience.


Into the fray

Well that was a very brief primer to provide a bit of context but its now time to actually start to do some coding. I’ll try and provide an update every few days or so to explain what I’ve been up to and hopefully it may be useful for others who are also trying to get under the hood of blockchains.

My first objective is to figure out how smart contracts on Ethereum work and how to write them and deploy them using Ethereum’s Solidity programming language.

Wish me luck…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade