special_messages.rs

Overview

This file extends the BlockBuilder struct with functionality to execute a specific category of internal messages called Dapp Config messages. These messages are related to updating the configuration of decentralized applications (Dapps) within the blockchain context. The primary method execute_dapp_config_messages orchestrates the creation, distribution, execution, and management of these Dapp Config messages during block production.

The method handles:

This file interacts closely with blockchain configuration, message routing, optimistic state management, and internal execution threading, all critical parts of the block production pipeline.


Detailed Explanation

BlockBuilder::execute_dapp_config_messages

pub(super) fn execute_dapp_config_messages(
    &mut self,
    blockchain_config: &BlockchainConfig,
    block_unixtime: u32,
    block_lt: u64,
    check_messages_map: &mut Option<HashMap<AccountAddress, BTreeMap<u64, UInt256>>>,
) -> anyhow::Result<()>

Purpose

Executes all queued Dapp Config messages during block construction, ensuring that configuration updates for Dapps are processed in the current block.

Parameters

Returns

Functionality & Workflow

  1. Initialization of Config Messages:

    • Clones self.dapp_minted_map to iterate over minted Dapp tokens.

    • Filters out entries with zero minted tokens.

    • For each entry, retrieves corresponding credit configuration from self.dapp_credit_map.

    • Skips configs marked as unlimited.

    • Calculates the config address using calculate_dapp_config_address.

    • Creates a "config touch" message with current block Unix time and minted amount.

  2. Message Routing and Caching:

    • Determines the destination address (dst_addr) and forms a routing tuple (AccountRouting).

    • Wraps the message into a WrappedMessage.

    • Checks if the destination belongs to the current optimistic state (thread):

      • If yes, caches the message in produced_internal_messages_to_the_current_thread.

      • Otherwise, caches it in produced_internal_messages_to_other_threads.

  3. Serialization and Envelope Creation:

    • Serializes the message.

    • Creates a MsgEnvelope with forwarding fees.

    • Creates an EnqueuedMsg for scheduling.

    • Generates an OutMsg with a default transaction (since these messages lack a parent transaction).

    • Stores the OutMsg in self.out_msg_descr for output message tracking.

  4. Parallel Execution:

    • Maintains a set of active destination accounts and a queue of active external threads.

    • Executes messages in parallel up to the allowed parallelization_level.

    • Ensures no two messages targeting the same destination run simultaneously.

    • Uses self.execute to run the message, passing execution time limits set to none (ExecutionTimeLimits::NO_LIMITS).

    • Collects execution results asynchronously:

      • Upon completion, applies post-transaction processing with self.after_transaction.

      • Frees destination slots for new messages.

  5. Loop Termination:

    • Continues until all config messages are executed and all threads are completed.

    • Logs progress at various stages for debugging and traceability.

Usage Example

let mut block_builder = BlockBuilder::new(...);
let blockchain_config = get_blockchain_config();
let block_unixtime = current_unix_time();
let block_lt = current_block_logical_time();
let mut check_messages_map = None;

block_builder.execute_dapp_config_messages(
    &blockchain_config,
    block_unixtime,
    block_lt,
    &mut check_messages_map,
)?;

Important Implementation Details


Interactions with Other Components


Mermaid Diagram: Structure of execute_dapp_config_messages

flowchart TD
A[Start: execute_dapp_config_messages] --> B[Iterate dapp_minted_map]
B --> C{value != 0 and !is_unlimit?}
C -->|No| B
C -->|Yes| D[Calculate config address]
D --> E[Create config touch message]
E --> F{Destination belongs to current thread?}
F -->|Yes| G[Cache in current thread map]
F -->|No| H[Cache in other threads map]
G --> I[Serialize message & create envelope]
H --> I
I --> J[Add OutMsg to output descriptor]
J --> K[Add message to config_messages queue]
K --> L[Initialize active_destinations & active_ext_threads]
L --> M{Parallel execution loop}
M --> N[Start message execution if parallel slots available]
N --> O[Add destination to active_destinations]
O --> M
M --> P{Check completed threads}
P --> Q[Process finished thread]
Q --> R[Remove destination from active_destinations]
R --> M
M --> S{All messages executed and threads empty?}
S -->|No| M
S -->|Yes| T[End: return Ok]

This documentation provides a comprehensive breakdown of the special_messages.rs file, focusing on the execution of Dapp Config messages within the block-building process. For further details on related types such as Message, AccountAddress, and execution threading, see their respective topics: Message Structure, Account Management, and Threaded Execution Model.