README
ΒΆ
π³ *MerkleTries Manager - High-Performance In-Memory Multi-MerkleTree Manager with disk-based snapshots
A high-performance, production-ready Merkle Tree implementation in Go with support for multiple trees, atomic snapshots, and advanced hashing algorithms. Designed for blockchain applications, trading systems, and distributed databases.
β¨ Features
Core Functionality
- π High Performance: Lock-free operations with
sync.Mapfor concurrent access - π Cryptographic Hashing: Support only Blake3 at now
- π² Multiple Trees: Manage multiple independent Merkle trees with a single manager
- β‘ Batch Operations: Efficient batch insert/update/delete operations
- π― Zero-Copy Design: Minimal allocations and memory overhead
Advanced Features
- πΈ Atomic Snapshots: Create consistent snapshots across multiple trees
- πΎ Persistent Storage: PebbleDB-backed snapshot storage with compression
- π Lock-Free Snapshots: Snapshot creation with minimal blocking (~80Β΅s)
- π Async Operations: Fire-and-forget asynchronous snapshot creation
- π Performance Metrics: Built-in metrics for monitoring and debugging
Tree Manager
- π Global Root Hash: Compute unified hash across all managed trees
- π Tree Discovery: List and query all managed trees
- ποΈ Configurable: Flexible configuration for different use cases
- π§Ή Auto Cleanup: Automatic resource management and cleanup
π¦ Installation
go get github.com/aleksraiden/mt-manager
π Quick Start
Basic Usage
package main
import (
"fmt"
"github.com/aleksraiden/mt-manager"
)
// Define your data structure
type Account struct {
ID uint64
Balance uint64
Nonce uint64
}
func (a *Account) Key() []byte {
key := make([]byte, 8)
binary.BigEndian.PutUint64(key[:], a.ID)
return key
}
func (a *Account) Serialize() []byte {
buf := make([]byte, 24)
binary.BigEndian.PutUint64(buf[0:8], a.ID)
binary.BigEndian.PutUint64(buf[8:16], a.Balance)
binary.BigEndian.PutUint64(buf[16:24], a.Nonce)
return buf
}
func main() {
// Create a tree manager
mgr := merkletree.NewManager[*Account](merkletree.DefaultConfig())
// Create or get a tree
accountsTree := mgr.GetOrCreateTree("accounts")
// Insert data
account := &Account{ID: 1, Balance: 1000, Nonce: 0}
accountsTree.Insert(account)
// Get data
retrieved, exists := accountsTree.Get(account.Key())
if exists {
fmt.Printf("Account balance: %d\n", retrieved.Balance)
}
// Compute tree root
root := accountsTree.Root()
fmt.Printf("Tree root: %x\n", root)
// Compute global root across all trees
globalRoot := mgr.ComputeGlobalRoot()
fmt.Printf("Global root: %x\n", globalRoot)
}
Snapshots
// Create manager with snapshot support
mgr, err := merkletree.NewManagerWithSnapshot[*Account](
merkletree.DefaultConfig(),
"./snapshots.db",
)
if err != nil {
log.Fatal(err)
}
defer mgr.CloseSnapshots()
// Insert data into multiple trees
accountsTree := mgr.GetOrCreateTree("accounts")
ordersTree := mgr.GetOrCreateTree("orders")
accountsTree.Insert(&Account{ID: 1, Balance: 1000, Nonce: 0})
ordersTree.Insert(&Order{ID: 100, Price: 50})
// Create synchronous snapshot
version, err := mgr.CreateSnapshot()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Snapshot created: %x\n", version)
// Create asynchronous snapshot (non-blocking)
resultChan := mgr.CreateSnapshotAsync()
// ... continue working ...
result := <-resultChan
if result.Error != nil {
log.Printf("Snapshot failed: %v", result.Error)
} else {
fmt.Printf("Async snapshot: %x (took %v)\n", result.Version[:8], result.Duration)
}
// Load snapshot
if err := mgr.LoadFromSnapshot(&version); err != nil {
log.Fatal(err)
}
// Get snapshot metadata
meta, err := mgr.GetSnapshotMetadata()
fmt.Printf("Total snapshots: %d, Size: %d bytes\n", meta.Count, meta.TotalSize)
Batch Operations
tree := mgr.GetOrCreateTree("accounts")
// Batch insert
accounts := []*Account{
{ID: 1, Balance: 1000, Nonce: 0},
{ID: 2, Balance: 2000, Nonce: 1},
{ID: 3, Balance: 3000, Nonce: 2},
}
tree.InsertBatch(accounts)
// Batch delete
keys := []byte{[^8]
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 2},
}
tree.DeleteBatch(keys)
π Performance Benchmarks
Scalability Tests
Dataset Sizes
π§ͺ Running Tests
Run All Tests
# Basic tests
go test ./... -v
# With race detector
go test ./... -race -v
# With coverage
go test ./... -cover -coverprofile=coverage.out
go tool cover -html=coverage.out
Run Benchmarks
# All benchmarks
go test -bench=. -benchmem
# Specific benchmarks
go test -bench=BenchmarkTree -benchmem
go test -bench=BenchmarkSnapshot -benchmem
go test -bench=BenchmarkHash -benchmem
# Extended benchmarks
go test -bench=. -benchmem -benchtime=10s
# CPU profiling
go test -bench=BenchmarkTreeInsert -cpuprofile=cpu.prof
go tool pprof cpu.prof
Snapshot Tests
# Snapshot functionality
go test -v -run=TestSnapshot
# Snapshot concurrency
go test -v -run=TestSnapshotConcurrent -race
# Snapshot stress test
go test -v -run=TestSnapshotStress -timeout=30m
Stress Tests
# High concurrency stress test
go test -v -run=TestConcurrentStress -timeout=10m
# Memory stress test
go test -v -run=TestMemoryStress -timeout=15m
ποΈ Architecture
Core Components
merkletree/
βββ tree.go # Core Merkle tree implementation
βββ node.go # Tree node structure
βββ manager.go # Multi-tree manager
βββ snapshot.go # Snapshot manager (lock-free)
βββ snapshot_storage.go # PebbleDB storage backend
βββ hash.go # Pluggable hash algorithms
βββ config.go # Configuration options
Design Principles
- Lock-Free Reads: Using
sync.Mapfor concurrent access without locks - Minimal Blocking: Snapshot capture blocks TreeManager for only ~80Β΅s
- Batch Optimization: PebbleDB batch writes with Snappy compression
- Cache Efficiency: Tree root caching to avoid recomputation
- Memory Efficiency: Lazy hash computation and pruning support
βοΈ Configuration
config := &merkletree.Config{
MaxDepth: 3, // Maximum tree depth
HashFunc: merkletree.HashFuncBlake3, // Hash algorithm
EnablePruning: false, // Enable node pruning
CacheSize: 1000, // Root hash cache size
}
mgr := merkletree.NewManager[*Account](config)
Hash Functions
HashFuncBlake3- Default, fastest (recommended)HashFuncSHA256- SHA-256 for compatibilityHashFuncKeccak256- Ethereum-compatible
π Current Version: v0.1.0
β Implemented Features
- Basic Merkle tree operations (Insert/Get/Delete)
- Multi-tree manager with global root
- Lock-free concurrent access
- Batch operations
- Multiple hash algorithm support
- Atomic snapshots with PebbleDB
- Asynchronous snapshot creation
- Snapshot metadata and versioning
- Tree traversal and iteration
- Comprehensive test suite
- Performance benchmarks
πΊοΈ Roadmap
v0.2.0 - Transactions
- Atomic transactions across multiple trees
- Transaction isolation levels
- Optimistic concurrency control
- Transaction timeout and cleanup
- MVCC (Multi-Version Concurrency Control)
v0.3.0 - Advanced Arity
- 16-ary tree implementation (hex-tree)
- 256-ary tree implementation (byte-tree)
- Sparse node representation for memory efficiency
- Hybrid adaptive arity switching
- Depth-based arity optimization
v0.4.0 - Performance Optimizations
- SIMD optimizations for hashing
- Memory-mapped file support
- Compressed sparse row for sparse nodes
- CPU cache prefetching
- Zero-copy serialization
v0.5.0 - Advanced Features
- Merkle proof generation and verification
- Incremental snapshot updates (delta snapshots)
- Snapshot compression policies
- Tree diff and merge operations
- Remote snapshot replication
v1.0.0 - Production Ready
- Full documentation and examples
- Performance tuning guide
- Production deployment guide
- Monitoring and metrics dashboard
- Backward compatibility guarantees
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
# Clone repository
git clone https://github.com/aleksraiden/mt-manager.git
cd merkletree
# Install dependencies
go mod download
# Run tests
go test ./... -v
# Run benchmarks
go test -bench=. -benchmem
Code Style
- Follow standard Go conventions
- Run
go fmtbefore committing - Ensure all tests pass
- Add tests for new features
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments
- Inspired by Ethereum's Merkle Patricia Tree
- Uses PebbleDB for persistent storage
- Uses Blake3 for fast cryptographic hashing
- Uses MessagePack for efficient serialization
π§ Contact
- GitHub: @yourusername
- Issues: GitHub Issues
Built with β€οΈ for high-performance blockchain and distributed systems
***
## π¨ Additional files to create:
### **LICENSE** (MIT License)
MIT License
Copyright (c) 2026 [Your Name]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Documentation
ΒΆ
There is no documentation for this package.