go-bstrust

module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT

README

Go-BSTrust

A Go library for managing peer trust relationships using BadgerDB for persistent storage.

Features

  • Persistent Storage: Uses BadgerDB for reliable, fast key-value storage
  • Named Peer Trust Management: Add, remove, and check trust status of libp2p peers with custom names
  • Multiple Trust Names: Support for multiple trust names per peer with timestamps
  • Efficient Queries: Fast lookups and iteration over trusted peers
  • Thread-Safe: All operations are safe for concurrent access with proper mutex protection
  • Race Condition Free: Comprehensive testing with Go's race detector

Installation

go get github.com/piax/go-bstrust

Trust Manager CLI

The package includes a web-based trust manager CLI tool:

# Build the trust manager
go build -o trustmgr ./cmd/trustmgr

# Run the trust manager (default data directory: ./data)
./trustmgr

# Run with custom data directory
./trustmgr --data-dir=/path/to/data

# Show help
./trustmgr --help

The trust manager provides a web interface at http://localhost:9001 with the following features:

  • Trusted Peers List: View all trusted peers with their names and timestamps
  • Multiple Names Support: Peers with multiple trust names are displayed as separate rows
  • Column Resizing: Drag column borders to resize table columns
  • Bulk Untrust: Select multiple entries with checkboxes and untrust them
  • Real-time Updates: Automatic refresh after operations

Usage

Basic Usage
package main

import (
    "log"
    "os"
    
    "github.com/libp2p/go-libp2p/core/peer"
    "github.com/piax/go-bstrust/trustrepo"
)

func main() {
    // Create a new TrustRepo instance
    // BadgerDB will automatically create the data directory if it doesn't exist
    dataDir := "./data"
    repo, err := trustrepo.NewBadgerTrustRepo(dataDir)
    if err != nil {
        log.Fatal(err)
    }
    defer repo.Close()
    
    // Create a peer ID
    peerID, _ := peer.Decode("12D3KooWExample")
    
    // Trust a peer with a name
    err = repo.TrustNamedPeer("my-friend", peerID)
    if err != nil {
        log.Fatal(err)
    }
    
    // Trust the same peer with another name
    err = repo.TrustNamedPeer("colleague", peerID)
    if err != nil {
        log.Fatal(err)
    }
    
    // Check if peer is trusted
    trusted, err := repo.IsTrusted(peerID)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Peer trusted: %v\n", trusted)
    
    // Get trust information for the peer
    trustInfos, err := repo.GetTrustInfo(peerID)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Trust info: %+v\n", trustInfos)
    
    // Get all trusted peers
    trustedPeers, err := repo.TrustedPeers()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Trusted peers: %v\n", trustedPeers)
    
    // Remove a specific trust name
    err = repo.UnTrustName("colleague", peerID)
    if err != nil {
        log.Fatal(err)
    }
    
    // Untrust a peer completely
    err = repo.UnTrustPeer(peerID)
    if err != nil {
        log.Fatal(err)
    }
}
Interface

The TrustRepo interface provides the following methods:

type TrustInfo struct {
    Name      string    `json:"name"`
    TrustedAt time.Time `json:"trusted_at"`
}

type TrustRepo interface {
    TrustNamedPeer(name string, pid peer.ID) error  // Add a peer to trusted list with a name
    UnTrustName(name string, pid peer.ID) error     // Remove a specific trust name from a peer
    UnTrustPeer(pid peer.ID) error                  // Remove a peer from trusted list completely
    IsTrusted(pid peer.ID) (bool, error)            // Check if a peer is trusted
    TrustedPeers() ([]peer.ID, error)               // Get all trusted peers
    GetTrustInfo(pid peer.ID) ([]TrustInfo, error)  // Get all trust information for a peer
    Close() error                                   // Close the database connection
}

Implementation Details

BadgerDB Integration

The BadgerTrustRepo struct implements the TrustRepo interface using BadgerDB:

  • Storage Format: Peer IDs are stored with the key prefix "peer:" followed by the peer ID bytes
  • Value Format: Trust information is stored as JSON containing an array of TrustInfo structs with names and timestamps
  • Multiple Names: Each peer can have multiple trust names, each with its own timestamp
  • Transactions: All operations use BadgerDB transactions for consistency
  • Iteration: The TrustedPeers() method efficiently iterates over all stored peer keys
Key Features
  1. Efficient Storage: Uses BadgerDB's optimized key-value storage
  2. ACID Compliance: All operations are transactional
  3. Memory Efficient: Values are streamed to avoid loading all data into memory
  4. Error Handling: Comprehensive error handling for database operations
  5. Thread Safety: Protected by RWMutex for concurrent read/write operations
  6. Graceful Shutdown: Proper resource cleanup and error handling on close

Dependencies

  • github.com/dgraph-io/badger/v4 - BadgerDB key-value store
  • github.com/libp2p/go-libp2p/core - libp2p peer ID types

Testing

The package includes comprehensive tests covering all functionality:

# Run all tests
go test ./trustrepo -v

# Run tests with race detector
go test ./trustrepo -v -race

# Run benchmarks
go test ./trustrepo -bench=. -benchmem
Test Coverage
  • Basic Operations: TrustNamedPeer, UnTrustName, UnTrustPeer, IsTrusted, TrustedPeers, GetTrustInfo
  • Edge Cases: Non-existent peers, concurrent access, multiple names per peer
  • Persistence: Data persistence across repo instances with trust information
  • Resource Management: Proper cleanup and error handling
  • Thread Safety: Concurrent mixed operations and race condition testing
  • Performance: Benchmarks for all major operations

License

This project is licensed under the MIT License.

Directories

Path Synopsis
cmd
trustmgr command

Jump to

Keyboard shortcuts

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