ton-society / grants-and-bounties

TON Foundation invites talent to imagine and realize projects that have the potential to integrate with the daily lives of users.
https://ton.org/grants
313 stars 137 forks source link

Rust TL-B serialization #874

Closed DevNebulaX closed 3 days ago

DevNebulaX commented 1 week ago

Summary

TL-B serialization is a fundamental component of the TON blockchain, enabling the encoding and decoding of data structures in a compact, efficient, and schema-based binary format. To provide Rust developers with seamless support for this critical feature, we propose developing a TL-B serialization library in Rust. This library will allow developers to work with TL-B schemas directly in their Rust applications, enhancing the capabilities of TON-based tools and services.

Context

The TL-B (Type Language - Binary) format is widely used within the TON blockchain for serializing and deserializing data structures, such as messages, cells, and state data. While other programming languages have partial implementations or utilities for TL-B serialization, Rust lacks a comprehensive and developer-friendly library tailored for this purpose. By creating a dedicated TL-B serialization library in Rust, we can empower developers to build efficient and reliable applications in the TON ecosystem. Key features of the proposed library include: • Support for defining and parsing TL-B schemas directly in Rust. • Serialization and deserialization of TL-B data structures into binary format. • Bit-level data manipulation for precise TL-B compliance. • Recursive structure handling for complex TL-B schemas. • Error handling to ensure data integrity and conformity with TL-B specifications. • Integration with existing TON blockchain tools, such as tonlibjson, for seamless workflows.

Suppose we have a simple TL-B schema: use std::io::{self, Read, Write};

// Example struct matching the TL-B schema struct User { text: String, age: i32, }

// Serialization impl User { fn serialize(&self, writer: &mut impl Write) -> io::Result<()> { // Serialize the text as a string (length-prefixed, for example) let text_bytes = self.text.as_bytes(); let length = text_bytes.len() as u32; writer.write_all(&length.to_be_bytes())?; writer.write_all(text_bytes)?;

    // Serialize the age as a 32-bit integer
    writer.write_all(&self.age.to_be_bytes())?;
    Ok(())
}

// Deserialization
fn deserialize(reader: &mut impl Read) -> io::Result<Self> {
    // Read the text length
    let mut length_bytes = [0u8; 4];
    reader.read_exact(&mut length_bytes)?;
    let length = u32::from_be_bytes(length_bytes);

    // Read the text
    let mut text_bytes = vec![0u8; length as usize];
    reader.read_exact(&mut text_bytes)?;

    // Read the age
    let mut age_bytes = [0u8; 4];
    reader.read_exact(&mut age_bytes)?;
    let age = i32::from_be_bytes(age_bytes);

    Ok(Self {
        text: String::from_utf8(text_bytes).unwrap(),
        age,
    })
}

}

References

Develop a Rust library that implements TL-B serialization, enabling developers to encode, decode, and manipulate TL-B data structures effortlessly within Rust applications.

Estimate suggested reward

$5,000 USD in TON equivalent

ProgramCrafter commented 1 week ago

Please visit #444 also.

Speaking of improvements, I'd like infallible (and preferably non-panicking) store functions; they seem more optimal. They could be implemented as in https://gist.github.com/ProgramCrafter/c79293307b7df0740b87ae244072a4d3.

delovoyhomie commented 3 days ago

Thank you for your submission. After careful review, we regret to inform you that we cannot proceed with your application at this time, as a Rust SDK has already been implemented as part of the Bounty program (see bounty #182).