from_maps.rs

Overview

This file defines the ThreadMessageQueueStateDiff struct and implements conversion logic to produce an updated ThreadMessageQueueState. It handles the application of incremental changes (diffs) to the thread message queue state, including adding and removing accounts, producing new messages, and consuming existing messages. The core functionality ensures that the state is kept consistent with respect to message ordering, message storage, and account presence.

This file interacts primarily with:

ThreadMessageQueueStateDiff Struct

pub struct ThreadMessageQueueStateDiff {
    initial_state: ThreadMessageQueueState,
    consumed_messages: HashMap<AccountAddress, HashSet<MessageIdentifier>>,
    removed_accounts: Vec<AccountAddress>,
    added_accounts: std::collections::BTreeMap<AccountAddress, AccountInbox>,
    produced_messages: HashMap<AccountAddress, Vec<(MessageIdentifier, Arc<WrappedMessage>)>>,
    db: MessageDurableStorage,
}

Purpose

Encapsulates the incremental changes to be applied to a thread message queue state including:

Builder Pattern

Uses typed_builder for ergonomic construction with visibility on builder methods and build method returning a Result<ThreadMessageQueueState>.


Conversion Implementation: From<ThreadMessageQueueStateDiff> for anyhow::Result<ThreadMessageQueueState>

This is the core method that applies the diff to the initial state to produce an updated state.

Parameters

Return Value

Detailed Workflow

  1. Initialize Working Copies:
    Clones messages (a BTreeMap<AccountAddress, AccountInbox>) and order_set (an OrderSet) from the initial state for mutation.

  2. Add Accounts:
    Inserts new accounts from added_accounts into messages. If the account is not already in the order set, it is inserted.

  3. Determine Intersection of Produced and Consumed Messages:
    Builds a set of message IDs that appear both in produced and consumed messages to avoid processing them twice.

  4. Add Produced Messages:
    For each account:

    • Retain messages that are either not in the intersection or have a redirect header.

    • Extend the tail sequence of the existing inbox with new messages.

    • Update the compacted history range based on database lookups of message sequences.

    • Ensure order set is updated with new accounts if needed.

  5. Remove Consumed Messages:
    For each account:

    • Retain only consumed messages not in the intersection.

    • Use MessagesRangeIterator to verify and update the message ranges.

    • Remove account from messages and order_set if empty after removal.

  6. Remove Accounts:
    Remove accounts listed in removed_accounts from messages and order_set.

  7. Update Cursor:
    Calculate a new cursor position based on the current size of messages.

  8. Construct New State:
    Returns a new ThreadMessageQueueState with updated messages, order set, and cursor.

Implementation Details and Assertions

Usage Example

let diff = ThreadMessageQueueStateDiff::builder()
    .with_initial_state(current_state)
    .with_consumed_messages(consumed_map)
    .with_removed_accounts(removed_vec)
    .with_added_accounts(added_map)
    .with_produced_messages(produced_map)
    .with_db(storage_handle)
    .build()?;

let updated_state: ThreadMessageQueueState = diff.into()?;

Test Module

Contains unit tests validating the behavior of ThreadMessageQueueStateDiff conversion:

The tests use helper functions to create empty states and messages, illustrating typical usage patterns.


Interaction with Other Components

See related topics such as ThreadMessageQueueState, MessageDurableStorage, and MessagesRangeIterator for in-depth details on these components.


Mermaid Diagram: Structure of from_maps.rs

classDiagram
class ThreadMessageQueueStateDiff {
-initial_state: ThreadMessageQueueState
-consumed_messages: HashMap<AccountAddress, HashSet<MessageIdentifier>>
-removed_accounts: Vec<AccountAddress>
-added_accounts: BTreeMap<AccountAddress, AccountInbox>
-produced_messages: HashMap<AccountAddress, Vec<(MessageIdentifier, Arc<WrappedMessage>)>>
-db: MessageDurableStorage
+from() Result<ThreadMessageQueueState>
}
ThreadMessageQueueStateDiff ..> ThreadMessageQueueState : uses
ThreadMessageQueueStateDiff ..> MessageDurableStorage : uses
ThreadMessageQueueStateDiff ..> HashMap : uses
ThreadMessageQueueStateDiff ..> HashSet : uses
ThreadMessageQueueStateDiff ..> AccountAddress : uses
ThreadMessageQueueStateDiff ..> MessageIdentifier : uses
ThreadMessageQueueStateDiff ..> WrappedMessage : uses
ThreadMessageQueueStateDiff ..> AccountInbox : uses
class ThreadMessageQueueState {
+messages: BTreeMap<AccountAddress, AccountInbox>
+order_set: OrderSet
+cursor: usize
}
class MessagesRangeIterator {
+new()
+next_range()
+remaining()
}
ThreadMessageQueueState ..> AccountInbox
AccountInbox ..> MessagesRangeIterator

This diagram visualizes the main struct and its relationships with key types used in the file. The conversion method (from) applies the diff to produce a new ThreadMessageQueueState.