diff --git a/README.md b/README.md index 0ace566..62e73f7 100644 --- a/README.md +++ b/README.md @@ -1,67 +1,61 @@ -[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-24ddc0f5d75046c5622901739e7c5dd533143b0c8e959d652212380cedb1ea36.svg)](https://classroom.github.com/a/bWLxfecg) -# Summer of Bitcoin 2024: Mine your first block +# Bitcoin Miner + +This repository contains a Go-based implementation of a Bitcoin mining simulation, designed to validate and mine blocks based on a set of transactions. The project aims to simulate the core mechanics of Bitcoin's mining process, from validating transactions to solving the proof-of-work puzzle by finding a valid block hash below a specified difficulty target. ## Overview -In this challenge, you are tasked with the simulation of mining process of a block, which includes validating and including transactions from a given set of transactions. -The repository contains a folder `mempool` which contains JSON files. -These files represent individual transactions, some of which may be invalid. Your goal is to successfully mine a block by including only the valid transactions, following the specific requirements outlined below. -## Objective -Your primary objective is to write a script that processes a series of transactions, validates them, and then mines them into a block. The output of your script should be a file named `output.txt` that follows a specific format. +This project simulates the process of Bitcoin mining by: -## Requirements -### Input -- You are provided with a folder named `mempool` containing several JSON files. Each file represents a transaction that includes all necessary information for validation. -- Among these transactions, some are invalid. Your script should be able to discern valid transactions from invalid ones. +- **Validating Transactions**: It takes a set of transactions in JSON format, checks them for validity, and only includes valid transactions in the block. +- **Constructing the Block**: It creates a valid block, starting with a coinbase transaction and then adding the validated transactions. The block header is assembled based on Bitcoin's block structure. +- **Mining the Block**: A proof-of-work mining algorithm iterates over nonces to find a block hash that satisfies the target difficulty, mimicking the actual mining process. -### Output -Your script must generate an output file named `output.txt` with the following structure: -- First line: The block header. -- Second line: The serialized coinbase transaction. -- Following lines: The transaction IDs (txids) of the transactions mined in the block, in order. The first txid should be that of the coinbase transaction +The project adheres to the difficulty target of `0000ffff00000000000000000000000000000000000000000000000000000000`, ensuring that the block hash meets the required conditions. -### Difficulty Target -The difficulty target is `0000ffff00000000000000000000000000000000000000000000000000000000`. This is the value that the block hash must be less than for the block to be successfully mined. +## Features -## Execution -- Create a file named `run.sh` that contains the command to execute your script. This file should ideally contain a single command like `python main.py` or `node index.js`. -- Your script should autonomously perform all tasks when `run.sh` is executed, without requiring any manual intervention. +- **Transaction Validation**: Transactions are parsed and validated based on standard Bitcoin rules to ensure only valid transactions are mined into the block. +- **Efficient Block Construction**: The block is created by optimizing space usage, starting with a coinbase transaction and prioritizing transactions with the highest fee-to-size ratio. +- **Proof-of-Work Mining**: The mining algorithm implements a nonce search, iterating until a valid block hash is found, which meets the defined difficulty target. +- **SHA-256 Hashing**: The block header hash is computed using double SHA-256, following Bitcoin's hashing process. +- **Output Formatting**: The mined block and its transactions are written to an output file in the correct format. + +## Project Structure + +- `main.go`: Contains the core logic for transaction validation, block construction, and mining. +- `mempool/`: Directory with individual JSON files representing transactions to be validated. +- `run.sh`: Shell script to execute the mining process. +- `output.txt`: The output file with the mined block details. -## Evaluation Criteria -Your submission will be evaluated based on the following criteria: +## Mining Algorithm -- **Score**: Your code output will be scored bases on the fee collected and the amount of available block space utilised. **You must score at least 60 points to pass the challenge.** -- **Correctness**: The `output.txt` file must be correctly formatted in the manner described above. -- **Code Quality**: Your code should be well-organized, commented, and follow best practices. -- **Efficiency**: Your solution should process transactions and mine the block efficiently. +The mining process follows the proof-of-work mechanism: + +1. **Transaction Validation**: Transactions are parsed from the mempool, and invalid transactions are discarded based on Bitcoin's validation rules. +2. **Block Construction**: A block is assembled, with a coinbase transaction at the start, followed by valid transactions from the mempool. +3. **Nonce Iteration**: The miner searches for a nonce value that, when hashed with the block header, produces a block hash lower than the target. +4. **Difficulty Target**: The difficulty target is set to `0000ffff00000000000000000000000000000000000000000000000000000000`. The miner adjusts the nonce until the block hash is below this value. + +## Execution -## Document your work +To run the miner, simply execute the run.sh script: -Apart from the code, you must also publish a `SOLUTION.md` file explaining your solution in the following format: -- **Design Approach:** Describe the approach you took to design your block construction program, explain all the key concepts of creating a valid block. -- **Implementation Details:** Provide pseudo code of your implementation, including sequence of logic, algorithms and variables used etc. -- **Results and Performance:** Present the results of your solution, and analyze the efficiency of your solution. -- **Conclusion:** Discuss any insights gained from solving the problem, and outline potential areas for future improvement or research. Include a list of references or resources consulted during the problem-solving process. +```bash +./run.sh +``` -## What NOT to Do +## Performance -In this challenge, it's crucial to understand and adhere to the following restrictions. These are put in place to ensure that you engage with the core concepts of bitcoin and apply your problem-solving skills to implement the solution from first principles. +This miner efficiently processes and validates transactions from the mempool, maximizing the collected fees while respecting block size limitations. The nonce search algorithm is designed to find a valid block hash within a reasonable time, simulating real-world mining efficiency. -- **Do Not Use Bitcoin Libraries for Transaction Validation:** You must not use any Bitcoin-specific libraries or frameworks that automate transaction validation processes. The intent of this challenge is for you to understand and implement the validation logic manually. -- **Permissible Libraries:** The use of standard cryptographic libraries, such as secp256k1 for elliptic curve cryptography, and standard hashing libraries (e.g., for SHA-256) is allowed and encouraged. These libraries are essential for implementing the cryptographic underpinnings of bitcoin without reinventing the wheel. - - **Implement the Mining Algorithm Yourself:** You are required to implement the mining algorithm on your own. This includes creating a way to correctly form a block header, calculate the hash, and meet the challenge of finding a hash below a certain target. +## Future Work -### Plagiarism Policy: -Our plagiarism detection checker thoroughly identifies any instances of copying or cheating. Participants are required to publish their solutions in the designated repository, which is private and accessible only to the individual and the administrator. Solutions should not be shared publicly or with peers. In case of plagiarism, both parties involved will be directly disqualified to maintain fairness and integrity. +Potential enhancements for the project include: -### AI Usage Disclaimer: -You may use AI tools like ChatGPT to gather information and explore alternative approaches, but avoid relying solely on AI for complete solutions. Verify and validate any insights obtained and maintain a balance between AI assistance and independent problem-solving. +- **Multi-threaded Mining**: Optimizing the mining process to run in parallel, speeding up nonce searches. +- **Advanced Transaction Prioritization**: Implementing more complex transaction fee prioritization to maximize miner rewards. +- **Improved Block Space Optimization**: More efficient handling of block space to include as many high-fee transactions as possible. -## Why These Restrictions? -These restrictions are designed to deepen your understanding of bitcoin technicals. -By completing this assignment, you will gain hands-on experience with the technology that make bitcoin secure and trustless. -Remember, the goal of this challenge is not just to produce a working solution but to engage critically with the fundamental components of bitcoin. This is an opportunity to showcase your problem-solving skills and your ability to implement complex algorithms from scratch. +## Acknowledgements -## Additional Information -- This challenge is designed to test your understanding of bitcoin fundamentals, including transaction validation and block mining processes. -- While the challenge focuses on the simulation of these processes, you are encouraged to implement your solution in a way that demonstrates depth of understanding and creativity. +This project was inspired by and developed as part of the **Summer of Bitcoin 2024** challenge, which provided a foundation for exploring Bitcoin’s technical and cryptographic underpinnings.