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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::{
common::{
fuel_storage::Mappable,
fuel_tx::{
Receipt,
Transaction,
UtxoId,
},
fuel_types::{
Bytes32,
ContractId,
MessageId,
},
fuel_vm::prelude::InterpreterError,
},
model::{
Coin,
FuelBlockConsensus,
FuelBlockDb,
Message,
},
};
use std::io::ErrorKind;
use thiserror::Error;
pub use crate::common::fuel_vm::storage::{
ContractsAssets,
ContractsInfo,
ContractsRawCode,
ContractsState,
};
pub trait Transactional {
fn commit(self) -> Result<(), Error>;
fn commit_box(self: Box<Self>) -> Result<(), Error>;
}
pub trait DatabaseTransaction<Database>:
Transactional + core::fmt::Debug + Send + Sync
{
fn database(&self) -> &Database;
fn database_mut(&mut self) -> &mut Database;
}
pub struct FuelBlocks;
impl Mappable for FuelBlocks {
type Key = Bytes32;
type SetValue = FuelBlockDb;
type GetValue = Self::SetValue;
}
pub struct ContractsLatestUtxo;
impl Mappable for ContractsLatestUtxo {
type Key = ContractId;
type SetValue = UtxoId;
type GetValue = Self::SetValue;
}
pub struct Receipts;
impl Mappable for Receipts {
type Key = Bytes32;
type SetValue = [Receipt];
type GetValue = Vec<Receipt>;
}
pub struct SealedBlockConsensus;
impl Mappable for SealedBlockConsensus {
type Key = Bytes32;
type SetValue = FuelBlockConsensus;
type GetValue = Self::SetValue;
}
pub struct Coins;
impl Mappable for Coins {
type Key = UtxoId;
type SetValue = Coin;
type GetValue = Self::SetValue;
}
pub struct Messages;
impl Mappable for Messages {
type Key = MessageId;
type SetValue = Message;
type GetValue = Self::SetValue;
}
pub struct Transactions;
impl Mappable for Transactions {
type Key = Bytes32;
type SetValue = Transaction;
type GetValue = Self::SetValue;
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("error performing binary serialization")]
Codec,
#[error("Failed to initialize chain")]
ChainAlreadyInitialized,
#[error("Chain is not yet initialized")]
ChainUninitialized,
#[error("Invalid database version")]
InvalidDatabaseVersion,
#[error("error occurred in the underlying datastore `{0}`")]
DatabaseError(Box<dyn std::error::Error + Send + Sync>),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl From<Error> for std::io::Error {
fn from(e: Error) -> Self {
std::io::Error::new(ErrorKind::Other, e)
}
}
#[derive(Debug, Error)]
pub enum KvStoreError {
#[error("generic error occurred")]
Error(Box<dyn std::error::Error + Send + Sync>),
#[error("resource of type `{0}` was not found at the: {1}")]
NotFound(&'static str, &'static str),
}
#[macro_export]
macro_rules! not_found {
($name: literal) => {
$crate::db::KvStoreError::NotFound($name, concat!(file!(), ":", line!()))
};
($ty: path) => {
$crate::db::KvStoreError::NotFound(
::core::any::type_name::<
<$ty as $crate::common::fuel_storage::Mappable>::GetValue,
>(),
concat!(file!(), ":", line!()),
)
};
}
impl From<Error> for KvStoreError {
fn from(e: Error) -> Self {
KvStoreError::Error(Box::new(e))
}
}
impl From<KvStoreError> for Error {
fn from(e: KvStoreError) -> Self {
Error::DatabaseError(Box::new(e))
}
}
impl From<KvStoreError> for std::io::Error {
fn from(e: KvStoreError) -> Self {
std::io::Error::new(ErrorKind::Other, e)
}
}
impl From<Error> for InterpreterError {
fn from(e: Error) -> Self {
InterpreterError::Io(std::io::Error::new(std::io::ErrorKind::Other, e))
}
}
impl From<KvStoreError> for InterpreterError {
fn from(e: KvStoreError) -> Self {
InterpreterError::Io(std::io::Error::new(std::io::ErrorKind::Other, e))
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn not_found_output() {
#[rustfmt::skip]
assert_eq!(
format!("{}", not_found!("BlockId")),
format!("resource of type `BlockId` was not found at the: {}:{}", file!(), line!() - 1)
);
#[rustfmt::skip]
assert_eq!(
format!("{}", not_found!(Coins)),
format!("resource of type `fuel_core_interfaces::model::coin::Coin` was not found at the: {}:{}", file!(), line!() - 1)
);
}
}