blockchain

module
v0.0.0-...-8b83558 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2025 License: Apache-2.0

README ΒΆ

Blockchain (Golang)

Blockchain is a complete, semantically correct implementation of a blockchain protocol written entirely in Go. It draws inspiration from both Bitcoin and Ethereum.

This project includes everything you'd expect from a working blockchain system: a Proof-of-Work consensus algorithm, peer-to-peer networking, a node pool for decentralized coordination, and a fully functioning wallet system.

πŸš€ Why I Built This

I built this project to gain a deep, practical understanding of how blockchain protocols operate at a low level. While theoretical knowledge is widely available, few resources offer the hands-on insight that comes from building a system from scratch. This project challenged me to master concurrency, peer-to-peer networking, protocol design and cryptographically audit-able databases.

πŸ”§ Features

βœ… Proof-of-Work Consensus
  • Transactions are cryptographically verified and batched into blocks.
  • Blocks are mined using Proof-of-Work algorithm.
  • Mined blocks are submitted back to the network for verification.
🌐 Peer-to-Peer Architecture
  • Nodes connect in a decentralized mesh network.
  • Automatic syncing and broadcasting of new transactions, blocks and peers.
  • Fault-tolerant and designed for dynamic membership (nodes can join/leave at any time).
πŸ’Ό Wallet System
  • Client cryptography; generating keys, signing transactions.
  • Submits signed transactions to the network.
  • Pulls account information from the network.

πŸ“¦ Installation & Usage

This project is designed to run out of the box with no setup. You can run a node by simply just executing the run.sh script. It will initialize the node, the node default configuration will check if there is an existing private key for this node, if not, one will be generated and the node will be started.

Once your node is running:

  1. Generate a wallet/account

    go run api/cmd/wallet/wallet.go generate --key path/to/priv.key
    
  2. Check wallet balance and account info

    go run api/cmd/wallet/wallet.go info --key path/to/priv.key
    
  3. Transfer tokens from the genesis account

    go run api/cmd/wallet/wallet.go transfer --key ops/genesis.key --amount 1000 --to <account-id>
    
Optional

Launch an alternate node

./run.sh --run-alt

This node will automatically be started with a random port and temporary storage, allowing you to simulate a multi-node environment, once the node is stopped the node data is deleted. You can start as many of them as you want, well until you run out ports. :D

πŸ§ͺ Developer Tools & Load Testing

To simulate real-world scenarios and stress test the network, you can use the included developer tools:

  1. Generate multiple accounts

    go run api/cmd/dev/genload/genload.go generate -accounts 10
    
  2. Seed generated accounts with initial balances

    go run api/cmd/dev/genload/genload.go seed --key ops/genesis.key --amount 1500
    
  3. Create randomized transactions between accounts

    go run api/cmd/dev/genload/genload.go transfer
    

These tools help simulate network activity, test node synchronization, and validate transaction integrity across the pool.

πŸ“˜ Blockchain Concepts (Quick Overview)

  • Blockchain: A distributed ledger of immutable, cryptographically linked blocks.
  • Proof-of-Work (PoW): A consensus mechanism where nodes solve computational puzzles to add blocks.
  • Node: A participant in the network that validates, relays, and optionally mines transactions.
  • Wallet: A cryptographic key pair that controls access to funds and signs transactions.
  • Transaction: A signed message transferring value from one wallet to another, recorded in the blockchain.

"The best way to understand a protocol is to build it."


Directories ΒΆ

Path Synopsis
api
cmd/blockchain command
cmd/dev
Package dev contains development tools and experiments.
Package dev contains development tools and experiments.
cmd/wallet command
Package wallet contains a command line implementation for the blockchain.
Package wallet contains a command line implementation for the blockchain.
http/routes
Package route contains all the HTTP API routes.
Package route contains all the HTTP API routes.
http/routes/check
Package check maintains the web based API for system checks.
Package check maintains the web based API for system checks.
http/routes/v1/private
Package private maintains the private APIs for this node.
Package private maintains the private APIs for this node.
http/routes/v1/public
Package public maintains the public APIs for this node.
Package public maintains the public APIs for this node.
wallet
Package wallet contains the blockchain wallet implementation.
Package wallet contains the blockchain wallet implementation.
app
database
Package database contains blockchain models and database interfaces.
Package database contains blockchain models and database interfaces.
genesis
Package genesis provides a function to load the genesis data.
Package genesis provides a function to load the genesis data.
mempool
Package mempool maintains the blockchain memory pool implementation.
Package mempool maintains the blockchain memory pool implementation.
mempool/selector
Package selector provides different transaction selecting algorithms.
Package selector provides different transaction selecting algorithms.
merkle
Package merkle provides an implementation of a merkel tree for validation support for the blockchain.
Package merkle provides an implementation of a merkel tree for validation support for the blockchain.
node
Package node handles peer-to-peer networking for blockchain nodes.
Package node handles peer-to-peer networking for blockchain nodes.
peer
Package peer maintains the node's peer to peer implementation.
Package peer maintains the node's peer to peer implementation.
signature
Package signature provides functions to sign and verify data.
Package signature provides functions to sign and verify data.
storage
Package storage provides a data store support to store the blockchain database.
Package storage provides a data store support to store the blockchain database.
storage/bolt
Package bolt provides a bbolt-based implementation of the blockchain Storage interface.
Package bolt provides a bbolt-based implementation of the blockchain Storage interface.
storage/disk
This implements the disk storage, it is a DEVELOPMENT ONLY storage device that writes and reads the blockchain on disk as JSON.
This implements the disk storage, it is a DEVELOPMENT ONLY storage device that writes and reads the blockchain on disk as JSON.
worker
Package worker handles mining, peer updates, and transaction sharing.
Package worker handles mining, peer updates, and transaction sharing.
internal
middleware
Package middleware provides middleware support to the web package.
Package middleware provides middleware support to the web package.
validator
Package validator provides validation for structs.
Package validator provides validation for structs.
web
Package web provides a lightweight web framework that builds upon Go’s standard http package to enhance functionality.
Package web provides a lightweight web framework that builds upon Go’s standard http package to enhance functionality.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL