forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 94
/
storage.rs
119 lines (103 loc) · 4.06 KB
/
storage.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
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
use async_trait::async_trait;
use chain::BlockHeader;
use derive_more::Display;
use primitives::hash::H256;
use std::collections::HashMap;
#[derive(Clone, Debug, Display, Eq, PartialEq)]
pub enum BlockHeaderStorageError {
#[display(fmt = "Can't add to the storage for {} - reason: {}", coin, reason)]
AddToStorageError {
coin: String,
reason: String,
},
#[display(fmt = "Can't get from the storage for {} - reason: {}", coin, reason)]
GetFromStorageError {
coin: String,
reason: String,
},
#[display(fmt = "Can't retrieve the table from the storage for {} - reason: {}", coin, reason)]
CantRetrieveTableError {
coin: String,
reason: String,
},
#[display(fmt = "Unable to delete block headers <= {to_height} from storage for {coin} - reason: {reason}")]
UnableToDeleteHeaders {
to_height: u64,
coin: String,
reason: String,
},
#[display(fmt = "Can't query from the storage - query: {} - reason: {}", query, reason)]
QueryError {
query: String,
reason: String,
},
#[display(fmt = "Can't init from the storage - coin: {} - reason: {}", coin, reason)]
InitializationError {
coin: String,
reason: String,
},
#[display(fmt = "Can't decode/deserialize from storage for {} - reason: {}", coin, reason)]
DecodeError {
coin: String,
reason: String,
},
Internal(String),
}
impl BlockHeaderStorageError {
pub fn init_err(ticker: &str, reason: String) -> BlockHeaderStorageError {
BlockHeaderStorageError::InitializationError {
coin: ticker.to_string(),
reason,
}
}
pub fn add_err(ticker: &str, reason: String) -> BlockHeaderStorageError {
BlockHeaderStorageError::AddToStorageError {
coin: ticker.to_string(),
reason,
}
}
pub fn table_err(ticker: &str, reason: String) -> BlockHeaderStorageError {
BlockHeaderStorageError::CantRetrieveTableError {
coin: ticker.to_string(),
reason,
}
}
pub fn get_err(ticker: &str, reason: String) -> BlockHeaderStorageError {
BlockHeaderStorageError::GetFromStorageError {
coin: ticker.to_string(),
reason,
}
}
pub fn delete_err(ticker: &str, reason: String, to_height: u64) -> BlockHeaderStorageError {
BlockHeaderStorageError::UnableToDeleteHeaders {
to_height,
coin: ticker.to_string(),
reason,
}
}
}
#[async_trait]
pub trait BlockHeaderStorageOps: Send + Sync + 'static {
/// Initializes collection/tables in storage for a specified coin
async fn init(&self) -> Result<(), BlockHeaderStorageError>;
async fn is_initialized_for(&self) -> Result<bool, BlockHeaderStorageError>;
// Adds multiple block headers to the selected coin's header storage
// Should store it as `COIN_HEIGHT=hex_string`
// use this function for headers that comes from `blockchain_block_headers`
async fn add_block_headers_to_storage(
&self,
headers: HashMap<u64, BlockHeader>,
) -> Result<(), BlockHeaderStorageError>;
/// Gets the block header by height from the selected coin's storage as BlockHeader
async fn get_block_header(&self, height: u64) -> Result<Option<BlockHeader>, BlockHeaderStorageError>;
/// Gets the block header by height from the selected coin's storage as hex
async fn get_block_header_raw(&self, height: u64) -> Result<Option<String>, BlockHeaderStorageError>;
async fn get_last_block_height(&self) -> Result<Option<u64>, BlockHeaderStorageError>;
async fn get_last_block_header_with_non_max_bits(
&self,
max_bits: u32,
) -> Result<Option<BlockHeader>, BlockHeaderStorageError>;
async fn get_block_height_by_hash(&self, hash: H256) -> Result<Option<i64>, BlockHeaderStorageError>;
async fn remove_headers_up_to_height(&self, to_height: u64) -> Result<(), BlockHeaderStorageError>;
async fn is_table_empty(&self) -> Result<(), BlockHeaderStorageError>;
}