Skip to main content

Crate mdk_memory_storage

Crate mdk_memory_storage 

Source
Expand description

Memory-based storage implementation for MDK.

This module provides a memory-based storage implementation for MDK (Marmot Development Kit). It implements the MdkStorageProvider trait, allowing it to be used as an in-memory storage backend.

Memory-based storage is non-persistent and will be cleared when the application terminates. It’s useful for testing or ephemeral applications where persistence isn’t required.

§Unified Storage Architecture

This implementation stores all MLS and MDK state in-memory. It supports snapshot and restore operations for rollback scenarios, analogous to SQLite savepoints.

Note: Snapshot and restore operations are atomic. create_snapshot() acquires a global read lock and restore_snapshot() acquires a global write lock on the storage state, ensuring consistency in multi-threaded environments.

§Memory Exhaustion Protection

This implementation includes input validation to prevent memory exhaustion attacks. The following limits are enforced (with configurable defaults via ValidationLimits):

§Customizing Limits

You can customize these limits using ValidationLimits and the builder pattern:

use mdk_memory_storage::{MdkMemoryStorage, ValidationLimits};

let limits = ValidationLimits::default()
    .with_cache_size(2000)
    .with_max_messages_per_group(5000)
    .with_max_relays_per_group(50);

let storage = MdkMemoryStorage::with_limits(limits);

Structs§

GroupScopedSnapshot
A group-scoped snapshot that only contains data for a single group.
MdkMemoryStorage
A memory-based storage implementation for MDK.
MemoryStorageSnapshot
A snapshot of all in-memory state that can be restored later.
ValidationLimits
Configurable validation limits for memory storage.

Constants§

DEFAULT_MAX_ADMINS_PER_GROUP
Default maximum number of admin pubkeys allowed per group. This prevents unbounded growth of the admin set.
DEFAULT_MAX_ADMINS_PER_WELCOME
Default maximum number of admin pubkeys allowed in a welcome message. This prevents oversized welcome messages from consuming excessive memory.
DEFAULT_MAX_GROUP_DESCRIPTION_LENGTH
Default maximum length of a group description in bytes (not characters). Multi-byte UTF-8 characters count as multiple bytes toward this limit. This prevents oversized group metadata from consuming excessive memory.
DEFAULT_MAX_GROUP_NAME_LENGTH
Default maximum length of a group name in bytes (not characters). Multi-byte UTF-8 characters count as multiple bytes toward this limit. This prevents oversized group metadata from consuming excessive memory.
DEFAULT_MAX_MESSAGES_PER_GROUP
Default maximum number of messages stored per group in the messages_by_group_cache. When this limit is reached, the oldest messages are evicted from the per-group cache. This prevents a single hot group from consuming excessive memory.
DEFAULT_MAX_RELAYS_PER_GROUP
Default maximum number of relays allowed per group to prevent memory exhaustion. This limit prevents attackers from growing a single cache entry unboundedly.
DEFAULT_MAX_RELAYS_PER_WELCOME
Default maximum number of relays allowed in a welcome message. This prevents oversized welcome messages from consuming excessive memory.
DEFAULT_MAX_RELAY_URL_LENGTH
Default maximum length of a relay URL in bytes. This prevents oversized relay URLs from consuming excessive memory.