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
use crate::database::{
    storage::DatabaseColumn,
    Column,
};
use fuel_core_storage::tables::ContractsInfo;

impl DatabaseColumn for ContractsInfo {
    fn column() -> Column {
        Column::ContractsInfo
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::database::Database;
    use fuel_core_storage::StorageAsMut;
    use fuel_core_types::{
        fuel_types::{
            Bytes32,
            ContractId,
            Salt,
        },
        fuel_vm::Contract,
    };
    use rand::{
        rngs::StdRng,
        Rng,
        SeedableRng,
    };

    #[test]
    fn get() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let contract_id: ContractId = ContractId::from([1u8; 32]);
        let contract: Contract = Contract::from(vec![32u8]);
        let root = contract.root();
        let salt: Salt = rng.gen();

        let database = &mut Database::default();
        database
            .storage::<ContractsInfo>()
            .insert(&contract_id, &(salt, root))
            .unwrap();

        assert_eq!(
            database
                .storage::<ContractsInfo>()
                .get(&contract_id)
                .unwrap()
                .unwrap()
                .into_owned(),
            (salt, root)
        );
    }

    #[test]
    fn put() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let contract_id: ContractId = ContractId::from([1u8; 32]);
        let contract: Contract = Contract::from(vec![32u8]);
        let root = contract.root();
        let salt: Salt = rng.gen();

        let database = &mut Database::default();
        database
            .storage::<ContractsInfo>()
            .insert(&contract_id, &(salt, root))
            .unwrap();

        let returned: (Salt, Bytes32) = *database
            .storage::<ContractsInfo>()
            .get(&contract_id)
            .unwrap()
            .unwrap();
        assert_eq!(returned, (salt, root));
    }

    #[test]
    fn remove() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let contract_id: ContractId = ContractId::from([1u8; 32]);
        let contract: Contract = Contract::from(vec![32u8]);
        let root = contract.root();
        let salt: Salt = rng.gen();

        let database = &mut Database::default();
        database
            .storage::<ContractsInfo>()
            .insert(&contract_id, &(salt, root))
            .unwrap();

        database
            .storage::<ContractsInfo>()
            .remove(&contract_id)
            .unwrap();

        assert!(!database
            .contains_key(contract_id.as_ref(), Column::ContractsInfo)
            .unwrap());
    }

    #[test]
    fn exists() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let contract_id: ContractId = ContractId::from([1u8; 32]);
        let contract: Contract = Contract::from(vec![32u8]);
        let root = contract.root();
        let salt: Salt = rng.gen();

        let database = &mut Database::default();
        database
            .storage::<ContractsInfo>()
            .insert(&contract_id, &(salt, root))
            .unwrap();

        assert!(database
            .storage::<ContractsInfo>()
            .contains_key(&contract_id)
            .unwrap());
    }
}