This project is a simplified blockchain implementation in Go. It includes basic structures for Blocks, Transactions, and a Blockchain.
type Block struct {
Transactions []*Transaction
PrevBlockHash []byte
Hash []byte
Timestamp int64
}
type Transaction struct {
Data []byte
}
type TransactionData struct {
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Signature string `json:"signature"`
}
type Blockchain struct {
Blocks []*Block
}
In this implementation, a Merkle tree is used to ensure the integrity of the transactions within a block. The Merkle tree is constructed using a bottom-up approach, where leaf nodes represent individual transactions, and internal nodes represent the hash concatenation of their child nodes.
To validate the integrity of a block, the Merkle root checksum is calculated and compared with the one stored in the block:
func ValidateBlockIntegrity(b *Block) bool {
merkleRootChecksum := NewMerkleTree(b.Transactions).GetMerkleRootChecksum()
return bytes.Equal(merkleRootChecksum, b.MerkleRootChecksum)
}
You can use this blockchain implementation for educational purposes, understanding the basic concepts of blockchains, and experimenting with Go programming.
Feel free to explore the code and modify it according to your learning needs.
To run the project, use the following commands:
git clone https://github.com/phucthuan1st/simplified-blockchain
cd simplified-blockchain
go run main.go
The blockchain provides a command-line interface with the following usage:
Usage:
No flag --> Interactive mode
--help --> Display command outline
--show --> Display all blocks from the current chain database
--addblock --> Create a new block and add it to the chain
Optional Flags:
--db pathToDBFile --> Specify the path to the database file (default: MYCHAINPATH or ./data/mychain.json)
Contributions are welcome. If you find any issues or want to enhance the project, please create a pull request.
This project is licensed under the GNU General Public License v3.0. Feel free to use, modify, and distribute the code in accordance with the terms of the license.
Feel free to explore the code and modify it according to your learning needs.
Happy learning and happy coding!