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):
DEFAULT_MAX_RELAYS_PER_GROUP: Maximum number of relays per groupDEFAULT_MAX_MESSAGES_PER_GROUP: Maximum messages stored per group in the cacheDEFAULT_MAX_GROUP_NAME_LENGTH: Maximum length of group name in bytesDEFAULT_MAX_GROUP_DESCRIPTION_LENGTH: Maximum length of group description in bytesDEFAULT_MAX_ADMINS_PER_GROUP: Maximum number of admin pubkeys per groupDEFAULT_MAX_RELAYS_PER_WELCOME: Maximum number of relays in a welcome messageDEFAULT_MAX_ADMINS_PER_WELCOME: Maximum number of admin pubkeys in a welcome messageDEFAULT_MAX_RELAY_URL_LENGTH: Maximum length of a relay URL in bytes
§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§
- Group
Scoped Snapshot - A group-scoped snapshot that only contains data for a single group.
- MdkMemory
Storage - A memory-based storage implementation for MDK.
- Memory
Storage Snapshot - A snapshot of all in-memory state that can be restored later.
- Validation
Limits - 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.