This project is a simple blockchain implementation written in Go. It includes functionality for creating blocks, adding transactions, implementing a Proof-of-Work (PoW) consensus mechanism, and interacting with the blockchain via a Command-Line Interface (CLI).
-
Blockchain Structure:
A chain of blocks linked together through cryptographic hashes. -
Transactions:
Transactions are stored in each block, showcasing the core idea of a blockchain as a ledger. -
Proof of Work (PoW):
A computational challenge that miners must solve to create a valid block. -
Block Details:
Each block contains:Index
: The block's position in the chain.Timestamp
: When the block was created.Transactions
: Data stored in the block.Nonce
: A value adjusted to meet the PoW requirements.Previous Hash
: The hash of the previous block in the chain.Current Hash
: The hash representing the block's contents.
The cli.go
file provides a command-line interface to interact with the blockchain.
Commands:
- Get Balance
getbalance -address ADDRESS
Fetch the balance for the given address.
- Create Blockchain
createblockchain -address ADDRESS
Initializes a new blockchain and rewards the address with the genesis block.
- Print Blockchain
printchain
Prints all blocks in the blockchain.
- Send Coins
send -from FROM -to TO -amount AMOUNT
Creates a transaction and mines a new block.
- Create Wallet
createwallet
Generates a new wallet and address.
- List Addresses
listaddresses
Lists all addresses stored in the wallet.
Prerequisites
- Go programming language installed (version 1.19 or later recommended).
Installation
- Clone the repository:
git clone https://github.com/ayushn2/blockchainx.git cd blockchainx
- Build the project:
go build
- Start the CLI:
go run main.go COMMAND [OPTIONS]
Replace COMMAND with one of the CLI commands mentioned above.
- Create a Blockchain:
go run main.go createblockchain -address your-address
- Print the Blockchain:
go run main.go printchain
- Send Coins:
go run main.go send -from sender-address -to receiver-address -amount 10
- Get Balance:
go run main.go getbalance -address your-address
- Create a Wallet:
go run main.go createwallet
- List Addresses:
go run main.go listaddresses
- Implement a peer-to-peer network.
- Enhance transaction validation.
- Add a smart contract-like feature for custom logic.
Contributions are welcome! Feel free to submit issues or pull requests.