default_thread_seqno.rs

Overview

The default_thread_seqno.rs file defines the LastSeqnoHandler struct and its asynchronous HTTP request handler implementation. This handler is designed to serve the last sequence number (seqno) of a default thread in the context of a web server. It integrates with the web server's internal state and returns the latest sequence number as a JSON response or an error message if retrieval fails.

This file primarily facilitates handling HTTP requests by extracting the last seqno from the web server's state and formatting it appropriately for client consumption.


Struct: LastSeqnoHandler

pub struct LastSeqnoHandler<TMessage, TMsgConverter, TBPResolver, TBocByAddrGetter, TSeqnoGetter> {
    _marker: PhantomData<(TMessage, TMsgConverter, TBPResolver, TBocByAddrGetter, TSeqnoGetter)>,
}

Description

LastSeqnoHandler is a generic struct parameterized over five types related to message processing and blockchain data retrieval:

The struct uses PhantomData to hold these generic types without actually storing values, ensuring type coherence without runtime overhead.

Methods

new() -> Self

Creates a new instance of LastSeqnoHandler. It initializes the struct with a default PhantomData.

Usage Example:

let handler = LastSeqnoHandler::<MyMessage, MyConverter, MyResolver, MyBocGetter, MySeqnoGetter>::new();

Trait Implementation: Handler for LastSeqnoHandler

#[async_trait]
impl<TMessage, TMsgConverter, TBPResolver, TBocByAddrGetter, TSeqnoGetter> Handler
    for LastSeqnoHandler<TMessage, TMsgConverter, TBPResolver, TBocByAddrGetter, TSeqnoGetter>
where
    TMessage: Clone + Send + Sync + 'static + std::fmt::Debug,
    TMsgConverter: Clone + Send + Sync + 'static + Fn(tvm_block::Message, [u8; 34]) -> anyhow::Result<TMessage>,
    TBPResolver: Clone + Send + Sync + 'static + FnMut([u8; 34]) -> ResolvingResult,
    TBocByAddrGetter: Clone + Send + Sync + 'static + Fn(String) -> anyhow::Result<(String, Option<String>)>,
    TSeqnoGetter: Clone + Send + Sync + 'static + Fn() -> anyhow::Result<u32>,
{
    async fn handle(
        &self,
        _req: &mut Request,
        depot: &mut Depot,
        res: &mut Response,
        _ctrl: &mut FlowCtrl,
    )
}

Description

This implementation defines the asynchronous handle method for the LastSeqnoHandler, enabling it to process HTTP requests.

handle Method

Parameters

Functionality

  1. Retrieve WebServer State:
    Attempts to obtain the WebServer instance from the depot. The WebServer is parameterized over the same generic types and holds the application state relevant to message handling and sequence numbers.

  2. Error Handling:
    If the WebServer instance is not found, the response status is set to 500 Internal Server Error, and a corresponding message is rendered.

  3. Retrieve Sequence Number:
    Calls the get_default_thread_seqno function on the WebServer instance to obtain the latest sequence number.

  4. Response Generation:

    • On success, returns a JSON object with field "last_seq_no" containing the sequence number.

    • On failure, sets the response status to 500 Internal Server Error and renders the error message.

Return Value

handle returns () as it operates via side effects on the response.

Usage Example

The handler is intended to be registered with a web server framework route to respond to HTTP GET requests for the last seqno.

router.get("/last-seqno", LastSeqnoHandler::new());

Important Implementation Details


Interaction with Other System Components


Visual Diagram

flowchart TD
A[HTTP Request] --> B["LastSeqnoHandler.handle()"]
B --> C{Obtain WebServer}
C -- Success --> D["Call get_default_thread_seqno()"]
C -- Failure --> E[Respond 500: WebServer not found]
D --> F{Result}
F -- Ok(seq_no) --> G[Respond JSON {last_seq_no: seq_no}]
F -- Err(error) --> H[Respond 500: error message]

This flowchart illustrates the request handling workflow within the LastSeqnoHandler. It begins with an incoming HTTP request, attempts to access the WebServer state, retrieves the last sequence number, and sends back a JSON response or an error message accordingly.