-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmod.rs
More file actions
194 lines (168 loc) · 6.82 KB
/
Copy pathmod.rs
File metadata and controls
194 lines (168 loc) · 6.82 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
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
// Copyright 2025 PRAGMA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pub mod in_memory_consensus_store;
use crate::Nonces;
use amaru_kernel::{BlockHeader, HeaderHash, IsHeader, Point, RawBlock};
use std::fmt::Display;
use std::iter::successors;
use thiserror::Error;
pub trait ReadOnlyChainStore<H>
where
H: IsHeader,
{
/// Try to load a header by its hash.
fn load_header(&self, hash: &HeaderHash) -> Option<H>;
fn get_children(&self, hash: &HeaderHash) -> Vec<HeaderHash>;
fn get_anchor_hash(&self) -> HeaderHash;
fn get_best_chain_hash(&self) -> HeaderHash;
/// Load a `HeaderHash` from the best chain.
/// Returns `None` if the point is not in the best chain.
fn load_from_best_chain(&self, point: &Point) -> Option<HeaderHash>;
/// Return the next `Point` on the best chain following given
/// `Point`, if it exists.
fn next_best_chain(&self, point: &Point) -> Option<Point>;
fn load_block(&self, hash: &HeaderHash) -> Result<RawBlock, StoreError>;
fn get_nonces(&self, header: &HeaderHash) -> Option<Nonces>;
fn has_header(&self, hash: &HeaderHash) -> bool;
/// Return the hashes of the best chain fragment, starting from the anchor.
fn retrieve_best_chain(&self) -> Vec<HeaderHash> {
let anchor = self.get_anchor_hash();
let mut best_chain = vec![];
let mut current_hash = self.get_best_chain_hash();
while let Some(header) = self.load_header(¤t_hash) {
best_chain.push(current_hash);
if header.hash() != anchor
&& let Some(parent) = header.parent()
{
current_hash = parent;
} else {
break;
}
}
best_chain.reverse();
best_chain
}
/// Return the ancestors of the header, including the header itself.
/// Stop at the anchor of the tree.
fn ancestors<'a>(&'a self, start: H) -> Box<dyn Iterator<Item = H> + 'a>
where
H: 'a,
{
let anchor = self.get_anchor_hash();
Box::new(successors(Some(start), move |h| {
if h.hash() == anchor {
None
} else {
h.parent().and_then(|p| self.load_header(&p))
}
}))
}
/// Return the hashes of the ancestors of the header, including the header hash itself.
fn ancestors_hashes<'a>(
&'a self,
hash: &HeaderHash,
) -> Box<dyn Iterator<Item = HeaderHash> + 'a>
where
H: 'a,
{
if let Some(header) = self.load_header(hash) {
Box::new(self.ancestors(header).map(|h| h.hash()))
} else {
Box::new(vec![*hash].into_iter())
}
}
}
/// A chain store interface that exposes diagnostic methods to load raw data.
pub trait DiagnosticChainStore {
/// Load all headers in the store.
///
/// NOTE: This can be very expensive for large stores and is only
/// used for diagnostics and testing purposes.
fn load_headers(&self) -> Box<dyn Iterator<Item = BlockHeader> + '_>;
/// Load all nonces in the store.
fn load_nonces(&self) -> Box<dyn Iterator<Item = (HeaderHash, Nonces)> + '_>;
fn load_blocks(&self) -> Box<dyn Iterator<Item = (HeaderHash, RawBlock)> + '_>;
fn load_parents_children(&self)
-> Box<dyn Iterator<Item = (HeaderHash, Vec<HeaderHash>)> + '_>;
}
impl<H: IsHeader> ReadOnlyChainStore<H> for Box<dyn ChainStore<H>> {
fn load_header(&self, hash: &HeaderHash) -> Option<H> {
self.as_ref().load_header(hash)
}
fn get_children(&self, hash: &HeaderHash) -> Vec<HeaderHash> {
self.as_ref().get_children(hash)
}
fn get_anchor_hash(&self) -> HeaderHash {
self.as_ref().get_anchor_hash()
}
fn get_best_chain_hash(&self) -> HeaderHash {
self.as_ref().get_best_chain_hash()
}
fn load_block(&self, hash: &HeaderHash) -> Result<RawBlock, StoreError> {
self.as_ref().load_block(hash)
}
fn get_nonces(&self, header: &HeaderHash) -> Option<Nonces> {
self.as_ref().get_nonces(header)
}
fn has_header(&self, hash: &HeaderHash) -> bool {
self.as_ref().has_header(hash)
}
fn load_from_best_chain(&self, point: &Point) -> Option<HeaderHash> {
self.as_ref().load_from_best_chain(point)
}
fn next_best_chain(&self, point: &Point) -> Option<Point> {
self.as_ref().next_best_chain(point)
}
}
/// A simple chain store interface that can store and retrieve headers indexed by their hash.
pub trait ChainStore<H>: ReadOnlyChainStore<H> + Send + Sync
where
H: IsHeader,
{
fn store_header(&self, header: &H) -> Result<(), StoreError>;
fn set_anchor_hash(&self, hash: &HeaderHash) -> Result<(), StoreError>;
fn set_best_chain_hash(&self, hash: &HeaderHash) -> Result<(), StoreError>;
fn store_block(&self, hash: &HeaderHash, block: &RawBlock) -> Result<(), StoreError>;
fn put_nonces(&self, header: &HeaderHash, nonces: &Nonces) -> Result<(), StoreError>;
/// Roll forward the best chain to the given point.
fn roll_forward_chain(&self, point: &Point) -> Result<(), StoreError>;
/// Rollback the best chain tip at the given point.
/// The point must exist on the best chain, and all points on chain after that
/// point will be deleted.
/// Returns the number of headers that were rolled back.
fn rollback_chain(&self, point: &Point) -> Result<usize, StoreError>;
}
#[derive(Error, PartialEq, Debug, serde::Serialize, serde::Deserialize)]
pub enum StoreError {
WriteError { error: String },
ReadError { error: String },
OpenError { error: String },
NotFound { hash: HeaderHash },
IncompatibleChainStoreVersions { stored: u16, current: u16 },
}
impl Display for StoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StoreError::WriteError { error } => write!(f, "WriteError: {}", error),
StoreError::ReadError { error } => write!(f, "ReadError: {}", error),
StoreError::OpenError { error } => write!(f, "OpenError: {}", error),
StoreError::NotFound { hash } => write!(f, "NotFound: {}", hash),
StoreError::IncompatibleChainStoreVersions { stored, current } => write!(
f,
"Incompatible DB Versions: found {}, expected {}",
stored, current
),
}
}
}