beef

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 22 Imported by: 2

Documentation

Overview

Package beef provides storage and retrieval functionality for BEEF (Background Evaluation Extended Format) data in overlay services.

BEEF is a compact format for representing Bitcoin transactions along with their merkle proofs, enabling SPV (Simplified Payment Verification) without needing the full blockchain.

The package includes implementations for:

  • Redis: Fast in-memory storage with optional persistence and TTL
  • MongoDB: Document storage with replication support
  • SQLite: Embedded database for local deployments
  • Filesystem: Directory-based storage for simple file persistence
  • LRU: In-memory cache with size limits and fallback support
  • JungleBus: Network fetching from JungleBus service

Storage Interface:

The BeefStorage interface defines two core operations:

  • SaveBeef: Store BEEF data associated with a transaction ID
  • LoadBeef: Retrieve BEEF data by transaction ID

Example Usage:

// Create Redis BEEF storage with TTL and fallback
beefStore, err := beef.NewRedisBeefStorage("redis://localhost:6379?ttl=24h", fallbackStorage)

// Save BEEF data
err = beefStore.SaveBeef(ctx, &txid, beefBytes)

// Load BEEF data
beefData, err := beefStore.LoadBeef(ctx, &txid)

Storage Chaining:

Multiple storage backends can be chained for caching and fallback:

diskStorage := beef.NewFilesystemBeefStorage("/data/beef")
redisCache := beef.NewRedisBeefStorage("redis://localhost:6379?ttl=1h", diskStorage)
lruCache := beef.NewLRUBeefStorage(50*1024*1024, redisCache)  // 50MB LRU -> Redis -> Disk

Implementation Notes:

  • BEEF data is stored as binary data (base64 encoded in Redis)
  • Transaction IDs are used as keys for efficient lookup
  • Storage implementations should handle concurrent access safely
  • LoadTx is now a standalone function, not a method on BeefStorage

Index

Constants

This section is empty.

Variables

View Source
var BeefKey = "beef"
View Source
var ErrNotFound = errors.New("not-found")

Functions

func LoadTx

func LoadTx(ctx context.Context, storage BeefStorage, txid *chainhash.Hash, chaintracker *headers_client.Client) (*transaction.Transaction, error)

LoadTx loads a transaction from any BeefStorage implementation with optional merkle path validation

func LoadTxFromBeef

func LoadTxFromBeef(ctx context.Context, beefBytes []byte, txid *chainhash.Hash, chaintracker *headers_client.Client) (*transaction.Transaction, error)

LoadTxFromBeef is a helper function that loads a transaction from BEEF bytes and optionally validates its merkle path

func ParseSize

func ParseSize(sizeStr string) (int64, error)

ParseSize parses size strings like "100mb", "1gb", "512KB" into bytes

Types

type BeefStorage

type BeefStorage interface {
	LoadBeef(ctx context.Context, txid *chainhash.Hash) ([]byte, error)
	SaveBeef(ctx context.Context, txid *chainhash.Hash, beefBytes []byte) error
}

func CreateBeefStorage

func CreateBeefStorage(connectionStrings []string) (BeefStorage, error)

CreateBeefStorage creates a hierarchical stack of BeefStorage implementations from a slice of connection strings. Each storage layer uses the next as its fallback.

The first connection string creates the top-level storage (checked first), and the last creates the bottom-level fallback (checked last).

Supported formats:

  • lru://?size=100mb or lru://?size=1gb (in-memory LRU cache with size limit)
  • redis://localhost:6379?ttl=24h (Redis with optional TTL parameter)
  • sqlite:///path/to/beef.db or sqlite://beef.db
  • file:///path/to/storage/dir
  • junglebus:// (fetches from JungleBus API)
  • ./beef.db (inferred as SQLite)
  • ./beef_storage/ (inferred as filesystem)

Example:

CreateBeefStorage([]string{"lru://?size=100mb", "redis://localhost:6379", "sqlite://beef.db", "junglebus://"})
Creates: LRU -> Redis -> SQLite -> JungleBus

type FilesystemBeefStorage

type FilesystemBeefStorage struct {
	// contains filtered or unexported fields
}

func NewFilesystemBeefStorage

func NewFilesystemBeefStorage(basePath string, fallback BeefStorage) (*FilesystemBeefStorage, error)

func (*FilesystemBeefStorage) LoadBeef

func (t *FilesystemBeefStorage) LoadBeef(ctx context.Context, txid *chainhash.Hash) ([]byte, error)

func (*FilesystemBeefStorage) SaveBeef

func (t *FilesystemBeefStorage) SaveBeef(ctx context.Context, txid *chainhash.Hash, beefBytes []byte) error

type JunglebusBeefStorage

type JunglebusBeefStorage struct {
	// contains filtered or unexported fields
}

func NewJunglebusBeefStorage

func NewJunglebusBeefStorage(junglebusURL string, fallback BeefStorage) *JunglebusBeefStorage

func (*JunglebusBeefStorage) LoadBeef

func (t *JunglebusBeefStorage) LoadBeef(ctx context.Context, txid *chainhash.Hash) ([]byte, error)

func (*JunglebusBeefStorage) SaveBeef

func (t *JunglebusBeefStorage) SaveBeef(ctx context.Context, txid *chainhash.Hash, beefBytes []byte) error

type LRUBeefStorage

type LRUBeefStorage struct {
	// contains filtered or unexported fields
}

func NewLRUBeefStorage

func NewLRUBeefStorage(maxBytes int64, fallback BeefStorage) *LRUBeefStorage

NewLRUBeefStorage creates a new LRU cache with the specified maximum size in bytes

func (*LRUBeefStorage) LoadBeef

func (t *LRUBeefStorage) LoadBeef(ctx context.Context, txid *chainhash.Hash) ([]byte, error)

func (*LRUBeefStorage) SaveBeef

func (t *LRUBeefStorage) SaveBeef(ctx context.Context, txid *chainhash.Hash, beefBytes []byte) error

func (*LRUBeefStorage) Stats

func (t *LRUBeefStorage) Stats() (currentBytes int64, maxBytes int64, entryCount int)

Stats returns cache statistics

type RedisBeefStorage

type RedisBeefStorage struct {
	// contains filtered or unexported fields
}

func NewRedisBeefStorage

func NewRedisBeefStorage(connString string, fallback BeefStorage) (*RedisBeefStorage, error)

func (*RedisBeefStorage) LoadBeef

func (t *RedisBeefStorage) LoadBeef(ctx context.Context, txid *chainhash.Hash) ([]byte, error)

func (*RedisBeefStorage) SaveBeef

func (t *RedisBeefStorage) SaveBeef(ctx context.Context, txid *chainhash.Hash, beefBytes []byte) error

type SQLiteBeefStorage

type SQLiteBeefStorage struct {
	// contains filtered or unexported fields
}

func NewSQLiteBeefStorage

func NewSQLiteBeefStorage(dbPath string, fallback BeefStorage) (*SQLiteBeefStorage, error)

func (*SQLiteBeefStorage) Close

func (t *SQLiteBeefStorage) Close() error

func (*SQLiteBeefStorage) LoadBeef

func (t *SQLiteBeefStorage) LoadBeef(ctx context.Context, txid *chainhash.Hash) ([]byte, error)

func (*SQLiteBeefStorage) SaveBeef

func (t *SQLiteBeefStorage) SaveBeef(ctx context.Context, txid *chainhash.Hash, beefBytes []byte) error

Jump to

Keyboard shortcuts

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