Another central concept for blockchain are the transactions.
Transactions are the records of data stored in the blockchain.
All the transactions are present in chronological order.
When the transactions are submitted over the blockchain, they are stored in a memory pool.
The memory pool is a synchronized container of data present with all the nodes.
All the transactions in the memory pool are unconfirmed.
Transactions means the transactions are not part of the blockchain yet.
The function of miners is taking these transactions from the memory pool.
Verify and validate them and finally put these unconfirmed transactions into the block they are mining.
After the block has been mined and added to the chain, the transactions are also confirmed over the
blockchain.
The transactions are stored in the form of a merkle tree.
Inside the block.
Merkle Tree makes sure that by looking at one single root, the verifiers can verify all the transactions
in a block.
We will talk more about Merkle trees soon.
Transactions over blockchain can also contain metadata information which can be used to store data over
the blockchain.
Once a transaction occurs over the blockchain, it goes to a memory pool.
Miners take the transactions from the memory pool, validate them by checking the information, such
as Does the sender have balance to send the coins?
Alternatively, does the receiver have a valid account on blockchain or not?
Similarly, many more validations are done by the miners for a transaction.
Once the transaction has been validated, miners put the transactions inside a block coming to the block
in a blockchain.
In simple terms, we can say that a block is a container of information that stores the data over the
blockchain.
A typical block mostly contains transactions, timestamp, a block identifier, nonce value and a previous
block hash.
A block could include additional information depending upon the platform you are using for your defined
use case.
Each block has the hash of the previous block and a chain of these blocks.
Using the hash value of the previous blocks forms the complete blockchain.
The blocks are stored in your hard drive as per the defined schema of the blockchain.
Different platforms have different schemas.
For example, Hyperledger uses key value schema for blocks.
There is also a time associated with the block, which makes sure that the blockchain is always under
control as per the time.
For example, in Bitcoin, a block is created every ten minutes.
All the transactions happening over the network within ten minutes can be put inside the block to be
added to the chain After the container of data is formed by the miners, the miners try to guess the
nonce value or a random number which will be added with the block data to generate a specific hash result.
We will discuss the same in the coming session of how blockchain works.
Just to give a clear overview of what we are talking about, you can take the example of Bitcoin inside
the Bitcoin.
This nonce value should be guessed, which makes sure that the resulting hash of the block has some
leading zeros.
The blockchain algorithm calculates the number of leading zeros and also forms the difficulty of the
chain.
When the miner is able to guess the nonce value, then the block is mined and added to the chain.
This process is also known as mining, and the miner receives a reward for doing the work, which is
12.5 BTC for Bitcoin.
If the resultant hash by a miner is higher than the targeted hash, then the other miner is given a
chance to guess the value of nouns and confirm the block.
Blocks are time bound in bitcoin.
If the blockchain algorithm sees that the block time is less than ten minutes, this means miners are
able to guess nouns in less than ten minutes.
Then blockchain algorithm increases the difficulty to make sure that the miner takes more time to guess
the nonce value and the block is being mined in ten minutes.
Similarly, if the block time is coming to be more than ten minutes, then the difficulty is lowered
to meet the ten minutes parameter.
Before moving further, let's understand the structure of a block.
A block contains plenty of information.
A typical structure of a block has following three components.
Block Header.
Block Identifiers and merkle trees.
A block header includes the metadata information about a block like a timestamp protocol, information,
nouns, difficulty, and the previous block hash, which is used to form the chain.
Every block has a hash value associated with it, and this hash value also act as a block identifier.
If the block data changes, this hash value will also be different.
Lastly, a block contains a merkle tree.
Merkle tree is a hash binary tree which is used to store and verify transactions in a block.
This is an example of a Bitcoin block, which you can see on the screen.
There is much information inside this block.
As you can see, the first field is the magic number.
This is the number which defines the network configuration.
For example, in the Bitcoin main network, the magic number always has the value as shown on the screen.
The next thing is the block size, which defines the number of bytes present in a block.
The third entry is the block header, which consists of six different items, including previous block
hash and nonce value.
After that, we have transaction counter, which is a positive integer specifying how many transactions
are present inside a block.
And finally, we have the list of transactions which has an equal number of transactions as per the
transaction counter.
The blocks in a blockchain may have different information than the Bitcoin.
You can add additional information to make it more secure.
Now let's see three essential components of a block.
Block Header.
Block Identifier and merkle trees.
The block header contains the metadata about the block.
A typical block header consists of previous block hash, which is used to connect the blocks.
The mining competition for the network includes the timestamp, the nouns and the difficulty.
The mining competition is the value which will be used by miners solving the block.
The last one is the Merkle tree root, which is the data structure.
To summarize all the transactions inside the block.
Merkle roots act in such a way that even if one of the transaction changes, the root of the tree will
be different, thus making sure that the verifiers can identify the changes in the block by looking
only at the single Merkle root.
Block identifiers in the code are the hash token for the block data, like private keys which are used
to sign the transaction and also provide a source of an identity for the person making the transaction.
A block is similarly identified by its cryptographic hash.
We already discussed block hash while defining blocks.
Bitcoin blockchain uses a Sha 256 hashing algorithm to create the block identifier.
You can employ different hash functions for your blockchain.
Every block contains the previous block hash with transactions, headers and metadata.
This forms the block data and is provided as an input to the hashing algorithm with nonce value to construct
its hash.
Another way of identifying a block is the block height.
This is the position of the block inside the blockchain.
For example, if we say that the block is in 7312 position, this means that there has been already
7311 blocks before this current block.
Block identifiers are necessary to make sure that we have a unique identity available for each block,
such that the block identifier can identify even a small change inside the blocks.
I mentioned earlier that the block has three primary data structures a block header, a block identifier,
and Merkle tree.
Merkle Tree is an essential concept concerning the blockchain.
Merkle tree follows bottom up approach where the finality of the tree is a root node, which summarizes
all the transactions for the block by producing a single digital signature for the entire set of the
transactions, users can verify the transactions just by looking at the Merkle root.
If a single detail inside any of the transaction changes for a block, then the Merkle root for the
particular block will also change.
Merkle tree is like a binary tree with transactions acting as the leaves of the tree.
Transactions in the Merkle tree are taken in a group of two and hash together.
This process continues to the point where we are left with one single hash, which is also known as
Merkle root hash.
Let's see through a diagram how the complete Merkle tree function.
You can see in the image on the screen that we have transactions A, B, C and D.
These transactions act like leaf nodes for the Merkle tree, as we have already mentioned.
A merkle tree follows a bottom up approach.
So we start from the transaction Leafs.
We take the hash of the transaction A and the hash of the transaction B.
Similarly, we take the hash of transaction C and D, Now we take a group of two transactions and accumulate
their hashes together and take the hash of the result.
In our example, we collect the hash of transaction A and B, and then take the hash of the result to
form hash of the transaction.
AB.
Similarly, on the other side, we create the hash of the transaction CD.
Now again, we follow the same process by accumulating the hashes of transaction AB and CD and take
the hash of the result to form one single hash value.
This hash value is known as Merkle root hash, which is stored inside the block.
A verifier doesn't need to go through all the transactions to verify the block.
It can directly check the Merkle root hash to verify all the transactions.
If even a single transaction is altered inside the block, then the Merkle root hash for the block will
be different.
Merkle tree is an event tree.
That means all the transactions are to be even in number to form the Merkle root hash.
If we have odd number of transactions inside the block, then the last transaction inside the tree is
duplicated to create an even tree.
Those were some of the critical blockchain fundamentals.
These concepts make up the characteristics and behavior of the blockchain.
Now let's move on to some additional fundamentals of the blockchain.