Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Problems
  • C
  • C++
  • Java
  • Python
  • JavaScript
  • Data Science
  • Machine Learning
  • Courses
  • Linux
  • DevOps
  • SQL
  • Web Development
  • System Design
  • Aptitude
  • GfG Premium
Open In App
Next Article:
How mining works?
Next article icon

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:

  1. 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.
  2. 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

Block8896
No. of transactions1800
Transaction volume$ 1349873
Timestamp2017-11-11 01:35:55
Relayed byViaBTC
Difficulty1765987453897.67
Size1063.67 kB
Nonce880
Block 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.

  1. 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.
  2. 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.
Block8896
No. of transactions1800
Transaction volume$ 13849673
Timestamp2017-11-11 01:35:55
Relayed byViaBTC
Difficulty1456324543655.677
Size1066.34 kB
Nonce880
Block hash000abddbcdef673bedb4 ...

Next Article
How mining works?

V

varungirdhar
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences