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
use core::convert::Infallible;

use ics23::CommitmentProof;
use tendermint::hash::Algorithm;
use tendermint::Hash;
use tracing::trace;

use crate::avl::{AsBytes, AvlTree};
use crate::context::{ProvableStore, Store};
use crate::types::{Height, Path, RawHeight, State};

/// A wrapper type around [`Vec`] that more easily facilitates the pruning of
/// its elements at a particular height / index. Keeps track of the latest
/// height at which its elements were pruned.
///
/// This type is used by [`InMemoryStore`] in order to prune old store entries.
#[derive(Debug, Clone, Default)]
pub struct PrunedVec<T> {
    vec: Vec<T>,
    /// The latest index at which elements were pruned. In other words,
    /// elements that exist at and before this index are no longer accessible.
    pruned: usize,
}

impl<T> PrunedVec<T> {
    pub fn push(&mut self, value: T) {
        self.vec.push(value);
    }

    pub fn get(&self, index: usize) -> Option<&T> {
        self.vec.get(index.checked_sub(self.pruned)?)
    }

    pub fn last(&self) -> Option<&T> {
        self.vec.last()
    }

    /// Returns the number of elements currently in the `PrunedVec`,
    /// i.e., the total number of elements minus the pruned elements.
    pub fn current_length(&self) -> usize {
        self.vec.len()
    }

    /// Returns the number of elements that have been pruned over the
    /// lifetime of the instance of this type.
    pub fn pruned_length(&self) -> usize {
        self.pruned
    }

    /// Returns the total number of elements that have been added to
    /// the `PrunedVec` over the lifetime of the instance of this type.
    /// This includes the number of pruned elements in its count.
    pub fn original_length(&self) -> usize {
        self.current_length() + self.pruned_length()
    }

    /// Removes all elements from the `PrunedVec` up to the specified
    /// index, inclusive. Note that `index` needs to be strictly greater
    /// than the current `self.pruned` index, otherwise this method is
    /// a no-op.
    pub fn prune(&mut self, index: usize) {
        trace!("pruning at index = {}", index);
        if index > self.pruned {
            self.vec.drain(0..index - self.pruned);
            self.pruned = index;
        }
    }
}

/// An in-memory store backed by an AvlTree.
///
/// [`InMemoryStore`] has two copies of the current working store - `staged` and `pending`.
///
/// Each transaction works on the `pending` copy. When a transaction returns:
/// - If it succeeded, the store _applies_ the transaction changes by copying `pending` to `staged`.
/// - If it failed, the store _reverts_ the transaction changes by copying `staged` to `pending`.
///
/// When a block is committed, the staged copy is copied into the committed store.
///
/// Note that this store implementation is not production-friendly. After each transaction,
/// the entire store is copied from `pending` to `staged`, or from `staged` to `pending`.
#[derive(Clone, Debug)]
pub struct InMemoryStore {
    /// A collection of states corresponding to every committed block height.
    store: PrunedVec<State>,
    /// The changes made as a result of successful transactions that are staged
    /// and waiting to be committed.
    staged: State,
    /// The dirty changes resulting from transactions that have not yet completed.
    pending: State,
}

impl InMemoryStore {
    #[inline]
    fn get_state(&self, height: Height) -> Option<&State> {
        match height {
            Height::Pending => Some(&self.pending),
            Height::Latest => self.store.last(),
            Height::Stable(height) => {
                if height == 0 {
                    None
                } else {
                    let h = height as usize;
                    self.store.get(h - 1)
                }
            }
        }
    }
}

impl Default for InMemoryStore {
    /// The store starts out with an empty state. We also initialize the pending location as empty.
    fn default() -> Self {
        let genesis_state = AvlTree::new();

        let store = PrunedVec::default();
        let staged = genesis_state.clone();
        let pending = genesis_state.clone();

        Self {
            store,
            staged,
            pending,
        }
    }
}

impl Store for InMemoryStore {
    type Error = Infallible;

    fn set(&mut self, path: Path, value: Vec<u8>) -> Result<Option<Vec<u8>>, Self::Error> {
        trace!("set at path = {}", path.to_string());
        Ok(self.pending.insert(path, value))
    }

    fn get(&self, height: Height, path: &Path) -> Option<Vec<u8>> {
        trace!(
            "get at path = {} at height = {:?}",
            path.to_string(),
            height
        );
        self.get_state(height).and_then(|v| v.get(path).cloned())
    }

    fn delete(&mut self, path: &Path) {
        self.pending.remove(path.clone());
    }

    fn commit(&mut self) -> Result<Vec<u8>, Self::Error> {
        self.apply()?;
        trace!("committing height: {}", self.current_height());
        self.store.push(self.staged.clone());
        Ok(self.root_hash())
    }

    fn apply(&mut self) -> Result<(), Self::Error> {
        trace!("applying height: {}", self.current_height());
        self.staged = self.pending.clone();
        Ok(())
    }

    fn reset(&mut self) {
        trace!("resetting height: {}", self.current_height());
        self.pending = self.staged.clone();
    }

    fn prune(&mut self, height: RawHeight) -> Result<RawHeight, Self::Error> {
        let h = height as usize;
        self.store.prune(h);
        Ok(height)
    }

    fn current_height(&self) -> u64 {
        self.store.original_length() as u64
    }

    fn get_keys(&self, key_prefix: &Path) -> Vec<Path> {
        let key_prefix = key_prefix.as_bytes();
        self.pending
            .get_keys()
            .into_iter()
            .filter(|&key| key.as_bytes().as_ref().starts_with(key_prefix.as_ref()))
            .cloned()
            .collect()
    }
}

impl ProvableStore for InMemoryStore {
    fn root_hash(&self) -> Vec<u8> {
        self.get_state(Height::Latest)
            .and_then(|s| s.root_hash())
            .unwrap_or(&Hash::from_bytes(Algorithm::Sha256, &[0u8; 32]).unwrap())
            .as_bytes()
            .to_vec()
    }

    fn get_proof(&self, height: Height, key: &Path) -> Option<CommitmentProof> {
        trace!(
            "get proof at path = {} at height = {:?}",
            key.to_string(),
            height
        );
        self.get_state(height).map(|v| v.get_proof(key))
    }
}

// TODO(hu55a1n1): import tests

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pruned_vec() {
        let mut pv = PrunedVec::default();
        pv.push(1);
        pv.push(2);
        pv.push(3);
        pv.push(4);
        pv.push(5);
        assert_eq!(pv.original_length(), 5);
        pv.prune(2);
        assert_eq!(pv.original_length(), 5);
        assert_eq!(pv.pruned_length(), 2);
        assert_eq!(pv.current_length(), 3);
        assert_eq!(pv.get(0), None);
        assert_eq!(pv.get(1), None);
        assert_eq!(pv.get(2), Some(&3));
        assert_eq!(pv.get(3), Some(&4));
        assert_eq!(pv.get(4), Some(&5));
        assert_eq!(pv.get(5), None);
        assert_eq!(pv.last(), Some(&5));
    }

    #[test]
    fn test_in_memory_store() {
        let mut store = InMemoryStore::default();
        assert!(!store.root_hash().is_empty());
        assert_eq!(store.current_height(), 0);

        let path = Path::from("a".to_owned());
        let value1 = vec![1, 2, 3];
        let value2 = vec![4, 5, 6];

        store.set(path.clone(), value1.clone()).unwrap();
        assert_eq!(store.get(Height::Pending, &path), Some(value1.clone()));
        assert_eq!(store.get(Height::Latest, &path), None);
        assert_eq!(store.get(Height::Stable(1), &path), None);

        store.apply().unwrap();
        store.commit().unwrap();

        assert_eq!(store.get(Height::Pending, &path), Some(value1.clone()));
        assert_eq!(store.get(Height::Latest, &path), Some(value1.clone()));
        assert_eq!(store.get(Height::Stable(1), &path), Some(value1.clone()));
        assert_eq!(store.get(Height::Stable(2), &path), None);
        assert_eq!(store.current_height(), 1);
        assert!(!store.root_hash().is_empty());

        store.set(path.clone(), value2.clone()).unwrap();
        assert_eq!(store.get(Height::Pending, &path), Some(value2.clone()));
        assert_eq!(store.get(Height::Latest, &path), Some(value1.clone()));
        assert_eq!(store.get(Height::Stable(1), &path), Some(value1.clone()));

        store.apply().unwrap();
        store.commit().unwrap();

        assert_eq!(store.get(Height::Pending, &path), Some(value2.clone()));
        assert_eq!(store.get(Height::Latest, &path), Some(value2.clone()));
        assert_eq!(store.get(Height::Stable(1), &path), Some(value1.clone()));
        assert_eq!(store.get(Height::Stable(2), &path), Some(value2.clone()));
        assert_eq!(store.get(Height::Stable(3), &path), None);
        assert_eq!(store.current_height(), 2);
        assert!(!store.root_hash().is_empty());
    }
}