Skip to content

A Blockchain with Proof-of-Work Mining, Transaction Handling, SHA-256 Block Hashing, Chain Validation, and JSON Exporting. Includes Support for Cryptocurrency-Like Transactions, Adjustable Mining Difficulty and Full Blockchain Integrity Checks.

Notifications You must be signed in to change notification settings

WillKirkmanM/blockchain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

BlockChain

A Blockchain with Proof-of-Work Mining, Transaction Handling, SHA-256 Block Hashing, Chain Validation, and JSON Exporting. Includes Support for Cryptocurrency-Like Transactions, Adjustable Mining Difficulty and Full Blockchain Integrity Checks.

Features

  • Proof of Work Mining: Implements a mining algorithm with adjustable difficulty
  • Transaction System: Support for cryptocurrency-like transactions between addresses
  • Chain Validation: Full blockchain validation including hash verification and proof-of-work checks
  • Balance Calculation: Calculate balances for any address across the entire blockchain
  • JSON Export: Export the entire blockchain to JSON format
  • Genesis Block: Automatic creation of the initial block in the chain

Structure

The blockchain consists of:

Core Types

  • Transaction: Represents transfers between addresses with amount
  • Block: Contains transactions, timestamp, hash, and proof-of-work nonce
  • Blockchain: Manages the chain of blocks and mining difficulty

Key Methods

  • NewBlockchain(): Creates a new blockchain with genesis block
  • AddBlock(transactions): Mines and adds a new block to the chain
  • ValidateChain(): Validates the entire blockchain integrity
  • GetBalance(address): Calculates balance for a given address
  • PrintBlockchain(): Displays the complete blockchain

Usage

Running the Application

go run main.go

Example Output

Creating blockchain with genesis block...
Mining block 1...
Block mined: 0000a1b2c3d4e5f6...
Mining block 2...
Block mined: 0000f6e5d4c3b2a1...

Blockchain:
Index: 0
Timestamp: 1640995200
Previous Hash: 0
Hash: 0000123abc...
Nonce: 0
Transactions: []
--------------------------------------------------
...

Is blockchain valid? true

Alice's balance: -7.00
Bob's balance: 4.00
Charlie's balance: 3.00

Creating Custom Transactions

// Create transactions
transactions := []Transaction{
    {From: "Alice", To: "Bob", Amount: 50.0},
    {From: "Bob", To: "Charlie", Amount: 25.0},
}

// Add to blockchain
blockchain.AddBlock(transactions)

Validating the Chain

isValid := blockchain.ValidateChain()
fmt.Printf("Blockchain is valid: %t\n", isValid)

Technical Details

Mining Algorithm

The implementation uses a proof-of-work algorithm that requires finding a hash with a specific number of leading zeros (determined by the difficulty level). The default difficulty is set to 4, meaning hashes must start with "0000".

Hash Calculation

Blocks are hashed using SHA-256 with the following components:

  • Block index
  • Timestamp
  • Previous block hash
  • Nonce value
  • All transaction data (from, to, amount)

Security Features

  • Immutable Chain: Each block references the previous block's hash
  • Proof of Work: Computational work required to add new blocks
  • Transaction Integrity: All transactions are included in block hashes
  • Chain Validation: Comprehensive validation of the entire chain

Dependencies

This implementation uses only Go standard library packages:

  • crypto/sha256 - For cryptographic hashing
  • encoding/hex - For hexadecimal encoding
  • encoding/json - For JSON serialisation
  • Standard packages for string manipulation and formatting

Configuration

You can modify the blockchain behavior by changing:

// In NewBlockchain()
Difficulty: 4, // Number of leading zeros required in hash

Higher difficulty values make mining more computationally expensive but more secure.

About

A Blockchain with Proof-of-Work Mining, Transaction Handling, SHA-256 Block Hashing, Chain Validation, and JSON Exporting. Includes Support for Cryptocurrency-Like Transactions, Adjustable Mining Difficulty and Full Blockchain Integrity Checks.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages