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
127
128
129
use crate::{
    database::{columns::CONTRACTS_CODE_ROOT, Database},
    state::Error,
};
use fuel_vm::prelude::{Bytes32, ContractId, Salt, Storage};
use std::borrow::Cow;

impl Storage<ContractId, (Salt, Bytes32)> for Database {
    type Error = Error;

    fn insert(
        &mut self,
        key: &ContractId,
        value: &(Salt, Bytes32),
    ) -> Result<Option<(Salt, Bytes32)>, Error> {
        Database::insert(self, key.as_ref(), CONTRACTS_CODE_ROOT, *value)
    }

    fn remove(&mut self, key: &ContractId) -> Result<Option<(Salt, Bytes32)>, Error> {
        Database::remove(self, key.as_ref(), CONTRACTS_CODE_ROOT)
    }

    fn get(&self, key: &ContractId) -> Result<Option<Cow<(Salt, Bytes32)>>, Error> {
        Database::get(self, key.as_ref(), CONTRACTS_CODE_ROOT)
    }

    fn contains_key(&self, key: &ContractId) -> Result<bool, Error> {
        Database::exists(self, key.as_ref(), CONTRACTS_CODE_ROOT)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fuel_vm::prelude::Contract;
    use rand::rngs::StdRng;
    use rand::{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 = Database::default();
        database
            .insert(
                contract_id.as_ref().to_vec(),
                CONTRACTS_CODE_ROOT,
                (salt, root),
            )
            .unwrap();

        assert_eq!(
            Storage::<ContractId, (Salt, Bytes32)>::get(&database, &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 mut database = Database::default();
        Storage::<ContractId, (Salt, Bytes32)>::insert(&mut database, &contract_id, &(salt, root))
            .unwrap();

        let returned: (Salt, Bytes32) = database
            .get(contract_id.as_ref(), CONTRACTS_CODE_ROOT)
            .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 mut database = Database::default();
        database
            .insert(
                contract_id.as_ref().to_vec(),
                CONTRACTS_CODE_ROOT,
                (salt, root),
            )
            .unwrap();

        Storage::<ContractId, (Salt, Bytes32)>::remove(&mut database, &contract_id).unwrap();

        assert!(!database
            .exists(contract_id.as_ref(), CONTRACTS_CODE_ROOT)
            .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 = Database::default();
        database
            .insert(
                contract_id.as_ref().to_vec(),
                CONTRACTS_CODE_ROOT,
                (salt, root),
            )
            .unwrap();

        assert!(
            Storage::<ContractId, (Salt, Bytes32)>::contains_key(&database, &contract_id).unwrap()
        );
    }
}