Forge - Consensus-Agnostic Blockchain Framework

Forge is a flexible, production-ready framework for building blockchain consensus mechanisms in Go. It provides a comprehensive set of abstractions and utilities that enable developers to implement any consensus algorithm (PoW, PoS, BFT, etc.) without reinventing common blockchain infrastructure.
🎯 Key Features
- Consensus-Agnostic Design - Implement any consensus algorithm using clean interfaces
- Plugin Architecture - Extensible system with hooks, events, and custom extensions
- Production-Ready Components - Battle-tested implementations for common blockchain needs
- Comprehensive Testing - Full test coverage with mocks and integration tests
- Performance Monitoring - Built-in metrics collection with Prometheus support
- Example Implementations - Complete proof-of-work example to learn from
🚀 Installation
go get zde37.com/forge
Requirements
- Go 1.21 or higher
- Make (for build automation)
- mockgen (for generating mocks)
Install Development Tools
go install go.uber.org/mock/mockgen@latest
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
⚡ Quick Start
1. Using the Framework
package main
import (
"context"
"log"
"zde37.com/forge/builder"
"zde37.com/forge/interfaces"
)
func main() {
ctx := context.Background()
myConsensus := &MyConsensusImplementation{
}
consensusBuilder := builder.NewConsensusBuilder(myConsensus)
consensusBuilder.
WithWorker(&MyWorker{}).
WithValidator(&MyValidator{}).
WithParameter("difficulty", uint64(16)).
WithValidationMode("sequential").
WithWorkerCount(4)
consensus, err := consensusBuilder.BuildWithContext(ctx)
if err != nil {
log.Fatal(err)
}
if err := consensus.Start(ctx); err != nil {
log.Fatal(err)
}
}
2. Running the Proof-of-Work Example
cd examples/proof-of-work
go run cmd/main.go
go run cmd/main.go -miners=8 -block-interval=5s -duration=120s
🏗️ Architecture
Forge follows a modular, interface-based architecture that separates concerns and enables flexibility:
┌─────────────────────────────────────────────────────────┐
│ Application Layer │
│ (Your Consensus Implementation) │
└─────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ Forge Framework │
├───────────────┬───────────────┬─────────────────────────┤
│ Interfaces │ Plugin │ Core │
│ │ System │ Components │
├───────────────┼───────────────┼─────────────────────────┤
│ • Consensus │ • Hooks │ • Builder Pattern │
│ • Block │ • Events │ • Worker Pools │
│ • Validator │ • Extensions │ • Validation Pipeline │
│ • Worker │ • Registry │ • Difficulty Calc │
│ • Storage │ │ • Hash Functions │
└───────────────┴───────────────┴─────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ Infrastructure Layer │
├───────────────┬───────────────┬─────────────────────────┤
│ Storage │ Metrics │ Error Handling │
│ • Memory │ • Collector │ • Retry Logic │
│ • File │ • Prometheus │ • Circuit Breaker │
│ │ • Tracking │ • Recovery │
└───────────────┴───────────────┴─────────────────────────┘
📦 Core Modules
Interfaces
Core abstractions for consensus mechanisms:
Consensus - Main consensus engine interface
Block - Block data structure
Validator - Validation logic
Worker - Work processing (mining, validation)
Storage - Persistence layer
Builder
Fluent API for constructing consensus mechanisms:
- Pre-configured builders for common consensus types
- Validation and configuration management
- Factory patterns for PoW, PoS, PBFT, Raft
Worker
Distributed work processing framework:
- Worker pools with multiple distribution strategies
- Concurrent job processing
- Metrics and throughput tracking
Validation
Flexible validation pipeline:
- Sequential and parallel validation modes
- Composable validators
- Rich error reporting
Plugin System
Extensibility through plugins:
- Hook system with 14+ injection points
- Event bus with pub/sub pattern
- Extension registry for custom logic
Storage
Multiple storage backends:
- In-memory storage for testing
- File-based persistent storage
- Transaction support with rollback
Metrics
Comprehensive observability:
- Counters, gauges, histograms, summaries
- Prometheus exposition format
- System and application metrics
Error Handling
Robust error management:
- Structured error types
- Retry with exponential backoff
- Circuit breaker pattern
- Panic recovery
🔧 Examples
Proof-of-Work Implementation
The examples/proof-of-work directory contains a complete Bitcoin-style PoW implementation:
- Multi-threaded mining with worker pools
- Dynamic difficulty adjustment
- Transaction pool management
- Block validation pipeline
- Event-driven architecture
- Real-time metrics
Running the Example
cd examples/proof-of-work
go run cmd/main.go
go run cmd/main.go \
-miners=8 \
-tx-generators=5 \
-tx-rate=200ms \
-block-interval=10s \
-duration=300s
🧪 Testing
Run Tests
make test
make test-race
make test-coverage
go test ./worker/... -v
Generate Mocks
make mocks
make refresh
Code Quality
make fmt
make lint
make vet
📖 Documentation
Module Documentation
Each module has detailed documentation in the main doc file:
- Builder Module - Consensus construction patterns
- Worker Module - Distributed work processing
- Validation Module - Validation pipeline architecture
- Plugin Module - Extensibility and hooks
- Storage Module - Persistence layer
- Metrics Module - Observability and monitoring
- Error Module - Error handling strategies
🤝 Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Workflow
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
Code Standards
- Follow Go best practices and idioms
- Maintain test coverage above 80%
- Update documentation for new features
- Add examples for complex features
📊 Performance
Forge is designed for production use with excellent performance characteristics:
- Concurrent Processing - Leverage Go's goroutines for parallel execution
- Memory Efficient - Minimal allocations with pooling where appropriate
- Scalable - Worker pools scale with available CPU cores
- Observable - Built-in metrics for performance monitoring
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For questions, issues, or suggestions, please open an issue on GitHub or feel free to reach out anytime hi@zde37.com