Skip to content

xSpoooK/spookcoin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦀 Mini Blockchain (Rust) — Educational PoC

A step-by-step Proof-of-Concept to build a Bitcoin-style mini blockchain in Rust.
Learn core blockchain concepts: blocks, hashing, Proof of Work, transactions, mining rewards, and optional networking.


🚀 Project Overview

This project is designed for educational purposes.
You'll build a simple blockchain from scratch, understand how Bitcoin works under the hood, and gradually add more features.

Core concepts covered:

  • Block structure and linking
  • SHA-256 hashing
  • Proof of Work (mining)
  • Transaction mempool
  • Mining rewards
  • Chain validation
  • Optional REST API and node synchronization

📍 Roadmap / Exercises

Exercise 1 — Create a Minimal Block

Goal: Understand the basic block structure.

Tasks:

  • Create a Block struct with:
    • index: u32
    • timestamp: u64
    • transactions: Vec<Transaction> (can be a simple struct or string for now)
    • previous_hash: String
  • Add a method to serialize the block into a string (for hashing later).

Success criteria:

  • You can create a block and print its serialized form.

Exercise 2 — Add SHA-256 Hashing

Goal: Understand cryptographic hashes.

Tasks:

  • Add a method compute_hash(&self) -> String to compute SHA-256 on the block's serialized content.
  • Ensure the hash changes if the block is modified.

Crates:

  • sha2 for hashing
  • serde + serde_json for serialization

Success criteria:

  • Printing block.compute_hash() works.
  • Changing block data changes the hash.

Exercise 3 — Build the Blockchain

Goal: Link blocks into a chain.

Tasks:

  • Create a Blockchain struct with chain: Vec<Block> and difficulty: usize.
  • Create a genesis block (previous_hash = "0").
  • Implement add_block() to create new blocks, set previous_hash, compute hash, and append to the chain.

Success criteria:

  • You can create a chain with multiple linked blocks.

Exercise 4 — Validate the Chain

Goal: Ensure blockchain integrity.

Tasks:

  • Implement is_chain_valid() -> bool:
    • Verify each block’s hash matches its contents.
    • Verify previous_hash matches the previous block.
  • Test by manually modifying a block.

Success criteria:

  • Any tampering breaks validation.

Exercise 5 — Add Proof of Work (Mining)

Goal: Implement mining similar to Bitcoin.

Tasks:

  • Add nonce: u64 to Block.
  • Implement mine(&mut self):
    • Increment nonce until the hash starts with difficulty zeros.

Success criteria:

  • Mining changes the hash to meet the difficulty.

Exercise 6 — Add Transactions

Goal: Simulate a mempool.

Tasks:

  • Create a Transaction struct: from: String, to: String, amount: f32.
  • Add pending_transactions: Vec<Transaction> in Blockchain.
  • Implement add_transaction(tx: Transaction) and mine_pending_transactions():
    • Create a block with pending transactions.
    • Mine it and add to the chain.
    • Clear pending transactions.

Success criteria:

  • Multiple transactions can be mined into a block.

Exercise 7 — Add Mining Rewards

Goal: Mimic Bitcoin’s block reward.

Tasks:

  • Automatically include a special transaction in mined blocks:
    • from = "network"
    • to = <miner_name>
    • amount = <reward>

Success criteria:

  • Miner earns coins each time a block is mined.

Exercise 8 — Optional: Minimal REST API

Goal: Turn blockchain into a node.

Tasks:

  • Use warp or actix-web to expose endpoints:
    • GET /chain → returns full blockchain
    • POST /transaction/new → add new transaction
    • GET /mine → mine pending transactions

Success criteria:

  • Interact with the blockchain via browser or HTTP client.

Exercise 9 — Optional: Node Synchronization

Goal: Learn distributed consensus.

Tasks:

  • Allow nodes to know each other.
  • Implement “longest valid chain wins”:
    • Compare chains between nodes
    • Keep the longest valid chain

Success criteria:

  • Nodes synchronize and resolve conflicts automatically.

🔧 Next Steps / Advanced

  • Implement a UTXO model
  • Sign transactions with ECDSA (k256 crate)
  • Add dynamic difficulty adjustment
  • Build a peer-to-peer network (TCP/WebSocket)
  • Limit block size and mempool rules

⚡ Recommended Rust Crates

  • sha2 — SHA-256 hashing
  • serde + serde_json — Serialization / JSON handling
  • chrono — Optional, for timestamps
  • warp / actix-web — Optional, REST API
  • k256 — Optional, for ECDSA signatures

📝 License

MIT / Educational use

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages