Blockchain Basics — A Deeper Look
What is a blockchain?
A blockchain is a continuously growing list of records — called blocks — which are linked and secured using cryptography.
Each block contains some form of data, a previous hash, and its own hash — apart from the first block in the blockchain called the ‘Genesis Block’ which contains no previous hash.
What is ‘hashing’ ?
Hashing is the process of taking an input of any length and turning it into a cryptographic output through a mathematical algorithm known as a hashing algorithm. Most blockchains use the SHA-256 hashing algorithm which produces a 64 character long hash code — SHA standing for Secure Hash Algorithm, and 256 as it takes 256 bits in memory. This hash code essentially acts as a digital fingerprint for the block.
There are 5 requirements that a cryptographic hash function needs to contain in order to be secure:
1. One-way: This meaning that the function is easy to compute for every input, but almost impossible to solve the inverse of said function. i.e. for data x it is easy to calculate the hashing function of x, but knowing the value of the final product it is extremely difficult to calculate the initial value of x.
2. Deterministic: If you give the function a particular input or set of data you will always get the same consistent result for said set of data.
3. Fast Computation: The hashing function must be able to return the hash for a particular input quickly and efficiently.
4. The Avalanche Effect: If a change is made to the data inputted into the function this change must trigger drastic changes in the outputted hash for the data. This property leads to the immutability of the blockchain.
5. Must withstand collisions: This means that 2 sets of data can’t have the same hash value after being put through the function. If 2 inputs map to the same output (hash value) this is known as a collision. Collisions aren’t ruled out by the SHA-256 hashing algorithm however they’re so rare that they can be dealt with easily.
Why are blockchains so secure? How are they immutable and what does ‘immutable’ even mean anyway?
One of the most common things you hear about blockchains is that the data entered into the chain is immutable. This simply means that the inputted blocks of data are unchanging and are unable to be altered in any way.
If a block is altered or changed (usually by a malicious user trying to change the data in the chain in a way to benefit themselves) the cryptographic links between blocks will no longer be valid. Each time data is changed in a block it’ll be given a new hash due to the SHA-256 hashing algorithm discussed above (as the new data entered will be different to the original data stored in the block, thus creating a new hash for the block). This hash will no longer match up with the next block’s ‘previous hash’. This breaks the link between blocks and so you can quickly find out not only if things have been altered on the blockchain, but at exactly what block they’ve been altered.
However, this doesn’t mean the blockchain is infallible. If someone were to come along and alter a block on the blockchain there is a way they could make the chain valid again. By fixing each broken cryptographic link in the chain from the block they changed all the way up to the most recent block (by correcting all of the previous hashes up to the current block) they would validate the chain. However, this would only work if there was one singular copy of the blockchain in existence, which leads us into our next topic — Distributed Peer to Peer networks.
When people talk about blockchains they often talk about how they’re distributed, what does this mean?
Blockchains are built on Distributed Ledger Technology (or DLT for short). According to Wikipedia DLT ‘is a consensus of replicated, shared, and synchronized digital data geographically spread across multiple sites, countries, or institutions. There is no central administrator or centralized data storage. A peer-to-peer network is required as well as consensus algorithms to ensure replication across nodes is undertaken.’ So, what does this mean in English?
A peer to peer network (or P2P network for short) is a vital component in how blockchain technology works, and why it is so secure. Each ‘peer’ on the network is a computer (commonly referred to as a ‘node’). All the nodes in the network are interconnected. Instead of one central server holding all the information on the blockchain, the data in the chain is distributed across all of the nodes. The more nodes that are in the network the more copies of the blockchain exist, which helps protect the chain against malicious users attempting to corrupt the data.
If someone comes along and tries to alter the blockchain the cryptographic links will break, and the chain will become invalid. Even if they then go and alter every block after this block and make the entire chain valid again (as mentioned above) it won’t matter because the network of ledgers (nodes) would see that there is an issue with one of the blockchains i.e. one of them would be invalid. The copies of the blockchain on the network no longer match. Without a P2P network there is no consensus amongst the various chains, and so if any of the chains are successfully altered the malicious user would get away with it as the chain would be valid and there would be no perceived issue. However, with a P2P network they’d need to successfully alter more than 50% of the nodes in the network to come to a consensus and in doing so successfully alter the chain — all of this before the next block is added to the chain (within a few seconds to minutes). This brings trust to a trustless network.
What is consensus in terms of blockchain technology?
When it comes to blockchain technology there are various types of consensus protocols. The underlying goal each of these protocols is to manage how the distributed network agrees to add blocks to the network or chain. The protocol has to decide which chain to grow i.e. if 2 blocks are entered into the blockchain at the same time it has to decide which block to add first. It also has to protect against attackers. There are many different consensus protocols, the most famous being the Proof-of-Work protocol.
Proof-of-Work — When it comes to the Proof-of-Work consensus algorithm the key saying is “The longest chain is king”. Meaning the chain with the most blocks will eventually win and become the actual validated chain. What matters here is where the second next block ends up, this decides the new valid form of the blockchain. When there’s a split chain, the side which has more than 50% of the hashing power will win consensus and become the actual chain.
What is mining?
One of the most common terms you hear when blockchain technology or cryptocurrencies are being discussed is ‘mining’, but what exactly is mining?
Mining is the process of adding transactions or blocks to the existing distributed ledger of transactions, or blockchain. Mining involves creating a hash of a block of transactions that cannot be easily forged, protecting the integrity of the entire blockchain without the need for a central system (i.e. it enables the decentralized nature of the blockchain).
Mining is typically done on a dedicated computer, as it requires a fast CPU, as well as higher electricity usage and more heat generated than typical computer operations. The main incentive for mining is that users who choose to use a computer for mining are rewarded for doing so — in the case of cryptocurrency the miner receives some of that cryptocurrency as a reward.
I mentioned above that each block contains a previous hash, a next hash, and some form of data. Each block also contains a field of data called a ‘nonce’ — which is an arbitrary number. This nonce field of data is what mining is all about. The nonce gives extra control as you can manipulate the blocks hash by changing the value of nonce — this meaning that for different values of this arbitrary number you can generate different hash values for the block, as the nonce itself is a piece of data contained in the block.
The puzzle that miners solve is to identify the value of nonce so that the hash output of the block being mined starts with a specific number of leading zeroes. The value of the nonce that will achieve this is what miners are trying to solve for. The number of leading zeroes to achieve is called the difficulty of the Blockchain network at the time of mining. The difficulty is decided by the Blockchain network itself.
In order for a new hash to be included in the blockchain it must be below the target. There’s no logical reason for this, it’s merely a way to create a challenge for miners. The ‘Golden Nonce’ awards you the block which then gets accepted by the blockchain. There is no formula for calculating the nonce, miners change the nonce again and again until a hash falls below the target.
What is the difference between blockchain technology and cryptocurrencies?
One of the most confusing questions when you first start delving into the world of blockchains is the distinction between blockchain technology and cryptocurrencies. Simply put blockchain is the technology that cryptocurrencies use as their base. A cryptocurrency refers to a digital currency that runs on a blockchain (a distributed ledger of transactions). The blockchain serves as the distributed ledger that forms the network. This network creates the means for transacting and enables the transfer of value and information.
What is a smart contract, and what makes it ‘smart’?
A smart contract is much like an everyday contract. Essentially it is a normal contract running on a blockchain. The rules and agreements between the two parties are coded into the contract and as it’s on a blockchain these rules are immutable and so can’t be altered. When the pre-defined rules are met, the agreement is automatically enforced by the code contained within the smart contract. This, again, takes away the need for a centralized body, as the agreement is automatic once the rules are met.
https://blockgeeks.com/guides/what-is-hashing/ https://lisk.io/academy/blockchain-basics/how-does-blockchain-work/what-is-a-peer-to-peer-network https://www.techopedia.com/definition/32530/mining-blockchain https://www.quora.com/What-is-the-concept-of-nonce-in-blockchain
Blockchain Basics — A Deeper Look was originally published in DataDrivenInvestor on Medium, where people are continuing the conversation by highlighting and responding to this story.