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 #16

Merged
merged 4 commits into from
Aug 10, 2023

Conversation

AminRezaei0x443
Copy link
Contributor

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)
    }

commit ae86f31
Author: Amin Rezaei <AminRezaei0x443@gmail.com>
Date:   Mon Jul 10 01:19:22 2023 +0330

    bump: version 0.5.5

commit df2adfe
Author: Amin Rezaei <AminRezaei0x443@gmail.com>
Date:   Mon Jul 10 01:18:52 2023 +0330

    fix: issue with mc addresses in store_raw_address

commit eeb6838
Author: Amin Rezaei <AminRezaei0x443@gmail.com>
Date:   Mon Jul 10 01:16:45 2023 +0330

    feature: impl getConfigParam
Copy link
Collaborator

@dbaranovstonfi dbaranovstonfi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of minor fixes are requested

tests/config_test.rs Outdated Show resolved Hide resolved
src/tl/result.rs Outdated Show resolved Hide resolved
@dbaranovstonfi dbaranovstonfi merged commit de21244 into ston-fi:main Aug 10, 2023
1 check passed
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.

3 participants