fuel_core_wasm_executor/
utils.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
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
use fuel_core_executor::executor::ExecutionOptions;
use fuel_core_storage::transactional::Changes;
use fuel_core_types::{
    blockchain::block::Block,
    services::{
        block_producer::Components,
        executor::{
            Error as ExecutorError,
            ExecutionResult,
            UncommittedResult,
            ValidationResult,
        },
        Uncommitted,
    },
};
#[cfg(feature = "std")]
use fuel_core_types_v0::services::executor::Error as ExecutorErrorV0;

/// Pack a pointer and length into an `u64`.
pub fn pack_ptr_and_len(ptr: u32, len: u32) -> u64 {
    (u64::from(len) << 32) | u64::from(ptr)
}

/// Unpacks an `u64` into the pointer and length.
pub fn unpack_ptr_and_len(val: u64) -> (u32, u32) {
    let ptr = u32::try_from(val & (u32::MAX as u64))
        .expect("It only contains first 32 bytes; qed");
    let len = u32::try_from(val >> 32).expect("It only contains first 32 bytes; qed");

    (ptr, len)
}

/// Pack a `exists`, `size` and `result` into one `u64`.
pub fn pack_exists_size_result(exists: bool, size: u32, result: u16) -> u64 {
    (u64::from(result) << 33 | u64::from(size) << 1) | u64::from(exists)
}

/// Unpacks an `u64` into `exists`, `size` and `result`.
pub fn unpack_exists_size_result(val: u64) -> (bool, u32, u16) {
    let exists = (val & 1u64) != 0;
    let size = u32::try_from((val >> 1) & (u32::MAX as u64))
        .expect("It only contains first 32 bytes; qed");
    let result = u16::try_from(val >> 33 & (u16::MAX as u64))
        .expect("It only contains first 16 bytes; qed");

    (exists, size, result)
}

/// The input type for the WASM executor. Enum allows handling different
/// versions of the input without introducing new host functions.
#[derive(Debug, serde::Serialize)]
pub enum InputSerializationType<'a> {
    V1 {
        block: WasmSerializationBlockTypes<'a, ()>,
        options: ExecutionOptions,
    },
}

#[derive(Debug, serde::Deserialize)]
pub enum InputDeserializationType {
    V1 {
        block: WasmDeserializationBlockTypes<()>,
        options: ExecutionOptions,
    },
}

#[derive(Debug, serde::Serialize)]
pub enum WasmSerializationBlockTypes<'a, TxSource> {
    /// DryRun mode where P is being produced.
    DryRun(Components<TxSource>),
    /// Production mode where P is being produced.
    Production(Components<TxSource>),
    /// Validation of a produced block.
    Validation(&'a Block),
}

#[derive(Debug, serde::Deserialize)]
pub enum WasmDeserializationBlockTypes<TxSource> {
    /// DryRun mode where P is being produced.
    DryRun(Components<TxSource>),
    /// Production mode where P is being produced.
    Production(Components<TxSource>),
    /// Validation of a produced block.
    Validation(Block),
}

/// The JSON version of the executor error. The serialization and deserialization
/// of the JSON error are less sensitive to the order of the variants in the enum.
/// It simplifies the error conversion between different versions of the execution.
///
/// If deserialization fails, it returns a string representation of the error that
/// still has useful information, even if the error is not supported by the native executor.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct JSONError(String);

#[cfg(feature = "std")]
impl From<ExecutorErrorV0> for JSONError {
    fn from(value: ExecutorErrorV0) -> Self {
        let json = serde_json::to_string(&value).unwrap_or_else(|e| {
            anyhow::anyhow!("Failed to serialize the V0 error: {:?}", e).to_string()
        });
        JSONError(json)
    }
}

impl From<ExecutorError> for JSONError {
    fn from(value: ExecutorError) -> Self {
        let json = serde_json::to_string(&value).unwrap_or_else(|e| {
            anyhow::anyhow!("Failed to serialize the error: {:?}", e).to_string()
        });
        JSONError(json)
    }
}

impl From<JSONError> for ExecutorError {
    fn from(value: JSONError) -> Self {
        serde_json::from_str(&value.0).unwrap_or(ExecutorError::Other(value.0))
    }
}

/// The return type for the WASM executor. Enum allows handling different
/// versions of the return without introducing new host functions.
#[cfg(feature = "std")]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub enum ReturnType {
    ExecutionV0(
        Result<Uncommitted<ExecutionResult<ExecutorErrorV0>, Changes>, ExecutorErrorV0>,
    ),
    ExecutionV1(Result<Uncommitted<ExecutionResult<JSONError>, Changes>, JSONError>),
    Validation(Result<Uncommitted<ValidationResult, Changes>, JSONError>),
}

/// The return type for the WASM executor. Enum allows handling different
/// versions of the return without introducing new host functions.
#[cfg(not(feature = "std"))]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub enum ReturnType {
    /// WASM executor doesn't use this variant, so from its perspective it is empty.
    ExecutionV0,
    ExecutionV1(Result<Uncommitted<ExecutionResult<JSONError>, Changes>, JSONError>),
    Validation(Result<Uncommitted<ValidationResult, Changes>, JSONError>),
}

/// Converts the latest execution result to the `ExecutionV1`.
pub fn convert_to_v1_execution_result(
    result: Result<UncommittedResult<Changes>, ExecutorError>,
) -> Result<Uncommitted<ExecutionResult<JSONError>, Changes>, JSONError> {
    result
        .map(|result| {
            let (result, changes) = result.into();
            let ExecutionResult {
                block,
                skipped_transactions,
                tx_status,
                events,
            } = result;

            let skipped_transactions: Vec<_> = skipped_transactions
                .into_iter()
                .map(|(id, error)| (id, JSONError::from(error)))
                .collect();

            let result = ExecutionResult {
                block,
                skipped_transactions,
                tx_status,
                events,
            };

            Uncommitted::new(result, changes)
        })
        .map_err(JSONError::from)
}

/// Converts the `ExecutionV1` to latest execution result.
pub fn convert_from_v1_execution_result(
    result: Result<Uncommitted<ExecutionResult<JSONError>, Changes>, JSONError>,
) -> Result<UncommittedResult<Changes>, ExecutorError> {
    result
        .map(|result| {
            let (result, changes) = result.into();
            let ExecutionResult {
                block,
                skipped_transactions,
                tx_status,
                events,
            } = result;

            let skipped_transactions: Vec<_> = skipped_transactions
                .into_iter()
                .map(|(id, error)| (id, ExecutorError::from(error)))
                .collect();

            let result = ExecutionResult {
                block,
                skipped_transactions,
                tx_status,
                events,
            };

            Uncommitted::new(result, changes)
        })
        .map_err(ExecutorError::from)
}

/// Converts the `ExecutionV0` to latest execution result.
#[cfg(feature = "std")]
pub fn convert_from_v0_execution_result(
    result: Result<
        Uncommitted<ExecutionResult<ExecutorErrorV0>, Changes>,
        ExecutorErrorV0,
    >,
) -> Result<UncommittedResult<Changes>, ExecutorError> {
    result
        .map(|result| {
            let (result, changes) = result.into();
            let ExecutionResult {
                block,
                skipped_transactions,
                tx_status,
                events,
            } = result;

            let skipped_transactions: Vec<_> = skipped_transactions
                .into_iter()
                .map(|(id, error)| (id, ExecutorError::from(JSONError::from(error))))
                .collect();

            let result = ExecutionResult {
                block,
                skipped_transactions,
                tx_status,
                events,
            };

            Uncommitted::new(result, changes)
        })
        .map_err(JSONError::from)
        .map_err(ExecutorError::from)
}

#[cfg(test)]
mod tests {
    use super::*;
    use fuel_core_types::services::executor::TransactionValidityError;
    #[cfg(feature = "std")]
    use fuel_core_types_v0::services::executor::TransactionValidityError as TransactionValidityErrorV0;
    use proptest::prelude::prop::*;

    proptest::proptest! {
        #[test]
        fn can_pack_any_values(exists: bool, size: u32, result: u16) {
            pack_exists_size_result(exists, size, result);
        }

        #[test]
        fn can_unpack_any_values(value: u64) {
            let _ = unpack_exists_size_result(value);
        }


        #[test]
        fn unpacks_packed_values(exists: bool, size: u32, result: u16) {
            let packed = pack_exists_size_result(exists, size, result);
            let (unpacked_exists, unpacked_size, unpacked_result) =
                unpack_exists_size_result(packed);

            proptest::prop_assert_eq!(exists, unpacked_exists);
            proptest::prop_assert_eq!(size, unpacked_size);
            proptest::prop_assert_eq!(result, unpacked_result);
        }
    }

    #[cfg(feature = "std")]
    #[test]
    fn can_convert_v0_error_to_v1() {
        // Given
        let v0 = ExecutorErrorV0::TransactionValidity(
            TransactionValidityErrorV0::CoinDoesNotExist(Default::default()),
        );

        // When
        let json: JSONError = v0.into();
        let v1: ExecutorError = json.into();

        // Then
        assert_eq!(
            v1,
            ExecutorError::TransactionValidity(
                TransactionValidityError::CoinDoesNotExist(Default::default())
            )
        );
    }
}