Table of Contents
To understand how transactions work in the Blockchain, we must first be clear about 2 fundamental concepts, the public key and the private key.
The concept of public and private keys
Public Key Cryptography (PKC) is based on “Trapdoor Functions”, which consists of a mathematical operation that is very simple to solve in one sense, but almost impossible to solve in the other, making it almost impossible to decrypt.
Solving this type of problem involves calculating the correct answer, and this takes computers enormous amounts of time, even thousands of years. Therefore, some mathematical practices, such as factoring prime numbers, are not productive for reverse engineering.
PKC uses digital signatures to secure private communications that have been made over a public channel and could be intercepted by anyone. If we apply the same in the field of cryptocurrencies, the main objective is to analyze if it is a forgery, checking if the signature of the owner of the funds is present in the transaction made.
When holding cryptocurrencies, what we actually have is a private key. This private key is what unlocks the “account” and allows us to make use of all the cryptocurrencies that are associated with it, and this same right of use is what makes it so extremely delicate, so we must take special care with its protection against theft or loss by storing them in safe places and making backup copies, for example.
This private key is not the only one that exists, we also have a public key and a cryptographic union between these two keys. In order to recover the public key we will need our private key, however, we cannot do the opposite.
Private key and public key
An easy relation that we can establish to understand the difference between one key and the other would be that the public keys would be the equivalent of our email, while the private key would be our password.
A person can generate as many public keys as he/she wants (addresses) from his/her private key, which is unique and serves as a password. These addresses will be visible to any user of the network since they will be the ones that person will use to send and receive his assets, for example, Bitcoin, but only this person will be able to enter and manage these addresses because he is the one who has the private key that gives him access to all of them.
Process of a transaction
In a transaction, the way to prove that we are the ones who have carried out any operation is thanks to our private key, which acts as a digital signature.
If we need to send a message to another person through a channel that is not private, anyone could be listening, so we could use our private key to encrypt the message and be able to send it with total security. In this process, we would create an output hash with our message that would be sent to our receiver using the receiver’s public key. Finally, once the receiver has the message in his possession, he could decrypt it using the output hash and the private key.
Any person trying to intercept the sent message would only have the public key, so it would be impossible to decrypt the message.
A Bitcoin transaction is the simple act of sending an amount of Bitcoins from one address to another. We can use an example to understand and see in more detail the procedure: John sends from one of his public addresses 1 BTC to another public address of any other user. Before being sent, this transaction is signed with John’s private key to record who the transaction belongs to and the amount sent.
The nodes that make up the Bitcoin network automatically verify and validate the transaction to ensure that it is not a forgery by making use of established consensus rules and cryptographic proof that the public and private key pair is valid. This process results in an extremely secure transaction that is impossible to forge.
Once the funds reach the destination user, this transaction starts to be confirmed by all nodes and is recorded in the blockchain, being totally irreversible. These addresses, therefore, can be used publicly without fear of exposing our assets, as a current account where we can receive or make transfers.
So far we have mentioned mining on several occasions and at some points we have delved into some aspects. However, the concept of mining can create some confusion so to clarify it we will use, once again, the Bitcoin Blockchain as an example throughout this part of the syllabus.
Mining is not about creating new Bitcoins. The mining process is what makes the Blockchain able to be a fully decentralized system. It gives security to the system and enables it without a central authority. We must clearly differentiate mining from the new Bitcoins that are generated and delivered to the miners by the actual process they perform.
Before we continue, let’s review some things we already know about mining. The miners are in charge of validating all the new transactions that are generated, registering them in the ledger, this procedure being irreversible. The blocks containing all transactions are mined approximately every 10 minutes. During this time, all miners are constantly competing to be the first to solve a mathematical equation based on a Hash algorithm which is called Proof of Work (PoW).
This proof is the demonstration that a miner has spent a lot of time and resources to solve the problem. When a block is “solved”, the contained transactions are considered confirmed and the Bitcoin in question can be spent on the transactions. Therefore, if we expect to receive Bitcoins in our wallet, our transaction will take approximately 10 minutes to be confirmed.
We already know that miners receive a reward in the form of Bitcoins when they find the solution to the complex mathematical problem posed, however, it is not the only reward they are eligible for. There are actually two types of rewards for miners: new Bitcoins or transaction fees.
Miners are needed to send Bitcoins through the network confirming the transactions that are being made. To perform this action we can pay as a tip, the amount of Bitcoins we want, although it is advisable nowadays to establish this “tip” around 0.0003 BTC. If we make standard transactions so that our transaction is carried out as quickly as possible, since each miner has a certain number of transactions that can be carried out and, therefore, prioritizes depending on the amounts that each user has given them for carrying out their transaction.
It should be noted that, as the reward per block to the miners decreases, the transaction fees will increase to compensate for their work in maintaining the network. Therefore, after the year 2140, the winning miner will only receive transaction fees as a reward.
Having said all this, the next step would be to understand how in a vast network full of nodes that, moreover, do not have any kind of trust with each other, can reach a consensus on who owns one or another Bitcoin. To explain this process we will take as an example a real block, the number 641587, for example. We will follow the life cycle of this block from its creation to its final validation, which was solved by Juan, the person in the example above, who is also involved in mining.
Juan competed to try to win the battle to validate block 641586, however, in the end, it was another miner who managed to solve it. Once block 641586 was mined, Juan began to create a candidate block, which would become block 641587, but not before updating his copy of the blockchain.
Juan’s node, while it is in the process of searching for the PoW for a block, keeps receiving new transactions that it accumulates and adds to the memory group or transaction group. All these transactions wait in these groups until it is time for them to be validated and added to a new block.
Once the node receives the notification that the current block has a valid PoW, the process of creating a new candidate block (which does not yet have a valid PoW) with the transactions mentioned above begins.
We can see that block 641587 has 2196 transactions inside it. This number refers to the number of transactions that were present in John’s transaction group when creating his candidate block.
We can see that block 641587 has within it 2196 transactions. This number refers to the number of transactions that were present in John’s transaction group when creating his candidate block.
The first thing Juan’s node does is to create the coinbase transaction. This transaction is generated to pay the miner his reward for mining the block, or what is the same, for finding the proof of work, and is therefore directed to the miner’s own wallet (blue box), in our example Juan. This type of transaction is totally different from the rest because the reward Bitcoins are created out of thin air, they do not come from someone’s wallet.
The transaction fees are also calculated by the nodes in the block.
John’s Reward = Reward per mining block + transaction fees
In this case, we can see that the reward per block is 6.25 BTC and the transaction fee is equal to 0.80450543 BTC.
6.25 + 0.80450543 = 7.05450543 BTC
Building the block header
The creation of a proper block header is the responsibility of the node itself and is composed of several pieces of information:
– The root of the Merkle tree.
– The hash of the previous block.
– The data on which the mining competition will be based.
The data for the mining competition, in turn, is a set of metadata containing:
– The version number of the software being used in order to be able to check equalizations and protocols.
– The elapsed seconds in Unix time (time elapsed from midnight on January 1, 1970) until the creation of the block.
– Target of the PoW algorithm for this block.
– Nonce: Counter used for the PoW algorithm.
The version when block 641587 was extracted is 0x3fffe000 which is represented in little-endian format in 4 bytes.
The timestamp is the number of seconds elapsed from January 1, 1970 to the date and time the block was created. Knowing that the date of the block is 31-07-2020 and the time 11:01, we can use a Unix Epoch conversion tool (e.g. https://www.epochconverter.com/) and get 1596193260000 seconds.
It is the network itself that marks the GOAL and, therefore, that establishes that a hash is valid or not. In case a hash is higher than the marked target, then it is considered as invalid, and that is what is used to calculate the difficulty. In the block we have taken for the example, the difficulty is 16,847,561,611,550.27.
Let’s analyze the hash of the block:
The first 19 zeros mark the difficulty, so a hash with less than 19 leading zeros would not be valid.
The last piece of information is the nonce, which is a 32-bit number that starts as 0. Once the competition begins, the miners assign random nonces in an attempt to find a valid hash. This is the only value that the miners change.
Once all this is done, John can extract the block. As we have seen above, the node must find a hash for the nonce that is below the target. Therefore, the 32-bit size of the nonce means that there are four billion possible combinations. Although technically, it is much larger because of something called an additional nonce. This is an additional space for a longer nonce, which means it can have tens of billions of combinations.
In Bitcoin, we already saw that the SHA256 hash function is used (which produces a 256-bit long output) and that with 2 different inputs (collision) it is impossible to calculate the same hash, just as it is also impossible to predict the output of any data in advance.
We know that mining consists of finding a nonce that produces a hash with a value less than or equal to that set by the difficulty. Because all the information is accessible to anyone, it is quite easy to be able to check whether the hash found is valid or otherwise invalid.
The PoW in Bitcoin is not a static parameter, but changes every 2,016 blocks so that the GOAL of mining one block every 10 minutes or so is achieved. This is because, if it were static, with the improvement of technology and computing power, those 10 minutes would be progressively reduced and the block limit would be reached much earlier than established.
The way to readjust the difficulty is simple, multiply the 2,016 blocks by the expected time, i.e. 10 minutes, and we get a result of 20,160 minutes. This is the time it should have taken to extract those 2,016 blocks, if the actual time has been less then the difficulty is readjusted to keep it within those 10 minutes.
Having explained the latter, let’s suppose that John’s node starts up and after about 10 minutes, finds a hash that turns out to be valid. We can see that the nonce used was 370,749,008.
Once the node has the valid hash, it immediately transmits it to all peers so that they can validate it. Valid data includes:
– The block header hash smaller than the target.
– A block size within the allowed limits.
– The timestamp of the block. – A first coinbase transaction (only the first one).
– A valid reward.
– All transactions within the blocks that are valid (they also have a checklist of their own).
All new blocks are validated independently by each node following the same rules. This process of reaching consensus in a decentralized manner ensures that no node in the network can cheat in any way.
Once it is established that the block is valid, all the miners that make up the network will update their copy of Blockchain to incorporate the new block 641587 and will use the hash of the block that Juan resolved in order to extract block 641588. This closes the cycle of a block to start again right after the next one as we have seen.
The problem with this mining process is that, due to the large computational power required to find a valid hash, it makes it almost impossible to achieve the reward if attempted individually. This is where the so-called “Pools” come in, which is nothing more than a mining pool with a coordinator in charge of organizing the miners. The coordinator will make sure that the miners use different values for the nonce so that they don’t waste hashing power by trying to create the same blocks. These coordinators will also be responsible for dividing the rewards and paying them to the participants.
These mining groups began to emerge as the difficulty of mining increased to the point where it became unprofitable for a single miner to attempt to solve a block due to the enormous amount of time and energy cost involved. As the value of the different cryptocurrencies has increased, many people have entered the mining world causing an increase in competition and the difficulty of block resolution.
These unions have obvious advantages, in the groups, all members join their computing power to solve the block, so it is solved much earlier, shortening the time ostensibly and having many more chances that any member of the group can solve the block. Once solved, each miner receives a percentage of the reward generated by solving the block depending on his contribution.
Although we can find pools all over the world, most of them are concentrated in China and in countries where the price of electricity is very low, as this way they manage to increase considerably the profitability of mining operations.
Within these mining groups there are different ways or methods of distributing rewards:
– Pay-per-share (PPS). The miner receives the payment corresponding to his contribution immediately from the total amount belonging to the group. An advantage of this method is that the balance can be withdrawn whenever the miner requires it without waiting. Another advantage is that the amounts that the miner will receive will be much more stable over time without many differences between them.
– Proportional. Miners receive shares based on the contribution they have made to the group. Once the group gets the reward, it is divided among all the miners depending on the amount of shares each of them has.
– Pay-per-last-N-shares (PPLNS). This method is similar to the proportional method, however, the reward received by the miners is based on X number of last shares instead of the number of shares received when solving the last block. The shares given to the miners are the same and the amount will be higher if the block is solved in less time than if it takes more.
– Geometric. In this method, each share sent by a miner receives a score depending on the connection time of the miner. When the block is solved and the group gets the reward, it is divided and distributed among all depending on the score. In this case the score given to each action does not vary depending on how long it takes to solve the block of that round.
– Multipool mining. This type of group mining is based on achieving the highest possible profitability by changing the mining between different cryptocurrencies. These changes will be made depending on different factors: – The value of the coin at the time of mining. – The time required to mine the block. – The number of miners involved in the resolution of the block.