Before I get into how the torrent system benefits us, first I want to explain how most other blockchains mining process works. Understanding this process will also help in realizing why our torrenting system fundamentally changes how our blockchain operates and the speed in which it operates.
So when mining, the mining software selects a number of transactions that exist in the mempool (a fancy word for memory). These are the transactions to be included in the block if the miner successfully mines a block.
Lets say there are 100 transactions to be included in our example. So the miner selects these 100 transactions and it goes through the process of validating each transaction. Validating is just a series of checks to ensure all rules are followed. It includes hashing the data and ensuring the hash they come up with matches the hash sent with the transaction, then they verify the signature.
This is done for each of the 100 transactions. This means during the mining process of our example, we start having hashed 100 transaction each 1 time, so 100 hashing functions. Now we must take each of those 100 hashes and pair them together. So transaction 1 hash is paired with transaction 2, then 3 and 4, etc.
Each of those paired hashes, are hashed. This creates 50 new hashes. We have now done 150 hashes the initial validation, and now all these pairings. We then take those 50 hashes and pair them together and hash them again so we have 25 hashes. We take the 25th hash and duplicate it so we have 26 hashes, we pair them together and hash them so we have 13. We duplicate the last and pair them again so we have 7. We pair the last and do this again so we have 4. We pair them and hash again so we have 2. We pair them again and hash 1 final time. This hash is called the merkle root hash.
So we just did 100 + 50 + 25 +13 + 7 + 4 + 2 + 1 = 202 hashing functions.
Then we use that merkle root hash in our block header, we add our other header data and we hash the header. We then check if the value of the hash is less than the target difficulty value. If not, we increase our nonce by 1, we hash again and check again.
This process continues over and over until we find a valid block. We then include all our selected transactions in the block.
This is a brief summary of how a block is mined. This is what makes mining a block such a complex process. There are a TON of hashing calculations that take place. There are a TON of verifications that take place. And this has to happen with each and every block.
So when a new block is mined, you have to stop mining the current block, select more transactions, and do the process again.
Equally when you validate a block, you have to perform all these same calculations to ensure the block is correct, transactions are correct, that they are in the correct order, that they have not been tampered with, that the correct funds exist in the user wallet, every detail has to be accounted for each with each and every block.
Obviously software handles all this, but its intensive work. This hashing process and validation process is where a bulk of time is spent in both mining and validating.
Well we changed all of this process in Contractless. We removed more of the hashing requirements entirely and just built the chain differently. We were able to do that because of our torrenting system.
So first let me say we built a custom torrenting system. We only used the base concepts of torrenting. If you have not idea what torrenting is I am about to explain.
So when sending a receiving files, typically you request a file from a computer, and that computer sends you back the data bit by bit, and your computer stores the data it receives bit by bit.
Torrenting changes this up. What happens is a user creates a file called a torrent. In that file they list the file they intend to share. They then take that file and break it up into small pieces.
For example imagine you want to share a file that is 100K. You make a torrent file and in it you list the file you are sharing, and then you break that file up into 100 1K pieces. Each of those pieces is hashed to ensure validity and security. You then share the torrent file.
Some when someone downloads the torrent file from you, they can requires the file piece by piece, and then put all the pieces together when they have them all to recreate the file. When the next person comes along, they can request pieces from both the original person who created the torrent, and from you who have downloaded it also, allowing them to download twice as fast 50% from you and 50% from the other person.
Because the file is hashed, the data is secured and cannot be changed or altered in any way.
So understanding how torrenting works let me explain the mining process on Contractless.
First you create a header for block. Then you hash that and check if its within the target difficulty. If not increase nonce and hash again. When a block is found, you add transactions from your mempool which were 100% validated before adding to memory.
You then save the block and create the torrent file thus securing all the data and the order its in. Just like the merkle hash this ensures no one can change the order, the contents, or add anything to the block. Then the torrent file is shared with other nodes. They download the file. As more and more nodes download syncing the file gets faster also.
So what we changed was we removed merkle hash tree, validated the transactions completely before adding to mempool, added transactions to the block after mining a block instead of selecting before, secured the transactions with torrent hashing instead of merkle tree root.
This makes verifying blocks 100s of times fasters as we don't have to do all this hash verification. If anyone did alter a block, the hash would not match any piece requested by other nodes and would never get shared.
Further it allows us to detect orphan chains by merely comparing the torrent data from one node to another without having to compare the data inside the blocks allowing us to detect orphan chains quicker. Orphan chains are what happens when two different nodes both find a block at the same time. Both submit to other nodes who validate. Eventually the network has to decide which nodes contain the actual correct data, the other chain is called an orphan chain and must correct.
So we can perform our mining and verification MUCH MUCH faster than other chains.