What is a blockchain?

2018 is poised to be year when cryptocurrencies become mainstream. The original cryptocurrency, Bitcoin, has entered the common jargon of the modern world last year as its valuation hit record a record high of nearly 20k USD/BTC, and stayed in the news as its valuation dropped to more reasonable levels. Ethereum is also gaining recognition as it became the #2 cryptocurrency in terms of market capitalization. In short, a little over 8 years since the creation of Bitcoin cryptocurrencies are gaining recognition and acceptance in the “real” world.

Cryptocurrencies are created as part of something called a blockchain. And more than cryptocurrencies, it is the blockchain idea which is expected to have a huge impact on the computing world, at least for the next couple of years. As such it is a good idea to learn what a blockchain is, at both a basic and more advanced level.

The Basics

At its core, a blockchain is a distributed ledger. Those with an accounting background will immediately recognize what a ledger is — it is a record of transactions. A blockchain is distributed, which means that entries in the ledger are written by many parties, as opposed to by one centralized authority.

Like an ordinary paper ledger, blockchains are write-once. Once a block has been verified and added to the blockchain it cannot be erased or modified. This insures that transactions cannot be taken back.

The Nodes

All these “parties” are actually computers running a node for the blockchain’s network on the internet. This involves executing software which contributes to the blockchain network. Depending on the network involved there may be several types of nodes in a blockchain; this will be explored in depth later.

The Blocks

Nodes compile a number of transactions into a block. How large the blocks are, and how often they are verified, varies widely between blockchains. For example, the Bitcoin blockchain generates a block every 10 minutes. The Ethereum blockchain, in comparison, generates a block in less than 20 seconds, and Bitshares blocks are generated every 3 seconds at most. A number of factors affect block time; if you’re not intimidated by math check out this article for more information.

The Chain

Blockchains are so named because each new block is appended to the previous block, effectively forming a chain. In fact one can always look at certain information in the latest block of any given blockchain and trace the blockchain’s history all the way back to its very first block.

Hashing

Since blocks are appended to the blockchain by several different nodes, there needs to be a way to ensure that only the block with the right data can be added at any given time. Otherwise there would be no way of ensuring the continuity of the blockchain from the genesis block to the most current one.

This is where hashing comes in. Hashing is a cryptographical technique that is used to generate a unique code that can be used to identify a set of data, rather like a fingerprint. The hash is generated from the transactions contained in the block and recorded as data in the block, which also includes the hash from the previous block. This is one of the mechanisms used to verify any new blocks. If the previous-block hash does not match the previous block’s recorded hash, then the current block is invalid and cannot be added to the chain.

The actual library used to generate the hashes depends on the blockchain. SHA256 is a popular one and is used by Bitcoin. Other libraries include scrypt, X11, Cryptonight and ETHash.

Hashing produces a completely different string if there is any change whatsoever to the original hashed content. The SHA256 library can produce a very large number of distinct values (3.4028237e+38) so arriving at the same value from two different pieces of content is extremely unlikely. By comparison, the chances of winning the Powerball lottery in the USA is 1 in 2.92e8. One could win this lottery 4 times and that would still be less likely than generating the same hash from 2 different sources. Thus the use of hash values makes blockchains virtually tamper-proof.

This was a very basic overview of blockchains. We’ve barely scratched the surface. In my next few articles I will be providing more in-depth coverage on subjects such as concensus algorithms, blockchain node types, the relationship between blockchains and cryptocurrencies, and how the blockchain can be used by businesses to streamline processes and reduce processing costs.

A solution to a fabled problem

We’ve all heard the story in school — when chess was invented in India the local King (they had lots of kings back then apparently) was so impressed by the game that he offered its creator whatever he wanted. The man (who was rumored to be wise but somewhat of a smart-ass) responded that all he wanted were a few grains of rice. The number of grains was to be determined as follows: using a chessboard, place 1 grain on the first square, two on the second, four on the third, 8 on the fourth, etc. To which the King immediately assented, until one of his ministers told him that not only was this more than he had to give, but was in fact more grain that was produced by the entire known world at the time.

So, being somewhat of a smart-ass myself and being in the middle of learning the python programming language I figured I’d come up with a neat and efficient way to calculate exactly what that number was. It’s not that difficult. Basically because the chessboard has 64 squares, the number you’re looking for is the sum of the solutions for 20 through 263.

Since Python is loosely-typed you don’t have to worry about declaring the right type of integer to hold the final result, which is astronomically big. Here’s the code for it:

numTotal = 0;
for numExponent in range (0,64):
    numTotal += 2**numExponent
    print "Total at", numExponent, ": ",numTotal
print "All done."

Note: even though I declared the range 0 to 64, 64 is not itself included.

So what’s the final result? It’s 18446744073709551615. 18 quintillion, 445 quadrillion, 744 trillion, 73 billion, 709 million, 551 thousand and 615. Quite a large number indeed and assuredly more than the King could deliver.