Skip to content

SummerOfBitcoin/code-challenge-2024-humblenginr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

This project simulates the process of Bitcoin mining by:

  • 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.

The project adheres to the difficulty target of 0000ffff00000000000000000000000000000000000000000000000000000000, ensuring that the block hash meets the required conditions.

Features

  • 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.

Mining Algorithm

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

To run the miner, simply execute the run.sh script:

./run.sh

Performance

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.

Future Work

Potential enhancements for the project include:

  • 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.

Acknowledgements

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.

About

A test for your understanding of Bitcoin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published