mt-manager

command module
v0.9.10 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: MIT Imports: 4 Imported by: 0

README ΒΆ

🌳 *MerkleTries Manager - High-Performance In-Memory Multi-MerkleTree Manager with disk-based snapshots

Go Version License Tests Coverage

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.Map for 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
  1. Lock-Free Reads: Using sync.Map for concurrent access without locks
  2. Minimal Blocking: Snapshot capture blocks TreeManager for only ~80Β΅s
  3. Batch Optimization: PebbleDB batch writes with Snappy compression
  4. Cache Efficiency: Tree root caching to avoid recomputation
  5. 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 compatibility
  • HashFuncKeccak256 - 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 fmt before 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


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 ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
cache_topn.go
cache_topn.go

Jump to

Keyboard shortcuts

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