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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crate::{
common::{
fuel_tx::{
CheckError,
TxId,
UtxoId,
},
fuel_types::{
Bytes32,
ContractId,
MessageId,
},
fuel_vm::backtrace::Backtrace,
},
db::KvStoreError,
model::{
BlockId,
FuelBlock,
PartialFuelBlock,
},
txpool::TransactionStatus,
};
use fuel_vm::fuel_tx::Transaction;
use std::error::Error as StdError;
use thiserror::Error;
pub type ExecutionBlock = ExecutionTypes<PartialFuelBlock, FuelBlock>;
pub type ExecutionType<T> = ExecutionTypes<T, T>;
#[derive(Debug, Clone, Copy)]
pub enum ExecutionTypes<P, V> {
Production(P),
Validation(V),
}
#[derive(Debug)]
pub struct ExecutionResult {
pub block: FuelBlock,
pub skipped_transactions: Vec<(Transaction, Error)>,
pub tx_status: Vec<TransactionExecutionStatus>,
}
#[derive(Debug, Clone)]
pub struct TransactionExecutionStatus {
pub id: Bytes32,
pub status: TransactionStatus,
}
#[derive(Debug)]
pub struct UncommittedResult<DbTransaction> {
result: ExecutionResult,
database_transaction: DbTransaction,
}
impl<DbTransaction> UncommittedResult<DbTransaction> {
pub fn new(result: ExecutionResult, database_transaction: DbTransaction) -> Self {
Self {
result,
database_transaction,
}
}
pub fn result(&self) -> &ExecutionResult {
&self.result
}
pub fn into(self) -> (ExecutionResult, DbTransaction) {
(self.result, self.database_transaction)
}
pub fn into_result(self) -> ExecutionResult {
self.result
}
pub fn into_transaction(self) -> DbTransaction {
self.database_transaction
}
}
#[derive(Debug, Clone, Copy)]
pub enum ExecutionKind {
Production,
Validation,
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum TransactionValidityError {
#[error("Coin input was already spent")]
CoinAlreadySpent(UtxoId),
#[error("Coin has not yet reached maturity")]
CoinHasNotMatured(UtxoId),
#[error("The specified coin doesn't exist")]
CoinDoesNotExist(UtxoId),
#[error("The specified message was already spent")]
MessageAlreadySpent(MessageId),
#[error(
"Message is not yet spendable, as it's DA height is newer than this block allows"
)]
MessageSpendTooEarly(MessageId),
#[error("The specified message doesn't exist")]
MessageDoesNotExist(MessageId),
#[error("Contract output index isn't valid: {0:#x}")]
InvalidContractInputIndex(UtxoId),
#[error("The transaction must have at least one coin or message input type: {0:#x}")]
NoCoinOrMessageInput(TxId),
#[error("The transaction contains predicate inputs which aren't enabled: {0:#x}")]
PredicateExecutionDisabled(TxId),
#[error(
"The transaction contains a predicate which failed to validate: TransactionId({0:#x})"
)]
InvalidPredicate(TxId),
#[error("Transaction validity: {0:#?}")]
Validation(#[from] CheckError),
#[error("Datastore error occurred")]
DataStoreError(Box<dyn StdError + Send + Sync>),
}
impl From<KvStoreError> for TransactionValidityError {
fn from(e: KvStoreError) -> Self {
Self::DataStoreError(Box::new(e))
}
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("Transaction id was already used: {0:#x}")]
TransactionIdCollision(Bytes32),
#[error("output already exists")]
OutputAlreadyExists,
#[error("The computed fee caused an integer overflow")]
FeeOverflow,
#[error("Not supported transaction: {0:?}")]
NotSupportedTransaction(Box<Transaction>),
#[error("The first transaction in the block is not `Mint` - coinbase.")]
CoinbaseIsNotFirstTransaction,
#[error("Coinbase should have one output.")]
CoinbaseSeveralOutputs,
#[error("Coinbase outputs is invalid.")]
CoinbaseOutputIsInvalid,
#[error("Coinbase amount mismatches with expected.")]
CoinbaseAmountMismatch,
#[error("Invalid transaction: {0}")]
TransactionValidity(#[from] TransactionValidityError),
#[error("corrupted block state")]
CorruptedBlockState(Box<dyn StdError + Send + Sync>),
#[error("Transaction({transaction_id:#x}) execution error: {error:?}")]
VmExecution {
error: fuel_vm::prelude::InterpreterError,
transaction_id: Bytes32,
},
#[error(transparent)]
InvalidTransaction(#[from] CheckError),
#[error("Execution error with backtrace")]
Backtrace(Box<Backtrace>),
#[error("Transaction doesn't match expected result: {transaction_id:#x}")]
InvalidTransactionOutcome { transaction_id: Bytes32 },
#[error("Transaction root is invalid")]
InvalidTransactionRoot,
#[error("The amount of charged fees is invalid")]
InvalidFeeAmount,
#[error("Block id is invalid")]
InvalidBlockId,
#[error("No matching utxo for contract id ${0:#x}")]
ContractUtxoMissing(ContractId),
}
impl ExecutionBlock {
pub fn id(&self) -> Option<BlockId> {
match self {
ExecutionTypes::Production(_) => None,
ExecutionTypes::Validation(v) => Some(v.id()),
}
}
pub fn txs_root(&self) -> Option<Bytes32> {
match self {
ExecutionTypes::Production(_) => None,
ExecutionTypes::Validation(v) => Some(v.header().transactions_root),
}
}
}
impl<P, V> ExecutionTypes<P, V> {
pub fn map_p<Q, F>(self, f: F) -> ExecutionTypes<Q, V>
where
F: FnOnce(P) -> Q,
{
match self {
ExecutionTypes::Production(p) => ExecutionTypes::Production(f(p)),
ExecutionTypes::Validation(v) => ExecutionTypes::Validation(v),
}
}
pub fn map_v<W, F>(self, f: F) -> ExecutionTypes<P, W>
where
F: FnOnce(V) -> W,
{
match self {
ExecutionTypes::Production(p) => ExecutionTypes::Production(p),
ExecutionTypes::Validation(v) => ExecutionTypes::Validation(f(v)),
}
}
pub fn as_ref(&self) -> ExecutionTypes<&P, &V> {
match *self {
ExecutionTypes::Production(ref p) => ExecutionTypes::Production(p),
ExecutionTypes::Validation(ref v) => ExecutionTypes::Validation(v),
}
}
pub fn as_mut(&mut self) -> ExecutionTypes<&mut P, &mut V> {
match *self {
ExecutionTypes::Production(ref mut p) => ExecutionTypes::Production(p),
ExecutionTypes::Validation(ref mut v) => ExecutionTypes::Validation(v),
}
}
pub fn to_kind(&self) -> ExecutionKind {
match self {
ExecutionTypes::Production(_) => ExecutionKind::Production,
ExecutionTypes::Validation(_) => ExecutionKind::Validation,
}
}
}
impl<T> ExecutionType<T> {
pub fn map<U, F>(self, f: F) -> ExecutionType<U>
where
F: FnOnce(T) -> U,
{
match self {
ExecutionTypes::Production(p) => ExecutionTypes::Production(f(p)),
ExecutionTypes::Validation(v) => ExecutionTypes::Validation(f(v)),
}
}
pub fn filter_map<U, F>(self, f: F) -> Option<ExecutionType<U>>
where
F: FnOnce(T) -> Option<U>,
{
match self {
ExecutionTypes::Production(p) => f(p).map(ExecutionTypes::Production),
ExecutionTypes::Validation(v) => f(v).map(ExecutionTypes::Validation),
}
}
pub fn into_inner(self) -> T {
match self {
ExecutionTypes::Production(t) | ExecutionTypes::Validation(t) => t,
}
}
pub fn split(self) -> (ExecutionKind, T) {
let kind = self.to_kind();
(kind, self.into_inner())
}
}
impl ExecutionKind {
pub fn wrap<T>(self, t: T) -> ExecutionType<T> {
match self {
ExecutionKind::Production => ExecutionTypes::Production(t),
ExecutionKind::Validation => ExecutionTypes::Validation(t),
}
}
}
impl From<Backtrace> for Error {
fn from(e: Backtrace) -> Self {
Error::Backtrace(Box::new(e))
}
}
impl From<KvStoreError> for Error {
fn from(e: KvStoreError) -> Self {
Error::CorruptedBlockState(Box::new(e))
}
}
impl From<crate::db::Error> for Error {
fn from(e: crate::db::Error) -> Self {
Error::CorruptedBlockState(Box::new(e))
}
}
impl<T> core::ops::Deref for ExecutionType<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
ExecutionTypes::Production(p) => p,
ExecutionTypes::Validation(v) => v,
}
}
}
impl<T> core::ops::DerefMut for ExecutionType<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
ExecutionTypes::Production(p) => p,
ExecutionTypes::Validation(v) => v,
}
}
}