Skip to content
/ go-coin Public

A simplified blockchain implementation in Go featuring proof-of-work mining, P2P networking, and account-based transactions.

Notifications You must be signed in to change notification settings

ldzoid/go-coin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoCoin

A simplified blockchain implementation in Go featuring proof-of-work mining, P2P networking, and account-based transactions.

Features

  • Proof-of-Work Mining: SHA-256 based mining with adjustable difficulty
  • P2P Network: Nodes discover and sync blocks with peers automatically
  • Account-Based Model: Accounts with balances and nonces (similar to Ethereum)
  • Transaction Pool: Pending transactions are included in mined blocks
  • Wallet Management: ECDSA key pairs for signing transactions
  • RPC Interface: HTTP API for interacting with the node
  • CLI Tool: Command-line interface for sending transactions and checking balances

Quick Start

Running a Single Node

# Start node (creates wallet automatically)
go run cmd/gocoin/main.go

# In another terminal, use the CLI
go run cmd/gocoin-cli/main.go

Running Multiple Nodes (P2P Network)

# Terminal 1: Start first node
go run cmd/gocoin/main.go

# Terminal 2: Start second node and connect to first
go run cmd/gocoin/main.go -db ./data2 -p2p 127.0.0.1:6001 -rpc 8002 -peer 127.0.0.1:6000

# Terminal 3: Use CLI (connects to node on port 8001 by default)
go run cmd/gocoin-cli/main.go

CLI Commands

The CLI provides an interactive shell for interacting with your node:

> wallet
# Shows your wallet address and private key

> account <address>
# Check balance and nonce for any address

> send <to_address> <value>
# Send coins to another address

> help
# Show available commands

> quit
# Exit the CLI

How It Works

Mining

  • Nodes compete to mine blocks with a 1-3 second randomized delay
  • First node to find a valid proof-of-work broadcasts to peers
  • Block reward is automatically credited to the miner's wallet
  • Difficulty is set to 12 leading zero bits by default

P2P Synchronization

  • Nodes exchange VERSION messages to compare chain heights
  • When behind, nodes request missing blocks from peers
  • Both nodes actively compete to produce the next block
  • Chain reorganizations handled automatically

Transactions

  • Transactions require valid signatures from the sender
  • Each account has a nonce to prevent replay attacks
  • Pending transactions are stored in the mempool
  • Miners include pending transactions in new blocks

Project Structure

cmd/
  gocoin/       - Main node binary
  gocoin-cli/   - CLI tool for interacting with nodes
internal/
  consensus/    - Mining and block validation logic
  db/           - BadgerDB wrapper for blockchain storage
  p2p/          - Peer-to-peer networking and sync protocol
  rpc/          - HTTP RPC server
  state/        - Account state management
  txpool/       - Transaction pool
  types/        - Core types (Block, Transaction, etc.)
  wallet/       - ECDSA wallet and key management
  config/       - Configuration constants

Configuration

Default settings (in internal/config/config.go):

  • P2P Port: 6000
  • RPC Port: 8001
  • Difficulty: 12 bits
  • Block Reward: 10 coins
  • Database: ./data directory

Override with flags:

go run cmd/gocoin/main.go -db ./custom-db -p2p 127.0.0.1:7000 -rpc 9000

Example Session

# Start two nodes
Terminal 1: go run cmd/gocoin/main.go
Terminal 2: go run cmd/gocoin/main.go -db ./data2 -p2p 127.0.0.1:6001 -rpc 8002 -peer 127.0.0.1:6000

# Watch them compete to mine blocks and sync with each other

# Use CLI to interact
Terminal 3: go run cmd/gocoin-cli/main.go
> wallet
Address: 0x1a2b3c...
Private Key: 0xabc123...

> account 0x1a2b3c...
Balance: 150
Nonce: 15

> send 0xDESTINATION_ADDRESS 25
Transaction sent!

Notes

  • This is a simplified educational blockchain - not for production use
  • Wallets are stored in wallet.txt (keep this secure!)
  • Each node needs its own database directory
  • Nodes automatically discover and sync with peers
  • Mining difficulty can be adjusted in config.go

About

A simplified blockchain implementation in Go featuring proof-of-work mining, P2P networking, and account-based transactions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages