April 25, 2024

Blockchain in seven steps: how mining works and how transactions are processed

Ever wondered how blockchain mining works, or how transactions are confirmed and...added to the blockchain? And me too. But due to the fact that I didn’t come across a clear and accessible step-by-step explanation of this process, I decided to write it myself. So, I present to you the result of my research on the topic of how transactions are processed on the blockchain. And in just seven steps.

Step 1: The user, intending to send someone else a certain amount in cryptocurrency or a token, confirms the transaction in his wallet application.

Step 2: The transaction is then transmitted by the applicationwallet to the network of the corresponding blockchain. There, she falls into the “pool of unconfirmed transactions”, where she will be located until some miner (the one who is engaged in mining) picks her up. Such a pool is a collection of unconfirmed transactions pending processing. Most often, unconfirmed transactions are collected not in one giant pool, but in small, divided local pools.

Step 3: Online miners (sometimes callednodes or nodes, but this is not quite the same as miners!), select transactions from pools and collect a new “block” from them. A block, in fact, is a set of transactions (at this point in time there are still unconfirmed transactions), plus additional metadata. Each miner collects its own transaction block. The same transactions can be added by different miners to their blocks.

Example: Consider two miners, miner A and miner B. Both of these miners may decide to include some transaction X in their blocks. Each blockchain has its own restrictions on the maximum block size. In the Bitcoin blockchain, the maximum block size is 1 megabyte (MB) of data. Before adding a transaction to the block, the miner needs to check whether the transaction meets the execution requirements in accordance with the transaction history in the blockchain. If the sender’s wallet balance contains enough funds according to the existing blockchain transaction history, the transaction is considered valid and can be added to the block. If the Bitcoin owner wants to speed up the processing of his transaction, he can offer a higher reward for mining. In this case, as a rule, miners will assign a higher priority to this transaction compared to others, due to the possibility of receiving a greater reward for the mining process.

Step 4: By selecting transactions and adding them to your block,miners thereby create a block of transactions. In order for a block to later be added to the blockchain (meaning that all nodes on the blockchain will record the transactions contained in that block), the block first needs a signature, also called a “proof of work.” -work or PoW). This signature is created by solving a very complex mathematical problem that is unique for each block of transactions. Different blocks correspond to different mathematical problems, so each miner will work on solving his own problem, unique to the block he formed. All problems are equally difficult to solve. Solving such a mathematical problem requires a lot of computing power (and, accordingly, a large amount of electricity). You can compare this to performing calculations on a calculator, only in this case everything is much more complicated and is done on a personal computer. This process is calledmining. If you want to know more about whatfor math problems you have to solve as you go (in fact, it's not that difficult), then please continue reading further. Otherwise, if you need a “lighter” material, go straight to “step 5”.

</p>

***

Mining, also known as hashing (Proof of Work consensus algorithm)

The mathematical problem that has to be solvedeach miner, when trying to add a block to the blockchain, is to find the hash amount (also called digital signature) for the data in this block. Moreover, the signature should begin with a certain number of consecutive zeros. Looks like a very difficult task, right? In fact, everything is not so complicated. Let me try to explain this in a simplified way.

Before we continue, it is important to know what a hash function is. In simple terms, a hash function is a mathematical problem that is verydifficult to decide, but the response received is verysimplycheck.

The hash function takes a string of input data,consisting of numbers and letters (any, in the literal sense of the word, string of random letters, numbers and/or symbols) and converts it into a new 32-character string consisting ofrandomletters and numbers. This 32-character string ishash amount. If any number or letter in the input string changes, the hash sum will also change randomly. However, the same input stringalwaysthe output will have the same hash sum.

Now bear in mind that the data insidea block is the input for hashing (the same data row). As a result of processing this input data with a hash function, we get the hash sum (32-character string). Bitcoin blockchain rules stipulate that only those blocks can be added to the blockchain whose signatures (hash amount) begin with a certain number of zeros. However, the generated output row is always random for different rows of data in the input. But what if the data line of the block as a result of processing by the hash function does not give a signature (hash sum) at the output, starting with that certain number of consecutive zeros? So this is the reason that miners have to repeatedly change a piece of data from their block, called "nons"(Eng. Nonce). Each time the miner changes the nons, this entails a slight change in the layout of the block data. And when the data structure inside the block (that is, its input data) changes, its signature (or its output data) also changes. So, every time a block's nons changes, this block gets a new random signature.

This procedure for changing nonse is repeatedminers an infinite number of times, until in the end they accidentally stumble upon an output line that satisfies the requirements for block signing (a certain number of zeros at the very beginning). The following is an example illustrating this process. The example uses seven zeros, but the number of zeros actually depends on the complexity of the block in the blockchain. The complexity of the block is a slightly more advanced topic, so I suggest you familiarize yourself with it later.

</p>

This is how miners have to looka suitable signature for your block, and this is also the reason why so much computing power is required to solve this mathematical problem. Iterating through such a huge number of non-numbers in an attempt to guess the right one takes a lot of time and large computing power. It should also be borne in mind that with the growth of hashing power (the number of miners) in the blockchain, the complexity of the mathematical problem will also increase, which will entail an increase in the average cost of electricity required to solve the block (more about this can be found here). You are great fellows if you read up to this point! Let's continue and go to step 5.

Note: In fact, this process is not defined as a mathematical problem, but rather is something deterministic - computers perform predefined operations on a number to find out if the result is exactly what is required.

***

Step 5: The miner, the first one to find a signature that satisfies the requirements for its block, broadcasts this block and its signature to all other miners.

Step 6: Other miners now check for legitimacysignature by taking the data string of the broadcast block and hashing it to check if the resulting hash sum actually results in a signature included in the block with the correct number of zeros (remember, right? –hard to decide, Buteasy verify) If the signature is valid, then all other miners will confirm its authenticity and agree that this block can be added to the blockchain (that is, they will reach a consensus, in other words - everyone will agree with each other - this is where the term consensus algorithm comes from). The term “proof of work” follows from this. The signature is precisely the required “proof” that the work was done (computing power was used up). Now the block can be added to the blockchain and sent to all other nodes in the network. Those, in turn, will accept the block and save it, adding transactions to their data, provided that all transactions within this block can be executed in accordance with the history recorded in the blockchain.

Step 7: After the block has been added to the chain,all other blocks that will be added after it will be considered a “confirmation” for this block. For example, if my transaction is included in block 502, and the blockchain is 507 blocks long, this means that my transaction has 5 confirmations (507-502). This is called confirmation, because every time a new block is added after yours, the blockchain again reaches consensus about the entire history of transactions, including your transaction and your block. You can also say that at the moment, the blockchain has confirmed your transaction 5 times. Etherscan also relies on this when it shows you the detailed details of your transaction. The more confirmations your transaction will have (in other words, the deeper your block is in the chain), the more difficult it will be for attackers to replace it (more about how this works, you can read here). After a new block is added to the blockchain, all miners need to start again from “step 3” and form a new transaction block. Miners cannot continue (in fact, they can, but this is somewhat beyond the scope of the topic discussed in this article) to mine the same block, in other words, to solve the problem - of the same block that they worked on for two reasons:

First: it may contain transactions that wereconfirmed by the last block already added to the blockchain (remember that different miners can select / include the same transaction (s) in the block that they decide). Any of those transactions, if they are started again, may be invalidated, because there may already be insufficient funds on the balance of the sender's wallet.

And second: each block must add a hash amount (signature)The last block added to the blockchain in its metadata. This is what makes blockchain blockchain (block chain). If a miner suddenly continues to mine the same block that he was already working on, other miners will notice that its hash amount does not match the one that was in the last block added to the blockchain and, as a result, this block is rejected.

 

</p>