lib.rs

Overview

This file implements the core web server infrastructure for handling external messages, account data, and BK set management within the system. It provides the WebServer struct which encapsulates the server configuration, dependencies, and runtime behavior. The server exposes HTTP routes supporting authentication, message validation, and shard state retrieval. It also integrates mechanisms for handling cryptographic signing, metrics collection, and BK set updates.

The file organizes middleware for request processing (e.g., authentication and metrics reporting) and defines utility functions such as TLS configuration generation. It leverages asynchronous programming and Tokio synchronization primitives to handle concurrent message processing and BK set state synchronization.


Entities and Components

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

A generic asynchronous web server struct parameterized by:

Properties

Methods

new(...) -> Self

Creates a new instance of WebServer.

Parameters:

Returns:

Usage example:

let server = WebServer::new(
    "127.0.0.1:8080",
    "./storage",
    incoming_sender,
    signing_request_sender,
    msg_converter,
    bp_resolver,
    boc_getter,
    seqno_getter,
    Some("owner_pubkey".to_string()),
    Some("keys.pem".to_string()),
    Some(metrics),
);
route(self) -> Router

Builds and returns the HTTP route tree for the server.

Returns:

run(self, bk_set_rx: watch::Receiver<ApiBkSet>) -> impl Future

Asynchronously runs the web server.

Behavior:

Parameters:

Usage example:

tokio::spawn(async move {
    server.run(bk_set_receiver).await;
});
issue_token(&self) -> Option<Token>

Generates an authorization token if signing keys and owner wallet public key are set.

Returns:


Functions and Handlers

rustls_config() -> RustlsConfig

Generates and returns a Rustls TLS configuration with self-signed certificates.

Usage:

Middleware Handlers

All handlers use the #[handler] attribute from the salvo framework and operate asynchronously.

report_block_request(req, res, depot, ctrl)

pass_unauthorized(req, res, depot, ctrl)

auth(req, res, depot, ctrl)

Authentication middleware.

validate_ext_message(req, res, depot, ctrl)

Validates incoming external messages:


Implementation Details and Algorithms


Interaction with Other Modules


Routing Structure Diagram

flowchart TD
WebServer -->|routes| Router_v2
Router_v2 --> Account_Route["/account"]
Router_v2 --> Messages_Route["/messages"]
Router_v2 --> BKSet_Route["/bk_set"]
Router_v2 --> BKSetUpdate_Route["/bk_set_update"]
Router_v2 --> SeqNo_Route["/default_thread_seqno"]
Router_v2 --> StorageLatest_Route["/storage_latest"]
Router_v2 --> Storage_Route["/storage/{*path}"]
Messages_Route --> Auth_Middleware
Messages_Route --> ValidateExtMsg_Middleware
Messages_Route --> ExtMessagesHandler
Account_Route --> Auth_Middleware
Account_Route --> BocByAddressHandler
SeqNo_Route --> Auth_Middleware
SeqNo_Route --> LastSeqnoHandler
Storage_Route --> ReportBlockRequest_Middleware
Storage_Route --> StaticDirHandler
subgraph Middleware
Auth_Middleware
ValidateExtMsg_Middleware
ReportBlockRequest_Middleware
end

Key Constants

Constant

Description

AUTH_HEADER

HTTP header name for authorization ("authorization").

PASS_UNAUTHORIZED_KEY

Depot key to mark requests that bypass authorization.

AUTHORIZED_BY_BK_KEY

Depot key indicating authorization by BK token.


Notes on Usage


This file is a central component for HTTP API exposure, message handling, authorization, and BK set state management within the system. It tightly integrates with other modules for cryptographic authentication, message parsing, and system metrics.