This is a simple blockchain implementation in Go. It consists of:
- Transactions with a sender, receiver, and amount
- Blocks that contain transactions and the hash of the previous block
- A Blockchain that consists of a series of blocks
- Generate new blocks containing transactions
- Calculate the hash for blocks
- Validate blocks by comparing hashes
- Maintain an immutable transaction history
The key components:
Transaction
struct - A basic transaction between a sender and receiverBlock
struct - Contains transactions, metadata, and cryptographic hashBlockchain
struct - Sequence of blocks with hash pointers
Core functionality:
NewBlock()
- Create and initialize new blockscalculateHash()
- Generate SHA-256 hash for blocksAddBlock()
- Append new blocks to the blockchain
The main()
function demonstrates creating a blockchain, adding blocks, and printing the results.
To run the example:
go run blockchain.go
This will initialize a blockchain, add some sample blocks, and print details to stdout.
Possible improvements:
- Persist the blockchain data structure
- Implement consensus algorithms
- Add transaction signatures and verification
- Optimize hashing and serialization
- Create miners that compete to add blocks
- Enable peer-to-peer network communication
- Add RPC interface for queries and updates
This code is released under the MIT License. Feel free to modify and reuse according to the terms.