v2.rs

Overview

This file defines a generic handler struct ExtMessagesHandler designed to process external messages received via an HTTP interface. It implements the Handler trait for asynchronous request handling using the Salvo web framework. The handler is responsible for:

The structure and behavior of this handler are highly parameterized by types representing messages, message converters, BP resolvers, and other utilities, making it adaptable for various message processing logic.


Structs and Implementations

ExtMessagesHandler<TMessage, TMsgConverter, TBPResolver, TBocByAddrGetter, TSeqnoGetter>

A generic struct representing the external messages handler.

Type parameters:

Fields:

Methods:

Usage example:

let handler = ExtMessagesHandler::<MyMessageType, MyMsgConverter, MyBPResolver, MyBocGetter, MySeqnoGetter>::new();

Trait Implementations

Handler Trait Implementation for ExtMessagesHandler

Implements the Handler trait asynchronously to process HTTP requests.

Constraints on type parameters:


handle(&self, req: &mut Request, depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl)

Asynchronously handles a single HTTP request representing an external message.

Parameters:

Workflow:

  1. Acquire WebServer instance from depot:
    If the WebServer state is missing, immediately respond with an internal server error.

  2. Retrieve ExternalMessage:
    Extracts the external message payload from the depot.

  3. Authorization check:

    • Determines if authorization is required using is_auth_required().

    • Checks if authorized by bearer token (AUTHORIZED_BY_BK_KEY).

    • If authorization is required and not authorized, validates the token from the message asynchronously.

    • Responds with detailed error responses on token verification failures (BAD_TOKEN, INVALID_SIGNATURE, TOKEN_EXPIRED).

  4. Block Producer resolution:

    • Resolves the active block producer(s) for the message's thread ID using the bp_resolver.

    • If the current node is not the active BP, responds with an error advising to resend the message to the active BP.

  5. Message conversion:

    • Converts the low-level TVM message into the domain message type TMessage using the into_external_message callback.

    • On failure, responds with an internal server error indicating message rejection.

  6. Telemetry metrics reporting:

    • Extracts the message sent time from headers.

    • Reports delivery duration to metrics if available.

  7. Message queueing:

    • Sends the converted message via the incoming_message_sender channel for asynchronous processing.

    • Awaits feedback about the processing outcome.

  8. Response generation:

    • On successful feedback, constructs an ExtMsgResponse with the message result and active producers, responds with HTTP 200 OK.

    • On error or unknown status, responds with internal server error.

  9. Telemetry reporting:

    • Reports message processing duration and status code to metrics.

Return:

Error Handling:


Important Implementation Details


Interactions with Other Components


Visual Diagram

classDiagram
class ExtMessagesHandler {
+new()
+handle()
}
class WebServer {
+bp_resolver: FnMut
+incoming_message_sender: Sender
+issue_token()
+metrics
+into_external_message: Fn
}
class ExternalMessage {
}
ExtMessagesHandler --> WebServer : obtains from depot
ExtMessagesHandler --> ExternalMessage : reads from depot
ExtMessagesHandler --> "Authorization Module" : validates tokens
ExtMessagesHandler --> "Telemetry Utilities" : reports metrics
ExtMessagesHandler --> "Error Rendering" : renders error responses

This class diagram illustrates the primary relationships where ExtMessagesHandler interacts with the WebServer and related modules to process and respond to external messages.