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
use lnpbp::bitcoin;
use lnpbp::rgb::prelude::*;
use std::collections::BTreeMap;
use super::sql::SqlCacheError;
use super::FileCacheError;
use crate::error::{BootstrapError, ServiceErrorDomain};
use crate::fungible::Asset;
use crate::util::file::FileMode;
pub trait Cache {
type Error: ::std::error::Error + Into<ServiceErrorDomain>;
fn assets(&self) -> Result<Vec<&Asset>, Self::Error>;
fn asset(&self, id: ContractId) -> Result<&Asset, Self::Error>;
fn has_asset(&self, id: ContractId) -> Result<bool, Self::Error>;
fn add_asset(&mut self, asset: Asset) -> Result<bool, Self::Error>;
fn remove_asset(&mut self, id: ContractId) -> Result<bool, Self::Error>;
fn asset_allocations(
&self,
contract_id: ContractId,
) -> Result<BTreeMap<bitcoin::OutPoint, Vec<AtomicValue>>, Self::Error>;
fn outpoint_assets(
&self,
outpoint: bitcoin::OutPoint,
) -> Result<BTreeMap<ContractId, Vec<AtomicValue>>, CacheError>;
}
#[derive(Clone, PartialEq, Eq, Debug, Display, Error)]
#[display(Debug)]
pub enum CacheError {
Io(String),
NotFound {
id: String,
},
DataAccessError {
id: String,
mode: FileMode,
details: Option<String>,
},
DataIntegrityError(String),
Sqlite(String),
}
impl From<CacheError> for ServiceErrorDomain {
fn from(_: CacheError) -> Self {
ServiceErrorDomain::Cache
}
}
impl From<CacheError> for BootstrapError {
fn from(_: CacheError) -> Self {
BootstrapError::CacheError
}
}
impl From<FileCacheError> for CacheError {
fn from(err: FileCacheError) -> Self {
match err {
FileCacheError::Io(e) => Self::Io(format!("{:?}", e)),
FileCacheError::HashName => Self::DataIntegrityError(
"File for a given hash id is not found".to_string(),
),
FileCacheError::Encoding(e) => {
Self::DataIntegrityError(format!("{:?}", e))
}
FileCacheError::BrokenHexFilenames => Self::DataIntegrityError(
"Broken filename structure in storage".to_string(),
),
#[cfg(feature = "serde_json")]
FileCacheError::SerdeJson(e) => {
Self::DataIntegrityError(format!("{:?}", e))
}
#[cfg(feature = "serde_yaml")]
FileCacheError::SerdeYaml(e) => {
Self::DataIntegrityError(format!("{:?}", e))
}
#[cfg(feature = "toml")]
FileCacheError::SerdeToml => Self::DataIntegrityError(format!(
"TOML serialization/deserialization error"
)),
FileCacheError::NotFound => {
Self::DataIntegrityError("Data file is not found".to_string())
}
}
}
}
impl From<SqlCacheError> for CacheError {
fn from(err: SqlCacheError) -> Self {
match err {
SqlCacheError::Io(e) => Self::Io(format!("{:?}", e)),
SqlCacheError::Sqlite(e) => {
Self::Sqlite(format!("Error from sqlite asset cache {}", e.to_string()))
}
SqlCacheError::HexDecoding(_) => Self::DataIntegrityError(format!(
"Wrong hex encoded data in sqlite asset cache table"
)),
SqlCacheError::Generic(e) => Self::DataIntegrityError(e),
SqlCacheError::WrongChainData(e) => Self::DataIntegrityError(format!(
"Wrong Chain data in sqlite asset cache table: {}",
e
)),
SqlCacheError::NotFound => {
Self::DataIntegrityError(format!("Asset cache sqlite database file not found"))
}
SqlCacheError::BlindKey(e) => Self::DataIntegrityError(format!(
"Wrong amount blinding factor in asset cache sqlite database: {}",
e
)),
SqlCacheError::Bech32(e) => Self::DataIntegrityError(e.to_string()),
}
}
}