Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement get_config_param and Fix issue with masterchain addresses #14

Closed
wants to merge 3 commits into from

Conversation

AminRezaei0x443
Copy link
Contributor

@AminRezaei0x443 AminRezaei0x443 commented Jul 10, 2023

This PR introduces the following modifications to the library:

  1. Implements the get_config_param function, enabling the retrieval of config cells and the reading of data from them.
  2. Resolves a problematic implementation of store_address that affects masterchain addresses. The issue can be reproduced via the example provided.
use anyhow::{bail, Ok, Result};
use tonlib::address::TonAddress;
use tonlib::cell::CellBuilder;

fn test_address(addr_: &str) -> Result<()> {
    let addr = TonAddress::from_base64_url(addr_)?;
    let c = CellBuilder::new().store_address(&addr)?.build()?;
    let mut p = c.parser();
    let c_ = p.load_u8(2)?;
    let anycast = p.load_bit()?;
    let wc = p.load_u8(8)? as i8;
    let hash = p.load_uint(256)?;
    println!(
        "constructor: {}, anycast?: {}, wc: {}, hash: {}",
        c_, anycast, wc, hash
    );
    p = c.parser();
    let addr = p.load_address();
    if addr.is_err() {
        bail!("Couldn't load address");
    }
    if !addr.unwrap().to_base64_url().eq(addr_) {
        bail!("Serialized and deserialized addr don't match!");
    }
    Ok(())
}
fn main() -> Result<()> {
    // This is a normal addr (wc = 0) | Will work as expected
    let wc_addr = "EQATcgoK4eGMgackgnMF8aHUJLR0Pq_U0hzcmiwAhPbEMmw_";
    let r = test_address(wc_addr);
    assert!(r.is_ok());
    // This is a mc addr (wc = -1) | Will fail
    let mc_addr = "Ef8HujUcsUlU_vClVUnZRqHQfnhvrc8C241HOIqRL5XDe2P7";
    let r = test_address(mc_addr);
    r.unwrap();

    Ok(())
}

This code should ideally run without errors for both addresses. However, it fails for the second one, with unexpected changes in constructor and anycast flag values, as seen in the log:

constructor: 2, anycast?: false, wc: 0, hash: 8795433999317782811755257230391905142775921687175506829826532924721519313970
constructor: 3, anycast?: true, wc: -1, hash: 3495190060236973450600321995803361255911842138770757834326427501877748155259
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Couldn't load address', src/main.rs:35:7
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

A deeper look at the code reveals that the problem possibly arises from how wc is masked with 0xff but then stored as an i8, potentially causing issues due to sign bits. Adjusting the code to use store_u8 instead seems to fix the issue:

    /// Stores address without optimizing hole address
    pub fn store_raw_address(&mut self, val: &TonAddress) -> anyhow::Result<&mut Self> {
        self.store_u8(2, 0b10u8)?;
        self.store_bit(false)?;
        let wc = (val.workchain & 0xff) as i8;
        self.store_i8(8, wc)?;
        self.store_slice(&val.hash_part)?;
        Ok(self)
    }

@AminRezaei0x443
Copy link
Contributor Author

Closing in favor of #16, which resolves merge conflict + squashed clean commits.

dbaranovstonfi added a commit that referenced this pull request Aug 1, 2023
Resolve "Configurable timeout for jetton meta loader"

Closes #14

See merge request ston-fi/oss/tonlib-rs!23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant