Blockchain for beginners part 1: The Block! What the block does, how it is setup, and how to understand it.

We have all heard the term blockchain once or twice in recent years. Whether it was in relation to bitcoin, some other crypto currency or at work if you are on the technical side of things. Usually, media hyped technologies get completely misconstrued and leave us all wondering wtf did I just read or watch. Well it can be pretty complex on higher levels and it can requires some basic level of understanding to know what it is you are even diving into. So let's start at the core level and go from there. Also, I provided some resources to help you understand the terminology used at the end of this writing.

What is a block?

A block is a section of code that interacts with a system. When we look at crypto currency it would be used for transactions. A decentralized patron somewhere on the net decides to purchase or sell some amount of a coin. For the purpose of understanding, let's say the coin is named cryptocoin. Now this cryptocoin is listed on an exchange called Gemicoin (no relation to Gemini or Coinbase). This decentralized patron chooses to purchase 10 cryptocoins at $1.00 per coin. While logged into Gemicoin the patron initiates the transaction. Here is where the Block comes in.

The Block is a piece of code waiting to get triggered by a transaction. Once the trigger happens, the Block is initiated with the instruction to record data and hash it (which a type of encryption). Let's say our very simple block only wants five pieces of data:

  1. The time created.
  2. The transaction amount.
  3. The previous hash from the previous block (we'll get to that).
  4. The current hash for the current block (it will make sense I promise).
  5. The difficulty level for creating the hash (promises for the line above will be kept ;).

Okay, here's a visual. If you are familiar with coding this is Javascript which is a seriously badass flexible language you can do a ton of cool things with.

class Block {

constructor (timestamp, transactions, previousHash = ''){

    this.timestamp = timestamp;

    this.transactions = transactions;

    this.previousHash = previousHash;

    this.hash = this.calculateHash();

    this.nonce = 0;
}

So you probably pieced some things together but I still want to explain a bit what you are seeing. First you see a couple words on the first line: 'class' and 'Block'. The word 'class' in javascript is used to create a sort of code template that can be reused throughout a program. Block is the name of the template in this case. It can be named whatever you choose. Following that are curly braces (programmer talk for brackets) this is used to tell Javascript these are objects following the open brace and ended with the closing brace. Now inside the braces on the first line you have what is a built-in function called a constructor. The constructor for intentional lack of a better word 'constructs' and provides a format to use for construction. In this case, the format inside of the open and close parenthesis ( ) consists of a 'timestamp', 'transactions', previousHash, and what's called and empty string marked by two quotation marks.

At this point each of the the items used to construct the block are terms or variables with intended data to collect and store in the block. Remember, it is a class being created so it will be used multiple times and this is a template meant to be flexible and keep collecting and storing that data as new blocks are created.

Following the constructor and the format it's creating are the objects it will create. There are five objects consisting of what is called key, value pairs. So, 'this.timestamp = timestamp' is a key, value pair. In this case four of the five have the same name only the key includes this and dot in front of the key. To understand more how this refers to the instance of the object on the global level refer to the resources and dig a bit. Trust me it will help a bunch. The outlier is the 'this.nonce = 0' which is used to set the mining difficulty for this case. This is determined by the number of nonce's that need to be in front of a hash for it to be considered valid for the block. So in this case it is zero. It will all make real sense but there always is an initial learning curve to even understand the basics of something when you jump with me in these rabbit holes. This is a good place to stop for now and Part 2 will be available soon. In part 2 we will cover more about the block and what make it more secure.

Resources and how to use them:

developer.mozilla.org/en-US/docs/Web/JavaSc..

This is a great resource to understand how javascript works. I want you to search in the reference bar in the top right corner using keywords you are looking to understand. For instance, search the word 'class' and see what it says. As you keep searching for words you are unclear about your understanding of what you are looking at will get deeper. Enjoy!