-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathmem.rs
53 lines (44 loc) · 1.19 KB
/
mem.rs
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
use std::collections::HashMap;
use anomaly::fail;
use tendermint::lite::{types::Header, Height, TrustedState};
use super::{Store, StoreHeight};
use crate::chain::Chain;
use crate::error;
pub struct MemStore<C>
where
C: Chain,
{
height: Height,
store: HashMap<Height, TrustedState<C::Commit, C::Header>>,
}
impl<C> Store<C> for MemStore<C>
where
C: Chain,
{
fn height(&self) -> Height {
self.height
}
fn add(&mut self, state: TrustedState<C::Commit, C::Header>) -> Result<(), error::Error> {
let height = state.last_header().header().height();
self.height = height;
self.store.insert(height, state);
Ok(())
}
fn get(
&self,
height: StoreHeight,
) -> Result<&TrustedState<C::Commit, C::Header>, error::Error> {
let height = match height {
StoreHeight::Current => self.height,
StoreHeight::GivenHeight(height) => height,
};
match self.store.get(&height) {
Some(state) => Ok(state),
None => fail!(
error::Kind::Store,
"could not load height {} from store",
height
),
}
}
}