-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharchive.rs
More file actions
116 lines (106 loc) · 3.31 KB
/
archive.rs
File metadata and controls
116 lines (106 loc) · 3.31 KB
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
use crate::types::*;
use crate::types::{hash, BlockHash, Body, GetValue};
use heed::byteorder::{BigEndian, ByteOrder};
use heed::types::*;
use heed::{Database, RoTxn, RwTxn};
use serde::{Deserialize, Serialize};
#[derive(Clone)]
pub struct Archive<A, C> {
// Block height to header.
headers: Database<OwnedType<[u8; 4]>, SerdeBincode<Header>>,
bodies: Database<OwnedType<[u8; 4]>, SerdeBincode<Body<A, C>>>,
hash_to_height: Database<OwnedType<[u8; 32]>, OwnedType<[u8; 4]>>,
}
impl<
A: Serialize + for<'de> Deserialize<'de> + 'static,
C: Clone + Serialize + for<'de> Deserialize<'de> + GetValue + 'static,
> Archive<A, C>
{
pub const NUM_DBS: u32 = 3;
pub fn new(env: &heed::Env) -> Result<Self, Error> {
let headers = env.create_database(Some("headers"))?;
let bodies = env.create_database(Some("bodies"))?;
let hash_to_height = env.create_database(Some("hash_to_height"))?;
Ok(Self {
headers,
bodies,
hash_to_height,
})
}
pub fn get_header(
&self,
txn: &RoTxn,
height: u32,
) -> Result<Option<Header>, Error> {
let height = height.to_be_bytes();
let header = self.headers.get(txn, &height)?;
Ok(header)
}
pub fn get_body(
&self,
txn: &RoTxn,
height: u32,
) -> Result<Option<Body<A, C>>, Error> {
let height = height.to_be_bytes();
let header = self.bodies.get(txn, &height)?;
Ok(header)
}
pub fn get_best_hash(&self, txn: &RoTxn) -> Result<BlockHash, Error> {
let best_hash = match self.headers.last(txn)? {
Some((_, header)) => hash(&header).into(),
None => [0; 32].into(),
};
Ok(best_hash)
}
pub fn get_height(&self, txn: &RoTxn) -> Result<u32, Error> {
let height = match self.headers.last(txn)? {
Some((height, _)) => BigEndian::read_u32(&height),
None => 0,
};
Ok(height)
}
pub fn put_body(
&self,
txn: &mut RwTxn,
header: &Header,
body: &Body<A, C>,
) -> Result<(), Error> {
if header.merkle_root != body.compute_merkle_root() {
return Err(Error::InvalidMerkleRoot);
}
let hash = header.hash();
let height = self
.hash_to_height
.get(txn, &hash.into())?
.ok_or(Error::NoHeader(hash))?;
self.bodies.put(txn, &height, body)?;
Ok(())
}
pub fn append_header(
&self,
txn: &mut RwTxn,
header: &Header,
) -> Result<(), Error> {
let height = self.get_height(txn)?;
let best_hash = self.get_best_hash(txn)?;
if header.prev_side_hash != best_hash {
return Err(Error::InvalidPrevSideHash);
}
let new_height = (height + 1).to_be_bytes();
self.headers.put(txn, &new_height, header)?;
self.hash_to_height
.put(txn, &header.hash().into(), &new_height)?;
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("heed error")]
Heed(#[from] heed::Error),
#[error("invalid previous side hash")]
InvalidPrevSideHash,
#[error("invalid merkle root")]
InvalidMerkleRoot,
#[error("no header with hash {0}")]
NoHeader(BlockHash),
}