helpers.rs

Overview

The helpers.rs file provides utility functions for handling and processing TON Virtual Machine (TVM) messages within an HTTP server context. Its primary responsibilities include decoding and parsing base64-encoded TVM messages into structured Message objects and extracting specific timestamp metadata from HTTP headers. This file relies on types and functions from the tvm_block and tvm_types crates to deserialize and interpret the binary message data.


Functions

parse_message

pub fn parse_message(id: &str, message_b64: &str) -> Result<Message, String>
let msg_id = "msg123";
let encoded_msg = "te6ccgEBAQEA..."; // base64 string
match parse_message(msg_id, encoded_msg) {
    Ok(message) => println!("Parsed message: {:?}", message),
    Err(e) => eprintln!("Failed to parse message: {}", e),
}

extract_ext_msg_sent_time

pub fn extract_ext_msg_sent_time(headers: &salvo::http::HeaderMap) -> Option<u64>
use salvo::http::HeaderMap;

let mut headers = HeaderMap::new();
headers.insert("X-EXT-MSG-SENT", "1680000000".parse().unwrap());

if let Some(sent_time) = extract_ext_msg_sent_time(&headers) {
    println!("Message sent time: {}", sent_time);
} else {
    println!("No sent time header found or invalid format");
}

Implementation Details and Algorithms


Interactions with Other Parts of the System


Diagram: Function Workflow in helpers.rs

flowchart TD
A["parse_message(id, message_b64)"]
A --> B[Decode base64 string]
B --> C[Read single root BOC]
C --> D[Construct Message from cell]
D -->|Success| E[Return Message]
B -->|Failure| F[Return decode error]
C -->|Failure| G[Return deserialization error]
D -->|Failure| H[Return parsing error]
I["extract_ext_msg_sent_time(headers)"]
I --> J[Get "X-EXT-MSG-SENT" header]
J --> K[Convert header to string]
K --> L[Parse string to u64]
L -->|Success| M["Return Some(timestamp)"]
J -->|Missing| N[Return None]
K -->|Error| N
L -->|Error| N

This flowchart illustrates the stepwise processing and error handling paths of the two main functions in this file.