tonlib_core/cell/
state_init.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use super::ArcCell;
use crate::cell::{Cell, CellBuilder, TonCellError};
use crate::TonHash;

pub struct StateInitBuilder {
    code: Option<ArcCell>,
    data: Option<ArcCell>,
    split_depth: bool,
    tick_tock: bool,
    library: bool,
}
pub struct StateInit {
    pub code: Option<ArcCell>,
    pub data: Option<ArcCell>,
}

impl StateInitBuilder {
    pub fn new(code: &ArcCell, data: &ArcCell) -> StateInitBuilder {
        StateInitBuilder {
            code: Some(code.clone()),
            data: Some(data.clone()),
            split_depth: false,
            tick_tock: false,
            library: false,
        }
    }

    pub fn with_split_depth(&mut self, split_depth: bool) -> &mut Self {
        self.split_depth = split_depth;
        self
    }

    pub fn with_tick_tock(&mut self, tick_tock: bool) -> &mut Self {
        self.tick_tock = tick_tock;
        self
    }

    pub fn with_library(&mut self, library: bool) -> &mut Self {
        self.library = library;
        self
    }

    pub fn build(&self) -> Result<Cell, TonCellError> {
        let mut builder = CellBuilder::new();
        builder
            .store_bit(self.split_depth)? //Split depth
            .store_bit(self.tick_tock)? //Tick tock
            .store_bit(self.code.is_some())? //Code
            .store_bit(self.data.is_some())? //Data
            .store_bit(self.library)?; //Library
        if let Some(code) = &self.code {
            builder.store_reference(code)?;
        }
        if let Some(data) = &self.data {
            builder.store_reference(data)?;
        }
        builder.build()
    }
}

impl StateInit {
    pub fn create_account_id(code: &ArcCell, data: &ArcCell) -> Result<TonHash, TonCellError> {
        Ok(StateInitBuilder::new(code, data).build()?.cell_hash())
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::StateInitBuilder;
    use crate::cell::{CellBuilder, TonCellError};

    #[test]
    fn test_state_init() -> Result<(), TonCellError> {
        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
        let state_init = StateInitBuilder::new(&code, &data)
            .with_split_depth(true)
            .with_tick_tock(true)
            .with_library(true)
            .build()?;

        assert_eq!(state_init.data[0], 0b11111000);
        println!("{:08b}", state_init.data[0]);

        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
        let state_init = StateInitBuilder::new(&code, &data)
            .with_split_depth(false)
            .with_tick_tock(false)
            .with_library(false)
            .build()?;

        assert_eq!(state_init.data[0], 0b00110000);

        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
        let state_init = StateInitBuilder::new(&code, &data)
            .with_split_depth(true)
            .with_tick_tock(false)
            .with_library(false)
            .build()?;

        assert_eq!(state_init.data[0], 0b10110000);

        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
        let state_init = StateInitBuilder::new(&code, &data)
            .with_split_depth(false)
            .with_tick_tock(true)
            .with_library(false)
            .build()?;
        assert_eq!(state_init.data[0], 0b01110000);

        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
        let state_init = StateInitBuilder::new(&code, &data)
            .with_split_depth(false)
            .with_tick_tock(false)
            .with_library(true)
            .build()?;
        assert_eq!(state_init.data[0], 0b00111000);
        Ok(())
    }
}