Blockchain technology may seem new to people nowadays, but actually the concept is a few years old already (first mentions date to year 2008). It represents an attempt at a decentralized and transparent ledger of online transactions. Though it is most known in conjunction with cryptocurrencies, this technology holds a much bigger potential.
Let’s start with the basics about blockchain – what it is and how it is represented.
Blockchain definition and structure
Blockchain is an ever-growing linear list of structures called “blocks” in chronological order, each containing data of sorts, for example list of all Bitcoin transactions made in a certain time interval. Each blockchain makes good use of these:
- cryptography based on private/public key pairs and hashing
- P2P network
Is a fundamental unit of any blockchain where any and every transaction is being written to and recorded. Blocks are unidirectionally linked together (pointer to previous block) to form a “chain”. One can imagine the block like an accouting book full of notes of transaction written in it. Once the block is completed and added to the blockchain, it can never be altered or removed. To complete a block it has to be “mined” by miners.
Mining signifies a time-consuming calculation of SHA256 hash of a given block’s header so that it satisfies given target criteria. If such calculation does not output a satisfying hash, the block header’s Nonce in the header (see block structure below) is incremented and calculation starts anew. Once a miner reaches hash which is equal or less than given target, the block is completed and accepted into the blockchain as its new last item.
Complicated? Imagine it like this: the miner needs to calculate hash function (no need to know any details how hash is calculated, just know that its output is a unique string with fixed number of places) of a block’s header that satisfies a target condition (for example its first 8 characters need to be all zeros). Such condition gives only a small probability of success, but remember thousands of computers are trying to find solution at any given second.
The difficulty (target number of calculated hash) is adjusted every 2016 blocks, so it always takes approximately 10 minutes to find a solution for any given block. Since every miner who finds a block’s solution is awarded bitcoins, constant adjusting of difficulty must take place to regulate the flow of the new coins into the market.
Structure of a block
|Block field||Block subfield||Note|
|Magic number||a constant – 0xD9B4BEF9|
|Block size||no of bytes till the end of block|
|Block header/||Version||block version no|
|hashPrevBlock||pointer to previous block|
|hashMerkleRoot||hash of the data (all transactions)|
|Time||timestamp as seconds|
|Bits||target number in compact format|
|Transaction counter||No. of transactions in data part|
|Transactions||All transaction data|
based on: https://en.bitcoin.it/wiki/Block
- At the time of writing this article, there are about 483,000 blocks in the chain already.
- Genesis block (for Bitcoin chain) was added to the blockchain on 3rd Jan 2009, it contained only one “transaction”, which was actually a first page title from The Times newspapers: “The Times 03/Jan/2009 Chancellor on brink of second bailout for banks”.
SHA-256 (also known as Secure Hash Algorithm 2 – SHA-2) is a one-way cryptographic function (meaning you can convert your input into SHA-256 hash, but there is no way of reverting hash into input it came from). It returns a signature of fixed size (256-bit/32 bytes) that is nearly unique to any input. The most fundamental characteristic of a hash function is that even the slightest change in the input ends up in altering at least half of the characters in the output hash. In effect that means a tiny change of a decimal point in one of hundreds of transactions written in a block will manifest itself in a visibly changed hash. Observe:
Say for example you want to send some amount of bitcoins to a friend
it’s SHA-256 hash would be:
but suppose there is a malicious person who tampers with the block with your recorded transaction and changes the decimal point tiny bit (with big consequences) to
One can see something is wrong just by looking at the hash, because it is completely changed!
You can try it on your own here, too:
Now back to blockchain – SHA-256 signature of a block (adhering to a certain target value) needs to be computed before block is added to the blockchain. It serves for two purposes – controls the flow of new Bitcoins because it is computationally difficult task and it serves as a pointer “address” of the block (do you remember hashPrevBlock field in the Header of a block? that is where address of the last block goes to before adding a new one).
Peer-to-Peer network (P2P)
So far we have talked about the blockchain’s basic structures and how it is formed. But what about it’s distribution between computers and network transport “layer” in general? This is the point where P2P networks come into play.
P2P is one of many different types of network. It consists of peers – nodes, computers connected to the network – who are equal and have the same privileges (in contrast with client-server type of network, where server is master and can have multiple clients connected to it). Peers communicate with each other, make part of their local resources (like bandwidth, RAM, processor time, disk space) available to the network, work is distributed between them automatically based on software and their capability. Disconnecting one or multiple peers has no effect on the network itself (while disconnecting the server in client-server model brings the whole network down). P2P architecture is completely decentralized, hence blockchain, which uses P2P network is decentralized, too.
Client-server vs P2P architecture of a network
(picture from https://www.wowza.com/resources/guides/p2p-unicast-streaming)
The very basic postulate is that every single peer node has a complete copy of the blockchain. Each time a new block is added to the blockchain peers synchronize and append the new addition to their copy of blockchain.
The incorruptibility (meaning inability to modify past records) lies within redistribution of the blockchain between peers. Imagine a simple blockchain network with 5 peers. Each has the same copy of the blockchain. But one peer decides to modify a decimal point in a transaction to get more of some cryptocurrency. What happens? As we know from the SHA-256 part of this post, hash signature of the modified block will change and since the blocks in the chain are linked together via hash of the previous block, the alteration of signatures will propagate itself through the chain to the newest block. Now, one peer has a modified blockchain and tries to convince the network his chain is the true one. What does the network do? It checks the chains of all peers for confirmation, but since four peers out of five have the same unaltered chain, the claim is rejected and the modified blockchain is disregarded. Majority wins.
In the next part we will take closer look at cryptocurrencies based on blockchain technology.