Skip to content

EduardoTerres/Mining-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Cryptocurrency Mining Simulation

Overview

This project is a distributed cryptocurrency mining simulation using shared memory, semaphores, and signals for synchronization. It includes:

  • A network of miners competing to solve a cryptographic function.
  • A monitor process that oversees the network and logs results.
  • Mechanisms to detect and remove dishonest miners.

Features

✅ Shared memory and semaphores for process synchronization.
✅ Secure handling of system signals (SIGINT, SIGUSR2) to prevent crashes.
✅ Efficient round-based mining with thread management.
Timeouts to prevent deadlocks.
Detection and removal of dishonest miners.


🛠️ How It Works

1️⃣ Initialization

  • Blocks SIGINT signals while initializing shared memory and semaphores.
  • Creates or accesses existing shared memory.
  • Sets up message queues for communication (execution continues even if unavailable).
  • Allocates memory for thread management and mining parameters.
  • Blocks SIGUSR2 and SIGINT during cycles to ensure predictable behavior.

2️⃣ Mining Process

  • Each miner adds its PID to the network and starts mining.
  • The process enters a suspended state (sigsuspend) awaiting SIGUSR2.
  • All miners compete to solve the same hash function.
  • A losing miner may still find the correct solution before its threads cancel.
  • 🛑 Solution: Introduced semaphores instead of signals to ensure stability.

3️⃣ Synchronization & Block Generation

  • The winning miner signals the others.
  • Miners follow an ordered execution:
    • Winner waits for losers to copy the block.
    • Losers wait for the winner to generate a new block.
  • Semaphores include a 3-second timeout to prevent deadlocks.
  • All miners must complete the round to maintain network integrity.

4️⃣ Data Storage & Monitoring

  • Blocks are stored in a linked list, allocating memory per round.
  • Miners log completed blocks into a file.
  • The monitor process only joins if at least one miner is active.
  • Miners send blocks after the monitor joins to prevent message queue overload.
  • Parent process filters blocks, while the child process writes them to a file.
  • Failed blocks are recorded but retried in the next round.

5️⃣ Security Against Dishonest Miners 🚨

  • Detects pirate miners who attempt to manipulate the network:
    • Using an incorrect hash function.
    • Attempting fake mining computations.
  • Since over 50% of votes can corrupt the blockchain, the winner terminates dishonest miners who vote against a valid solution.

📌 Self-Assessment & Improvements

  • ✅ Meets all project requirements, including the voting system and monitoring.
  • ✅ Extensive testing: Over 90% execution success rate, even with pirate miner attacks.
  • ✅ Memory management validated with Valgrind (no leaks or segmentation faults).
  • ❗ Potential improvements:
    • More robust failure handling.
    • Prevent deadlocks if a process crashes while holding a semaphore.

🔚 Conclusion

This project is a simplified cryptocurrency mining simulation with:

  • Robust synchronization mechanisms.
  • Secure handling of fraudulent miners.
  • Strong error management and logging.

While it’s not perfect, it effectively balances performance, security, and stability in a multi-process mining environment. 🚀


🏗️ Installation & Usage

📌 Requirements

  • Linux-based system (recommended)
  • GCC compiler
  • make
  • Basic knowledge of C programming

⚙️ Compilation

make

▶️ Running the Miner Network

./miner

📡 Running the Monitor

./monitor

📜 License

This project is licensed under the MIT License.

👥 Contributors


💡 Feedback & Contributions

Feel free to open issues or submit pull requests to improve this project! 🚀

About

A distributed cryptocurrency mining simulation for C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published