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
use engine_shared::stored_value::StoredValue;
use engine_storage::global_state::StateReader;
use mint::{Mint, RuntimeProvider, StorageProvider};
use types::{
    account::PublicKey,
    bytesrepr::{FromBytes, ToBytes},
    system_contract_errors::mint::Error,
    CLTyped, CLValue, Key, URef,
};

use crate::{execution, runtime_context::RuntimeContext};

impl<'a, R> RuntimeProvider for RuntimeContext<'a, R>
where
    R: StateReader<Key, StoredValue>,
    R::Error: Into<execution::Error>,
{
    fn get_caller(&self) -> PublicKey {
        self.get_caller()
    }

    fn put_key(&mut self, name: &str, key: Key) {
        // TODO: update RuntimeProvider to better handle errors
        self.put_key(name.to_string(), key).expect("should put key")
    }
}

// TODO: update Mint + StorageProvider to better handle errors
impl<'a, R> StorageProvider for RuntimeContext<'a, R>
where
    R: StateReader<Key, StoredValue>,
    R::Error: Into<execution::Error>,
{
    fn new_uref<T: CLTyped + ToBytes>(&mut self, init: T) -> URef {
        let cl_value: CLValue = CLValue::from_t(init).expect("should convert value");
        self.new_uref(StoredValue::CLValue(cl_value))
            .expect("should create new uref")
    }

    fn write_local<K: ToBytes, V: CLTyped + ToBytes>(&mut self, key: K, value: V) {
        let key_bytes = key.to_bytes().expect("should serialize");
        let cl_value = CLValue::from_t(value).expect("should convert");
        self.write_ls(&key_bytes, cl_value)
            .expect("should write local state")
    }

    fn read_local<K: ToBytes, V: CLTyped + FromBytes>(
        &mut self,
        key: &K,
    ) -> Result<Option<V>, Error> {
        let key_bytes = key.to_bytes().expect("should serialize");
        let maybe_value = self.read_ls(&key_bytes).map_err(|_| Error::Storage)?;
        match maybe_value {
            Some(value) => {
                let value = CLValue::into_t(value).unwrap();
                Ok(Some(value))
            }
            None => Ok(None),
        }
    }

    fn read<T: CLTyped + FromBytes>(&mut self, uref: URef) -> Result<Option<T>, Error> {
        let maybe_value = self.read_gs(&Key::URef(uref)).map_err(|_| Error::Storage)?;
        match maybe_value {
            Some(StoredValue::CLValue(value)) => {
                let value = CLValue::into_t(value).unwrap();
                Ok(Some(value))
            }
            Some(error) => panic!("should have received value: {:?}", error),
            None => Ok(None),
        }
    }

    fn write<T: CLTyped + ToBytes>(&mut self, uref: URef, value: T) -> Result<(), Error> {
        let cl_value = CLValue::from_t(value).expect("should convert");
        self.write_gs(Key::URef(uref), StoredValue::CLValue(cl_value))
            .map_err(|_| Error::Storage)
    }

    fn add<T: CLTyped + ToBytes>(&mut self, uref: URef, value: T) -> Result<(), Error> {
        let cl_value = CLValue::from_t(value).expect("should convert");
        self.add_gs(Key::URef(uref), StoredValue::CLValue(cl_value))
            .map_err(|_| Error::Storage)
    }
}

impl<'a, R> Mint for RuntimeContext<'a, R>
where
    R: StateReader<Key, StoredValue>,
    R::Error: Into<execution::Error>,
{
}