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.
- 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
The blockchain consists of:
Transaction: Represents transfers between addresses with amountBlock: Contains transactions, timestamp, hash, and proof-of-work nonceBlockchain: Manages the chain of blocks and mining difficulty
NewBlockchain(): Creates a new blockchain with genesis blockAddBlock(transactions): Mines and adds a new block to the chainValidateChain(): Validates the entire blockchain integrityGetBalance(address): Calculates balance for a given addressPrintBlockchain(): Displays the complete blockchain
go run main.go
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
// Create transactions
transactions := []Transaction{
{From: "Alice", To: "Bob", Amount: 50.0},
{From: "Bob", To: "Charlie", Amount: 25.0},
}
// Add to blockchain
blockchain.AddBlock(transactions)
isValid := blockchain.ValidateChain()
fmt.Printf("Blockchain is valid: %t\n", isValid)
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".
Blocks are hashed using SHA-256 with the following components:
- Block index
- Timestamp
- Previous block hash
- Nonce value
- All transaction data (from, to, amount)
- 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
This implementation uses only Go standard library packages:
crypto/sha256- For cryptographic hashingencoding/hex- For hexadecimal encodingencoding/json- For JSON serialisation- Standard packages for string manipulation and formatting
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.