How mining works? Last Updated : 17 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Cryptocurrencies are one of the hottest topics being discussed on the Internet today. But before understanding how mining works, we can ask ourselves the following two questions, these are: Why is mining necessary? Cryptocurrency mining is not just about making money. Miners have an important function in the blockchain network. They solve mathematical problems and approve other users’ transactions over the internet. Miners save blockchains from block fraud and hacker attacks and guarantee the network’s decentralization. But the main function of mining is different. The main function is to reach an agreement the basis on which a transaction can be considered valid so that no user will be able to spend coins which were already spent in another transaction. That’s why receiving bitcoin is just a side effect of doing useful work.What is the purpose of crypto miners? Whenever a cryptocurrency transaction is made, a cryptocurrency miner is responsible for ensuring the authenticity of information and updating the blockchain with the valid transaction. The mining process involves competing with other crypto miners to solve complicated mathematical problems with cryptographic hash functions that are associated with a block containing the transaction data. The first cryptocurrency miner to solve the puzzle is rewarded by being able to authorize the transaction, and in return for the service provided, crypto miners earn small amounts of cryptocurrency of their own. Now that we know the purpose of cryptocurrency mining and miners we can move ahead and understand how it works. For that, we can have a look at the structure of a block in the blockchain:Table - Block structure Block8896No. of transactions1800Transaction volume$ 1349873Timestamp2017-11-11 01:35:55Relayed byViaBTCDifficulty1765987453897.67Size1063.67 kBNonce880Block hash0000abcdcee78ab745b9f ... A block actually contains many more fields other than the ones mentioned above. But we will focus on the Block Hash and Nonce field for now. Block Hash - Well, the data in the blockchain can be "stored" by storing the hashes of data, such that you can at least verify that's the data whose hash was stored if you have the data to hash yourself and check against the one in the blockchain. A Merkle tree is one way of doing this. Refer to the following diagram to understand why a Merkle tree is used. Figure - Merkle treeSo to construct a Merkle tree you make a tree data structure with each of the leaf nodes containing the hash of a section of the data to be stored. From there you take the hash of the concatenation of the child nodes, and propagate the value up to the parent, continuing this process up to the tree until a final hash is generated. Even a small change at the leaves will be propagated upwards and change the resultant hash. A hash is a 256-bit hexadecimal number which is generated using a hashing function, e.g., Bitcoin uses the SHA256 Algorithm to generate a hash. We know that the purpose of a miner is to calculate the hash of the block for which he’ll be awarded. Seems easy enough. In order to level the field, every miner is restricted to a certain target hash. A target hash implies that for every block that miner mines if it’s calculated hash is greater than the target hash then it’s discarded.Nonce - This is where the concept of Nonce comes into play. Nonce stands for “Number used only once”. As we know the miner has to calculate the hash which should be below the target hash assigned to him. But he cannot change the Block number, Data, Previous Hash in order to guess a new hash due to the avalanche effect. The only field that he can vary is the Nonce. A nonce is a 32-bit number. So there can be a maximum of approximately 4 billion possible Nonce values as 2^(32) = 4, 294, 967, 296. The nonce is randomly selected for each iteration. So for each iteration, a random integer between 0 and 4, 294, 967, 296 is selected. Consider the following table:Figure - all possible hashesIn the above figure the circles represent a hash value obtained for a particular nonce value. The Black circles imply that a hash calculated is invalid. Green circle represents a valid hash. The TARGET assigned to a miner implies that a block can be mined by the miner only if the calculated hash consists of 4 leading zeros here (only as an example. Target hash can vary with multiple leading zeros). All hashes greater than that will be discarded. We start with a nonce value of 88 which as we can see, yields a hash greater than target hash. The nonce value is changed to 777 and the hash is recalculated. We repeat the process until we find a nonce which yields a hash whose value is less than the target hash. According to above figure, this happens at Nonce value 7778. Now the block can be authenticated by the miner and added to the Blockchain.Block8896No. of transactions1800Transaction volume$ 13849673Timestamp2017-11-11 01:35:55Relayed byViaBTCDifficulty1456324543655.677Size1066.34 kBNonce880Block hash000abddbcdef673bedb4 ... Comment More infoAdvertise with us Next Article How mining works? V varungirdhar Follow Improve Article Tags : Ethical Hacking Similar Reads What is Ethereum Virtual Machine and How it Works? The Ethereum Virtual Machine (EVM) is a crucial component of the Ethereum blockchain that enables the execution of smart contracts and decentralized applications (DApps). When developers write smart contracts in languages like Solidity, the EVM processes these contracts, managing state changes and e 12 min read Working of Tor Browser The Tor Browser is like a secret agent for your internet-related activities which helps you browse the web without revealing your identity. It is like wearing an invisible cloak online, that is what the Tor Browser does for your privacy. In this article, Let us understand the workings of the Tor bro 4 min read How Address Resolution Protocol (ARP) Works? When computer programs send or get messages, they usually use something called an IP address, which is like a virtual address. But underneath, the real talk happens using another type of address called a MAC address, which is like a device's actual home address.So, our goal is to find out the MAC ad 11 min read How does the Token-Based Authentication work ? Digital transformation brings security concerns for users to protect their identity from bogus eyes. According to US Norton, on average 8 lakh accounts are being hacked every year. There is a demand for high-security systems and cybersecurity regulations for authentication. Traditional methods rely 6 min read How Does Two-Factor Authentication (2FA) Work? Two-factor authentication (2FA) is a security system that requires two distinct forms of identification in order to access something. Two-factor authentication can be used to strengthen the security of an online account, a smartphone, or even a door. 2FA does this by requiring two types of informati 8 min read Like