odra-vm 2.9.1

Odra Virtual Machine for testing and development.
Documentation
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use anyhow::{Context, Result};
use odra_core::casper_types::{
    bytesrepr::{Bytes, Error, FromBytes, ToBytes},
    U512
};
use odra_core::prelude::*;
use std::{
    collections::{hash_map::DefaultHasher, BTreeMap},
    hash::{Hash, Hasher}
};

use super::balance::AccountBalance;

#[derive(Default, Clone)]
pub struct Storage {
    state: BTreeMap<u64, Bytes>,
    named_state: BTreeMap<u64, BTreeMap<u64, Bytes>>,
    pub balances: BTreeMap<Address, AccountBalance>,
    state_snapshot: Option<BTreeMap<u64, Bytes>>,
    named_state_snapshot: Option<BTreeMap<u64, BTreeMap<u64, Bytes>>>,
    balances_snapshot: Option<BTreeMap<Address, AccountBalance>>
}

impl Storage {
    pub fn new(balances: BTreeMap<Address, AccountBalance>) -> Self {
        Self {
            state: Default::default(),
            named_state: Default::default(),
            balances,
            state_snapshot: Default::default(),
            named_state_snapshot: Default::default(),
            balances_snapshot: Default::default()
        }
    }

    pub fn balance_of(&self, address: &Address) -> Option<&AccountBalance> {
        self.balances.get(address)
    }

    pub fn set_balance(&mut self, address: Address, balance: AccountBalance) {
        self.balances.insert(address, balance);
    }

    pub fn transfer(&mut self, from: &Address, to: &Address, amount: &U512) -> Result<()> {
        let from_balance = self.balances.get_mut(from).context("Unknown address")?;
        from_balance.reduce(*amount)?;
        let to_balance = self.balances.get_mut(to).context("Unknown address")?;
        to_balance.increase(*amount)
    }

    pub fn increase_balance(&mut self, address: &Address, amount: &U512) -> Result<()> {
        let balance = self.balances.get_mut(address).context("Unknown address")?;
        balance.increase(*amount)
    }

    pub fn decrease_balance(&mut self, address: &Address, amount: &U512) -> Result<()> {
        let balance = self.balances.get_mut(address).context("Unknown address")?;
        balance.reduce(*amount)
    }

    pub fn get_value(&self, address: &Address, key: &[u8]) -> Result<Option<Bytes>, Error> {
        let hash = Storage::hashed_key(address, key);
        let result = self.state.get(&hash).cloned();

        match result {
            Some(res) => Ok(Some(res)),
            None => Ok(None)
        }
    }

    pub fn set_value(&mut self, address: &Address, key: &[u8], value: Bytes) -> Result<(), Error> {
        let hash = Storage::hashed_key(address, key);
        self.state.insert(hash, value);
        Ok(())
    }

    pub fn insert_dict_value(
        &mut self,
        address: &Address,
        collection: &[u8],
        key: &[u8],
        value: Bytes
    ) -> Result<(), Error> {
        let dict = Self::hashed_key(address, collection);
        let hash = Storage::hashed_key(address, key);
        let dict_values = self.named_state.entry(dict).or_default();
        dict_values.insert(hash, value);
        Ok(())
    }

    pub fn remove_dict(&mut self, address: &Address, collection: &[u8]) {
        let dict = Self::hashed_key(address, collection);
        self.named_state.remove(&dict);
    }

    pub fn get_dict_value(
        &self,
        address: &Address,
        collection: &[u8],
        key: &[u8]
    ) -> Result<Option<Bytes>, Error> {
        let dict = Self::hashed_key(address, collection);
        let hash = Storage::hashed_key(address, key);
        let dict_values = self.named_state.get(&dict);
        if let Some(dict) = dict_values {
            let result = dict.get(&hash).cloned();
            Ok(result)
        } else {
            Ok(None)
        }
    }

    pub fn take_snapshot(&mut self) {
        self.state_snapshot = Some(self.state.clone());
        self.named_state_snapshot = Some(self.named_state.clone());
        self.balances_snapshot = Some(self.balances.clone());
    }

    pub fn drop_snapshot(&mut self) {
        self.state_snapshot = None;
        self.named_state_snapshot = None;
        self.balances_snapshot = None;
    }

    pub fn restore_snapshot(&mut self) {
        if let Some(snapshot) = self.state_snapshot.clone() {
            self.state = snapshot;
            self.state_snapshot = None;
        };
        if let Some(snapshot) = self.named_state_snapshot.clone() {
            self.named_state = snapshot;
            self.named_state_snapshot = None;
        };
        if let Some(snapshot) = self.balances_snapshot.clone() {
            self.balances = snapshot;
            self.balances_snapshot = None;
        };
    }

    fn hashed_key<H: Hash>(address: &Address, key: H) -> u64 {
        let mut hasher = DefaultHasher::new();
        address.hash(&mut hasher);
        key.hash(&mut hasher);
        hasher.finish()
    }
}

#[cfg(test)]
mod test {
    use odra_core::casper_types::bytesrepr::Bytes;
    use odra_core::prelude::Address;
    use odra_core::utils::serialize;

    use crate::vm::utils;

    use super::Storage;

    fn setup() -> (Address, [u8; 3], u8) {
        let address = utils::account_address_from_str("add");
        let key = b"key";
        let value = 88u8;

        (address, *key, value)
    }

    #[test]
    fn read_write_single_value() {
        // given an empty storage
        let mut storage = Storage::default();
        let (address, key, value) = setup();

        // when put a value
        storage
            .set_value(&address, &key, serialize(&value))
            .unwrap();

        // then the value can be read
        assert_eq!(
            storage.get_value(&address, &key).unwrap(),
            Some(serialize(&value))
        );
    }

    #[test]
    fn override_single_value() {
        // given a storage with some stored value
        let mut storage = Storage::default();
        let (address, key, value) = setup();
        storage
            .set_value(&address, &key, serialize(&value))
            .unwrap();

        // when the next value is set under the same key
        let next_value = String::from("new_value");
        storage
            .set_value(&address, &key, serialize(&next_value))
            .unwrap();

        // then the previous value is replaced
        assert_eq!(
            storage.get_value(&address, &key).unwrap(),
            Some(serialize(&next_value))
        );
    }

    #[test]
    fn read_non_existing_key_returns_none() {
        // given an empty storage
        let storage = Storage::default();
        let (address, key, _) = setup();

        // when lookup a key
        let result: Option<Bytes> = storage.get_value(&address, &key).unwrap();

        // then the None value is returned
        assert_eq!(result, None);
    }

    #[test]
    fn read_write_dict_value() {
        // given an empty storage
        let mut storage = Storage::default();
        let (address, key, value) = setup();
        let collection = b"dict";

        // when put a value into a collection
        storage
            .insert_dict_value(&address, collection, &key, serialize(&value))
            .unwrap();

        // then the value can be read
        assert_eq!(
            storage.get_dict_value(&address, collection, &key).unwrap(),
            Some(serialize(&value))
        );
    }

    #[test]
    fn read_from_non_existing_collection_returns_none() {
        // given storage with some stored value
        let mut storage = Storage::default();
        let (address, key, value) = setup();
        let collection = b"dict";
        storage
            .insert_dict_value(&address, collection, &key, serialize(&value))
            .unwrap();

        // when read a value from a non exisiting collection
        let non_existing_collection = b"collection";
        let result: Option<Bytes> = storage
            .get_dict_value(&address, non_existing_collection, &key)
            .unwrap();

        // then None is returned
        assert_eq!(result, None);
    }

    #[test]
    fn read_from_non_existing_key_from_existing_collection_returns_none() {
        // given storage with some stored value
        let mut storage = Storage::default();
        let (address, key, value) = setup();
        let collection = b"dict";
        storage
            .insert_dict_value(&address, collection, &key, serialize(&value))
            .unwrap();

        // when read a value from a non existing collection
        let non_existing_key = [2u8];
        let result: Option<Bytes> = storage
            .get_dict_value(&address, collection, &non_existing_key)
            .unwrap();

        // then None is returned
        assert_eq!(result, None);
    }

    #[test]
    fn remove_dict_erases_all_dict_records() {
        // given storage with some stored value
        let mut storage = Storage::default();
        let address = utils::account_address_from_str("add");
        let key1 = b"key";
        let key2 = b"key2";
        let value1 = 88u8;
        let value2 = 89u8;
        let collection = b"dict";
        storage
            .insert_dict_value(&address, collection, key1, serialize(&value1))
            .unwrap();
        storage
            .insert_dict_value(&address, collection, key2, serialize(&value2))
            .unwrap();

        assert_eq!(
            storage.get_dict_value(&address, collection, key1).unwrap(),
            Some(serialize(&value1))
        );
        assert_eq!(
            storage.get_dict_value(&address, collection, key2).unwrap(),
            Some(serialize(&value2))
        );

        // when remove a dictionary
        storage.remove_dict(&address, collection);

        // then all values from the dictionary are removed
        assert_eq!(
            storage.get_dict_value(&address, collection, key1).unwrap(),
            None
        );
        assert_eq!(
            storage.get_dict_value(&address, collection, key2).unwrap(),
            None
        );
    }

    #[test]
    fn restore_snapshot() {
        // given storage with some state and a snapshot of the previous state
        let mut storage = Storage::default();
        let (address, key, initial_value) = setup();
        storage
            .set_value(&address, &key, serialize(&initial_value))
            .unwrap();
        storage
            .insert_dict_value(&address, b"dict", &key, serialize(&initial_value))
            .unwrap();
        storage.take_snapshot();
        let next_value = String::from("next_value");
        storage
            .set_value(&address, &key, serialize(&next_value))
            .unwrap();
        storage
            .insert_dict_value(&address, b"dict", &key, serialize(&next_value))
            .unwrap();

        // when restore the snapshot
        storage.restore_snapshot();

        // then the changes are reverted
        assert_eq!(
            storage.get_value(&address, &key).unwrap(),
            Some(serialize(&initial_value))
        );
        assert_eq!(
            storage.get_dict_value(&address, b"dict", &key).unwrap(),
            Some(serialize(&initial_value))
        );
        // the snapshot is removed
        assert_eq!(storage.state_snapshot, None);
    }

    #[test]
    fn test_snapshot_override() {
        // given storage with some state and a snapshot of the previous state
        let mut storage = Storage::default();
        let (address, key, initial_value) = setup();
        let second_value = 2_000u32;
        let third_value = 3_000u32;
        storage
            .set_value(&address, &key, serialize(&initial_value))
            .unwrap();
        storage.take_snapshot();
        storage
            .set_value(&address, &key, serialize(&second_value))
            .unwrap();

        // when take another snapshot and restore it
        storage.take_snapshot();
        storage
            .set_value(&address, &key, serialize(&third_value))
            .unwrap();
        storage.restore_snapshot();

        // then the most recent snapshot is restored
        assert_eq!(
            storage.get_value(&address, &key).unwrap(),
            Some(serialize(&second_value)),
        );
    }

    #[test]
    fn drop_snapshot() {
        // given storage with some state and a snapshot of the previous state
        let mut storage = Storage::default();
        let (address, key, initial_value) = setup();
        let next_value = 1_000u32;
        storage
            .set_value(&address, &key, serialize(&initial_value))
            .unwrap();
        storage.take_snapshot();
        storage
            .set_value(&address, &key, serialize(&next_value))
            .unwrap();

        // when the snapshot is dropped
        storage.drop_snapshot();

        // then storage state does not change
        assert_eq!(
            storage.get_value(&address, &key).unwrap(),
            Some(serialize(&next_value)),
        );
        // the snapshot is wiped out
        assert_eq!(storage.state_snapshot, None);
    }
}